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