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