GApplication: add chain-up checks
[platform/upstream/glib.git] / gio / gapplication.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 /* Prologue {{{1 */
23 #include "config.h"
24
25 #include "gapplication.h"
26
27 #include "gapplicationcommandline.h"
28 #include "gapplicationimpl.h"
29 #include "gactiongroup.h"
30 #include "gsettings.h"
31
32 #include "gioenumtypes.h"
33 #include "gioenums.h"
34 #include "gfile.h"
35
36 #include "glibintl.h"
37
38 #include <string.h>
39
40 /**
41  * SECTION:gapplication
42  * @title: GApplication
43  * @short_description: Core application class
44  *
45  * A #GApplication is the foundation of an application, unique for a
46  * given application identifier.  The GApplication class wraps some
47  * low-level platform-specific services and is intended to act as the
48  * foundation for higher-level application classes such as
49  * #GtkApplication or #MxApplication.  In general, you should not use
50  * this class outside of a higher level framework.
51  *
52  * One of the core features that GApplication provides is process
53  * uniqueness, in the context of a "session".  The session concept is
54  * platform-dependent, but corresponds roughly to a graphical desktop
55  * login.  When your application is launched again, its arguments
56  * are passed through platform communication to the already running
57  * program. The already running instance of the program is called the
58  * <firstterm>primary instance</firstterm>.
59  *
60  * Before using GApplication, you must choose an "application identifier".
61  * The expected form of an application identifier is very close to that of
62  * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
63  * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
64  * For details on valid application identifiers, see
65  * g_application_id_is_valid().
66  *
67  * The application identifier is claimed by the application as a
68  * well-known bus name on the user's session bus.  This means that the
69  * uniqueness of your application is scoped to the current session.  It
70  * also means that your application may provide additional services
71  * (through registration of other object paths) at that bus name.
72  *
73  * The registration of these object paths should be done with the shared
74  * GDBus session bus.  Note that due to the internal architecture of
75  * GDBus, method calls can be dispatched at any time (even if a main
76  * loop is not running).  For this reason, you must ensure that any
77  * object paths that you wish to register are registered before
78  * #GApplication attempts to acquire the bus name of your application
79  * (which happens in g_application_register()).  Unfortunately, this
80  * means that you cannot use g_application_get_is_remote() to decide if
81  * you want to register object paths.
82  *
83  * GApplication provides convenient life cycle management by maintaining
84  * a <firstterm>use count</firstterm> for the primary application instance.
85  * The use count can be changed using g_application_hold() and
86  * g_application_release(). If it drops to zero, the application exits.
87  *
88  * GApplication also implements the #GActionGroup interface and lets you
89  * easily export actions by adding them with g_application_set_action_group().
90  * When invoking an action by calling g_action_group_activate_action() on
91  * the application, it is always invoked in the primary instance.
92  *
93  * There is a number of different entry points into a #GApplication:
94  * <itemizedlist>
95  * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
96  * <listitem>via 'Open' (i.e. opening some files)</listitem>
97  * <listitem>by handling a command-line</listitem>
98  * <listitem>via activating an action</listitem>
99  * </itemizedlist>
100  * The #GApplication::startup signal lets you handle the application
101  * initialization for all of these in a single place.
102  *
103  * Regardless of which of these entry points is used to start the application,
104  * GApplication passes some <firstterm id="platform-data">platform
105  * data</firstterm> from the launching instance to the primary instance,
106  * in the form of a #GVariant dictionary mapping strings to variants.
107  * To use platform data, override the @before_emit or @after_emit virtual
108  * functions in your #GApplication subclass. When dealing with
109  * #GApplicationCommandline objects, the platform data is directly
110  * available via g_application_command_line_get_cwd(),
111  * g_application_command_line_get_environ() and
112  * g_application_command_line_get_platform_data().
113  *
114  * As the name indicates, the platform data may vary depending on the
115  * operating system, but it always includes the current directory (key
116  * "cwd"), and optionally the environment (ie the set of environment
117  * variables and their values) of the calling process (key "environ").
118  * The environment is only added to the platform data if the
119  * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
120  * can add their own platform data by overriding the @add_platform_data
121  * virtual function. For instance, #GtkApplication adds startup notification
122  * data in this way.
123  *
124  * To parse commandline arguments you may handle the
125  * #GApplication::command-line signal or override the local_command_line()
126  * vfunc, to parse them in either the primary instance or the local instance,
127  * respectively.
128  *
129  * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
130  * <programlisting>
131  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
132  *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
133  * </xi:include>
134  * </programlisting>
135  * </example>
136  *
137  * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
138  * <programlisting>
139  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
140  *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
141  * </xi:include>
142  * </programlisting>
143  * </example>
144  */
145
146 struct _GApplicationPrivate
147 {
148   GApplicationFlags  flags;
149   gchar             *id;
150
151   GActionGroup      *actions;
152   GMainLoop         *mainloop;
153
154   guint              inactivity_timeout_id;
155   guint              inactivity_timeout;
156   guint              use_count;
157
158   guint              is_registered : 1;
159   guint              is_remote : 1;
160   guint              did_startup : 1;
161   guint              did_shutdown : 1;
162
163   GHashTable        *remote_actions;  /* string -> RemoteActionInfo */
164   GApplicationImpl  *impl;
165 };
166
167 enum
168 {
169   PROP_NONE,
170   PROP_APPLICATION_ID,
171   PROP_FLAGS,
172   PROP_IS_REGISTERED,
173   PROP_IS_REMOTE,
174   PROP_INACTIVITY_TIMEOUT,
175   PROP_ACTION_GROUP
176 };
177
178 enum
179 {
180   SIGNAL_STARTUP,
181   SIGNAL_SHUTDOWN,
182   SIGNAL_ACTIVATE,
183   SIGNAL_OPEN,
184   SIGNAL_ACTION,
185   SIGNAL_COMMAND_LINE,
186   NR_SIGNALS
187 };
188
189 static guint g_application_signals[NR_SIGNALS];
190
191 static void g_application_action_group_iface_init (GActionGroupInterface *);
192 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
193  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
194    g_application_action_group_iface_init))
195
196 /* vfunc defaults {{{1 */
197 static void
198 g_application_real_before_emit (GApplication *application,
199                                 GVariant     *platform_data)
200 {
201 }
202
203 static void
204 g_application_real_after_emit (GApplication *application,
205                                GVariant     *platform_data)
206 {
207 }
208
209 static void
210 g_application_real_startup (GApplication *application)
211 {
212   application->priv->did_startup = TRUE;
213 }
214
215 static void
216 g_application_real_shutdown (GApplication *application)
217 {
218   application->priv->did_shutdown = TRUE;
219 }
220
221 static void
222 g_application_real_activate (GApplication *application)
223 {
224   if (!g_signal_has_handler_pending (application,
225                                      g_application_signals[SIGNAL_ACTIVATE],
226                                      0, TRUE) &&
227       G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
228     {
229       static gboolean warned;
230
231       if (warned)
232         return;
233
234       g_warning ("Your application does not implement "
235                  "g_application_activate() and has no handlers connected "
236                  "to the 'activate' signal.  It should do one of these.");
237       warned = TRUE;
238     }
239 }
240
241 static void
242 g_application_real_open (GApplication  *application,
243                          GFile        **files,
244                          gint           n_files,
245                          const gchar   *hint)
246 {
247   if (!g_signal_has_handler_pending (application,
248                                      g_application_signals[SIGNAL_OPEN],
249                                      0, TRUE) &&
250       G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
251     {
252       static gboolean warned;
253
254       if (warned)
255         return;
256
257       g_warning ("Your application claims to support opening files "
258                  "but does not implement g_application_open() and has no "
259                  "handlers connected to the 'open' signal.");
260       warned = TRUE;
261     }
262 }
263
264 static int
265 g_application_real_command_line (GApplication            *application,
266                                  GApplicationCommandLine *cmdline)
267 {
268   if (!g_signal_has_handler_pending (application,
269                                      g_application_signals[SIGNAL_COMMAND_LINE],
270                                      0, TRUE) &&
271       G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
272     {
273       static gboolean warned;
274
275       if (warned)
276         return 1;
277
278       g_warning ("Your application claims to support custom command line "
279                  "handling but does not implement g_application_command_line() "
280                  "and has no handlers connected to the 'command-line' signal.");
281
282       warned = TRUE;
283     }
284
285     return 1;
286 }
287
288 static gboolean
289 g_application_real_local_command_line (GApplication   *application,
290                                        gchar        ***arguments,
291                                        int            *exit_status)
292 {
293   if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
294     return FALSE;
295
296   else
297     {
298       GError *error = NULL;
299       gint n_args;
300
301       if (!g_application_register (application, NULL, &error))
302         {
303           g_critical ("%s", error->message);
304           g_error_free (error);
305           *exit_status = 1;
306           return TRUE;
307         }
308
309       n_args = g_strv_length (*arguments);
310
311       if (application->priv->flags & G_APPLICATION_IS_SERVICE)
312         {
313           if ((*exit_status = n_args > 1))
314             {
315               g_printerr ("GApplication service mode takes no arguments.\n");
316               application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
317             }
318
319           return TRUE;
320         }
321
322       if (n_args <= 1)
323         {
324           g_application_activate (application);
325           *exit_status = 0;
326         }
327
328       else
329         {
330           if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
331             {
332               g_critical ("This application can not open files.");
333               *exit_status = 1;
334             }
335           else
336             {
337               GFile **files;
338               gint n_files;
339               gint i;
340
341               n_files = n_args - 1;
342               files = g_new (GFile *, n_files);
343
344               for (i = 0; i < n_files; i++)
345                 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
346
347               g_application_open (application, files, n_files, "");
348
349               for (i = 0; i < n_files; i++)
350                 g_object_unref (files[i]);
351               g_free (files);
352
353               *exit_status = 0;
354             }
355         }
356
357       return TRUE;
358     }
359 }
360
361 static void
362 g_application_real_add_platform_data (GApplication    *application,
363                                       GVariantBuilder *builder)
364 {
365 }
366
367 static void
368 g_application_real_quit_mainloop (GApplication *application)
369 {
370   if (application->priv->mainloop != NULL)
371     g_main_loop_quit (application->priv->mainloop);
372 }
373
374 static void
375 g_application_real_run_mainloop (GApplication *application)
376 {
377   if (application->priv->mainloop == NULL)
378     application->priv->mainloop = g_main_loop_new (NULL, FALSE);
379
380   g_main_loop_run (application->priv->mainloop);
381 }
382
383 /* GObject implementation stuff {{{1 */
384 static void
385 g_application_set_property (GObject      *object,
386                             guint         prop_id,
387                             const GValue *value,
388                             GParamSpec   *pspec)
389 {
390   GApplication *application = G_APPLICATION (object);
391
392   switch (prop_id)
393     {
394     case PROP_APPLICATION_ID:
395       g_application_set_application_id (application,
396                                         g_value_get_string (value));
397       break;
398
399     case PROP_FLAGS:
400       g_application_set_flags (application, g_value_get_flags (value));
401       break;
402
403     case PROP_INACTIVITY_TIMEOUT:
404       g_application_set_inactivity_timeout (application,
405                                             g_value_get_uint (value));
406       break;
407
408     case PROP_ACTION_GROUP:
409       g_application_set_action_group (application,
410                                       g_value_get_object (value));
411       break;
412
413     default:
414       g_assert_not_reached ();
415     }
416 }
417
418 /**
419  * g_application_set_action_group:
420  * @application: a #GApplication
421  * @action_group: (allow-none): a #GActionGroup, or %NULL
422  *
423  * Sets or unsets the group of actions associated with the application.
424  *
425  * These actions are the actions that can be remotely invoked.
426  *
427  * It is an error to call this function after the application has been
428  * registered.
429  *
430  * Since: 2.28
431  **/
432 void
433 g_application_set_action_group (GApplication *application,
434                                 GActionGroup *action_group)
435 {
436   g_return_if_fail (G_IS_APPLICATION (application));
437   g_return_if_fail (!application->priv->is_registered);
438
439   if (application->priv->actions != NULL)
440     g_object_unref (application->priv->actions);
441
442   application->priv->actions = action_group;
443
444   if (application->priv->actions != NULL)
445     g_object_ref (application->priv->actions);
446 }
447
448 static void
449 g_application_get_property (GObject    *object,
450                             guint       prop_id,
451                             GValue     *value,
452                             GParamSpec *pspec)
453 {
454   GApplication *application = G_APPLICATION (object);
455
456   switch (prop_id)
457     {
458     case PROP_APPLICATION_ID:
459       g_value_set_string (value,
460                           g_application_get_application_id (application));
461       break;
462
463     case PROP_FLAGS:
464       g_value_set_flags (value,
465                          g_application_get_flags (application));
466       break;
467
468     case PROP_IS_REGISTERED:
469       g_value_set_boolean (value,
470                            g_application_get_is_registered (application));
471       break;
472
473     case PROP_IS_REMOTE:
474       g_value_set_boolean (value,
475                            g_application_get_is_remote (application));
476       break;
477
478     case PROP_INACTIVITY_TIMEOUT:
479       g_value_set_uint (value,
480                         g_application_get_inactivity_timeout (application));
481       break;
482
483     default:
484       g_assert_not_reached ();
485     }
486 }
487
488 static void
489 g_application_constructed (GObject *object)
490 {
491   GApplication *application = G_APPLICATION (object);
492
493   g_assert (application->priv->id != NULL);
494 }
495
496 static void
497 g_application_finalize (GObject *object)
498 {
499   GApplication *application = G_APPLICATION (object);
500
501   if (application->priv->impl)
502     g_application_impl_destroy (application->priv->impl);
503   g_free (application->priv->id);
504
505   if (application->priv->mainloop)
506     g_main_loop_unref (application->priv->mainloop);
507
508   G_OBJECT_CLASS (g_application_parent_class)
509     ->finalize (object);
510 }
511
512 static void
513 g_application_init (GApplication *application)
514 {
515   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
516                                                    G_TYPE_APPLICATION,
517                                                    GApplicationPrivate);
518 }
519
520 static void
521 g_application_class_init (GApplicationClass *class)
522 {
523   GObjectClass *object_class = G_OBJECT_CLASS (class);
524
525   object_class->constructed = g_application_constructed;
526   object_class->finalize = g_application_finalize;
527   object_class->get_property = g_application_get_property;
528   object_class->set_property = g_application_set_property;
529
530   class->before_emit = g_application_real_before_emit;
531   class->after_emit = g_application_real_after_emit;
532   class->startup = g_application_real_startup;
533   class->shutdown = g_application_real_shutdown;
534   class->activate = g_application_real_activate;
535   class->open = g_application_real_open;
536   class->command_line = g_application_real_command_line;
537   class->local_command_line = g_application_real_local_command_line;
538   class->add_platform_data = g_application_real_add_platform_data;
539   class->quit_mainloop = g_application_real_quit_mainloop;
540   class->run_mainloop = g_application_real_run_mainloop;
541
542   g_object_class_install_property (object_class, PROP_APPLICATION_ID,
543     g_param_spec_string ("application-id",
544                          P_("Application identifier"),
545                          P_("The unique identifier for the application"),
546                          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
547                          G_PARAM_STATIC_STRINGS));
548
549   g_object_class_install_property (object_class, PROP_FLAGS,
550     g_param_spec_flags ("flags",
551                         P_("Application flags"),
552                         P_("Flags specifying the behaviour of the application"),
553                         G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
554                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
555
556   g_object_class_install_property (object_class, PROP_IS_REGISTERED,
557     g_param_spec_boolean ("is-registered",
558                           P_("Is registered"),
559                           P_("If g_application_register() has been called"),
560                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
561
562   g_object_class_install_property (object_class, PROP_IS_REMOTE,
563     g_param_spec_boolean ("is-remote",
564                           P_("Is remote"),
565                           P_("If this application instance is remote"),
566                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
567
568   g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
569     g_param_spec_uint ("inactivity-timeout",
570                        P_("Inactivity timeout"),
571                        P_("Time (ms) to stay alive after becoming idle"),
572                        0, G_MAXUINT, 0,
573                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574
575   g_object_class_install_property (object_class, PROP_ACTION_GROUP,
576     g_param_spec_object ("action-group",
577                          P_("Action group"),
578                          P_("The group of actions that the application exports"),
579                          G_TYPE_ACTION_GROUP,
580                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
581
582   /**
583    * GApplication::startup:
584    * @application: the application
585    *
586    * The ::startup signal is emitted on the primary instance immediately
587    * after registration. See g_application_register().
588    */
589   g_application_signals[SIGNAL_STARTUP] =
590     g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
591                   G_STRUCT_OFFSET (GApplicationClass, startup),
592                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
593
594   /**
595    * GApplication::shutdown:
596    * @application: the application
597    *
598    * The ::shutdown signal is emitted only on the registered primary instance
599    * immediately after the main loop terminates.
600    */
601   g_application_signals[SIGNAL_SHUTDOWN] =
602     g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
603                   G_STRUCT_OFFSET (GApplicationClass, shutdown),
604                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
605
606   /**
607    * GApplication::activate:
608    * @application: the application
609    *
610    * The ::activate signal is emitted on the primary instance when an
611    * activation occurs. See g_application_activate().
612    */
613   g_application_signals[SIGNAL_ACTIVATE] =
614     g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
615                   G_STRUCT_OFFSET (GApplicationClass, activate),
616                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
617
618
619   /**
620    * GApplication::open:
621    * @application: the application
622    * @files: (array length=n_files) (element-type GFile): an array of #GFiles
623    * @n_files: the length of @files
624    * @hint: a hint provided by the calling instance
625    *
626    * The ::open signal is emitted on the primary instance when there are
627    * files to open. See g_application_open() for more information.
628    */
629   g_application_signals[SIGNAL_OPEN] =
630     g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
631                   G_STRUCT_OFFSET (GApplicationClass, open),
632                   NULL, NULL, NULL,
633                   G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
634
635   /**
636    * GApplication::command-line:
637    * @application: the application
638    * @command_line: a #GApplicationCommandLine representing the
639    *     passed commandline
640    *
641    * The ::command-line signal is emitted on the primary instance when
642    * a commandline is not handled locally. See g_application_run() and
643    * the #GApplicationCommandline documentation for more information.
644    *
645    * Returns: An integer that is set as the exit status for the calling
646    *   process. See g_application_command_line_set_exit_status().
647    */
648   g_application_signals[SIGNAL_COMMAND_LINE] =
649     g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
650                   G_STRUCT_OFFSET (GApplicationClass, command_line),
651                   g_signal_accumulator_first_wins, NULL,
652                   NULL,
653                   G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
654
655   g_type_class_add_private (class, sizeof (GApplicationPrivate));
656 }
657
658 static GVariant *
659 get_platform_data (GApplication *application)
660 {
661   GVariantBuilder *builder;
662   GVariant *result;
663
664   builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
665
666   {
667     gchar *cwd = g_get_current_dir ();
668     g_variant_builder_add (builder, "{sv}", "cwd",
669                            g_variant_new_bytestring (cwd));
670     g_free (cwd);
671   }
672
673   if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
674     {
675       GVariant *array;
676       gchar **envp;
677
678       envp = g_get_environ ();
679       array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
680       g_strfreev (envp);
681
682       g_variant_builder_add (builder, "{sv}", "environ", array);
683     }
684
685   G_APPLICATION_GET_CLASS (application)->
686     add_platform_data (application, builder);
687
688   result = g_variant_builder_end (builder);
689   g_variant_builder_unref (builder);
690
691   return result;
692 }
693
694 /* Application ID validity {{{1 */
695
696 /**
697  * g_application_id_is_valid:
698  * @application_id: a potential application identifier
699  * @returns: %TRUE if @application_id is valid
700  *
701  * Checks if @application_id is a valid application identifier.
702  *
703  * A valid ID is required for calls to g_application_new() and
704  * g_application_set_application_id().
705  *
706  * For convenience, the restrictions on application identifiers are
707  * reproduced here:
708  * <itemizedlist>
709  *   <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
710  *   <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
711  *   <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
712  *   <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
713  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
714  * </itemizedlist>
715  **/
716 gboolean
717 g_application_id_is_valid (const gchar *application_id)
718 {
719   gsize len;
720   gboolean allow_dot;
721   gboolean has_dot;
722
723   len = strlen (application_id);
724
725   if (len > 255)
726     return FALSE;
727
728   if (!g_ascii_isalpha (application_id[0]))
729     return FALSE;
730
731   if (application_id[len-1] == '.')
732     return FALSE;
733
734   application_id++;
735   allow_dot = TRUE;
736   has_dot = FALSE;
737   for (; *application_id; application_id++)
738     {
739       if (g_ascii_isalnum (*application_id) ||
740           (*application_id == '-') ||
741           (*application_id == '_'))
742         {
743           allow_dot = TRUE;
744         }
745       else if (allow_dot && *application_id == '.')
746         {
747           has_dot = TRUE;
748           allow_dot = FALSE;
749         }
750       else
751         return FALSE;
752     }
753
754   if (!has_dot)
755     return FALSE;
756
757   return TRUE;
758 }
759
760 /* Public Constructor {{{1 */
761 /**
762  * g_application_new:
763  * @application_id: the application id
764  * @flags: the application flags
765  * @returns: a new #GApplication instance
766  *
767  * Creates a new #GApplication instance.
768  *
769  * This function calls g_type_init() for you.
770  *
771  * The application id must be valid.  See g_application_id_is_valid().
772  **/
773 GApplication *
774 g_application_new (const gchar       *application_id,
775                    GApplicationFlags  flags)
776 {
777   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
778
779   g_type_init ();
780
781   return g_object_new (G_TYPE_APPLICATION,
782                        "application-id", application_id,
783                        "flags", flags,
784                        NULL);
785 }
786
787 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
788 /**
789  * g_application_get_application_id:
790  * @application: a #GApplication
791  * @returns: the identifier for @application, owned by @application
792  *
793  * Gets the unique identifier for @application.
794  *
795  * Since: 2.28
796  **/
797 const gchar *
798 g_application_get_application_id (GApplication *application)
799 {
800   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
801
802   return application->priv->id;
803 }
804
805 /**
806  * g_application_set_application_id:
807  * @application: a #GApplication
808  * @application_id: the identifier for @application
809  *
810  * Sets the unique identifier for @application.
811  *
812  * The application id can only be modified if @application has not yet
813  * been registered.
814  *
815  * The application id must be valid.  See g_application_id_is_valid().
816  *
817  * Since: 2.28
818  **/
819 void
820 g_application_set_application_id (GApplication *application,
821                                   const gchar  *application_id)
822 {
823   g_return_if_fail (G_IS_APPLICATION (application));
824
825   if (g_strcmp0 (application->priv->id, application_id) != 0)
826     {
827       g_return_if_fail (g_application_id_is_valid (application_id));
828       g_return_if_fail (!application->priv->is_registered);
829
830       g_free (application->priv->id);
831       application->priv->id = g_strdup (application_id);
832
833       g_object_notify (G_OBJECT (application), "application-id");
834     }
835 }
836
837 /**
838  * g_application_get_flags:
839  * @application: a #GApplication
840  * @returns: the flags for @application
841  *
842  * Gets the flags for @application.
843  *
844  * See #GApplicationFlags.
845  *
846  * Since: 2.28
847  **/
848 GApplicationFlags
849 g_application_get_flags (GApplication *application)
850 {
851   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
852
853   return application->priv->flags;
854 }
855
856 /**
857  * g_application_set_flags:
858  * @application: a #GApplication
859  * @flags: the flags for @application
860  *
861  * Sets the flags for @application.
862  *
863  * The flags can only be modified if @application has not yet been
864  * registered.
865  *
866  * See #GApplicationFlags.
867  *
868  * Since: 2.28
869  **/
870 void
871 g_application_set_flags (GApplication      *application,
872                          GApplicationFlags  flags)
873 {
874   g_return_if_fail (G_IS_APPLICATION (application));
875
876   if (application->priv->flags != flags)
877     {
878       g_return_if_fail (!application->priv->is_registered);
879
880       application->priv->flags = flags;
881
882       g_object_notify (G_OBJECT (application), "flags");
883     }
884 }
885
886 /**
887  * g_application_get_inactivity_timeout:
888  * @application: a #GApplication
889  *
890  * Gets the current inactivity timeout for the application.
891  *
892  * This is the amount of time (in milliseconds) after the last call to
893  * g_application_release() before the application stops running.
894  *
895  * Returns: the timeout, in milliseconds
896  *
897  * Since: 2.28
898  **/
899 guint
900 g_application_get_inactivity_timeout (GApplication *application)
901 {
902   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
903
904   return application->priv->inactivity_timeout;
905 }
906
907 /**
908  * g_application_set_inactivity_timeout:
909  * @application: a #GApplication
910  * @inactivity_timeout: the timeout, in milliseconds
911  *
912  * Sets the current inactivity timeout for the application.
913  *
914  * This is the amount of time (in milliseconds) after the last call to
915  * g_application_release() before the application stops running.
916  *
917  * This call has no side effects of its own.  The value set here is only
918  * used for next time g_application_release() drops the use count to
919  * zero.  Any timeouts currently in progress are not impacted.
920  *
921  * Since: 2.28
922  **/
923 void
924 g_application_set_inactivity_timeout (GApplication *application,
925                                       guint         inactivity_timeout)
926 {
927   g_return_if_fail (G_IS_APPLICATION (application));
928
929   if (application->priv->inactivity_timeout != inactivity_timeout)
930     {
931       application->priv->inactivity_timeout = inactivity_timeout;
932
933       g_object_notify (G_OBJECT (application), "inactivity-timeout");
934     }
935 }
936 /* Read-only property getters (is registered, is remote) {{{1 */
937 /**
938  * g_application_get_is_registered:
939  * @application: a #GApplication
940  * @returns: %TRUE if @application is registered
941  *
942  * Checks if @application is registered.
943  *
944  * An application is registered if g_application_register() has been
945  * successfully called.
946  *
947  * Since: 2.28
948  **/
949 gboolean
950 g_application_get_is_registered (GApplication *application)
951 {
952   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
953
954   return application->priv->is_registered;
955 }
956
957 /**
958  * g_application_get_is_remote:
959  * @application: a #GApplication
960  * @returns: %TRUE if @application is remote
961  *
962  * Checks if @application is remote.
963  *
964  * If @application is remote then it means that another instance of
965  * application already exists (the 'primary' instance).  Calls to
966  * perform actions on @application will result in the actions being
967  * performed by the primary instance.
968  *
969  * The value of this property cannot be accessed before
970  * g_application_register() has been called.  See
971  * g_application_get_is_registered().
972  *
973  * Since: 2.28
974  **/
975 gboolean
976 g_application_get_is_remote (GApplication *application)
977 {
978   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
979   g_return_val_if_fail (application->priv->is_registered, FALSE);
980
981   return application->priv->is_remote;
982 }
983
984 /* Register {{{1 */
985 /**
986  * g_application_register:
987  * @application: a #GApplication
988  * @cancellable: a #GCancellable, or %NULL
989  * @error: a pointer to a NULL #GError, or %NULL
990  * @returns: %TRUE if registration succeeded
991  *
992  * Attempts registration of the application.
993  *
994  * This is the point at which the application discovers if it is the
995  * primary instance or merely acting as a remote for an already-existing
996  * primary instance.  This is implemented by attempting to acquire the
997  * application identifier as a unique bus name on the session bus using
998  * GDBus.
999  *
1000  * Due to the internal architecture of GDBus, method calls can be
1001  * dispatched at any time (even if a main loop is not running).  For
1002  * this reason, you must ensure that any object paths that you wish to
1003  * register are registered before calling this function.
1004  *
1005  * If the application has already been registered then %TRUE is
1006  * returned with no work performed.
1007  *
1008  * The #GApplication::startup signal is emitted if registration succeeds
1009  * and @application is the primary instance.
1010  *
1011  * In the event of an error (such as @cancellable being cancelled, or a
1012  * failure to connect to the session bus), %FALSE is returned and @error
1013  * is set appropriately.
1014  *
1015  * Note: the return value of this function is not an indicator that this
1016  * instance is or is not the primary instance of the application.  See
1017  * g_application_get_is_remote() for that.
1018  *
1019  * Since: 2.28
1020  **/
1021 gboolean
1022 g_application_register (GApplication  *application,
1023                         GCancellable  *cancellable,
1024                         GError       **error)
1025 {
1026   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1027
1028   if (!application->priv->is_registered)
1029     {
1030       if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1031         {
1032           application->priv->impl =
1033             g_application_impl_register (application, application->priv->id,
1034                                          application->priv->flags,
1035                                          &application->priv->remote_actions,
1036                                          cancellable, error);
1037
1038           if (application->priv->impl == NULL)
1039             return FALSE;
1040         }
1041
1042       application->priv->is_remote = application->priv->remote_actions != NULL;
1043       application->priv->is_registered = TRUE;
1044
1045       g_object_notify (G_OBJECT (application), "is-registered");
1046
1047       if (!application->priv->is_remote)
1048         {
1049           g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1050
1051           if (!application->priv->did_startup)
1052             g_critical ("GApplication subclass '%s' failed to chain up on"
1053                         " ::startup (from start of override function)",
1054                         G_OBJECT_TYPE_NAME (application));
1055         }
1056     }
1057
1058   return TRUE;
1059 }
1060
1061 /* Hold/release {{{1 */
1062 /**
1063  * g_application_hold:
1064  * @application: a #GApplication
1065  *
1066  * Increases the use count of @application.
1067  *
1068  * Use this function to indicate that the application has a reason to
1069  * continue to run.  For example, g_application_hold() is called by GTK+
1070  * when a toplevel window is on the screen.
1071  *
1072  * To cancel the hold, call g_application_release().
1073  **/
1074 void
1075 g_application_hold (GApplication *application)
1076 {
1077   if (application->priv->inactivity_timeout_id)
1078     {
1079       g_source_remove (application->priv->inactivity_timeout_id);
1080       application->priv->inactivity_timeout_id = 0;
1081     }
1082
1083   application->priv->use_count++;
1084 }
1085
1086 static gboolean
1087 inactivity_timeout_expired (gpointer data)
1088 {
1089   GApplication *application = G_APPLICATION (data);
1090
1091   G_APPLICATION_GET_CLASS (application)
1092     ->quit_mainloop (application);
1093
1094   return FALSE;
1095 }
1096
1097
1098 /**
1099  * g_application_release:
1100  * @application: a #GApplication
1101  *
1102  * Decrease the use count of @application.
1103  *
1104  * When the use count reaches zero, the application will stop running.
1105  *
1106  * Never call this function except to cancel the effect of a previous
1107  * call to g_application_hold().
1108  **/
1109 void
1110 g_application_release (GApplication *application)
1111 {
1112   application->priv->use_count--;
1113
1114   if (application->priv->use_count == 0)
1115     {
1116       if (application->priv->inactivity_timeout)
1117         application->priv->inactivity_timeout_id =
1118           g_timeout_add (application->priv->inactivity_timeout,
1119                          inactivity_timeout_expired, application);
1120
1121       else
1122         G_APPLICATION_GET_CLASS (application)
1123           ->quit_mainloop (application);
1124     }
1125 }
1126
1127 /* Activate, Open {{{1 */
1128 /**
1129  * g_application_activate:
1130  * @application: a #GApplication
1131  *
1132  * Activates the application.
1133  *
1134  * In essence, this results in the #GApplication::activate() signal being
1135  * emitted in the primary instance.
1136  *
1137  * The application must be registered before calling this function.
1138  *
1139  * Since: 2.28
1140  **/
1141 void
1142 g_application_activate (GApplication *application)
1143 {
1144   g_return_if_fail (G_IS_APPLICATION (application));
1145   g_return_if_fail (application->priv->is_registered);
1146
1147   if (application->priv->is_remote)
1148     g_application_impl_activate (application->priv->impl,
1149                                  get_platform_data (application));
1150
1151   else
1152     g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1153 }
1154
1155 /**
1156  * g_application_open:
1157  * @application: a #GApplication
1158  * @files: (array length=n_files): an array of #GFiles to open
1159  * @n_files: the length of the @files array
1160  * @hint: a hint (or ""), but never %NULL
1161  *
1162  * Opens the given files.
1163  *
1164  * In essence, this results in the #GApplication::open signal being emitted
1165  * in the primary instance.
1166  *
1167  * @n_files must be greater than zero.
1168  *
1169  * @hint is simply passed through to the ::open signal.  It is
1170  * intended to be used by applications that have multiple modes for
1171  * opening files (eg: "view" vs "edit", etc).  Unless you have a need
1172  * for this functionality, you should use "".
1173  *
1174  * The application must be registered before calling this function
1175  * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1176  *
1177  * Since: 2.28
1178  **/
1179 void
1180 g_application_open (GApplication  *application,
1181                     GFile        **files,
1182                     gint           n_files,
1183                     const gchar   *hint)
1184 {
1185   g_return_if_fail (G_IS_APPLICATION (application));
1186   g_return_if_fail (application->priv->flags &
1187                     G_APPLICATION_HANDLES_OPEN);
1188   g_return_if_fail (application->priv->is_registered);
1189
1190   if (application->priv->is_remote)
1191     g_application_impl_open (application->priv->impl,
1192                              files, n_files, hint,
1193                              get_platform_data (application));
1194
1195   else
1196     g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1197                    0, files, n_files, hint);
1198 }
1199
1200 /* Run {{{1 */
1201 /**
1202  * g_application_run:
1203  * @application: a #GApplication
1204  * @argc: the argc from main() (or 0 if @argv is %NULL)
1205  * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1206  * @returns: the exit status
1207  *
1208  * Runs the application.
1209  *
1210  * This function is intended to be run from main() and its return value
1211  * is intended to be returned by main(). Although you are expected to pass
1212  * the @argc, @argv parameters from main() to this function, it is possible
1213  * to pass %NULL if @argv is not available or commandline handling is not
1214  * required.
1215  *
1216  * First, the local_command_line() virtual function is invoked.
1217  * This function always runs on the local instance. It gets passed a pointer
1218  * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1219  * that it handled (shifting up remaining arguments). See
1220  * <xref linkend="gapplication-example-cmdline2"/> for an example of
1221  * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1222  * after setting <literal>argc = g_strv_length (argv);</literal>.
1223  *
1224  * The last argument to local_command_line() is a pointer to the @status
1225  * variable which can used to set the exit status that is returned from
1226  * g_application_run().
1227  *
1228  * If local_command_line() returns %TRUE, the command line is expected
1229  * to be completely handled, including possibly registering as the primary
1230  * instance, calling g_application_activate() or g_application_open(), etc.
1231  *
1232  * If local_command_line() returns %FALSE then the application is registered
1233  * and the #GApplication::command-line signal is emitted in the primary
1234  * instance (which may or may not be this instance). The signal handler
1235  * gets passed a #GApplicationCommandline object that (among other things)
1236  * contains the remaining commandline arguments that have not been handled
1237  * by local_command_line().
1238  *
1239  * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1240  * flag set then the default implementation of local_command_line()
1241  * always returns %FALSE immediately, resulting in the commandline
1242  * always being handled in the primary instance.
1243  *
1244  * Otherwise, the default implementation of local_command_line() tries
1245  * to do a couple of things that are probably reasonable for most
1246  * applications.  First, g_application_register() is called to attempt
1247  * to register the application.  If that works, then the command line
1248  * arguments are inspected.  If no commandline arguments are given, then
1249  * g_application_activate() is called.  If commandline arguments are
1250  * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1251  * are assumed to be filenames and g_application_open() is called.
1252  *
1253  * If you need to handle commandline arguments that are not filenames,
1254  * and you don't mind commandline handling to happen in the primary
1255  * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1256  * process the commandline arguments in your #GApplication::command-line
1257  * signal handler, either manually or using the #GOptionContext API.
1258  *
1259  * If you are interested in doing more complicated local handling of the
1260  * commandline then you should implement your own #GApplication subclass
1261  * and override local_command_line(). In this case, you most likely want
1262  * to return %TRUE from your local_command_line() implementation to
1263  * suppress the default handling. See
1264  * <xref linkend="gapplication-example-cmdline2"/> for an example.
1265  *
1266  * If, after the above is done, the use count of the application is zero
1267  * then the exit status is returned immediately.  If the use count is
1268  * non-zero then the mainloop is run until the use count falls to zero,
1269  * at which point 0 is returned.
1270  *
1271  * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1272  * use count of zero is delayed for a while (ie: the instance stays
1273  * around to provide its <emphasis>service</emphasis> to others).
1274  *
1275  * Since: 2.28
1276  **/
1277 int
1278 g_application_run (GApplication  *application,
1279                    int            argc,
1280                    char         **argv)
1281 {
1282   gchar **arguments;
1283   int status;
1284   gint i;
1285
1286   g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1287   g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1288
1289   arguments = g_new (gchar *, argc + 1);
1290   for (i = 0; i < argc; i++)
1291     arguments[i] = g_strdup (argv[i]);
1292   arguments[i] = NULL;
1293
1294   if (g_get_prgname () == NULL && argc > 0)
1295     {
1296       gchar *prgname;
1297
1298       prgname = g_path_get_basename (argv[0]);
1299       g_set_prgname (prgname);
1300       g_free (prgname);
1301     }
1302
1303   if (!G_APPLICATION_GET_CLASS (application)
1304         ->local_command_line (application, &arguments, &status))
1305     {
1306       GError *error = NULL;
1307
1308       if (!g_application_register (application, NULL, &error))
1309         {
1310           g_printerr ("%s", error->message);
1311           g_error_free (error);
1312           return 1;
1313         }
1314
1315       if (application->priv->is_remote)
1316         {
1317           GVariant *platform_data;
1318
1319           platform_data = get_platform_data (application);
1320           status = g_application_impl_command_line (application->priv->impl,
1321                                                     arguments, platform_data);
1322         }
1323       else
1324         {
1325           GApplicationCommandLine *cmdline;
1326           GVariant *v;
1327
1328           v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1329           cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1330                                   "arguments", v, NULL);
1331           g_signal_emit (application,
1332                          g_application_signals[SIGNAL_COMMAND_LINE],
1333                          0, cmdline, &status);
1334           g_object_unref (cmdline);
1335         }
1336     }
1337
1338   g_strfreev (arguments);
1339
1340   if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1341       application->priv->is_registered &&
1342       !application->priv->use_count &&
1343       !application->priv->inactivity_timeout_id)
1344     {
1345       application->priv->inactivity_timeout_id =
1346         g_timeout_add (10000, inactivity_timeout_expired, application);
1347     }
1348
1349   if (application->priv->use_count ||
1350       application->priv->inactivity_timeout_id)
1351     {
1352       G_APPLICATION_GET_CLASS (application)
1353         ->run_mainloop (application);
1354       status = 0;
1355     }
1356
1357   if (!application->priv->is_remote)
1358     {
1359       g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1360
1361       if (!application->priv->did_shutdown)
1362         g_critical ("GApplication subclass '%s' failed to chain up on"
1363                     " ::shutdown (from end of override function)",
1364                     G_OBJECT_TYPE_NAME (application));
1365     }
1366
1367   if (application->priv->impl)
1368     g_application_impl_flush (application->priv->impl);
1369
1370   g_settings_sync ();
1371
1372   return status;
1373 }
1374
1375 static gboolean
1376 g_application_has_action (GActionGroup *action_group,
1377                           const gchar  *action_name)
1378 {
1379   GApplication *application = G_APPLICATION (action_group);
1380
1381   g_return_val_if_fail (application->priv->is_registered, FALSE);
1382
1383   if (application->priv->remote_actions != NULL)
1384     return g_hash_table_lookup (application->priv->remote_actions,
1385                                 action_name) != NULL;
1386
1387   return application->priv->actions &&
1388          g_action_group_has_action (application->priv->actions, action_name);
1389 }
1390
1391 static gchar **
1392 g_application_list_actions (GActionGroup *action_group)
1393 {
1394   GApplication *application = G_APPLICATION (action_group);
1395
1396   g_return_val_if_fail (application->priv->is_registered, NULL);
1397
1398   if (application->priv->remote_actions != NULL)
1399     {
1400       GHashTableIter iter;
1401       gint n, i = 0;
1402       gchar **keys;
1403       gpointer key;
1404
1405       n = g_hash_table_size (application->priv->remote_actions);
1406       keys = g_new (gchar *, n + 1);
1407
1408       g_hash_table_iter_init (&iter, application->priv->remote_actions);
1409       while (g_hash_table_iter_next (&iter, &key, NULL))
1410         keys[i++] = g_strdup (key);
1411       g_assert_cmpint (i, ==, n);
1412       keys[n] = NULL;
1413
1414       return keys;
1415     }
1416
1417   else if (application->priv->actions != NULL)
1418     return g_action_group_list_actions (application->priv->actions);
1419
1420   else
1421     /* empty string array */
1422     return g_new0 (gchar *, 1);
1423 }
1424
1425 static gboolean
1426 g_application_get_action_enabled (GActionGroup *action_group,
1427                                   const gchar  *action_name)
1428 {
1429   GApplication *application = G_APPLICATION (action_group);
1430
1431   g_return_val_if_fail (application->priv->remote_actions != NULL ||
1432                         application->priv->actions != NULL, FALSE);
1433   g_return_val_if_fail (application->priv->is_registered, FALSE);
1434
1435   if (application->priv->remote_actions)
1436     {
1437       RemoteActionInfo *info;
1438
1439       info = g_hash_table_lookup (application->priv->remote_actions,
1440                                   action_name);
1441
1442       return info && info->enabled;
1443     }
1444
1445   return g_action_group_get_action_enabled (application->priv->actions,
1446                                             action_name);
1447 }
1448
1449 static const GVariantType *
1450 g_application_get_action_parameter_type (GActionGroup *action_group,
1451                                          const gchar  *action_name)
1452 {
1453   GApplication *application = G_APPLICATION (action_group);
1454
1455   g_return_val_if_fail (application->priv->remote_actions != NULL ||
1456                         application->priv->actions != NULL, NULL);
1457   g_return_val_if_fail (application->priv->is_registered, NULL);
1458
1459   if (application->priv->remote_actions)
1460     {
1461       RemoteActionInfo *info;
1462
1463       info = g_hash_table_lookup (application->priv->remote_actions,
1464                                   action_name);
1465
1466       if (info)
1467         return info->parameter_type;
1468       else
1469         return NULL;
1470     }
1471
1472   return g_action_group_get_action_parameter_type (application->priv->actions,
1473                                                    action_name);
1474 }
1475
1476 static const GVariantType *
1477 g_application_get_action_state_type (GActionGroup *action_group,
1478                                      const gchar  *action_name)
1479 {
1480   GApplication *application = G_APPLICATION (action_group);
1481
1482   g_return_val_if_fail (application->priv->remote_actions != NULL ||
1483                         application->priv->actions != NULL, NULL);
1484   g_return_val_if_fail (application->priv->is_registered, NULL);
1485
1486   if (application->priv->remote_actions)
1487     {
1488       RemoteActionInfo *info;
1489
1490       info = g_hash_table_lookup (application->priv->remote_actions,
1491                                   action_name);
1492
1493       if (info && info->state)
1494         return g_variant_get_type (info->state);
1495       else
1496         return NULL;
1497     }
1498
1499   return g_action_group_get_action_state_type (application->priv->actions,
1500                                                action_name);
1501 }
1502
1503 static GVariant *
1504 g_application_get_action_state (GActionGroup *action_group,
1505                                 const gchar  *action_name)
1506 {
1507   GApplication *application = G_APPLICATION (action_group);
1508
1509   g_return_val_if_fail (application->priv->remote_actions != NULL ||
1510                         application->priv->actions != NULL, NULL);
1511   g_return_val_if_fail (application->priv->is_registered, NULL);
1512
1513   if (application->priv->remote_actions)
1514     {
1515       RemoteActionInfo *info;
1516
1517       info = g_hash_table_lookup (application->priv->remote_actions,
1518                                   action_name);
1519
1520       if (info && info->state)
1521         return g_variant_ref (info->state);
1522       else
1523         return NULL;
1524     }
1525
1526   return g_action_group_get_action_state (application->priv->actions,
1527                                           action_name);
1528 }
1529
1530 static void
1531 g_application_change_action_state (GActionGroup *action_group,
1532                                    const gchar  *action_name,
1533                                    GVariant     *value)
1534 {
1535   GApplication *application = G_APPLICATION (action_group);
1536
1537   g_return_if_fail (application->priv->is_remote ||
1538                     application->priv->actions != NULL);
1539   g_return_if_fail (application->priv->is_registered);
1540
1541   if (application->priv->is_remote)
1542     g_application_impl_change_action_state (application->priv->impl,
1543                                             action_name, value,
1544                                             get_platform_data (application));
1545
1546   else
1547     g_action_group_change_action_state (application->priv->actions,
1548                                         action_name, value);
1549 }
1550
1551 static void
1552 g_application_activate_action (GActionGroup *action_group,
1553                                const gchar  *action_name,
1554                                GVariant     *parameter)
1555 {
1556   GApplication *application = G_APPLICATION (action_group);
1557
1558   g_return_if_fail (application->priv->is_remote ||
1559                     application->priv->actions != NULL);
1560   g_return_if_fail (application->priv->is_registered);
1561
1562   if (application->priv->is_remote)
1563     g_application_impl_activate_action (application->priv->impl,
1564                                         action_name, parameter,
1565                                         get_platform_data (application));
1566
1567   else
1568     g_action_group_activate_action (application->priv->actions,
1569                                     action_name, parameter);
1570 }
1571
1572 static void
1573 g_application_action_group_iface_init (GActionGroupInterface *iface)
1574 {
1575   iface->has_action = g_application_has_action;
1576   iface->list_actions = g_application_list_actions;
1577
1578   iface->get_action_enabled = g_application_get_action_enabled;
1579   iface->get_action_parameter_type = g_application_get_action_parameter_type;
1580   iface->get_action_state_type = g_application_get_action_state_type;
1581   iface->get_action_state = g_application_get_action_state;
1582   iface->change_action_state = g_application_change_action_state;
1583   iface->activate_action = g_application_activate_action;
1584 }
1585
1586 /* Epilogue {{{1 */
1587 /* vim:set foldmethod=marker: */