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