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