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