gapplication: Stop handle-local-options emission on errors
[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, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 /* Prologue {{{1 */
21 #include "config.h"
22
23 #include "gapplication.h"
24
25 #include "gapplicationcommandline.h"
26 #include "gsimpleactiongroup.h"
27 #include "gremoteactiongroup.h"
28 #include "gapplicationimpl.h"
29 #include "gactiongroup.h"
30 #include "gactionmap.h"
31 #include "gmenumodel.h"
32 #include "gsettings.h"
33 #include "gnotification-private.h"
34 #include "gnotificationbackend.h"
35 #include "gdbusutils.h"
36
37 #include "gioenumtypes.h"
38 #include "gioenums.h"
39 #include "gfile.h"
40
41 #include "glibintl.h"
42
43 #include <string.h>
44
45 /**
46  * SECTION:gapplication
47  * @title: GApplication
48  * @short_description: Core application class
49  * @include: gio/gio.h
50  *
51  * A #GApplication is the foundation of an application.  It wraps some
52  * low-level platform-specific services and is intended to act as the
53  * foundation for higher-level application classes such as
54  * #GtkApplication or #MxApplication.  In general, you should not use
55  * this class outside of a higher level framework.
56  *
57  * GApplication provides convenient life cycle management by maintaining
58  * a "use count" for the primary application instance. The use count can
59  * be changed using g_application_hold() and g_application_release(). If
60  * it drops to zero, the application exits. Higher-level classes such as
61  * #GtkApplication employ the use count to ensure that the application
62  * stays alive as long as it has any opened windows.
63  *
64  * Another feature that GApplication (optionally) provides is process
65  * uniqueness. Applications can make use of this functionality by
66  * providing a unique application ID. If given, only one application
67  * with this ID can be running at a time per session. The session
68  * concept is platform-dependent, but corresponds roughly to a graphical
69  * desktop login. When your application is launched again, its
70  * arguments are passed through platform communication to the already
71  * running program. The already running instance of the program is
72  * called the "primary instance"; for non-unique applications this is
73  * the always the current instance. On Linux, the D-Bus session bus
74  * is used for communication.
75  *
76  * The use of #GApplication differs from some other commonly-used
77  * uniqueness libraries (such as libunique) in important ways. The
78  * application is not expected to manually register itself and check
79  * if it is the primary instance. Instead, the main() function of a
80  * #GApplication should do very little more than instantiating the
81  * application instance, possibly connecting signal handlers, then
82  * calling g_application_run(). All checks for uniqueness are done
83  * internally. If the application is the primary instance then the
84  * startup signal is emitted and the mainloop runs. If the application
85  * is not the primary instance then a signal is sent to the primary
86  * instance and g_application_run() promptly returns. See the code
87  * examples below.
88  *
89  * If used, the expected form of an application identifier is very close
90  * to that of of a
91  * [DBus bus name](http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface).
92  * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
93  * For details on valid application identifiers, see g_application_id_is_valid().
94  *
95  * On Linux, the application identifier is claimed as a well-known bus name
96  * on the user's session bus.  This means that the uniqueness of your
97  * application is scoped to the current session.  It also means that your
98  * application may provide additional services (through registration of other
99  * object paths) at that bus name.  The registration of these object paths
100  * should be done with the shared GDBus session bus.  Note that due to the
101  * internal architecture of GDBus, method calls can be dispatched at any time
102  * (even if a main loop is not running).  For this reason, you must ensure that
103  * any object paths that you wish to register are registered before #GApplication
104  * attempts to acquire the bus name of your application (which happens in
105  * g_application_register()).  Unfortunately, this means that you cannot use
106  * g_application_get_is_remote() to decide if you want to register object paths.
107  *
108  * GApplication also implements the #GActionGroup and #GActionMap
109  * interfaces and lets you easily export actions by adding them with
110  * g_action_map_add_action(). When invoking an action by calling
111  * g_action_group_activate_action() on the application, it is always
112  * invoked in the primary instance. The actions are also exported on
113  * the session bus, and GIO provides the #GDBusActionGroup wrapper to
114  * conveniently access them remotely. GIO provides a #GDBusMenuModel wrapper
115  * for remote access to exported #GMenuModels.
116  *
117  * There is a number of different entry points into a GApplication:
118  *
119  * - via 'Activate' (i.e. just starting the application)
120  *
121  * - via 'Open' (i.e. opening some files)
122  *
123  * - by handling a command-line
124  *
125  * - via activating an action
126  *
127  * The #GApplication::startup signal lets you handle the application
128  * initialization for all of these in a single place.
129  *
130  * Regardless of which of these entry points is used to start the
131  * application, GApplication passes some "platform data from the
132  * launching instance to the primary instance, in the form of a
133  * #GVariant dictionary mapping strings to variants. To use platform
134  * data, override the @before_emit or @after_emit virtual functions
135  * in your #GApplication subclass. When dealing with
136  * #GApplicationCommandLine objects, the platform data is
137  * directly available via g_application_command_line_get_cwd(),
138  * g_application_command_line_get_environ() and
139  * g_application_command_line_get_platform_data().
140  *
141  * As the name indicates, the platform data may vary depending on the
142  * operating system, but it always includes the current directory (key
143  * "cwd"), and optionally the environment (ie the set of environment
144  * variables and their values) of the calling process (key "environ").
145  * The environment is only added to the platform data if the
146  * %G_APPLICATION_SEND_ENVIRONMENT flag is set. #GApplication subclasses
147  * can add their own platform data by overriding the @add_platform_data
148  * virtual function. For instance, #GtkApplication adds startup notification
149  * data in this way.
150  *
151  * To parse commandline arguments you may handle the
152  * #GApplication::command-line signal or override the local_command_line()
153  * vfunc, to parse them in either the primary instance or the local instance,
154  * respectively.
155  *
156  * For an example of opening files with a GApplication, see
157  * [gapplication-example-open.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-open.c).
158  *
159  * For an example of using actions with GApplication, see
160  * [gapplication-example-actions.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-actions.c).
161  *
162  * For an example of using extra D-Bus hooks with GApplication, see
163  * [gapplication-example-dbushooks.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-dbushooks.c).
164  */
165
166 /**
167  * GApplication:
168  *
169  * #GApplication is an opaque data structure and can only be accessed
170  * using the following functions.
171  * Since: 2.28
172  */
173
174 /**
175  * GApplicationClass:
176  * @startup: invoked on the primary instance immediately after registration
177  * @shutdown: invoked only on the registered primary instance immediately
178  *      after the main loop terminates
179  * @activate: invoked on the primary instance when an activation occurs
180  * @open: invoked on the primary instance when there are files to open
181  * @command_line: invoked on the primary instance when a command-line is
182  *   not handled locally
183  * @local_command_line: invoked (locally) when the process has been invoked
184  *     via commandline execution (as opposed to, say, D-Bus activation - which
185  *     is not currently supported by GApplication). The virtual function has
186  *     the chance to inspect (and possibly replace) the list of command line
187  *     arguments. See g_application_run() for more information.
188  * @before_emit: invoked on the primary instance before 'activate', 'open',
189  *     'command-line' or any action invocation, gets the 'platform data' from
190  *     the calling instance
191  * @after_emit: invoked on the primary instance after 'activate', 'open',
192  *     'command-line' or any action invocation, gets the 'platform data' from
193  *     the calling instance
194  * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
195  *     the primary instance when activating, opening or invoking actions
196  * @quit_mainloop: Used to be invoked on the primary instance when the use
197  *     count of the application drops to zero (and after any inactivity
198  *     timeout, if requested). Not used anymore since 2.32
199  * @run_mainloop: Used to be invoked on the primary instance from
200  *     g_application_run() if the use-count is non-zero. Since 2.32,
201  *     GApplication is iterating the main context directly and is not
202  *     using @run_mainloop anymore
203  * @dbus_register: invoked locally during registration, if the application is
204  *     using its D-Bus backend. You can use this to export extra objects on the
205  *     bus, that need to exist before the application tries to own the bus name.
206  *     The function is passed the #GDBusConnection to to session bus, and the
207  *     object path that #GApplication will use to export is D-Bus API.
208  *     If this function returns %TRUE, registration will proceed; otherwise
209  *     registration will abort. Since: 2.34
210  * @dbus_unregister: invoked locally during unregistration, if the application
211  *     is using its D-Bus backend. Use this to undo anything done by the
212  *     @dbus_register vfunc. Since: 2.34
213  * @handle_local_options: invoked locally after the parsing of the commandline
214  *  options has occurred.
215  *
216  * Virtual function table for #GApplication.
217  *
218  * Since: 2.28
219  */
220
221 struct _GApplicationPrivate
222 {
223   GApplicationFlags  flags;
224   gchar             *id;
225   gchar             *resource_path;
226
227   GActionGroup      *actions;
228   GMenuModel        *app_menu;
229   GMenuModel        *menubar;
230
231   guint              inactivity_timeout_id;
232   guint              inactivity_timeout;
233   guint              use_count;
234   guint              busy_count;
235
236   guint              is_registered : 1;
237   guint              is_remote : 1;
238   guint              did_startup : 1;
239   guint              did_shutdown : 1;
240   guint              must_quit_now : 1;
241
242   GRemoteActionGroup *remote_actions;
243   GApplicationImpl   *impl;
244
245   GNotificationBackend *notifications;
246
247   /* GOptionContext support */
248   GOptionGroup       *main_options;
249   GSList             *option_groups;
250   GHashTable         *packed_options;
251   gboolean            options_parsed;
252
253   /* Allocated option strings, from g_application_add_main_option() */
254   GSList             *option_strings;
255 };
256
257 enum
258 {
259   PROP_NONE,
260   PROP_APPLICATION_ID,
261   PROP_FLAGS,
262   PROP_RESOURCE_BASE_PATH,
263   PROP_IS_REGISTERED,
264   PROP_IS_REMOTE,
265   PROP_INACTIVITY_TIMEOUT,
266   PROP_ACTION_GROUP,
267   PROP_IS_BUSY
268 };
269
270 enum
271 {
272   SIGNAL_STARTUP,
273   SIGNAL_SHUTDOWN,
274   SIGNAL_ACTIVATE,
275   SIGNAL_OPEN,
276   SIGNAL_ACTION,
277   SIGNAL_COMMAND_LINE,
278   SIGNAL_HANDLE_LOCAL_OPTIONS,
279   NR_SIGNALS
280 };
281
282 static guint g_application_signals[NR_SIGNALS];
283
284 static void g_application_action_group_iface_init (GActionGroupInterface *);
285 static void g_application_action_map_iface_init (GActionMapInterface *);
286 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
287  G_ADD_PRIVATE (GApplication)
288  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
289  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
290
291 /* GApplicationExportedActions {{{1 */
292
293 /* We create a subclass of GSimpleActionGroup that implements
294  * GRemoteActionGroup and deals with the platform data using
295  * GApplication's before/after_emit vfuncs.  This is the action group we
296  * will be exporting.
297  *
298  * We could implement GRemoteActionGroup on GApplication directly, but
299  * this would be potentially extremely confusing to have exposed as part
300  * of the public API of GApplication.  We certainly don't want anyone in
301  * the same process to be calling these APIs...
302  */
303 typedef GSimpleActionGroupClass GApplicationExportedActionsClass;
304 typedef struct
305 {
306   GSimpleActionGroup parent_instance;
307   GApplication *application;
308 } GApplicationExportedActions;
309
310 static GType g_application_exported_actions_get_type   (void);
311 static void  g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface);
312 G_DEFINE_TYPE_WITH_CODE (GApplicationExportedActions, g_application_exported_actions, G_TYPE_SIMPLE_ACTION_GROUP,
313                          G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_application_exported_actions_iface_init))
314
315 static void
316 g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
317                                                      const gchar        *action_name,
318                                                      GVariant           *parameter,
319                                                      GVariant           *platform_data)
320 {
321   GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
322
323   G_APPLICATION_GET_CLASS (exported->application)
324     ->before_emit (exported->application, platform_data);
325
326   g_action_group_activate_action (G_ACTION_GROUP (exported), action_name, parameter);
327
328   G_APPLICATION_GET_CLASS (exported->application)
329     ->after_emit (exported->application, platform_data);
330 }
331
332 static void
333 g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
334                                                          const gchar        *action_name,
335                                                          GVariant           *value,
336                                                          GVariant           *platform_data)
337 {
338   GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
339
340   G_APPLICATION_GET_CLASS (exported->application)
341     ->before_emit (exported->application, platform_data);
342
343   g_action_group_change_action_state (G_ACTION_GROUP (exported), action_name, value);
344
345   G_APPLICATION_GET_CLASS (exported->application)
346     ->after_emit (exported->application, platform_data);
347 }
348
349 static void
350 g_application_exported_actions_init (GApplicationExportedActions *actions)
351 {
352 }
353
354 static void
355 g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface)
356 {
357   iface->activate_action_full = g_application_exported_actions_activate_action_full;
358   iface->change_action_state_full = g_application_exported_actions_change_action_state_full;
359 }
360
361 static void
362 g_application_exported_actions_class_init (GApplicationExportedActionsClass *class)
363 {
364 }
365
366 static GActionGroup *
367 g_application_exported_actions_new (GApplication *application)
368 {
369   GApplicationExportedActions *actions;
370
371   actions = g_object_new (g_application_exported_actions_get_type (), NULL);
372   actions->application = application;
373
374   return G_ACTION_GROUP (actions);
375 }
376
377 /* Command line option handling {{{1 */
378
379 static void
380 free_option_entry (gpointer data)
381 {
382   GOptionEntry *entry = data;
383
384   switch (entry->arg)
385     {
386     case G_OPTION_ARG_STRING:
387     case G_OPTION_ARG_FILENAME:
388       g_free (*(gchar **) entry->arg_data);
389       break;
390
391     case G_OPTION_ARG_STRING_ARRAY:
392     case G_OPTION_ARG_FILENAME_ARRAY:
393       g_strfreev (*(gchar ***) entry->arg_data);
394       break;
395
396     default:
397       /* most things require no free... */
398       break;
399     }
400
401   /* ...except for the space that we allocated for it ourselves */
402   g_free (entry->arg_data);
403
404   g_slice_free (GOptionEntry, entry);
405 }
406
407 static void
408 g_application_pack_option_entries (GApplication *application,
409                                    GVariantDict *dict)
410 {
411   GHashTableIter iter;
412   gpointer item;
413
414   g_hash_table_iter_init (&iter, application->priv->packed_options);
415   while (g_hash_table_iter_next (&iter, NULL, &item))
416     {
417       GOptionEntry *entry = item;
418       GVariant *value = NULL;
419
420       switch (entry->arg)
421         {
422         case G_OPTION_ARG_NONE:
423           if (*(gboolean *) entry->arg_data != 2)
424             value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
425           break;
426
427         case G_OPTION_ARG_STRING:
428           if (*(gchar **) entry->arg_data)
429             value = g_variant_new_string (*(gchar **) entry->arg_data);
430           break;
431
432         case G_OPTION_ARG_INT:
433           if (*(gint32 *) entry->arg_data)
434             value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
435           break;
436
437         case G_OPTION_ARG_FILENAME:
438           if (*(gchar **) entry->arg_data)
439             value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
440           break;
441
442         case G_OPTION_ARG_STRING_ARRAY:
443           if (*(gchar ***) entry->arg_data)
444             value = g_variant_new_strv (*(const gchar ***) entry->arg_data, -1);
445           break;
446
447         case G_OPTION_ARG_FILENAME_ARRAY:
448           if (*(gchar ***) entry->arg_data)
449             value = g_variant_new_bytestring_array (*(const gchar ***) entry->arg_data, -1);
450           break;
451
452         case G_OPTION_ARG_DOUBLE:
453           if (*(gdouble *) entry->arg_data)
454             value = g_variant_new_double (*(gdouble *) entry->arg_data);
455           break;
456
457         case G_OPTION_ARG_INT64:
458           if (*(gint64 *) entry->arg_data)
459             value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
460           break;
461
462         default:
463           g_assert_not_reached ();
464         }
465
466       if (value)
467         g_variant_dict_insert_value (dict, entry->long_name, value);
468     }
469 }
470
471 static GVariantDict *
472 g_application_parse_command_line (GApplication   *application,
473                                   gchar        ***arguments,
474                                   GError        **error)
475 {
476   gboolean become_service = FALSE;
477   GVariantDict *dict = NULL;
478   GOptionContext *context;
479
480   /* Due to the memory management of GOptionGroup we can only parse
481    * options once.  That's because once you add a group to the
482    * GOptionContext there is no way to get it back again.  This is fine:
483    * local_command_line() should never get invoked more than once
484    * anyway.  Add a sanity check just to be sure.
485    */
486   g_return_val_if_fail (!application->priv->options_parsed, NULL);
487
488   context = g_option_context_new (NULL);
489
490   /* If the application has not registered local options and it has
491    * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
492    * their primary instance commandline handler may want to deal with
493    * the arguments.  We must therefore ignore them.
494    *
495    * We must also ignore --help in this case since some applications
496    * will try to handle this from the remote side.  See #737869.
497    */
498   if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
499     {
500       g_option_context_set_ignore_unknown_options (context, TRUE);
501       g_option_context_set_help_enabled (context, FALSE);
502     }
503
504   /* Add the main option group, if it exists */
505   if (application->priv->main_options)
506     {
507       /* This consumes the main_options */
508       g_option_context_set_main_group (context, application->priv->main_options);
509       application->priv->main_options = NULL;
510     }
511
512   /* Add any other option groups if they exist.  Adding them to the
513    * context will consume them, so we free the list as we go...
514    */
515   while (application->priv->option_groups)
516     {
517       g_option_context_add_group (context, application->priv->option_groups->data);
518       application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
519                                                               application->priv->option_groups);
520     }
521
522   /* In the case that we are not explicitly marked as a service or a
523    * launcher then we want to add the "--gapplication-service" option to
524    * allow the process to be made into a service.
525    */
526   if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
527     {
528       GOptionGroup *option_group;
529       GOptionEntry entries[] = {
530         { "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
531           N_("Enter GApplication service mode (use from D-Bus service files)") },
532         { NULL }
533       };
534
535       option_group = g_option_group_new ("gapplication",
536                                          _("GApplication options"), _("Show GApplication options"),
537                                          NULL, NULL);
538       g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
539       g_option_group_add_entries (option_group, entries);
540
541       g_option_context_add_group (context, option_group);
542     }
543
544   /* Now we parse... */
545   if (!g_option_context_parse_strv (context, arguments, error))
546     goto out;
547
548   /* Check for --gapplication-service */
549   if (become_service)
550     application->priv->flags |= G_APPLICATION_IS_SERVICE;
551
552   dict = g_variant_dict_new (NULL);
553   if (application->priv->packed_options)
554     {
555       g_application_pack_option_entries (application, dict);
556       g_hash_table_unref (application->priv->packed_options);
557       application->priv->packed_options = NULL;
558     }
559
560 out:
561   /* Make sure we don't run again */
562   application->priv->options_parsed = TRUE;
563
564   g_option_context_free (context);
565
566   return dict;
567 }
568
569 static void
570 add_packed_option (GApplication *application,
571                    GOptionEntry *entry)
572 {
573   switch (entry->arg)
574     {
575     case G_OPTION_ARG_NONE:
576       entry->arg_data = g_new (gboolean, 1);
577       *(gboolean *) entry->arg_data = 2;
578       break;
579
580     case G_OPTION_ARG_INT:
581       entry->arg_data = g_new0 (gint, 1);
582       break;
583
584     case G_OPTION_ARG_STRING:
585     case G_OPTION_ARG_FILENAME:
586     case G_OPTION_ARG_STRING_ARRAY:
587     case G_OPTION_ARG_FILENAME_ARRAY:
588       entry->arg_data = g_new0 (gpointer, 1);
589       break;
590
591     case G_OPTION_ARG_INT64:
592       entry->arg_data = g_new0 (gint64, 1);
593       break;
594
595     case G_OPTION_ARG_DOUBLE:
596       entry->arg_data = g_new0 (gdouble, 1);
597       break;
598
599     default:
600       g_return_if_reached ();
601     }
602
603   if (!application->priv->packed_options)
604     application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
605
606   g_hash_table_insert (application->priv->packed_options,
607                        g_strdup (entry->long_name),
608                        g_slice_dup (GOptionEntry, entry));
609 }
610
611 /**
612  * g_application_add_main_option_entries:
613  * @application: a #GApplication
614  * @entries: (array zero-terminated=1) (element-type GOptionEntry) a
615  *           %NULL-terminated list of #GOptionEntrys
616  *
617  * Adds main option entries to be handled by @application.
618  *
619  * This function is comparable to g_option_context_add_main_entries().
620  *
621  * After the commandline arguments are parsed, the
622  * #GApplication::handle-local-options signal will be emitted.  At this
623  * point, the application can inspect the values pointed to by @arg_data
624  * in the given #GOptionEntrys.
625  *
626  * Unlike #GOptionContext, #GApplication supports giving a %NULL
627  * @arg_data for a non-callback #GOptionEntry.  This results in the
628  * argument in question being packed into a #GVariantDict which is also
629  * passed to #GApplication::handle-local-options, where it can be
630  * inspected and modified.  If %G_APPLICATION_HANDLES_COMMAND_LINE is
631  * set, then the resulting dictionary is sent to the primary instance,
632  * where g_application_command_line_get_options_dict() will return it.
633  * This "packing" is done according to the type of the argument --
634  * booleans for normal flags, strings for strings, bytestrings for
635  * filenames, etc.  The packing only occurs if the flag is given (ie: we
636  * do not pack a "false" #GVariant in the case that a flag is missing).
637  *
638  * In general, it is recommended that all commandline arguments are
639  * parsed locally.  The options dictionary should then be used to
640  * transmit the result of the parsing to the primary instance, where
641  * g_variant_dict_lookup() can be used.  For local options, it is
642  * possible to either use @arg_data in the usual way, or to consult (and
643  * potentially remove) the option from the options dictionary.
644  *
645  * This function is new in GLib 2.40.  Before then, the only real choice
646  * was to send all of the commandline arguments (options and all) to the
647  * primary instance for handling.  #GApplication ignored them completely
648  * on the local side.  Calling this function "opts in" to the new
649  * behaviour, and in particular, means that unrecognised options will be
650  * treated as errors.  Unrecognised options have never been ignored when
651  * %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
652  *
653  * If #GApplication::handle-local-options needs to see the list of
654  * filenames, then the use of %G_OPTION_REMAINING is recommended.  If
655  * @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
656  * the options dictionary.  If you do use %G_OPTION_REMAINING then you
657  * need to handle these arguments for yourself because once they are
658  * consumed, they will no longer be visible to the default handling
659  * (which treats them as filenames to be opened).
660  *
661  * Since: 2.40
662  */
663 void
664 g_application_add_main_option_entries (GApplication       *application,
665                                        const GOptionEntry *entries)
666 {
667   gint i;
668
669   g_return_if_fail (G_IS_APPLICATION (application));
670   g_return_if_fail (entries != NULL);
671
672   if (!application->priv->main_options)
673     {
674       application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
675       g_option_group_set_translation_domain (application->priv->main_options, NULL);
676     }
677
678   for (i = 0; entries[i].long_name; i++)
679     {
680       GOptionEntry my_entries[2] = { { NULL }, { NULL } };
681       my_entries[0] = entries[i];
682
683       if (!my_entries[0].arg_data)
684         add_packed_option (application, &my_entries[0]);
685
686       g_option_group_add_entries (application->priv->main_options, my_entries);
687     }
688 }
689
690 /**
691  * g_application_add_main_option:
692  * @application: the #GApplication
693  * @long_name: the long name of an option used to specify it in a commandline
694  * @short_name: the short name of an option
695  * @flags: flags from #GOptionFlags
696  * @arg: the type of the option, as a #GOptionArg
697  * @description: the description for the option in `--help` output
698  * @arg_description: (nullable): the placeholder to use for the extra argument
699  *    parsed by the option in `--help` output
700  *
701  * Add an option to be handled by @application.
702  *
703  * Calling this function is the equivalent of calling
704  * g_application_add_main_option_entries() with a single #GOptionEntry
705  * that has its arg_data member set to %NULL.
706  *
707  * The parsed arguments will be packed into a #GVariantDict which
708  * is passed to #GApplication::handle-local-options. If
709  * %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
710  * be sent to the primary instance. See
711  * g_application_add_main_option_entries() for more details.
712  *
713  * See #GOptionEntry for more documentation of the arguments.
714  *
715  * Since: 2.42
716  **/
717 void
718 g_application_add_main_option (GApplication *application,
719                                const char   *long_name,
720                                char          short_name,
721                                GOptionFlags  flags,
722                                GOptionArg    arg,
723                                const char   *description,
724                                const char   *arg_description)
725 {
726   gchar *dup_string;
727   GOptionEntry my_entry[2] = {
728     { NULL, short_name, flags, arg, NULL, NULL, NULL },
729     { NULL }
730   };
731
732   g_return_if_fail (G_IS_APPLICATION (application));
733   g_return_if_fail (long_name != NULL);
734   g_return_if_fail (description != NULL);
735
736   my_entry[0].long_name = dup_string = g_strdup (long_name);
737   application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
738
739   my_entry[0].description = dup_string = g_strdup (description);
740   application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
741
742   my_entry[0].arg_description = dup_string = g_strdup (arg_description);
743   application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
744
745   g_application_add_main_option_entries (application, my_entry);
746 }
747
748 /**
749  * g_application_add_option_group:
750  * @application: the #GApplication
751  * @group: a #GOptionGroup
752  *
753  * Adds a #GOptionGroup to the commandline handling of @application.
754  *
755  * This function is comparable to g_option_context_add_group().
756  *
757  * Unlike g_application_add_main_option_entries(), this function does
758  * not deal with %NULL @arg_data and never transmits options to the
759  * primary instance.
760  *
761  * The reason for that is because, by the time the options arrive at the
762  * primary instance, it is typically too late to do anything with them.
763  * Taking the GTK option group as an example: GTK will already have been
764  * initialised by the time the #GApplication::command-line handler runs.
765  * In the case that this is not the first-running instance of the
766  * application, the existing instance may already have been running for
767  * a very long time.
768  *
769  * This means that the options from #GOptionGroup are only really usable
770  * in the case that the instance of the application being run is the
771  * first instance.  Passing options like `--display=` or `--gdk-debug=`
772  * on future runs will have no effect on the existing primary instance.
773  *
774  * Calling this function will cause the options in the supplied option
775  * group to be parsed, but it does not cause you to be "opted in" to the
776  * new functionality whereby unrecognised options are rejected even if
777  * %G_APPLICATION_HANDLES_COMMAND_LINE was given.
778  *
779  * Since: 2.40
780  **/
781 void
782 g_application_add_option_group (GApplication *application,
783                                 GOptionGroup *group)
784 {
785   g_return_if_fail (G_IS_APPLICATION (application));
786   g_return_if_fail (group != NULL);
787
788   application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
789 }
790
791 /* vfunc defaults {{{1 */
792 static void
793 g_application_real_before_emit (GApplication *application,
794                                 GVariant     *platform_data)
795 {
796 }
797
798 static void
799 g_application_real_after_emit (GApplication *application,
800                                GVariant     *platform_data)
801 {
802 }
803
804 static void
805 g_application_real_startup (GApplication *application)
806 {
807   application->priv->did_startup = TRUE;
808 }
809
810 static void
811 g_application_real_shutdown (GApplication *application)
812 {
813   application->priv->did_shutdown = TRUE;
814 }
815
816 static void
817 g_application_real_activate (GApplication *application)
818 {
819   if (!g_signal_has_handler_pending (application,
820                                      g_application_signals[SIGNAL_ACTIVATE],
821                                      0, TRUE) &&
822       G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
823     {
824       static gboolean warned;
825
826       if (warned)
827         return;
828
829       g_warning ("Your application does not implement "
830                  "g_application_activate() and has no handlers connected "
831                  "to the 'activate' signal.  It should do one of these.");
832       warned = TRUE;
833     }
834 }
835
836 static void
837 g_application_real_open (GApplication  *application,
838                          GFile        **files,
839                          gint           n_files,
840                          const gchar   *hint)
841 {
842   if (!g_signal_has_handler_pending (application,
843                                      g_application_signals[SIGNAL_OPEN],
844                                      0, TRUE) &&
845       G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
846     {
847       static gboolean warned;
848
849       if (warned)
850         return;
851
852       g_warning ("Your application claims to support opening files "
853                  "but does not implement g_application_open() and has no "
854                  "handlers connected to the 'open' signal.");
855       warned = TRUE;
856     }
857 }
858
859 static int
860 g_application_real_command_line (GApplication            *application,
861                                  GApplicationCommandLine *cmdline)
862 {
863   if (!g_signal_has_handler_pending (application,
864                                      g_application_signals[SIGNAL_COMMAND_LINE],
865                                      0, TRUE) &&
866       G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
867     {
868       static gboolean warned;
869
870       if (warned)
871         return 1;
872
873       g_warning ("Your application claims to support custom command line "
874                  "handling but does not implement g_application_command_line() "
875                  "and has no handlers connected to the 'command-line' signal.");
876
877       warned = TRUE;
878     }
879
880     return 1;
881 }
882
883 static gint
884 g_application_real_handle_local_options (GApplication *application,
885                                          GVariantDict *options)
886 {
887   return -1;
888 }
889
890 static GVariant *
891 get_platform_data (GApplication *application,
892                    GVariant     *options)
893 {
894   GVariantBuilder *builder;
895   GVariant *result;
896
897   builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
898
899   {
900     gchar *cwd = g_get_current_dir ();
901     g_variant_builder_add (builder, "{sv}", "cwd",
902                            g_variant_new_bytestring (cwd));
903     g_free (cwd);
904   }
905
906   if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
907     {
908       GVariant *array;
909       gchar **envp;
910
911       envp = g_get_environ ();
912       array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
913       g_strfreev (envp);
914
915       g_variant_builder_add (builder, "{sv}", "environ", array);
916     }
917
918   if (options)
919     g_variant_builder_add (builder, "{sv}", "options", options);
920
921   G_APPLICATION_GET_CLASS (application)->
922     add_platform_data (application, builder);
923
924   result = g_variant_builder_end (builder);
925   g_variant_builder_unref (builder);
926
927   return result;
928 }
929
930 static void
931 g_application_call_command_line (GApplication        *application,
932                                  const gchar * const *arguments,
933                                  GVariant            *options,
934                                  gint                *exit_status)
935 {
936   if (application->priv->is_remote)
937     {
938       GVariant *platform_data;
939
940       platform_data = get_platform_data (application, options);
941       *exit_status = g_application_impl_command_line (application->priv->impl, arguments, platform_data);
942     }
943   else
944     {
945       GApplicationCommandLine *cmdline;
946       GVariant *v;
947
948       v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
949       cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
950                               "arguments", v,
951                               "options", options,
952                               NULL);
953       g_signal_emit (application, g_application_signals[SIGNAL_COMMAND_LINE], 0, cmdline, exit_status);
954       g_object_unref (cmdline);
955     }
956 }
957
958 static gboolean
959 g_application_real_local_command_line (GApplication   *application,
960                                        gchar        ***arguments,
961                                        int            *exit_status)
962 {
963   GError *error = NULL;
964   GVariantDict *options;
965   gint n_args;
966
967   options = g_application_parse_command_line (application, arguments, &error);
968   if (!options)
969     {
970       g_printerr ("%s\n", error->message);
971       *exit_status = 1;
972       return TRUE;
973     }
974
975   g_signal_emit (application, g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS], 0, options, exit_status);
976
977   if (*exit_status >= 0)
978     {
979       g_variant_dict_unref (options);
980       return TRUE;
981     }
982
983   if (!g_application_register (application, NULL, &error))
984     {
985       g_printerr ("Failed to register: %s\n", error->message);
986       g_variant_dict_unref (options);
987       g_error_free (error);
988       *exit_status = 1;
989       return TRUE;
990     }
991
992   n_args = g_strv_length (*arguments);
993
994   if (application->priv->flags & G_APPLICATION_IS_SERVICE)
995     {
996       if ((*exit_status = n_args > 1))
997         {
998           g_printerr ("GApplication service mode takes no arguments.\n");
999           application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
1000           *exit_status = 1;
1001         }
1002       else
1003         *exit_status = 0;
1004     }
1005   else if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
1006     {
1007       g_application_call_command_line (application,
1008                                        (const gchar **) *arguments,
1009                                        g_variant_dict_end (options),
1010                                        exit_status);
1011     }
1012   else
1013     {
1014       if (n_args <= 1)
1015         {
1016           g_application_activate (application);
1017           *exit_status = 0;
1018         }
1019
1020       else
1021         {
1022           if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
1023             {
1024               g_critical ("This application can not open files.");
1025               *exit_status = 1;
1026             }
1027           else
1028             {
1029               GFile **files;
1030               gint n_files;
1031               gint i;
1032
1033               n_files = n_args - 1;
1034               files = g_new (GFile *, n_files);
1035
1036               for (i = 0; i < n_files; i++)
1037                 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
1038
1039               g_application_open (application, files, n_files, "");
1040
1041               for (i = 0; i < n_files; i++)
1042                 g_object_unref (files[i]);
1043               g_free (files);
1044
1045               *exit_status = 0;
1046             }
1047         }
1048     }
1049
1050   g_variant_dict_unref (options);
1051
1052   return TRUE;
1053 }
1054
1055 static void
1056 g_application_real_add_platform_data (GApplication    *application,
1057                                       GVariantBuilder *builder)
1058 {
1059 }
1060
1061 static gboolean
1062 g_application_real_dbus_register (GApplication    *application,
1063                                   GDBusConnection *connection,
1064                                   const gchar     *object_path,
1065                                   GError         **error)
1066 {
1067   return TRUE;
1068 }
1069
1070 static void
1071 g_application_real_dbus_unregister (GApplication    *application,
1072                                     GDBusConnection *connection,
1073                                     const gchar     *object_path)
1074 {
1075 }
1076
1077 /* GObject implementation stuff {{{1 */
1078 static void
1079 g_application_set_property (GObject      *object,
1080                             guint         prop_id,
1081                             const GValue *value,
1082                             GParamSpec   *pspec)
1083 {
1084   GApplication *application = G_APPLICATION (object);
1085
1086   switch (prop_id)
1087     {
1088     case PROP_APPLICATION_ID:
1089       g_application_set_application_id (application,
1090                                         g_value_get_string (value));
1091       break;
1092
1093     case PROP_FLAGS:
1094       g_application_set_flags (application, g_value_get_flags (value));
1095       break;
1096
1097     case PROP_RESOURCE_BASE_PATH:
1098       g_application_set_resource_base_path (application, g_value_get_string (value));
1099       break;
1100
1101     case PROP_INACTIVITY_TIMEOUT:
1102       g_application_set_inactivity_timeout (application,
1103                                             g_value_get_uint (value));
1104       break;
1105
1106     case PROP_ACTION_GROUP:
1107       g_clear_object (&application->priv->actions);
1108       application->priv->actions = g_value_dup_object (value);
1109       break;
1110
1111     default:
1112       g_assert_not_reached ();
1113     }
1114 }
1115
1116 /**
1117  * g_application_set_action_group:
1118  * @application: a #GApplication
1119  * @action_group: (allow-none): a #GActionGroup, or %NULL
1120  *
1121  * This used to be how actions were associated with a #GApplication.
1122  * Now there is #GActionMap for that.
1123  *
1124  * Since: 2.28
1125  *
1126  * Deprecated:2.32:Use the #GActionMap interface instead.  Never ever
1127  * mix use of this API with use of #GActionMap on the same @application
1128  * or things will go very badly wrong.  This function is known to
1129  * introduce buggy behaviour (ie: signals not emitted on changes to the
1130  * action group), so you should really use #GActionMap instead.
1131  **/
1132 void
1133 g_application_set_action_group (GApplication *application,
1134                                 GActionGroup *action_group)
1135 {
1136   g_return_if_fail (G_IS_APPLICATION (application));
1137   g_return_if_fail (!application->priv->is_registered);
1138
1139   if (application->priv->actions != NULL)
1140     g_object_unref (application->priv->actions);
1141
1142   application->priv->actions = action_group;
1143
1144   if (application->priv->actions != NULL)
1145     g_object_ref (application->priv->actions);
1146 }
1147
1148 static void
1149 g_application_get_property (GObject    *object,
1150                             guint       prop_id,
1151                             GValue     *value,
1152                             GParamSpec *pspec)
1153 {
1154   GApplication *application = G_APPLICATION (object);
1155
1156   switch (prop_id)
1157     {
1158     case PROP_APPLICATION_ID:
1159       g_value_set_string (value,
1160                           g_application_get_application_id (application));
1161       break;
1162
1163     case PROP_FLAGS:
1164       g_value_set_flags (value,
1165                          g_application_get_flags (application));
1166       break;
1167
1168     case PROP_RESOURCE_BASE_PATH:
1169       g_value_set_string (value, g_application_get_resource_base_path (application));
1170       break;
1171
1172     case PROP_IS_REGISTERED:
1173       g_value_set_boolean (value,
1174                            g_application_get_is_registered (application));
1175       break;
1176
1177     case PROP_IS_REMOTE:
1178       g_value_set_boolean (value,
1179                            g_application_get_is_remote (application));
1180       break;
1181
1182     case PROP_INACTIVITY_TIMEOUT:
1183       g_value_set_uint (value,
1184                         g_application_get_inactivity_timeout (application));
1185       break;
1186
1187     case PROP_IS_BUSY:
1188       g_value_set_boolean (value, g_application_get_is_busy (application));
1189       break;
1190
1191     default:
1192       g_assert_not_reached ();
1193     }
1194 }
1195
1196 static void
1197 g_application_constructed (GObject *object)
1198 {
1199   GApplication *application = G_APPLICATION (object);
1200
1201   if (g_application_get_default () == NULL)
1202     g_application_set_default (application);
1203
1204   /* People should not set properties from _init... */
1205   g_assert (application->priv->resource_path == NULL);
1206
1207   if (application->priv->id != NULL)
1208     {
1209       gint i;
1210
1211       application->priv->resource_path = g_strconcat ("/", application->priv->id, NULL);
1212
1213       for (i = 1; application->priv->resource_path[i]; i++)
1214         if (application->priv->resource_path[i] == '.')
1215           application->priv->resource_path[i] = '/';
1216     }
1217 }
1218
1219 static void
1220 g_application_finalize (GObject *object)
1221 {
1222   GApplication *application = G_APPLICATION (object);
1223
1224   g_slist_free_full (application->priv->option_groups, (GDestroyNotify) g_option_group_unref);
1225   if (application->priv->main_options)
1226     g_option_group_unref (application->priv->main_options);
1227   if (application->priv->packed_options)
1228     {
1229       g_slist_free_full (application->priv->option_strings, g_free);
1230       g_hash_table_unref (application->priv->packed_options);
1231     }
1232   if (application->priv->impl)
1233     g_application_impl_destroy (application->priv->impl);
1234   g_free (application->priv->id);
1235
1236   if (g_application_get_default () == application)
1237     g_application_set_default (NULL);
1238
1239   if (application->priv->actions)
1240     g_object_unref (application->priv->actions);
1241
1242   if (application->priv->notifications)
1243     g_object_unref (application->priv->notifications);
1244
1245   g_free (application->priv->resource_path);
1246
1247   G_OBJECT_CLASS (g_application_parent_class)
1248     ->finalize (object);
1249 }
1250
1251 static void
1252 g_application_init (GApplication *application)
1253 {
1254   application->priv = g_application_get_instance_private (application);
1255
1256   application->priv->actions = g_application_exported_actions_new (application);
1257
1258   /* application->priv->actions is the one and only ref on the group, so when
1259    * we dispose, the action group will die, disconnecting all signals.
1260    */
1261   g_signal_connect_swapped (application->priv->actions, "action-added",
1262                             G_CALLBACK (g_action_group_action_added), application);
1263   g_signal_connect_swapped (application->priv->actions, "action-enabled-changed",
1264                             G_CALLBACK (g_action_group_action_enabled_changed), application);
1265   g_signal_connect_swapped (application->priv->actions, "action-state-changed",
1266                             G_CALLBACK (g_action_group_action_state_changed), application);
1267   g_signal_connect_swapped (application->priv->actions, "action-removed",
1268                             G_CALLBACK (g_action_group_action_removed), application);
1269 }
1270
1271 static gboolean
1272 g_application_handle_local_options_accumulator (GSignalInvocationHint *ihint,
1273                                                 GValue                *return_accu,
1274                                                 const GValue          *handler_return,
1275                                                 gpointer               dummy)
1276 {
1277   gint value;
1278
1279   value = g_value_get_int (handler_return);
1280   g_value_set_int (return_accu, value);
1281
1282   return value < 0;
1283 }
1284
1285 static void
1286 g_application_class_init (GApplicationClass *class)
1287 {
1288   GObjectClass *object_class = G_OBJECT_CLASS (class);
1289
1290   object_class->constructed = g_application_constructed;
1291   object_class->finalize = g_application_finalize;
1292   object_class->get_property = g_application_get_property;
1293   object_class->set_property = g_application_set_property;
1294
1295   class->before_emit = g_application_real_before_emit;
1296   class->after_emit = g_application_real_after_emit;
1297   class->startup = g_application_real_startup;
1298   class->shutdown = g_application_real_shutdown;
1299   class->activate = g_application_real_activate;
1300   class->open = g_application_real_open;
1301   class->command_line = g_application_real_command_line;
1302   class->local_command_line = g_application_real_local_command_line;
1303   class->handle_local_options = g_application_real_handle_local_options;
1304   class->add_platform_data = g_application_real_add_platform_data;
1305   class->dbus_register = g_application_real_dbus_register;
1306   class->dbus_unregister = g_application_real_dbus_unregister;
1307
1308   g_object_class_install_property (object_class, PROP_APPLICATION_ID,
1309     g_param_spec_string ("application-id",
1310                          P_("Application identifier"),
1311                          P_("The unique identifier for the application"),
1312                          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
1313                          G_PARAM_STATIC_STRINGS));
1314
1315   g_object_class_install_property (object_class, PROP_FLAGS,
1316     g_param_spec_flags ("flags",
1317                         P_("Application flags"),
1318                         P_("Flags specifying the behaviour of the application"),
1319                         G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
1320                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1321
1322   g_object_class_install_property (object_class, PROP_RESOURCE_BASE_PATH,
1323     g_param_spec_string ("resource-base-path",
1324                          P_("Resource base path"),
1325                          P_("The base resource path for the application"),
1326                          NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1327
1328   g_object_class_install_property (object_class, PROP_IS_REGISTERED,
1329     g_param_spec_boolean ("is-registered",
1330                           P_("Is registered"),
1331                           P_("If g_application_register() has been called"),
1332                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1333
1334   g_object_class_install_property (object_class, PROP_IS_REMOTE,
1335     g_param_spec_boolean ("is-remote",
1336                           P_("Is remote"),
1337                           P_("If this application instance is remote"),
1338                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1339
1340   g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
1341     g_param_spec_uint ("inactivity-timeout",
1342                        P_("Inactivity timeout"),
1343                        P_("Time (ms) to stay alive after becoming idle"),
1344                        0, G_MAXUINT, 0,
1345                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1346
1347   g_object_class_install_property (object_class, PROP_ACTION_GROUP,
1348     g_param_spec_object ("action-group",
1349                          P_("Action group"),
1350                          P_("The group of actions that the application exports"),
1351                          G_TYPE_ACTION_GROUP,
1352                          G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1353
1354   /**
1355    * GApplication:is-busy:
1356    *
1357    * Whether the application is currently marked as busy through
1358    * g_application_mark_busy() or g_application_bind_busy_property().
1359    *
1360    * Since: 2.44
1361    */
1362   g_object_class_install_property (object_class, PROP_IS_BUSY,
1363     g_param_spec_boolean ("is-busy",
1364                           P_("Is busy"),
1365                           P_("If this application is currently marked busy"),
1366                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1367
1368   /**
1369    * GApplication::startup:
1370    * @application: the application
1371    *
1372    * The ::startup signal is emitted on the primary instance immediately
1373    * after registration. See g_application_register().
1374    */
1375   g_application_signals[SIGNAL_STARTUP] =
1376     g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
1377                   G_STRUCT_OFFSET (GApplicationClass, startup),
1378                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1379
1380   /**
1381    * GApplication::shutdown:
1382    * @application: the application
1383    *
1384    * The ::shutdown signal is emitted only on the registered primary instance
1385    * immediately after the main loop terminates.
1386    */
1387   g_application_signals[SIGNAL_SHUTDOWN] =
1388     g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1389                   G_STRUCT_OFFSET (GApplicationClass, shutdown),
1390                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1391
1392   /**
1393    * GApplication::activate:
1394    * @application: the application
1395    *
1396    * The ::activate signal is emitted on the primary instance when an
1397    * activation occurs. See g_application_activate().
1398    */
1399   g_application_signals[SIGNAL_ACTIVATE] =
1400     g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1401                   G_STRUCT_OFFSET (GApplicationClass, activate),
1402                   NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1403
1404
1405   /**
1406    * GApplication::open:
1407    * @application: the application
1408    * @files: (array length=n_files) (element-type GFile): an array of #GFiles
1409    * @n_files: the length of @files
1410    * @hint: a hint provided by the calling instance
1411    *
1412    * The ::open signal is emitted on the primary instance when there are
1413    * files to open. See g_application_open() for more information.
1414    */
1415   g_application_signals[SIGNAL_OPEN] =
1416     g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1417                   G_STRUCT_OFFSET (GApplicationClass, open),
1418                   NULL, NULL, NULL,
1419                   G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
1420
1421   /**
1422    * GApplication::command-line:
1423    * @application: the application
1424    * @command_line: a #GApplicationCommandLine representing the
1425    *     passed commandline
1426    *
1427    * The ::command-line signal is emitted on the primary instance when
1428    * a commandline is not handled locally. See g_application_run() and
1429    * the #GApplicationCommandLine documentation for more information.
1430    *
1431    * Returns: An integer that is set as the exit status for the calling
1432    *   process. See g_application_command_line_set_exit_status().
1433    */
1434   g_application_signals[SIGNAL_COMMAND_LINE] =
1435     g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1436                   G_STRUCT_OFFSET (GApplicationClass, command_line),
1437                   g_signal_accumulator_first_wins, NULL,
1438                   NULL,
1439                   G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
1440
1441   /**
1442    * GApplication::handle-local-options:
1443    * @application: the application
1444    * @options: the options dictionary
1445    *
1446    * The ::handle-local-options signal is emitted on the local instance
1447    * after the parsing of the commandline options has occurred.
1448    *
1449    * You can add options to be recognised during commandline option
1450    * parsing using g_application_add_main_option_entries() and
1451    * g_application_add_option_group().
1452    *
1453    * Signal handlers can inspect @options (along with values pointed to
1454    * from the @arg_data of an installed #GOptionEntrys) in order to
1455    * decide to perform certain actions, including direct local handling
1456    * (which may be useful for options like --version).
1457    *
1458    * In the event that the application is marked
1459    * %G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will
1460    * send the @options dictionary to the primary instance where it can be
1461    * read with g_application_command_line_get_options_dict().  The signal
1462    * handler can modify the dictionary before returning, and the
1463    * modified dictionary will be sent.
1464    *
1465    * In the event that %G_APPLICATION_HANDLES_COMMAND_LINE is not set,
1466    * "normal processing" will treat the remaining uncollected command
1467    * line arguments as filenames or URIs.  If there are no arguments,
1468    * the application is activated by g_application_activate().  One or
1469    * more arguments results in a call to g_application_open().
1470    *
1471    * If you want to handle the local commandline arguments for yourself
1472    * by converting them to calls to g_application_open() or
1473    * g_action_group_activate_action() then you must be sure to register
1474    * the application first.  You should probably not call
1475    * g_application_activate() for yourself, however: just return -1 and
1476    * allow the default handler to do it for you.  This will ensure that
1477    * the `--gapplication-service` switch works properly (i.e. no activation
1478    * in that case).
1479    *
1480    * Note that this signal is emitted from the default implementation of
1481    * local_command_line().  If you override that function and don't
1482    * chain up then this signal will never be emitted.
1483    *
1484    * You can override local_command_line() if you need more powerful
1485    * capabilities than what is provided here, but this should not
1486    * normally be required.
1487    *
1488    * Returns: an exit code. If you have handled your options and want
1489    * to exit the process, return a non-negative option, 0 for success,
1490    * and a positive value for failure. To continue, return -1 to let
1491    * the default option processing continue.
1492    *
1493    * Since: 2.40
1494    **/
1495   g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS] =
1496     g_signal_new ("handle-local-options", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1497                   G_STRUCT_OFFSET (GApplicationClass, handle_local_options),
1498                   g_application_handle_local_options_accumulator, NULL, NULL,
1499                   G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
1500
1501 }
1502
1503 /* Application ID validity {{{1 */
1504
1505 /**
1506  * g_application_id_is_valid:
1507  * @application_id: a potential application identifier
1508  *
1509  * Checks if @application_id is a valid application identifier.
1510  *
1511  * A valid ID is required for calls to g_application_new() and
1512  * g_application_set_application_id().
1513  *
1514  * For convenience, the restrictions on application identifiers are
1515  * reproduced here:
1516  *
1517  * - Application identifiers must contain only the ASCII characters
1518  *   "[A-Z][a-z][0-9]_-." and must not begin with a digit.
1519  *
1520  * - Application identifiers must contain at least one '.' (period)
1521  *   character (and thus at least three elements).
1522  *
1523  * - Application identifiers must not begin or end with a '.' (period)
1524  *   character.
1525  *
1526  * - Application identifiers must not contain consecutive '.' (period)
1527  *   characters.
1528  *
1529  * - Application identifiers must not exceed 255 characters.
1530  *
1531  * Returns: %TRUE if @application_id is valid
1532  */
1533 gboolean
1534 g_application_id_is_valid (const gchar *application_id)
1535 {
1536   gsize len;
1537   gboolean allow_dot;
1538   gboolean has_dot;
1539
1540   len = strlen (application_id);
1541
1542   if (len > 255)
1543     return FALSE;
1544
1545   if (!g_ascii_isalpha (application_id[0]))
1546     return FALSE;
1547
1548   if (application_id[len-1] == '.')
1549     return FALSE;
1550
1551   application_id++;
1552   allow_dot = TRUE;
1553   has_dot = FALSE;
1554   for (; *application_id; application_id++)
1555     {
1556       if (g_ascii_isalnum (*application_id) ||
1557           (*application_id == '-') ||
1558           (*application_id == '_'))
1559         {
1560           allow_dot = TRUE;
1561         }
1562       else if (allow_dot && *application_id == '.')
1563         {
1564           has_dot = TRUE;
1565           allow_dot = FALSE;
1566         }
1567       else
1568         return FALSE;
1569     }
1570
1571   if (!has_dot)
1572     return FALSE;
1573
1574   return TRUE;
1575 }
1576
1577 /* Public Constructor {{{1 */
1578 /**
1579  * g_application_new:
1580  * @application_id: (allow-none): the application id
1581  * @flags: the application flags
1582  *
1583  * Creates a new #GApplication instance.
1584  *
1585  * If non-%NULL, the application id must be valid.  See
1586  * g_application_id_is_valid().
1587  *
1588  * If no application ID is given then some features of #GApplication
1589  * (most notably application uniqueness) will be disabled.
1590  *
1591  * Returns: a new #GApplication instance
1592  **/
1593 GApplication *
1594 g_application_new (const gchar       *application_id,
1595                    GApplicationFlags  flags)
1596 {
1597   g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
1598
1599   return g_object_new (G_TYPE_APPLICATION,
1600                        "application-id", application_id,
1601                        "flags", flags,
1602                        NULL);
1603 }
1604
1605 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
1606 /**
1607  * g_application_get_application_id:
1608  * @application: a #GApplication
1609  *
1610  * Gets the unique identifier for @application.
1611  *
1612  * Returns: the identifier for @application, owned by @application
1613  *
1614  * Since: 2.28
1615  **/
1616 const gchar *
1617 g_application_get_application_id (GApplication *application)
1618 {
1619   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1620
1621   return application->priv->id;
1622 }
1623
1624 /**
1625  * g_application_set_application_id:
1626  * @application: a #GApplication
1627  * @application_id: (allow-none): the identifier for @application
1628  *
1629  * Sets the unique identifier for @application.
1630  *
1631  * The application id can only be modified if @application has not yet
1632  * been registered.
1633  *
1634  * If non-%NULL, the application id must be valid.  See
1635  * g_application_id_is_valid().
1636  *
1637  * Since: 2.28
1638  **/
1639 void
1640 g_application_set_application_id (GApplication *application,
1641                                   const gchar  *application_id)
1642 {
1643   g_return_if_fail (G_IS_APPLICATION (application));
1644
1645   if (g_strcmp0 (application->priv->id, application_id) != 0)
1646     {
1647       g_return_if_fail (application_id == NULL || g_application_id_is_valid (application_id));
1648       g_return_if_fail (!application->priv->is_registered);
1649
1650       g_free (application->priv->id);
1651       application->priv->id = g_strdup (application_id);
1652
1653       g_object_notify (G_OBJECT (application), "application-id");
1654     }
1655 }
1656
1657 /**
1658  * g_application_get_flags:
1659  * @application: a #GApplication
1660  *
1661  * Gets the flags for @application.
1662  *
1663  * See #GApplicationFlags.
1664  *
1665  * Returns: the flags for @application
1666  *
1667  * Since: 2.28
1668  **/
1669 GApplicationFlags
1670 g_application_get_flags (GApplication *application)
1671 {
1672   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1673
1674   return application->priv->flags;
1675 }
1676
1677 /**
1678  * g_application_set_flags:
1679  * @application: a #GApplication
1680  * @flags: the flags for @application
1681  *
1682  * Sets the flags for @application.
1683  *
1684  * The flags can only be modified if @application has not yet been
1685  * registered.
1686  *
1687  * See #GApplicationFlags.
1688  *
1689  * Since: 2.28
1690  **/
1691 void
1692 g_application_set_flags (GApplication      *application,
1693                          GApplicationFlags  flags)
1694 {
1695   g_return_if_fail (G_IS_APPLICATION (application));
1696
1697   if (application->priv->flags != flags)
1698     {
1699       g_return_if_fail (!application->priv->is_registered);
1700
1701       application->priv->flags = flags;
1702
1703       g_object_notify (G_OBJECT (application), "flags");
1704     }
1705 }
1706
1707 /**
1708  * g_application_get_resource_base_path:
1709  * @application: a #GApplication
1710  *
1711  * Gets the resource base path of @application.
1712  *
1713  * See g_application_set_resource_base_path() for more information.
1714  *
1715  * Returns: (nullable): the base resource path, if one is set
1716  *
1717  * Since: 2.42
1718  */
1719 const gchar *
1720 g_application_get_resource_base_path (GApplication *application)
1721 {
1722   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1723
1724   return application->priv->resource_path;
1725 }
1726
1727 /**
1728  * g_application_set_resource_base_path:
1729  * @application: a #GApplication
1730  * @resource_path: (nullable): the resource path to use
1731  *
1732  * Sets (or unsets) the base resource path of @application.
1733  *
1734  * The path is used to automatically load various [application
1735  * resources][gresource] such as menu layouts and action descriptions.
1736  * The various types of resources will be found at fixed names relative
1737  * to the given base path.
1738  *
1739  * By default, the resource base path is determined from the application
1740  * ID by prefixing '/' and replacing each '.' with '/'.  This is done at
1741  * the time that the #GApplication object is constructed.  Changes to
1742  * the application ID after that point will not have an impact on the
1743  * resource base path.
1744  *
1745  * As an example, if the application has an ID of "org.example.app" then
1746  * the default resource base path will be "/org/example/app".  If this
1747  * is a #GtkApplication (and you have not manually changed the path)
1748  * then Gtk will then search for the menus of the application at
1749  * "/org/example/app/gtk/menus.ui".
1750  *
1751  * See #GResource for more information about adding resources to your
1752  * application.
1753  *
1754  * You can disable automatic resource loading functionality by setting
1755  * the path to %NULL.
1756  *
1757  * Changing the resource base path once the application is running is
1758  * not recommended.  The point at which the resource path is consulted
1759  * for forming paths for various purposes is unspecified.
1760  *
1761  * Since: 2.42
1762  */
1763 void
1764 g_application_set_resource_base_path (GApplication *application,
1765                                       const gchar  *resource_path)
1766 {
1767   g_return_if_fail (G_IS_APPLICATION (application));
1768   g_return_if_fail (resource_path == NULL || g_str_has_prefix (resource_path, "/"));
1769
1770   if (g_strcmp0 (application->priv->resource_path, resource_path) != 0)
1771     {
1772       g_free (application->priv->resource_path);
1773
1774       application->priv->resource_path = g_strdup (resource_path);
1775
1776       g_object_notify (G_OBJECT (application), "resource-base-path");
1777     }
1778 }
1779
1780 /**
1781  * g_application_get_inactivity_timeout:
1782  * @application: a #GApplication
1783  *
1784  * Gets the current inactivity timeout for the application.
1785  *
1786  * This is the amount of time (in milliseconds) after the last call to
1787  * g_application_release() before the application stops running.
1788  *
1789  * Returns: the timeout, in milliseconds
1790  *
1791  * Since: 2.28
1792  **/
1793 guint
1794 g_application_get_inactivity_timeout (GApplication *application)
1795 {
1796   g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1797
1798   return application->priv->inactivity_timeout;
1799 }
1800
1801 /**
1802  * g_application_set_inactivity_timeout:
1803  * @application: a #GApplication
1804  * @inactivity_timeout: the timeout, in milliseconds
1805  *
1806  * Sets the current inactivity timeout for the application.
1807  *
1808  * This is the amount of time (in milliseconds) after the last call to
1809  * g_application_release() before the application stops running.
1810  *
1811  * This call has no side effects of its own.  The value set here is only
1812  * used for next time g_application_release() drops the use count to
1813  * zero.  Any timeouts currently in progress are not impacted.
1814  *
1815  * Since: 2.28
1816  **/
1817 void
1818 g_application_set_inactivity_timeout (GApplication *application,
1819                                       guint         inactivity_timeout)
1820 {
1821   g_return_if_fail (G_IS_APPLICATION (application));
1822
1823   if (application->priv->inactivity_timeout != inactivity_timeout)
1824     {
1825       application->priv->inactivity_timeout = inactivity_timeout;
1826
1827       g_object_notify (G_OBJECT (application), "inactivity-timeout");
1828     }
1829 }
1830 /* Read-only property getters (is registered, is remote, dbus stuff) {{{1 */
1831 /**
1832  * g_application_get_is_registered:
1833  * @application: a #GApplication
1834  *
1835  * Checks if @application is registered.
1836  *
1837  * An application is registered if g_application_register() has been
1838  * successfully called.
1839  *
1840  * Returns: %TRUE if @application is registered
1841  *
1842  * Since: 2.28
1843  **/
1844 gboolean
1845 g_application_get_is_registered (GApplication *application)
1846 {
1847   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1848
1849   return application->priv->is_registered;
1850 }
1851
1852 /**
1853  * g_application_get_is_remote:
1854  * @application: a #GApplication
1855  *
1856  * Checks if @application is remote.
1857  *
1858  * If @application is remote then it means that another instance of
1859  * application already exists (the 'primary' instance).  Calls to
1860  * perform actions on @application will result in the actions being
1861  * performed by the primary instance.
1862  *
1863  * The value of this property cannot be accessed before
1864  * g_application_register() has been called.  See
1865  * g_application_get_is_registered().
1866  *
1867  * Returns: %TRUE if @application is remote
1868  *
1869  * Since: 2.28
1870  **/
1871 gboolean
1872 g_application_get_is_remote (GApplication *application)
1873 {
1874   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1875   g_return_val_if_fail (application->priv->is_registered, FALSE);
1876
1877   return application->priv->is_remote;
1878 }
1879
1880 /**
1881  * g_application_get_dbus_connection:
1882  * @application: a #GApplication
1883  *
1884  * Gets the #GDBusConnection being used by the application, or %NULL.
1885  *
1886  * If #GApplication is using its D-Bus backend then this function will
1887  * return the #GDBusConnection being used for uniqueness and
1888  * communication with the desktop environment and other instances of the
1889  * application.
1890  *
1891  * If #GApplication is not using D-Bus then this function will return
1892  * %NULL.  This includes the situation where the D-Bus backend would
1893  * normally be in use but we were unable to connect to the bus.
1894  *
1895  * This function must not be called before the application has been
1896  * registered.  See g_application_get_is_registered().
1897  *
1898  * Returns: (transfer none): a #GDBusConnection, or %NULL
1899  *
1900  * Since: 2.34
1901  **/
1902 GDBusConnection *
1903 g_application_get_dbus_connection (GApplication *application)
1904 {
1905   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1906   g_return_val_if_fail (application->priv->is_registered, FALSE);
1907
1908   return g_application_impl_get_dbus_connection (application->priv->impl);
1909 }
1910
1911 /**
1912  * g_application_get_dbus_object_path:
1913  * @application: a #GApplication
1914  *
1915  * Gets the D-Bus object path being used by the application, or %NULL.
1916  *
1917  * If #GApplication is using its D-Bus backend then this function will
1918  * return the D-Bus object path that #GApplication is using.  If the
1919  * application is the primary instance then there is an object published
1920  * at this path.  If the application is not the primary instance then
1921  * the result of this function is undefined.
1922  *
1923  * If #GApplication is not using D-Bus then this function will return
1924  * %NULL.  This includes the situation where the D-Bus backend would
1925  * normally be in use but we were unable to connect to the bus.
1926  *
1927  * This function must not be called before the application has been
1928  * registered.  See g_application_get_is_registered().
1929  *
1930  * Returns: the object path, or %NULL
1931  *
1932  * Since: 2.34
1933  **/
1934 const gchar *
1935 g_application_get_dbus_object_path (GApplication *application)
1936 {
1937   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1938   g_return_val_if_fail (application->priv->is_registered, FALSE);
1939
1940   return g_application_impl_get_dbus_object_path (application->priv->impl);
1941 }
1942
1943
1944 /* Register {{{1 */
1945 /**
1946  * g_application_register:
1947  * @application: a #GApplication
1948  * @cancellable: (allow-none): a #GCancellable, or %NULL
1949  * @error: a pointer to a NULL #GError, or %NULL
1950  *
1951  * Attempts registration of the application.
1952  *
1953  * This is the point at which the application discovers if it is the
1954  * primary instance or merely acting as a remote for an already-existing
1955  * primary instance.  This is implemented by attempting to acquire the
1956  * application identifier as a unique bus name on the session bus using
1957  * GDBus.
1958  *
1959  * If there is no application ID or if %G_APPLICATION_NON_UNIQUE was
1960  * given, then this process will always become the primary instance.
1961  *
1962  * Due to the internal architecture of GDBus, method calls can be
1963  * dispatched at any time (even if a main loop is not running).  For
1964  * this reason, you must ensure that any object paths that you wish to
1965  * register are registered before calling this function.
1966  *
1967  * If the application has already been registered then %TRUE is
1968  * returned with no work performed.
1969  *
1970  * The #GApplication::startup signal is emitted if registration succeeds
1971  * and @application is the primary instance (including the non-unique
1972  * case).
1973  *
1974  * In the event of an error (such as @cancellable being cancelled, or a
1975  * failure to connect to the session bus), %FALSE is returned and @error
1976  * is set appropriately.
1977  *
1978  * Note: the return value of this function is not an indicator that this
1979  * instance is or is not the primary instance of the application.  See
1980  * g_application_get_is_remote() for that.
1981  *
1982  * Returns: %TRUE if registration succeeded
1983  *
1984  * Since: 2.28
1985  **/
1986 gboolean
1987 g_application_register (GApplication  *application,
1988                         GCancellable  *cancellable,
1989                         GError       **error)
1990 {
1991   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1992
1993   if (!application->priv->is_registered)
1994     {
1995       if (application->priv->id == NULL)
1996         application->priv->flags |= G_APPLICATION_NON_UNIQUE;
1997
1998       application->priv->impl =
1999         g_application_impl_register (application, application->priv->id,
2000                                      application->priv->flags,
2001                                      application->priv->actions,
2002                                      &application->priv->remote_actions,
2003                                      cancellable, error);
2004
2005       if (application->priv->impl == NULL)
2006         return FALSE;
2007
2008       application->priv->is_remote = application->priv->remote_actions != NULL;
2009       application->priv->is_registered = TRUE;
2010
2011       g_object_notify (G_OBJECT (application), "is-registered");
2012
2013       if (!application->priv->is_remote)
2014         {
2015           g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
2016
2017           if (!application->priv->did_startup)
2018             g_critical ("GApplication subclass '%s' failed to chain up on"
2019                         " ::startup (from start of override function)",
2020                         G_OBJECT_TYPE_NAME (application));
2021         }
2022     }
2023
2024   return TRUE;
2025 }
2026
2027 /* Hold/release {{{1 */
2028 /**
2029  * g_application_hold:
2030  * @application: a #GApplication
2031  *
2032  * Increases the use count of @application.
2033  *
2034  * Use this function to indicate that the application has a reason to
2035  * continue to run.  For example, g_application_hold() is called by GTK+
2036  * when a toplevel window is on the screen.
2037  *
2038  * To cancel the hold, call g_application_release().
2039  **/
2040 void
2041 g_application_hold (GApplication *application)
2042 {
2043   g_return_if_fail (G_IS_APPLICATION (application));
2044
2045   if (application->priv->inactivity_timeout_id)
2046     {
2047       g_source_remove (application->priv->inactivity_timeout_id);
2048       application->priv->inactivity_timeout_id = 0;
2049     }
2050
2051   application->priv->use_count++;
2052 }
2053
2054 static gboolean
2055 inactivity_timeout_expired (gpointer data)
2056 {
2057   GApplication *application = G_APPLICATION (data);
2058
2059   application->priv->inactivity_timeout_id = 0;
2060
2061   return G_SOURCE_REMOVE;
2062 }
2063
2064
2065 /**
2066  * g_application_release:
2067  * @application: a #GApplication
2068  *
2069  * Decrease the use count of @application.
2070  *
2071  * When the use count reaches zero, the application will stop running.
2072  *
2073  * Never call this function except to cancel the effect of a previous
2074  * call to g_application_hold().
2075  **/
2076 void
2077 g_application_release (GApplication *application)
2078 {
2079   g_return_if_fail (G_IS_APPLICATION (application));
2080   g_return_if_fail (application->priv->use_count > 0);
2081
2082   application->priv->use_count--;
2083
2084   if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
2085     application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
2086                                                               inactivity_timeout_expired, application);
2087 }
2088
2089 /* Activate, Open {{{1 */
2090 /**
2091  * g_application_activate:
2092  * @application: a #GApplication
2093  *
2094  * Activates the application.
2095  *
2096  * In essence, this results in the #GApplication::activate signal being
2097  * emitted in the primary instance.
2098  *
2099  * The application must be registered before calling this function.
2100  *
2101  * Since: 2.28
2102  **/
2103 void
2104 g_application_activate (GApplication *application)
2105 {
2106   g_return_if_fail (G_IS_APPLICATION (application));
2107   g_return_if_fail (application->priv->is_registered);
2108
2109   if (application->priv->is_remote)
2110     g_application_impl_activate (application->priv->impl,
2111                                  get_platform_data (application, NULL));
2112
2113   else
2114     g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
2115 }
2116
2117 /**
2118  * g_application_open:
2119  * @application: a #GApplication
2120  * @files: (array length=n_files): an array of #GFiles to open
2121  * @n_files: the length of the @files array
2122  * @hint: a hint (or ""), but never %NULL
2123  *
2124  * Opens the given files.
2125  *
2126  * In essence, this results in the #GApplication::open signal being emitted
2127  * in the primary instance.
2128  *
2129  * @n_files must be greater than zero.
2130  *
2131  * @hint is simply passed through to the ::open signal.  It is
2132  * intended to be used by applications that have multiple modes for
2133  * opening files (eg: "view" vs "edit", etc).  Unless you have a need
2134  * for this functionality, you should use "".
2135  *
2136  * The application must be registered before calling this function
2137  * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
2138  *
2139  * Since: 2.28
2140  **/
2141 void
2142 g_application_open (GApplication  *application,
2143                     GFile        **files,
2144                     gint           n_files,
2145                     const gchar   *hint)
2146 {
2147   g_return_if_fail (G_IS_APPLICATION (application));
2148   g_return_if_fail (application->priv->flags &
2149                     G_APPLICATION_HANDLES_OPEN);
2150   g_return_if_fail (application->priv->is_registered);
2151
2152   if (application->priv->is_remote)
2153     g_application_impl_open (application->priv->impl,
2154                              files, n_files, hint,
2155                              get_platform_data (application, NULL));
2156
2157   else
2158     g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
2159                    0, files, n_files, hint);
2160 }
2161
2162 /* Run {{{1 */
2163 /**
2164  * g_application_run:
2165  * @application: a #GApplication
2166  * @argc: the argc from main() (or 0 if @argv is %NULL)
2167  * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
2168  *
2169  * Runs the application.
2170  *
2171  * This function is intended to be run from main() and its return value
2172  * is intended to be returned by main(). Although you are expected to pass
2173  * the @argc, @argv parameters from main() to this function, it is possible
2174  * to pass %NULL if @argv is not available or commandline handling is not
2175  * required.  Note that on Windows, @argc and @argv are ignored, and
2176  * g_win32_get_command_line() is called internally (for proper support
2177  * of Unicode commandline arguments).
2178  *
2179  * #GApplication will attempt to parse the commandline arguments.  You
2180  * can add commandline flags to the list of recognised options by way of
2181  * g_application_add_main_option_entries().  After this, the
2182  * #GApplication::handle-local-options signal is emitted, from which the
2183  * application can inspect the values of its #GOptionEntrys.
2184  *
2185  * #GApplication::handle-local-options is a good place to handle options
2186  * such as `--version`, where an immediate reply from the local process is
2187  * desired (instead of communicating with an already-running instance).
2188  * A #GApplication::handle-local-options handler can stop further processing
2189  * by returning a non-negative value, which then becomes the exit status of
2190  * the process.
2191  *
2192  * What happens next depends on the flags: if
2193  * %G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining
2194  * commandline arguments are sent to the primary instance, where a
2195  * #GApplication::command-line signal is emitted.  Otherwise, the
2196  * remaining commandline arguments are assumed to be a list of files.
2197  * If there are no files listed, the application is activated via the
2198  * #GApplication::activate signal.  If there are one or more files, and
2199  * %G_APPLICATION_HANDLES_OPEN was specified then the files are opened
2200  * via the #GApplication::open signal.
2201  *
2202  * If you are interested in doing more complicated local handling of the
2203  * commandline then you should implement your own #GApplication subclass
2204  * and override local_command_line(). In this case, you most likely want
2205  * to return %TRUE from your local_command_line() implementation to
2206  * suppress the default handling. See
2207  * [gapplication-example-cmdline2.c][gapplication-example-cmdline2]
2208  * for an example.
2209  *
2210  * If, after the above is done, the use count of the application is zero
2211  * then the exit status is returned immediately.  If the use count is
2212  * non-zero then the default main context is iterated until the use count
2213  * falls to zero, at which point 0 is returned.
2214  *
2215  * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
2216  * run for as much as 10 seconds with a use count of zero while waiting
2217  * for the message that caused the activation to arrive.  After that,
2218  * if the use count falls to zero the application will exit immediately,
2219  * except in the case that g_application_set_inactivity_timeout() is in
2220  * use.
2221  *
2222  * This function sets the prgname (g_set_prgname()), if not already set,
2223  * to the basename of argv[0].
2224  *
2225  * Since 2.40, applications that are not explicitly flagged as services
2226  * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
2227  * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
2228  * default handler for local_command_line) if "--gapplication-service"
2229  * was given in the command line.  If this flag is present then normal
2230  * commandline processing is interrupted and the
2231  * %G_APPLICATION_IS_SERVICE flag is set.  This provides a "compromise"
2232  * solution whereby running an application directly from the commandline
2233  * will invoke it in the normal way (which can be useful for debugging)
2234  * while still allowing applications to be D-Bus activated in service
2235  * mode.  The D-Bus service file should invoke the executable with
2236  * "--gapplication-service" as the sole commandline argument.  This
2237  * approach is suitable for use by most graphical applications but
2238  * should not be used from applications like editors that need precise
2239  * control over when processes invoked via the commandline will exit and
2240  * what their exit status will be.
2241  *
2242  * Returns: the exit status
2243  *
2244  * Since: 2.28
2245  **/
2246 int
2247 g_application_run (GApplication  *application,
2248                    int            argc,
2249                    char         **argv)
2250 {
2251   gchar **arguments;
2252   int status;
2253
2254   g_return_val_if_fail (G_IS_APPLICATION (application), 1);
2255   g_return_val_if_fail (argc == 0 || argv != NULL, 1);
2256   g_return_val_if_fail (!application->priv->must_quit_now, 1);
2257
2258 #ifdef G_OS_WIN32
2259   arguments = g_win32_get_command_line ();
2260 #else
2261   {
2262     gint i;
2263
2264     arguments = g_new (gchar *, argc + 1);
2265     for (i = 0; i < argc; i++)
2266       arguments[i] = g_strdup (argv[i]);
2267     arguments[i] = NULL;
2268   }
2269 #endif
2270
2271   if (g_get_prgname () == NULL && argc > 0)
2272     {
2273       gchar *prgname;
2274
2275       prgname = g_path_get_basename (argv[0]);
2276       g_set_prgname (prgname);
2277       g_free (prgname);
2278     }
2279
2280   if (!G_APPLICATION_GET_CLASS (application)
2281         ->local_command_line (application, &arguments, &status))
2282     {
2283       GError *error = NULL;
2284
2285       if (!g_application_register (application, NULL, &error))
2286         {
2287           g_printerr ("Failed to register: %s\n", error->message);
2288           g_error_free (error);
2289           return 1;
2290         }
2291
2292       g_application_call_command_line (application, (const gchar **) arguments, NULL, &status);
2293     }
2294
2295   g_strfreev (arguments);
2296
2297   if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
2298       application->priv->is_registered &&
2299       !application->priv->use_count &&
2300       !application->priv->inactivity_timeout_id)
2301     {
2302       application->priv->inactivity_timeout_id =
2303         g_timeout_add (10000, inactivity_timeout_expired, application);
2304     }
2305
2306   while (application->priv->use_count || application->priv->inactivity_timeout_id)
2307     {
2308       if (application->priv->must_quit_now)
2309         break;
2310
2311       g_main_context_iteration (NULL, TRUE);
2312       status = 0;
2313     }
2314
2315   if (application->priv->is_registered && !application->priv->is_remote)
2316     {
2317       g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
2318
2319       if (!application->priv->did_shutdown)
2320         g_critical ("GApplication subclass '%s' failed to chain up on"
2321                     " ::shutdown (from end of override function)",
2322                     G_OBJECT_TYPE_NAME (application));
2323     }
2324
2325   if (application->priv->impl)
2326     g_application_impl_flush (application->priv->impl);
2327
2328   g_settings_sync ();
2329
2330   if (!application->priv->must_quit_now)
2331     while (g_main_context_iteration (NULL, FALSE))
2332       ;
2333
2334   return status;
2335 }
2336
2337 static gchar **
2338 g_application_list_actions (GActionGroup *action_group)
2339 {
2340   GApplication *application = G_APPLICATION (action_group);
2341
2342   g_return_val_if_fail (application->priv->is_registered, NULL);
2343
2344   if (application->priv->remote_actions != NULL)
2345     return g_action_group_list_actions (G_ACTION_GROUP (application->priv->remote_actions));
2346
2347   else if (application->priv->actions != NULL)
2348     return g_action_group_list_actions (application->priv->actions);
2349
2350   else
2351     /* empty string array */
2352     return g_new0 (gchar *, 1);
2353 }
2354
2355 static gboolean
2356 g_application_query_action (GActionGroup        *group,
2357                             const gchar         *action_name,
2358                             gboolean            *enabled,
2359                             const GVariantType **parameter_type,
2360                             const GVariantType **state_type,
2361                             GVariant           **state_hint,
2362                             GVariant           **state)
2363 {
2364   GApplication *application = G_APPLICATION (group);
2365
2366   g_return_val_if_fail (application->priv->is_registered, FALSE);
2367
2368   if (application->priv->remote_actions != NULL)
2369     return g_action_group_query_action (G_ACTION_GROUP (application->priv->remote_actions),
2370                                         action_name,
2371                                         enabled,
2372                                         parameter_type,
2373                                         state_type,
2374                                         state_hint,
2375                                         state);
2376
2377   if (application->priv->actions != NULL)
2378     return g_action_group_query_action (application->priv->actions,
2379                                         action_name,
2380                                         enabled,
2381                                         parameter_type,
2382                                         state_type,
2383                                         state_hint,
2384                                         state);
2385
2386   return FALSE;
2387 }
2388
2389 static void
2390 g_application_change_action_state (GActionGroup *action_group,
2391                                    const gchar  *action_name,
2392                                    GVariant     *value)
2393 {
2394   GApplication *application = G_APPLICATION (action_group);
2395
2396   g_return_if_fail (application->priv->is_remote ||
2397                     application->priv->actions != NULL);
2398   g_return_if_fail (application->priv->is_registered);
2399
2400   if (application->priv->remote_actions)
2401     g_remote_action_group_change_action_state_full (application->priv->remote_actions,
2402                                                     action_name, value, get_platform_data (application, NULL));
2403
2404   else
2405     g_action_group_change_action_state (application->priv->actions, action_name, value);
2406 }
2407
2408 static void
2409 g_application_activate_action (GActionGroup *action_group,
2410                                const gchar  *action_name,
2411                                GVariant     *parameter)
2412 {
2413   GApplication *application = G_APPLICATION (action_group);
2414
2415   g_return_if_fail (application->priv->is_remote ||
2416                     application->priv->actions != NULL);
2417   g_return_if_fail (application->priv->is_registered);
2418
2419   if (application->priv->remote_actions)
2420     g_remote_action_group_activate_action_full (application->priv->remote_actions,
2421                                                 action_name, parameter, get_platform_data (application, NULL));
2422
2423   else
2424     g_action_group_activate_action (application->priv->actions, action_name, parameter);
2425 }
2426
2427 static GAction *
2428 g_application_lookup_action (GActionMap  *action_map,
2429                              const gchar *action_name)
2430 {
2431   GApplication *application = G_APPLICATION (action_map);
2432
2433   g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
2434
2435   return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
2436 }
2437
2438 static void
2439 g_application_add_action (GActionMap *action_map,
2440                           GAction    *action)
2441 {
2442   GApplication *application = G_APPLICATION (action_map);
2443
2444   g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2445
2446   g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
2447 }
2448
2449 static void
2450 g_application_remove_action (GActionMap  *action_map,
2451                              const gchar *action_name)
2452 {
2453   GApplication *application = G_APPLICATION (action_map);
2454
2455   g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2456
2457   g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
2458 }
2459
2460 static void
2461 g_application_action_group_iface_init (GActionGroupInterface *iface)
2462 {
2463   iface->list_actions = g_application_list_actions;
2464   iface->query_action = g_application_query_action;
2465   iface->change_action_state = g_application_change_action_state;
2466   iface->activate_action = g_application_activate_action;
2467 }
2468
2469 static void
2470 g_application_action_map_iface_init (GActionMapInterface *iface)
2471 {
2472   iface->lookup_action = g_application_lookup_action;
2473   iface->add_action = g_application_add_action;
2474   iface->remove_action = g_application_remove_action;
2475 }
2476
2477 /* Default Application {{{1 */
2478
2479 static GApplication *default_app;
2480
2481 /**
2482  * g_application_get_default:
2483  *
2484  * Returns the default #GApplication instance for this process.
2485  *
2486  * Normally there is only one #GApplication per process and it becomes
2487  * the default when it is created.  You can exercise more control over
2488  * this by using g_application_set_default().
2489  *
2490  * If there is no default application then %NULL is returned.
2491  *
2492  * Returns: (transfer none): the default application for this process, or %NULL
2493  *
2494  * Since: 2.32
2495  **/
2496 GApplication *
2497 g_application_get_default (void)
2498 {
2499   return default_app;
2500 }
2501
2502 /**
2503  * g_application_set_default:
2504  * @application: (allow-none): the application to set as default, or %NULL
2505  *
2506  * Sets or unsets the default application for the process, as returned
2507  * by g_application_get_default().
2508  *
2509  * This function does not take its own reference on @application.  If
2510  * @application is destroyed then the default application will revert
2511  * back to %NULL.
2512  *
2513  * Since: 2.32
2514  **/
2515 void
2516 g_application_set_default (GApplication *application)
2517 {
2518   default_app = application;
2519 }
2520
2521 /**
2522  * g_application_quit:
2523  * @application: a #GApplication
2524  *
2525  * Immediately quits the application.
2526  *
2527  * Upon return to the mainloop, g_application_run() will return,
2528  * calling only the 'shutdown' function before doing so.
2529  *
2530  * The hold count is ignored.
2531  *
2532  * The result of calling g_application_run() again after it returns is
2533  * unspecified.
2534  *
2535  * Since: 2.32
2536  **/
2537 void
2538 g_application_quit (GApplication *application)
2539 {
2540   g_return_if_fail (G_IS_APPLICATION (application));
2541
2542   application->priv->must_quit_now = TRUE;
2543 }
2544
2545 /**
2546  * g_application_mark_busy:
2547  * @application: a #GApplication
2548  *
2549  * Increases the busy count of @application.
2550  *
2551  * Use this function to indicate that the application is busy, for instance
2552  * while a long running operation is pending.
2553  *
2554  * The busy state will be exposed to other processes, so a session shell will
2555  * use that information to indicate the state to the user (e.g. with a
2556  * spinner).
2557  *
2558  * To cancel the busy indication, use g_application_unmark_busy().
2559  *
2560  * Since: 2.38
2561  **/
2562 void
2563 g_application_mark_busy (GApplication *application)
2564 {
2565   gboolean was_busy;
2566
2567   g_return_if_fail (G_IS_APPLICATION (application));
2568
2569   was_busy = (application->priv->busy_count > 0);
2570   application->priv->busy_count++;
2571
2572   if (!was_busy)
2573     {
2574       g_application_impl_set_busy_state (application->priv->impl, TRUE);
2575       g_object_notify (G_OBJECT (application), "is-busy");
2576     }
2577 }
2578
2579 /**
2580  * g_application_unmark_busy:
2581  * @application: a #GApplication
2582  *
2583  * Decreases the busy count of @application.
2584  *
2585  * When the busy count reaches zero, the new state will be propagated
2586  * to other processes.
2587  *
2588  * This function must only be called to cancel the effect of a previous
2589  * call to g_application_mark_busy().
2590  *
2591  * Since: 2.38
2592  **/
2593 void
2594 g_application_unmark_busy (GApplication *application)
2595 {
2596   g_return_if_fail (G_IS_APPLICATION (application));
2597   g_return_if_fail (application->priv->busy_count > 0);
2598
2599   application->priv->busy_count--;
2600
2601   if (application->priv->busy_count == 0)
2602     {
2603       g_application_impl_set_busy_state (application->priv->impl, FALSE);
2604       g_object_notify (G_OBJECT (application), "is-busy");
2605     }
2606 }
2607
2608 /**
2609  * g_application_get_is_busy:
2610  * @application: a #GApplication
2611  *
2612  * Gets the application's current busy state, as set through
2613  * g_application_mark_busy() or g_application_bind_busy_property().
2614  *
2615  * Returns: %TRUE if @application is currenty marked as busy
2616  *
2617  * Since: 2.44
2618  */
2619 gboolean
2620 g_application_get_is_busy (GApplication *application)
2621 {
2622   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2623
2624   return application->priv->busy_count > 0;
2625 }
2626
2627 /* Notifications {{{1 */
2628
2629 /**
2630  * g_application_send_notification:
2631  * @application: a #GApplication
2632  * @id: (allow-none): id of the notification, or %NULL
2633  * @notification: the #GNotification to send
2634  *
2635  * Sends a notification on behalf of @application to the desktop shell.
2636  * There is no guarantee that the notification is displayed immediately,
2637  * or even at all.
2638  *
2639  * Notifications may persist after the application exits. It will be
2640  * D-Bus-activated when the notification or one of its actions is
2641  * activated.
2642  *
2643  * Modifying @notification after this call has no effect. However, the
2644  * object can be reused for a later call to this function.
2645  *
2646  * @id may be any string that uniquely identifies the event for the
2647  * application. It does not need to be in any special format. For
2648  * example, "new-message" might be appropriate for a notification about
2649  * new messages.
2650  *
2651  * If a previous notification was sent with the same @id, it will be
2652  * replaced with @notification and shown again as if it was a new
2653  * notification. This works even for notifications sent from a previous
2654  * execution of the application, as long as @id is the same string.
2655  *
2656  * @id may be %NULL, but it is impossible to replace or withdraw
2657  * notifications without an id.
2658  *
2659  * If @notification is no longer relevant, it can be withdrawn with
2660  * g_application_withdraw_notification().
2661  *
2662  * Since: 2.40
2663  */
2664 void
2665 g_application_send_notification (GApplication  *application,
2666                                  const gchar   *id,
2667                                  GNotification *notification)
2668 {
2669   gchar *generated_id = NULL;
2670
2671   g_return_if_fail (G_IS_APPLICATION (application));
2672   g_return_if_fail (G_IS_NOTIFICATION (notification));
2673   g_return_if_fail (g_application_get_is_registered (application));
2674   g_return_if_fail (!g_application_get_is_remote (application));
2675
2676   if (application->priv->notifications == NULL)
2677     application->priv->notifications = g_notification_backend_new_default (application);
2678
2679   if (id == NULL)
2680     {
2681       generated_id = g_dbus_generate_guid ();
2682       id = generated_id;
2683     }
2684
2685   g_notification_backend_send_notification (application->priv->notifications, id, notification);
2686
2687   g_free (generated_id);
2688 }
2689
2690 /**
2691  * g_application_withdraw_notification:
2692  * @application: a #GApplication
2693  * @id: id of a previously sent notification
2694  *
2695  * Withdraws a notification that was sent with
2696  * g_application_send_notification().
2697  *
2698  * This call does nothing if a notification with @id doesn't exist or
2699  * the notification was never sent.
2700  *
2701  * This function works even for notifications sent in previous
2702  * executions of this application, as long @id is the same as it was for
2703  * the sent notification.
2704  *
2705  * Note that notifications are dismissed when the user clicks on one
2706  * of the buttons in a notification or triggers its default action, so
2707  * there is no need to explicitly withdraw the notification in that case.
2708  *
2709  * Since: 2.40
2710  */
2711 void
2712 g_application_withdraw_notification (GApplication *application,
2713                                      const gchar  *id)
2714 {
2715   g_return_if_fail (G_IS_APPLICATION (application));
2716   g_return_if_fail (id != NULL);
2717
2718   if (application->priv->notifications == NULL)
2719     application->priv->notifications = g_notification_backend_new_default (application);
2720
2721   g_notification_backend_withdraw_notification (application->priv->notifications, id);
2722 }
2723
2724 /* Busy binding {{{1 */
2725
2726 typedef struct
2727 {
2728   GApplication *app;
2729   gboolean is_busy;
2730 } GApplicationBusyBinding;
2731
2732 static void
2733 g_application_busy_binding_destroy (gpointer  data,
2734                                     GClosure *closure)
2735 {
2736   GApplicationBusyBinding *binding = data;
2737
2738   if (binding->is_busy)
2739     g_application_unmark_busy (binding->app);
2740
2741   g_object_unref (binding->app);
2742   g_slice_free (GApplicationBusyBinding, binding);
2743 }
2744
2745 static void
2746 g_application_notify_busy_binding (GObject    *object,
2747                                    GParamSpec *pspec,
2748                                    gpointer    user_data)
2749 {
2750   GApplicationBusyBinding *binding = user_data;
2751   gboolean is_busy;
2752
2753   g_object_get (object, pspec->name, &is_busy, NULL);
2754
2755   if (is_busy && !binding->is_busy)
2756     g_application_mark_busy (binding->app);
2757   else if (!is_busy && binding->is_busy)
2758     g_application_unmark_busy (binding->app);
2759
2760   binding->is_busy = is_busy;
2761 }
2762
2763 /**
2764  * g_application_bind_busy_property:
2765  * @application: a #GApplication
2766  * @object: (type GObject.Object): a #GObject
2767  * @property: the name of a boolean property of @object
2768  *
2769  * Marks @application as busy (see g_application_mark_busy()) while
2770  * @property on @object is %TRUE.
2771  *
2772  * The binding holds a reference to @application while it is active, but
2773  * not to @object. Instead, the binding is destroyed when @object is
2774  * finalized.
2775  *
2776  * Since: 2.44
2777  */
2778 void
2779 g_application_bind_busy_property (GApplication *application,
2780                                   gpointer      object,
2781                                   const gchar  *property)
2782 {
2783   guint notify_id;
2784   GQuark property_quark;
2785   GParamSpec *pspec;
2786   GApplicationBusyBinding *binding;
2787   GClosure *closure;
2788
2789   g_return_if_fail (G_IS_APPLICATION (application));
2790   g_return_if_fail (G_IS_OBJECT (object));
2791   g_return_if_fail (property != NULL);
2792
2793   notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
2794   property_quark = g_quark_from_string (property);
2795   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2796
2797   g_return_if_fail (pspec != NULL && pspec->value_type == G_TYPE_BOOLEAN);
2798
2799   if (g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
2800                              notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL) > 0)
2801     {
2802       g_critical ("%s: '%s' is already bound to the busy state of the application", G_STRFUNC, property);
2803       return;
2804     }
2805
2806   binding = g_slice_new (GApplicationBusyBinding);
2807   binding->app = g_object_ref (application);
2808   binding->is_busy = FALSE;
2809
2810   closure = g_cclosure_new (G_CALLBACK (g_application_notify_busy_binding), binding,
2811                             g_application_busy_binding_destroy);
2812   g_signal_connect_closure_by_id (object, notify_id, property_quark, closure, FALSE);
2813
2814   /* fetch the initial value */
2815   g_application_notify_busy_binding (object, pspec, binding);
2816 }
2817
2818 /**
2819  * g_application_unbind_busy_property:
2820  * @application: a #GApplication
2821  * @object: (type GObject.Object): a #GObject
2822  * @property: the name of a boolean property of @object
2823  *
2824  * Destroys a binding between @property and the busy state of
2825  * @application that was previously created with
2826  * g_application_bind_busy_property().
2827  *
2828  * Since: 2.44
2829  */
2830 void
2831 g_application_unbind_busy_property (GApplication *application,
2832                                     gpointer      object,
2833                                     const gchar  *property)
2834 {
2835   guint notify_id;
2836   GQuark property_quark;
2837   gulong handler_id;
2838
2839   g_return_if_fail (G_IS_APPLICATION (application));
2840   g_return_if_fail (G_IS_OBJECT (object));
2841   g_return_if_fail (property != NULL);
2842
2843   notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
2844   property_quark = g_quark_from_string (property);
2845
2846   handler_id = g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
2847                                       notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL);
2848   if (handler_id == 0)
2849     {
2850       g_critical ("%s: '%s' is not bound to the busy state of the application", G_STRFUNC, property);
2851       return;
2852     }
2853
2854   g_signal_handler_disconnect (object, handler_id);
2855 }
2856
2857 /* Epilogue {{{1 */
2858 /* vim:set foldmethod=marker: */