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