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>
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"
46 * SECTION:gapplication
47 * @title: GApplication
48 * @short_description: Core application class
51 * A #GApplication is the foundation of an application. It wraps some
52 * low-level platform-specific services and is intended to act as the
53 * foundation for higher-level application classes such as
54 * #GtkApplication or #MxApplication. In general, you should not use
55 * this class outside of a higher level framework.
57 * GApplication provides convenient life cycle management by maintaining
58 * a <firstterm>use count</firstterm> for the primary application instance.
59 * The use count can be changed using g_application_hold() and
60 * g_application_release(). If it drops to zero, the application exits.
61 * Higher-level classes such as #GtkApplication employ the use count to
62 * ensure that the application stays alive as long as it has any opened
65 * Another feature that GApplication (optionally) provides is process
66 * uniqueness. Applications can make use of this functionality by
67 * providing a unique application ID. If given, only one application
68 * with this ID can be running at a time per session. The session
69 * concept is platform-dependent, but corresponds roughly to a graphical
70 * desktop login. When your application is launched again, its
71 * arguments are passed through platform communication to the already
72 * running program. The already running instance of the program is
73 * called the <firstterm>primary instance</firstterm>; for non-unique
74 * applications this is the always the current instance.
75 * On Linux, the D-Bus session bus is used for communication.
77 * The use of #GApplication differs from some other commonly-used
78 * uniqueness libraries (such as libunique) in important ways. The
79 * application is not expected to manually register itself and check
80 * if it is the primary instance. Instead, the main() function of a
81 * #GApplication should do very little more than instantiating the
82 * application instance, possibly connecting signal handlers, then
83 * calling g_application_run(). All checks for uniqueness are done
84 * internally. If the application is the primary instance then the
85 * startup signal is emitted and the mainloop runs. If the application
86 * is not the primary instance then a signal is sent to the primary
87 * instance and g_application_run() promptly returns. See the code
90 * If used, the expected form of an application identifier is very close
92 * [DBus bus name](http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface).
93 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
94 * For details on valid application identifiers, see g_application_id_is_valid().
96 * On Linux, the application identifier is claimed as a well-known bus name
97 * on the user's session bus. This means that the uniqueness of your
98 * application is scoped to the current session. It also means that your
99 * application may provide additional services (through registration of other
100 * object paths) at that bus name. The registration of these object paths
101 * should be done with the shared GDBus session bus. Note that due to the
102 * internal architecture of GDBus, method calls can be dispatched at any time
103 * (even if a main loop is not running). For this reason, you must ensure that
104 * any object paths that you wish to register are registered before #GApplication
105 * attempts to acquire the bus name of your application (which happens in
106 * g_application_register()). Unfortunately, this means that you cannot use
107 * g_application_get_is_remote() to decide if you want to register object paths.
109 * GApplication also implements the #GActionGroup and #GActionMap
110 * interfaces and lets you easily export actions by adding them with
111 * g_action_map_add_action(). When invoking an action by calling
112 * g_action_group_activate_action() on the application, it is always
113 * invoked in the primary instance. The actions are also exported on
114 * the session bus, and GIO provides the #GDBusActionGroup wrapper to
115 * conveniently access them remotely. GIO provides a #GDBusMenuModel wrapper
116 * for remote access to exported #GMenuModels.
118 * There is a number of different entry points into a GApplication:
120 * - via 'Activate' (i.e. just starting the application)
122 * - via 'Open' (i.e. opening some files)
124 * - by handling a command-line
126 * - via activating an action
128 * The #GApplication::startup signal lets you handle the application
129 * initialization for all of these in a single place.
131 * Regardless of which of these entry points is used to start the application,
132 * GApplication passes some <firstterm id="platform-data">platform
133 * data</firstterm> from the launching instance to the primary instance,
134 * in the form of a #GVariant dictionary mapping strings to variants.
135 * To use platform data, override the @before_emit or @after_emit virtual
136 * functions in your #GApplication subclass. When dealing with
137 * #GApplicationCommandLine objects, the platform data is directly
138 * available via g_application_command_line_get_cwd(),
139 * g_application_command_line_get_environ() and
140 * g_application_command_line_get_platform_data().
142 * As the name indicates, the platform data may vary depending on the
143 * operating system, but it always includes the current directory (key
144 * "cwd"), and optionally the environment (ie the set of environment
145 * variables and their values) of the calling process (key "environ").
146 * The environment is only added to the platform data if the
147 * %G_APPLICATION_SEND_ENVIRONMENT flag is set. #GApplication subclasses
148 * can add their own platform data by overriding the @add_platform_data
149 * virtual function. For instance, #GtkApplication adds startup notification
152 * To parse commandline arguments you may handle the
153 * #GApplication::command-line signal or override the local_command_line()
154 * vfunc, to parse them in either the primary instance or the local instance,
157 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
159 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
160 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
165 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
167 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
168 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
173 * <example id="gapplication-example-dbushooks"><title>Using extra D-Bus hooks with a GApplication</title>
175 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-dbushooks.c">
176 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
184 * @startup: invoked on the primary instance immediately after registration
185 * @shutdown: invoked only on the registered primary instance immediately
186 * after the main loop terminates
187 * @activate: invoked on the primary instance when an activation occurs
188 * @open: invoked on the primary instance when there are files to open
189 * @command_line: invoked on the primary instance when a command-line is
190 * not handled locally
191 * @local_command_line: invoked (locally) when the process has been invoked
192 * via commandline execution (as opposed to, say, D-Bus activation - which
193 * is not currently supported by GApplication). The virtual function has
194 * the chance to inspect (and possibly replace) the list of command line
195 * arguments. See g_application_run() for more information.
196 * @before_emit: invoked on the primary instance before 'activate', 'open',
197 * 'command-line' or any action invocation, gets the 'platform data' from
198 * the calling instance
199 * @after_emit: invoked on the primary instance after 'activate', 'open',
200 * 'command-line' or any action invocation, gets the 'platform data' from
201 * the calling instance
202 * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
203 * the primary instance when activating, opening or invoking actions
204 * @quit_mainloop: Used to be invoked on the primary instance when the use
205 * count of the application drops to zero (and after any inactivity
206 * timeout, if requested). Not used anymore since 2.32
207 * @run_mainloop: Used to be invoked on the primary instance from
208 * g_application_run() if the use-count is non-zero. Since 2.32,
209 * GApplication is iterating the main context directly and is not
210 * using @run_mainloop anymore
211 * @dbus_register: invoked locally during registration, if the application is
212 * using its D-Bus backend. You can use this to export extra objects on the
213 * bus, that need to exist before the application tries to own the bus name.
214 * The function is passed the #GDBusConnection to to session bus, and the
215 * object path that #GApplication will use to export is D-Bus API.
216 * If this function returns %TRUE, registration will proceed; otherwise
217 * registration will abort. Since: 2.34
218 * @dbus_unregister: invoked locally during unregistration, if the application
219 * is using its D-Bus backend. Use this to undo anything done by the
220 * @dbus_register vfunc. Since: 2.34
222 * Virtual function table for #GApplication.
227 struct _GApplicationPrivate
229 GApplicationFlags flags;
232 GActionGroup *actions;
233 GMenuModel *app_menu;
236 guint inactivity_timeout_id;
237 guint inactivity_timeout;
241 guint is_registered : 1;
243 guint did_startup : 1;
244 guint did_shutdown : 1;
245 guint must_quit_now : 1;
247 GRemoteActionGroup *remote_actions;
248 GApplicationImpl *impl;
250 GNotificationBackend *notifications;
252 /* GOptionContext support */
253 GOptionGroup *main_options;
254 GSList *option_groups;
255 GHashTable *packed_options;
256 gboolean options_parsed;
266 PROP_INACTIVITY_TIMEOUT,
278 SIGNAL_HANDLE_LOCAL_OPTIONS,
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
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;
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))
316 g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
317 const gchar *action_name,
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);
333 g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
334 const gchar *action_name,
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);
350 g_application_exported_actions_init (GApplicationExportedActions *actions)
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;
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 */
380 free_option_entry (gpointer data)
382 GOptionEntry *entry = data;
386 case G_OPTION_ARG_STRING:
387 case G_OPTION_ARG_FILENAME:
388 g_free (*(gchar **) entry->arg_data);
391 case G_OPTION_ARG_STRING_ARRAY:
392 case G_OPTION_ARG_FILENAME_ARRAY:
393 g_strfreev (*(gchar ***) entry->arg_data);
397 /* most things require no free... */
401 /* ...except for the space that we allocated for it ourselves */
402 g_free (entry->arg_data);
404 g_slice_free (GOptionEntry, entry);
408 g_application_pack_option_entries (GApplication *application,
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;
422 case G_OPTION_ARG_NONE:
423 if (*(gboolean *) entry->arg_data != 2)
424 value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
427 case G_OPTION_ARG_STRING:
428 if (*(gchar **) entry->arg_data)
429 value = g_variant_new_string (*(gchar **) entry->arg_data);
432 case G_OPTION_ARG_INT:
433 if (*(gint32 *) entry->arg_data)
434 value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
437 case G_OPTION_ARG_FILENAME:
438 if (*(gchar **) entry->arg_data)
439 value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
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);
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);
452 case G_OPTION_ARG_DOUBLE:
453 if (*(gdouble *) entry->arg_data)
454 value = g_variant_new_double (*(gdouble *) entry->arg_data);
457 case G_OPTION_ARG_INT64:
458 if (*(gint64 *) entry->arg_data)
459 value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
463 g_assert_not_reached ();
467 g_variant_dict_insert_value (dict, entry->long_name, value);
471 static GVariantDict *
472 g_application_parse_command_line (GApplication *application,
476 gboolean become_service = FALSE;
477 GVariantDict *dict = NULL;
478 GOptionContext *context;
480 /* Due to the memory management of GOptionGroup we can only parse
481 * options once. That's because once you add a group to the
482 * GOptionContext there is no way to get it back again. This is fine:
483 * local_command_line() should never get invoked more than once
484 * anyway. Add a sanity check just to be sure.
486 g_return_val_if_fail (!application->priv->options_parsed, NULL);
488 context = g_option_context_new (NULL);
490 /* Add the main option group, if it exists */
491 if (application->priv->main_options)
493 /* This consumes the main_options */
494 g_option_context_set_main_group (context, application->priv->main_options);
495 application->priv->main_options = NULL;
498 /* Add any other option groups if they exist. Adding them to the
499 * context will consume them, so we free the list as we go...
501 while (application->priv->option_groups)
503 g_option_context_add_group (context, application->priv->option_groups->data);
504 application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
505 application->priv->option_groups);
508 /* If the application has not registered local options and it has
509 * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
510 * their primary instance commandline handler may want to deal with
511 * the arguments. We must therefore ignore them.
513 if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
514 g_option_context_set_ignore_unknown_options (context, TRUE);
516 /* In the case that we are not explicitly marked as a service or a
517 * launcher then we want to add the "--gapplication-service" option to
518 * allow the process to be made into a service.
520 if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
522 GOptionGroup *option_group;
523 GOptionEntry entries[] = {
524 { "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
525 N_("Enter GApplication service mode (use from D-Bus service files)") },
529 option_group = g_option_group_new ("gapplication",
530 _("GApplication options"), _("Show GApplication options"),
532 g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
533 g_option_group_add_entries (option_group, entries);
535 g_option_context_add_group (context, option_group);
538 /* Now we parse... */
539 if (!g_option_context_parse_strv (context, arguments, error))
542 /* Check for --gapplication-service */
544 application->priv->flags |= G_APPLICATION_IS_SERVICE;
546 dict = g_variant_dict_new (NULL);
547 if (application->priv->packed_options)
549 g_application_pack_option_entries (application, dict);
550 g_hash_table_unref (application->priv->packed_options);
551 application->priv->packed_options = NULL;
555 /* Make sure we don't run again */
556 application->priv->options_parsed = TRUE;
558 g_option_context_free (context);
564 add_packed_option (GApplication *application,
569 case G_OPTION_ARG_NONE:
570 entry->arg_data = g_new (gboolean, 1);
571 *(gboolean *) entry->arg_data = 2;
574 case G_OPTION_ARG_INT:
575 entry->arg_data = g_new0 (gint, 1);
578 case G_OPTION_ARG_STRING:
579 case G_OPTION_ARG_FILENAME:
580 case G_OPTION_ARG_STRING_ARRAY:
581 case G_OPTION_ARG_FILENAME_ARRAY:
582 entry->arg_data = g_new0 (gpointer, 1);
585 case G_OPTION_ARG_INT64:
586 entry->arg_data = g_new0 (gint64, 1);
589 case G_OPTION_ARG_DOUBLE:
590 entry->arg_data = g_new0 (gdouble, 1);
594 g_return_if_reached ();
597 if (!application->priv->packed_options)
598 application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
600 g_hash_table_insert (application->priv->packed_options,
601 g_strdup (entry->long_name),
602 g_slice_dup (GOptionEntry, entry));
606 * g_application_add_main_option_entries:
607 * @application: a #GApplication
608 * @entries: a %NULL-terminated list of #GOptionEntrys
610 * Adds main option entries to be handled by @application.
612 * This function is comparable to g_option_context_add_main_entries().
614 * After the commandline arguments are parsed, the
615 * #GApplication::handle-local-options signal will be emitted. At this
616 * point, the application can inspect the values pointed to by @arg_data
617 * in the given #GOptionEntrys.
619 * Unlike #GOptionContext, #GApplication supports giving a %NULL
620 * @arg_data for a non-callback #GOptionEntry. This results in the
621 * argument in question being packed into a #GVariantDict which is also
622 * passed to #GApplication::handle-local-options, where it can be
623 * inspected and modified. If %G_APPLICATION_HANDLES_COMMAND_LINE is
624 * set, then the resulting dictionary is sent to the primary instance,
625 * where g_application_command_line_get_options_dict() will return it.
626 * This "packing" is done according to the type of the argument --
627 * booleans for normal flags, strings for strings, bytestrings for
628 * filenames, etc. The packing only occurs if the flag is given (ie: we
629 * do not pack a "false" #GVariant in the case that a flag is missing).
631 * In general, it is recommended that all commandline arguments are
632 * parsed locally. The options dictionary should then be used to
633 * transmit the result of the parsing to the primary instance, where
634 * g_variant_dict_lookup() can be used. For local options, it is
635 * possible to either use @arg_data in the usual way, or to consult (and
636 * potentially remove) the option from the options dictionary.
638 * This function is new in GLib 2.40. Before then, the only real choice
639 * was to send all of the commandline arguments (options and all) to the
640 * primary instance for handling. #GApplication ignored them completely
641 * on the local side. Calling this function "opts in" to the new
642 * behaviour, and in particular, means that unrecognised options will be
643 * treated as errors. Unrecognised options have never been ignored when
644 * %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
646 * If #GApplication::handle-local-options needs to see the list of
647 * filenames, then the use of %G_OPTION_REMAINING is recommended. If
648 * @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
649 * the options dictionary. If you do use %G_OPTION_REMAINING then you
650 * need to handle these arguments for yourself because once they are
651 * consumed, they will no longer be visible to the default handling
652 * (which treats them as filenames to be opened).
657 g_application_add_main_option_entries (GApplication *application,
658 const GOptionEntry *entries)
662 g_return_if_fail (G_IS_APPLICATION (application));
663 g_return_if_fail (entries != NULL);
665 if (!application->priv->main_options)
666 application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
668 for (i = 0; entries[i].long_name; i++)
670 GOptionEntry my_entries[2] = { entries[i], { NULL } };
672 if (!my_entries[0].arg_data)
673 add_packed_option (application, &my_entries[0]);
675 g_option_group_add_entries (application->priv->main_options, my_entries);
680 * g_application_add_option_group:
681 * @application: the #GApplication
682 * @group: a #GOptionGroup
684 * Adds a #GOptionGroup to the commandline handling of @application.
686 * This function is comparable to g_option_context_add_group().
688 * Unlike g_application_add_main_option_entries(), this function does
689 * not deal with %NULL @arg_data and never transmits options to the
692 * The reason for that is because, by the time the options arrive at the
693 * primary instance, it is typically too late to do anything with them.
694 * Taking the GTK option group as an example: GTK will already have been
695 * initialised by the time the #GApplication::command-line handler runs.
696 * In the case that this is not the first-running instance of the
697 * application, the existing instance may already have been running for
700 * This means that the options from #GOptionGroup are only really usable
701 * in the case that the instance of the application being run is the
702 * first instance. Passing options like <literal>--display=</literal>
703 * or <literal>--gdk-debug=</literal> on future runs will have no effect
704 * on the existing primary instance.
706 * Calling this function will cause the options in the supplied option
707 * group to be parsed, but it does not cause you to be "opted in" to the
708 * new functionality whereby unrecognised options are rejected even if
709 * %G_APPLICATION_HANDLES_COMMAND_LINE was given.
714 g_application_add_option_group (GApplication *application,
717 g_return_if_fail (G_IS_APPLICATION (application));
718 g_return_if_fail (group != NULL);
720 application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
723 /* vfunc defaults {{{1 */
725 g_application_real_before_emit (GApplication *application,
726 GVariant *platform_data)
731 g_application_real_after_emit (GApplication *application,
732 GVariant *platform_data)
737 g_application_real_startup (GApplication *application)
739 application->priv->did_startup = TRUE;
743 g_application_real_shutdown (GApplication *application)
745 application->priv->did_shutdown = TRUE;
749 g_application_real_activate (GApplication *application)
751 if (!g_signal_has_handler_pending (application,
752 g_application_signals[SIGNAL_ACTIVATE],
754 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
756 static gboolean warned;
761 g_warning ("Your application does not implement "
762 "g_application_activate() and has no handlers connected "
763 "to the 'activate' signal. It should do one of these.");
769 g_application_real_open (GApplication *application,
774 if (!g_signal_has_handler_pending (application,
775 g_application_signals[SIGNAL_OPEN],
777 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
779 static gboolean warned;
784 g_warning ("Your application claims to support opening files "
785 "but does not implement g_application_open() and has no "
786 "handlers connected to the 'open' signal.");
792 g_application_real_command_line (GApplication *application,
793 GApplicationCommandLine *cmdline)
795 if (!g_signal_has_handler_pending (application,
796 g_application_signals[SIGNAL_COMMAND_LINE],
798 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
800 static gboolean warned;
805 g_warning ("Your application claims to support custom command line "
806 "handling but does not implement g_application_command_line() "
807 "and has no handlers connected to the 'command-line' signal.");
816 g_application_real_handle_local_options (GApplication *application,
817 GVariantDict *options)
823 get_platform_data (GApplication *application,
826 GVariantBuilder *builder;
829 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
832 gchar *cwd = g_get_current_dir ();
833 g_variant_builder_add (builder, "{sv}", "cwd",
834 g_variant_new_bytestring (cwd));
838 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
843 envp = g_get_environ ();
844 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
847 g_variant_builder_add (builder, "{sv}", "environ", array);
851 g_variant_builder_add (builder, "{sv}", "options", options);
853 G_APPLICATION_GET_CLASS (application)->
854 add_platform_data (application, builder);
856 result = g_variant_builder_end (builder);
857 g_variant_builder_unref (builder);
863 g_application_call_command_line (GApplication *application,
864 const gchar * const *arguments,
868 if (application->priv->is_remote)
870 GVariant *platform_data;
872 platform_data = get_platform_data (application, options);
873 *exit_status = g_application_impl_command_line (application->priv->impl, arguments, platform_data);
877 GApplicationCommandLine *cmdline;
880 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
881 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
885 g_signal_emit (application, g_application_signals[SIGNAL_COMMAND_LINE], 0, cmdline, exit_status);
886 g_object_unref (cmdline);
891 g_application_real_local_command_line (GApplication *application,
895 GError *error = NULL;
896 GVariantDict *options;
899 options = g_application_parse_command_line (application, arguments, &error);
902 g_printerr ("%s\n", error->message);
907 g_signal_emit (application, g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS], 0, options, exit_status);
909 if (*exit_status >= 0)
911 g_variant_dict_unref (options);
915 if (!g_application_register (application, NULL, &error))
917 g_printerr ("Failed to register: %s\n", error->message);
918 g_variant_dict_unref (options);
919 g_error_free (error);
924 n_args = g_strv_length (*arguments);
926 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
928 if ((*exit_status = n_args > 1))
930 g_printerr ("GApplication service mode takes no arguments.\n");
931 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
937 else if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
939 g_application_call_command_line (application,
940 (const gchar **) *arguments,
941 g_variant_dict_end (options),
948 g_application_activate (application);
954 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
956 g_critical ("This application can not open files.");
965 n_files = n_args - 1;
966 files = g_new (GFile *, n_files);
968 for (i = 0; i < n_files; i++)
969 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
971 g_application_open (application, files, n_files, "");
973 for (i = 0; i < n_files; i++)
974 g_object_unref (files[i]);
982 g_variant_dict_unref (options);
988 g_application_real_add_platform_data (GApplication *application,
989 GVariantBuilder *builder)
994 g_application_real_dbus_register (GApplication *application,
995 GDBusConnection *connection,
996 const gchar *object_path,
1003 g_application_real_dbus_unregister (GApplication *application,
1004 GDBusConnection *connection,
1005 const gchar *object_path)
1009 /* GObject implementation stuff {{{1 */
1011 g_application_set_property (GObject *object,
1013 const GValue *value,
1016 GApplication *application = G_APPLICATION (object);
1020 case PROP_APPLICATION_ID:
1021 g_application_set_application_id (application,
1022 g_value_get_string (value));
1026 g_application_set_flags (application, g_value_get_flags (value));
1029 case PROP_INACTIVITY_TIMEOUT:
1030 g_application_set_inactivity_timeout (application,
1031 g_value_get_uint (value));
1034 case PROP_ACTION_GROUP:
1035 g_clear_object (&application->priv->actions);
1036 application->priv->actions = g_value_dup_object (value);
1040 g_assert_not_reached ();
1045 * g_application_set_action_group:
1046 * @application: a #GApplication
1047 * @action_group: (allow-none): a #GActionGroup, or %NULL
1049 * This used to be how actions were associated with a #GApplication.
1050 * Now there is #GActionMap for that.
1054 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
1055 * mix use of this API with use of #GActionMap on the same @application
1056 * or things will go very badly wrong. This function is known to
1057 * introduce buggy behaviour (ie: signals not emitted on changes to the
1058 * action group), so you should really use #GActionMap instead.
1061 g_application_set_action_group (GApplication *application,
1062 GActionGroup *action_group)
1064 g_return_if_fail (G_IS_APPLICATION (application));
1065 g_return_if_fail (!application->priv->is_registered);
1067 if (application->priv->actions != NULL)
1068 g_object_unref (application->priv->actions);
1070 application->priv->actions = action_group;
1072 if (application->priv->actions != NULL)
1073 g_object_ref (application->priv->actions);
1077 g_application_get_property (GObject *object,
1082 GApplication *application = G_APPLICATION (object);
1086 case PROP_APPLICATION_ID:
1087 g_value_set_string (value,
1088 g_application_get_application_id (application));
1092 g_value_set_flags (value,
1093 g_application_get_flags (application));
1096 case PROP_IS_REGISTERED:
1097 g_value_set_boolean (value,
1098 g_application_get_is_registered (application));
1101 case PROP_IS_REMOTE:
1102 g_value_set_boolean (value,
1103 g_application_get_is_remote (application));
1106 case PROP_INACTIVITY_TIMEOUT:
1107 g_value_set_uint (value,
1108 g_application_get_inactivity_timeout (application));
1112 g_assert_not_reached ();
1117 g_application_constructed (GObject *object)
1119 GApplication *application = G_APPLICATION (object);
1121 if (g_application_get_default () == NULL)
1122 g_application_set_default (application);
1126 g_application_finalize (GObject *object)
1128 GApplication *application = G_APPLICATION (object);
1130 g_slist_free_full (application->priv->option_groups, (GDestroyNotify) g_option_group_free);
1131 if (application->priv->main_options)
1132 g_option_group_free (application->priv->main_options);
1133 if (application->priv->packed_options)
1134 g_hash_table_unref (application->priv->packed_options);
1136 if (application->priv->impl)
1137 g_application_impl_destroy (application->priv->impl);
1138 g_free (application->priv->id);
1140 if (g_application_get_default () == application)
1141 g_application_set_default (NULL);
1143 if (application->priv->actions)
1144 g_object_unref (application->priv->actions);
1146 if (application->priv->notifications)
1147 g_object_unref (application->priv->notifications);
1149 G_OBJECT_CLASS (g_application_parent_class)
1150 ->finalize (object);
1154 g_application_init (GApplication *application)
1156 application->priv = g_application_get_instance_private (application);
1158 application->priv->actions = g_application_exported_actions_new (application);
1160 /* application->priv->actions is the one and only ref on the group, so when
1161 * we dispose, the action group will die, disconnecting all signals.
1163 g_signal_connect_swapped (application->priv->actions, "action-added",
1164 G_CALLBACK (g_action_group_action_added), application);
1165 g_signal_connect_swapped (application->priv->actions, "action-enabled-changed",
1166 G_CALLBACK (g_action_group_action_enabled_changed), application);
1167 g_signal_connect_swapped (application->priv->actions, "action-state-changed",
1168 G_CALLBACK (g_action_group_action_state_changed), application);
1169 g_signal_connect_swapped (application->priv->actions, "action-removed",
1170 G_CALLBACK (g_action_group_action_removed), application);
1174 g_application_handle_local_options_accumulator (GSignalInvocationHint *ihint,
1175 GValue *return_accu,
1176 const GValue *handler_return,
1181 value = g_value_get_int (handler_return);
1182 g_value_set_int (return_accu, value);
1188 g_application_class_init (GApplicationClass *class)
1190 GObjectClass *object_class = G_OBJECT_CLASS (class);
1192 object_class->constructed = g_application_constructed;
1193 object_class->finalize = g_application_finalize;
1194 object_class->get_property = g_application_get_property;
1195 object_class->set_property = g_application_set_property;
1197 class->before_emit = g_application_real_before_emit;
1198 class->after_emit = g_application_real_after_emit;
1199 class->startup = g_application_real_startup;
1200 class->shutdown = g_application_real_shutdown;
1201 class->activate = g_application_real_activate;
1202 class->open = g_application_real_open;
1203 class->command_line = g_application_real_command_line;
1204 class->local_command_line = g_application_real_local_command_line;
1205 class->handle_local_options = g_application_real_handle_local_options;
1206 class->add_platform_data = g_application_real_add_platform_data;
1207 class->dbus_register = g_application_real_dbus_register;
1208 class->dbus_unregister = g_application_real_dbus_unregister;
1210 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
1211 g_param_spec_string ("application-id",
1212 P_("Application identifier"),
1213 P_("The unique identifier for the application"),
1214 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
1215 G_PARAM_STATIC_STRINGS));
1217 g_object_class_install_property (object_class, PROP_FLAGS,
1218 g_param_spec_flags ("flags",
1219 P_("Application flags"),
1220 P_("Flags specifying the behaviour of the application"),
1221 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
1222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1224 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
1225 g_param_spec_boolean ("is-registered",
1226 P_("Is registered"),
1227 P_("If g_application_register() has been called"),
1228 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1230 g_object_class_install_property (object_class, PROP_IS_REMOTE,
1231 g_param_spec_boolean ("is-remote",
1233 P_("If this application instance is remote"),
1234 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1236 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
1237 g_param_spec_uint ("inactivity-timeout",
1238 P_("Inactivity timeout"),
1239 P_("Time (ms) to stay alive after becoming idle"),
1241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1243 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
1244 g_param_spec_object ("action-group",
1246 P_("The group of actions that the application exports"),
1247 G_TYPE_ACTION_GROUP,
1248 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1251 * GApplication::startup:
1252 * @application: the application
1254 * The ::startup signal is emitted on the primary instance immediately
1255 * after registration. See g_application_register().
1257 g_application_signals[SIGNAL_STARTUP] =
1258 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
1259 G_STRUCT_OFFSET (GApplicationClass, startup),
1260 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1263 * GApplication::shutdown:
1264 * @application: the application
1266 * The ::shutdown signal is emitted only on the registered primary instance
1267 * immediately after the main loop terminates.
1269 g_application_signals[SIGNAL_SHUTDOWN] =
1270 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1271 G_STRUCT_OFFSET (GApplicationClass, shutdown),
1272 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1275 * GApplication::activate:
1276 * @application: the application
1278 * The ::activate signal is emitted on the primary instance when an
1279 * activation occurs. See g_application_activate().
1281 g_application_signals[SIGNAL_ACTIVATE] =
1282 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1283 G_STRUCT_OFFSET (GApplicationClass, activate),
1284 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1288 * GApplication::open:
1289 * @application: the application
1290 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
1291 * @n_files: the length of @files
1292 * @hint: a hint provided by the calling instance
1294 * The ::open signal is emitted on the primary instance when there are
1295 * files to open. See g_application_open() for more information.
1297 g_application_signals[SIGNAL_OPEN] =
1298 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1299 G_STRUCT_OFFSET (GApplicationClass, open),
1301 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
1304 * GApplication::command-line:
1305 * @application: the application
1306 * @command_line: a #GApplicationCommandLine representing the
1307 * passed commandline
1309 * The ::command-line signal is emitted on the primary instance when
1310 * a commandline is not handled locally. See g_application_run() and
1311 * the #GApplicationCommandLine documentation for more information.
1313 * Returns: An integer that is set as the exit status for the calling
1314 * process. See g_application_command_line_set_exit_status().
1316 g_application_signals[SIGNAL_COMMAND_LINE] =
1317 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1318 G_STRUCT_OFFSET (GApplicationClass, command_line),
1319 g_signal_accumulator_first_wins, NULL,
1321 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
1324 * GApplication::handle-local-options:
1325 * @application: the application
1326 * @options: the options dictionary
1328 * The ::handle-local-options signal is emitted on the local instance
1329 * after the parsing of the commandline options has occurred.
1331 * You can add options to be recognised during commandline option
1332 * parsing using g_application_add_main_option_entries() and
1333 * g_application_add_option_group().
1335 * Signal handlers can inspect @options (along with values pointed to
1336 * from the @arg_data of an installed #GOptionEntrys) in order to
1337 * decide to perform certain actions, including direct local handling
1338 * (which may be useful for options like --version).
1340 * If the options have been "handled" then a non-negative value should
1341 * be returned. In this case, the return value is the exit status: 0
1342 * for success and a positive value for failure. -1 means to continue
1343 * normal processing.
1345 * In the event that the application is marked
1346 * %G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will
1347 * send the @option dictionary to the primary instance where it can be
1348 * read with g_application_command_line_get_options(). The signal
1349 * handler can modify the dictionary before returning, and the
1350 * modified dictionary will be sent.
1352 * In the event that %G_APPLICATION_HANDLES_COMMAND_LINE is not set,
1353 * "normal processing" will treat the remaining uncollected command
1354 * line arguments as filenames or URIs. If there are no arguments,
1355 * the application is activated by g_application_activate(). One or
1356 * more arguments results in a call to g_application_open().
1358 * If you want to handle the local commandline arguments for yourself
1359 * by converting them to calls to g_application_open() or
1360 * g_action_group_activate_action() then you must be sure to register
1361 * the application first. You should probably not call
1362 * g_application_activate() for yourself, however: just return -1 and
1363 * allow the default handler to do it for you. This will ensure that
1364 * the <literal>--gapplication-service</literal> switch works properly
1365 * (ie: no activation in that case).
1367 * Note that this signal is emitted from the default implementation of
1368 * local_command_line(). If you override that function and don't
1369 * chain up then this signal will never be emitted.
1371 * You can override local_command_line() if you need more powerful
1372 * capabilities than what is provided here, but this should not
1373 * normally be required.
1377 g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS] =
1378 g_signal_new ("handle-local-options", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1379 G_STRUCT_OFFSET (GApplicationClass, handle_local_options),
1380 g_application_handle_local_options_accumulator, NULL, NULL,
1381 G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
1385 /* Application ID validity {{{1 */
1388 * g_application_id_is_valid:
1389 * @application_id: a potential application identifier
1391 * Checks if @application_id is a valid application identifier.
1393 * A valid ID is required for calls to g_application_new() and
1394 * g_application_set_application_id().
1396 * For convenience, the restrictions on application identifiers are
1399 * - Application identifiers must contain only the ASCII characters
1400 * "[A-Z][a-z][0-9]_-." and must not begin with a digit.
1402 * - Application identifiers must contain at least one '.' (period)
1403 * character (and thus at least three elements).
1405 * - Application identifiers must not begin or end with a '.' (period)
1408 * - Application identifiers must not contain consecutive '.' (period)
1411 * - Application identifiers must not exceed 255 characters.
1413 * Returns: %TRUE if @application_id is valid
1416 g_application_id_is_valid (const gchar *application_id)
1422 len = strlen (application_id);
1427 if (!g_ascii_isalpha (application_id[0]))
1430 if (application_id[len-1] == '.')
1436 for (; *application_id; application_id++)
1438 if (g_ascii_isalnum (*application_id) ||
1439 (*application_id == '-') ||
1440 (*application_id == '_'))
1444 else if (allow_dot && *application_id == '.')
1459 /* Public Constructor {{{1 */
1461 * g_application_new:
1462 * @application_id: (allow-none): the application id
1463 * @flags: the application flags
1465 * Creates a new #GApplication instance.
1467 * If non-%NULL, the application id must be valid. See
1468 * g_application_id_is_valid().
1470 * If no application ID is given then some features of #GApplication
1471 * (most notably application uniqueness) will be disabled.
1473 * Returns: a new #GApplication instance
1476 g_application_new (const gchar *application_id,
1477 GApplicationFlags flags)
1479 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
1481 return g_object_new (G_TYPE_APPLICATION,
1482 "application-id", application_id,
1487 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
1489 * g_application_get_application_id:
1490 * @application: a #GApplication
1492 * Gets the unique identifier for @application.
1494 * Returns: the identifier for @application, owned by @application
1499 g_application_get_application_id (GApplication *application)
1501 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1503 return application->priv->id;
1507 * g_application_set_application_id:
1508 * @application: a #GApplication
1509 * @application_id: (allow-none): the identifier for @application
1511 * Sets the unique identifier for @application.
1513 * The application id can only be modified if @application has not yet
1516 * If non-%NULL, the application id must be valid. See
1517 * g_application_id_is_valid().
1522 g_application_set_application_id (GApplication *application,
1523 const gchar *application_id)
1525 g_return_if_fail (G_IS_APPLICATION (application));
1527 if (g_strcmp0 (application->priv->id, application_id) != 0)
1529 g_return_if_fail (application_id == NULL || g_application_id_is_valid (application_id));
1530 g_return_if_fail (!application->priv->is_registered);
1532 g_free (application->priv->id);
1533 application->priv->id = g_strdup (application_id);
1535 g_object_notify (G_OBJECT (application), "application-id");
1540 * g_application_get_flags:
1541 * @application: a #GApplication
1543 * Gets the flags for @application.
1545 * See #GApplicationFlags.
1547 * Returns: the flags for @application
1552 g_application_get_flags (GApplication *application)
1554 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1556 return application->priv->flags;
1560 * g_application_set_flags:
1561 * @application: a #GApplication
1562 * @flags: the flags for @application
1564 * Sets the flags for @application.
1566 * The flags can only be modified if @application has not yet been
1569 * See #GApplicationFlags.
1574 g_application_set_flags (GApplication *application,
1575 GApplicationFlags flags)
1577 g_return_if_fail (G_IS_APPLICATION (application));
1579 if (application->priv->flags != flags)
1581 g_return_if_fail (!application->priv->is_registered);
1583 application->priv->flags = flags;
1585 g_object_notify (G_OBJECT (application), "flags");
1590 * g_application_get_inactivity_timeout:
1591 * @application: a #GApplication
1593 * Gets the current inactivity timeout for the application.
1595 * This is the amount of time (in milliseconds) after the last call to
1596 * g_application_release() before the application stops running.
1598 * Returns: the timeout, in milliseconds
1603 g_application_get_inactivity_timeout (GApplication *application)
1605 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1607 return application->priv->inactivity_timeout;
1611 * g_application_set_inactivity_timeout:
1612 * @application: a #GApplication
1613 * @inactivity_timeout: the timeout, in milliseconds
1615 * Sets the current inactivity timeout for the application.
1617 * This is the amount of time (in milliseconds) after the last call to
1618 * g_application_release() before the application stops running.
1620 * This call has no side effects of its own. The value set here is only
1621 * used for next time g_application_release() drops the use count to
1622 * zero. Any timeouts currently in progress are not impacted.
1627 g_application_set_inactivity_timeout (GApplication *application,
1628 guint inactivity_timeout)
1630 g_return_if_fail (G_IS_APPLICATION (application));
1632 if (application->priv->inactivity_timeout != inactivity_timeout)
1634 application->priv->inactivity_timeout = inactivity_timeout;
1636 g_object_notify (G_OBJECT (application), "inactivity-timeout");
1639 /* Read-only property getters (is registered, is remote, dbus stuff) {{{1 */
1641 * g_application_get_is_registered:
1642 * @application: a #GApplication
1644 * Checks if @application is registered.
1646 * An application is registered if g_application_register() has been
1647 * successfully called.
1649 * Returns: %TRUE if @application is registered
1654 g_application_get_is_registered (GApplication *application)
1656 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1658 return application->priv->is_registered;
1662 * g_application_get_is_remote:
1663 * @application: a #GApplication
1665 * Checks if @application is remote.
1667 * If @application is remote then it means that another instance of
1668 * application already exists (the 'primary' instance). Calls to
1669 * perform actions on @application will result in the actions being
1670 * performed by the primary instance.
1672 * The value of this property cannot be accessed before
1673 * g_application_register() has been called. See
1674 * g_application_get_is_registered().
1676 * Returns: %TRUE if @application is remote
1681 g_application_get_is_remote (GApplication *application)
1683 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1684 g_return_val_if_fail (application->priv->is_registered, FALSE);
1686 return application->priv->is_remote;
1690 * g_application_get_dbus_connection:
1691 * @application: a #GApplication
1693 * Gets the #GDBusConnection being used by the application, or %NULL.
1695 * If #GApplication is using its D-Bus backend then this function will
1696 * return the #GDBusConnection being used for uniqueness and
1697 * communication with the desktop environment and other instances of the
1700 * If #GApplication is not using D-Bus then this function will return
1701 * %NULL. This includes the situation where the D-Bus backend would
1702 * normally be in use but we were unable to connect to the bus.
1704 * This function must not be called before the application has been
1705 * registered. See g_application_get_is_registered().
1707 * Returns: (transfer none): a #GDBusConnection, or %NULL
1712 g_application_get_dbus_connection (GApplication *application)
1714 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1715 g_return_val_if_fail (application->priv->is_registered, FALSE);
1717 return g_application_impl_get_dbus_connection (application->priv->impl);
1721 * g_application_get_dbus_object_path:
1722 * @application: a #GApplication
1724 * Gets the D-Bus object path being used by the application, or %NULL.
1726 * If #GApplication is using its D-Bus backend then this function will
1727 * return the D-Bus object path that #GApplication is using. If the
1728 * application is the primary instance then there is an object published
1729 * at this path. If the application is not the primary instance then
1730 * the result of this function is undefined.
1732 * If #GApplication is not using D-Bus then this function will return
1733 * %NULL. This includes the situation where the D-Bus backend would
1734 * normally be in use but we were unable to connect to the bus.
1736 * This function must not be called before the application has been
1737 * registered. See g_application_get_is_registered().
1739 * Returns: the object path, or %NULL
1744 g_application_get_dbus_object_path (GApplication *application)
1746 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1747 g_return_val_if_fail (application->priv->is_registered, FALSE);
1749 return g_application_impl_get_dbus_object_path (application->priv->impl);
1754 * g_application_register:
1755 * @application: a #GApplication
1756 * @cancellable: (allow-none): a #GCancellable, or %NULL
1757 * @error: a pointer to a NULL #GError, or %NULL
1759 * Attempts registration of the application.
1761 * This is the point at which the application discovers if it is the
1762 * primary instance or merely acting as a remote for an already-existing
1763 * primary instance. This is implemented by attempting to acquire the
1764 * application identifier as a unique bus name on the session bus using
1767 * If there is no application ID or if %G_APPLICATION_NON_UNIQUE was
1768 * given, then this process will always become the primary instance.
1770 * Due to the internal architecture of GDBus, method calls can be
1771 * dispatched at any time (even if a main loop is not running). For
1772 * this reason, you must ensure that any object paths that you wish to
1773 * register are registered before calling this function.
1775 * If the application has already been registered then %TRUE is
1776 * returned with no work performed.
1778 * The #GApplication::startup signal is emitted if registration succeeds
1779 * and @application is the primary instance (including the non-unique
1782 * In the event of an error (such as @cancellable being cancelled, or a
1783 * failure to connect to the session bus), %FALSE is returned and @error
1784 * is set appropriately.
1786 * Note: the return value of this function is not an indicator that this
1787 * instance is or is not the primary instance of the application. See
1788 * g_application_get_is_remote() for that.
1790 * Returns: %TRUE if registration succeeded
1795 g_application_register (GApplication *application,
1796 GCancellable *cancellable,
1799 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1801 if (!application->priv->is_registered)
1803 if (application->priv->id == NULL)
1804 application->priv->flags |= G_APPLICATION_NON_UNIQUE;
1806 application->priv->impl =
1807 g_application_impl_register (application, application->priv->id,
1808 application->priv->flags,
1809 application->priv->actions,
1810 &application->priv->remote_actions,
1811 cancellable, error);
1813 if (application->priv->impl == NULL)
1816 application->priv->is_remote = application->priv->remote_actions != NULL;
1817 application->priv->is_registered = TRUE;
1819 g_object_notify (G_OBJECT (application), "is-registered");
1821 if (!application->priv->is_remote)
1823 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1825 if (!application->priv->did_startup)
1826 g_critical ("GApplication subclass '%s' failed to chain up on"
1827 " ::startup (from start of override function)",
1828 G_OBJECT_TYPE_NAME (application));
1835 /* Hold/release {{{1 */
1837 * g_application_hold:
1838 * @application: a #GApplication
1840 * Increases the use count of @application.
1842 * Use this function to indicate that the application has a reason to
1843 * continue to run. For example, g_application_hold() is called by GTK+
1844 * when a toplevel window is on the screen.
1846 * To cancel the hold, call g_application_release().
1849 g_application_hold (GApplication *application)
1851 g_return_if_fail (G_IS_APPLICATION (application));
1853 if (application->priv->inactivity_timeout_id)
1855 g_source_remove (application->priv->inactivity_timeout_id);
1856 application->priv->inactivity_timeout_id = 0;
1859 application->priv->use_count++;
1863 inactivity_timeout_expired (gpointer data)
1865 GApplication *application = G_APPLICATION (data);
1867 application->priv->inactivity_timeout_id = 0;
1869 return G_SOURCE_REMOVE;
1874 * g_application_release:
1875 * @application: a #GApplication
1877 * Decrease the use count of @application.
1879 * When the use count reaches zero, the application will stop running.
1881 * Never call this function except to cancel the effect of a previous
1882 * call to g_application_hold().
1885 g_application_release (GApplication *application)
1887 g_return_if_fail (G_IS_APPLICATION (application));
1889 application->priv->use_count--;
1891 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1892 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1893 inactivity_timeout_expired, application);
1896 /* Activate, Open {{{1 */
1898 * g_application_activate:
1899 * @application: a #GApplication
1901 * Activates the application.
1903 * In essence, this results in the #GApplication::activate signal being
1904 * emitted in the primary instance.
1906 * The application must be registered before calling this function.
1911 g_application_activate (GApplication *application)
1913 g_return_if_fail (G_IS_APPLICATION (application));
1914 g_return_if_fail (application->priv->is_registered);
1916 if (application->priv->is_remote)
1917 g_application_impl_activate (application->priv->impl,
1918 get_platform_data (application, NULL));
1921 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1925 * g_application_open:
1926 * @application: a #GApplication
1927 * @files: (array length=n_files): an array of #GFiles to open
1928 * @n_files: the length of the @files array
1929 * @hint: a hint (or ""), but never %NULL
1931 * Opens the given files.
1933 * In essence, this results in the #GApplication::open signal being emitted
1934 * in the primary instance.
1936 * @n_files must be greater than zero.
1938 * @hint is simply passed through to the ::open signal. It is
1939 * intended to be used by applications that have multiple modes for
1940 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1941 * for this functionality, you should use "".
1943 * The application must be registered before calling this function
1944 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1949 g_application_open (GApplication *application,
1954 g_return_if_fail (G_IS_APPLICATION (application));
1955 g_return_if_fail (application->priv->flags &
1956 G_APPLICATION_HANDLES_OPEN);
1957 g_return_if_fail (application->priv->is_registered);
1959 if (application->priv->is_remote)
1960 g_application_impl_open (application->priv->impl,
1961 files, n_files, hint,
1962 get_platform_data (application, NULL));
1965 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1966 0, files, n_files, hint);
1971 * g_application_run:
1972 * @application: a #GApplication
1973 * @argc: the argc from main() (or 0 if @argv is %NULL)
1974 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1976 * Runs the application.
1978 * This function is intended to be run from main() and its return value
1979 * is intended to be returned by main(). Although you are expected to pass
1980 * the @argc, @argv parameters from main() to this function, it is possible
1981 * to pass %NULL if @argv is not available or commandline handling is not
1982 * required. Note that on Windows, @argc and @argv are ignored, and
1983 * g_win32_get_command_line() is called internally (for proper support
1984 * of Unicode commandline arguments).
1986 * #GApplication will attempt to parse the commandline arguments. You
1987 * can add commandline flags to the list of recognised options by way of
1988 * g_application_add_main_option_entries(). After this, the
1989 * #GApplication::handle-local-options signal is emitted, from which the
1990 * application can inspect the values of its #GOptionEntrys.
1992 * #GApplication::handle-local-options is a good place to handle options
1993 * such as <literal>--version</literal>, where an immediate reply from
1994 * the local process is desired (instead of communicating with an
1995 * already-running instance). A #GApplication::handle-local-options
1996 * handler can stop further processing by returning a non-negative
1997 * value, which then becomes the exit status of the process.
1999 * What happens next depends on the flags: if
2000 * %G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining
2001 * commandline arguments are sent to the primary instance, where a
2002 * #GApplication::command-line signal is emitted. Otherwise, the
2003 * remaining commandline arguments are assumed to be a list of files.
2004 * If there are no files listed, the application is activated via the
2005 * #GApplication::activate signal. If there are one or more files, and
2006 * %G_APPLICATION_HANDLES_OPEN was specified then the files are opened
2007 * via the #GApplication::open signal.
2009 * If you are interested in doing more complicated local handling of the
2010 * commandline then you should implement your own #GApplication subclass
2011 * and override local_command_line(). In this case, you most likely want
2012 * to return %TRUE from your local_command_line() implementation to
2013 * suppress the default handling. See
2014 * <xref linkend="gapplication-example-cmdline2"/> for an example.
2016 * If, after the above is done, the use count of the application is zero
2017 * then the exit status is returned immediately. If the use count is
2018 * non-zero then the default main context is iterated until the use count
2019 * falls to zero, at which point 0 is returned.
2021 * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
2022 * run for as much as 10 seconds with a use count of zero while waiting
2023 * for the message that caused the activation to arrive. After that,
2024 * if the use count falls to zero the application will exit immediately,
2025 * except in the case that g_application_set_inactivity_timeout() is in
2028 * This function sets the prgname (g_set_prgname()), if not already set,
2029 * to the basename of argv[0]. Since 2.38, if %G_APPLICATION_IS_SERVICE
2030 * is specified, the prgname is set to the application ID. The main
2031 * impact of this is is that the wmclass of windows created by Gtk+ will
2032 * be set accordingly, which helps the window manager determine which
2033 * application is showing the window.
2035 * Since 2.40, applications that are not explicitly flagged as services
2036 * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
2037 * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
2038 * default handler for local_command_line) if "--gapplication-service"
2039 * was given in the command line. If this flag is present then normal
2040 * commandline processing is interrupted and the
2041 * %G_APPLICATION_IS_SERVICE flag is set. This provides a "compromise"
2042 * solution whereby running an application directly from the commandline
2043 * will invoke it in the normal way (which can be useful for debugging)
2044 * while still allowing applications to be D-Bus activated in service
2045 * mode. The D-Bus service file should invoke the executable with
2046 * "--gapplication-service" as the sole commandline argument. This
2047 * approach is suitable for use by most graphical applications but
2048 * should not be used from applications like editors that need precise
2049 * control over when processes invoked via the commandline will exit and
2050 * what their exit status will be.
2052 * Returns: the exit status
2057 g_application_run (GApplication *application,
2064 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
2065 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
2066 g_return_val_if_fail (!application->priv->must_quit_now, 1);
2069 arguments = g_win32_get_command_line ();
2074 arguments = g_new (gchar *, argc + 1);
2075 for (i = 0; i < argc; i++)
2076 arguments[i] = g_strdup (argv[i]);
2077 arguments[i] = NULL;
2081 if (g_get_prgname () == NULL)
2083 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
2085 g_set_prgname (application->priv->id);
2091 prgname = g_path_get_basename (argv[0]);
2092 g_set_prgname (prgname);
2097 if (!G_APPLICATION_GET_CLASS (application)
2098 ->local_command_line (application, &arguments, &status))
2100 GError *error = NULL;
2102 if (!g_application_register (application, NULL, &error))
2104 g_printerr ("Failed to register: %s\n", error->message);
2105 g_error_free (error);
2109 g_application_call_command_line (application, (const gchar **) arguments, NULL, &status);
2112 g_strfreev (arguments);
2114 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
2115 application->priv->is_registered &&
2116 !application->priv->use_count &&
2117 !application->priv->inactivity_timeout_id)
2119 application->priv->inactivity_timeout_id =
2120 g_timeout_add (10000, inactivity_timeout_expired, application);
2123 while (application->priv->use_count || application->priv->inactivity_timeout_id)
2125 if (application->priv->must_quit_now)
2128 g_main_context_iteration (NULL, TRUE);
2132 if (application->priv->is_registered && !application->priv->is_remote)
2134 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
2136 if (!application->priv->did_shutdown)
2137 g_critical ("GApplication subclass '%s' failed to chain up on"
2138 " ::shutdown (from end of override function)",
2139 G_OBJECT_TYPE_NAME (application));
2142 if (application->priv->impl)
2143 g_application_impl_flush (application->priv->impl);
2151 g_application_list_actions (GActionGroup *action_group)
2153 GApplication *application = G_APPLICATION (action_group);
2155 g_return_val_if_fail (application->priv->is_registered, NULL);
2157 if (application->priv->remote_actions != NULL)
2158 return g_action_group_list_actions (G_ACTION_GROUP (application->priv->remote_actions));
2160 else if (application->priv->actions != NULL)
2161 return g_action_group_list_actions (application->priv->actions);
2164 /* empty string array */
2165 return g_new0 (gchar *, 1);
2169 g_application_query_action (GActionGroup *group,
2170 const gchar *action_name,
2172 const GVariantType **parameter_type,
2173 const GVariantType **state_type,
2174 GVariant **state_hint,
2177 GApplication *application = G_APPLICATION (group);
2179 g_return_val_if_fail (application->priv->is_registered, FALSE);
2181 if (application->priv->remote_actions != NULL)
2182 return g_action_group_query_action (G_ACTION_GROUP (application->priv->remote_actions),
2190 if (application->priv->actions != NULL)
2191 return g_action_group_query_action (application->priv->actions,
2203 g_application_change_action_state (GActionGroup *action_group,
2204 const gchar *action_name,
2207 GApplication *application = G_APPLICATION (action_group);
2209 g_return_if_fail (application->priv->is_remote ||
2210 application->priv->actions != NULL);
2211 g_return_if_fail (application->priv->is_registered);
2213 if (application->priv->remote_actions)
2214 g_remote_action_group_change_action_state_full (application->priv->remote_actions,
2215 action_name, value, get_platform_data (application, NULL));
2218 g_action_group_change_action_state (application->priv->actions, action_name, value);
2222 g_application_activate_action (GActionGroup *action_group,
2223 const gchar *action_name,
2224 GVariant *parameter)
2226 GApplication *application = G_APPLICATION (action_group);
2228 g_return_if_fail (application->priv->is_remote ||
2229 application->priv->actions != NULL);
2230 g_return_if_fail (application->priv->is_registered);
2232 if (application->priv->remote_actions)
2233 g_remote_action_group_activate_action_full (application->priv->remote_actions,
2234 action_name, parameter, get_platform_data (application, NULL));
2237 g_action_group_activate_action (application->priv->actions, action_name, parameter);
2241 g_application_lookup_action (GActionMap *action_map,
2242 const gchar *action_name)
2244 GApplication *application = G_APPLICATION (action_map);
2246 g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
2248 return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
2252 g_application_add_action (GActionMap *action_map,
2255 GApplication *application = G_APPLICATION (action_map);
2257 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2259 g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
2263 g_application_remove_action (GActionMap *action_map,
2264 const gchar *action_name)
2266 GApplication *application = G_APPLICATION (action_map);
2268 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2270 g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
2274 g_application_action_group_iface_init (GActionGroupInterface *iface)
2276 iface->list_actions = g_application_list_actions;
2277 iface->query_action = g_application_query_action;
2278 iface->change_action_state = g_application_change_action_state;
2279 iface->activate_action = g_application_activate_action;
2283 g_application_action_map_iface_init (GActionMapInterface *iface)
2285 iface->lookup_action = g_application_lookup_action;
2286 iface->add_action = g_application_add_action;
2287 iface->remove_action = g_application_remove_action;
2290 /* Default Application {{{1 */
2292 static GApplication *default_app;
2295 * g_application_get_default:
2297 * Returns the default #GApplication instance for this process.
2299 * Normally there is only one #GApplication per process and it becomes
2300 * the default when it is created. You can exercise more control over
2301 * this by using g_application_set_default().
2303 * If there is no default application then %NULL is returned.
2305 * Returns: (transfer none): the default application for this process, or %NULL
2310 g_application_get_default (void)
2316 * g_application_set_default:
2317 * @application: (allow-none): the application to set as default, or %NULL
2319 * Sets or unsets the default application for the process, as returned
2320 * by g_application_get_default().
2322 * This function does not take its own reference on @application. If
2323 * @application is destroyed then the default application will revert
2329 g_application_set_default (GApplication *application)
2331 default_app = application;
2335 * g_application_quit:
2336 * @application: a #GApplication
2338 * Immediately quits the application.
2340 * Upon return to the mainloop, g_application_run() will return,
2341 * calling only the 'shutdown' function before doing so.
2343 * The hold count is ignored.
2345 * The result of calling g_application_run() again after it returns is
2351 g_application_quit (GApplication *application)
2353 g_return_if_fail (G_IS_APPLICATION (application));
2355 application->priv->must_quit_now = TRUE;
2359 * g_application_mark_busy:
2360 * @application: a #GApplication
2362 * Increases the busy count of @application.
2364 * Use this function to indicate that the application is busy, for instance
2365 * while a long running operation is pending.
2367 * The busy state will be exposed to other processes, so a session shell will
2368 * use that information to indicate the state to the user (e.g. with a
2371 * To cancel the busy indication, use g_application_unmark_busy().
2376 g_application_mark_busy (GApplication *application)
2380 g_return_if_fail (G_IS_APPLICATION (application));
2382 was_busy = (application->priv->busy_count > 0);
2383 application->priv->busy_count++;
2386 g_application_impl_set_busy_state (application->priv->impl, TRUE);
2390 * g_application_unmark_busy:
2391 * @application: a #GApplication
2393 * Decreases the busy count of @application.
2395 * When the busy count reaches zero, the new state will be propagated
2396 * to other processes.
2398 * This function must only be called to cancel the effect of a previous
2399 * call to g_application_mark_busy().
2404 g_application_unmark_busy (GApplication *application)
2406 g_return_if_fail (G_IS_APPLICATION (application));
2407 g_return_if_fail (application->priv->busy_count > 0);
2409 application->priv->busy_count--;
2411 if (application->priv->busy_count == 0)
2412 g_application_impl_set_busy_state (application->priv->impl, FALSE);
2415 /* Notifications {{{1 */
2418 * g_application_send_notification:
2419 * @application: a #GApplication
2420 * @id: (allow-none): id of the notification, or %NULL
2421 * @notification: the #GNotification to send
2423 * Sends a notification on behalf of @application to the desktop shell.
2424 * There is no guarantee that the notification is displayed immediately,
2427 * Notifications may persist after the application exits. It will be
2428 * D-Bus-activated when the notification or one of its actions is
2431 * Modifying @notification after this call has no effect. However, the
2432 * object can be reused for a later call to this function.
2434 * @id may be any string that uniquely identifies the event for the
2435 * application. It does not need to be in any special format. For
2436 * example, "new-message" might be appropriate for a notification about
2439 * If a previous notification was sent with the same @id, it will be
2440 * replaced with @notification and shown again as if it was a new
2441 * notification. This works even for notifications sent from a previous
2442 * execution of the application, as long as @id is the same string.
2444 * @id may be %NULL, but it is impossible to replace or withdraw
2445 * notifications without an id.
2447 * If @notification is no longer relevant, it can be withdrawn with
2448 * g_application_withdraw_notification().
2453 g_application_send_notification (GApplication *application,
2455 GNotification *notification)
2457 gchar *generated_id = NULL;
2459 g_return_if_fail (G_IS_APPLICATION (application));
2460 g_return_if_fail (G_IS_NOTIFICATION (notification));
2461 g_return_if_fail (g_application_get_is_registered (application));
2462 g_return_if_fail (!g_application_get_is_remote (application));
2464 if (application->priv->notifications == NULL)
2465 application->priv->notifications = g_notification_backend_new_default (application);
2469 generated_id = g_dbus_generate_guid ();
2473 g_notification_backend_send_notification (application->priv->notifications, id, notification);
2475 g_free (generated_id);
2479 * g_application_withdraw_notification:
2480 * @application: a #GApplication
2481 * @id: id of a previously sent notification
2483 * Withdraws a notification that was sent with
2484 * g_application_send_notification().
2486 * This call does nothing if a notification with @id doesn't exist or
2487 * the notification was never sent.
2489 * This function works even for notifications sent in previous
2490 * executions of this application, as long @id is the same as it was for
2491 * the sent notification.
2493 * Note that notifications are dismissed when the user clicks on one
2494 * of the buttons in a notification or triggers its default action, so
2495 * there is no need to explicitly withdraw the notification in that case.
2500 g_application_withdraw_notification (GApplication *application,
2503 g_return_if_fail (G_IS_APPLICATION (application));
2504 g_return_if_fail (id != NULL);
2506 if (application->priv->notifications)
2507 g_notification_backend_withdraw_notification (application->priv->notifications, id);
2511 /* vim:set foldmethod=marker: */