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