Update German translation
[glib.git] / gio / gapplication.c
blobe068aef06ab03a95f02cc34d0f73fcff91d87737
1 /*
2 * Copyright © 2010 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
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>
20 /* Prologue {{{1 */
21 #include "config.h"
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 "gmenumodel.h"
32 #include "gsettings.h"
33 #include "gnotification-private.h"
34 #include "gnotificationbackend.h"
35 #include "gdbusutils.h"
37 #include "gioenumtypes.h"
38 #include "gioenums.h"
39 #include "gfile.h"
41 #include "glibintl.h"
43 #include <string.h>
45 /**
46 * SECTION:gapplication
47 * @title: GApplication
48 * @short_description: Core application class
49 * @include: gio/gio.h
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
87 * examples below.
89 * If used, the expected form of an application identifier is very close
90 * to that of of a
91 * [D-Bus bus name](http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface).
92 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
93 * For details on valid application identifiers, see g_application_id_is_valid().
95 * On Linux, the application identifier is claimed as a well-known bus name
96 * on the user's session bus. This means that the uniqueness of your
97 * application is scoped to the current session. It also means that your
98 * application may provide additional services (through registration of other
99 * object paths) at that bus name. The registration of these object paths
100 * should be done with the shared GDBus session bus. Note that due to the
101 * internal architecture of GDBus, method calls can be dispatched at any time
102 * (even if a main loop is not running). For this reason, you must ensure that
103 * any object paths that you wish to register are registered before #GApplication
104 * attempts to acquire the bus name of your application (which happens in
105 * g_application_register()). Unfortunately, this means that you cannot use
106 * g_application_get_is_remote() to decide if you want to register object paths.
108 * GApplication also implements the #GActionGroup and #GActionMap
109 * interfaces and lets you easily export actions by adding them with
110 * g_action_map_add_action(). When invoking an action by calling
111 * g_action_group_activate_action() on the application, it is always
112 * invoked in the primary instance. The actions are also exported on
113 * the session bus, and GIO provides the #GDBusActionGroup wrapper to
114 * conveniently access them remotely. GIO provides a #GDBusMenuModel wrapper
115 * for remote access to exported #GMenuModels.
117 * There is a number of different entry points into a GApplication:
119 * - via 'Activate' (i.e. just starting the application)
121 * - via 'Open' (i.e. opening some files)
123 * - by handling a command-line
125 * - via activating an action
127 * The #GApplication::startup signal lets you handle the application
128 * initialization for all of these in a single place.
130 * Regardless of which of these entry points is used to start the
131 * application, GApplication passes some "platform data from the
132 * launching instance to the primary instance, in the form of a
133 * #GVariant dictionary mapping strings to variants. To use platform
134 * data, override the @before_emit or @after_emit virtual functions
135 * in your #GApplication subclass. When dealing with
136 * #GApplicationCommandLine objects, the platform data is
137 * directly available via g_application_command_line_get_cwd(),
138 * g_application_command_line_get_environ() and
139 * g_application_command_line_get_platform_data().
141 * As the name indicates, the platform data may vary depending on the
142 * operating system, but it always includes the current directory (key
143 * "cwd"), and optionally the environment (ie the set of environment
144 * variables and their values) of the calling process (key "environ").
145 * The environment is only added to the platform data if the
146 * %G_APPLICATION_SEND_ENVIRONMENT flag is set. #GApplication subclasses
147 * can add their own platform data by overriding the @add_platform_data
148 * virtual function. For instance, #GtkApplication adds startup notification
149 * data in this way.
151 * To parse commandline arguments you may handle the
152 * #GApplication::command-line signal or override the local_command_line()
153 * vfunc, to parse them in either the primary instance or the local instance,
154 * respectively.
156 * For an example of opening files with a GApplication, see
157 * [gapplication-example-open.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-open.c).
159 * For an example of using actions with GApplication, see
160 * [gapplication-example-actions.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-actions.c).
162 * For an example of using extra D-Bus hooks with GApplication, see
163 * [gapplication-example-dbushooks.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-dbushooks.c).
167 * GApplication:
169 * #GApplication is an opaque data structure and can only be accessed
170 * using the following functions.
171 * Since: 2.28
175 * GApplicationClass:
176 * @startup: invoked on the primary instance immediately after registration
177 * @shutdown: invoked only on the registered primary instance immediately
178 * after the main loop terminates
179 * @activate: invoked on the primary instance when an activation occurs
180 * @open: invoked on the primary instance when there are files to open
181 * @command_line: invoked on the primary instance when a command-line is
182 * not handled locally
183 * @local_command_line: invoked (locally). The virtual function has the chance
184 * to inspect (and possibly replace) command line arguments. See
185 * g_application_run() for more information. Also see the
186 * #GApplication::handle-local-options signal, which is a simpler
187 * alternative to handling some commandline options locally
188 * @before_emit: invoked on the primary instance before 'activate', 'open',
189 * 'command-line' or any action invocation, gets the 'platform data' from
190 * the calling instance
191 * @after_emit: invoked on the primary instance after 'activate', 'open',
192 * 'command-line' or any action invocation, gets the 'platform data' from
193 * the calling instance
194 * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
195 * the primary instance when activating, opening or invoking actions
196 * @quit_mainloop: Used to be invoked on the primary instance when the use
197 * count of the application drops to zero (and after any inactivity
198 * timeout, if requested). Not used anymore since 2.32
199 * @run_mainloop: Used to be invoked on the primary instance from
200 * g_application_run() if the use-count is non-zero. Since 2.32,
201 * GApplication is iterating the main context directly and is not
202 * using @run_mainloop anymore
203 * @dbus_register: invoked locally during registration, if the application is
204 * using its D-Bus backend. You can use this to export extra objects on the
205 * bus, that need to exist before the application tries to own the bus name.
206 * The function is passed the #GDBusConnection to to session bus, and the
207 * object path that #GApplication will use to export is D-Bus API.
208 * If this function returns %TRUE, registration will proceed; otherwise
209 * registration will abort. Since: 2.34
210 * @dbus_unregister: invoked locally during unregistration, if the application
211 * is using its D-Bus backend. Use this to undo anything done by the
212 * @dbus_register vfunc. Since: 2.34
213 * @handle_local_options: invoked locally after the parsing of the commandline
214 * options has occurred. Since: 2.40
216 * Virtual function table for #GApplication.
218 * Since: 2.28
221 struct _GApplicationPrivate
223 GApplicationFlags flags;
224 gchar *id;
225 gchar *resource_path;
227 GActionGroup *actions;
228 GMenuModel *app_menu;
229 GMenuModel *menubar;
231 guint inactivity_timeout_id;
232 guint inactivity_timeout;
233 guint use_count;
234 guint busy_count;
236 guint is_registered : 1;
237 guint is_remote : 1;
238 guint did_startup : 1;
239 guint did_shutdown : 1;
240 guint must_quit_now : 1;
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;
253 /* Allocated option strings, from g_application_add_main_option() */
254 GSList *option_strings;
257 enum
259 PROP_NONE,
260 PROP_APPLICATION_ID,
261 PROP_FLAGS,
262 PROP_RESOURCE_BASE_PATH,
263 PROP_IS_REGISTERED,
264 PROP_IS_REMOTE,
265 PROP_INACTIVITY_TIMEOUT,
266 PROP_ACTION_GROUP,
267 PROP_IS_BUSY
270 enum
272 SIGNAL_STARTUP,
273 SIGNAL_SHUTDOWN,
274 SIGNAL_ACTIVATE,
275 SIGNAL_OPEN,
276 SIGNAL_ACTION,
277 SIGNAL_COMMAND_LINE,
278 SIGNAL_HANDLE_LOCAL_OPTIONS,
279 NR_SIGNALS
282 static guint g_application_signals[NR_SIGNALS];
284 static void g_application_action_group_iface_init (GActionGroupInterface *);
285 static void g_application_action_map_iface_init (GActionMapInterface *);
286 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
287 G_ADD_PRIVATE (GApplication)
288 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
289 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
291 /* GApplicationExportedActions {{{1 */
293 /* We create a subclass of GSimpleActionGroup that implements
294 * GRemoteActionGroup and deals with the platform data using
295 * GApplication's before/after_emit vfuncs. This is the action group we
296 * will be exporting.
298 * We could implement GRemoteActionGroup on GApplication directly, but
299 * this would be potentially extremely confusing to have exposed as part
300 * of the public API of GApplication. We certainly don't want anyone in
301 * the same process to be calling these APIs...
303 typedef GSimpleActionGroupClass GApplicationExportedActionsClass;
304 typedef struct
306 GSimpleActionGroup parent_instance;
307 GApplication *application;
308 } GApplicationExportedActions;
310 static GType g_application_exported_actions_get_type (void);
311 static void g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface);
312 G_DEFINE_TYPE_WITH_CODE (GApplicationExportedActions, g_application_exported_actions, G_TYPE_SIMPLE_ACTION_GROUP,
313 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_application_exported_actions_iface_init))
315 static void
316 g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
317 const gchar *action_name,
318 GVariant *parameter,
319 GVariant *platform_data)
321 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
323 G_APPLICATION_GET_CLASS (exported->application)
324 ->before_emit (exported->application, platform_data);
326 g_action_group_activate_action (G_ACTION_GROUP (exported), action_name, parameter);
328 G_APPLICATION_GET_CLASS (exported->application)
329 ->after_emit (exported->application, platform_data);
332 static void
333 g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
334 const gchar *action_name,
335 GVariant *value,
336 GVariant *platform_data)
338 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
340 G_APPLICATION_GET_CLASS (exported->application)
341 ->before_emit (exported->application, platform_data);
343 g_action_group_change_action_state (G_ACTION_GROUP (exported), action_name, value);
345 G_APPLICATION_GET_CLASS (exported->application)
346 ->after_emit (exported->application, platform_data);
349 static void
350 g_application_exported_actions_init (GApplicationExportedActions *actions)
354 static void
355 g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface)
357 iface->activate_action_full = g_application_exported_actions_activate_action_full;
358 iface->change_action_state_full = g_application_exported_actions_change_action_state_full;
361 static void
362 g_application_exported_actions_class_init (GApplicationExportedActionsClass *class)
366 static GActionGroup *
367 g_application_exported_actions_new (GApplication *application)
369 GApplicationExportedActions *actions;
371 actions = g_object_new (g_application_exported_actions_get_type (), NULL);
372 actions->application = application;
374 return G_ACTION_GROUP (actions);
377 /* Command line option handling {{{1 */
379 static void
380 free_option_entry (gpointer data)
382 GOptionEntry *entry = data;
384 switch (entry->arg)
386 case G_OPTION_ARG_STRING:
387 case G_OPTION_ARG_FILENAME:
388 g_free (*(gchar **) entry->arg_data);
389 break;
391 case G_OPTION_ARG_STRING_ARRAY:
392 case G_OPTION_ARG_FILENAME_ARRAY:
393 g_strfreev (*(gchar ***) entry->arg_data);
394 break;
396 default:
397 /* most things require no free... */
398 break;
401 /* ...except for the space that we allocated for it ourselves */
402 g_free (entry->arg_data);
404 g_slice_free (GOptionEntry, entry);
407 static void
408 g_application_pack_option_entries (GApplication *application,
409 GVariantDict *dict)
411 GHashTableIter iter;
412 gpointer item;
414 g_hash_table_iter_init (&iter, application->priv->packed_options);
415 while (g_hash_table_iter_next (&iter, NULL, &item))
417 GOptionEntry *entry = item;
418 GVariant *value = NULL;
420 switch (entry->arg)
422 case G_OPTION_ARG_NONE:
423 if (*(gboolean *) entry->arg_data != 2)
424 value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
425 break;
427 case G_OPTION_ARG_STRING:
428 if (*(gchar **) entry->arg_data)
429 value = g_variant_new_string (*(gchar **) entry->arg_data);
430 break;
432 case G_OPTION_ARG_INT:
433 if (*(gint32 *) entry->arg_data)
434 value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
435 break;
437 case G_OPTION_ARG_FILENAME:
438 if (*(gchar **) entry->arg_data)
439 value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
440 break;
442 case G_OPTION_ARG_STRING_ARRAY:
443 if (*(gchar ***) entry->arg_data)
444 value = g_variant_new_strv (*(const gchar ***) entry->arg_data, -1);
445 break;
447 case G_OPTION_ARG_FILENAME_ARRAY:
448 if (*(gchar ***) entry->arg_data)
449 value = g_variant_new_bytestring_array (*(const gchar ***) entry->arg_data, -1);
450 break;
452 case G_OPTION_ARG_DOUBLE:
453 if (*(gdouble *) entry->arg_data)
454 value = g_variant_new_double (*(gdouble *) entry->arg_data);
455 break;
457 case G_OPTION_ARG_INT64:
458 if (*(gint64 *) entry->arg_data)
459 value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
460 break;
462 default:
463 g_assert_not_reached ();
466 if (value)
467 g_variant_dict_insert_value (dict, entry->long_name, value);
471 static GVariantDict *
472 g_application_parse_command_line (GApplication *application,
473 gchar ***arguments,
474 GError **error)
476 gboolean become_service = FALSE;
477 gchar *app_id = NULL;
478 GVariantDict *dict = NULL;
479 GOptionContext *context;
480 GOptionGroup *gapplication_group;
482 /* Due to the memory management of GOptionGroup we can only parse
483 * options once. That's because once you add a group to the
484 * GOptionContext there is no way to get it back again. This is fine:
485 * local_command_line() should never get invoked more than once
486 * anyway. Add a sanity check just to be sure.
488 g_return_val_if_fail (!application->priv->options_parsed, NULL);
490 context = g_option_context_new (NULL);
492 gapplication_group = g_option_group_new ("gapplication",
493 _("GApplication options"), _("Show GApplication options"),
494 NULL, NULL);
495 g_option_group_set_translation_domain (gapplication_group, GETTEXT_PACKAGE);
496 g_option_context_add_group (context, gapplication_group);
498 /* If the application has not registered local options and it has
499 * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
500 * their primary instance commandline handler may want to deal with
501 * the arguments. We must therefore ignore them.
503 * We must also ignore --help in this case since some applications
504 * will try to handle this from the remote side. See #737869.
506 if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
508 g_option_context_set_ignore_unknown_options (context, TRUE);
509 g_option_context_set_help_enabled (context, FALSE);
512 /* Add the main option group, if it exists */
513 if (application->priv->main_options)
515 /* This consumes the main_options */
516 g_option_context_set_main_group (context, application->priv->main_options);
517 application->priv->main_options = NULL;
520 /* Add any other option groups if they exist. Adding them to the
521 * context will consume them, so we free the list as we go...
523 while (application->priv->option_groups)
525 g_option_context_add_group (context, application->priv->option_groups->data);
526 application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
527 application->priv->option_groups);
530 /* In the case that we are not explicitly marked as a service or a
531 * launcher then we want to add the "--gapplication-service" option to
532 * allow the process to be made into a service.
534 if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
536 GOptionEntry entries[] = {
537 { "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
538 N_("Enter GApplication service mode (use from D-Bus service files)") },
539 { NULL }
542 g_option_group_add_entries (gapplication_group, entries);
545 /* Allow overriding the ID if the application allows it */
546 if (application->priv->flags & G_APPLICATION_CAN_OVERRIDE_APP_ID)
548 GOptionEntry entries[] = {
549 { "gapplication-app-id", '\0', 0, G_OPTION_ARG_STRING, &app_id,
550 N_("Override the application’s ID") },
551 { NULL }
554 g_option_group_add_entries (gapplication_group, entries);
557 /* Now we parse... */
558 if (!g_option_context_parse_strv (context, arguments, error))
559 goto out;
561 /* Check for --gapplication-service */
562 if (become_service)
563 application->priv->flags |= G_APPLICATION_IS_SERVICE;
565 /* Check for --gapplication-app-id */
566 if (app_id)
567 g_application_set_application_id (application, app_id);
569 dict = g_variant_dict_new (NULL);
570 if (application->priv->packed_options)
572 g_application_pack_option_entries (application, dict);
573 g_hash_table_unref (application->priv->packed_options);
574 application->priv->packed_options = NULL;
577 out:
578 /* Make sure we don't run again */
579 application->priv->options_parsed = TRUE;
581 g_option_context_free (context);
582 g_free (app_id);
584 return dict;
587 static void
588 add_packed_option (GApplication *application,
589 GOptionEntry *entry)
591 switch (entry->arg)
593 case G_OPTION_ARG_NONE:
594 entry->arg_data = g_new (gboolean, 1);
595 *(gboolean *) entry->arg_data = 2;
596 break;
598 case G_OPTION_ARG_INT:
599 entry->arg_data = g_new0 (gint, 1);
600 break;
602 case G_OPTION_ARG_STRING:
603 case G_OPTION_ARG_FILENAME:
604 case G_OPTION_ARG_STRING_ARRAY:
605 case G_OPTION_ARG_FILENAME_ARRAY:
606 entry->arg_data = g_new0 (gpointer, 1);
607 break;
609 case G_OPTION_ARG_INT64:
610 entry->arg_data = g_new0 (gint64, 1);
611 break;
613 case G_OPTION_ARG_DOUBLE:
614 entry->arg_data = g_new0 (gdouble, 1);
615 break;
617 default:
618 g_return_if_reached ();
621 if (!application->priv->packed_options)
622 application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
624 g_hash_table_insert (application->priv->packed_options,
625 g_strdup (entry->long_name),
626 g_slice_dup (GOptionEntry, entry));
630 * g_application_add_main_option_entries:
631 * @application: a #GApplication
632 * @entries: (array zero-terminated=1) (element-type GOptionEntry) a
633 * %NULL-terminated list of #GOptionEntrys
635 * Adds main option entries to be handled by @application.
637 * This function is comparable to g_option_context_add_main_entries().
639 * After the commandline arguments are parsed, the
640 * #GApplication::handle-local-options signal will be emitted. At this
641 * point, the application can inspect the values pointed to by @arg_data
642 * in the given #GOptionEntrys.
644 * Unlike #GOptionContext, #GApplication supports giving a %NULL
645 * @arg_data for a non-callback #GOptionEntry. This results in the
646 * argument in question being packed into a #GVariantDict which is also
647 * passed to #GApplication::handle-local-options, where it can be
648 * inspected and modified. If %G_APPLICATION_HANDLES_COMMAND_LINE is
649 * set, then the resulting dictionary is sent to the primary instance,
650 * where g_application_command_line_get_options_dict() will return it.
651 * This "packing" is done according to the type of the argument --
652 * booleans for normal flags, strings for strings, bytestrings for
653 * filenames, etc. The packing only occurs if the flag is given (ie: we
654 * do not pack a "false" #GVariant in the case that a flag is missing).
656 * In general, it is recommended that all commandline arguments are
657 * parsed locally. The options dictionary should then be used to
658 * transmit the result of the parsing to the primary instance, where
659 * g_variant_dict_lookup() can be used. For local options, it is
660 * possible to either use @arg_data in the usual way, or to consult (and
661 * potentially remove) the option from the options dictionary.
663 * This function is new in GLib 2.40. Before then, the only real choice
664 * was to send all of the commandline arguments (options and all) to the
665 * primary instance for handling. #GApplication ignored them completely
666 * on the local side. Calling this function "opts in" to the new
667 * behaviour, and in particular, means that unrecognised options will be
668 * treated as errors. Unrecognised options have never been ignored when
669 * %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
671 * If #GApplication::handle-local-options needs to see the list of
672 * filenames, then the use of %G_OPTION_REMAINING is recommended. If
673 * @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
674 * the options dictionary. If you do use %G_OPTION_REMAINING then you
675 * need to handle these arguments for yourself because once they are
676 * consumed, they will no longer be visible to the default handling
677 * (which treats them as filenames to be opened).
679 * It is important to use the proper GVariant format when retrieving
680 * the options with g_variant_dict_lookup():
681 * - for %G_OPTION_ARG_NONE, use b
682 * - for %G_OPTION_ARG_STRING, use &s
683 * - for %G_OPTION_ARG_INT, use i
684 * - for %G_OPTION_ARG_INT64, use x
685 * - for %G_OPTION_ARG_DOUBLE, use d
686 * - for %G_OPTION_ARG_FILENAME, use ^ay
687 * - for %G_OPTION_ARG_STRING_ARRAY, use &as
688 * - for %G_OPTION_ARG_FILENAME_ARRAY, use ^aay
690 * Since: 2.40
692 void
693 g_application_add_main_option_entries (GApplication *application,
694 const GOptionEntry *entries)
696 gint i;
698 g_return_if_fail (G_IS_APPLICATION (application));
699 g_return_if_fail (entries != NULL);
701 if (!application->priv->main_options)
703 application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
704 g_option_group_set_translation_domain (application->priv->main_options, NULL);
707 for (i = 0; entries[i].long_name; i++)
709 GOptionEntry my_entries[2] = { { NULL }, { NULL } };
710 my_entries[0] = entries[i];
712 if (!my_entries[0].arg_data)
713 add_packed_option (application, &my_entries[0]);
715 g_option_group_add_entries (application->priv->main_options, my_entries);
720 * g_application_add_main_option:
721 * @application: the #GApplication
722 * @long_name: the long name of an option used to specify it in a commandline
723 * @short_name: the short name of an option
724 * @flags: flags from #GOptionFlags
725 * @arg: the type of the option, as a #GOptionArg
726 * @description: the description for the option in `--help` output
727 * @arg_description: (nullable): the placeholder to use for the extra argument
728 * parsed by the option in `--help` output
730 * Add an option to be handled by @application.
732 * Calling this function is the equivalent of calling
733 * g_application_add_main_option_entries() with a single #GOptionEntry
734 * that has its arg_data member set to %NULL.
736 * The parsed arguments will be packed into a #GVariantDict which
737 * is passed to #GApplication::handle-local-options. If
738 * %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
739 * be sent to the primary instance. See
740 * g_application_add_main_option_entries() for more details.
742 * See #GOptionEntry for more documentation of the arguments.
744 * Since: 2.42
746 void
747 g_application_add_main_option (GApplication *application,
748 const char *long_name,
749 char short_name,
750 GOptionFlags flags,
751 GOptionArg arg,
752 const char *description,
753 const char *arg_description)
755 gchar *dup_string;
756 GOptionEntry my_entry[2] = {
757 { NULL, short_name, flags, arg, NULL, NULL, NULL },
758 { NULL }
761 g_return_if_fail (G_IS_APPLICATION (application));
762 g_return_if_fail (long_name != NULL);
763 g_return_if_fail (description != NULL);
765 my_entry[0].long_name = dup_string = g_strdup (long_name);
766 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
768 my_entry[0].description = dup_string = g_strdup (description);
769 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
771 my_entry[0].arg_description = dup_string = g_strdup (arg_description);
772 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
774 g_application_add_main_option_entries (application, my_entry);
778 * g_application_add_option_group:
779 * @application: the #GApplication
780 * @group: (transfer full): a #GOptionGroup
782 * Adds a #GOptionGroup to the commandline handling of @application.
784 * This function is comparable to g_option_context_add_group().
786 * Unlike g_application_add_main_option_entries(), this function does
787 * not deal with %NULL @arg_data and never transmits options to the
788 * primary instance.
790 * The reason for that is because, by the time the options arrive at the
791 * primary instance, it is typically too late to do anything with them.
792 * Taking the GTK option group as an example: GTK will already have been
793 * initialised by the time the #GApplication::command-line handler runs.
794 * In the case that this is not the first-running instance of the
795 * application, the existing instance may already have been running for
796 * a very long time.
798 * This means that the options from #GOptionGroup are only really usable
799 * in the case that the instance of the application being run is the
800 * first instance. Passing options like `--display=` or `--gdk-debug=`
801 * on future runs will have no effect on the existing primary instance.
803 * Calling this function will cause the options in the supplied option
804 * group to be parsed, but it does not cause you to be "opted in" to the
805 * new functionality whereby unrecognised options are rejected even if
806 * %G_APPLICATION_HANDLES_COMMAND_LINE was given.
808 * Since: 2.40
810 void
811 g_application_add_option_group (GApplication *application,
812 GOptionGroup *group)
814 g_return_if_fail (G_IS_APPLICATION (application));
815 g_return_if_fail (group != NULL);
817 application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
820 /* vfunc defaults {{{1 */
821 static void
822 g_application_real_before_emit (GApplication *application,
823 GVariant *platform_data)
827 static void
828 g_application_real_after_emit (GApplication *application,
829 GVariant *platform_data)
833 static void
834 g_application_real_startup (GApplication *application)
836 application->priv->did_startup = TRUE;
839 static void
840 g_application_real_shutdown (GApplication *application)
842 application->priv->did_shutdown = TRUE;
845 static void
846 g_application_real_activate (GApplication *application)
848 if (!g_signal_has_handler_pending (application,
849 g_application_signals[SIGNAL_ACTIVATE],
850 0, TRUE) &&
851 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
853 static gboolean warned;
855 if (warned)
856 return;
858 g_warning ("Your application does not implement "
859 "g_application_activate() and has no handlers connected "
860 "to the 'activate' signal. It should do one of these.");
861 warned = TRUE;
865 static void
866 g_application_real_open (GApplication *application,
867 GFile **files,
868 gint n_files,
869 const gchar *hint)
871 if (!g_signal_has_handler_pending (application,
872 g_application_signals[SIGNAL_OPEN],
873 0, TRUE) &&
874 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
876 static gboolean warned;
878 if (warned)
879 return;
881 g_warning ("Your application claims to support opening files "
882 "but does not implement g_application_open() and has no "
883 "handlers connected to the 'open' signal.");
884 warned = TRUE;
888 static int
889 g_application_real_command_line (GApplication *application,
890 GApplicationCommandLine *cmdline)
892 if (!g_signal_has_handler_pending (application,
893 g_application_signals[SIGNAL_COMMAND_LINE],
894 0, TRUE) &&
895 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
897 static gboolean warned;
899 if (warned)
900 return 1;
902 g_warning ("Your application claims to support custom command line "
903 "handling but does not implement g_application_command_line() "
904 "and has no handlers connected to the 'command-line' signal.");
906 warned = TRUE;
909 return 1;
912 static gint
913 g_application_real_handle_local_options (GApplication *application,
914 GVariantDict *options)
916 return -1;
919 static GVariant *
920 get_platform_data (GApplication *application,
921 GVariant *options)
923 GVariantBuilder *builder;
924 GVariant *result;
926 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
929 gchar *cwd = g_get_current_dir ();
930 g_variant_builder_add (builder, "{sv}", "cwd",
931 g_variant_new_bytestring (cwd));
932 g_free (cwd);
935 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
937 GVariant *array;
938 gchar **envp;
940 envp = g_get_environ ();
941 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
942 g_strfreev (envp);
944 g_variant_builder_add (builder, "{sv}", "environ", array);
947 if (options)
948 g_variant_builder_add (builder, "{sv}", "options", options);
950 G_APPLICATION_GET_CLASS (application)->
951 add_platform_data (application, builder);
953 result = g_variant_builder_end (builder);
954 g_variant_builder_unref (builder);
956 return result;
959 static void
960 g_application_call_command_line (GApplication *application,
961 const gchar * const *arguments,
962 GVariant *options,
963 gint *exit_status)
965 if (application->priv->is_remote)
967 GVariant *platform_data;
969 platform_data = get_platform_data (application, options);
970 *exit_status = g_application_impl_command_line (application->priv->impl, arguments, platform_data);
972 else
974 GApplicationCommandLine *cmdline;
975 GVariant *v;
977 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
978 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
979 "arguments", v,
980 "options", options,
981 NULL);
982 g_signal_emit (application, g_application_signals[SIGNAL_COMMAND_LINE], 0, cmdline, exit_status);
983 g_object_unref (cmdline);
987 static gboolean
988 g_application_real_local_command_line (GApplication *application,
989 gchar ***arguments,
990 int *exit_status)
992 GError *error = NULL;
993 GVariantDict *options;
994 gint n_args;
996 options = g_application_parse_command_line (application, arguments, &error);
997 if (!options)
999 g_printerr ("%s\n", error->message);
1000 *exit_status = 1;
1001 return TRUE;
1004 g_signal_emit (application, g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS], 0, options, exit_status);
1006 if (*exit_status >= 0)
1008 g_variant_dict_unref (options);
1009 return TRUE;
1012 if (!g_application_register (application, NULL, &error))
1014 g_printerr ("Failed to register: %s\n", error->message);
1015 g_variant_dict_unref (options);
1016 g_error_free (error);
1017 *exit_status = 1;
1018 return TRUE;
1021 n_args = g_strv_length (*arguments);
1023 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
1025 if ((*exit_status = n_args > 1))
1027 g_printerr ("GApplication service mode takes no arguments.\n");
1028 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
1029 *exit_status = 1;
1031 else
1032 *exit_status = 0;
1034 else if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
1036 g_application_call_command_line (application,
1037 (const gchar **) *arguments,
1038 g_variant_dict_end (options),
1039 exit_status);
1041 else
1043 if (n_args <= 1)
1045 g_application_activate (application);
1046 *exit_status = 0;
1049 else
1051 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
1053 g_critical ("This application can not open files.");
1054 *exit_status = 1;
1056 else
1058 GFile **files;
1059 gint n_files;
1060 gint i;
1062 n_files = n_args - 1;
1063 files = g_new (GFile *, n_files);
1065 for (i = 0; i < n_files; i++)
1066 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
1068 g_application_open (application, files, n_files, "");
1070 for (i = 0; i < n_files; i++)
1071 g_object_unref (files[i]);
1072 g_free (files);
1074 *exit_status = 0;
1079 g_variant_dict_unref (options);
1081 return TRUE;
1084 static void
1085 g_application_real_add_platform_data (GApplication *application,
1086 GVariantBuilder *builder)
1090 static gboolean
1091 g_application_real_dbus_register (GApplication *application,
1092 GDBusConnection *connection,
1093 const gchar *object_path,
1094 GError **error)
1096 return TRUE;
1099 static void
1100 g_application_real_dbus_unregister (GApplication *application,
1101 GDBusConnection *connection,
1102 const gchar *object_path)
1106 /* GObject implementation stuff {{{1 */
1107 static void
1108 g_application_set_property (GObject *object,
1109 guint prop_id,
1110 const GValue *value,
1111 GParamSpec *pspec)
1113 GApplication *application = G_APPLICATION (object);
1115 switch (prop_id)
1117 case PROP_APPLICATION_ID:
1118 g_application_set_application_id (application,
1119 g_value_get_string (value));
1120 break;
1122 case PROP_FLAGS:
1123 g_application_set_flags (application, g_value_get_flags (value));
1124 break;
1126 case PROP_RESOURCE_BASE_PATH:
1127 g_application_set_resource_base_path (application, g_value_get_string (value));
1128 break;
1130 case PROP_INACTIVITY_TIMEOUT:
1131 g_application_set_inactivity_timeout (application,
1132 g_value_get_uint (value));
1133 break;
1135 case PROP_ACTION_GROUP:
1136 g_clear_object (&application->priv->actions);
1137 application->priv->actions = g_value_dup_object (value);
1138 break;
1140 default:
1141 g_assert_not_reached ();
1146 * g_application_set_action_group:
1147 * @application: a #GApplication
1148 * @action_group: (nullable): a #GActionGroup, or %NULL
1150 * This used to be how actions were associated with a #GApplication.
1151 * Now there is #GActionMap for that.
1153 * Since: 2.28
1155 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
1156 * mix use of this API with use of #GActionMap on the same @application
1157 * or things will go very badly wrong. This function is known to
1158 * introduce buggy behaviour (ie: signals not emitted on changes to the
1159 * action group), so you should really use #GActionMap instead.
1161 void
1162 g_application_set_action_group (GApplication *application,
1163 GActionGroup *action_group)
1165 g_return_if_fail (G_IS_APPLICATION (application));
1166 g_return_if_fail (!application->priv->is_registered);
1168 if (application->priv->actions != NULL)
1169 g_object_unref (application->priv->actions);
1171 application->priv->actions = action_group;
1173 if (application->priv->actions != NULL)
1174 g_object_ref (application->priv->actions);
1177 static void
1178 g_application_get_property (GObject *object,
1179 guint prop_id,
1180 GValue *value,
1181 GParamSpec *pspec)
1183 GApplication *application = G_APPLICATION (object);
1185 switch (prop_id)
1187 case PROP_APPLICATION_ID:
1188 g_value_set_string (value,
1189 g_application_get_application_id (application));
1190 break;
1192 case PROP_FLAGS:
1193 g_value_set_flags (value,
1194 g_application_get_flags (application));
1195 break;
1197 case PROP_RESOURCE_BASE_PATH:
1198 g_value_set_string (value, g_application_get_resource_base_path (application));
1199 break;
1201 case PROP_IS_REGISTERED:
1202 g_value_set_boolean (value,
1203 g_application_get_is_registered (application));
1204 break;
1206 case PROP_IS_REMOTE:
1207 g_value_set_boolean (value,
1208 g_application_get_is_remote (application));
1209 break;
1211 case PROP_INACTIVITY_TIMEOUT:
1212 g_value_set_uint (value,
1213 g_application_get_inactivity_timeout (application));
1214 break;
1216 case PROP_IS_BUSY:
1217 g_value_set_boolean (value, g_application_get_is_busy (application));
1218 break;
1220 default:
1221 g_assert_not_reached ();
1225 static void
1226 g_application_constructed (GObject *object)
1228 GApplication *application = G_APPLICATION (object);
1230 if (g_application_get_default () == NULL)
1231 g_application_set_default (application);
1233 /* People should not set properties from _init... */
1234 g_assert (application->priv->resource_path == NULL);
1236 if (application->priv->id != NULL)
1238 gint i;
1240 application->priv->resource_path = g_strconcat ("/", application->priv->id, NULL);
1242 for (i = 1; application->priv->resource_path[i]; i++)
1243 if (application->priv->resource_path[i] == '.')
1244 application->priv->resource_path[i] = '/';
1248 static void
1249 g_application_finalize (GObject *object)
1251 GApplication *application = G_APPLICATION (object);
1253 g_slist_free_full (application->priv->option_groups, (GDestroyNotify) g_option_group_unref);
1254 if (application->priv->main_options)
1255 g_option_group_unref (application->priv->main_options);
1256 if (application->priv->packed_options)
1257 g_hash_table_unref (application->priv->packed_options);
1259 g_slist_free_full (application->priv->option_strings, g_free);
1261 if (application->priv->impl)
1262 g_application_impl_destroy (application->priv->impl);
1263 g_free (application->priv->id);
1265 if (g_application_get_default () == application)
1266 g_application_set_default (NULL);
1268 if (application->priv->actions)
1269 g_object_unref (application->priv->actions);
1271 if (application->priv->notifications)
1272 g_object_unref (application->priv->notifications);
1274 g_free (application->priv->resource_path);
1276 G_OBJECT_CLASS (g_application_parent_class)
1277 ->finalize (object);
1280 static void
1281 g_application_init (GApplication *application)
1283 application->priv = g_application_get_instance_private (application);
1285 application->priv->actions = g_application_exported_actions_new (application);
1287 /* application->priv->actions is the one and only ref on the group, so when
1288 * we dispose, the action group will die, disconnecting all signals.
1290 g_signal_connect_swapped (application->priv->actions, "action-added",
1291 G_CALLBACK (g_action_group_action_added), application);
1292 g_signal_connect_swapped (application->priv->actions, "action-enabled-changed",
1293 G_CALLBACK (g_action_group_action_enabled_changed), application);
1294 g_signal_connect_swapped (application->priv->actions, "action-state-changed",
1295 G_CALLBACK (g_action_group_action_state_changed), application);
1296 g_signal_connect_swapped (application->priv->actions, "action-removed",
1297 G_CALLBACK (g_action_group_action_removed), application);
1300 static gboolean
1301 g_application_handle_local_options_accumulator (GSignalInvocationHint *ihint,
1302 GValue *return_accu,
1303 const GValue *handler_return,
1304 gpointer dummy)
1306 gint value;
1308 value = g_value_get_int (handler_return);
1309 g_value_set_int (return_accu, value);
1311 return value < 0;
1314 static void
1315 g_application_class_init (GApplicationClass *class)
1317 GObjectClass *object_class = G_OBJECT_CLASS (class);
1319 object_class->constructed = g_application_constructed;
1320 object_class->finalize = g_application_finalize;
1321 object_class->get_property = g_application_get_property;
1322 object_class->set_property = g_application_set_property;
1324 class->before_emit = g_application_real_before_emit;
1325 class->after_emit = g_application_real_after_emit;
1326 class->startup = g_application_real_startup;
1327 class->shutdown = g_application_real_shutdown;
1328 class->activate = g_application_real_activate;
1329 class->open = g_application_real_open;
1330 class->command_line = g_application_real_command_line;
1331 class->local_command_line = g_application_real_local_command_line;
1332 class->handle_local_options = g_application_real_handle_local_options;
1333 class->add_platform_data = g_application_real_add_platform_data;
1334 class->dbus_register = g_application_real_dbus_register;
1335 class->dbus_unregister = g_application_real_dbus_unregister;
1337 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
1338 g_param_spec_string ("application-id",
1339 P_("Application identifier"),
1340 P_("The unique identifier for the application"),
1341 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
1342 G_PARAM_STATIC_STRINGS));
1344 g_object_class_install_property (object_class, PROP_FLAGS,
1345 g_param_spec_flags ("flags",
1346 P_("Application flags"),
1347 P_("Flags specifying the behaviour of the application"),
1348 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
1349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1351 g_object_class_install_property (object_class, PROP_RESOURCE_BASE_PATH,
1352 g_param_spec_string ("resource-base-path",
1353 P_("Resource base path"),
1354 P_("The base resource path for the application"),
1355 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1357 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
1358 g_param_spec_boolean ("is-registered",
1359 P_("Is registered"),
1360 P_("If g_application_register() has been called"),
1361 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1363 g_object_class_install_property (object_class, PROP_IS_REMOTE,
1364 g_param_spec_boolean ("is-remote",
1365 P_("Is remote"),
1366 P_("If this application instance is remote"),
1367 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1369 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
1370 g_param_spec_uint ("inactivity-timeout",
1371 P_("Inactivity timeout"),
1372 P_("Time (ms) to stay alive after becoming idle"),
1373 0, G_MAXUINT, 0,
1374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1376 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
1377 g_param_spec_object ("action-group",
1378 P_("Action group"),
1379 P_("The group of actions that the application exports"),
1380 G_TYPE_ACTION_GROUP,
1381 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1384 * GApplication:is-busy:
1386 * Whether the application is currently marked as busy through
1387 * g_application_mark_busy() or g_application_bind_busy_property().
1389 * Since: 2.44
1391 g_object_class_install_property (object_class, PROP_IS_BUSY,
1392 g_param_spec_boolean ("is-busy",
1393 P_("Is busy"),
1394 P_("If this application is currently marked busy"),
1395 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1398 * GApplication::startup:
1399 * @application: the application
1401 * The ::startup signal is emitted on the primary instance immediately
1402 * after registration. See g_application_register().
1404 g_application_signals[SIGNAL_STARTUP] =
1405 g_signal_new (I_("startup"), G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
1406 G_STRUCT_OFFSET (GApplicationClass, startup),
1407 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1410 * GApplication::shutdown:
1411 * @application: the application
1413 * The ::shutdown signal is emitted only on the registered primary instance
1414 * immediately after the main loop terminates.
1416 g_application_signals[SIGNAL_SHUTDOWN] =
1417 g_signal_new (I_("shutdown"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1418 G_STRUCT_OFFSET (GApplicationClass, shutdown),
1419 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1422 * GApplication::activate:
1423 * @application: the application
1425 * The ::activate signal is emitted on the primary instance when an
1426 * activation occurs. See g_application_activate().
1428 g_application_signals[SIGNAL_ACTIVATE] =
1429 g_signal_new (I_("activate"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1430 G_STRUCT_OFFSET (GApplicationClass, activate),
1431 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1435 * GApplication::open:
1436 * @application: the application
1437 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
1438 * @n_files: the length of @files
1439 * @hint: a hint provided by the calling instance
1441 * The ::open signal is emitted on the primary instance when there are
1442 * files to open. See g_application_open() for more information.
1444 g_application_signals[SIGNAL_OPEN] =
1445 g_signal_new (I_("open"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1446 G_STRUCT_OFFSET (GApplicationClass, open),
1447 NULL, NULL, NULL,
1448 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
1451 * GApplication::command-line:
1452 * @application: the application
1453 * @command_line: a #GApplicationCommandLine representing the
1454 * passed commandline
1456 * The ::command-line signal is emitted on the primary instance when
1457 * a commandline is not handled locally. See g_application_run() and
1458 * the #GApplicationCommandLine documentation for more information.
1460 * Returns: An integer that is set as the exit status for the calling
1461 * process. See g_application_command_line_set_exit_status().
1463 g_application_signals[SIGNAL_COMMAND_LINE] =
1464 g_signal_new (I_("command-line"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1465 G_STRUCT_OFFSET (GApplicationClass, command_line),
1466 g_signal_accumulator_first_wins, NULL,
1467 NULL,
1468 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
1471 * GApplication::handle-local-options:
1472 * @application: the application
1473 * @options: the options dictionary
1475 * The ::handle-local-options signal is emitted on the local instance
1476 * after the parsing of the commandline options has occurred.
1478 * You can add options to be recognised during commandline option
1479 * parsing using g_application_add_main_option_entries() and
1480 * g_application_add_option_group().
1482 * Signal handlers can inspect @options (along with values pointed to
1483 * from the @arg_data of an installed #GOptionEntrys) in order to
1484 * decide to perform certain actions, including direct local handling
1485 * (which may be useful for options like --version).
1487 * In the event that the application is marked
1488 * %G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will
1489 * send the @options dictionary to the primary instance where it can be
1490 * read with g_application_command_line_get_options_dict(). The signal
1491 * handler can modify the dictionary before returning, and the
1492 * modified dictionary will be sent.
1494 * In the event that %G_APPLICATION_HANDLES_COMMAND_LINE is not set,
1495 * "normal processing" will treat the remaining uncollected command
1496 * line arguments as filenames or URIs. If there are no arguments,
1497 * the application is activated by g_application_activate(). One or
1498 * more arguments results in a call to g_application_open().
1500 * If you want to handle the local commandline arguments for yourself
1501 * by converting them to calls to g_application_open() or
1502 * g_action_group_activate_action() then you must be sure to register
1503 * the application first. You should probably not call
1504 * g_application_activate() for yourself, however: just return -1 and
1505 * allow the default handler to do it for you. This will ensure that
1506 * the `--gapplication-service` switch works properly (i.e. no activation
1507 * in that case).
1509 * Note that this signal is emitted from the default implementation of
1510 * local_command_line(). If you override that function and don't
1511 * chain up then this signal will never be emitted.
1513 * You can override local_command_line() if you need more powerful
1514 * capabilities than what is provided here, but this should not
1515 * normally be required.
1517 * Returns: an exit code. If you have handled your options and want
1518 * to exit the process, return a non-negative option, 0 for success,
1519 * and a positive value for failure. To continue, return -1 to let
1520 * the default option processing continue.
1522 * Since: 2.40
1524 g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS] =
1525 g_signal_new (I_("handle-local-options"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1526 G_STRUCT_OFFSET (GApplicationClass, handle_local_options),
1527 g_application_handle_local_options_accumulator, NULL, NULL,
1528 G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
1532 /* Application ID validity {{{1 */
1535 * g_application_id_is_valid:
1536 * @application_id: a potential application identifier
1538 * Checks if @application_id is a valid application identifier.
1540 * A valid ID is required for calls to g_application_new() and
1541 * g_application_set_application_id().
1543 * For convenience, the restrictions on application identifiers are
1544 * reproduced here:
1546 * - Application identifiers must contain only the ASCII characters
1547 * "[A-Z][a-z][0-9]_-." and must not begin with a digit.
1549 * - Application identifiers must contain at least one '.' (period)
1550 * character (and thus at least three elements).
1552 * - Application identifiers must not begin or end with a '.' (period)
1553 * character.
1555 * - Application identifiers must not contain consecutive '.' (period)
1556 * characters.
1558 * - Application identifiers must not exceed 255 characters.
1560 * Returns: %TRUE if @application_id is valid
1562 gboolean
1563 g_application_id_is_valid (const gchar *application_id)
1565 gsize len;
1566 gboolean allow_dot;
1567 gboolean has_dot;
1569 len = strlen (application_id);
1571 if (len > 255)
1572 return FALSE;
1574 if (!g_ascii_isalpha (application_id[0]))
1575 return FALSE;
1577 if (application_id[len-1] == '.')
1578 return FALSE;
1580 application_id++;
1581 allow_dot = TRUE;
1582 has_dot = FALSE;
1583 for (; *application_id; application_id++)
1585 if (g_ascii_isalnum (*application_id) ||
1586 (*application_id == '-') ||
1587 (*application_id == '_'))
1589 allow_dot = TRUE;
1591 else if (allow_dot && *application_id == '.')
1593 has_dot = TRUE;
1594 allow_dot = FALSE;
1596 else
1597 return FALSE;
1600 if (!has_dot)
1601 return FALSE;
1603 return TRUE;
1606 /* Public Constructor {{{1 */
1608 * g_application_new:
1609 * @application_id: (nullable): the application id
1610 * @flags: the application flags
1612 * Creates a new #GApplication instance.
1614 * If non-%NULL, the application id must be valid. See
1615 * g_application_id_is_valid().
1617 * If no application ID is given then some features of #GApplication
1618 * (most notably application uniqueness) will be disabled.
1620 * Returns: a new #GApplication instance
1622 GApplication *
1623 g_application_new (const gchar *application_id,
1624 GApplicationFlags flags)
1626 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
1628 return g_object_new (G_TYPE_APPLICATION,
1629 "application-id", application_id,
1630 "flags", flags,
1631 NULL);
1634 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
1636 * g_application_get_application_id:
1637 * @application: a #GApplication
1639 * Gets the unique identifier for @application.
1641 * Returns: the identifier for @application, owned by @application
1643 * Since: 2.28
1645 const gchar *
1646 g_application_get_application_id (GApplication *application)
1648 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1650 return application->priv->id;
1654 * g_application_set_application_id:
1655 * @application: a #GApplication
1656 * @application_id: (nullable): the identifier for @application
1658 * Sets the unique identifier for @application.
1660 * The application id can only be modified if @application has not yet
1661 * been registered.
1663 * If non-%NULL, the application id must be valid. See
1664 * g_application_id_is_valid().
1666 * Since: 2.28
1668 void
1669 g_application_set_application_id (GApplication *application,
1670 const gchar *application_id)
1672 g_return_if_fail (G_IS_APPLICATION (application));
1674 if (g_strcmp0 (application->priv->id, application_id) != 0)
1676 g_return_if_fail (application_id == NULL || g_application_id_is_valid (application_id));
1677 g_return_if_fail (!application->priv->is_registered);
1679 g_free (application->priv->id);
1680 application->priv->id = g_strdup (application_id);
1682 g_object_notify (G_OBJECT (application), "application-id");
1687 * g_application_get_flags:
1688 * @application: a #GApplication
1690 * Gets the flags for @application.
1692 * See #GApplicationFlags.
1694 * Returns: the flags for @application
1696 * Since: 2.28
1698 GApplicationFlags
1699 g_application_get_flags (GApplication *application)
1701 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1703 return application->priv->flags;
1707 * g_application_set_flags:
1708 * @application: a #GApplication
1709 * @flags: the flags for @application
1711 * Sets the flags for @application.
1713 * The flags can only be modified if @application has not yet been
1714 * registered.
1716 * See #GApplicationFlags.
1718 * Since: 2.28
1720 void
1721 g_application_set_flags (GApplication *application,
1722 GApplicationFlags flags)
1724 g_return_if_fail (G_IS_APPLICATION (application));
1726 if (application->priv->flags != flags)
1728 g_return_if_fail (!application->priv->is_registered);
1730 application->priv->flags = flags;
1732 g_object_notify (G_OBJECT (application), "flags");
1737 * g_application_get_resource_base_path:
1738 * @application: a #GApplication
1740 * Gets the resource base path of @application.
1742 * See g_application_set_resource_base_path() for more information.
1744 * Returns: (nullable): the base resource path, if one is set
1746 * Since: 2.42
1748 const gchar *
1749 g_application_get_resource_base_path (GApplication *application)
1751 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1753 return application->priv->resource_path;
1757 * g_application_set_resource_base_path:
1758 * @application: a #GApplication
1759 * @resource_path: (nullable): the resource path to use
1761 * Sets (or unsets) the base resource path of @application.
1763 * The path is used to automatically load various [application
1764 * resources][gresource] such as menu layouts and action descriptions.
1765 * The various types of resources will be found at fixed names relative
1766 * to the given base path.
1768 * By default, the resource base path is determined from the application
1769 * ID by prefixing '/' and replacing each '.' with '/'. This is done at
1770 * the time that the #GApplication object is constructed. Changes to
1771 * the application ID after that point will not have an impact on the
1772 * resource base path.
1774 * As an example, if the application has an ID of "org.example.app" then
1775 * the default resource base path will be "/org/example/app". If this
1776 * is a #GtkApplication (and you have not manually changed the path)
1777 * then Gtk will then search for the menus of the application at
1778 * "/org/example/app/gtk/menus.ui".
1780 * See #GResource for more information about adding resources to your
1781 * application.
1783 * You can disable automatic resource loading functionality by setting
1784 * the path to %NULL.
1786 * Changing the resource base path once the application is running is
1787 * not recommended. The point at which the resource path is consulted
1788 * for forming paths for various purposes is unspecified. When writing
1789 * a sub-class of #GApplication you should either set the
1790 * #GApplication:resource-base-path property at construction time, or call
1791 * this function during the instance initialization. Alternatively, you
1792 * can call this function in the #GApplicationClass.startup virtual function,
1793 * before chaining up to the parent implementation.
1795 * Since: 2.42
1797 void
1798 g_application_set_resource_base_path (GApplication *application,
1799 const gchar *resource_path)
1801 g_return_if_fail (G_IS_APPLICATION (application));
1802 g_return_if_fail (resource_path == NULL || g_str_has_prefix (resource_path, "/"));
1804 if (g_strcmp0 (application->priv->resource_path, resource_path) != 0)
1806 g_free (application->priv->resource_path);
1808 application->priv->resource_path = g_strdup (resource_path);
1810 g_object_notify (G_OBJECT (application), "resource-base-path");
1815 * g_application_get_inactivity_timeout:
1816 * @application: a #GApplication
1818 * Gets the current inactivity timeout for the application.
1820 * This is the amount of time (in milliseconds) after the last call to
1821 * g_application_release() before the application stops running.
1823 * Returns: the timeout, in milliseconds
1825 * Since: 2.28
1827 guint
1828 g_application_get_inactivity_timeout (GApplication *application)
1830 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1832 return application->priv->inactivity_timeout;
1836 * g_application_set_inactivity_timeout:
1837 * @application: a #GApplication
1838 * @inactivity_timeout: the timeout, in milliseconds
1840 * Sets the current inactivity timeout for the application.
1842 * This is the amount of time (in milliseconds) after the last call to
1843 * g_application_release() before the application stops running.
1845 * This call has no side effects of its own. The value set here is only
1846 * used for next time g_application_release() drops the use count to
1847 * zero. Any timeouts currently in progress are not impacted.
1849 * Since: 2.28
1851 void
1852 g_application_set_inactivity_timeout (GApplication *application,
1853 guint inactivity_timeout)
1855 g_return_if_fail (G_IS_APPLICATION (application));
1857 if (application->priv->inactivity_timeout != inactivity_timeout)
1859 application->priv->inactivity_timeout = inactivity_timeout;
1861 g_object_notify (G_OBJECT (application), "inactivity-timeout");
1864 /* Read-only property getters (is registered, is remote, dbus stuff) {{{1 */
1866 * g_application_get_is_registered:
1867 * @application: a #GApplication
1869 * Checks if @application is registered.
1871 * An application is registered if g_application_register() has been
1872 * successfully called.
1874 * Returns: %TRUE if @application is registered
1876 * Since: 2.28
1878 gboolean
1879 g_application_get_is_registered (GApplication *application)
1881 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1883 return application->priv->is_registered;
1887 * g_application_get_is_remote:
1888 * @application: a #GApplication
1890 * Checks if @application is remote.
1892 * If @application is remote then it means that another instance of
1893 * application already exists (the 'primary' instance). Calls to
1894 * perform actions on @application will result in the actions being
1895 * performed by the primary instance.
1897 * The value of this property cannot be accessed before
1898 * g_application_register() has been called. See
1899 * g_application_get_is_registered().
1901 * Returns: %TRUE if @application is remote
1903 * Since: 2.28
1905 gboolean
1906 g_application_get_is_remote (GApplication *application)
1908 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1909 g_return_val_if_fail (application->priv->is_registered, FALSE);
1911 return application->priv->is_remote;
1915 * g_application_get_dbus_connection:
1916 * @application: a #GApplication
1918 * Gets the #GDBusConnection being used by the application, or %NULL.
1920 * If #GApplication is using its D-Bus backend then this function will
1921 * return the #GDBusConnection being used for uniqueness and
1922 * communication with the desktop environment and other instances of the
1923 * application.
1925 * If #GApplication is not using D-Bus then this function will return
1926 * %NULL. This includes the situation where the D-Bus backend would
1927 * normally be in use but we were unable to connect to the bus.
1929 * This function must not be called before the application has been
1930 * registered. See g_application_get_is_registered().
1932 * Returns: (transfer none): a #GDBusConnection, or %NULL
1934 * Since: 2.34
1936 GDBusConnection *
1937 g_application_get_dbus_connection (GApplication *application)
1939 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1940 g_return_val_if_fail (application->priv->is_registered, FALSE);
1942 return g_application_impl_get_dbus_connection (application->priv->impl);
1946 * g_application_get_dbus_object_path:
1947 * @application: a #GApplication
1949 * Gets the D-Bus object path being used by the application, or %NULL.
1951 * If #GApplication is using its D-Bus backend then this function will
1952 * return the D-Bus object path that #GApplication is using. If the
1953 * application is the primary instance then there is an object published
1954 * at this path. If the application is not the primary instance then
1955 * the result of this function is undefined.
1957 * If #GApplication is not using D-Bus then this function will return
1958 * %NULL. This includes the situation where the D-Bus backend would
1959 * normally be in use but we were unable to connect to the bus.
1961 * This function must not be called before the application has been
1962 * registered. See g_application_get_is_registered().
1964 * Returns: the object path, or %NULL
1966 * Since: 2.34
1968 const gchar *
1969 g_application_get_dbus_object_path (GApplication *application)
1971 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1972 g_return_val_if_fail (application->priv->is_registered, FALSE);
1974 return g_application_impl_get_dbus_object_path (application->priv->impl);
1978 /* Register {{{1 */
1980 * g_application_register:
1981 * @application: a #GApplication
1982 * @cancellable: (nullable): a #GCancellable, or %NULL
1983 * @error: a pointer to a NULL #GError, or %NULL
1985 * Attempts registration of the application.
1987 * This is the point at which the application discovers if it is the
1988 * primary instance or merely acting as a remote for an already-existing
1989 * primary instance. This is implemented by attempting to acquire the
1990 * application identifier as a unique bus name on the session bus using
1991 * GDBus.
1993 * If there is no application ID or if %G_APPLICATION_NON_UNIQUE was
1994 * given, then this process will always become the primary instance.
1996 * Due to the internal architecture of GDBus, method calls can be
1997 * dispatched at any time (even if a main loop is not running). For
1998 * this reason, you must ensure that any object paths that you wish to
1999 * register are registered before calling this function.
2001 * If the application has already been registered then %TRUE is
2002 * returned with no work performed.
2004 * The #GApplication::startup signal is emitted if registration succeeds
2005 * and @application is the primary instance (including the non-unique
2006 * case).
2008 * In the event of an error (such as @cancellable being cancelled, or a
2009 * failure to connect to the session bus), %FALSE is returned and @error
2010 * is set appropriately.
2012 * Note: the return value of this function is not an indicator that this
2013 * instance is or is not the primary instance of the application. See
2014 * g_application_get_is_remote() for that.
2016 * Returns: %TRUE if registration succeeded
2018 * Since: 2.28
2020 gboolean
2021 g_application_register (GApplication *application,
2022 GCancellable *cancellable,
2023 GError **error)
2025 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2027 if (!application->priv->is_registered)
2029 if (application->priv->id == NULL)
2030 application->priv->flags |= G_APPLICATION_NON_UNIQUE;
2032 application->priv->impl =
2033 g_application_impl_register (application, application->priv->id,
2034 application->priv->flags,
2035 application->priv->actions,
2036 &application->priv->remote_actions,
2037 cancellable, error);
2039 if (application->priv->impl == NULL)
2040 return FALSE;
2042 application->priv->is_remote = application->priv->remote_actions != NULL;
2043 application->priv->is_registered = TRUE;
2045 g_object_notify (G_OBJECT (application), "is-registered");
2047 if (!application->priv->is_remote)
2049 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
2051 if (!application->priv->did_startup)
2052 g_critical ("GApplication subclass '%s' failed to chain up on"
2053 " ::startup (from start of override function)",
2054 G_OBJECT_TYPE_NAME (application));
2058 return TRUE;
2061 /* Hold/release {{{1 */
2063 * g_application_hold:
2064 * @application: a #GApplication
2066 * Increases the use count of @application.
2068 * Use this function to indicate that the application has a reason to
2069 * continue to run. For example, g_application_hold() is called by GTK+
2070 * when a toplevel window is on the screen.
2072 * To cancel the hold, call g_application_release().
2074 void
2075 g_application_hold (GApplication *application)
2077 g_return_if_fail (G_IS_APPLICATION (application));
2079 if (application->priv->inactivity_timeout_id)
2081 g_source_remove (application->priv->inactivity_timeout_id);
2082 application->priv->inactivity_timeout_id = 0;
2085 application->priv->use_count++;
2088 static gboolean
2089 inactivity_timeout_expired (gpointer data)
2091 GApplication *application = G_APPLICATION (data);
2093 application->priv->inactivity_timeout_id = 0;
2095 return G_SOURCE_REMOVE;
2100 * g_application_release:
2101 * @application: a #GApplication
2103 * Decrease the use count of @application.
2105 * When the use count reaches zero, the application will stop running.
2107 * Never call this function except to cancel the effect of a previous
2108 * call to g_application_hold().
2110 void
2111 g_application_release (GApplication *application)
2113 g_return_if_fail (G_IS_APPLICATION (application));
2114 g_return_if_fail (application->priv->use_count > 0);
2116 application->priv->use_count--;
2118 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
2119 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
2120 inactivity_timeout_expired, application);
2123 /* Activate, Open {{{1 */
2125 * g_application_activate:
2126 * @application: a #GApplication
2128 * Activates the application.
2130 * In essence, this results in the #GApplication::activate signal being
2131 * emitted in the primary instance.
2133 * The application must be registered before calling this function.
2135 * Since: 2.28
2137 void
2138 g_application_activate (GApplication *application)
2140 g_return_if_fail (G_IS_APPLICATION (application));
2141 g_return_if_fail (application->priv->is_registered);
2143 if (application->priv->is_remote)
2144 g_application_impl_activate (application->priv->impl,
2145 get_platform_data (application, NULL));
2147 else
2148 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
2152 * g_application_open:
2153 * @application: a #GApplication
2154 * @files: (array length=n_files): an array of #GFiles to open
2155 * @n_files: the length of the @files array
2156 * @hint: a hint (or ""), but never %NULL
2158 * Opens the given files.
2160 * In essence, this results in the #GApplication::open signal being emitted
2161 * in the primary instance.
2163 * @n_files must be greater than zero.
2165 * @hint is simply passed through to the ::open signal. It is
2166 * intended to be used by applications that have multiple modes for
2167 * opening files (eg: "view" vs "edit", etc). Unless you have a need
2168 * for this functionality, you should use "".
2170 * The application must be registered before calling this function
2171 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
2173 * Since: 2.28
2175 void
2176 g_application_open (GApplication *application,
2177 GFile **files,
2178 gint n_files,
2179 const gchar *hint)
2181 g_return_if_fail (G_IS_APPLICATION (application));
2182 g_return_if_fail (application->priv->flags &
2183 G_APPLICATION_HANDLES_OPEN);
2184 g_return_if_fail (application->priv->is_registered);
2186 if (application->priv->is_remote)
2187 g_application_impl_open (application->priv->impl,
2188 files, n_files, hint,
2189 get_platform_data (application, NULL));
2191 else
2192 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
2193 0, files, n_files, hint);
2196 /* Run {{{1 */
2198 * g_application_run:
2199 * @application: a #GApplication
2200 * @argc: the argc from main() (or 0 if @argv is %NULL)
2201 * @argv: (array length=argc) (nullable): the argv from main(), or %NULL
2203 * Runs the application.
2205 * This function is intended to be run from main() and its return value
2206 * is intended to be returned by main(). Although you are expected to pass
2207 * the @argc, @argv parameters from main() to this function, it is possible
2208 * to pass %NULL if @argv is not available or commandline handling is not
2209 * required. Note that on Windows, @argc and @argv are ignored, and
2210 * g_win32_get_command_line() is called internally (for proper support
2211 * of Unicode commandline arguments).
2213 * #GApplication will attempt to parse the commandline arguments. You
2214 * can add commandline flags to the list of recognised options by way of
2215 * g_application_add_main_option_entries(). After this, the
2216 * #GApplication::handle-local-options signal is emitted, from which the
2217 * application can inspect the values of its #GOptionEntrys.
2219 * #GApplication::handle-local-options is a good place to handle options
2220 * such as `--version`, where an immediate reply from the local process is
2221 * desired (instead of communicating with an already-running instance).
2222 * A #GApplication::handle-local-options handler can stop further processing
2223 * by returning a non-negative value, which then becomes the exit status of
2224 * the process.
2226 * What happens next depends on the flags: if
2227 * %G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining
2228 * commandline arguments are sent to the primary instance, where a
2229 * #GApplication::command-line signal is emitted. Otherwise, the
2230 * remaining commandline arguments are assumed to be a list of files.
2231 * If there are no files listed, the application is activated via the
2232 * #GApplication::activate signal. If there are one or more files, and
2233 * %G_APPLICATION_HANDLES_OPEN was specified then the files are opened
2234 * via the #GApplication::open signal.
2236 * If you are interested in doing more complicated local handling of the
2237 * commandline then you should implement your own #GApplication subclass
2238 * and override local_command_line(). In this case, you most likely want
2239 * to return %TRUE from your local_command_line() implementation to
2240 * suppress the default handling. See
2241 * [gapplication-example-cmdline2.c][gapplication-example-cmdline2]
2242 * for an example.
2244 * If, after the above is done, the use count of the application is zero
2245 * then the exit status is returned immediately. If the use count is
2246 * non-zero then the default main context is iterated until the use count
2247 * falls to zero, at which point 0 is returned.
2249 * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
2250 * run for as much as 10 seconds with a use count of zero while waiting
2251 * for the message that caused the activation to arrive. After that,
2252 * if the use count falls to zero the application will exit immediately,
2253 * except in the case that g_application_set_inactivity_timeout() is in
2254 * use.
2256 * This function sets the prgname (g_set_prgname()), if not already set,
2257 * to the basename of argv[0].
2259 * Much like g_main_loop_run(), this function will acquire the main context
2260 * for the duration that the application is running.
2262 * Since 2.40, applications that are not explicitly flagged as services
2263 * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
2264 * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
2265 * default handler for local_command_line) if "--gapplication-service"
2266 * was given in the command line. If this flag is present then normal
2267 * commandline processing is interrupted and the
2268 * %G_APPLICATION_IS_SERVICE flag is set. This provides a "compromise"
2269 * solution whereby running an application directly from the commandline
2270 * will invoke it in the normal way (which can be useful for debugging)
2271 * while still allowing applications to be D-Bus activated in service
2272 * mode. The D-Bus service file should invoke the executable with
2273 * "--gapplication-service" as the sole commandline argument. This
2274 * approach is suitable for use by most graphical applications but
2275 * should not be used from applications like editors that need precise
2276 * control over when processes invoked via the commandline will exit and
2277 * what their exit status will be.
2279 * Returns: the exit status
2281 * Since: 2.28
2284 g_application_run (GApplication *application,
2285 int argc,
2286 char **argv)
2288 gchar **arguments;
2289 int status;
2290 GMainContext *context;
2291 gboolean acquired_context;
2293 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
2294 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
2295 g_return_val_if_fail (!application->priv->must_quit_now, 1);
2297 #ifdef G_OS_WIN32
2299 gint new_argc = 0;
2301 arguments = g_win32_get_command_line ();
2304 * CommandLineToArgvW(), which is called by g_win32_get_command_line(),
2305 * pulls in the whole command line that is used to call the program. This is
2306 * fine in cases where the program is a .exe program, but in the cases where the
2307 * program is a called via a script, such as PyGObject's gtk-demo.py, which is normally
2308 * called using 'python gtk-demo.py' on Windows, the program name (argv[0])
2309 * returned by g_win32_get_command_line() will not be the argv[0] that ->local_command_line()
2310 * would expect, causing the program to fail with "This application can not open files."
2312 new_argc = g_strv_length (arguments);
2314 if (new_argc > argc)
2316 gint i;
2318 for (i = 0; i < new_argc - argc; i++)
2319 g_free (arguments[i]);
2321 memmove (&arguments[0],
2322 &arguments[new_argc - argc],
2323 sizeof (arguments[0]) * (argc + 1));
2326 #else
2328 gint i;
2330 arguments = g_new (gchar *, argc + 1);
2331 for (i = 0; i < argc; i++)
2332 arguments[i] = g_strdup (argv[i]);
2333 arguments[i] = NULL;
2335 #endif
2337 if (g_get_prgname () == NULL && argc > 0)
2339 gchar *prgname;
2341 prgname = g_path_get_basename (argv[0]);
2342 g_set_prgname (prgname);
2343 g_free (prgname);
2346 context = g_main_context_default ();
2347 acquired_context = g_main_context_acquire (context);
2348 g_return_val_if_fail (acquired_context, 0);
2350 if (!G_APPLICATION_GET_CLASS (application)
2351 ->local_command_line (application, &arguments, &status))
2353 GError *error = NULL;
2355 if (!g_application_register (application, NULL, &error))
2357 g_printerr ("Failed to register: %s\n", error->message);
2358 g_error_free (error);
2359 return 1;
2362 g_application_call_command_line (application, (const gchar **) arguments, NULL, &status);
2365 g_strfreev (arguments);
2367 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
2368 application->priv->is_registered &&
2369 !application->priv->use_count &&
2370 !application->priv->inactivity_timeout_id)
2372 application->priv->inactivity_timeout_id =
2373 g_timeout_add (10000, inactivity_timeout_expired, application);
2376 while (application->priv->use_count || application->priv->inactivity_timeout_id)
2378 if (application->priv->must_quit_now)
2379 break;
2381 g_main_context_iteration (context, TRUE);
2382 status = 0;
2385 if (application->priv->is_registered && !application->priv->is_remote)
2387 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
2389 if (!application->priv->did_shutdown)
2390 g_critical ("GApplication subclass '%s' failed to chain up on"
2391 " ::shutdown (from end of override function)",
2392 G_OBJECT_TYPE_NAME (application));
2395 if (application->priv->impl)
2397 g_application_impl_flush (application->priv->impl);
2398 g_application_impl_destroy (application->priv->impl);
2399 application->priv->impl = NULL;
2402 g_settings_sync ();
2404 if (!application->priv->must_quit_now)
2405 while (g_main_context_iteration (context, FALSE))
2408 g_main_context_release (context);
2410 return status;
2413 static gchar **
2414 g_application_list_actions (GActionGroup *action_group)
2416 GApplication *application = G_APPLICATION (action_group);
2418 g_return_val_if_fail (application->priv->is_registered, NULL);
2420 if (application->priv->remote_actions != NULL)
2421 return g_action_group_list_actions (G_ACTION_GROUP (application->priv->remote_actions));
2423 else if (application->priv->actions != NULL)
2424 return g_action_group_list_actions (application->priv->actions);
2426 else
2427 /* empty string array */
2428 return g_new0 (gchar *, 1);
2431 static gboolean
2432 g_application_query_action (GActionGroup *group,
2433 const gchar *action_name,
2434 gboolean *enabled,
2435 const GVariantType **parameter_type,
2436 const GVariantType **state_type,
2437 GVariant **state_hint,
2438 GVariant **state)
2440 GApplication *application = G_APPLICATION (group);
2442 g_return_val_if_fail (application->priv->is_registered, FALSE);
2444 if (application->priv->remote_actions != NULL)
2445 return g_action_group_query_action (G_ACTION_GROUP (application->priv->remote_actions),
2446 action_name,
2447 enabled,
2448 parameter_type,
2449 state_type,
2450 state_hint,
2451 state);
2453 if (application->priv->actions != NULL)
2454 return g_action_group_query_action (application->priv->actions,
2455 action_name,
2456 enabled,
2457 parameter_type,
2458 state_type,
2459 state_hint,
2460 state);
2462 return FALSE;
2465 static void
2466 g_application_change_action_state (GActionGroup *action_group,
2467 const gchar *action_name,
2468 GVariant *value)
2470 GApplication *application = G_APPLICATION (action_group);
2472 g_return_if_fail (application->priv->is_remote ||
2473 application->priv->actions != NULL);
2474 g_return_if_fail (application->priv->is_registered);
2476 if (application->priv->remote_actions)
2477 g_remote_action_group_change_action_state_full (application->priv->remote_actions,
2478 action_name, value, get_platform_data (application, NULL));
2480 else
2481 g_action_group_change_action_state (application->priv->actions, action_name, value);
2484 static void
2485 g_application_activate_action (GActionGroup *action_group,
2486 const gchar *action_name,
2487 GVariant *parameter)
2489 GApplication *application = G_APPLICATION (action_group);
2491 g_return_if_fail (application->priv->is_remote ||
2492 application->priv->actions != NULL);
2493 g_return_if_fail (application->priv->is_registered);
2495 if (application->priv->remote_actions)
2496 g_remote_action_group_activate_action_full (application->priv->remote_actions,
2497 action_name, parameter, get_platform_data (application, NULL));
2499 else
2500 g_action_group_activate_action (application->priv->actions, action_name, parameter);
2503 static GAction *
2504 g_application_lookup_action (GActionMap *action_map,
2505 const gchar *action_name)
2507 GApplication *application = G_APPLICATION (action_map);
2509 g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
2511 return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
2514 static void
2515 g_application_add_action (GActionMap *action_map,
2516 GAction *action)
2518 GApplication *application = G_APPLICATION (action_map);
2520 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2522 g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
2525 static void
2526 g_application_remove_action (GActionMap *action_map,
2527 const gchar *action_name)
2529 GApplication *application = G_APPLICATION (action_map);
2531 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2533 g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
2536 static void
2537 g_application_action_group_iface_init (GActionGroupInterface *iface)
2539 iface->list_actions = g_application_list_actions;
2540 iface->query_action = g_application_query_action;
2541 iface->change_action_state = g_application_change_action_state;
2542 iface->activate_action = g_application_activate_action;
2545 static void
2546 g_application_action_map_iface_init (GActionMapInterface *iface)
2548 iface->lookup_action = g_application_lookup_action;
2549 iface->add_action = g_application_add_action;
2550 iface->remove_action = g_application_remove_action;
2553 /* Default Application {{{1 */
2555 static GApplication *default_app;
2558 * g_application_get_default:
2560 * Returns the default #GApplication instance for this process.
2562 * Normally there is only one #GApplication per process and it becomes
2563 * the default when it is created. You can exercise more control over
2564 * this by using g_application_set_default().
2566 * If there is no default application then %NULL is returned.
2568 * Returns: (transfer none): the default application for this process, or %NULL
2570 * Since: 2.32
2572 GApplication *
2573 g_application_get_default (void)
2575 return default_app;
2579 * g_application_set_default:
2580 * @application: (nullable): the application to set as default, or %NULL
2582 * Sets or unsets the default application for the process, as returned
2583 * by g_application_get_default().
2585 * This function does not take its own reference on @application. If
2586 * @application is destroyed then the default application will revert
2587 * back to %NULL.
2589 * Since: 2.32
2591 void
2592 g_application_set_default (GApplication *application)
2594 default_app = application;
2598 * g_application_quit:
2599 * @application: a #GApplication
2601 * Immediately quits the application.
2603 * Upon return to the mainloop, g_application_run() will return,
2604 * calling only the 'shutdown' function before doing so.
2606 * The hold count is ignored.
2608 * The result of calling g_application_run() again after it returns is
2609 * unspecified.
2611 * Since: 2.32
2613 void
2614 g_application_quit (GApplication *application)
2616 g_return_if_fail (G_IS_APPLICATION (application));
2618 application->priv->must_quit_now = TRUE;
2622 * g_application_mark_busy:
2623 * @application: a #GApplication
2625 * Increases the busy count of @application.
2627 * Use this function to indicate that the application is busy, for instance
2628 * while a long running operation is pending.
2630 * The busy state will be exposed to other processes, so a session shell will
2631 * use that information to indicate the state to the user (e.g. with a
2632 * spinner).
2634 * To cancel the busy indication, use g_application_unmark_busy().
2636 * Since: 2.38
2638 void
2639 g_application_mark_busy (GApplication *application)
2641 gboolean was_busy;
2643 g_return_if_fail (G_IS_APPLICATION (application));
2645 was_busy = (application->priv->busy_count > 0);
2646 application->priv->busy_count++;
2648 if (!was_busy)
2650 g_application_impl_set_busy_state (application->priv->impl, TRUE);
2651 g_object_notify (G_OBJECT (application), "is-busy");
2656 * g_application_unmark_busy:
2657 * @application: a #GApplication
2659 * Decreases the busy count of @application.
2661 * When the busy count reaches zero, the new state will be propagated
2662 * to other processes.
2664 * This function must only be called to cancel the effect of a previous
2665 * call to g_application_mark_busy().
2667 * Since: 2.38
2669 void
2670 g_application_unmark_busy (GApplication *application)
2672 g_return_if_fail (G_IS_APPLICATION (application));
2673 g_return_if_fail (application->priv->busy_count > 0);
2675 application->priv->busy_count--;
2677 if (application->priv->busy_count == 0)
2679 g_application_impl_set_busy_state (application->priv->impl, FALSE);
2680 g_object_notify (G_OBJECT (application), "is-busy");
2685 * g_application_get_is_busy:
2686 * @application: a #GApplication
2688 * Gets the application's current busy state, as set through
2689 * g_application_mark_busy() or g_application_bind_busy_property().
2691 * Returns: %TRUE if @application is currenty marked as busy
2693 * Since: 2.44
2695 gboolean
2696 g_application_get_is_busy (GApplication *application)
2698 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2700 return application->priv->busy_count > 0;
2703 /* Notifications {{{1 */
2706 * g_application_send_notification:
2707 * @application: a #GApplication
2708 * @id: (nullable): id of the notification, or %NULL
2709 * @notification: the #GNotification to send
2711 * Sends a notification on behalf of @application to the desktop shell.
2712 * There is no guarantee that the notification is displayed immediately,
2713 * or even at all.
2715 * Notifications may persist after the application exits. It will be
2716 * D-Bus-activated when the notification or one of its actions is
2717 * activated.
2719 * Modifying @notification after this call has no effect. However, the
2720 * object can be reused for a later call to this function.
2722 * @id may be any string that uniquely identifies the event for the
2723 * application. It does not need to be in any special format. For
2724 * example, "new-message" might be appropriate for a notification about
2725 * new messages.
2727 * If a previous notification was sent with the same @id, it will be
2728 * replaced with @notification and shown again as if it was a new
2729 * notification. This works even for notifications sent from a previous
2730 * execution of the application, as long as @id is the same string.
2732 * @id may be %NULL, but it is impossible to replace or withdraw
2733 * notifications without an id.
2735 * If @notification is no longer relevant, it can be withdrawn with
2736 * g_application_withdraw_notification().
2738 * Since: 2.40
2740 void
2741 g_application_send_notification (GApplication *application,
2742 const gchar *id,
2743 GNotification *notification)
2745 gchar *generated_id = NULL;
2747 g_return_if_fail (G_IS_APPLICATION (application));
2748 g_return_if_fail (G_IS_NOTIFICATION (notification));
2749 g_return_if_fail (g_application_get_is_registered (application));
2750 g_return_if_fail (!g_application_get_is_remote (application));
2752 if (application->priv->notifications == NULL)
2753 application->priv->notifications = g_notification_backend_new_default (application);
2755 if (id == NULL)
2757 generated_id = g_dbus_generate_guid ();
2758 id = generated_id;
2761 g_notification_backend_send_notification (application->priv->notifications, id, notification);
2763 g_free (generated_id);
2767 * g_application_withdraw_notification:
2768 * @application: a #GApplication
2769 * @id: id of a previously sent notification
2771 * Withdraws a notification that was sent with
2772 * g_application_send_notification().
2774 * This call does nothing if a notification with @id doesn't exist or
2775 * the notification was never sent.
2777 * This function works even for notifications sent in previous
2778 * executions of this application, as long @id is the same as it was for
2779 * the sent notification.
2781 * Note that notifications are dismissed when the user clicks on one
2782 * of the buttons in a notification or triggers its default action, so
2783 * there is no need to explicitly withdraw the notification in that case.
2785 * Since: 2.40
2787 void
2788 g_application_withdraw_notification (GApplication *application,
2789 const gchar *id)
2791 g_return_if_fail (G_IS_APPLICATION (application));
2792 g_return_if_fail (id != NULL);
2794 if (application->priv->notifications == NULL)
2795 application->priv->notifications = g_notification_backend_new_default (application);
2797 g_notification_backend_withdraw_notification (application->priv->notifications, id);
2800 /* Busy binding {{{1 */
2802 typedef struct
2804 GApplication *app;
2805 gboolean is_busy;
2806 } GApplicationBusyBinding;
2808 static void
2809 g_application_busy_binding_destroy (gpointer data,
2810 GClosure *closure)
2812 GApplicationBusyBinding *binding = data;
2814 if (binding->is_busy)
2815 g_application_unmark_busy (binding->app);
2817 g_object_unref (binding->app);
2818 g_slice_free (GApplicationBusyBinding, binding);
2821 static void
2822 g_application_notify_busy_binding (GObject *object,
2823 GParamSpec *pspec,
2824 gpointer user_data)
2826 GApplicationBusyBinding *binding = user_data;
2827 gboolean is_busy;
2829 g_object_get (object, pspec->name, &is_busy, NULL);
2831 if (is_busy && !binding->is_busy)
2832 g_application_mark_busy (binding->app);
2833 else if (!is_busy && binding->is_busy)
2834 g_application_unmark_busy (binding->app);
2836 binding->is_busy = is_busy;
2840 * g_application_bind_busy_property:
2841 * @application: a #GApplication
2842 * @object: (type GObject.Object): a #GObject
2843 * @property: the name of a boolean property of @object
2845 * Marks @application as busy (see g_application_mark_busy()) while
2846 * @property on @object is %TRUE.
2848 * The binding holds a reference to @application while it is active, but
2849 * not to @object. Instead, the binding is destroyed when @object is
2850 * finalized.
2852 * Since: 2.44
2854 void
2855 g_application_bind_busy_property (GApplication *application,
2856 gpointer object,
2857 const gchar *property)
2859 guint notify_id;
2860 GQuark property_quark;
2861 GParamSpec *pspec;
2862 GApplicationBusyBinding *binding;
2863 GClosure *closure;
2865 g_return_if_fail (G_IS_APPLICATION (application));
2866 g_return_if_fail (G_IS_OBJECT (object));
2867 g_return_if_fail (property != NULL);
2869 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
2870 property_quark = g_quark_from_string (property);
2871 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2873 g_return_if_fail (pspec != NULL && pspec->value_type == G_TYPE_BOOLEAN);
2875 if (g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
2876 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL) > 0)
2878 g_critical ("%s: '%s' is already bound to the busy state of the application", G_STRFUNC, property);
2879 return;
2882 binding = g_slice_new (GApplicationBusyBinding);
2883 binding->app = g_object_ref (application);
2884 binding->is_busy = FALSE;
2886 closure = g_cclosure_new (G_CALLBACK (g_application_notify_busy_binding), binding,
2887 g_application_busy_binding_destroy);
2888 g_signal_connect_closure_by_id (object, notify_id, property_quark, closure, FALSE);
2890 /* fetch the initial value */
2891 g_application_notify_busy_binding (object, pspec, binding);
2895 * g_application_unbind_busy_property:
2896 * @application: a #GApplication
2897 * @object: (type GObject.Object): a #GObject
2898 * @property: the name of a boolean property of @object
2900 * Destroys a binding between @property and the busy state of
2901 * @application that was previously created with
2902 * g_application_bind_busy_property().
2904 * Since: 2.44
2906 void
2907 g_application_unbind_busy_property (GApplication *application,
2908 gpointer object,
2909 const gchar *property)
2911 guint notify_id;
2912 GQuark property_quark;
2913 gulong handler_id;
2915 g_return_if_fail (G_IS_APPLICATION (application));
2916 g_return_if_fail (G_IS_OBJECT (object));
2917 g_return_if_fail (property != NULL);
2919 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
2920 property_quark = g_quark_from_string (property);
2922 handler_id = g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
2923 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL);
2924 if (handler_id == 0)
2926 g_critical ("%s: '%s' is not bound to the busy state of the application", G_STRFUNC, property);
2927 return;
2930 g_signal_handler_disconnect (object, handler_id);
2933 /* Epilogue {{{1 */
2934 /* vim:set foldmethod=marker: */