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