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