6522aaa2dac31723c70cc70b217c68277bc777ad
[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
31 #include "gioenumtypes.h"
32 #include "gio-marshal.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 convenience, the restrictions on application identifiers are reproduced
65  * here:
66  * <itemizedlist>
67  *   <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
68  *   <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
69  *   <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
70  *   <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
71  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
72  * </itemizedlist>
73  *
74  * GApplication provides convenient life cycle management by maintaining
75  * a <firstterm>use count</firstterm> for the primary application instance.
76  * The use count can be changed using g_application_hold() and
77  * g_application_release(). If it drops to zero, the application exits.
78  *
79  * <example id="gapplication-example-open"><title>Opening files with a GApplication</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
80  */
81
82 struct _GApplicationPrivate
83 {
84   GApplicationFlags  flags;
85   gchar             *id;
86
87   GActionGroup      *actions;
88   GMainLoop         *mainloop;
89
90   guint              inactivity_timeout_id;
91   guint              inactivity_timeout;
92   guint              use_count;
93
94   guint              is_registered : 1;
95   guint              is_remote : 1;
96
97   GApplicationImpl  *impl;
98 };
99
100 enum
101 {
102   PROP_NONE,
103   PROP_APPLICATION_ID,
104   PROP_FLAGS,
105   PROP_IS_REGISTERED,
106   PROP_IS_REMOTE,
107   PROP_INACTIVITY_TIMEOUT,
108   PROP_ACTION_GROUP
109 };
110
111 enum
112 {
113   SIGNAL_STARTUP,
114   SIGNAL_ACTIVATE,
115   SIGNAL_OPEN,
116   SIGNAL_ACTION,
117   SIGNAL_COMMAND_LINE,
118   NR_SIGNALS
119 };
120
121 static guint g_application_signals[NR_SIGNALS];
122
123 static void g_application_action_group_iface_init (GActionGroupInterface *);
124 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
125  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
126    g_application_action_group_iface_init))
127
128 /* vfunc defaults {{{1 */
129 static void
130 g_application_real_before_emit (GApplication *application,
131                                 GVariant     *platform_data)
132 {
133 }
134
135 static void
136 g_application_real_after_emit (GApplication *application,
137                                GVariant     *platform_data)
138 {
139 }
140
141 static void
142 g_application_real_startup (GApplication *application)
143 {
144 }
145
146 static void
147 g_application_real_activate (GApplication *application)
148 {
149   if (!g_signal_has_handler_pending (application,
150                                      g_application_signals[SIGNAL_ACTIVATE],
151                                      0, TRUE))
152     {
153       static gboolean warned;
154
155       if (warned)
156         return;
157
158       g_warning ("Your application does not implement "
159                  "g_application_activate() and has no handlers connected "
160                  "to the 'activate' signal.  It should do one of these.");
161       warned = TRUE;
162     }
163 }
164
165 static void
166 g_application_real_open (GApplication  *application,
167                          GFile        **files,
168                          gint           n_files,
169                          const gchar   *hint)
170 {
171   if (!g_signal_has_handler_pending (application,
172                                      g_application_signals[SIGNAL_OPEN],
173                                      0, TRUE))
174     {
175       static gboolean warned;
176
177       if (warned)
178         return;
179
180       g_warning ("Your application claims to support opening files "
181                  "but does not implement g_application_open() and has no "
182                  "handlers connected to the 'open' signal.");
183       warned = TRUE;
184     }
185 }
186
187 static int
188 g_application_real_command_line (GApplication            *application,
189                                  GApplicationCommandLine *cmdline)
190 {
191   static gboolean warned;
192
193   if (warned) 
194     return 1;
195
196   g_warning ("Your application claims to support custom command line "
197              "handling but does not implement g_application_command_line() "
198              "and has no handlers connected to the 'command-line' signal.");
199
200   warned = TRUE;
201
202   return 1;
203 }
204
205 static gboolean
206 g_application_real_local_command_line (GApplication   *application,
207                                        gchar        ***arguments,
208                                        int            *exit_status)
209 {
210   if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
211     return FALSE;
212
213   else
214     {
215       GError *error = NULL;
216       gint n_args;
217
218       if (!g_application_register (application, NULL, &error))
219         {
220           g_critical ("%s", error->message);
221           g_error_free (error);
222           *exit_status = 1;
223           return TRUE;
224         }
225
226       n_args = g_strv_length (*arguments);
227
228       if (application->priv->flags & G_APPLICATION_IS_SERVICE)
229         {
230           if (n_args > 1)
231             {
232               g_printerr ("GApplication service mode takes no arguments.\n");
233               application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
234             }
235
236           return TRUE;
237         }
238
239       if (n_args <= 1)
240         {
241           g_application_activate (application);
242           *exit_status = 0;
243         }
244
245       else
246         {
247           if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
248             {
249               g_critical ("This application can not open files.");
250               *exit_status = 1;
251             }
252           else
253             {
254               GFile **files;
255               gint n_files;
256               gint i;
257
258               n_files = n_args - 1;
259               files = g_new (GFile *, n_files);
260
261               for (i = 0; i < n_files; i++)
262                 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
263
264               g_application_open (application, files, n_files, "");
265
266               for (i = 0; i < n_files; i++)
267                 g_object_unref (files[i]);
268               g_free (files);
269
270               *exit_status = 0;
271             }
272         }
273
274       return TRUE;
275     }
276 }
277
278 static void
279 g_application_real_add_platform_data (GApplication    *application,
280                                       GVariantBuilder *builder)
281 {
282 }
283
284 static void
285 g_application_real_quit_mainloop (GApplication *application)
286 {
287   if (application->priv->mainloop != NULL)
288     g_main_loop_quit (application->priv->mainloop);
289 }
290
291 static void
292 g_application_real_run_mainloop (GApplication *application)
293 {
294   if (application->priv->mainloop == NULL)
295     application->priv->mainloop = g_main_loop_new (NULL, FALSE);
296
297   g_main_loop_run (application->priv->mainloop);
298 }
299
300 /* GObject implementation stuff {{{1 */
301 static void
302 g_application_set_property (GObject      *object,
303                             guint         prop_id,
304                             const GValue *value,
305                             GParamSpec   *pspec)
306 {
307   GApplication *application = G_APPLICATION (object);
308
309   switch (prop_id)
310     {
311     case PROP_APPLICATION_ID:
312       g_application_set_application_id (application,
313                                         g_value_get_string (value));
314       break;
315
316     case PROP_FLAGS:
317       g_application_set_flags (application, g_value_get_flags (value));
318       break;
319
320     case PROP_INACTIVITY_TIMEOUT:
321       g_application_set_inactivity_timeout (application,
322                                             g_value_get_uint (value));
323       break;
324
325     case PROP_ACTION_GROUP:
326       g_application_set_action_group (application,
327                                       g_value_get_object (value));
328       break;
329
330     default:
331       g_assert_not_reached ();
332     }
333 }
334
335 /**
336  * g_application_set_action_group:
337  * @application: a #GApplication
338  * @action_group: a #GActionGroup, or %NULL
339  *
340  * Sets or unsets the group of actions associated with the application.
341  *
342  * These actions are the actions that can be remotely invoked.
343  *
344  * It is an error to call this function after the application has been
345  * registered.
346  *
347  * Since: 2.28
348  **/
349 void
350 g_application_set_action_group (GApplication *application,
351                                 GActionGroup *action_group)
352 {
353   g_return_if_fail (G_IS_APPLICATION (application));
354   g_return_if_fail (application->priv->is_registered);
355
356   if (application->priv->actions != NULL)
357     g_object_unref (application->priv->actions);
358
359   application->priv->actions = action_group;
360
361   if (application->priv->actions != NULL)
362     g_object_ref (application->priv->actions);
363 }
364
365 static void
366 g_application_get_property (GObject    *object,
367                             guint       prop_id,
368                             GValue     *value,
369                             GParamSpec *pspec)
370 {
371   GApplication *application = G_APPLICATION (object);
372
373   switch (prop_id)
374     {
375     case PROP_APPLICATION_ID:
376       g_value_set_string (value,
377                           g_application_get_application_id (application));
378       break;
379
380     case PROP_FLAGS:
381       g_value_set_flags (value,
382                          g_application_get_flags (application));
383       break;
384
385     case PROP_IS_REGISTERED:
386       g_value_set_boolean (value,
387                            g_application_get_is_registered (application));
388       break;
389
390     case PROP_IS_REMOTE:
391       g_value_set_boolean (value,
392                            g_application_get_is_remote (application));
393       break;
394
395     case PROP_INACTIVITY_TIMEOUT:
396       g_value_set_uint (value,
397                         g_application_get_inactivity_timeout (application));
398       break;
399
400     default:
401       g_assert_not_reached ();
402     }
403 }
404
405 static void
406 g_application_constructed (GObject *object)
407 {
408   GApplication *application = G_APPLICATION (object);
409
410   g_assert (application->priv->id != NULL);
411 }
412
413 static void
414 g_application_finalize (GObject *object)
415 {
416   GApplication *application = G_APPLICATION (object);
417
418   if (application->priv->impl)
419     g_application_impl_destroy (application->priv->impl);
420   g_free (application->priv->id);
421
422   if (application->priv->mainloop)
423     g_main_loop_unref (application->priv->mainloop);
424
425   G_OBJECT_CLASS (g_application_parent_class)
426     ->finalize (object);
427 }
428
429 static void
430 g_application_init (GApplication *application)
431 {
432   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
433                                                    G_TYPE_APPLICATION,
434                                                    GApplicationPrivate);
435 }
436
437 static void
438 g_application_class_init (GApplicationClass *class)
439 {
440   GObjectClass *object_class = G_OBJECT_CLASS (class);
441
442   object_class->constructed = g_application_constructed;
443   object_class->finalize = g_application_finalize;
444   object_class->get_property = g_application_get_property;
445   object_class->set_property = g_application_set_property;
446
447   class->before_emit = g_application_real_before_emit;
448   class->after_emit = g_application_real_after_emit;
449   class->startup = g_application_real_startup;
450   class->activate = g_application_real_activate;
451   class->open = g_application_real_open;
452   class->command_line = g_application_real_command_line;
453   class->local_command_line = g_application_real_local_command_line;
454   class->add_platform_data = g_application_real_add_platform_data;
455   class->quit_mainloop = g_application_real_quit_mainloop;
456   class->run_mainloop = g_application_real_run_mainloop;
457
458   g_object_class_install_property (object_class, PROP_APPLICATION_ID,
459     g_param_spec_string ("application-id",
460                          P_("Application identifier"),
461                          P_("The unique identifier for the application"),
462                          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
463                          G_PARAM_STATIC_STRINGS));
464
465   g_object_class_install_property (object_class, PROP_FLAGS,
466     g_param_spec_flags ("flags",
467                         P_("Application flags"),
468                         P_("Flags specifying the behaviour of the application"),
469                         G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
470                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
471
472   g_object_class_install_property (object_class, PROP_IS_REGISTERED,
473     g_param_spec_boolean ("is-registered",
474                           P_("Is registered"),
475                           P_("If g_application_register() has been called"),
476                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
477
478   g_object_class_install_property (object_class, PROP_IS_REMOTE,
479     g_param_spec_boolean ("is-remote",
480                           P_("Is remote"),
481                           P_("If this application instance is remote"),
482                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
483
484   g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
485     g_param_spec_boolean ("inactivity-timeout",
486                           P_("Inactivity timeout"),
487                           P_("Iime (ms) to stay alive after becoming idle"),
488                           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489
490   g_object_class_install_property (object_class, PROP_ACTION_GROUP,
491     g_param_spec_object ("action-group",
492                          P_("Action group"),
493                          P_("The group of actions that the application exports"),
494                          G_TYPE_ACTION_GROUP, 
495                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
496
497   /**
498    * GApplication::startup:
499    * @application: the application
500    *
501    * The ::startup signal is emitted on the primary instance immediately
502    * after registration. See g_activation_register().
503    */
504   g_application_signals[SIGNAL_STARTUP] =
505     g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
506                   G_STRUCT_OFFSET (GApplicationClass, startup),
507                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
508
509   /**
510    * GApplication::activate:
511    * @application: the application
512    *
513    * The ::activate signal is emitted on the primary instance when an
514    * activation occurs. See g_application_activate().
515    */
516   g_application_signals[SIGNAL_ACTIVATE] =
517     g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
518                   G_STRUCT_OFFSET (GApplicationClass, activate),
519                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
520
521
522   /**
523    * GApplication::open:
524    * @application: the application
525    * @files: an array of #GFile objects
526    * @n_files: the length of @files
527    * @hint: a hint provided by the calling instance
528    *
529    * The ::open signal is emitted on the primary instance when there are
530    * files to open. See g_application_open() for more information.
531    */
532   g_application_signals[SIGNAL_OPEN] =
533     g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
534                   G_STRUCT_OFFSET (GApplicationClass, open),
535                   NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
536                   G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
537
538   /**
539    * GApplication::command-line:
540    * @application: the application
541    * @command_line: a #GApplicationCommandLine representing the
542    *     passed commandline
543    *
544    * The ::command-line signal is emitted on the primary instance when
545    * a commandline is not handled locally. See g_application_run() for
546    * more information.
547    */
548   g_application_signals[SIGNAL_COMMAND_LINE] =
549     g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
550                   G_STRUCT_OFFSET (GApplicationClass, command_line),
551                   g_signal_accumulator_first_wins, NULL,
552                   _gio_marshal_INT__OBJECT,
553                   G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
554
555   g_type_class_add_private (class, sizeof (GApplicationPrivate));
556 }
557
558 static GVariant *
559 get_platform_data (GApplication *application)
560 {
561   GVariantBuilder *builder;
562   GVariant *result;
563
564   builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
565
566   {
567     gchar *cwd = g_get_current_dir ();
568     g_variant_builder_add (builder, "{sv}", "cwd",
569                            g_variant_new_bytestring (cwd));
570     g_free (cwd);
571   }
572
573   G_APPLICATION_GET_CLASS (application)->
574     add_platform_data (application, builder);
575
576   result = g_variant_builder_end (builder);
577   g_variant_builder_unref (builder);
578
579   return result;
580 }
581
582 /* Application ID validity {{{1 */
583
584 /**
585  * g_application_id_is_valid:
586  * @application_id: a potential application identifier
587  * @returns: %TRUE if @application_id is valid
588  *
589  * Checks if @application_id is a valid application ID.  A valid ID is
590  * required for calls to g_application_new() and
591  * g_application_set_application_id().
592  **/
593 gboolean
594 g_application_id_is_valid (const gchar *application_id)
595 {
596   gboolean allow_dot;
597
598   if (strlen (application_id) > 255)
599     return FALSE;
600
601   if (!g_ascii_isalpha (*application_id))
602     return FALSE;
603
604   application_id++;
605   allow_dot = FALSE;
606   for (; *application_id; application_id++)
607     {
608       if (g_ascii_isalnum (*application_id) ||
609           (*application_id == '-') ||
610           (*application_id == '_'))
611         allow_dot = TRUE;
612       else if (allow_dot && *application_id == '.')
613         allow_dot = FALSE;
614       else
615         return FALSE;
616     }
617
618   return TRUE;
619 }
620  
621 /* Public Constructor {{{1 */
622 /**
623  * g_application_new:
624  * @application_id: the application id
625  * @flags: the application flags
626  * @returns: a new #GApplication instance
627  *
628  * Creates a new #GApplication instance.
629  *
630  * This function calls g_type_init() for you.
631  *
632  * The application id must be valid.  See g_application_id_is_valid().
633  **/
634 GApplication *
635 g_application_new (const gchar       *application_id,
636                    GApplicationFlags  flags)
637 {
638   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
639
640   g_type_init ();
641
642   return g_object_new (G_TYPE_APPLICATION,
643                        "application-id", application_id,
644                        "flags", flags,
645                        NULL);
646 }
647
648 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
649 /**
650  * g_application_get_application_id:
651  * @application: a #GApplication
652  * @returns: the identifier for @application, owned by @application
653  *
654  * Gets the unique identifier for @application.
655  *
656  * Since: 2.28
657  **/
658 const gchar *
659 g_application_get_application_id (GApplication *application)
660 {
661   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
662
663   return application->priv->id;
664 }
665
666 /**
667  * g_application_set_application_id:
668  * @application: a #GApplication
669  * @application_id: the identifier for @application
670  *
671  * Sets the unique identifier for @application.
672  *
673  * The application id can only be modified if @application has not yet
674  * been registered.
675  *
676  * The application id must be valid.  See g_application_id_is_valid().
677  *
678  * Since: 2.28
679  **/
680 void
681 g_application_set_application_id (GApplication *application,
682                                   const gchar  *application_id)
683 {
684   g_return_if_fail (G_IS_APPLICATION (application));
685
686   if (g_strcmp0 (application->priv->id, application_id) != 0)
687     {
688       g_return_if_fail (g_application_id_is_valid (application_id));
689       g_return_if_fail (!application->priv->is_registered);
690
691       g_free (application->priv->id);
692       application->priv->id = g_strdup (application_id);
693
694       g_object_notify (G_OBJECT (application), "application-id");
695     }
696 }
697
698 /**
699  * g_application_get_flags:
700  * @application: a #GApplication
701  * @returns: the flags for @application
702  *
703  * Gets the flags for @application.
704  *
705  * See #GApplicationFlags.
706  *
707  * Since: 2.28
708  **/
709 GApplicationFlags
710 g_application_get_flags (GApplication *application)
711 {
712   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
713
714   return application->priv->flags;
715 }
716
717 /**
718  * g_application_set_flags:
719  * @application: a #GApplication
720  * @flags: the flags for @application
721  *
722  * Sets the flags for @application.
723  *
724  * The flags can only be modified if @application has not yet been
725  * registered.
726  *
727  * See #GApplicationFlags.
728  *
729  * Since: 2.28
730  **/
731 void
732 g_application_set_flags (GApplication      *application,
733                          GApplicationFlags  flags)
734 {
735   g_return_if_fail (G_IS_APPLICATION (application));
736
737   if (application->priv->flags != flags)
738     {
739       g_return_if_fail (!application->priv->is_registered);
740
741       application->priv->flags = flags;
742
743       g_object_notify (G_OBJECT (application), "flags");
744     }
745 }
746
747 /**
748  * g_application_get_inactivity_timeout:
749  * @application: a #GApplication
750  *
751  * Gets the current inactivity timeout for the application.
752  *
753  * This is the amount of time (in milliseconds) after the last call to
754  * g_application_release() before the application stops running.
755  *
756  * Returns: the timeout, in milliseconds
757  *
758  * Since: 2.28
759  **/
760 guint
761 g_application_get_inactivity_timeout (GApplication *application)
762 {
763   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
764
765   return application->priv->inactivity_timeout;
766 }
767
768 /**
769  * g_application_set_inactivity_timeout:
770  * @application: a #GApplication
771  * @inactivity_timeout: the timeout, in milliseconds
772  *
773  * Sets the current inactivity timeout for the application.
774  *
775  * This is the amount of time (in milliseconds) after the last call to
776  * g_application_release() before the application stops running.
777  *
778  * This call has no side effects of its own.  The value set here is only
779  * used for next time g_application_release() drops the use count to
780  * zero.  Any timeouts currently in progress are not impacted.
781  *
782  * Returns: the timeout, in milliseconds
783  *
784  * Since: 2.28
785  **/
786 void
787 g_application_set_inactivity_timeout (GApplication *application,
788                                       guint         inactivity_timeout)
789 {
790   g_return_if_fail (G_IS_APPLICATION (application));
791
792   if (application->priv->inactivity_timeout != inactivity_timeout)
793     {
794       application->priv->inactivity_timeout = inactivity_timeout;
795
796       g_object_notify (G_OBJECT (application), "inactivity-timeout");
797     }
798 }
799 /* Read-only property getters (is registered, is remote) {{{1 */
800 /**
801  * g_application_get_is_registered:
802  * @application: a #GApplication
803  * @returns: %TRUE if @application is registered
804  *
805  * Checks if @application is registered.
806  *
807  * An application is registered if g_application_register() has been
808  * successfully called.
809  *
810  * Since: 2.28
811  **/
812 gboolean
813 g_application_get_is_registered (GApplication *application)
814 {
815   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
816
817   return application->priv->is_registered;
818 }
819
820 /**
821  * g_application_get_is_remote:
822  * @application: a #GApplication
823  * @returns: %TRUE if @application is remote
824  *
825  * Checks if @application is remote.
826  *
827  * If @application is remote then it means that another instance of
828  * application already exists (the 'primary' instance).  Calls to
829  * perform actions on @application will result in the actions being
830  * performed by the primary instance.
831  *
832  * The value of this property can not be accessed before
833  * g_application_register() has been called.  See
834  * g_application_get_is_registered().
835  *
836  * Since: 2.28
837  **/
838 gboolean
839 g_application_get_is_remote (GApplication *application)
840 {
841   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
842   g_return_val_if_fail (application->priv->is_registered, FALSE);
843
844   return application->priv->is_remote;
845 }
846
847 /* Register {{{1 */
848 /**
849  * g_application_register:
850  * @application: a #GApplication
851  * @cancellable: a #GCancellable, or %NULL
852  * @error: a pointer to a NULL #GError, or %NULL
853  * @returns: %TRUE if registration succeeded
854  *
855  * Attempts registration of the application.
856  *
857  * This is the point at which the application discovers if it is the
858  * primary instance or merely acting as a remote for an already-existing
859  * primary instance.
860  *
861  * If the application has already been registered then %TRUE is
862  * returned with no work performed.
863  *
864  * The startup() virtual function is invoked if registration succeeds
865  * and @application is the primary instance.
866  *
867  * In the event of an error (such as @cancellable being cancelled, or a
868  * failure to connect to the session bus), %FALSE is returned and @error
869  * is set appropriately.
870  *
871  * Note: the return value of this function is not an indicator that this
872  * instance is or is not the primary instance of the application.  See
873  * g_application_get_is_remote() for that.
874  *
875  * Since: 2.28
876  **/
877 gboolean
878 g_application_register (GApplication  *application,
879                         GCancellable  *cancellable,
880                         GError       **error)
881 {
882   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
883
884   if (!application->priv->is_registered)
885     {
886       gboolean is_remote;
887       gboolean try;
888
889       /* don't try to be the primary instance if
890        * G_APPLICATION_IS_LAUNCHER was specified.
891        */
892       try = !(application->priv->flags & G_APPLICATION_IS_LAUNCHER);
893
894       application->priv->impl =
895         g_application_impl_register (application, application->priv->id,
896                                      application->priv->flags,
897                                      &is_remote, cancellable, error);
898
899       if (application->priv->impl == NULL)
900         return FALSE;
901
902       application->priv->is_remote = is_remote;
903       application->priv->is_registered = TRUE;
904
905       g_object_notify (G_OBJECT (application), "is-registered");
906
907       if (!is_remote)
908         g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
909     }
910
911   return TRUE;
912 }
913
914 /* Hold/release {{{1 */
915 /**
916  * g_application_hold:
917  * @application: a #GApplication
918  *
919  * Increases the use count of @application.
920  *
921  * Use this function to indicate that the application has a reason to
922  * continue to run.  For example, g_application_hold() is called by GTK+ 
923  * when a toplevel window is on the screen.
924  *
925  * To cancel the hold, call g_application_release().
926  **/
927 void
928 g_application_hold (GApplication *application)
929 {
930   if (application->priv->inactivity_timeout_id)
931     {
932       g_source_remove (application->priv->inactivity_timeout_id);
933       application->priv->inactivity_timeout_id = 0;
934     }
935
936   application->priv->use_count++;
937 }
938
939 static gboolean
940 inactivity_timeout_expired (gpointer data)
941 {
942   GApplication *application = G_APPLICATION (data);
943
944   G_APPLICATION_GET_CLASS (application)
945     ->quit_mainloop (application);
946
947   return FALSE;
948 }
949
950
951 /**
952  * g_application_release:
953  * @application: a #GApplication
954  *
955  * Decrease the use count of @application.
956  *
957  * When the use count reaches zero, the application will stop running.
958  *
959  * Never call this function except to cancel the effect of a previous
960  * call to g_application_hold().
961  **/
962 void
963 g_application_release (GApplication *application)
964 {
965   application->priv->use_count--;
966
967   if (application->priv->use_count == 0)
968     {
969       if (application->priv->inactivity_timeout)
970         application->priv->inactivity_timeout_id =
971           g_timeout_add (application->priv->inactivity_timeout,
972                          inactivity_timeout_expired, application);
973
974       else
975         G_APPLICATION_GET_CLASS (application)
976           ->quit_mainloop (application);
977     }
978 }
979
980 /* Activate, Open {{{1 */
981 /**
982  * g_application_activate:
983  * @application: a #GApplication
984  *
985  * Activates the application.
986  *
987  * In essence, this results in the activate() virtual function being
988  * invoked in the primary instance.
989  *
990  * The application must be registered before calling this function.
991  *
992  * Since: 2.28
993  **/
994 void
995 g_application_activate (GApplication *application)
996 {
997   g_return_if_fail (G_IS_APPLICATION (application));
998   g_return_if_fail (application->priv->is_registered);
999
1000   if (application->priv->is_remote)
1001     g_application_impl_activate (application->priv->impl,
1002                                  get_platform_data (application));
1003
1004   else
1005     g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1006 }
1007
1008 /**
1009  * g_application_open:
1010  * @application: a #GApplication
1011  * @files: an array of #GFile<!-- -->s to open
1012  * @n_files: the length of the @files array
1013  * @hint: a hint (or ""), but never %NULL
1014  *
1015  * Opens the given files.
1016  *
1017  * In essence, this results in the open() virtual function being invoked
1018  * in the primary instance.
1019  *
1020  * @n_files must be greater than zero.
1021  *
1022  * @hint is simply passed through to the open() virtual function.  It is
1023  * intended to be used by applications that have multiple modes for
1024  * opening files (eg: "view" vs "edit", etc).  Unless you have a need
1025  * for this functionality, you should use "".
1026  *
1027  * The application must be registered before calling this function and
1028  * it must have the %G_APPLICATION_HANDLES_OPEN flag set.  The open()
1029  * virtual function should also be implemented in order for anything
1030  * meaningful to happen.
1031  *
1032  * Since: 2.28
1033  **/
1034 void
1035 g_application_open (GApplication  *application,
1036                     GFile        **files,
1037                     gint           n_files,
1038                     const gchar   *hint)
1039 {
1040   g_return_if_fail (G_IS_APPLICATION (application));
1041   g_return_if_fail (application->priv->flags &
1042                     G_APPLICATION_HANDLES_OPEN);
1043   g_return_if_fail (application->priv->is_registered);
1044
1045   if (application->priv->is_remote)
1046     g_application_impl_open (application->priv->impl,
1047                              files, n_files, hint,
1048                              get_platform_data (application));
1049
1050   else
1051     g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1052                    0, files, n_files, hint);
1053 }
1054
1055 /* Run {{{1 */
1056 /**
1057  * g_application_run:
1058  * @application: a #GApplication
1059  * @argc: the argc from main()
1060  * @argv: the argv from main()
1061  * @returns: the exit status
1062  *
1063  * Runs the application.
1064  *
1065  * This function is intended to be run from main() and its return value
1066  * is intended to be returned by main().
1067  *
1068  * First, the local_command_line() virtual function is invoked.  This
1069  * function always runs on the local instance.  If that function returns
1070  * %FALSE then the application is registered and the #GApplication::command-line
1071  * signal is emitted in the primary instance (which may or may not be
1072  * this instance).
1073  *
1074  * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1075  * flag set then the default implementation of local_command_line()
1076  * always returns %FALSE immediately, resulting in the commandline
1077  * always being handled in the primary instance.
1078  *
1079  * Otherwise, the default implementation of local_command_line() tries
1080  * to do a couple of things that are probably reasonable for most
1081  * applications.  First, g_application_register() is called to attempt
1082  * to register the application.  If that works, then the command line
1083  * arguments are inspected.  If no commandline arguments are given, then
1084  * g_application_activate() is called.  If commandline arguments are
1085  * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1086  * are assumed to be filenames and g_application_open() is called.
1087  *
1088  * If you are interested in doing more complicated local handling of the
1089  * commandline then you should implement your own #GApplication subclass
1090  * and override local_command_line(). See
1091  * <xref linkend="gapplication-example-cmdline2"/> for an example.
1092  *
1093  * If, after the above is done, the use count of the application is zero
1094  * then the exit status is returned immediately.  If the use count is
1095  * non-zero then the mainloop is run until the use count falls to zero,
1096  * at which point 0 is returned.
1097  *
1098  * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1099  * use count of zero is delayed for a while (ie: the instance stays
1100  * around to provide its <emphasis>service</emphasis> to others).
1101  *
1102  * Since: 2.28
1103  **/
1104 int
1105 g_application_run (GApplication  *application,
1106                    int            argc,
1107                    char         **argv)
1108 {
1109   gchar **arguments;
1110   int status;
1111   gint i;
1112
1113   g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1114   g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1115
1116   arguments = g_new (gchar *, argc + 1);
1117   for (i = 0; i < argc; i++)
1118     arguments[i] = g_strdup (argv[i]);
1119   arguments[i] = NULL;
1120
1121   if (g_get_prgname () == NULL && argc > 0)
1122     {
1123       gchar *prgname;
1124
1125       prgname = g_path_get_basename (argv[0]);
1126       g_set_prgname (prgname);
1127       g_free (prgname);
1128     }
1129
1130   if (!G_APPLICATION_GET_CLASS (application)
1131         ->local_command_line (application, &arguments, &status))
1132     {
1133       GError *error = NULL;
1134
1135       if (!g_application_register (application, NULL, &error))
1136         {
1137           g_printerr ("%s", error->message);
1138           g_error_free (error);
1139           return 1;
1140         }
1141
1142       if (application->priv->is_remote)
1143         {
1144           GVariant *platform_data;
1145
1146           platform_data = get_platform_data (application);
1147           status = g_application_impl_command_line (application->priv->impl,
1148                                                     arguments, platform_data);
1149         }
1150       else
1151         {
1152           GApplicationCommandLine *cmdline;
1153           GVariant *v;
1154
1155           v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1156           cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1157                                   "arguments", v, NULL);
1158           g_signal_emit (application,
1159                          g_application_signals[SIGNAL_COMMAND_LINE],
1160                          0, cmdline, &status);
1161           g_object_unref (cmdline);
1162         }
1163     }
1164
1165   g_strfreev (arguments);
1166
1167   if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1168       !application->priv->use_count &&
1169       !application->priv->inactivity_timeout_id)
1170     {
1171       application->priv->inactivity_timeout_id =
1172         g_timeout_add (10000, inactivity_timeout_expired, application);
1173     }
1174
1175   if (application->priv->use_count ||
1176       application->priv->inactivity_timeout_id)
1177     {
1178       G_APPLICATION_GET_CLASS (application)
1179         ->run_mainloop (application);
1180       status = 0;
1181     }
1182
1183   if (application->priv->impl)
1184     g_application_impl_flush (application->priv->impl);
1185
1186   return status;
1187 }
1188
1189 static gboolean
1190 g_application_has_action (GActionGroup *action_group,
1191                           const gchar  *action_name)
1192 {
1193   GApplication *application = G_APPLICATION (action_group);
1194
1195   g_return_val_if_fail (application->priv->is_registered, FALSE);
1196
1197   return application->priv->actions &&
1198          g_action_group_has_action (application->priv->actions, action_name);
1199 }
1200
1201 static gchar **
1202 g_application_list_actions (GActionGroup *action_group)
1203 {
1204   GApplication *application = G_APPLICATION (action_group);
1205
1206   g_return_val_if_fail (application->priv->is_registered, NULL);
1207
1208   if (application->priv->actions != NULL)
1209     return g_action_group_list_actions (application->priv->actions);
1210
1211   else
1212     /* empty string array */
1213     return g_new0 (gchar *, 1);
1214 }
1215
1216 static gboolean
1217 g_application_get_action_enabled (GActionGroup *action_group,
1218                                   const gchar  *action_name)
1219 {
1220   GApplication *application = G_APPLICATION (action_group);
1221
1222   g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1223   g_return_val_if_fail (application->priv->is_registered, FALSE);
1224
1225   return g_action_group_get_action_enabled (application->priv->actions,
1226                                             action_name);
1227 }
1228
1229 static const GVariantType *
1230 g_application_get_action_parameter_type (GActionGroup *action_group,
1231                                          const gchar  *action_name)
1232 {
1233   GApplication *application = G_APPLICATION (action_group);
1234
1235   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1236   g_return_val_if_fail (application->priv->is_registered, NULL);
1237
1238   return g_action_group_get_action_parameter_type (application->priv->actions,
1239                                                    action_name);
1240 }
1241
1242 static const GVariantType *
1243 g_application_get_action_state_type (GActionGroup *action_group,
1244                                      const gchar  *action_name)
1245 {
1246   GApplication *application = G_APPLICATION (action_group);
1247
1248   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1249   g_return_val_if_fail (application->priv->is_registered, NULL);
1250
1251   return g_action_group_get_action_state_type (application->priv->actions,
1252                                                action_name);
1253 }
1254
1255 static GVariant *
1256 g_application_get_action_state_hint (GActionGroup *action_group,
1257                                      const gchar  *action_name)
1258 {
1259   GApplication *application = G_APPLICATION (action_group);
1260
1261   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1262   g_return_val_if_fail (application->priv->is_registered, NULL);
1263
1264   return g_action_group_get_action_state_hint (application->priv->actions,
1265                                                action_name);
1266 }
1267
1268 static GVariant *
1269 g_application_get_action_state (GActionGroup *action_group,
1270                                 const gchar  *action_name)
1271 {
1272   GApplication *application = G_APPLICATION (action_group);
1273
1274   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1275   g_return_val_if_fail (application->priv->is_registered, NULL);
1276
1277   return g_action_group_get_action_state (application->priv->actions,
1278                                           action_name);
1279 }
1280
1281 static void
1282 g_application_change_action_state (GActionGroup *action_group,
1283                                    const gchar  *action_name,
1284                                    GVariant     *value)
1285 {
1286   GApplication *application = G_APPLICATION (action_group);
1287
1288   g_return_if_fail (application->priv->actions != NULL);
1289   g_return_if_fail (application->priv->is_registered);
1290
1291   g_action_group_change_action_state (application->priv->actions,
1292                                       action_name, value);
1293 }
1294
1295 static void
1296 g_application_activate_action (GActionGroup *action_group,
1297                                const gchar  *action_name,
1298                                GVariant     *parameter)
1299 {
1300   GApplication *application = G_APPLICATION (action_group);
1301
1302   g_return_if_fail (application->priv->actions != NULL);
1303   g_return_if_fail (application->priv->is_registered);
1304
1305   g_action_group_activate_action (application->priv->actions,
1306                                   action_name, parameter);
1307 }
1308
1309 static void
1310 g_application_action_group_iface_init (GActionGroupInterface *iface)
1311 {
1312   iface->has_action = g_application_has_action;
1313   iface->list_actions = g_application_list_actions;
1314
1315   iface->get_action_enabled = g_application_get_action_enabled;
1316   iface->get_action_parameter_type = g_application_get_action_parameter_type;
1317   iface->get_action_state_type = g_application_get_action_state_type;
1318   iface->get_action_state_hint = g_application_get_action_state_hint;
1319   iface->get_action_state = g_application_get_action_state;
1320   iface->change_action_state = g_application_change_action_state;
1321   iface->activate_action = g_application_activate_action;
1322 }
1323
1324 /* Epilogue {{{1 */
1325 /* vim:set foldmethod=marker: */