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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
25 #include "gapplication.h"
27 #include "gapplicationcommandline.h"
28 #include "gsimpleactiongroup.h"
29 #include "gapplicationimpl.h"
30 #include "gactiongroup.h"
31 #include "gactionmap.h"
32 #include "gmenumodel.h"
33 #include "gsettings.h"
35 #include "gioenumtypes.h"
44 * SECTION:gapplication
45 * @title: GApplication
46 * @short_description: Core application class
48 * A #GApplication is the foundation of an application, unique for a
49 * given application identifier. The GApplication class wraps some
50 * low-level platform-specific services and is intended to act as the
51 * foundation for higher-level application classes such as
52 * #GtkApplication or #MxApplication. In general, you should not use
53 * this class outside of a higher level framework.
55 * One of the core features that GApplication provides is process
56 * uniqueness, in the context of a "session". The session concept is
57 * platform-dependent, but corresponds roughly to a graphical desktop
58 * login. When your application is launched again, its arguments
59 * are passed through platform communication to the already running
60 * program. The already running instance of the program is called the
61 * <firstterm>primary instance</firstterm>.
63 * Before using GApplication, you must choose an "application identifier".
64 * The expected form of an application identifier is very close to that of
65 * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
66 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
67 * For details on valid application identifiers, see
68 * g_application_id_is_valid().
70 * The application identifier is claimed by the application as a
71 * well-known bus name on the user's session bus. This means that the
72 * uniqueness of your application is scoped to the current session. It
73 * also means that your application may provide additional services
74 * (through registration of other object paths) at that bus name.
76 * The registration of these object paths should be done with the shared
77 * GDBus session bus. Note that due to the internal architecture of
78 * GDBus, method calls can be dispatched at any time (even if a main
79 * loop is not running). For this reason, you must ensure that any
80 * object paths that you wish to register are registered before
81 * #GApplication attempts to acquire the bus name of your application
82 * (which happens in g_application_register()). Unfortunately, this
83 * means that you cannot use g_application_get_is_remote() to decide if
84 * you want to register object paths.
86 * GApplication provides convenient life cycle management by maintaining
87 * a <firstterm>use count</firstterm> for the primary application instance.
88 * The use count can be changed using g_application_hold() and
89 * g_application_release(). If it drops to zero, the application exits.
91 * GApplication also implements the #GActionGroup and #GActionMap
92 * interfaces and lets you easily export actions by adding them with
93 * g_action_map_add_action(). When invoking an action by calling
94 * g_action_group_activate_action() on the application, it is always
95 * invoked in the primary instance. The actions are also exported on
96 * the session bus, and GIO provides the #GDBusActionGroup wrapper to
97 * conveniently access them remotely. Additionally, g_application_set_app_menu()
98 * and g_application_set_menubar() can be used to export representation
99 * data for the actions, in the form of #GMenuModels.
101 * There is a number of different entry points into a #GApplication:
103 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
104 * <listitem>via 'Open' (i.e. opening some files)</listitem>
105 * <listitem>by handling a command-line</listitem>
106 * <listitem>via activating an action</listitem>
108 * The #GApplication::startup signal lets you handle the application
109 * initialization for all of these in a single place.
111 * Regardless of which of these entry points is used to start the application,
112 * GApplication passes some <firstterm id="platform-data">platform
113 * data</firstterm> from the launching instance to the primary instance,
114 * in the form of a #GVariant dictionary mapping strings to variants.
115 * To use platform data, override the @before_emit or @after_emit virtual
116 * functions in your #GApplication subclass. When dealing with
117 * #GApplicationCommandline objects, the platform data is directly
118 * available via g_application_command_line_get_cwd(),
119 * g_application_command_line_get_environ() and
120 * g_application_command_line_get_platform_data().
122 * As the name indicates, the platform data may vary depending on the
123 * operating system, but it always includes the current directory (key
124 * "cwd"), and optionally the environment (ie the set of environment
125 * variables and their values) of the calling process (key "environ").
126 * The environment is only added to the platform data if the
127 * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
128 * can add their own platform data by overriding the @add_platform_data
129 * virtual function. For instance, #GtkApplication adds startup notification
132 * To parse commandline arguments you may handle the
133 * #GApplication::command-line signal or override the local_command_line()
134 * vfunc, to parse them in either the primary instance or the local instance,
137 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
139 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
140 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
145 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
147 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
148 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
153 * <example id="gapplication-example-menu"><title>A GApplication with menus</title>
155 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-menu.c">
156 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
164 * @startup: invoked on the primary instance immediately after registration
165 * @shutdown: invoked only on the registered primary instance immediately
166 * after the main loop terminates
167 * @activate: invoked on the primary instance when an activation occurs
168 * @open: invoked on the primary instance when there are files to open
169 * @command_line: invoked on the primary instance when a command-line is
170 * not handled locally
171 * @local_command_line: invoked (locally) when the process has been invoked
172 * via commandline execution (as opposed to, say, D-Bus activation - which
173 * is not currently supported by GApplication). The virtual function has
174 * the chance to inspect (and possibly replace) the list of command line
175 * arguments. See g_application_run() for more information.
176 * @before_emit: invoked on the primary instance before 'activate', 'open',
177 * 'command-line' or any action invocation, gets the 'platform data' from
178 * the calling instance
179 * @after_emit: invoked on the primary instance after 'activate', 'open',
180 * 'command-line' or any action invocation, gets the 'platform data' from
181 * the calling instance
182 * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
183 * the primary instance when activating, opening or invoking actions
184 * @quit_mainloop: Used to be invoked on the primary instance when the use
185 * count of the application drops to zero (and after any inactivity
186 * timeout, if requested). Not used anymore since 2.32
187 * @run_mainloop: Used to be invoked on the primary instance from
188 * g_application_run() if the use-count is non-zero. Since 2.32,
189 * GApplication is iterating the main context directly and is not
190 * using @run_mainloop anymore
192 * Virtual function table for #GApplication.
197 struct _GApplicationPrivate
199 GApplicationFlags flags;
202 GActionGroup *actions;
203 GMenuModel *app_menu;
206 guint inactivity_timeout_id;
207 guint inactivity_timeout;
210 guint is_registered : 1;
212 guint did_startup : 1;
213 guint did_shutdown : 1;
215 GActionGroup *remote_actions;
216 GApplicationImpl *impl;
226 PROP_INACTIVITY_TIMEOUT,
243 static guint g_application_signals[NR_SIGNALS];
245 static void g_application_action_group_iface_init (GActionGroupInterface *);
246 static void g_application_action_map_iface_init (GActionMapInterface *);
247 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
248 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
249 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
251 /* vfunc defaults {{{1 */
253 g_application_real_before_emit (GApplication *application,
254 GVariant *platform_data)
259 g_application_real_after_emit (GApplication *application,
260 GVariant *platform_data)
265 g_application_real_startup (GApplication *application)
267 application->priv->did_startup = TRUE;
271 g_application_real_shutdown (GApplication *application)
273 application->priv->did_shutdown = TRUE;
277 g_application_real_activate (GApplication *application)
279 if (!g_signal_has_handler_pending (application,
280 g_application_signals[SIGNAL_ACTIVATE],
282 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
284 static gboolean warned;
289 g_warning ("Your application does not implement "
290 "g_application_activate() and has no handlers connected "
291 "to the 'activate' signal. It should do one of these.");
297 g_application_real_open (GApplication *application,
302 if (!g_signal_has_handler_pending (application,
303 g_application_signals[SIGNAL_OPEN],
305 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
307 static gboolean warned;
312 g_warning ("Your application claims to support opening files "
313 "but does not implement g_application_open() and has no "
314 "handlers connected to the 'open' signal.");
320 g_application_real_command_line (GApplication *application,
321 GApplicationCommandLine *cmdline)
323 if (!g_signal_has_handler_pending (application,
324 g_application_signals[SIGNAL_COMMAND_LINE],
326 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
328 static gboolean warned;
333 g_warning ("Your application claims to support custom command line "
334 "handling but does not implement g_application_command_line() "
335 "and has no handlers connected to the 'command-line' signal.");
344 g_application_real_local_command_line (GApplication *application,
348 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
353 GError *error = NULL;
356 if (!g_application_register (application, NULL, &error))
358 g_critical ("%s", error->message);
359 g_error_free (error);
364 n_args = g_strv_length (*arguments);
366 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
368 if ((*exit_status = n_args > 1))
370 g_printerr ("GApplication service mode takes no arguments.\n");
371 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
379 g_application_activate (application);
385 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
387 g_critical ("This application can not open files.");
396 n_files = n_args - 1;
397 files = g_new (GFile *, n_files);
399 for (i = 0; i < n_files; i++)
400 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
402 g_application_open (application, files, n_files, "");
404 for (i = 0; i < n_files; i++)
405 g_object_unref (files[i]);
417 g_application_real_add_platform_data (GApplication *application,
418 GVariantBuilder *builder)
422 /* GObject implementation stuff {{{1 */
424 g_application_set_property (GObject *object,
429 GApplication *application = G_APPLICATION (object);
433 case PROP_APPLICATION_ID:
434 g_application_set_application_id (application,
435 g_value_get_string (value));
439 g_application_set_flags (application, g_value_get_flags (value));
442 case PROP_INACTIVITY_TIMEOUT:
443 g_application_set_inactivity_timeout (application,
444 g_value_get_uint (value));
447 case PROP_ACTION_GROUP:
448 g_clear_object (&application->priv->actions);
449 application->priv->actions = g_value_dup_object (value);
453 g_application_set_app_menu (application, g_value_get_object (value));
457 g_application_set_menubar (application, g_value_get_object (value));
461 g_assert_not_reached ();
466 * g_application_set_action_group:
467 * @application: a #GApplication
468 * @action_group: (allow-none): a #GActionGroup, or %NULL
470 * This used to be how actions were associated with a #GApplication.
471 * Now there is #GActionMap for that.
475 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
476 * mix use of this API with use of #GActionMap on the same @application
477 * or things will go very badly wrong.
480 g_application_set_action_group (GApplication *application,
481 GActionGroup *action_group)
483 g_return_if_fail (G_IS_APPLICATION (application));
484 g_return_if_fail (!application->priv->is_registered);
486 if (application->priv->actions != NULL)
487 g_object_unref (application->priv->actions);
489 application->priv->actions = action_group;
491 if (application->priv->actions != NULL)
492 g_object_ref (application->priv->actions);
496 * g_application_set_app_menu:
497 * @application: a #GApplication
498 * @app_menu: (allow-none): a #GMenuModel, or %NULL
500 * Sets or unsets the application menu for @application.
502 * The application menu is a single menu containing items that typically
503 * impact the application as a whole, rather than acting on a specific
504 * window or document. For example, you would expect to see
505 * "Preferences" or "Quit" in an application menu, but not "Save" or
508 * If supported, the application menu will be rendered by the desktop
514 g_application_set_app_menu (GApplication *application,
515 GMenuModel *app_menu)
517 g_return_if_fail (G_IS_APPLICATION (application));
519 if (app_menu != application->priv->app_menu)
521 if (application->priv->app_menu != NULL)
522 g_object_unref (application->priv->app_menu);
524 application->priv->app_menu = app_menu;
526 if (application->priv->app_menu != NULL)
527 g_object_ref (application->priv->app_menu);
529 g_object_notify (G_OBJECT (application), "app-menu");
534 * g_application_get_app_menu:
535 * @application: a #GApplication
537 * Returns the menu model that has been set with
538 * g_application_set_app_menu().
540 * Returns: the application menu of @application
545 g_application_get_app_menu (GApplication *application)
547 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
549 return application->priv->app_menu;
553 * g_application_set_menubar:
554 * @application: a #GApplication
555 * @menubar: (allow-none): a #GMenuModel, or %NULL
557 * Sets or unsets the menubar for windows of @application.
559 * This is a menubar in the traditional sense.
561 * Depending on the desktop environment, this may appear at the top of
562 * each window, or at the top of the screen. In some environments, if
563 * both the application menu and the menubar are set, the application
564 * menu will be presented as if it were the first item of the menubar.
565 * Other environments treat the two as completely separate -- for
566 * example, the application menu may be rendered by the desktop shell
567 * while the menubar (if set) remains in each individual window.
572 g_application_set_menubar (GApplication *application,
575 g_return_if_fail (G_IS_APPLICATION (application));
577 if (menubar != application->priv->menubar)
579 if (application->priv->menubar != NULL)
580 g_object_unref (application->priv->menubar);
582 application->priv->menubar = menubar;
584 if (application->priv->menubar != NULL)
585 g_object_ref (application->priv->menubar);
587 g_object_notify (G_OBJECT (application), "menubar");
592 * g_application_get_menubar:
593 * @application: a #GApplication
595 * Returns the menu model that has been set with
596 * g_application_set_menubar().
598 * Returns: the menubar for windows of @application
603 g_application_get_menubar (GApplication *application)
605 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
607 return application->priv->menubar;
611 g_application_get_property (GObject *object,
616 GApplication *application = G_APPLICATION (object);
620 case PROP_APPLICATION_ID:
621 g_value_set_string (value,
622 g_application_get_application_id (application));
626 g_value_set_flags (value,
627 g_application_get_flags (application));
630 case PROP_IS_REGISTERED:
631 g_value_set_boolean (value,
632 g_application_get_is_registered (application));
636 g_value_set_boolean (value,
637 g_application_get_is_remote (application));
640 case PROP_INACTIVITY_TIMEOUT:
641 g_value_set_uint (value,
642 g_application_get_inactivity_timeout (application));
646 g_value_set_object (value,
647 g_application_get_app_menu (application));
651 g_value_set_object (value,
652 g_application_get_menubar (application));
656 g_assert_not_reached ();
661 g_application_constructed (GObject *object)
663 GApplication *application = G_APPLICATION (object);
665 g_assert (application->priv->id != NULL);
667 if (g_application_get_default () == NULL)
668 g_application_set_default (application);
672 g_application_finalize (GObject *object)
674 GApplication *application = G_APPLICATION (object);
676 if (application->priv->impl)
677 g_application_impl_destroy (application->priv->impl);
678 g_free (application->priv->id);
680 if (g_application_get_default () == application)
681 g_application_set_default (NULL);
683 if (application->priv->app_menu)
684 g_object_unref (application->priv->app_menu);
686 if (application->priv->menubar)
687 g_object_unref (application->priv->menubar);
689 if (application->priv->actions)
690 g_object_unref (application->priv->actions);
692 G_OBJECT_CLASS (g_application_parent_class)
697 g_application_init (GApplication *application)
699 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
701 GApplicationPrivate);
702 application->priv->actions = G_ACTION_GROUP (g_simple_action_group_new ());
706 g_application_class_init (GApplicationClass *class)
708 GObjectClass *object_class = G_OBJECT_CLASS (class);
710 object_class->constructed = g_application_constructed;
711 object_class->finalize = g_application_finalize;
712 object_class->get_property = g_application_get_property;
713 object_class->set_property = g_application_set_property;
715 class->before_emit = g_application_real_before_emit;
716 class->after_emit = g_application_real_after_emit;
717 class->startup = g_application_real_startup;
718 class->shutdown = g_application_real_shutdown;
719 class->activate = g_application_real_activate;
720 class->open = g_application_real_open;
721 class->command_line = g_application_real_command_line;
722 class->local_command_line = g_application_real_local_command_line;
723 class->add_platform_data = g_application_real_add_platform_data;
725 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
726 g_param_spec_string ("application-id",
727 P_("Application identifier"),
728 P_("The unique identifier for the application"),
729 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
730 G_PARAM_STATIC_STRINGS));
732 g_object_class_install_property (object_class, PROP_FLAGS,
733 g_param_spec_flags ("flags",
734 P_("Application flags"),
735 P_("Flags specifying the behaviour of the application"),
736 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
737 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
739 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
740 g_param_spec_boolean ("is-registered",
742 P_("If g_application_register() has been called"),
743 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
745 g_object_class_install_property (object_class, PROP_IS_REMOTE,
746 g_param_spec_boolean ("is-remote",
748 P_("If this application instance is remote"),
749 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
751 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
752 g_param_spec_uint ("inactivity-timeout",
753 P_("Inactivity timeout"),
754 P_("Time (ms) to stay alive after becoming idle"),
756 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
758 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
759 g_param_spec_object ("action-group",
761 P_("The group of actions that the application exports"),
763 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
765 g_object_class_install_property (object_class, PROP_APP_MENU,
766 g_param_spec_object ("app-menu",
767 P_("Application menu"),
768 P_("The GMenuModel for the application menu"),
770 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
772 g_object_class_install_property (object_class, PROP_MENUBAR,
773 g_param_spec_object ("menubar",
775 P_("The GMenuModel for the menubar"),
777 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
780 * GApplication::startup:
781 * @application: the application
783 * The ::startup signal is emitted on the primary instance immediately
784 * after registration. See g_application_register().
786 g_application_signals[SIGNAL_STARTUP] =
787 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
788 G_STRUCT_OFFSET (GApplicationClass, startup),
789 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
792 * GApplication::shutdown:
793 * @application: the application
795 * The ::shutdown signal is emitted only on the registered primary instance
796 * immediately after the main loop terminates.
798 g_application_signals[SIGNAL_SHUTDOWN] =
799 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
800 G_STRUCT_OFFSET (GApplicationClass, shutdown),
801 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
804 * GApplication::activate:
805 * @application: the application
807 * The ::activate signal is emitted on the primary instance when an
808 * activation occurs. See g_application_activate().
810 g_application_signals[SIGNAL_ACTIVATE] =
811 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
812 G_STRUCT_OFFSET (GApplicationClass, activate),
813 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
817 * GApplication::open:
818 * @application: the application
819 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
820 * @n_files: the length of @files
821 * @hint: a hint provided by the calling instance
823 * The ::open signal is emitted on the primary instance when there are
824 * files to open. See g_application_open() for more information.
826 g_application_signals[SIGNAL_OPEN] =
827 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
828 G_STRUCT_OFFSET (GApplicationClass, open),
830 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
833 * GApplication::command-line:
834 * @application: the application
835 * @command_line: a #GApplicationCommandLine representing the
838 * The ::command-line signal is emitted on the primary instance when
839 * a commandline is not handled locally. See g_application_run() and
840 * the #GApplicationCommandline documentation for more information.
842 * Returns: An integer that is set as the exit status for the calling
843 * process. See g_application_command_line_set_exit_status().
845 g_application_signals[SIGNAL_COMMAND_LINE] =
846 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
847 G_STRUCT_OFFSET (GApplicationClass, command_line),
848 g_signal_accumulator_first_wins, NULL,
850 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
852 g_type_class_add_private (class, sizeof (GApplicationPrivate));
856 get_platform_data (GApplication *application)
858 GVariantBuilder *builder;
861 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
864 gchar *cwd = g_get_current_dir ();
865 g_variant_builder_add (builder, "{sv}", "cwd",
866 g_variant_new_bytestring (cwd));
870 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
875 envp = g_get_environ ();
876 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
879 g_variant_builder_add (builder, "{sv}", "environ", array);
882 G_APPLICATION_GET_CLASS (application)->
883 add_platform_data (application, builder);
885 result = g_variant_builder_end (builder);
886 g_variant_builder_unref (builder);
891 /* Application ID validity {{{1 */
894 * g_application_id_is_valid:
895 * @application_id: a potential application identifier
897 * Checks if @application_id is a valid application identifier.
899 * A valid ID is required for calls to g_application_new() and
900 * g_application_set_application_id().
902 * For convenience, the restrictions on application identifiers are
905 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
906 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
907 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
908 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
909 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
912 * Returns: %TRUE if @application_id is valid
915 g_application_id_is_valid (const gchar *application_id)
921 len = strlen (application_id);
926 if (!g_ascii_isalpha (application_id[0]))
929 if (application_id[len-1] == '.')
935 for (; *application_id; application_id++)
937 if (g_ascii_isalnum (*application_id) ||
938 (*application_id == '-') ||
939 (*application_id == '_'))
943 else if (allow_dot && *application_id == '.')
958 /* Public Constructor {{{1 */
961 * @application_id: the application id
962 * @flags: the application flags
964 * Creates a new #GApplication instance.
966 * This function calls g_type_init() for you.
968 * The application id must be valid. See g_application_id_is_valid().
970 * Returns: a new #GApplication instance
973 g_application_new (const gchar *application_id,
974 GApplicationFlags flags)
976 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
980 return g_object_new (G_TYPE_APPLICATION,
981 "application-id", application_id,
986 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
988 * g_application_get_application_id:
989 * @application: a #GApplication
991 * Gets the unique identifier for @application.
993 * Returns: the identifier for @application, owned by @application
998 g_application_get_application_id (GApplication *application)
1000 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1002 return application->priv->id;
1006 * g_application_set_application_id:
1007 * @application: a #GApplication
1008 * @application_id: the identifier for @application
1010 * Sets the unique identifier for @application.
1012 * The application id can only be modified if @application has not yet
1015 * The application id must be valid. See g_application_id_is_valid().
1020 g_application_set_application_id (GApplication *application,
1021 const gchar *application_id)
1023 g_return_if_fail (G_IS_APPLICATION (application));
1025 if (g_strcmp0 (application->priv->id, application_id) != 0)
1027 g_return_if_fail (g_application_id_is_valid (application_id));
1028 g_return_if_fail (!application->priv->is_registered);
1030 g_free (application->priv->id);
1031 application->priv->id = g_strdup (application_id);
1033 g_object_notify (G_OBJECT (application), "application-id");
1038 * g_application_get_flags:
1039 * @application: a #GApplication
1041 * Gets the flags for @application.
1043 * See #GApplicationFlags.
1045 * Returns: the flags for @application
1050 g_application_get_flags (GApplication *application)
1052 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1054 return application->priv->flags;
1058 * g_application_set_flags:
1059 * @application: a #GApplication
1060 * @flags: the flags for @application
1062 * Sets the flags for @application.
1064 * The flags can only be modified if @application has not yet been
1067 * See #GApplicationFlags.
1072 g_application_set_flags (GApplication *application,
1073 GApplicationFlags flags)
1075 g_return_if_fail (G_IS_APPLICATION (application));
1077 if (application->priv->flags != flags)
1079 g_return_if_fail (!application->priv->is_registered);
1081 application->priv->flags = flags;
1083 g_object_notify (G_OBJECT (application), "flags");
1088 * g_application_get_inactivity_timeout:
1089 * @application: a #GApplication
1091 * Gets the current inactivity timeout for the application.
1093 * This is the amount of time (in milliseconds) after the last call to
1094 * g_application_release() before the application stops running.
1096 * Returns: the timeout, in milliseconds
1101 g_application_get_inactivity_timeout (GApplication *application)
1103 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1105 return application->priv->inactivity_timeout;
1109 * g_application_set_inactivity_timeout:
1110 * @application: a #GApplication
1111 * @inactivity_timeout: the timeout, in milliseconds
1113 * Sets the current inactivity timeout for the application.
1115 * This is the amount of time (in milliseconds) after the last call to
1116 * g_application_release() before the application stops running.
1118 * This call has no side effects of its own. The value set here is only
1119 * used for next time g_application_release() drops the use count to
1120 * zero. Any timeouts currently in progress are not impacted.
1125 g_application_set_inactivity_timeout (GApplication *application,
1126 guint inactivity_timeout)
1128 g_return_if_fail (G_IS_APPLICATION (application));
1130 if (application->priv->inactivity_timeout != inactivity_timeout)
1132 application->priv->inactivity_timeout = inactivity_timeout;
1134 g_object_notify (G_OBJECT (application), "inactivity-timeout");
1137 /* Read-only property getters (is registered, is remote) {{{1 */
1139 * g_application_get_is_registered:
1140 * @application: a #GApplication
1142 * Checks if @application is registered.
1144 * An application is registered if g_application_register() has been
1145 * successfully called.
1147 * Returns: %TRUE if @application is registered
1152 g_application_get_is_registered (GApplication *application)
1154 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1156 return application->priv->is_registered;
1160 * g_application_get_is_remote:
1161 * @application: a #GApplication
1163 * Checks if @application is remote.
1165 * If @application is remote then it means that another instance of
1166 * application already exists (the 'primary' instance). Calls to
1167 * perform actions on @application will result in the actions being
1168 * performed by the primary instance.
1170 * The value of this property cannot be accessed before
1171 * g_application_register() has been called. See
1172 * g_application_get_is_registered().
1174 * Returns: %TRUE if @application is remote
1179 g_application_get_is_remote (GApplication *application)
1181 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1182 g_return_val_if_fail (application->priv->is_registered, FALSE);
1184 return application->priv->is_remote;
1189 * g_application_register:
1190 * @application: a #GApplication
1191 * @cancellable: a #GCancellable, or %NULL
1192 * @error: a pointer to a NULL #GError, or %NULL
1194 * Attempts registration of the application.
1196 * This is the point at which the application discovers if it is the
1197 * primary instance or merely acting as a remote for an already-existing
1198 * primary instance. This is implemented by attempting to acquire the
1199 * application identifier as a unique bus name on the session bus using
1202 * Due to the internal architecture of GDBus, method calls can be
1203 * dispatched at any time (even if a main loop is not running). For
1204 * this reason, you must ensure that any object paths that you wish to
1205 * register are registered before calling this function.
1207 * If the application has already been registered then %TRUE is
1208 * returned with no work performed.
1210 * The #GApplication::startup signal is emitted if registration succeeds
1211 * and @application is the primary instance.
1213 * In the event of an error (such as @cancellable being cancelled, or a
1214 * failure to connect to the session bus), %FALSE is returned and @error
1215 * is set appropriately.
1217 * Note: the return value of this function is not an indicator that this
1218 * instance is or is not the primary instance of the application. See
1219 * g_application_get_is_remote() for that.
1221 * Returns: %TRUE if registration succeeded
1226 g_application_register (GApplication *application,
1227 GCancellable *cancellable,
1230 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1232 if (!application->priv->is_registered)
1234 if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1236 application->priv->impl =
1237 g_application_impl_register (application, application->priv->id,
1238 application->priv->flags,
1239 &application->priv->remote_actions,
1240 cancellable, error);
1242 if (application->priv->impl == NULL)
1246 application->priv->is_remote = application->priv->remote_actions != NULL;
1247 application->priv->is_registered = TRUE;
1249 g_object_notify (G_OBJECT (application), "is-registered");
1251 if (!application->priv->is_remote)
1253 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1255 if (!application->priv->did_startup)
1256 g_critical ("GApplication subclass '%s' failed to chain up on"
1257 " ::startup (from start of override function)",
1258 G_OBJECT_TYPE_NAME (application));
1265 /* Hold/release {{{1 */
1267 * g_application_hold:
1268 * @application: a #GApplication
1270 * Increases the use count of @application.
1272 * Use this function to indicate that the application has a reason to
1273 * continue to run. For example, g_application_hold() is called by GTK+
1274 * when a toplevel window is on the screen.
1276 * To cancel the hold, call g_application_release().
1279 g_application_hold (GApplication *application)
1281 if (application->priv->inactivity_timeout_id)
1283 g_source_remove (application->priv->inactivity_timeout_id);
1284 application->priv->inactivity_timeout_id = 0;
1287 application->priv->use_count++;
1291 inactivity_timeout_expired (gpointer data)
1293 GApplication *application = G_APPLICATION (data);
1295 application->priv->inactivity_timeout_id = 0;
1302 * g_application_release:
1303 * @application: a #GApplication
1305 * Decrease the use count of @application.
1307 * When the use count reaches zero, the application will stop running.
1309 * Never call this function except to cancel the effect of a previous
1310 * call to g_application_hold().
1313 g_application_release (GApplication *application)
1315 application->priv->use_count--;
1317 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1318 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1319 inactivity_timeout_expired, application);
1322 /* Activate, Open {{{1 */
1324 * g_application_activate:
1325 * @application: a #GApplication
1327 * Activates the application.
1329 * In essence, this results in the #GApplication::activate() signal being
1330 * emitted in the primary instance.
1332 * The application must be registered before calling this function.
1337 g_application_activate (GApplication *application)
1339 g_return_if_fail (G_IS_APPLICATION (application));
1340 g_return_if_fail (application->priv->is_registered);
1342 if (application->priv->is_remote)
1343 g_application_impl_activate (application->priv->impl,
1344 get_platform_data (application));
1347 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1351 * g_application_open:
1352 * @application: a #GApplication
1353 * @files: (array length=n_files): an array of #GFiles to open
1354 * @n_files: the length of the @files array
1355 * @hint: a hint (or ""), but never %NULL
1357 * Opens the given files.
1359 * In essence, this results in the #GApplication::open signal being emitted
1360 * in the primary instance.
1362 * @n_files must be greater than zero.
1364 * @hint is simply passed through to the ::open signal. It is
1365 * intended to be used by applications that have multiple modes for
1366 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1367 * for this functionality, you should use "".
1369 * The application must be registered before calling this function
1370 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1375 g_application_open (GApplication *application,
1380 g_return_if_fail (G_IS_APPLICATION (application));
1381 g_return_if_fail (application->priv->flags &
1382 G_APPLICATION_HANDLES_OPEN);
1383 g_return_if_fail (application->priv->is_registered);
1385 if (application->priv->is_remote)
1386 g_application_impl_open (application->priv->impl,
1387 files, n_files, hint,
1388 get_platform_data (application));
1391 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1392 0, files, n_files, hint);
1397 * g_application_run:
1398 * @application: a #GApplication
1399 * @argc: the argc from main() (or 0 if @argv is %NULL)
1400 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1402 * Runs the application.
1404 * This function is intended to be run from main() and its return value
1405 * is intended to be returned by main(). Although you are expected to pass
1406 * the @argc, @argv parameters from main() to this function, it is possible
1407 * to pass %NULL if @argv is not available or commandline handling is not
1410 * First, the local_command_line() virtual function is invoked.
1411 * This function always runs on the local instance. It gets passed a pointer
1412 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1413 * that it handled (shifting up remaining arguments). See
1414 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1415 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1416 * after setting <literal>argc = g_strv_length (argv);</literal>.
1418 * The last argument to local_command_line() is a pointer to the @status
1419 * variable which can used to set the exit status that is returned from
1420 * g_application_run().
1422 * If local_command_line() returns %TRUE, the command line is expected
1423 * to be completely handled, including possibly registering as the primary
1424 * instance, calling g_application_activate() or g_application_open(), etc.
1426 * If local_command_line() returns %FALSE then the application is registered
1427 * and the #GApplication::command-line signal is emitted in the primary
1428 * instance (which may or may not be this instance). The signal handler
1429 * gets passed a #GApplicationCommandline object that (among other things)
1430 * contains the remaining commandline arguments that have not been handled
1431 * by local_command_line().
1433 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1434 * flag set then the default implementation of local_command_line()
1435 * always returns %FALSE immediately, resulting in the commandline
1436 * always being handled in the primary instance.
1438 * Otherwise, the default implementation of local_command_line() tries
1439 * to do a couple of things that are probably reasonable for most
1440 * applications. First, g_application_register() is called to attempt
1441 * to register the application. If that works, then the command line
1442 * arguments are inspected. If no commandline arguments are given, then
1443 * g_application_activate() is called. If commandline arguments are
1444 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1445 * are assumed to be filenames and g_application_open() is called.
1447 * If you need to handle commandline arguments that are not filenames,
1448 * and you don't mind commandline handling to happen in the primary
1449 * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1450 * process the commandline arguments in your #GApplication::command-line
1451 * signal handler, either manually or using the #GOptionContext API.
1453 * If you are interested in doing more complicated local handling of the
1454 * commandline then you should implement your own #GApplication subclass
1455 * and override local_command_line(). In this case, you most likely want
1456 * to return %TRUE from your local_command_line() implementation to
1457 * suppress the default handling. See
1458 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1460 * If, after the above is done, the use count of the application is zero
1461 * then the exit status is returned immediately. If the use count is
1462 * non-zero then the default main context is iterated until the use count
1463 * falls to zero, at which point 0 is returned.
1465 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1466 * use count of zero is delayed for a while (ie: the instance stays
1467 * around to provide its <emphasis>service</emphasis> to others).
1469 * Returns: the exit status
1474 g_application_run (GApplication *application,
1482 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1483 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1485 arguments = g_new (gchar *, argc + 1);
1486 for (i = 0; i < argc; i++)
1487 arguments[i] = g_strdup (argv[i]);
1488 arguments[i] = NULL;
1490 if (g_get_prgname () == NULL && argc > 0)
1494 prgname = g_path_get_basename (argv[0]);
1495 g_set_prgname (prgname);
1499 if (!G_APPLICATION_GET_CLASS (application)
1500 ->local_command_line (application, &arguments, &status))
1502 GError *error = NULL;
1504 if (!g_application_register (application, NULL, &error))
1506 g_printerr ("%s", error->message);
1507 g_error_free (error);
1511 if (application->priv->is_remote)
1513 GVariant *platform_data;
1515 platform_data = get_platform_data (application);
1516 status = g_application_impl_command_line (application->priv->impl,
1517 arguments, platform_data);
1521 GApplicationCommandLine *cmdline;
1524 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1525 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1526 "arguments", v, NULL);
1527 g_signal_emit (application,
1528 g_application_signals[SIGNAL_COMMAND_LINE],
1529 0, cmdline, &status);
1530 g_object_unref (cmdline);
1534 g_strfreev (arguments);
1536 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1537 application->priv->is_registered &&
1538 !application->priv->use_count &&
1539 !application->priv->inactivity_timeout_id)
1541 application->priv->inactivity_timeout_id =
1542 g_timeout_add (10000, inactivity_timeout_expired, application);
1545 while (application->priv->use_count || application->priv->inactivity_timeout_id)
1547 g_main_context_iteration (NULL, TRUE);
1551 if (!application->priv->is_remote)
1553 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1555 if (!application->priv->did_shutdown)
1556 g_critical ("GApplication subclass '%s' failed to chain up on"
1557 " ::shutdown (from end of override function)",
1558 G_OBJECT_TYPE_NAME (application));
1561 if (application->priv->impl)
1562 g_application_impl_flush (application->priv->impl);
1570 g_application_list_actions (GActionGroup *action_group)
1572 GApplication *application = G_APPLICATION (action_group);
1574 g_return_val_if_fail (application->priv->is_registered, NULL);
1576 if (application->priv->remote_actions != NULL)
1577 return g_action_group_list_actions (application->priv->remote_actions);
1579 else if (application->priv->actions != NULL)
1580 return g_action_group_list_actions (application->priv->actions);
1583 /* empty string array */
1584 return g_new0 (gchar *, 1);
1588 g_application_query_action (GActionGroup *group,
1589 const gchar *action_name,
1591 const GVariantType **parameter_type,
1592 const GVariantType **state_type,
1593 GVariant **state_hint,
1596 GApplication *application = G_APPLICATION (group);
1598 g_return_val_if_fail (application->priv->is_registered, FALSE);
1600 if (application->priv->remote_actions != NULL)
1601 return g_action_group_query_action (application->priv->remote_actions,
1609 if (application->priv->actions != NULL)
1610 return g_action_group_query_action (application->priv->actions,
1622 g_application_change_action_state (GActionGroup *action_group,
1623 const gchar *action_name,
1626 GApplication *application = G_APPLICATION (action_group);
1628 g_return_if_fail (application->priv->is_remote ||
1629 application->priv->actions != NULL);
1630 g_return_if_fail (application->priv->is_registered);
1632 if (application->priv->remote_actions)
1633 return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
1636 g_action_group_change_action_state (application->priv->actions,
1637 action_name, value);
1641 g_application_activate_action (GActionGroup *action_group,
1642 const gchar *action_name,
1643 GVariant *parameter)
1645 GApplication *application = G_APPLICATION (action_group);
1647 g_return_if_fail (application->priv->is_remote ||
1648 application->priv->actions != NULL);
1649 g_return_if_fail (application->priv->is_registered);
1651 if (application->priv->remote_actions)
1652 return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
1655 g_action_group_activate_action (application->priv->actions,
1656 action_name, parameter);
1660 g_application_lookup_action (GActionMap *action_map,
1661 const gchar *action_name)
1663 GApplication *application = G_APPLICATION (action_map);
1665 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions), NULL);
1667 return g_simple_action_group_lookup (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1671 g_application_add_action (GActionMap *action_map,
1674 GApplication *application = G_APPLICATION (action_map);
1676 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1678 g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (application->priv->actions), action);
1682 g_application_remove_action (GActionMap *action_map,
1683 const gchar *action_name)
1685 GApplication *application = G_APPLICATION (action_map);
1687 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1689 g_simple_action_group_remove (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1693 g_application_action_group_iface_init (GActionGroupInterface *iface)
1695 iface->list_actions = g_application_list_actions;
1696 iface->query_action = g_application_query_action;
1697 iface->change_action_state = g_application_change_action_state;
1698 iface->activate_action = g_application_activate_action;
1702 g_application_action_map_iface_init (GActionMapInterface *iface)
1704 iface->lookup_action = g_application_lookup_action;
1705 iface->add_action = g_application_add_action;
1706 iface->remove_action = g_application_remove_action;
1709 /* Default Application {{{1 */
1711 static GApplication *default_app;
1714 * g_application_get_default:
1715 * @returns: (transfer none): the default application for this process, or %NULL
1717 * Returns the default #GApplication instance for this process.
1719 * Normally there is only one #GApplication per process and it becomes
1720 * the default when it is created. You can exercise more control over
1721 * this by using g_application_set_default().
1723 * If there is no default application then %NULL is returned.
1728 g_application_get_default (void)
1734 * g_application_set_default:
1735 * @application: the application to set as default, or %NULL
1737 * Sets or unsets the default application for the process, as returned
1738 * by g_application_get_default().
1740 * This function does not take its own reference on @application. If
1741 * @application is destroyed then the default application will revert
1747 g_application_set_default (GApplication *application)
1749 default_app = application;
1753 /* vim:set foldmethod=marker: */