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