2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
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.
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/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
23 #include "gapplication.h"
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 "gsettings.h"
32 #include "gnotification-private.h"
33 #include "gnotificationbackend.h"
34 #include "gdbusutils.h"
36 #include "gioenumtypes.h"
41 #include "gmarshal-internal.h"
46 * SECTION:gapplication
47 * @title: GApplication
48 * @short_description: Core application class
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.
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.
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.
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
89 * If used, the expected form of an application identifier is the same as
91 * [D-Bus well-known bus name](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus).
92 * Examples include: `com.example.MyApp`, `org.example.internal_apps.Calculator`,
93 * `org._7_zip.Archiver`.
94 * For details on valid application identifiers, see g_application_id_is_valid().
96 * On Linux, the application identifier is claimed as a well-known bus name
97 * on the user's session bus. This means that the uniqueness of your
98 * application is scoped to the current session. It also means that your
99 * application may provide additional services (through registration of other
100 * object paths) at that bus name. The registration of these object paths
101 * should be done with the shared GDBus session bus. Note that due to the
102 * internal architecture of GDBus, method calls can be dispatched at any time
103 * (even if a main loop is not running). For this reason, you must ensure that
104 * any object paths that you wish to register are registered before #GApplication
105 * attempts to acquire the bus name of your application (which happens in
106 * g_application_register()). Unfortunately, this means that you cannot use
107 * g_application_get_is_remote() to decide if you want to register object paths.
109 * GApplication also implements the #GActionGroup and #GActionMap
110 * interfaces and lets you easily export actions by adding them with
111 * g_action_map_add_action(). When invoking an action by calling
112 * g_action_group_activate_action() on the application, it is always
113 * invoked in the primary instance. The actions are also exported on
114 * the session bus, and GIO provides the #GDBusActionGroup wrapper to
115 * conveniently access them remotely. GIO provides a #GDBusMenuModel wrapper
116 * for remote access to exported #GMenuModels.
118 * There is a number of different entry points into a GApplication:
120 * - via 'Activate' (i.e. just starting the application)
122 * - via 'Open' (i.e. opening some files)
124 * - by handling a command-line
126 * - via activating an action
128 * The #GApplication::startup signal lets you handle the application
129 * initialization for all of these in a single place.
131 * Regardless of which of these entry points is used to start the
132 * application, GApplication passes some "platform data from the
133 * launching instance to the primary instance, in the form of a
134 * #GVariant dictionary mapping strings to variants. To use platform
135 * data, override the @before_emit or @after_emit virtual functions
136 * in your #GApplication subclass. When dealing with
137 * #GApplicationCommandLine objects, the platform data is
138 * directly available via g_application_command_line_get_cwd(),
139 * g_application_command_line_get_environ() and
140 * g_application_command_line_get_platform_data().
142 * As the name indicates, the platform data may vary depending on the
143 * operating system, but it always includes the current directory (key
144 * "cwd"), and optionally the environment (ie the set of environment
145 * variables and their values) of the calling process (key "environ").
146 * The environment is only added to the platform data if the
147 * %G_APPLICATION_SEND_ENVIRONMENT flag is set. #GApplication subclasses
148 * can add their own platform data by overriding the @add_platform_data
149 * virtual function. For instance, #GtkApplication adds startup notification
152 * To parse commandline arguments you may handle the
153 * #GApplication::command-line signal or override the local_command_line()
154 * vfunc, to parse them in either the primary instance or the local instance,
157 * For an example of opening files with a GApplication, see
158 * [gapplication-example-open.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-open.c).
160 * For an example of using actions with GApplication, see
161 * [gapplication-example-actions.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-actions.c).
163 * For an example of using extra D-Bus hooks with GApplication, see
164 * [gapplication-example-dbushooks.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-dbushooks.c).
170 * #GApplication is an opaque data structure and can only be accessed
171 * using the following functions.
177 * @startup: invoked on the primary instance immediately after registration
178 * @shutdown: invoked only on the registered primary instance immediately
179 * after the main loop terminates
180 * @activate: invoked on the primary instance when an activation occurs
181 * @open: invoked on the primary instance when there are files to open
182 * @command_line: invoked on the primary instance when a command-line is
183 * not handled locally
184 * @local_command_line: invoked (locally). The virtual function has the chance
185 * to inspect (and possibly replace) command line arguments. See
186 * g_application_run() for more information. Also see the
187 * #GApplication::handle-local-options signal, which is a simpler
188 * alternative to handling some commandline options locally
189 * @before_emit: invoked on the primary instance before 'activate', 'open',
190 * 'command-line' or any action invocation, gets the 'platform data' from
191 * the calling instance
192 * @after_emit: invoked on the primary instance after 'activate', 'open',
193 * 'command-line' or any action invocation, gets the 'platform data' from
194 * the calling instance
195 * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
196 * the primary instance when activating, opening or invoking actions
197 * @quit_mainloop: Used to be invoked on the primary instance when the use
198 * count of the application drops to zero (and after any inactivity
199 * timeout, if requested). Not used anymore since 2.32
200 * @run_mainloop: Used to be invoked on the primary instance from
201 * g_application_run() if the use-count is non-zero. Since 2.32,
202 * GApplication is iterating the main context directly and is not
203 * using @run_mainloop anymore
204 * @dbus_register: invoked locally during registration, if the application is
205 * using its D-Bus backend. You can use this to export extra objects on the
206 * bus, that need to exist before the application tries to own the bus name.
207 * The function is passed the #GDBusConnection to to session bus, and the
208 * object path that #GApplication will use to export is D-Bus API.
209 * If this function returns %TRUE, registration will proceed; otherwise
210 * registration will abort. Since: 2.34
211 * @dbus_unregister: invoked locally during unregistration, if the application
212 * is using its D-Bus backend. Use this to undo anything done by
213 * the @dbus_register vfunc. Since: 2.34
214 * @handle_local_options: invoked locally after the parsing of the commandline
215 * options has occurred. Since: 2.40
216 * @name_lost: invoked when another instance is taking over the name. Since: 2.60
218 * Virtual function table for #GApplication.
223 struct _GApplicationPrivate
225 GApplicationFlags flags;
227 gchar *resource_path;
229 GActionGroup *actions;
231 guint inactivity_timeout_id;
232 guint inactivity_timeout;
236 guint is_registered : 1;
238 guint did_startup : 1;
239 guint did_shutdown : 1;
240 guint must_quit_now : 1;
242 GRemoteActionGroup *remote_actions;
243 GApplicationImpl *impl;
245 GNotificationBackend *notifications;
247 /* GOptionContext support */
248 GOptionGroup *main_options;
249 GSList *option_groups;
250 GHashTable *packed_options;
251 gboolean options_parsed;
252 gchar *parameter_string;
256 /* Allocated option strings, from g_application_add_main_option() */
257 GSList *option_strings;
265 PROP_RESOURCE_BASE_PATH,
268 PROP_INACTIVITY_TIMEOUT,
281 SIGNAL_HANDLE_LOCAL_OPTIONS,
286 static guint g_application_signals[NR_SIGNALS];
288 static void g_application_action_group_iface_init (GActionGroupInterface *);
289 static void g_application_action_map_iface_init (GActionMapInterface *);
290 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
291 G_ADD_PRIVATE (GApplication)
292 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
293 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
295 /* GApplicationExportedActions {{{1 */
297 /* We create a subclass of GSimpleActionGroup that implements
298 * GRemoteActionGroup and deals with the platform data using
299 * GApplication's before/after_emit vfuncs. This is the action group we
302 * We could implement GRemoteActionGroup on GApplication directly, but
303 * this would be potentially extremely confusing to have exposed as part
304 * of the public API of GApplication. We certainly don't want anyone in
305 * the same process to be calling these APIs...
307 typedef GSimpleActionGroupClass GApplicationExportedActionsClass;
310 GSimpleActionGroup parent_instance;
311 GApplication *application;
312 } GApplicationExportedActions;
314 static GType g_application_exported_actions_get_type (void);
315 static void g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface);
316 G_DEFINE_TYPE_WITH_CODE (GApplicationExportedActions, g_application_exported_actions, G_TYPE_SIMPLE_ACTION_GROUP,
317 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_application_exported_actions_iface_init))
320 g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
321 const gchar *action_name,
323 GVariant *platform_data)
325 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
327 G_APPLICATION_GET_CLASS (exported->application)
328 ->before_emit (exported->application, platform_data);
330 g_action_group_activate_action (G_ACTION_GROUP (exported), action_name, parameter);
332 G_APPLICATION_GET_CLASS (exported->application)
333 ->after_emit (exported->application, platform_data);
337 g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
338 const gchar *action_name,
340 GVariant *platform_data)
342 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
344 G_APPLICATION_GET_CLASS (exported->application)
345 ->before_emit (exported->application, platform_data);
347 g_action_group_change_action_state (G_ACTION_GROUP (exported), action_name, value);
349 G_APPLICATION_GET_CLASS (exported->application)
350 ->after_emit (exported->application, platform_data);
354 g_application_exported_actions_init (GApplicationExportedActions *actions)
359 g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface)
361 iface->activate_action_full = g_application_exported_actions_activate_action_full;
362 iface->change_action_state_full = g_application_exported_actions_change_action_state_full;
366 g_application_exported_actions_class_init (GApplicationExportedActionsClass *class)
370 static GActionGroup *
371 g_application_exported_actions_new (GApplication *application)
373 GApplicationExportedActions *actions;
375 actions = g_object_new (g_application_exported_actions_get_type (), NULL);
376 actions->application = application;
378 return G_ACTION_GROUP (actions);
381 /* Command line option handling {{{1 */
384 free_option_entry (gpointer data)
386 GOptionEntry *entry = data;
390 case G_OPTION_ARG_STRING:
391 case G_OPTION_ARG_FILENAME:
392 g_free (*(gchar **) entry->arg_data);
395 case G_OPTION_ARG_STRING_ARRAY:
396 case G_OPTION_ARG_FILENAME_ARRAY:
397 g_strfreev (*(gchar ***) entry->arg_data);
401 /* most things require no free... */
405 /* ...except for the space that we allocated for it ourselves */
406 g_free (entry->arg_data);
408 g_slice_free (GOptionEntry, entry);
412 g_application_pack_option_entries (GApplication *application,
418 g_hash_table_iter_init (&iter, application->priv->packed_options);
419 while (g_hash_table_iter_next (&iter, NULL, &item))
421 GOptionEntry *entry = item;
422 GVariant *value = NULL;
426 case G_OPTION_ARG_NONE:
427 if (*(gboolean *) entry->arg_data != 2)
428 value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
431 case G_OPTION_ARG_STRING:
432 if (*(gchar **) entry->arg_data)
433 value = g_variant_new_string (*(gchar **) entry->arg_data);
436 case G_OPTION_ARG_INT:
437 if (*(gint32 *) entry->arg_data)
438 value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
441 case G_OPTION_ARG_FILENAME:
442 if (*(gchar **) entry->arg_data)
443 value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
446 case G_OPTION_ARG_STRING_ARRAY:
447 if (*(gchar ***) entry->arg_data)
448 value = g_variant_new_strv (*(const gchar ***) entry->arg_data, -1);
451 case G_OPTION_ARG_FILENAME_ARRAY:
452 if (*(gchar ***) entry->arg_data)
453 value = g_variant_new_bytestring_array (*(const gchar ***) entry->arg_data, -1);
456 case G_OPTION_ARG_DOUBLE:
457 if (*(gdouble *) entry->arg_data)
458 value = g_variant_new_double (*(gdouble *) entry->arg_data);
461 case G_OPTION_ARG_INT64:
462 if (*(gint64 *) entry->arg_data)
463 value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
467 g_assert_not_reached ();
471 g_variant_dict_insert_value (dict, entry->long_name, value);
475 static GVariantDict *
476 g_application_parse_command_line (GApplication *application,
480 gboolean become_service = FALSE;
481 gchar *app_id = NULL;
482 gboolean replace = FALSE;
483 GVariantDict *dict = NULL;
484 GOptionContext *context;
485 GOptionGroup *gapplication_group;
487 /* Due to the memory management of GOptionGroup we can only parse
488 * options once. That's because once you add a group to the
489 * GOptionContext there is no way to get it back again. This is fine:
490 * local_command_line() should never get invoked more than once
491 * anyway. Add a sanity check just to be sure.
493 g_return_val_if_fail (!application->priv->options_parsed, NULL);
495 context = g_option_context_new (application->priv->parameter_string);
496 g_option_context_set_summary (context, application->priv->summary);
497 g_option_context_set_description (context, application->priv->description);
499 gapplication_group = g_option_group_new ("gapplication",
500 _("GApplication options"), _("Show GApplication options"),
502 g_option_group_set_translation_domain (gapplication_group, GETTEXT_PACKAGE);
503 g_option_context_add_group (context, gapplication_group);
505 /* If the application has not registered local options and it has
506 * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
507 * their primary instance commandline handler may want to deal with
508 * the arguments. We must therefore ignore them.
510 * We must also ignore --help in this case since some applications
511 * will try to handle this from the remote side. See #737869.
513 if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
515 g_option_context_set_ignore_unknown_options (context, TRUE);
516 g_option_context_set_help_enabled (context, FALSE);
519 /* Add the main option group, if it exists */
520 if (application->priv->main_options)
522 /* This consumes the main_options */
523 g_option_context_set_main_group (context, application->priv->main_options);
524 application->priv->main_options = NULL;
527 /* Add any other option groups if they exist. Adding them to the
528 * context will consume them, so we free the list as we go...
530 while (application->priv->option_groups)
532 g_option_context_add_group (context, application->priv->option_groups->data);
533 application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
534 application->priv->option_groups);
537 /* In the case that we are not explicitly marked as a service or a
538 * launcher then we want to add the "--gapplication-service" option to
539 * allow the process to be made into a service.
541 if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
543 GOptionEntry entries[] = {
544 { "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
545 N_("Enter GApplication service mode (use from D-Bus service files)") },
549 g_option_group_add_entries (gapplication_group, entries);
552 /* Allow overriding the ID if the application allows it */
553 if (application->priv->flags & G_APPLICATION_CAN_OVERRIDE_APP_ID)
555 GOptionEntry entries[] = {
556 { "gapplication-app-id", '\0', 0, G_OPTION_ARG_STRING, &app_id,
557 N_("Override the application’s ID") },
561 g_option_group_add_entries (gapplication_group, entries);
564 /* Allow replacing if the application allows it */
565 if (application->priv->flags & G_APPLICATION_ALLOW_REPLACEMENT)
567 GOptionEntry entries[] = {
568 { "gapplication-replace", '\0', 0, G_OPTION_ARG_NONE, &replace,
569 N_("Replace the running instance") },
573 g_option_group_add_entries (gapplication_group, entries);
576 /* Now we parse... */
577 if (!g_option_context_parse_strv (context, arguments, error))
580 /* Check for --gapplication-service */
582 application->priv->flags |= G_APPLICATION_IS_SERVICE;
584 /* Check for --gapplication-app-id */
586 g_application_set_application_id (application, app_id);
588 /* Check for --gapplication-replace */
590 application->priv->flags |= G_APPLICATION_REPLACE;
592 dict = g_variant_dict_new (NULL);
593 if (application->priv->packed_options)
595 g_application_pack_option_entries (application, dict);
596 g_hash_table_unref (application->priv->packed_options);
597 application->priv->packed_options = NULL;
601 /* Make sure we don't run again */
602 application->priv->options_parsed = TRUE;
604 g_option_context_free (context);
611 add_packed_option (GApplication *application,
616 case G_OPTION_ARG_NONE:
617 entry->arg_data = g_new (gboolean, 1);
618 *(gboolean *) entry->arg_data = 2;
621 case G_OPTION_ARG_INT:
622 entry->arg_data = g_new0 (gint, 1);
625 case G_OPTION_ARG_STRING:
626 case G_OPTION_ARG_FILENAME:
627 case G_OPTION_ARG_STRING_ARRAY:
628 case G_OPTION_ARG_FILENAME_ARRAY:
629 entry->arg_data = g_new0 (gpointer, 1);
632 case G_OPTION_ARG_INT64:
633 entry->arg_data = g_new0 (gint64, 1);
636 case G_OPTION_ARG_DOUBLE:
637 entry->arg_data = g_new0 (gdouble, 1);
641 g_return_if_reached ();
644 if (!application->priv->packed_options)
645 application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
647 g_hash_table_insert (application->priv->packed_options,
648 g_strdup (entry->long_name),
649 g_slice_dup (GOptionEntry, entry));
653 * g_application_add_main_option_entries:
654 * @application: a #GApplication
655 * @entries: (array zero-terminated=1) (element-type GOptionEntry) a
656 * %NULL-terminated list of #GOptionEntrys
658 * Adds main option entries to be handled by @application.
660 * This function is comparable to g_option_context_add_main_entries().
662 * After the commandline arguments are parsed, the
663 * #GApplication::handle-local-options signal will be emitted. At this
664 * point, the application can inspect the values pointed to by @arg_data
665 * in the given #GOptionEntrys.
667 * Unlike #GOptionContext, #GApplication supports giving a %NULL
668 * @arg_data for a non-callback #GOptionEntry. This results in the
669 * argument in question being packed into a #GVariantDict which is also
670 * passed to #GApplication::handle-local-options, where it can be
671 * inspected and modified. If %G_APPLICATION_HANDLES_COMMAND_LINE is
672 * set, then the resulting dictionary is sent to the primary instance,
673 * where g_application_command_line_get_options_dict() will return it.
674 * This "packing" is done according to the type of the argument --
675 * booleans for normal flags, strings for strings, bytestrings for
676 * filenames, etc. The packing only occurs if the flag is given (ie: we
677 * do not pack a "false" #GVariant in the case that a flag is missing).
679 * In general, it is recommended that all commandline arguments are
680 * parsed locally. The options dictionary should then be used to
681 * transmit the result of the parsing to the primary instance, where
682 * g_variant_dict_lookup() can be used. For local options, it is
683 * possible to either use @arg_data in the usual way, or to consult (and
684 * potentially remove) the option from the options dictionary.
686 * This function is new in GLib 2.40. Before then, the only real choice
687 * was to send all of the commandline arguments (options and all) to the
688 * primary instance for handling. #GApplication ignored them completely
689 * on the local side. Calling this function "opts in" to the new
690 * behaviour, and in particular, means that unrecognised options will be
691 * treated as errors. Unrecognised options have never been ignored when
692 * %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
694 * If #GApplication::handle-local-options needs to see the list of
695 * filenames, then the use of %G_OPTION_REMAINING is recommended. If
696 * @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
697 * the options dictionary. If you do use %G_OPTION_REMAINING then you
698 * need to handle these arguments for yourself because once they are
699 * consumed, they will no longer be visible to the default handling
700 * (which treats them as filenames to be opened).
702 * It is important to use the proper GVariant format when retrieving
703 * the options with g_variant_dict_lookup():
704 * - for %G_OPTION_ARG_NONE, use b
705 * - for %G_OPTION_ARG_STRING, use &s
706 * - for %G_OPTION_ARG_INT, use i
707 * - for %G_OPTION_ARG_INT64, use x
708 * - for %G_OPTION_ARG_DOUBLE, use d
709 * - for %G_OPTION_ARG_FILENAME, use ^ay
710 * - for %G_OPTION_ARG_STRING_ARRAY, use &as
711 * - for %G_OPTION_ARG_FILENAME_ARRAY, use ^aay
716 g_application_add_main_option_entries (GApplication *application,
717 const GOptionEntry *entries)
721 g_return_if_fail (G_IS_APPLICATION (application));
722 g_return_if_fail (entries != NULL);
724 if (!application->priv->main_options)
726 application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
727 g_option_group_set_translation_domain (application->priv->main_options, NULL);
730 for (i = 0; entries[i].long_name; i++)
732 GOptionEntry my_entries[2] = { { NULL }, { NULL } };
733 my_entries[0] = entries[i];
735 if (!my_entries[0].arg_data)
736 add_packed_option (application, &my_entries[0]);
738 g_option_group_add_entries (application->priv->main_options, my_entries);
743 * g_application_add_main_option:
744 * @application: the #GApplication
745 * @long_name: the long name of an option used to specify it in a commandline
746 * @short_name: the short name of an option
747 * @flags: flags from #GOptionFlags
748 * @arg: the type of the option, as a #GOptionArg
749 * @description: the description for the option in `--help` output
750 * @arg_description: (nullable): the placeholder to use for the extra argument
751 * parsed by the option in `--help` output
753 * Add an option to be handled by @application.
755 * Calling this function is the equivalent of calling
756 * g_application_add_main_option_entries() with a single #GOptionEntry
757 * that has its arg_data member set to %NULL.
759 * The parsed arguments will be packed into a #GVariantDict which
760 * is passed to #GApplication::handle-local-options. If
761 * %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
762 * be sent to the primary instance. See
763 * g_application_add_main_option_entries() for more details.
765 * See #GOptionEntry for more documentation of the arguments.
770 g_application_add_main_option (GApplication *application,
771 const char *long_name,
775 const char *description,
776 const char *arg_description)
779 GOptionEntry my_entry[2] = {
780 { NULL, short_name, flags, arg, NULL, NULL, NULL },
784 g_return_if_fail (G_IS_APPLICATION (application));
785 g_return_if_fail (long_name != NULL);
786 g_return_if_fail (description != NULL);
788 my_entry[0].long_name = dup_string = g_strdup (long_name);
789 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
791 my_entry[0].description = dup_string = g_strdup (description);
792 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
794 my_entry[0].arg_description = dup_string = g_strdup (arg_description);
795 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
797 g_application_add_main_option_entries (application, my_entry);
801 * g_application_add_option_group:
802 * @application: the #GApplication
803 * @group: (transfer full): a #GOptionGroup
805 * Adds a #GOptionGroup to the commandline handling of @application.
807 * This function is comparable to g_option_context_add_group().
809 * Unlike g_application_add_main_option_entries(), this function does
810 * not deal with %NULL @arg_data and never transmits options to the
813 * The reason for that is because, by the time the options arrive at the
814 * primary instance, it is typically too late to do anything with them.
815 * Taking the GTK option group as an example: GTK will already have been
816 * initialised by the time the #GApplication::command-line handler runs.
817 * In the case that this is not the first-running instance of the
818 * application, the existing instance may already have been running for
821 * This means that the options from #GOptionGroup are only really usable
822 * in the case that the instance of the application being run is the
823 * first instance. Passing options like `--display=` or `--gdk-debug=`
824 * on future runs will have no effect on the existing primary instance.
826 * Calling this function will cause the options in the supplied option
827 * group to be parsed, but it does not cause you to be "opted in" to the
828 * new functionality whereby unrecognised options are rejected even if
829 * %G_APPLICATION_HANDLES_COMMAND_LINE was given.
834 g_application_add_option_group (GApplication *application,
837 g_return_if_fail (G_IS_APPLICATION (application));
838 g_return_if_fail (group != NULL);
840 application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
844 * g_application_set_option_context_parameter_string:
845 * @application: the #GApplication
846 * @parameter_string: (nullable): a string which is displayed
847 * in the first line of `--help` output, after the usage summary `programname [OPTION...]`.
849 * Sets the parameter string to be used by the commandline handling of @application.
851 * This function registers the argument to be passed to g_option_context_new()
852 * when the internal #GOptionContext of @application is created.
854 * See g_option_context_new() for more information about @parameter_string.
859 g_application_set_option_context_parameter_string (GApplication *application,
860 const gchar *parameter_string)
862 g_return_if_fail (G_IS_APPLICATION (application));
864 g_free (application->priv->parameter_string);
865 application->priv->parameter_string = g_strdup (parameter_string);
869 * g_application_set_option_context_summary:
870 * @application: the #GApplication
871 * @summary: (nullable): a string to be shown in `--help` output
872 * before the list of options, or %NULL
874 * Adds a summary to the @application option context.
876 * See g_option_context_set_summary() for more information.
881 g_application_set_option_context_summary (GApplication *application,
882 const gchar *summary)
884 g_return_if_fail (G_IS_APPLICATION (application));
886 g_free (application->priv->summary);
887 application->priv->summary = g_strdup (summary);
891 * g_application_set_option_context_description:
892 * @application: the #GApplication
893 * @description: (nullable): a string to be shown in `--help` output
894 * after the list of options, or %NULL
896 * Adds a description to the @application option context.
898 * See g_option_context_set_description() for more information.
903 g_application_set_option_context_description (GApplication *application,
904 const gchar *description)
906 g_return_if_fail (G_IS_APPLICATION (application));
908 g_free (application->priv->description);
909 application->priv->description = g_strdup (description);
914 /* vfunc defaults {{{1 */
916 g_application_real_before_emit (GApplication *application,
917 GVariant *platform_data)
922 g_application_real_after_emit (GApplication *application,
923 GVariant *platform_data)
928 g_application_real_startup (GApplication *application)
930 application->priv->did_startup = TRUE;
934 g_application_real_shutdown (GApplication *application)
936 application->priv->did_shutdown = TRUE;
940 g_application_real_activate (GApplication *application)
942 if (!g_signal_has_handler_pending (application,
943 g_application_signals[SIGNAL_ACTIVATE],
945 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
947 static gboolean warned;
952 g_warning ("Your application does not implement "
953 "g_application_activate() and has no handlers connected "
954 "to the 'activate' signal. It should do one of these.");
960 g_application_real_open (GApplication *application,
965 if (!g_signal_has_handler_pending (application,
966 g_application_signals[SIGNAL_OPEN],
968 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
970 static gboolean warned;
975 g_warning ("Your application claims to support opening files "
976 "but does not implement g_application_open() and has no "
977 "handlers connected to the 'open' signal.");
983 g_application_real_command_line (GApplication *application,
984 GApplicationCommandLine *cmdline)
986 if (!g_signal_has_handler_pending (application,
987 g_application_signals[SIGNAL_COMMAND_LINE],
989 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
991 static gboolean warned;
996 g_warning ("Your application claims to support custom command line "
997 "handling but does not implement g_application_command_line() "
998 "and has no handlers connected to the 'command-line' signal.");
1007 g_application_real_handle_local_options (GApplication *application,
1008 GVariantDict *options)
1014 get_platform_data (GApplication *application,
1017 GVariantBuilder *builder;
1020 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
1023 gchar *cwd = g_get_current_dir ();
1024 g_variant_builder_add (builder, "{sv}", "cwd",
1025 g_variant_new_bytestring (cwd));
1029 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
1034 envp = g_get_environ ();
1035 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
1038 g_variant_builder_add (builder, "{sv}", "environ", array);
1042 g_variant_builder_add (builder, "{sv}", "options", options);
1044 G_APPLICATION_GET_CLASS (application)->
1045 add_platform_data (application, builder);
1047 result = g_variant_builder_end (builder);
1048 g_variant_builder_unref (builder);
1054 g_application_call_command_line (GApplication *application,
1055 const gchar * const *arguments,
1059 if (application->priv->is_remote)
1061 GVariant *platform_data;
1063 platform_data = get_platform_data (application, options);
1064 *exit_status = g_application_impl_command_line (application->priv->impl, arguments, platform_data);
1068 GApplicationCommandLine *cmdline;
1071 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1072 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1076 g_signal_emit (application, g_application_signals[SIGNAL_COMMAND_LINE], 0, cmdline, exit_status);
1077 g_object_unref (cmdline);
1082 g_application_real_local_command_line (GApplication *application,
1086 GError *error = NULL;
1087 GVariantDict *options;
1090 options = g_application_parse_command_line (application, arguments, &error);
1093 g_printerr ("%s\n", error->message);
1098 g_signal_emit (application, g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS], 0, options, exit_status);
1100 if (*exit_status >= 0)
1102 g_variant_dict_unref (options);
1106 if (!g_application_register (application, NULL, &error))
1108 g_printerr ("Failed to register: %s\n", error->message);
1109 g_variant_dict_unref (options);
1110 g_error_free (error);
1115 n_args = g_strv_length (*arguments);
1117 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
1119 if ((*exit_status = n_args > 1))
1121 g_printerr ("GApplication service mode takes no arguments.\n");
1122 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
1128 else if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
1130 g_application_call_command_line (application,
1131 (const gchar **) *arguments,
1132 g_variant_dict_end (options),
1139 g_application_activate (application);
1145 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
1147 g_critical ("This application can not open files.");
1156 n_files = n_args - 1;
1157 files = g_new (GFile *, n_files);
1159 for (i = 0; i < n_files; i++)
1160 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
1162 g_application_open (application, files, n_files, "");
1164 for (i = 0; i < n_files; i++)
1165 g_object_unref (files[i]);
1173 g_variant_dict_unref (options);
1179 g_application_real_add_platform_data (GApplication *application,
1180 GVariantBuilder *builder)
1185 g_application_real_dbus_register (GApplication *application,
1186 GDBusConnection *connection,
1187 const gchar *object_path,
1194 g_application_real_dbus_unregister (GApplication *application,
1195 GDBusConnection *connection,
1196 const gchar *object_path)
1201 g_application_real_name_lost (GApplication *application)
1203 g_application_quit (application);
1207 /* GObject implementation stuff {{{1 */
1209 g_application_set_property (GObject *object,
1211 const GValue *value,
1214 GApplication *application = G_APPLICATION (object);
1218 case PROP_APPLICATION_ID:
1219 g_application_set_application_id (application,
1220 g_value_get_string (value));
1224 g_application_set_flags (application, g_value_get_flags (value));
1227 case PROP_RESOURCE_BASE_PATH:
1228 g_application_set_resource_base_path (application, g_value_get_string (value));
1231 case PROP_INACTIVITY_TIMEOUT:
1232 g_application_set_inactivity_timeout (application,
1233 g_value_get_uint (value));
1236 case PROP_ACTION_GROUP:
1237 g_clear_object (&application->priv->actions);
1238 application->priv->actions = g_value_dup_object (value);
1242 g_assert_not_reached ();
1247 * g_application_set_action_group:
1248 * @application: a #GApplication
1249 * @action_group: (nullable): a #GActionGroup, or %NULL
1251 * This used to be how actions were associated with a #GApplication.
1252 * Now there is #GActionMap for that.
1256 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
1257 * mix use of this API with use of #GActionMap on the same @application
1258 * or things will go very badly wrong. This function is known to
1259 * introduce buggy behaviour (ie: signals not emitted on changes to the
1260 * action group), so you should really use #GActionMap instead.
1263 g_application_set_action_group (GApplication *application,
1264 GActionGroup *action_group)
1266 g_return_if_fail (G_IS_APPLICATION (application));
1267 g_return_if_fail (!application->priv->is_registered);
1269 if (application->priv->actions != NULL)
1270 g_object_unref (application->priv->actions);
1272 application->priv->actions = action_group;
1274 if (application->priv->actions != NULL)
1275 g_object_ref (application->priv->actions);
1279 g_application_get_property (GObject *object,
1284 GApplication *application = G_APPLICATION (object);
1288 case PROP_APPLICATION_ID:
1289 g_value_set_string (value,
1290 g_application_get_application_id (application));
1294 g_value_set_flags (value,
1295 g_application_get_flags (application));
1298 case PROP_RESOURCE_BASE_PATH:
1299 g_value_set_string (value, g_application_get_resource_base_path (application));
1302 case PROP_IS_REGISTERED:
1303 g_value_set_boolean (value,
1304 g_application_get_is_registered (application));
1307 case PROP_IS_REMOTE:
1308 g_value_set_boolean (value,
1309 g_application_get_is_remote (application));
1312 case PROP_INACTIVITY_TIMEOUT:
1313 g_value_set_uint (value,
1314 g_application_get_inactivity_timeout (application));
1318 g_value_set_boolean (value, g_application_get_is_busy (application));
1322 g_assert_not_reached ();
1327 g_application_constructed (GObject *object)
1329 GApplication *application = G_APPLICATION (object);
1331 if (g_application_get_default () == NULL)
1332 g_application_set_default (application);
1334 /* People should not set properties from _init... */
1335 g_assert (application->priv->resource_path == NULL);
1337 if (application->priv->id != NULL)
1341 application->priv->resource_path = g_strconcat ("/", application->priv->id, NULL);
1343 for (i = 1; application->priv->resource_path[i]; i++)
1344 if (application->priv->resource_path[i] == '.')
1345 application->priv->resource_path[i] = '/';
1350 g_application_dispose (GObject *object)
1352 GApplication *application = G_APPLICATION (object);
1354 if (application->priv->impl != NULL &&
1355 G_APPLICATION_GET_CLASS (application)->dbus_unregister != g_application_real_dbus_unregister)
1357 static gboolean warned;
1361 g_warning ("Your application did not unregister from D-Bus before destruction. "
1362 "Consider using g_application_run().");
1368 G_OBJECT_CLASS (g_application_parent_class)->dispose (object);
1372 g_application_finalize (GObject *object)
1374 GApplication *application = G_APPLICATION (object);
1376 if (application->priv->inactivity_timeout_id)
1377 g_source_remove (application->priv->inactivity_timeout_id);
1379 g_slist_free_full (application->priv->option_groups, (GDestroyNotify) g_option_group_unref);
1380 if (application->priv->main_options)
1381 g_option_group_unref (application->priv->main_options);
1382 if (application->priv->packed_options)
1383 g_hash_table_unref (application->priv->packed_options);
1385 g_free (application->priv->parameter_string);
1386 g_free (application->priv->summary);
1387 g_free (application->priv->description);
1389 g_slist_free_full (application->priv->option_strings, g_free);
1391 if (application->priv->impl)
1392 g_application_impl_destroy (application->priv->impl);
1393 g_free (application->priv->id);
1395 if (g_application_get_default () == application)
1396 g_application_set_default (NULL);
1398 if (application->priv->actions)
1399 g_object_unref (application->priv->actions);
1401 if (application->priv->notifications)
1402 g_object_unref (application->priv->notifications);
1404 g_free (application->priv->resource_path);
1406 G_OBJECT_CLASS (g_application_parent_class)
1407 ->finalize (object);
1411 g_application_init (GApplication *application)
1413 application->priv = g_application_get_instance_private (application);
1415 application->priv->actions = g_application_exported_actions_new (application);
1417 /* application->priv->actions is the one and only ref on the group, so when
1418 * we dispose, the action group will die, disconnecting all signals.
1420 g_signal_connect_swapped (application->priv->actions, "action-added",
1421 G_CALLBACK (g_action_group_action_added), application);
1422 g_signal_connect_swapped (application->priv->actions, "action-enabled-changed",
1423 G_CALLBACK (g_action_group_action_enabled_changed), application);
1424 g_signal_connect_swapped (application->priv->actions, "action-state-changed",
1425 G_CALLBACK (g_action_group_action_state_changed), application);
1426 g_signal_connect_swapped (application->priv->actions, "action-removed",
1427 G_CALLBACK (g_action_group_action_removed), application);
1431 g_application_handle_local_options_accumulator (GSignalInvocationHint *ihint,
1432 GValue *return_accu,
1433 const GValue *handler_return,
1438 value = g_value_get_int (handler_return);
1439 g_value_set_int (return_accu, value);
1445 g_application_class_init (GApplicationClass *class)
1447 GObjectClass *object_class = G_OBJECT_CLASS (class);
1449 object_class->constructed = g_application_constructed;
1450 object_class->dispose = g_application_dispose;
1451 object_class->finalize = g_application_finalize;
1452 object_class->get_property = g_application_get_property;
1453 object_class->set_property = g_application_set_property;
1455 class->before_emit = g_application_real_before_emit;
1456 class->after_emit = g_application_real_after_emit;
1457 class->startup = g_application_real_startup;
1458 class->shutdown = g_application_real_shutdown;
1459 class->activate = g_application_real_activate;
1460 class->open = g_application_real_open;
1461 class->command_line = g_application_real_command_line;
1462 class->local_command_line = g_application_real_local_command_line;
1463 class->handle_local_options = g_application_real_handle_local_options;
1464 class->add_platform_data = g_application_real_add_platform_data;
1465 class->dbus_register = g_application_real_dbus_register;
1466 class->dbus_unregister = g_application_real_dbus_unregister;
1467 class->name_lost = g_application_real_name_lost;
1469 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
1470 g_param_spec_string ("application-id",
1471 P_("Application identifier"),
1472 P_("The unique identifier for the application"),
1473 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
1474 G_PARAM_STATIC_STRINGS));
1476 g_object_class_install_property (object_class, PROP_FLAGS,
1477 g_param_spec_flags ("flags",
1478 P_("Application flags"),
1479 P_("Flags specifying the behaviour of the application"),
1480 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
1481 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1483 g_object_class_install_property (object_class, PROP_RESOURCE_BASE_PATH,
1484 g_param_spec_string ("resource-base-path",
1485 P_("Resource base path"),
1486 P_("The base resource path for the application"),
1487 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1489 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
1490 g_param_spec_boolean ("is-registered",
1491 P_("Is registered"),
1492 P_("If g_application_register() has been called"),
1493 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1495 g_object_class_install_property (object_class, PROP_IS_REMOTE,
1496 g_param_spec_boolean ("is-remote",
1498 P_("If this application instance is remote"),
1499 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1501 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
1502 g_param_spec_uint ("inactivity-timeout",
1503 P_("Inactivity timeout"),
1504 P_("Time (ms) to stay alive after becoming idle"),
1506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1508 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
1509 g_param_spec_object ("action-group",
1511 P_("The group of actions that the application exports"),
1512 G_TYPE_ACTION_GROUP,
1513 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1516 * GApplication:is-busy:
1518 * Whether the application is currently marked as busy through
1519 * g_application_mark_busy() or g_application_bind_busy_property().
1523 g_object_class_install_property (object_class, PROP_IS_BUSY,
1524 g_param_spec_boolean ("is-busy",
1526 P_("If this application is currently marked busy"),
1527 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1530 * GApplication::startup:
1531 * @application: the application
1533 * The ::startup signal is emitted on the primary instance immediately
1534 * after registration. See g_application_register().
1536 g_application_signals[SIGNAL_STARTUP] =
1537 g_signal_new (I_("startup"), G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
1538 G_STRUCT_OFFSET (GApplicationClass, startup),
1539 NULL, NULL, NULL, G_TYPE_NONE, 0);
1542 * GApplication::shutdown:
1543 * @application: the application
1545 * The ::shutdown signal is emitted only on the registered primary instance
1546 * immediately after the main loop terminates.
1548 g_application_signals[SIGNAL_SHUTDOWN] =
1549 g_signal_new (I_("shutdown"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1550 G_STRUCT_OFFSET (GApplicationClass, shutdown),
1551 NULL, NULL, NULL, G_TYPE_NONE, 0);
1554 * GApplication::activate:
1555 * @application: the application
1557 * The ::activate signal is emitted on the primary instance when an
1558 * activation occurs. See g_application_activate().
1560 g_application_signals[SIGNAL_ACTIVATE] =
1561 g_signal_new (I_("activate"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1562 G_STRUCT_OFFSET (GApplicationClass, activate),
1563 NULL, NULL, NULL, G_TYPE_NONE, 0);
1567 * GApplication::open:
1568 * @application: the application
1569 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
1570 * @n_files: the length of @files
1571 * @hint: a hint provided by the calling instance
1573 * The ::open signal is emitted on the primary instance when there are
1574 * files to open. See g_application_open() for more information.
1576 g_application_signals[SIGNAL_OPEN] =
1577 g_signal_new (I_("open"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1578 G_STRUCT_OFFSET (GApplicationClass, open),
1580 _g_cclosure_marshal_VOID__POINTER_INT_STRING,
1581 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
1582 g_signal_set_va_marshaller (g_application_signals[SIGNAL_OPEN],
1583 G_TYPE_FROM_CLASS (class),
1584 _g_cclosure_marshal_VOID__POINTER_INT_STRINGv);
1587 * GApplication::command-line:
1588 * @application: the application
1589 * @command_line: a #GApplicationCommandLine representing the
1590 * passed commandline
1592 * The ::command-line signal is emitted on the primary instance when
1593 * a commandline is not handled locally. See g_application_run() and
1594 * the #GApplicationCommandLine documentation for more information.
1596 * Returns: An integer that is set as the exit status for the calling
1597 * process. See g_application_command_line_set_exit_status().
1599 g_application_signals[SIGNAL_COMMAND_LINE] =
1600 g_signal_new (I_("command-line"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1601 G_STRUCT_OFFSET (GApplicationClass, command_line),
1602 g_signal_accumulator_first_wins, NULL,
1603 _g_cclosure_marshal_INT__OBJECT,
1604 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
1605 g_signal_set_va_marshaller (g_application_signals[SIGNAL_COMMAND_LINE],
1606 G_TYPE_FROM_CLASS (class),
1607 _g_cclosure_marshal_INT__OBJECTv);
1610 * GApplication::handle-local-options:
1611 * @application: the application
1612 * @options: the options dictionary
1614 * The ::handle-local-options signal is emitted on the local instance
1615 * after the parsing of the commandline options has occurred.
1617 * You can add options to be recognised during commandline option
1618 * parsing using g_application_add_main_option_entries() and
1619 * g_application_add_option_group().
1621 * Signal handlers can inspect @options (along with values pointed to
1622 * from the @arg_data of an installed #GOptionEntrys) in order to
1623 * decide to perform certain actions, including direct local handling
1624 * (which may be useful for options like --version).
1626 * In the event that the application is marked
1627 * %G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will
1628 * send the @options dictionary to the primary instance where it can be
1629 * read with g_application_command_line_get_options_dict(). The signal
1630 * handler can modify the dictionary before returning, and the
1631 * modified dictionary will be sent.
1633 * In the event that %G_APPLICATION_HANDLES_COMMAND_LINE is not set,
1634 * "normal processing" will treat the remaining uncollected command
1635 * line arguments as filenames or URIs. If there are no arguments,
1636 * the application is activated by g_application_activate(). One or
1637 * more arguments results in a call to g_application_open().
1639 * If you want to handle the local commandline arguments for yourself
1640 * by converting them to calls to g_application_open() or
1641 * g_action_group_activate_action() then you must be sure to register
1642 * the application first. You should probably not call
1643 * g_application_activate() for yourself, however: just return -1 and
1644 * allow the default handler to do it for you. This will ensure that
1645 * the `--gapplication-service` switch works properly (i.e. no activation
1648 * Note that this signal is emitted from the default implementation of
1649 * local_command_line(). If you override that function and don't
1650 * chain up then this signal will never be emitted.
1652 * You can override local_command_line() if you need more powerful
1653 * capabilities than what is provided here, but this should not
1654 * normally be required.
1656 * Returns: an exit code. If you have handled your options and want
1657 * to exit the process, return a non-negative option, 0 for success,
1658 * and a positive value for failure. To continue, return -1 to let
1659 * the default option processing continue.
1663 g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS] =
1664 g_signal_new (I_("handle-local-options"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1665 G_STRUCT_OFFSET (GApplicationClass, handle_local_options),
1666 g_application_handle_local_options_accumulator, NULL,
1667 _g_cclosure_marshal_INT__BOXED,
1668 G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
1669 g_signal_set_va_marshaller (g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS],
1670 G_TYPE_FROM_CLASS (class),
1671 _g_cclosure_marshal_INT__BOXEDv);
1674 * GApplication::name-lost:
1675 * @application: the application
1677 * The ::name-lost signal is emitted only on the registered primary instance
1678 * when a new instance has taken over. This can only happen if the application
1679 * is using the %G_APPLICATION_ALLOW_REPLACEMENT flag.
1681 * The default handler for this signal calls g_application_quit().
1683 * Returns: %TRUE if the signal has been handled
1687 g_application_signals[SIGNAL_NAME_LOST] =
1688 g_signal_new (I_("name-lost"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1689 G_STRUCT_OFFSET (GApplicationClass, name_lost),
1690 g_signal_accumulator_true_handled, NULL,
1691 _g_cclosure_marshal_BOOLEAN__VOID,
1693 g_signal_set_va_marshaller (g_application_signals[SIGNAL_NAME_LOST],
1694 G_TYPE_FROM_CLASS (class),
1695 _g_cclosure_marshal_BOOLEAN__VOIDv);
1698 /* Application ID validity {{{1 */
1701 * g_application_id_is_valid:
1702 * @application_id: a potential application identifier
1704 * Checks if @application_id is a valid application identifier.
1706 * A valid ID is required for calls to g_application_new() and
1707 * g_application_set_application_id().
1709 * Application identifiers follow the same format as
1710 * [D-Bus well-known bus names](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus).
1711 * For convenience, the restrictions on application identifiers are
1714 * - Application identifiers are composed of 1 or more elements separated by a
1715 * period (`.`) character. All elements must contain at least one character.
1717 * - Each element must only contain the ASCII characters `[A-Z][a-z][0-9]_-`,
1718 * with `-` discouraged in new application identifiers. Each element must not
1719 * begin with a digit.
1721 * - Application identifiers must contain at least one `.` (period) character
1722 * (and thus at least two elements).
1724 * - Application identifiers must not begin with a `.` (period) character.
1726 * - Application identifiers must not exceed 255 characters.
1728 * Note that the hyphen (`-`) character is allowed in application identifiers,
1729 * but is problematic or not allowed in various specifications and APIs that
1730 * refer to D-Bus, such as
1731 * [Flatpak application IDs](http://docs.flatpak.org/en/latest/introduction.html#identifiers),
1733 * [`DBusActivatable` interface in the Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus),
1734 * and the convention that an application's "main" interface and object path
1735 * resemble its application identifier and bus name. To avoid situations that
1736 * require special-case handling, it is recommended that new application
1737 * identifiers consistently replace hyphens with underscores.
1739 * Like D-Bus interface names, application identifiers should start with the
1740 * reversed DNS domain name of the author of the interface (in lower-case), and
1741 * it is conventional for the rest of the application identifier to consist of
1742 * words run together, with initial capital letters.
1744 * As with D-Bus interface names, if the author's DNS domain name contains
1745 * hyphen/minus characters they should be replaced by underscores, and if it
1746 * contains leading digits they should be escaped by prepending an underscore.
1747 * For example, if the owner of 7-zip.org used an application identifier for an
1748 * archiving application, it might be named `org._7_zip.Archiver`.
1750 * Returns: %TRUE if @application_id is valid
1753 g_application_id_is_valid (const gchar *application_id)
1755 return g_dbus_is_name (application_id) &&
1756 !g_dbus_is_unique_name (application_id);
1759 /* Public Constructor {{{1 */
1761 * g_application_new:
1762 * @application_id: (nullable): the application id
1763 * @flags: the application flags
1765 * Creates a new #GApplication instance.
1767 * If non-%NULL, the application id must be valid. See
1768 * g_application_id_is_valid().
1770 * If no application ID is given then some features of #GApplication
1771 * (most notably application uniqueness) will be disabled.
1773 * Returns: a new #GApplication instance
1776 g_application_new (const gchar *application_id,
1777 GApplicationFlags flags)
1779 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
1781 return g_object_new (G_TYPE_APPLICATION,
1782 "application-id", application_id,
1787 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
1789 * g_application_get_application_id:
1790 * @application: a #GApplication
1792 * Gets the unique identifier for @application.
1794 * Returns: the identifier for @application, owned by @application
1799 g_application_get_application_id (GApplication *application)
1801 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1803 return application->priv->id;
1807 * g_application_set_application_id:
1808 * @application: a #GApplication
1809 * @application_id: (nullable): the identifier for @application
1811 * Sets the unique identifier for @application.
1813 * The application id can only be modified if @application has not yet
1816 * If non-%NULL, the application id must be valid. See
1817 * g_application_id_is_valid().
1822 g_application_set_application_id (GApplication *application,
1823 const gchar *application_id)
1825 g_return_if_fail (G_IS_APPLICATION (application));
1827 if (g_strcmp0 (application->priv->id, application_id) != 0)
1829 g_return_if_fail (application_id == NULL || g_application_id_is_valid (application_id));
1830 g_return_if_fail (!application->priv->is_registered);
1832 g_free (application->priv->id);
1833 application->priv->id = g_strdup (application_id);
1835 g_object_notify (G_OBJECT (application), "application-id");
1840 * g_application_get_flags:
1841 * @application: a #GApplication
1843 * Gets the flags for @application.
1845 * See #GApplicationFlags.
1847 * Returns: the flags for @application
1852 g_application_get_flags (GApplication *application)
1854 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1856 return application->priv->flags;
1860 * g_application_set_flags:
1861 * @application: a #GApplication
1862 * @flags: the flags for @application
1864 * Sets the flags for @application.
1866 * The flags can only be modified if @application has not yet been
1869 * See #GApplicationFlags.
1874 g_application_set_flags (GApplication *application,
1875 GApplicationFlags flags)
1877 g_return_if_fail (G_IS_APPLICATION (application));
1879 if (application->priv->flags != flags)
1881 g_return_if_fail (!application->priv->is_registered);
1883 application->priv->flags = flags;
1885 g_object_notify (G_OBJECT (application), "flags");
1890 * g_application_get_resource_base_path:
1891 * @application: a #GApplication
1893 * Gets the resource base path of @application.
1895 * See g_application_set_resource_base_path() for more information.
1897 * Returns: (nullable): the base resource path, if one is set
1902 g_application_get_resource_base_path (GApplication *application)
1904 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1906 return application->priv->resource_path;
1910 * g_application_set_resource_base_path:
1911 * @application: a #GApplication
1912 * @resource_path: (nullable): the resource path to use
1914 * Sets (or unsets) the base resource path of @application.
1916 * The path is used to automatically load various [application
1917 * resources][gresource] such as menu layouts and action descriptions.
1918 * The various types of resources will be found at fixed names relative
1919 * to the given base path.
1921 * By default, the resource base path is determined from the application
1922 * ID by prefixing '/' and replacing each '.' with '/'. This is done at
1923 * the time that the #GApplication object is constructed. Changes to
1924 * the application ID after that point will not have an impact on the
1925 * resource base path.
1927 * As an example, if the application has an ID of "org.example.app" then
1928 * the default resource base path will be "/org/example/app". If this
1929 * is a #GtkApplication (and you have not manually changed the path)
1930 * then Gtk will then search for the menus of the application at
1931 * "/org/example/app/gtk/menus.ui".
1933 * See #GResource for more information about adding resources to your
1936 * You can disable automatic resource loading functionality by setting
1937 * the path to %NULL.
1939 * Changing the resource base path once the application is running is
1940 * not recommended. The point at which the resource path is consulted
1941 * for forming paths for various purposes is unspecified. When writing
1942 * a sub-class of #GApplication you should either set the
1943 * #GApplication:resource-base-path property at construction time, or call
1944 * this function during the instance initialization. Alternatively, you
1945 * can call this function in the #GApplicationClass.startup virtual function,
1946 * before chaining up to the parent implementation.
1951 g_application_set_resource_base_path (GApplication *application,
1952 const gchar *resource_path)
1954 g_return_if_fail (G_IS_APPLICATION (application));
1955 g_return_if_fail (resource_path == NULL || g_str_has_prefix (resource_path, "/"));
1957 if (g_strcmp0 (application->priv->resource_path, resource_path) != 0)
1959 g_free (application->priv->resource_path);
1961 application->priv->resource_path = g_strdup (resource_path);
1963 g_object_notify (G_OBJECT (application), "resource-base-path");
1968 * g_application_get_inactivity_timeout:
1969 * @application: a #GApplication
1971 * Gets the current inactivity timeout for the application.
1973 * This is the amount of time (in milliseconds) after the last call to
1974 * g_application_release() before the application stops running.
1976 * Returns: the timeout, in milliseconds
1981 g_application_get_inactivity_timeout (GApplication *application)
1983 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1985 return application->priv->inactivity_timeout;
1989 * g_application_set_inactivity_timeout:
1990 * @application: a #GApplication
1991 * @inactivity_timeout: the timeout, in milliseconds
1993 * Sets the current inactivity timeout for the application.
1995 * This is the amount of time (in milliseconds) after the last call to
1996 * g_application_release() before the application stops running.
1998 * This call has no side effects of its own. The value set here is only
1999 * used for next time g_application_release() drops the use count to
2000 * zero. Any timeouts currently in progress are not impacted.
2005 g_application_set_inactivity_timeout (GApplication *application,
2006 guint inactivity_timeout)
2008 g_return_if_fail (G_IS_APPLICATION (application));
2010 if (application->priv->inactivity_timeout != inactivity_timeout)
2012 application->priv->inactivity_timeout = inactivity_timeout;
2014 g_object_notify (G_OBJECT (application), "inactivity-timeout");
2017 /* Read-only property getters (is registered, is remote, dbus stuff) {{{1 */
2019 * g_application_get_is_registered:
2020 * @application: a #GApplication
2022 * Checks if @application is registered.
2024 * An application is registered if g_application_register() has been
2025 * successfully called.
2027 * Returns: %TRUE if @application is registered
2032 g_application_get_is_registered (GApplication *application)
2034 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2036 return application->priv->is_registered;
2040 * g_application_get_is_remote:
2041 * @application: a #GApplication
2043 * Checks if @application is remote.
2045 * If @application is remote then it means that another instance of
2046 * application already exists (the 'primary' instance). Calls to
2047 * perform actions on @application will result in the actions being
2048 * performed by the primary instance.
2050 * The value of this property cannot be accessed before
2051 * g_application_register() has been called. See
2052 * g_application_get_is_registered().
2054 * Returns: %TRUE if @application is remote
2059 g_application_get_is_remote (GApplication *application)
2061 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2062 g_return_val_if_fail (application->priv->is_registered, FALSE);
2064 return application->priv->is_remote;
2068 * g_application_get_dbus_connection:
2069 * @application: a #GApplication
2071 * Gets the #GDBusConnection being used by the application, or %NULL.
2073 * If #GApplication is using its D-Bus backend then this function will
2074 * return the #GDBusConnection being used for uniqueness and
2075 * communication with the desktop environment and other instances of the
2078 * If #GApplication is not using D-Bus then this function will return
2079 * %NULL. This includes the situation where the D-Bus backend would
2080 * normally be in use but we were unable to connect to the bus.
2082 * This function must not be called before the application has been
2083 * registered. See g_application_get_is_registered().
2085 * Returns: (transfer none): a #GDBusConnection, or %NULL
2090 g_application_get_dbus_connection (GApplication *application)
2092 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2093 g_return_val_if_fail (application->priv->is_registered, FALSE);
2095 return g_application_impl_get_dbus_connection (application->priv->impl);
2099 * g_application_get_dbus_object_path:
2100 * @application: a #GApplication
2102 * Gets the D-Bus object path being used by the application, or %NULL.
2104 * If #GApplication is using its D-Bus backend then this function will
2105 * return the D-Bus object path that #GApplication is using. If the
2106 * application is the primary instance then there is an object published
2107 * at this path. If the application is not the primary instance then
2108 * the result of this function is undefined.
2110 * If #GApplication is not using D-Bus then this function will return
2111 * %NULL. This includes the situation where the D-Bus backend would
2112 * normally be in use but we were unable to connect to the bus.
2114 * This function must not be called before the application has been
2115 * registered. See g_application_get_is_registered().
2117 * Returns: the object path, or %NULL
2122 g_application_get_dbus_object_path (GApplication *application)
2124 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2125 g_return_val_if_fail (application->priv->is_registered, FALSE);
2127 return g_application_impl_get_dbus_object_path (application->priv->impl);
2133 * g_application_register:
2134 * @application: a #GApplication
2135 * @cancellable: (nullable): a #GCancellable, or %NULL
2136 * @error: a pointer to a NULL #GError, or %NULL
2138 * Attempts registration of the application.
2140 * This is the point at which the application discovers if it is the
2141 * primary instance or merely acting as a remote for an already-existing
2142 * primary instance. This is implemented by attempting to acquire the
2143 * application identifier as a unique bus name on the session bus using
2146 * If there is no application ID or if %G_APPLICATION_NON_UNIQUE was
2147 * given, then this process will always become the primary instance.
2149 * Due to the internal architecture of GDBus, method calls can be
2150 * dispatched at any time (even if a main loop is not running). For
2151 * this reason, you must ensure that any object paths that you wish to
2152 * register are registered before calling this function.
2154 * If the application has already been registered then %TRUE is
2155 * returned with no work performed.
2157 * The #GApplication::startup signal is emitted if registration succeeds
2158 * and @application is the primary instance (including the non-unique
2161 * In the event of an error (such as @cancellable being cancelled, or a
2162 * failure to connect to the session bus), %FALSE is returned and @error
2163 * is set appropriately.
2165 * Note: the return value of this function is not an indicator that this
2166 * instance is or is not the primary instance of the application. See
2167 * g_application_get_is_remote() for that.
2169 * Returns: %TRUE if registration succeeded
2174 g_application_register (GApplication *application,
2175 GCancellable *cancellable,
2178 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2180 if (!application->priv->is_registered)
2182 if (application->priv->id == NULL)
2183 application->priv->flags |= G_APPLICATION_NON_UNIQUE;
2185 application->priv->impl =
2186 g_application_impl_register (application, application->priv->id,
2187 application->priv->flags,
2188 application->priv->actions,
2189 &application->priv->remote_actions,
2190 cancellable, error);
2192 if (application->priv->impl == NULL)
2195 application->priv->is_remote = application->priv->remote_actions != NULL;
2196 application->priv->is_registered = TRUE;
2198 g_object_notify (G_OBJECT (application), "is-registered");
2200 if (!application->priv->is_remote)
2202 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
2204 if (!application->priv->did_startup)
2205 g_critical ("GApplication subclass '%s' failed to chain up on"
2206 " ::startup (from start of override function)",
2207 G_OBJECT_TYPE_NAME (application));
2214 /* Hold/release {{{1 */
2216 * g_application_hold:
2217 * @application: a #GApplication
2219 * Increases the use count of @application.
2221 * Use this function to indicate that the application has a reason to
2222 * continue to run. For example, g_application_hold() is called by GTK+
2223 * when a toplevel window is on the screen.
2225 * To cancel the hold, call g_application_release().
2228 g_application_hold (GApplication *application)
2230 g_return_if_fail (G_IS_APPLICATION (application));
2232 if (application->priv->inactivity_timeout_id)
2234 g_source_remove (application->priv->inactivity_timeout_id);
2235 application->priv->inactivity_timeout_id = 0;
2238 application->priv->use_count++;
2242 inactivity_timeout_expired (gpointer data)
2244 GApplication *application = G_APPLICATION (data);
2246 application->priv->inactivity_timeout_id = 0;
2248 return G_SOURCE_REMOVE;
2253 * g_application_release:
2254 * @application: a #GApplication
2256 * Decrease the use count of @application.
2258 * When the use count reaches zero, the application will stop running.
2260 * Never call this function except to cancel the effect of a previous
2261 * call to g_application_hold().
2264 g_application_release (GApplication *application)
2266 g_return_if_fail (G_IS_APPLICATION (application));
2267 g_return_if_fail (application->priv->use_count > 0);
2269 application->priv->use_count--;
2271 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
2272 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
2273 inactivity_timeout_expired, application);
2276 /* Activate, Open {{{1 */
2278 * g_application_activate:
2279 * @application: a #GApplication
2281 * Activates the application.
2283 * In essence, this results in the #GApplication::activate signal being
2284 * emitted in the primary instance.
2286 * The application must be registered before calling this function.
2291 g_application_activate (GApplication *application)
2293 g_return_if_fail (G_IS_APPLICATION (application));
2294 g_return_if_fail (application->priv->is_registered);
2296 if (application->priv->is_remote)
2297 g_application_impl_activate (application->priv->impl,
2298 get_platform_data (application, NULL));
2301 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
2305 * g_application_open:
2306 * @application: a #GApplication
2307 * @files: (array length=n_files): an array of #GFiles to open
2308 * @n_files: the length of the @files array
2309 * @hint: a hint (or ""), but never %NULL
2311 * Opens the given files.
2313 * In essence, this results in the #GApplication::open signal being emitted
2314 * in the primary instance.
2316 * @n_files must be greater than zero.
2318 * @hint is simply passed through to the ::open signal. It is
2319 * intended to be used by applications that have multiple modes for
2320 * opening files (eg: "view" vs "edit", etc). Unless you have a need
2321 * for this functionality, you should use "".
2323 * The application must be registered before calling this function
2324 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
2329 g_application_open (GApplication *application,
2334 g_return_if_fail (G_IS_APPLICATION (application));
2335 g_return_if_fail (application->priv->flags &
2336 G_APPLICATION_HANDLES_OPEN);
2337 g_return_if_fail (application->priv->is_registered);
2339 if (application->priv->is_remote)
2340 g_application_impl_open (application->priv->impl,
2341 files, n_files, hint,
2342 get_platform_data (application, NULL));
2345 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
2346 0, files, n_files, hint);
2351 * g_application_run:
2352 * @application: a #GApplication
2353 * @argc: the argc from main() (or 0 if @argv is %NULL)
2354 * @argv: (array length=argc) (element-type filename) (nullable):
2355 * the argv from main(), or %NULL
2357 * Runs the application.
2359 * This function is intended to be run from main() and its return value
2360 * is intended to be returned by main(). Although you are expected to pass
2361 * the @argc, @argv parameters from main() to this function, it is possible
2362 * to pass %NULL if @argv is not available or commandline handling is not
2363 * required. Note that on Windows, @argc and @argv are ignored, and
2364 * g_win32_get_command_line() is called internally (for proper support
2365 * of Unicode commandline arguments).
2367 * #GApplication will attempt to parse the commandline arguments. You
2368 * can add commandline flags to the list of recognised options by way of
2369 * g_application_add_main_option_entries(). After this, the
2370 * #GApplication::handle-local-options signal is emitted, from which the
2371 * application can inspect the values of its #GOptionEntrys.
2373 * #GApplication::handle-local-options is a good place to handle options
2374 * such as `--version`, where an immediate reply from the local process is
2375 * desired (instead of communicating with an already-running instance).
2376 * A #GApplication::handle-local-options handler can stop further processing
2377 * by returning a non-negative value, which then becomes the exit status of
2380 * What happens next depends on the flags: if
2381 * %G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining
2382 * commandline arguments are sent to the primary instance, where a
2383 * #GApplication::command-line signal is emitted. Otherwise, the
2384 * remaining commandline arguments are assumed to be a list of files.
2385 * If there are no files listed, the application is activated via the
2386 * #GApplication::activate signal. If there are one or more files, and
2387 * %G_APPLICATION_HANDLES_OPEN was specified then the files are opened
2388 * via the #GApplication::open signal.
2390 * If you are interested in doing more complicated local handling of the
2391 * commandline then you should implement your own #GApplication subclass
2392 * and override local_command_line(). In this case, you most likely want
2393 * to return %TRUE from your local_command_line() implementation to
2394 * suppress the default handling. See
2395 * [gapplication-example-cmdline2.c][gapplication-example-cmdline2]
2398 * If, after the above is done, the use count of the application is zero
2399 * then the exit status is returned immediately. If the use count is
2400 * non-zero then the default main context is iterated until the use count
2401 * falls to zero, at which point 0 is returned.
2403 * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
2404 * run for as much as 10 seconds with a use count of zero while waiting
2405 * for the message that caused the activation to arrive. After that,
2406 * if the use count falls to zero the application will exit immediately,
2407 * except in the case that g_application_set_inactivity_timeout() is in
2410 * This function sets the prgname (g_set_prgname()), if not already set,
2411 * to the basename of argv[0].
2413 * Much like g_main_loop_run(), this function will acquire the main context
2414 * for the duration that the application is running.
2416 * Since 2.40, applications that are not explicitly flagged as services
2417 * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
2418 * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
2419 * default handler for local_command_line) if "--gapplication-service"
2420 * was given in the command line. If this flag is present then normal
2421 * commandline processing is interrupted and the
2422 * %G_APPLICATION_IS_SERVICE flag is set. This provides a "compromise"
2423 * solution whereby running an application directly from the commandline
2424 * will invoke it in the normal way (which can be useful for debugging)
2425 * while still allowing applications to be D-Bus activated in service
2426 * mode. The D-Bus service file should invoke the executable with
2427 * "--gapplication-service" as the sole commandline argument. This
2428 * approach is suitable for use by most graphical applications but
2429 * should not be used from applications like editors that need precise
2430 * control over when processes invoked via the commandline will exit and
2431 * what their exit status will be.
2433 * Returns: the exit status
2438 g_application_run (GApplication *application,
2444 GMainContext *context;
2445 gboolean acquired_context;
2447 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
2448 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
2449 g_return_val_if_fail (!application->priv->must_quit_now, 1);
2455 arguments = g_win32_get_command_line ();
2458 * CommandLineToArgvW(), which is called by g_win32_get_command_line(),
2459 * pulls in the whole command line that is used to call the program. This is
2460 * fine in cases where the program is a .exe program, but in the cases where the
2461 * program is a called via a script, such as PyGObject's gtk-demo.py, which is normally
2462 * called using 'python gtk-demo.py' on Windows, the program name (argv[0])
2463 * returned by g_win32_get_command_line() will not be the argv[0] that ->local_command_line()
2464 * would expect, causing the program to fail with "This application can not open files."
2466 new_argc = g_strv_length (arguments);
2468 if (new_argc > argc)
2472 for (i = 0; i < new_argc - argc; i++)
2473 g_free (arguments[i]);
2475 memmove (&arguments[0],
2476 &arguments[new_argc - argc],
2477 sizeof (arguments[0]) * (argc + 1));
2480 #elif defined(__APPLE__)
2485 * OSX adds an unexpected parameter on the format -psn_X_XXXXXX
2486 * when opening the application using Launch Services. In order
2487 * to avoid that GOption fails to parse this parameter we just
2488 * skip it if it was provided.
2489 * See: https://gitlab.gnome.org/GNOME/glib/issues/1784
2491 arguments = g_new (gchar *, argc + 1);
2492 for (i = 0, j = 0; i < argc; i++)
2494 if (!g_str_has_prefix (argv[i], "-psn_"))
2496 arguments[j] = g_strdup (argv[i]);
2500 arguments[j] = NULL;
2506 arguments = g_new (gchar *, argc + 1);
2507 for (i = 0; i < argc; i++)
2508 arguments[i] = g_strdup (argv[i]);
2509 arguments[i] = NULL;
2513 if (g_get_prgname () == NULL && argc > 0)
2517 prgname = g_path_get_basename (argv[0]);
2518 g_set_prgname (prgname);
2522 context = g_main_context_default ();
2523 acquired_context = g_main_context_acquire (context);
2524 g_return_val_if_fail (acquired_context, 0);
2526 if (!G_APPLICATION_GET_CLASS (application)
2527 ->local_command_line (application, &arguments, &status))
2529 GError *error = NULL;
2531 if (!g_application_register (application, NULL, &error))
2533 g_printerr ("Failed to register: %s\n", error->message);
2534 g_error_free (error);
2538 g_application_call_command_line (application, (const gchar **) arguments, NULL, &status);
2541 g_strfreev (arguments);
2543 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
2544 application->priv->is_registered &&
2545 !application->priv->use_count &&
2546 !application->priv->inactivity_timeout_id)
2548 application->priv->inactivity_timeout_id =
2549 g_timeout_add (10000, inactivity_timeout_expired, application);
2552 while (application->priv->use_count || application->priv->inactivity_timeout_id)
2554 if (application->priv->must_quit_now)
2557 g_main_context_iteration (context, TRUE);
2561 if (application->priv->is_registered && !application->priv->is_remote)
2563 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
2565 if (!application->priv->did_shutdown)
2566 g_critical ("GApplication subclass '%s' failed to chain up on"
2567 " ::shutdown (from end of override function)",
2568 G_OBJECT_TYPE_NAME (application));
2571 if (application->priv->impl)
2573 g_application_impl_flush (application->priv->impl);
2574 g_application_impl_destroy (application->priv->impl);
2575 application->priv->impl = NULL;
2580 if (!application->priv->must_quit_now)
2581 while (g_main_context_iteration (context, FALSE))
2584 g_main_context_release (context);
2590 g_application_list_actions (GActionGroup *action_group)
2592 GApplication *application = G_APPLICATION (action_group);
2594 g_return_val_if_fail (application->priv->is_registered, NULL);
2596 if (application->priv->remote_actions != NULL)
2597 return g_action_group_list_actions (G_ACTION_GROUP (application->priv->remote_actions));
2599 else if (application->priv->actions != NULL)
2600 return g_action_group_list_actions (application->priv->actions);
2603 /* empty string array */
2604 return g_new0 (gchar *, 1);
2608 g_application_query_action (GActionGroup *group,
2609 const gchar *action_name,
2611 const GVariantType **parameter_type,
2612 const GVariantType **state_type,
2613 GVariant **state_hint,
2616 GApplication *application = G_APPLICATION (group);
2618 g_return_val_if_fail (application->priv->is_registered, FALSE);
2620 if (application->priv->remote_actions != NULL)
2621 return g_action_group_query_action (G_ACTION_GROUP (application->priv->remote_actions),
2629 if (application->priv->actions != NULL)
2630 return g_action_group_query_action (application->priv->actions,
2642 g_application_change_action_state (GActionGroup *action_group,
2643 const gchar *action_name,
2646 GApplication *application = G_APPLICATION (action_group);
2648 g_return_if_fail (application->priv->is_remote ||
2649 application->priv->actions != NULL);
2650 g_return_if_fail (application->priv->is_registered);
2652 if (application->priv->remote_actions)
2653 g_remote_action_group_change_action_state_full (application->priv->remote_actions,
2654 action_name, value, get_platform_data (application, NULL));
2657 g_action_group_change_action_state (application->priv->actions, action_name, value);
2661 g_application_activate_action (GActionGroup *action_group,
2662 const gchar *action_name,
2663 GVariant *parameter)
2665 GApplication *application = G_APPLICATION (action_group);
2667 g_return_if_fail (application->priv->is_remote ||
2668 application->priv->actions != NULL);
2669 g_return_if_fail (application->priv->is_registered);
2671 if (application->priv->remote_actions)
2672 g_remote_action_group_activate_action_full (application->priv->remote_actions,
2673 action_name, parameter, get_platform_data (application, NULL));
2676 g_action_group_activate_action (application->priv->actions, action_name, parameter);
2680 g_application_lookup_action (GActionMap *action_map,
2681 const gchar *action_name)
2683 GApplication *application = G_APPLICATION (action_map);
2685 g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
2687 return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
2691 g_application_add_action (GActionMap *action_map,
2694 GApplication *application = G_APPLICATION (action_map);
2696 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2698 g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
2702 g_application_remove_action (GActionMap *action_map,
2703 const gchar *action_name)
2705 GApplication *application = G_APPLICATION (action_map);
2707 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2709 g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
2713 g_application_action_group_iface_init (GActionGroupInterface *iface)
2715 iface->list_actions = g_application_list_actions;
2716 iface->query_action = g_application_query_action;
2717 iface->change_action_state = g_application_change_action_state;
2718 iface->activate_action = g_application_activate_action;
2722 g_application_action_map_iface_init (GActionMapInterface *iface)
2724 iface->lookup_action = g_application_lookup_action;
2725 iface->add_action = g_application_add_action;
2726 iface->remove_action = g_application_remove_action;
2729 /* Default Application {{{1 */
2731 static GApplication *default_app;
2734 * g_application_get_default:
2736 * Returns the default #GApplication instance for this process.
2738 * Normally there is only one #GApplication per process and it becomes
2739 * the default when it is created. You can exercise more control over
2740 * this by using g_application_set_default().
2742 * If there is no default application then %NULL is returned.
2744 * Returns: (transfer none): the default application for this process, or %NULL
2749 g_application_get_default (void)
2755 * g_application_set_default:
2756 * @application: (nullable): the application to set as default, or %NULL
2758 * Sets or unsets the default application for the process, as returned
2759 * by g_application_get_default().
2761 * This function does not take its own reference on @application. If
2762 * @application is destroyed then the default application will revert
2768 g_application_set_default (GApplication *application)
2770 default_app = application;
2774 * g_application_quit:
2775 * @application: a #GApplication
2777 * Immediately quits the application.
2779 * Upon return to the mainloop, g_application_run() will return,
2780 * calling only the 'shutdown' function before doing so.
2782 * The hold count is ignored.
2783 * Take care if your code has called g_application_hold() on the application and
2784 * is therefore still expecting it to exist.
2785 * (Note that you may have called g_application_hold() indirectly, for example
2786 * through gtk_application_add_window().)
2788 * The result of calling g_application_run() again after it returns is
2794 g_application_quit (GApplication *application)
2796 g_return_if_fail (G_IS_APPLICATION (application));
2798 application->priv->must_quit_now = TRUE;
2802 * g_application_mark_busy:
2803 * @application: a #GApplication
2805 * Increases the busy count of @application.
2807 * Use this function to indicate that the application is busy, for instance
2808 * while a long running operation is pending.
2810 * The busy state will be exposed to other processes, so a session shell will
2811 * use that information to indicate the state to the user (e.g. with a
2814 * To cancel the busy indication, use g_application_unmark_busy().
2819 g_application_mark_busy (GApplication *application)
2823 g_return_if_fail (G_IS_APPLICATION (application));
2825 was_busy = (application->priv->busy_count > 0);
2826 application->priv->busy_count++;
2830 g_application_impl_set_busy_state (application->priv->impl, TRUE);
2831 g_object_notify (G_OBJECT (application), "is-busy");
2836 * g_application_unmark_busy:
2837 * @application: a #GApplication
2839 * Decreases the busy count of @application.
2841 * When the busy count reaches zero, the new state will be propagated
2842 * to other processes.
2844 * This function must only be called to cancel the effect of a previous
2845 * call to g_application_mark_busy().
2850 g_application_unmark_busy (GApplication *application)
2852 g_return_if_fail (G_IS_APPLICATION (application));
2853 g_return_if_fail (application->priv->busy_count > 0);
2855 application->priv->busy_count--;
2857 if (application->priv->busy_count == 0)
2859 g_application_impl_set_busy_state (application->priv->impl, FALSE);
2860 g_object_notify (G_OBJECT (application), "is-busy");
2865 * g_application_get_is_busy:
2866 * @application: a #GApplication
2868 * Gets the application's current busy state, as set through
2869 * g_application_mark_busy() or g_application_bind_busy_property().
2871 * Returns: %TRUE if @application is currenty marked as busy
2876 g_application_get_is_busy (GApplication *application)
2878 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2880 return application->priv->busy_count > 0;
2883 /* Notifications {{{1 */
2886 * g_application_send_notification:
2887 * @application: a #GApplication
2888 * @id: (nullable): id of the notification, or %NULL
2889 * @notification: the #GNotification to send
2891 * Sends a notification on behalf of @application to the desktop shell.
2892 * There is no guarantee that the notification is displayed immediately,
2895 * Notifications may persist after the application exits. It will be
2896 * D-Bus-activated when the notification or one of its actions is
2899 * Modifying @notification after this call has no effect. However, the
2900 * object can be reused for a later call to this function.
2902 * @id may be any string that uniquely identifies the event for the
2903 * application. It does not need to be in any special format. For
2904 * example, "new-message" might be appropriate for a notification about
2907 * If a previous notification was sent with the same @id, it will be
2908 * replaced with @notification and shown again as if it was a new
2909 * notification. This works even for notifications sent from a previous
2910 * execution of the application, as long as @id is the same string.
2912 * @id may be %NULL, but it is impossible to replace or withdraw
2913 * notifications without an id.
2915 * If @notification is no longer relevant, it can be withdrawn with
2916 * g_application_withdraw_notification().
2921 g_application_send_notification (GApplication *application,
2923 GNotification *notification)
2925 gchar *generated_id = NULL;
2927 g_return_if_fail (G_IS_APPLICATION (application));
2928 g_return_if_fail (G_IS_NOTIFICATION (notification));
2929 g_return_if_fail (g_application_get_is_registered (application));
2930 g_return_if_fail (!g_application_get_is_remote (application));
2932 if (application->priv->notifications == NULL)
2933 application->priv->notifications = g_notification_backend_new_default (application);
2937 generated_id = g_dbus_generate_guid ();
2941 g_notification_backend_send_notification (application->priv->notifications, id, notification);
2943 g_free (generated_id);
2947 * g_application_withdraw_notification:
2948 * @application: a #GApplication
2949 * @id: id of a previously sent notification
2951 * Withdraws a notification that was sent with
2952 * g_application_send_notification().
2954 * This call does nothing if a notification with @id doesn't exist or
2955 * the notification was never sent.
2957 * This function works even for notifications sent in previous
2958 * executions of this application, as long @id is the same as it was for
2959 * the sent notification.
2961 * Note that notifications are dismissed when the user clicks on one
2962 * of the buttons in a notification or triggers its default action, so
2963 * there is no need to explicitly withdraw the notification in that case.
2968 g_application_withdraw_notification (GApplication *application,
2971 g_return_if_fail (G_IS_APPLICATION (application));
2972 g_return_if_fail (id != NULL);
2974 if (application->priv->notifications == NULL)
2975 application->priv->notifications = g_notification_backend_new_default (application);
2977 g_notification_backend_withdraw_notification (application->priv->notifications, id);
2980 /* Busy binding {{{1 */
2986 } GApplicationBusyBinding;
2989 g_application_busy_binding_destroy (gpointer data,
2992 GApplicationBusyBinding *binding = data;
2994 if (binding->is_busy)
2995 g_application_unmark_busy (binding->app);
2997 g_object_unref (binding->app);
2998 g_slice_free (GApplicationBusyBinding, binding);
3002 g_application_notify_busy_binding (GObject *object,
3006 GApplicationBusyBinding *binding = user_data;
3009 g_object_get (object, pspec->name, &is_busy, NULL);
3011 if (is_busy && !binding->is_busy)
3012 g_application_mark_busy (binding->app);
3013 else if (!is_busy && binding->is_busy)
3014 g_application_unmark_busy (binding->app);
3016 binding->is_busy = is_busy;
3020 * g_application_bind_busy_property:
3021 * @application: a #GApplication
3022 * @object: (type GObject.Object): a #GObject
3023 * @property: the name of a boolean property of @object
3025 * Marks @application as busy (see g_application_mark_busy()) while
3026 * @property on @object is %TRUE.
3028 * The binding holds a reference to @application while it is active, but
3029 * not to @object. Instead, the binding is destroyed when @object is
3035 g_application_bind_busy_property (GApplication *application,
3037 const gchar *property)
3040 GQuark property_quark;
3042 GApplicationBusyBinding *binding;
3045 g_return_if_fail (G_IS_APPLICATION (application));
3046 g_return_if_fail (G_IS_OBJECT (object));
3047 g_return_if_fail (property != NULL);
3049 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
3050 property_quark = g_quark_from_string (property);
3051 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
3053 g_return_if_fail (pspec != NULL && pspec->value_type == G_TYPE_BOOLEAN);
3055 if (g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
3056 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL) > 0)
3058 g_critical ("%s: '%s' is already bound to the busy state of the application", G_STRFUNC, property);
3062 binding = g_slice_new (GApplicationBusyBinding);
3063 binding->app = g_object_ref (application);
3064 binding->is_busy = FALSE;
3066 closure = g_cclosure_new (G_CALLBACK (g_application_notify_busy_binding), binding,
3067 g_application_busy_binding_destroy);
3068 g_signal_connect_closure_by_id (object, notify_id, property_quark, closure, FALSE);
3070 /* fetch the initial value */
3071 g_application_notify_busy_binding (object, pspec, binding);
3075 * g_application_unbind_busy_property:
3076 * @application: a #GApplication
3077 * @object: (type GObject.Object): a #GObject
3078 * @property: the name of a boolean property of @object
3080 * Destroys a binding between @property and the busy state of
3081 * @application that was previously created with
3082 * g_application_bind_busy_property().
3087 g_application_unbind_busy_property (GApplication *application,
3089 const gchar *property)
3092 GQuark property_quark;
3095 g_return_if_fail (G_IS_APPLICATION (application));
3096 g_return_if_fail (G_IS_OBJECT (object));
3097 g_return_if_fail (property != NULL);
3099 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
3100 property_quark = g_quark_from_string (property);
3102 handler_id = g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
3103 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL);
3104 if (handler_id == 0)
3106 g_critical ("%s: '%s' is not bound to the busy state of the application", G_STRFUNC, property);
3110 g_signal_handler_disconnect (object, handler_id);
3114 /* vim:set foldmethod=marker: */