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