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