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>
162 struct _GApplicationPrivate
164 GApplicationFlags flags;
167 GActionGroup *actions;
168 GMenuModel *app_menu;
171 guint inactivity_timeout_id;
172 guint inactivity_timeout;
175 guint is_registered : 1;
177 guint did_startup : 1;
178 guint did_shutdown : 1;
180 GActionGroup *remote_actions;
181 GApplicationImpl *impl;
191 PROP_INACTIVITY_TIMEOUT,
208 static guint g_application_signals[NR_SIGNALS];
210 static void g_application_action_group_iface_init (GActionGroupInterface *);
211 static void g_application_action_map_iface_init (GActionMapInterface *);
212 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
213 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
214 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
216 /* vfunc defaults {{{1 */
218 g_application_real_before_emit (GApplication *application,
219 GVariant *platform_data)
224 g_application_real_after_emit (GApplication *application,
225 GVariant *platform_data)
230 g_application_real_startup (GApplication *application)
232 application->priv->did_startup = TRUE;
236 g_application_real_shutdown (GApplication *application)
238 application->priv->did_shutdown = TRUE;
242 g_application_real_activate (GApplication *application)
244 if (!g_signal_has_handler_pending (application,
245 g_application_signals[SIGNAL_ACTIVATE],
247 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
249 static gboolean warned;
254 g_warning ("Your application does not implement "
255 "g_application_activate() and has no handlers connected "
256 "to the 'activate' signal. It should do one of these.");
262 g_application_real_open (GApplication *application,
267 if (!g_signal_has_handler_pending (application,
268 g_application_signals[SIGNAL_OPEN],
270 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
272 static gboolean warned;
277 g_warning ("Your application claims to support opening files "
278 "but does not implement g_application_open() and has no "
279 "handlers connected to the 'open' signal.");
285 g_application_real_command_line (GApplication *application,
286 GApplicationCommandLine *cmdline)
288 if (!g_signal_has_handler_pending (application,
289 g_application_signals[SIGNAL_COMMAND_LINE],
291 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
293 static gboolean warned;
298 g_warning ("Your application claims to support custom command line "
299 "handling but does not implement g_application_command_line() "
300 "and has no handlers connected to the 'command-line' signal.");
309 g_application_real_local_command_line (GApplication *application,
313 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
318 GError *error = NULL;
321 if (!g_application_register (application, NULL, &error))
323 g_critical ("%s", error->message);
324 g_error_free (error);
329 n_args = g_strv_length (*arguments);
331 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
333 if ((*exit_status = n_args > 1))
335 g_printerr ("GApplication service mode takes no arguments.\n");
336 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
344 g_application_activate (application);
350 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
352 g_critical ("This application can not open files.");
361 n_files = n_args - 1;
362 files = g_new (GFile *, n_files);
364 for (i = 0; i < n_files; i++)
365 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
367 g_application_open (application, files, n_files, "");
369 for (i = 0; i < n_files; i++)
370 g_object_unref (files[i]);
382 g_application_real_add_platform_data (GApplication *application,
383 GVariantBuilder *builder)
387 /* GObject implementation stuff {{{1 */
389 g_application_set_property (GObject *object,
394 GApplication *application = G_APPLICATION (object);
398 case PROP_APPLICATION_ID:
399 g_application_set_application_id (application,
400 g_value_get_string (value));
404 g_application_set_flags (application, g_value_get_flags (value));
407 case PROP_INACTIVITY_TIMEOUT:
408 g_application_set_inactivity_timeout (application,
409 g_value_get_uint (value));
412 case PROP_ACTION_GROUP:
413 g_clear_object (&application->priv->actions);
414 application->priv->actions = g_value_dup_object (value);
418 g_application_set_app_menu (application, g_value_get_object (value));
422 g_application_set_menubar (application, g_value_get_object (value));
426 g_assert_not_reached ();
431 * g_application_set_action_group:
432 * @application: a #GApplication
433 * @action_group: (allow-none): a #GActionGroup, or %NULL
435 * This used to be how actions were associated with a #GApplication.
436 * Now there is #GActionMap for that.
440 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
441 * mix use of this API with use of #GActionMap on the same @application
442 * or things will go very badly wrong.
445 g_application_set_action_group (GApplication *application,
446 GActionGroup *action_group)
448 g_return_if_fail (G_IS_APPLICATION (application));
449 g_return_if_fail (!application->priv->is_registered);
451 if (application->priv->actions != NULL)
452 g_object_unref (application->priv->actions);
454 application->priv->actions = action_group;
456 if (application->priv->actions != NULL)
457 g_object_ref (application->priv->actions);
461 * g_application_set_app_menu:
462 * @application: a #GApplication
463 * @app_menu: (allow-none): a #GMenuModel, or %NULL
465 * Sets or unsets the application menu for @application.
467 * The application menu is a single menu containing items that typically
468 * impact the application as a whole, rather than acting on a specific
469 * window or document. For example, you would expect to see
470 * "Preferences" or "Quit" in an application menu, but not "Save" or
473 * If supported, the application menu will be rendered by the desktop
479 g_application_set_app_menu (GApplication *application,
480 GMenuModel *app_menu)
482 g_return_if_fail (G_IS_APPLICATION (application));
484 if (app_menu != application->priv->app_menu)
486 if (application->priv->app_menu != NULL)
487 g_object_unref (application->priv->app_menu);
489 application->priv->app_menu = app_menu;
491 if (application->priv->app_menu != NULL)
492 g_object_ref (application->priv->app_menu);
494 g_object_notify (G_OBJECT (application), "app-menu");
499 * g_application_get_app_menu:
500 * @application: a #GApplication
502 * Returns the menu model that has been set with
503 * g_application_set_app_menu().
505 * Returns: the application menu of @application
510 g_application_get_app_menu (GApplication *application)
512 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
514 return application->priv->app_menu;
518 * g_application_set_menubar:
519 * @application: a #GApplication
520 * @menubar: (allow-none): a #GMenuModel, or %NULL
522 * Sets or unsets the menubar for windows of @application.
524 * This is a menubar in the traditional sense.
526 * Depending on the desktop environment, this may appear at the top of
527 * each window, or at the top of the screen. In some environments, if
528 * both the application menu and the menubar are set, the application
529 * menu will be presented as if it were the first item of the menubar.
530 * Other environments treat the two as completely separate -- for
531 * example, the application menu may be rendered by the desktop shell
532 * while the menubar (if set) remains in each individual window.
537 g_application_set_menubar (GApplication *application,
540 g_return_if_fail (G_IS_APPLICATION (application));
542 if (menubar != application->priv->menubar)
544 if (application->priv->menubar != NULL)
545 g_object_unref (application->priv->menubar);
547 application->priv->menubar = menubar;
549 if (application->priv->menubar != NULL)
550 g_object_ref (application->priv->menubar);
552 g_object_notify (G_OBJECT (application), "menubar");
557 * g_application_get_menubar:
558 * @application: a #GApplication
560 * Returns the menu model that has been set with
561 * g_application_set_menubar().
563 * Returns: the menubar for windows of @application
568 g_application_get_menubar (GApplication *application)
570 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
572 return application->priv->menubar;
576 g_application_get_property (GObject *object,
581 GApplication *application = G_APPLICATION (object);
585 case PROP_APPLICATION_ID:
586 g_value_set_string (value,
587 g_application_get_application_id (application));
591 g_value_set_flags (value,
592 g_application_get_flags (application));
595 case PROP_IS_REGISTERED:
596 g_value_set_boolean (value,
597 g_application_get_is_registered (application));
601 g_value_set_boolean (value,
602 g_application_get_is_remote (application));
605 case PROP_INACTIVITY_TIMEOUT:
606 g_value_set_uint (value,
607 g_application_get_inactivity_timeout (application));
611 g_value_set_object (value,
612 g_application_get_app_menu (application));
616 g_value_set_object (value,
617 g_application_get_menubar (application));
621 g_assert_not_reached ();
626 g_application_constructed (GObject *object)
628 GApplication *application = G_APPLICATION (object);
630 g_assert (application->priv->id != NULL);
632 if (g_application_get_default () == NULL)
633 g_application_set_default (application);
637 g_application_finalize (GObject *object)
639 GApplication *application = G_APPLICATION (object);
641 if (application->priv->impl)
642 g_application_impl_destroy (application->priv->impl);
643 g_free (application->priv->id);
645 if (g_application_get_default () == application)
646 g_application_set_default (NULL);
648 if (application->priv->app_menu)
649 g_object_unref (application->priv->app_menu);
651 if (application->priv->menubar)
652 g_object_unref (application->priv->menubar);
654 if (application->priv->actions)
655 g_object_unref (application->priv->actions);
657 G_OBJECT_CLASS (g_application_parent_class)
662 g_application_init (GApplication *application)
664 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
666 GApplicationPrivate);
667 application->priv->actions = G_ACTION_GROUP (g_simple_action_group_new ());
671 g_application_class_init (GApplicationClass *class)
673 GObjectClass *object_class = G_OBJECT_CLASS (class);
675 object_class->constructed = g_application_constructed;
676 object_class->finalize = g_application_finalize;
677 object_class->get_property = g_application_get_property;
678 object_class->set_property = g_application_set_property;
680 class->before_emit = g_application_real_before_emit;
681 class->after_emit = g_application_real_after_emit;
682 class->startup = g_application_real_startup;
683 class->shutdown = g_application_real_shutdown;
684 class->activate = g_application_real_activate;
685 class->open = g_application_real_open;
686 class->command_line = g_application_real_command_line;
687 class->local_command_line = g_application_real_local_command_line;
688 class->add_platform_data = g_application_real_add_platform_data;
690 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
691 g_param_spec_string ("application-id",
692 P_("Application identifier"),
693 P_("The unique identifier for the application"),
694 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
695 G_PARAM_STATIC_STRINGS));
697 g_object_class_install_property (object_class, PROP_FLAGS,
698 g_param_spec_flags ("flags",
699 P_("Application flags"),
700 P_("Flags specifying the behaviour of the application"),
701 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
705 g_param_spec_boolean ("is-registered",
707 P_("If g_application_register() has been called"),
708 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
710 g_object_class_install_property (object_class, PROP_IS_REMOTE,
711 g_param_spec_boolean ("is-remote",
713 P_("If this application instance is remote"),
714 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
716 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
717 g_param_spec_uint ("inactivity-timeout",
718 P_("Inactivity timeout"),
719 P_("Time (ms) to stay alive after becoming idle"),
721 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
723 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
724 g_param_spec_object ("action-group",
726 P_("The group of actions that the application exports"),
728 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
730 g_object_class_install_property (object_class, PROP_APP_MENU,
731 g_param_spec_object ("app-menu",
732 P_("Application menu"),
733 P_("The GMenuModel for the application menu"),
735 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737 g_object_class_install_property (object_class, PROP_MENUBAR,
738 g_param_spec_object ("menubar",
740 P_("The GMenuModel for the menubar"),
742 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
745 * GApplication::startup:
746 * @application: the application
748 * The ::startup signal is emitted on the primary instance immediately
749 * after registration. See g_application_register().
751 g_application_signals[SIGNAL_STARTUP] =
752 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
753 G_STRUCT_OFFSET (GApplicationClass, startup),
754 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
757 * GApplication::shutdown:
758 * @application: the application
760 * The ::shutdown signal is emitted only on the registered primary instance
761 * immediately after the main loop terminates.
763 g_application_signals[SIGNAL_SHUTDOWN] =
764 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
765 G_STRUCT_OFFSET (GApplicationClass, shutdown),
766 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
769 * GApplication::activate:
770 * @application: the application
772 * The ::activate signal is emitted on the primary instance when an
773 * activation occurs. See g_application_activate().
775 g_application_signals[SIGNAL_ACTIVATE] =
776 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
777 G_STRUCT_OFFSET (GApplicationClass, activate),
778 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
782 * GApplication::open:
783 * @application: the application
784 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
785 * @n_files: the length of @files
786 * @hint: a hint provided by the calling instance
788 * The ::open signal is emitted on the primary instance when there are
789 * files to open. See g_application_open() for more information.
791 g_application_signals[SIGNAL_OPEN] =
792 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
793 G_STRUCT_OFFSET (GApplicationClass, open),
795 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
798 * GApplication::command-line:
799 * @application: the application
800 * @command_line: a #GApplicationCommandLine representing the
803 * The ::command-line signal is emitted on the primary instance when
804 * a commandline is not handled locally. See g_application_run() and
805 * the #GApplicationCommandline documentation for more information.
807 * Returns: An integer that is set as the exit status for the calling
808 * process. See g_application_command_line_set_exit_status().
810 g_application_signals[SIGNAL_COMMAND_LINE] =
811 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
812 G_STRUCT_OFFSET (GApplicationClass, command_line),
813 g_signal_accumulator_first_wins, NULL,
815 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
817 g_type_class_add_private (class, sizeof (GApplicationPrivate));
821 get_platform_data (GApplication *application)
823 GVariantBuilder *builder;
826 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
829 gchar *cwd = g_get_current_dir ();
830 g_variant_builder_add (builder, "{sv}", "cwd",
831 g_variant_new_bytestring (cwd));
835 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
840 envp = g_get_environ ();
841 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
844 g_variant_builder_add (builder, "{sv}", "environ", array);
847 G_APPLICATION_GET_CLASS (application)->
848 add_platform_data (application, builder);
850 result = g_variant_builder_end (builder);
851 g_variant_builder_unref (builder);
856 /* Application ID validity {{{1 */
859 * g_application_id_is_valid:
860 * @application_id: a potential application identifier
862 * Checks if @application_id is a valid application identifier.
864 * A valid ID is required for calls to g_application_new() and
865 * g_application_set_application_id().
867 * For convenience, the restrictions on application identifiers are
870 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
871 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
872 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
873 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
874 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
877 * Returns: %TRUE if @application_id is valid
880 g_application_id_is_valid (const gchar *application_id)
886 len = strlen (application_id);
891 if (!g_ascii_isalpha (application_id[0]))
894 if (application_id[len-1] == '.')
900 for (; *application_id; application_id++)
902 if (g_ascii_isalnum (*application_id) ||
903 (*application_id == '-') ||
904 (*application_id == '_'))
908 else if (allow_dot && *application_id == '.')
923 /* Public Constructor {{{1 */
926 * @application_id: the application id
927 * @flags: the application flags
929 * Creates a new #GApplication instance.
931 * This function calls g_type_init() for you.
933 * The application id must be valid. See g_application_id_is_valid().
935 * Returns: a new #GApplication instance
938 g_application_new (const gchar *application_id,
939 GApplicationFlags flags)
941 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
945 return g_object_new (G_TYPE_APPLICATION,
946 "application-id", application_id,
951 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
953 * g_application_get_application_id:
954 * @application: a #GApplication
956 * Gets the unique identifier for @application.
958 * Returns: the identifier for @application, owned by @application
963 g_application_get_application_id (GApplication *application)
965 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
967 return application->priv->id;
971 * g_application_set_application_id:
972 * @application: a #GApplication
973 * @application_id: the identifier for @application
975 * Sets the unique identifier for @application.
977 * The application id can only be modified if @application has not yet
980 * The application id must be valid. See g_application_id_is_valid().
985 g_application_set_application_id (GApplication *application,
986 const gchar *application_id)
988 g_return_if_fail (G_IS_APPLICATION (application));
990 if (g_strcmp0 (application->priv->id, application_id) != 0)
992 g_return_if_fail (g_application_id_is_valid (application_id));
993 g_return_if_fail (!application->priv->is_registered);
995 g_free (application->priv->id);
996 application->priv->id = g_strdup (application_id);
998 g_object_notify (G_OBJECT (application), "application-id");
1003 * g_application_get_flags:
1004 * @application: a #GApplication
1006 * Gets the flags for @application.
1008 * See #GApplicationFlags.
1010 * Returns: the flags for @application
1015 g_application_get_flags (GApplication *application)
1017 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1019 return application->priv->flags;
1023 * g_application_set_flags:
1024 * @application: a #GApplication
1025 * @flags: the flags for @application
1027 * Sets the flags for @application.
1029 * The flags can only be modified if @application has not yet been
1032 * See #GApplicationFlags.
1037 g_application_set_flags (GApplication *application,
1038 GApplicationFlags flags)
1040 g_return_if_fail (G_IS_APPLICATION (application));
1042 if (application->priv->flags != flags)
1044 g_return_if_fail (!application->priv->is_registered);
1046 application->priv->flags = flags;
1048 g_object_notify (G_OBJECT (application), "flags");
1053 * g_application_get_inactivity_timeout:
1054 * @application: a #GApplication
1056 * Gets the current inactivity timeout for the application.
1058 * This is the amount of time (in milliseconds) after the last call to
1059 * g_application_release() before the application stops running.
1061 * Returns: the timeout, in milliseconds
1066 g_application_get_inactivity_timeout (GApplication *application)
1068 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1070 return application->priv->inactivity_timeout;
1074 * g_application_set_inactivity_timeout:
1075 * @application: a #GApplication
1076 * @inactivity_timeout: the timeout, in milliseconds
1078 * Sets the current inactivity timeout for the application.
1080 * This is the amount of time (in milliseconds) after the last call to
1081 * g_application_release() before the application stops running.
1083 * This call has no side effects of its own. The value set here is only
1084 * used for next time g_application_release() drops the use count to
1085 * zero. Any timeouts currently in progress are not impacted.
1090 g_application_set_inactivity_timeout (GApplication *application,
1091 guint inactivity_timeout)
1093 g_return_if_fail (G_IS_APPLICATION (application));
1095 if (application->priv->inactivity_timeout != inactivity_timeout)
1097 application->priv->inactivity_timeout = inactivity_timeout;
1099 g_object_notify (G_OBJECT (application), "inactivity-timeout");
1102 /* Read-only property getters (is registered, is remote) {{{1 */
1104 * g_application_get_is_registered:
1105 * @application: a #GApplication
1107 * Checks if @application is registered.
1109 * An application is registered if g_application_register() has been
1110 * successfully called.
1112 * Returns: %TRUE if @application is registered
1117 g_application_get_is_registered (GApplication *application)
1119 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1121 return application->priv->is_registered;
1125 * g_application_get_is_remote:
1126 * @application: a #GApplication
1128 * Checks if @application is remote.
1130 * If @application is remote then it means that another instance of
1131 * application already exists (the 'primary' instance). Calls to
1132 * perform actions on @application will result in the actions being
1133 * performed by the primary instance.
1135 * The value of this property cannot be accessed before
1136 * g_application_register() has been called. See
1137 * g_application_get_is_registered().
1139 * Returns: %TRUE if @application is remote
1144 g_application_get_is_remote (GApplication *application)
1146 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1147 g_return_val_if_fail (application->priv->is_registered, FALSE);
1149 return application->priv->is_remote;
1154 * g_application_register:
1155 * @application: a #GApplication
1156 * @cancellable: a #GCancellable, or %NULL
1157 * @error: a pointer to a NULL #GError, or %NULL
1159 * Attempts registration of the application.
1161 * This is the point at which the application discovers if it is the
1162 * primary instance or merely acting as a remote for an already-existing
1163 * primary instance. This is implemented by attempting to acquire the
1164 * application identifier as a unique bus name on the session bus using
1167 * Due to the internal architecture of GDBus, method calls can be
1168 * dispatched at any time (even if a main loop is not running). For
1169 * this reason, you must ensure that any object paths that you wish to
1170 * register are registered before calling this function.
1172 * If the application has already been registered then %TRUE is
1173 * returned with no work performed.
1175 * The #GApplication::startup signal is emitted if registration succeeds
1176 * and @application is the primary instance.
1178 * In the event of an error (such as @cancellable being cancelled, or a
1179 * failure to connect to the session bus), %FALSE is returned and @error
1180 * is set appropriately.
1182 * Note: the return value of this function is not an indicator that this
1183 * instance is or is not the primary instance of the application. See
1184 * g_application_get_is_remote() for that.
1186 * Returns: %TRUE if registration succeeded
1191 g_application_register (GApplication *application,
1192 GCancellable *cancellable,
1195 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1197 if (!application->priv->is_registered)
1199 if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1201 application->priv->impl =
1202 g_application_impl_register (application, application->priv->id,
1203 application->priv->flags,
1204 &application->priv->remote_actions,
1205 cancellable, error);
1207 if (application->priv->impl == NULL)
1211 application->priv->is_remote = application->priv->remote_actions != NULL;
1212 application->priv->is_registered = TRUE;
1214 g_object_notify (G_OBJECT (application), "is-registered");
1216 if (!application->priv->is_remote)
1218 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1220 if (!application->priv->did_startup)
1221 g_critical ("GApplication subclass '%s' failed to chain up on"
1222 " ::startup (from start of override function)",
1223 G_OBJECT_TYPE_NAME (application));
1230 /* Hold/release {{{1 */
1232 * g_application_hold:
1233 * @application: a #GApplication
1235 * Increases the use count of @application.
1237 * Use this function to indicate that the application has a reason to
1238 * continue to run. For example, g_application_hold() is called by GTK+
1239 * when a toplevel window is on the screen.
1241 * To cancel the hold, call g_application_release().
1244 g_application_hold (GApplication *application)
1246 if (application->priv->inactivity_timeout_id)
1248 g_source_remove (application->priv->inactivity_timeout_id);
1249 application->priv->inactivity_timeout_id = 0;
1252 application->priv->use_count++;
1256 inactivity_timeout_expired (gpointer data)
1258 GApplication *application = G_APPLICATION (data);
1260 application->priv->inactivity_timeout_id = 0;
1267 * g_application_release:
1268 * @application: a #GApplication
1270 * Decrease the use count of @application.
1272 * When the use count reaches zero, the application will stop running.
1274 * Never call this function except to cancel the effect of a previous
1275 * call to g_application_hold().
1278 g_application_release (GApplication *application)
1280 application->priv->use_count--;
1282 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1283 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1284 inactivity_timeout_expired, application);
1287 /* Activate, Open {{{1 */
1289 * g_application_activate:
1290 * @application: a #GApplication
1292 * Activates the application.
1294 * In essence, this results in the #GApplication::activate() signal being
1295 * emitted in the primary instance.
1297 * The application must be registered before calling this function.
1302 g_application_activate (GApplication *application)
1304 g_return_if_fail (G_IS_APPLICATION (application));
1305 g_return_if_fail (application->priv->is_registered);
1307 if (application->priv->is_remote)
1308 g_application_impl_activate (application->priv->impl,
1309 get_platform_data (application));
1312 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1316 * g_application_open:
1317 * @application: a #GApplication
1318 * @files: (array length=n_files): an array of #GFiles to open
1319 * @n_files: the length of the @files array
1320 * @hint: a hint (or ""), but never %NULL
1322 * Opens the given files.
1324 * In essence, this results in the #GApplication::open signal being emitted
1325 * in the primary instance.
1327 * @n_files must be greater than zero.
1329 * @hint is simply passed through to the ::open signal. It is
1330 * intended to be used by applications that have multiple modes for
1331 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1332 * for this functionality, you should use "".
1334 * The application must be registered before calling this function
1335 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1340 g_application_open (GApplication *application,
1345 g_return_if_fail (G_IS_APPLICATION (application));
1346 g_return_if_fail (application->priv->flags &
1347 G_APPLICATION_HANDLES_OPEN);
1348 g_return_if_fail (application->priv->is_registered);
1350 if (application->priv->is_remote)
1351 g_application_impl_open (application->priv->impl,
1352 files, n_files, hint,
1353 get_platform_data (application));
1356 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1357 0, files, n_files, hint);
1362 * g_application_run:
1363 * @application: a #GApplication
1364 * @argc: the argc from main() (or 0 if @argv is %NULL)
1365 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1367 * Runs the application.
1369 * This function is intended to be run from main() and its return value
1370 * is intended to be returned by main(). Although you are expected to pass
1371 * the @argc, @argv parameters from main() to this function, it is possible
1372 * to pass %NULL if @argv is not available or commandline handling is not
1375 * First, the local_command_line() virtual function is invoked.
1376 * This function always runs on the local instance. It gets passed a pointer
1377 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1378 * that it handled (shifting up remaining arguments). See
1379 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1380 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1381 * after setting <literal>argc = g_strv_length (argv);</literal>.
1383 * The last argument to local_command_line() is a pointer to the @status
1384 * variable which can used to set the exit status that is returned from
1385 * g_application_run().
1387 * If local_command_line() returns %TRUE, the command line is expected
1388 * to be completely handled, including possibly registering as the primary
1389 * instance, calling g_application_activate() or g_application_open(), etc.
1391 * If local_command_line() returns %FALSE then the application is registered
1392 * and the #GApplication::command-line signal is emitted in the primary
1393 * instance (which may or may not be this instance). The signal handler
1394 * gets passed a #GApplicationCommandline object that (among other things)
1395 * contains the remaining commandline arguments that have not been handled
1396 * by local_command_line().
1398 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1399 * flag set then the default implementation of local_command_line()
1400 * always returns %FALSE immediately, resulting in the commandline
1401 * always being handled in the primary instance.
1403 * Otherwise, the default implementation of local_command_line() tries
1404 * to do a couple of things that are probably reasonable for most
1405 * applications. First, g_application_register() is called to attempt
1406 * to register the application. If that works, then the command line
1407 * arguments are inspected. If no commandline arguments are given, then
1408 * g_application_activate() is called. If commandline arguments are
1409 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1410 * are assumed to be filenames and g_application_open() is called.
1412 * If you need to handle commandline arguments that are not filenames,
1413 * and you don't mind commandline handling to happen in the primary
1414 * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1415 * process the commandline arguments in your #GApplication::command-line
1416 * signal handler, either manually or using the #GOptionContext API.
1418 * If you are interested in doing more complicated local handling of the
1419 * commandline then you should implement your own #GApplication subclass
1420 * and override local_command_line(). In this case, you most likely want
1421 * to return %TRUE from your local_command_line() implementation to
1422 * suppress the default handling. See
1423 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1425 * If, after the above is done, the use count of the application is zero
1426 * then the exit status is returned immediately. If the use count is
1427 * non-zero then the default main context is iterated until the use count
1428 * falls to zero, at which point 0 is returned.
1430 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1431 * use count of zero is delayed for a while (ie: the instance stays
1432 * around to provide its <emphasis>service</emphasis> to others).
1434 * Returns: the exit status
1439 g_application_run (GApplication *application,
1447 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1448 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1450 arguments = g_new (gchar *, argc + 1);
1451 for (i = 0; i < argc; i++)
1452 arguments[i] = g_strdup (argv[i]);
1453 arguments[i] = NULL;
1455 if (g_get_prgname () == NULL && argc > 0)
1459 prgname = g_path_get_basename (argv[0]);
1460 g_set_prgname (prgname);
1464 if (!G_APPLICATION_GET_CLASS (application)
1465 ->local_command_line (application, &arguments, &status))
1467 GError *error = NULL;
1469 if (!g_application_register (application, NULL, &error))
1471 g_printerr ("%s", error->message);
1472 g_error_free (error);
1476 if (application->priv->is_remote)
1478 GVariant *platform_data;
1480 platform_data = get_platform_data (application);
1481 status = g_application_impl_command_line (application->priv->impl,
1482 arguments, platform_data);
1486 GApplicationCommandLine *cmdline;
1489 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1490 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1491 "arguments", v, NULL);
1492 g_signal_emit (application,
1493 g_application_signals[SIGNAL_COMMAND_LINE],
1494 0, cmdline, &status);
1495 g_object_unref (cmdline);
1499 g_strfreev (arguments);
1501 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1502 application->priv->is_registered &&
1503 !application->priv->use_count &&
1504 !application->priv->inactivity_timeout_id)
1506 application->priv->inactivity_timeout_id =
1507 g_timeout_add (10000, inactivity_timeout_expired, application);
1510 while (application->priv->use_count || application->priv->inactivity_timeout_id)
1512 g_main_context_iteration (NULL, TRUE);
1516 if (!application->priv->is_remote)
1518 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1520 if (!application->priv->did_shutdown)
1521 g_critical ("GApplication subclass '%s' failed to chain up on"
1522 " ::shutdown (from end of override function)",
1523 G_OBJECT_TYPE_NAME (application));
1526 if (application->priv->impl)
1527 g_application_impl_flush (application->priv->impl);
1535 g_application_list_actions (GActionGroup *action_group)
1537 GApplication *application = G_APPLICATION (action_group);
1539 g_return_val_if_fail (application->priv->is_registered, NULL);
1541 if (application->priv->remote_actions != NULL)
1542 return g_action_group_list_actions (application->priv->remote_actions);
1544 else if (application->priv->actions != NULL)
1545 return g_action_group_list_actions (application->priv->actions);
1548 /* empty string array */
1549 return g_new0 (gchar *, 1);
1553 g_application_query_action (GActionGroup *group,
1554 const gchar *action_name,
1556 const GVariantType **parameter_type,
1557 const GVariantType **state_type,
1558 GVariant **state_hint,
1561 GApplication *application = G_APPLICATION (group);
1563 g_return_val_if_fail (application->priv->is_registered, FALSE);
1565 if (application->priv->remote_actions != NULL)
1566 return g_action_group_query_action (application->priv->remote_actions,
1574 if (application->priv->actions != NULL)
1575 return g_action_group_query_action (application->priv->actions,
1587 g_application_change_action_state (GActionGroup *action_group,
1588 const gchar *action_name,
1591 GApplication *application = G_APPLICATION (action_group);
1593 g_return_if_fail (application->priv->is_remote ||
1594 application->priv->actions != NULL);
1595 g_return_if_fail (application->priv->is_registered);
1597 if (application->priv->remote_actions)
1598 return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
1601 g_action_group_change_action_state (application->priv->actions,
1602 action_name, value);
1606 g_application_activate_action (GActionGroup *action_group,
1607 const gchar *action_name,
1608 GVariant *parameter)
1610 GApplication *application = G_APPLICATION (action_group);
1612 g_return_if_fail (application->priv->is_remote ||
1613 application->priv->actions != NULL);
1614 g_return_if_fail (application->priv->is_registered);
1616 if (application->priv->remote_actions)
1617 return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
1620 g_action_group_activate_action (application->priv->actions,
1621 action_name, parameter);
1625 g_application_lookup_action (GActionMap *action_map,
1626 const gchar *action_name)
1628 GApplication *application = G_APPLICATION (action_map);
1630 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions), NULL);
1632 return g_simple_action_group_lookup (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1636 g_application_add_action (GActionMap *action_map,
1639 GApplication *application = G_APPLICATION (action_map);
1641 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1643 g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (application->priv->actions), action);
1647 g_application_remove_action (GActionMap *action_map,
1648 const gchar *action_name)
1650 GApplication *application = G_APPLICATION (action_map);
1652 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1654 g_simple_action_group_remove (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1658 g_application_action_group_iface_init (GActionGroupInterface *iface)
1660 iface->list_actions = g_application_list_actions;
1661 iface->query_action = g_application_query_action;
1662 iface->change_action_state = g_application_change_action_state;
1663 iface->activate_action = g_application_activate_action;
1667 g_application_action_map_iface_init (GActionMapInterface *iface)
1669 iface->lookup_action = g_application_lookup_action;
1670 iface->add_action = g_application_add_action;
1671 iface->remove_action = g_application_remove_action;
1674 /* Default Application {{{1 */
1676 static GApplication *default_app;
1679 * g_application_get_default:
1680 * @returns: (transfer none): the default application for this process, or %NULL
1682 * Returns the default #GApplication instance for this process.
1684 * Normally there is only one #GApplication per process and it becomes
1685 * the default when it is created. You can exercise more control over
1686 * this by using g_application_set_default().
1688 * If there is no default application then %NULL is returned.
1693 g_application_get_default (void)
1699 * g_application_set_default:
1700 * @application: the application to set as default, or %NULL
1702 * Sets or unsets the default application for the process, as returned
1703 * by g_application_get_default().
1705 * This function does not take its own reference on @application. If
1706 * @application is destroyed then the default application will revert
1712 g_application_set_default (GApplication *application)
1714 default_app = application;
1718 /* vim:set foldmethod=marker: */