GApplication: support environment passing
[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 details on valid application identifiers, see
65  * g_application_id_is_valid().
66  *
67  * GApplication provides convenient life cycle management by maintaining
68  * a <firstterm>use count</firstterm> for the primary application instance.
69  * The use count can be changed using g_application_hold() and
70  * g_application_release(). If it drops to zero, the application exits.
71  *
72  * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
73  * <programlisting>
74  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
75  *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
76  * </xi:include>
77  * </programlisting>
78  * </example>
79  */
80
81 struct _GApplicationPrivate
82 {
83   GApplicationFlags  flags;
84   gchar             *id;
85
86   GActionGroup      *actions;
87   GMainLoop         *mainloop;
88
89   guint              inactivity_timeout_id;
90   guint              inactivity_timeout;
91   guint              use_count;
92
93   guint              is_registered : 1;
94   guint              is_remote : 1;
95
96   GHashTable        *remote_actions;  /* string -> RemoteActionInfo */
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 ((*exit_status = 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_uint ("inactivity-timeout",
486                        P_("Inactivity timeout"),
487                        P_("Iime (ms) to stay alive after becoming idle"),
488                        0, G_MAXUINT, 0,
489                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490
491   g_object_class_install_property (object_class, PROP_ACTION_GROUP,
492     g_param_spec_object ("action-group",
493                          P_("Action group"),
494                          P_("The group of actions that the application exports"),
495                          G_TYPE_ACTION_GROUP, 
496                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
497
498   /**
499    * GApplication::startup:
500    * @application: the application
501    *
502    * The ::startup signal is emitted on the primary instance immediately
503    * after registration. See g_activation_register().
504    */
505   g_application_signals[SIGNAL_STARTUP] =
506     g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
507                   G_STRUCT_OFFSET (GApplicationClass, startup),
508                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
509
510   /**
511    * GApplication::activate:
512    * @application: the application
513    *
514    * The ::activate signal is emitted on the primary instance when an
515    * activation occurs. See g_application_activate().
516    */
517   g_application_signals[SIGNAL_ACTIVATE] =
518     g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
519                   G_STRUCT_OFFSET (GApplicationClass, activate),
520                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
521
522
523   /**
524    * GApplication::open:
525    * @application: the application
526    * @files: an array of #GFile objects
527    * @n_files: the length of @files
528    * @hint: a hint provided by the calling instance
529    *
530    * The ::open signal is emitted on the primary instance when there are
531    * files to open. See g_application_open() for more information.
532    */
533   g_application_signals[SIGNAL_OPEN] =
534     g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
535                   G_STRUCT_OFFSET (GApplicationClass, open),
536                   NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
537                   G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
538
539   /**
540    * GApplication::command-line:
541    * @application: the application
542    * @command_line: a #GApplicationCommandLine representing the
543    *     passed commandline
544    *
545    * The ::command-line signal is emitted on the primary instance when
546    * a commandline is not handled locally. See g_application_run() for
547    * more information.
548    */
549   g_application_signals[SIGNAL_COMMAND_LINE] =
550     g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
551                   G_STRUCT_OFFSET (GApplicationClass, command_line),
552                   g_signal_accumulator_first_wins, NULL,
553                   _gio_marshal_INT__OBJECT,
554                   G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
555
556   g_type_class_add_private (class, sizeof (GApplicationPrivate));
557 }
558
559 static GVariant *
560 get_platform_data (GApplication *application)
561 {
562   GVariantBuilder *builder;
563   GVariant *result;
564
565   builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
566
567   {
568     gchar *cwd = g_get_current_dir ();
569     g_variant_builder_add (builder, "{sv}", "cwd",
570                            g_variant_new_bytestring (cwd));
571     g_free (cwd);
572   }
573
574   if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
575     {
576       gchar **envp = g_get_environ ();
577       g_variant_builder_add (builder, "{sv}", "environ",
578                              g_variant_new_strv ((const gchar **) envp, -1));
579       g_strfreev (envp);
580     }
581
582   G_APPLICATION_GET_CLASS (application)->
583     add_platform_data (application, builder);
584
585   result = g_variant_builder_end (builder);
586   g_variant_builder_unref (builder);
587
588   return result;
589 }
590
591 /* Application ID validity {{{1 */
592
593 /**
594  * g_application_id_is_valid:
595  * @application_id: a potential application identifier
596  * @returns: %TRUE if @application_id is valid
597  *
598  * Checks if @application_id is a valid application identifier.
599  *
600  * A valid ID is required for calls to g_application_new() and
601  * g_application_set_application_id().
602  *
603  * For convenience, the restrictions on application identifiers are
604  * reproduced here:
605  * <itemizedlist>
606  *   <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
607  *   <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
608  *   <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
609  *   <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
610  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
611  * </itemizedlist>
612  **/
613 gboolean
614 g_application_id_is_valid (const gchar *application_id)
615 {
616   gboolean allow_dot;
617
618   if (strlen (application_id) > 255)
619     return FALSE;
620
621   if (!g_ascii_isalpha (*application_id))
622     return FALSE;
623
624   application_id++;
625   allow_dot = FALSE;
626   for (; *application_id; application_id++)
627     {
628       if (g_ascii_isalnum (*application_id) ||
629           (*application_id == '-') ||
630           (*application_id == '_'))
631         allow_dot = TRUE;
632       else if (allow_dot && *application_id == '.')
633         allow_dot = FALSE;
634       else
635         return FALSE;
636     }
637
638   return TRUE;
639 }
640  
641 /* Public Constructor {{{1 */
642 /**
643  * g_application_new:
644  * @application_id: the application id
645  * @flags: the application flags
646  * @returns: a new #GApplication instance
647  *
648  * Creates a new #GApplication instance.
649  *
650  * This function calls g_type_init() for you.
651  *
652  * The application id must be valid.  See g_application_id_is_valid().
653  **/
654 GApplication *
655 g_application_new (const gchar       *application_id,
656                    GApplicationFlags  flags)
657 {
658   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
659
660   g_type_init ();
661
662   return g_object_new (G_TYPE_APPLICATION,
663                        "application-id", application_id,
664                        "flags", flags,
665                        NULL);
666 }
667
668 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
669 /**
670  * g_application_get_application_id:
671  * @application: a #GApplication
672  * @returns: the identifier for @application, owned by @application
673  *
674  * Gets the unique identifier for @application.
675  *
676  * Since: 2.28
677  **/
678 const gchar *
679 g_application_get_application_id (GApplication *application)
680 {
681   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
682
683   return application->priv->id;
684 }
685
686 /**
687  * g_application_set_application_id:
688  * @application: a #GApplication
689  * @application_id: the identifier for @application
690  *
691  * Sets the unique identifier for @application.
692  *
693  * The application id can only be modified if @application has not yet
694  * been registered.
695  *
696  * The application id must be valid.  See g_application_id_is_valid().
697  *
698  * Since: 2.28
699  **/
700 void
701 g_application_set_application_id (GApplication *application,
702                                   const gchar  *application_id)
703 {
704   g_return_if_fail (G_IS_APPLICATION (application));
705
706   if (g_strcmp0 (application->priv->id, application_id) != 0)
707     {
708       g_return_if_fail (g_application_id_is_valid (application_id));
709       g_return_if_fail (!application->priv->is_registered);
710
711       g_free (application->priv->id);
712       application->priv->id = g_strdup (application_id);
713
714       g_object_notify (G_OBJECT (application), "application-id");
715     }
716 }
717
718 /**
719  * g_application_get_flags:
720  * @application: a #GApplication
721  * @returns: the flags for @application
722  *
723  * Gets the flags for @application.
724  *
725  * See #GApplicationFlags.
726  *
727  * Since: 2.28
728  **/
729 GApplicationFlags
730 g_application_get_flags (GApplication *application)
731 {
732   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
733
734   return application->priv->flags;
735 }
736
737 /**
738  * g_application_set_flags:
739  * @application: a #GApplication
740  * @flags: the flags for @application
741  *
742  * Sets the flags for @application.
743  *
744  * The flags can only be modified if @application has not yet been
745  * registered.
746  *
747  * See #GApplicationFlags.
748  *
749  * Since: 2.28
750  **/
751 void
752 g_application_set_flags (GApplication      *application,
753                          GApplicationFlags  flags)
754 {
755   g_return_if_fail (G_IS_APPLICATION (application));
756
757   if (application->priv->flags != flags)
758     {
759       g_return_if_fail (!application->priv->is_registered);
760
761       application->priv->flags = flags;
762
763       g_object_notify (G_OBJECT (application), "flags");
764     }
765 }
766
767 /**
768  * g_application_get_inactivity_timeout:
769  * @application: a #GApplication
770  *
771  * Gets the current inactivity timeout for the application.
772  *
773  * This is the amount of time (in milliseconds) after the last call to
774  * g_application_release() before the application stops running.
775  *
776  * Returns: the timeout, in milliseconds
777  *
778  * Since: 2.28
779  **/
780 guint
781 g_application_get_inactivity_timeout (GApplication *application)
782 {
783   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
784
785   return application->priv->inactivity_timeout;
786 }
787
788 /**
789  * g_application_set_inactivity_timeout:
790  * @application: a #GApplication
791  * @inactivity_timeout: the timeout, in milliseconds
792  *
793  * Sets the current inactivity timeout for the application.
794  *
795  * This is the amount of time (in milliseconds) after the last call to
796  * g_application_release() before the application stops running.
797  *
798  * This call has no side effects of its own.  The value set here is only
799  * used for next time g_application_release() drops the use count to
800  * zero.  Any timeouts currently in progress are not impacted.
801  *
802  * Returns: the timeout, in milliseconds
803  *
804  * Since: 2.28
805  **/
806 void
807 g_application_set_inactivity_timeout (GApplication *application,
808                                       guint         inactivity_timeout)
809 {
810   g_return_if_fail (G_IS_APPLICATION (application));
811
812   if (application->priv->inactivity_timeout != inactivity_timeout)
813     {
814       application->priv->inactivity_timeout = inactivity_timeout;
815
816       g_object_notify (G_OBJECT (application), "inactivity-timeout");
817     }
818 }
819 /* Read-only property getters (is registered, is remote) {{{1 */
820 /**
821  * g_application_get_is_registered:
822  * @application: a #GApplication
823  * @returns: %TRUE if @application is registered
824  *
825  * Checks if @application is registered.
826  *
827  * An application is registered if g_application_register() has been
828  * successfully called.
829  *
830  * Since: 2.28
831  **/
832 gboolean
833 g_application_get_is_registered (GApplication *application)
834 {
835   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
836
837   return application->priv->is_registered;
838 }
839
840 /**
841  * g_application_get_is_remote:
842  * @application: a #GApplication
843  * @returns: %TRUE if @application is remote
844  *
845  * Checks if @application is remote.
846  *
847  * If @application is remote then it means that another instance of
848  * application already exists (the 'primary' instance).  Calls to
849  * perform actions on @application will result in the actions being
850  * performed by the primary instance.
851  *
852  * The value of this property can not be accessed before
853  * g_application_register() has been called.  See
854  * g_application_get_is_registered().
855  *
856  * Since: 2.28
857  **/
858 gboolean
859 g_application_get_is_remote (GApplication *application)
860 {
861   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
862   g_return_val_if_fail (application->priv->is_registered, FALSE);
863
864   return application->priv->is_remote;
865 }
866
867 /* Register {{{1 */
868 /**
869  * g_application_register:
870  * @application: a #GApplication
871  * @cancellable: a #GCancellable, or %NULL
872  * @error: a pointer to a NULL #GError, or %NULL
873  * @returns: %TRUE if registration succeeded
874  *
875  * Attempts registration of the application.
876  *
877  * This is the point at which the application discovers if it is the
878  * primary instance or merely acting as a remote for an already-existing
879  * primary instance.
880  *
881  * If the application has already been registered then %TRUE is
882  * returned with no work performed.
883  *
884  * The #GApplication::startup signal is emitted if registration succeeds
885  * and @application is the primary instance.
886  *
887  * In the event of an error (such as @cancellable being cancelled, or a
888  * failure to connect to the session bus), %FALSE is returned and @error
889  * is set appropriately.
890  *
891  * Note: the return value of this function is not an indicator that this
892  * instance is or is not the primary instance of the application.  See
893  * g_application_get_is_remote() for that.
894  *
895  * Since: 2.28
896  **/
897 gboolean
898 g_application_register (GApplication  *application,
899                         GCancellable  *cancellable,
900                         GError       **error)
901 {
902   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
903
904   if (!application->priv->is_registered)
905     {
906       application->priv->impl =
907         g_application_impl_register (application, application->priv->id,
908                                      application->priv->flags,
909                                      &application->priv->remote_actions,
910                                      cancellable, error);
911
912       if (application->priv->impl == NULL)
913         return FALSE;
914
915       application->priv->is_remote = application->priv->remote_actions != NULL;
916       application->priv->is_registered = TRUE;
917
918       g_object_notify (G_OBJECT (application), "is-registered");
919
920       if (!application->priv->is_remote)
921         g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
922     }
923
924   return TRUE;
925 }
926
927 /* Hold/release {{{1 */
928 /**
929  * g_application_hold:
930  * @application: a #GApplication
931  *
932  * Increases the use count of @application.
933  *
934  * Use this function to indicate that the application has a reason to
935  * continue to run.  For example, g_application_hold() is called by GTK+ 
936  * when a toplevel window is on the screen.
937  *
938  * To cancel the hold, call g_application_release().
939  **/
940 void
941 g_application_hold (GApplication *application)
942 {
943   if (application->priv->inactivity_timeout_id)
944     {
945       g_source_remove (application->priv->inactivity_timeout_id);
946       application->priv->inactivity_timeout_id = 0;
947     }
948
949   application->priv->use_count++;
950 }
951
952 static gboolean
953 inactivity_timeout_expired (gpointer data)
954 {
955   GApplication *application = G_APPLICATION (data);
956
957   G_APPLICATION_GET_CLASS (application)
958     ->quit_mainloop (application);
959
960   return FALSE;
961 }
962
963
964 /**
965  * g_application_release:
966  * @application: a #GApplication
967  *
968  * Decrease the use count of @application.
969  *
970  * When the use count reaches zero, the application will stop running.
971  *
972  * Never call this function except to cancel the effect of a previous
973  * call to g_application_hold().
974  **/
975 void
976 g_application_release (GApplication *application)
977 {
978   application->priv->use_count--;
979
980   if (application->priv->use_count == 0)
981     {
982       if (application->priv->inactivity_timeout)
983         application->priv->inactivity_timeout_id =
984           g_timeout_add (application->priv->inactivity_timeout,
985                          inactivity_timeout_expired, application);
986
987       else
988         G_APPLICATION_GET_CLASS (application)
989           ->quit_mainloop (application);
990     }
991 }
992
993 /* Activate, Open {{{1 */
994 /**
995  * g_application_activate:
996  * @application: a #GApplication
997  *
998  * Activates the application.
999  *
1000  * In essence, this results in the #GApplication::activate() signal being
1001  * emitted in the primary instance.
1002  *
1003  * The application must be registered before calling this function.
1004  *
1005  * Since: 2.28
1006  **/
1007 void
1008 g_application_activate (GApplication *application)
1009 {
1010   g_return_if_fail (G_IS_APPLICATION (application));
1011   g_return_if_fail (application->priv->is_registered);
1012
1013   if (application->priv->is_remote)
1014     g_application_impl_activate (application->priv->impl,
1015                                  get_platform_data (application));
1016
1017   else
1018     g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1019 }
1020
1021 /**
1022  * g_application_open:
1023  * @application: a #GApplication
1024  * @files: an array of #GFiles to open
1025  * @n_files: the length of the @files array
1026  * @hint: a hint (or ""), but never %NULL
1027  *
1028  * Opens the given files.
1029  *
1030  * In essence, this results in the #GApplication::open signal being emitted
1031  * in the primary instance.
1032  *
1033  * @n_files must be greater than zero.
1034  *
1035  * @hint is simply passed through to the ::open signal.  It is
1036  * intended to be used by applications that have multiple modes for
1037  * opening files (eg: "view" vs "edit", etc).  Unless you have a need
1038  * for this functionality, you should use "".
1039  *
1040  * The application must be registered before calling this function
1041  * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1042  *
1043  * Since: 2.28
1044  **/
1045 void
1046 g_application_open (GApplication  *application,
1047                     GFile        **files,
1048                     gint           n_files,
1049                     const gchar   *hint)
1050 {
1051   g_return_if_fail (G_IS_APPLICATION (application));
1052   g_return_if_fail (application->priv->flags &
1053                     G_APPLICATION_HANDLES_OPEN);
1054   g_return_if_fail (application->priv->is_registered);
1055
1056   if (application->priv->is_remote)
1057     g_application_impl_open (application->priv->impl,
1058                              files, n_files, hint,
1059                              get_platform_data (application));
1060
1061   else
1062     g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1063                    0, files, n_files, hint);
1064 }
1065
1066 /* Run {{{1 */
1067 /**
1068  * g_application_run:
1069  * @application: a #GApplication
1070  * @argc: the argc from main()
1071  * @argv: the argv from main()
1072  * @returns: the exit status
1073  *
1074  * Runs the application.
1075  *
1076  * This function is intended to be run from main() and its return value
1077  * is intended to be returned by main().
1078  *
1079  * First, the local_command_line() virtual function is invoked.  This
1080  * function always runs on the local instance.  If that function returns
1081  * %FALSE then the application is registered and the #GApplication::command-line
1082  * signal is emitted in the primary instance (which may or may not be
1083  * this instance).
1084  *
1085  * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1086  * flag set then the default implementation of local_command_line()
1087  * always returns %FALSE immediately, resulting in the commandline
1088  * always being handled in the primary instance.
1089  *
1090  * Otherwise, the default implementation of local_command_line() tries
1091  * to do a couple of things that are probably reasonable for most
1092  * applications.  First, g_application_register() is called to attempt
1093  * to register the application.  If that works, then the command line
1094  * arguments are inspected.  If no commandline arguments are given, then
1095  * g_application_activate() is called.  If commandline arguments are
1096  * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1097  * are assumed to be filenames and g_application_open() is called.
1098  *
1099  * If you are interested in doing more complicated local handling of the
1100  * commandline then you should implement your own #GApplication subclass
1101  * and override local_command_line(). See
1102  * <xref linkend="gapplication-example-cmdline2"/> for an example.
1103  *
1104  * If, after the above is done, the use count of the application is zero
1105  * then the exit status is returned immediately.  If the use count is
1106  * non-zero then the mainloop is run until the use count falls to zero,
1107  * at which point 0 is returned.
1108  *
1109  * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1110  * use count of zero is delayed for a while (ie: the instance stays
1111  * around to provide its <emphasis>service</emphasis> to others).
1112  *
1113  * Since: 2.28
1114  **/
1115 int
1116 g_application_run (GApplication  *application,
1117                    int            argc,
1118                    char         **argv)
1119 {
1120   gchar **arguments;
1121   int status;
1122   gint i;
1123
1124   g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1125   g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1126
1127   arguments = g_new (gchar *, argc + 1);
1128   for (i = 0; i < argc; i++)
1129     arguments[i] = g_strdup (argv[i]);
1130   arguments[i] = NULL;
1131
1132   if (g_get_prgname () == NULL && argc > 0)
1133     {
1134       gchar *prgname;
1135
1136       prgname = g_path_get_basename (argv[0]);
1137       g_set_prgname (prgname);
1138       g_free (prgname);
1139     }
1140
1141   if (!G_APPLICATION_GET_CLASS (application)
1142         ->local_command_line (application, &arguments, &status))
1143     {
1144       GError *error = NULL;
1145
1146       if (!g_application_register (application, NULL, &error))
1147         {
1148           g_printerr ("%s", error->message);
1149           g_error_free (error);
1150           return 1;
1151         }
1152
1153       if (application->priv->is_remote)
1154         {
1155           GVariant *platform_data;
1156
1157           platform_data = get_platform_data (application);
1158           status = g_application_impl_command_line (application->priv->impl,
1159                                                     arguments, platform_data);
1160         }
1161       else
1162         {
1163           GApplicationCommandLine *cmdline;
1164           GVariant *v;
1165
1166           v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1167           cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1168                                   "arguments", v, NULL);
1169           g_signal_emit (application,
1170                          g_application_signals[SIGNAL_COMMAND_LINE],
1171                          0, cmdline, &status);
1172           g_object_unref (cmdline);
1173         }
1174     }
1175
1176   g_strfreev (arguments);
1177
1178   if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1179       application->priv->is_registered &&
1180       !application->priv->use_count &&
1181       !application->priv->inactivity_timeout_id)
1182     {
1183       application->priv->inactivity_timeout_id =
1184         g_timeout_add (10000, inactivity_timeout_expired, application);
1185     }
1186
1187   if (application->priv->use_count ||
1188       application->priv->inactivity_timeout_id)
1189     {
1190       G_APPLICATION_GET_CLASS (application)
1191         ->run_mainloop (application);
1192       status = 0;
1193     }
1194
1195   if (application->priv->impl)
1196     g_application_impl_flush (application->priv->impl);
1197
1198   return status;
1199 }
1200
1201 static gboolean
1202 g_application_has_action (GActionGroup *action_group,
1203                           const gchar  *action_name)
1204 {
1205   GApplication *application = G_APPLICATION (action_group);
1206
1207   g_return_val_if_fail (application->priv->is_registered, FALSE);
1208
1209   if (application->priv->remote_actions != NULL)
1210     return g_hash_table_lookup (application->priv->remote_actions,
1211                                 action_name) != NULL;
1212
1213   return application->priv->actions &&
1214          g_action_group_has_action (application->priv->actions, action_name);
1215 }
1216
1217 static gchar **
1218 g_application_list_actions (GActionGroup *action_group)
1219 {
1220   GApplication *application = G_APPLICATION (action_group);
1221
1222   g_return_val_if_fail (application->priv->is_registered, NULL);
1223
1224   if (application->priv->remote_actions != NULL)
1225     {
1226       GHashTableIter iter;
1227       gint n, i = 0;
1228       gchar **keys;
1229       gpointer key;
1230
1231       n = g_hash_table_size (application->priv->remote_actions);
1232       keys = g_new (gchar *, n + 1);
1233
1234       g_hash_table_iter_init (&iter, application->priv->remote_actions);
1235       while (g_hash_table_iter_next (&iter, &key, NULL))
1236         keys[i++] = g_strdup (key);
1237       g_assert_cmpint (i, ==, n);
1238       keys[n] = NULL;
1239
1240       return keys;
1241     }
1242
1243   else if (application->priv->actions != NULL)
1244     return g_action_group_list_actions (application->priv->actions);
1245
1246   else
1247     /* empty string array */
1248     return g_new0 (gchar *, 1);
1249 }
1250
1251 static gboolean
1252 g_application_get_action_enabled (GActionGroup *action_group,
1253                                   const gchar  *action_name)
1254 {
1255   GApplication *application = G_APPLICATION (action_group);
1256
1257   g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1258   g_return_val_if_fail (application->priv->is_registered, FALSE);
1259
1260   if (application->priv->remote_actions)
1261     {
1262       RemoteActionInfo *info;
1263
1264       info = g_hash_table_lookup (application->priv->remote_actions,
1265                                   action_name);
1266
1267       return info && info->enabled;
1268     }
1269
1270   return g_action_group_get_action_enabled (application->priv->actions,
1271                                             action_name);
1272 }
1273
1274 static const GVariantType *
1275 g_application_get_action_parameter_type (GActionGroup *action_group,
1276                                          const gchar  *action_name)
1277 {
1278   GApplication *application = G_APPLICATION (action_group);
1279
1280   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1281   g_return_val_if_fail (application->priv->is_registered, NULL);
1282
1283   if (application->priv->remote_actions)
1284     {
1285       RemoteActionInfo *info;
1286
1287       info = g_hash_table_lookup (application->priv->remote_actions,
1288                                   action_name);
1289
1290       if (info)
1291         return info->parameter_type;
1292       else
1293         return NULL;
1294     }
1295
1296   return g_action_group_get_action_parameter_type (application->priv->actions,
1297                                                    action_name);
1298 }
1299
1300 static const GVariantType *
1301 g_application_get_action_state_type (GActionGroup *action_group,
1302                                      const gchar  *action_name)
1303 {
1304   GApplication *application = G_APPLICATION (action_group);
1305
1306   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1307   g_return_val_if_fail (application->priv->is_registered, NULL);
1308
1309   if (application->priv->remote_actions)
1310     {
1311       RemoteActionInfo *info;
1312
1313       info = g_hash_table_lookup (application->priv->remote_actions,
1314                                   action_name);
1315
1316       if (info && info->state)
1317         return g_variant_get_type (info->state);
1318       else
1319         return NULL;
1320     }
1321
1322   return g_action_group_get_action_state_type (application->priv->actions,
1323                                                action_name);
1324 }
1325
1326 static GVariant *
1327 g_application_get_action_state (GActionGroup *action_group,
1328                                 const gchar  *action_name)
1329 {
1330   GApplication *application = G_APPLICATION (action_group);
1331
1332   g_return_val_if_fail (application->priv->actions != NULL, NULL);
1333   g_return_val_if_fail (application->priv->is_registered, NULL);
1334
1335   if (application->priv->remote_actions)
1336     {
1337       RemoteActionInfo *info;
1338
1339       info = g_hash_table_lookup (application->priv->remote_actions,
1340                                   action_name);
1341
1342       if (info && info->state)
1343         return g_variant_ref (info->state);
1344       else
1345         return NULL;
1346     }
1347
1348   return g_action_group_get_action_state (application->priv->actions,
1349                                           action_name);
1350 }
1351
1352 static void
1353 g_application_change_action_state (GActionGroup *action_group,
1354                                    const gchar  *action_name,
1355                                    GVariant     *value)
1356 {
1357   GApplication *application = G_APPLICATION (action_group);
1358
1359   g_return_if_fail (application->priv->is_registered);
1360
1361   if (application->priv->is_remote)
1362     g_application_impl_change_action_state (application->priv->impl,
1363                                             action_name, value,
1364                                             get_platform_data (application));
1365
1366   else
1367     g_action_group_change_action_state (application->priv->actions,
1368                                         action_name, value);
1369 }
1370
1371 static void
1372 g_application_activate_action (GActionGroup *action_group,
1373                                const gchar  *action_name,
1374                                GVariant     *parameter)
1375 {
1376   GApplication *application = G_APPLICATION (action_group);
1377
1378   g_return_if_fail (application->priv->is_registered);
1379
1380   if (application->priv->is_remote)
1381     g_application_impl_activate_action (application->priv->impl,
1382                                         action_name, parameter,
1383                                         get_platform_data (application));
1384
1385   else
1386     g_action_group_activate_action (application->priv->actions,
1387                                     action_name, parameter);
1388 }
1389
1390 static void
1391 g_application_action_group_iface_init (GActionGroupInterface *iface)
1392 {
1393   iface->has_action = g_application_has_action;
1394   iface->list_actions = g_application_list_actions;
1395
1396   iface->get_action_enabled = g_application_get_action_enabled;
1397   iface->get_action_parameter_type = g_application_get_action_parameter_type;
1398   iface->get_action_state_type = g_application_get_action_state_type;
1399   iface->get_action_state = g_application_get_action_state;
1400   iface->change_action_state = g_application_change_action_state;
1401   iface->activate_action = g_application_activate_action;
1402 }
1403
1404 /* Epilogue {{{1 */
1405 /* vim:set foldmethod=marker: */