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 interface and lets you
92 * easily export actions by adding them with g_application_set_action_group().
93 * When invoking an action by calling g_action_group_activate_action() on
94 * the application, it is always invoked in the primary instance. The actions
95 * are also exported on the session bus, and GIO provides the #GDBusActionGroup
96 * wrapper to conveniently access them remotely. Additionally,
97 * g_application_set_menu() can be used to export representation data
98 * for the actions, in the form of a #GMenuModel.
100 * There is a number of different entry points into a #GApplication:
102 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
103 * <listitem>via 'Open' (i.e. opening some files)</listitem>
104 * <listitem>by handling a command-line</listitem>
105 * <listitem>via activating an action</listitem>
107 * The #GApplication::startup signal lets you handle the application
108 * initialization for all of these in a single place.
110 * Regardless of which of these entry points is used to start the application,
111 * GApplication passes some <firstterm id="platform-data">platform
112 * data</firstterm> from the launching instance to the primary instance,
113 * in the form of a #GVariant dictionary mapping strings to variants.
114 * To use platform data, override the @before_emit or @after_emit virtual
115 * functions in your #GApplication subclass. When dealing with
116 * #GApplicationCommandline objects, the platform data is directly
117 * available via g_application_command_line_get_cwd(),
118 * g_application_command_line_get_environ() and
119 * g_application_command_line_get_platform_data().
121 * As the name indicates, the platform data may vary depending on the
122 * operating system, but it always includes the current directory (key
123 * "cwd"), and optionally the environment (ie the set of environment
124 * variables and their values) of the calling process (key "environ").
125 * The environment is only added to the platform data if the
126 * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
127 * can add their own platform data by overriding the @add_platform_data
128 * virtual function. For instance, #GtkApplication adds startup notification
131 * To parse commandline arguments you may handle the
132 * #GApplication::command-line signal or override the local_command_line()
133 * vfunc, to parse them in either the primary instance or the local instance,
136 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
138 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
139 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
144 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
146 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
147 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
152 * <example id="gapplication-example-menu"><title>A GApplication with menus</title>
154 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-menu.c">
155 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
161 struct _GApplicationPrivate
163 GApplicationFlags flags;
166 GActionGroup *actions;
167 GMenuModel *app_menu;
170 guint inactivity_timeout_id;
171 guint inactivity_timeout;
174 guint is_registered : 1;
176 guint did_startup : 1;
177 guint did_shutdown : 1;
179 GActionGroup *remote_actions;
180 GApplicationImpl *impl;
190 PROP_INACTIVITY_TIMEOUT,
207 static guint g_application_signals[NR_SIGNALS];
209 static void g_application_action_group_iface_init (GActionGroupInterface *);
210 static void g_application_action_map_iface_init (GActionMapInterface *);
211 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
212 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
213 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
215 /* vfunc defaults {{{1 */
217 g_application_real_before_emit (GApplication *application,
218 GVariant *platform_data)
223 g_application_real_after_emit (GApplication *application,
224 GVariant *platform_data)
229 g_application_real_startup (GApplication *application)
231 application->priv->did_startup = TRUE;
235 g_application_real_shutdown (GApplication *application)
237 application->priv->did_shutdown = TRUE;
241 g_application_real_activate (GApplication *application)
243 if (!g_signal_has_handler_pending (application,
244 g_application_signals[SIGNAL_ACTIVATE],
246 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
248 static gboolean warned;
253 g_warning ("Your application does not implement "
254 "g_application_activate() and has no handlers connected "
255 "to the 'activate' signal. It should do one of these.");
261 g_application_real_open (GApplication *application,
266 if (!g_signal_has_handler_pending (application,
267 g_application_signals[SIGNAL_OPEN],
269 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
271 static gboolean warned;
276 g_warning ("Your application claims to support opening files "
277 "but does not implement g_application_open() and has no "
278 "handlers connected to the 'open' signal.");
284 g_application_real_command_line (GApplication *application,
285 GApplicationCommandLine *cmdline)
287 if (!g_signal_has_handler_pending (application,
288 g_application_signals[SIGNAL_COMMAND_LINE],
290 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
292 static gboolean warned;
297 g_warning ("Your application claims to support custom command line "
298 "handling but does not implement g_application_command_line() "
299 "and has no handlers connected to the 'command-line' signal.");
308 g_application_real_local_command_line (GApplication *application,
312 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
317 GError *error = NULL;
320 if (!g_application_register (application, NULL, &error))
322 g_critical ("%s", error->message);
323 g_error_free (error);
328 n_args = g_strv_length (*arguments);
330 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
332 if ((*exit_status = n_args > 1))
334 g_printerr ("GApplication service mode takes no arguments.\n");
335 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
343 g_application_activate (application);
349 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
351 g_critical ("This application can not open files.");
360 n_files = n_args - 1;
361 files = g_new (GFile *, n_files);
363 for (i = 0; i < n_files; i++)
364 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
366 g_application_open (application, files, n_files, "");
368 for (i = 0; i < n_files; i++)
369 g_object_unref (files[i]);
381 g_application_real_add_platform_data (GApplication *application,
382 GVariantBuilder *builder)
386 /* GObject implementation stuff {{{1 */
388 g_application_set_property (GObject *object,
393 GApplication *application = G_APPLICATION (object);
397 case PROP_APPLICATION_ID:
398 g_application_set_application_id (application,
399 g_value_get_string (value));
403 g_application_set_flags (application, g_value_get_flags (value));
406 case PROP_INACTIVITY_TIMEOUT:
407 g_application_set_inactivity_timeout (application,
408 g_value_get_uint (value));
411 case PROP_ACTION_GROUP:
412 g_clear_object (&application->priv->actions);
413 application->priv->actions = g_value_dup_object (value);
417 g_application_set_app_menu (application, g_value_get_object (value));
421 g_application_set_menubar (application, g_value_get_object (value));
425 g_assert_not_reached ();
430 * g_application_set_action_group:
431 * @application: a #GApplication
432 * @action_group: (allow-none): a #GActionGroup, or %NULL
434 * This used to be how actions were associated with a #GApplication.
435 * Now there is #GActionMap for that.
439 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
440 * mix use of this API with use of #GActionMap on the same @application
441 * or things will go very badly wrong.
444 g_application_set_action_group (GApplication *application,
445 GActionGroup *action_group)
447 g_return_if_fail (G_IS_APPLICATION (application));
448 g_return_if_fail (!application->priv->is_registered);
450 if (application->priv->actions != NULL)
451 g_object_unref (application->priv->actions);
453 application->priv->actions = action_group;
455 if (application->priv->actions != NULL)
456 g_object_ref (application->priv->actions);
460 * g_application_set_app_menu:
461 * @application: a #GApplication
462 * @app_menu: (allow-none): a #GMenuModel, or %NULL
464 * Sets or unsets the application menu for @application.
466 * The application menu is a single menu containing items that typically
467 * impact the application as a whole, rather than acting on a specific
468 * window or document. For example, you would expect to see
469 * "Preferences" or "Quit" in an application menu, but not "Save" or
472 * If supported, the application menu will be rendered by the desktop
475 * It is an error to call this function after the application has been
481 g_application_set_app_menu (GApplication *application,
482 GMenuModel *app_menu)
484 g_return_if_fail (G_IS_APPLICATION (application));
485 g_return_if_fail (!application->priv->is_registered);
487 if (app_menu != application->priv->app_menu)
489 if (application->priv->app_menu != NULL)
490 g_object_unref (application->priv->app_menu);
492 application->priv->app_menu = app_menu;
494 if (application->priv->app_menu != NULL)
495 g_object_ref (application->priv->app_menu);
497 g_object_notify (G_OBJECT (application), "app-menu");
502 * g_application_get_app_menu:
503 * @application: a #GApplication
505 * Returns the menu model that has been set with
506 * g_application_set_app_menu().
508 * Returns: the application menu of @application
513 g_application_get_app_menu (GApplication *application)
515 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
517 return application->priv->app_menu;
521 * g_application_set_menubar:
522 * @application: a #GApplication
523 * @menubar: (allow-none): a #GMenuModel, or %NULL
525 * Sets or unsets the menubar for windows of @application.
527 * This is a menubar in the traditional sense.
529 * Depending on the desktop environment, this may appear at the top of
530 * each window, or at the top of the screen. In some environments, if
531 * both the application menu and the menubar are set, the application
532 * menu will be presented as if it were the first item of the menubar.
533 * Other environments treat the two as completely separate -- for
534 * example, the application menu may be rendered by the desktop shell
535 * while the menubar (if set) remains in each individual window.
537 * It is an error to call this function after the application has been
543 g_application_set_menubar (GApplication *application,
546 g_return_if_fail (G_IS_APPLICATION (application));
547 g_return_if_fail (!application->priv->is_registered);
549 if (menubar != application->priv->menubar)
551 if (application->priv->menubar != NULL)
552 g_object_unref (application->priv->menubar);
554 application->priv->menubar = menubar;
556 if (application->priv->menubar != NULL)
557 g_object_ref (application->priv->menubar);
559 g_object_notify (G_OBJECT (application), "menubar");
564 * g_application_get_menubar:
565 * @application: a #GApplication
567 * Returns the menu model that has been set with
568 * g_application_set_menubar().
570 * Returns: the menubar for windows of @application
575 g_application_get_menubar (GApplication *application)
577 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
579 return application->priv->menubar;
583 g_application_get_property (GObject *object,
588 GApplication *application = G_APPLICATION (object);
592 case PROP_APPLICATION_ID:
593 g_value_set_string (value,
594 g_application_get_application_id (application));
598 g_value_set_flags (value,
599 g_application_get_flags (application));
602 case PROP_IS_REGISTERED:
603 g_value_set_boolean (value,
604 g_application_get_is_registered (application));
608 g_value_set_boolean (value,
609 g_application_get_is_remote (application));
612 case PROP_INACTIVITY_TIMEOUT:
613 g_value_set_uint (value,
614 g_application_get_inactivity_timeout (application));
618 g_assert_not_reached ();
623 g_application_constructed (GObject *object)
625 GApplication *application = G_APPLICATION (object);
627 g_assert (application->priv->id != NULL);
629 if (g_application_get_default () == NULL)
630 g_application_set_default (application);
634 g_application_finalize (GObject *object)
636 GApplication *application = G_APPLICATION (object);
638 if (application->priv->impl)
639 g_application_impl_destroy (application->priv->impl);
640 g_free (application->priv->id);
642 if (g_application_get_default () == application)
643 g_application_set_default (NULL);
645 if (application->priv->app_menu)
646 g_object_unref (application->priv->app_menu);
648 if (application->priv->menubar)
649 g_object_unref (application->priv->menubar);
651 if (application->priv->actions)
652 g_object_unref (application->priv->actions);
654 G_OBJECT_CLASS (g_application_parent_class)
659 g_application_init (GApplication *application)
661 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
663 GApplicationPrivate);
664 application->priv->actions = G_ACTION_GROUP (g_simple_action_group_new ());
668 g_application_class_init (GApplicationClass *class)
670 GObjectClass *object_class = G_OBJECT_CLASS (class);
672 object_class->constructed = g_application_constructed;
673 object_class->finalize = g_application_finalize;
674 object_class->get_property = g_application_get_property;
675 object_class->set_property = g_application_set_property;
677 class->before_emit = g_application_real_before_emit;
678 class->after_emit = g_application_real_after_emit;
679 class->startup = g_application_real_startup;
680 class->shutdown = g_application_real_shutdown;
681 class->activate = g_application_real_activate;
682 class->open = g_application_real_open;
683 class->command_line = g_application_real_command_line;
684 class->local_command_line = g_application_real_local_command_line;
685 class->add_platform_data = g_application_real_add_platform_data;
687 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
688 g_param_spec_string ("application-id",
689 P_("Application identifier"),
690 P_("The unique identifier for the application"),
691 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
692 G_PARAM_STATIC_STRINGS));
694 g_object_class_install_property (object_class, PROP_FLAGS,
695 g_param_spec_flags ("flags",
696 P_("Application flags"),
697 P_("Flags specifying the behaviour of the application"),
698 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
699 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
701 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
702 g_param_spec_boolean ("is-registered",
704 P_("If g_application_register() has been called"),
705 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
707 g_object_class_install_property (object_class, PROP_IS_REMOTE,
708 g_param_spec_boolean ("is-remote",
710 P_("If this application instance is remote"),
711 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
713 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
714 g_param_spec_uint ("inactivity-timeout",
715 P_("Inactivity timeout"),
716 P_("Time (ms) to stay alive after becoming idle"),
718 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
720 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
721 g_param_spec_object ("action-group",
723 P_("The group of actions that the application exports"),
725 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
727 g_object_class_install_property (object_class, PROP_APP_MENU,
728 g_param_spec_object ("app-menu",
729 P_("Application menu"),
730 P_("The GMenuModel for the application menu"),
732 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
734 g_object_class_install_property (object_class, PROP_MENUBAR,
735 g_param_spec_object ("menubar",
737 P_("The GMenuModel for the menubar"),
739 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
742 * GApplication::startup:
743 * @application: the application
745 * The ::startup signal is emitted on the primary instance immediately
746 * after registration. See g_application_register().
748 g_application_signals[SIGNAL_STARTUP] =
749 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
750 G_STRUCT_OFFSET (GApplicationClass, startup),
751 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
754 * GApplication::shutdown:
755 * @application: the application
757 * The ::shutdown signal is emitted only on the registered primary instance
758 * immediately after the main loop terminates.
760 g_application_signals[SIGNAL_SHUTDOWN] =
761 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
762 G_STRUCT_OFFSET (GApplicationClass, shutdown),
763 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
766 * GApplication::activate:
767 * @application: the application
769 * The ::activate signal is emitted on the primary instance when an
770 * activation occurs. See g_application_activate().
772 g_application_signals[SIGNAL_ACTIVATE] =
773 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
774 G_STRUCT_OFFSET (GApplicationClass, activate),
775 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
779 * GApplication::open:
780 * @application: the application
781 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
782 * @n_files: the length of @files
783 * @hint: a hint provided by the calling instance
785 * The ::open signal is emitted on the primary instance when there are
786 * files to open. See g_application_open() for more information.
788 g_application_signals[SIGNAL_OPEN] =
789 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
790 G_STRUCT_OFFSET (GApplicationClass, open),
792 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
795 * GApplication::command-line:
796 * @application: the application
797 * @command_line: a #GApplicationCommandLine representing the
800 * The ::command-line signal is emitted on the primary instance when
801 * a commandline is not handled locally. See g_application_run() and
802 * the #GApplicationCommandline documentation for more information.
804 * Returns: An integer that is set as the exit status for the calling
805 * process. See g_application_command_line_set_exit_status().
807 g_application_signals[SIGNAL_COMMAND_LINE] =
808 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
809 G_STRUCT_OFFSET (GApplicationClass, command_line),
810 g_signal_accumulator_first_wins, NULL,
812 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
814 g_type_class_add_private (class, sizeof (GApplicationPrivate));
818 get_platform_data (GApplication *application)
820 GVariantBuilder *builder;
823 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
826 gchar *cwd = g_get_current_dir ();
827 g_variant_builder_add (builder, "{sv}", "cwd",
828 g_variant_new_bytestring (cwd));
832 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
837 envp = g_get_environ ();
838 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
841 g_variant_builder_add (builder, "{sv}", "environ", array);
844 G_APPLICATION_GET_CLASS (application)->
845 add_platform_data (application, builder);
847 result = g_variant_builder_end (builder);
848 g_variant_builder_unref (builder);
853 /* Application ID validity {{{1 */
856 * g_application_id_is_valid:
857 * @application_id: a potential application identifier
859 * Checks if @application_id is a valid application identifier.
861 * A valid ID is required for calls to g_application_new() and
862 * g_application_set_application_id().
864 * For convenience, the restrictions on application identifiers are
867 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
868 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
869 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
870 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
871 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
874 * Returns: %TRUE if @application_id is valid
877 g_application_id_is_valid (const gchar *application_id)
883 len = strlen (application_id);
888 if (!g_ascii_isalpha (application_id[0]))
891 if (application_id[len-1] == '.')
897 for (; *application_id; application_id++)
899 if (g_ascii_isalnum (*application_id) ||
900 (*application_id == '-') ||
901 (*application_id == '_'))
905 else if (allow_dot && *application_id == '.')
920 /* Public Constructor {{{1 */
923 * @application_id: the application id
924 * @flags: the application flags
926 * Creates a new #GApplication instance.
928 * This function calls g_type_init() for you.
930 * The application id must be valid. See g_application_id_is_valid().
932 * Returns: a new #GApplication instance
935 g_application_new (const gchar *application_id,
936 GApplicationFlags flags)
938 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
942 return g_object_new (G_TYPE_APPLICATION,
943 "application-id", application_id,
948 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
950 * g_application_get_application_id:
951 * @application: a #GApplication
953 * Gets the unique identifier for @application.
955 * Returns: the identifier for @application, owned by @application
960 g_application_get_application_id (GApplication *application)
962 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
964 return application->priv->id;
968 * g_application_set_application_id:
969 * @application: a #GApplication
970 * @application_id: the identifier for @application
972 * Sets the unique identifier for @application.
974 * The application id can only be modified if @application has not yet
977 * The application id must be valid. See g_application_id_is_valid().
982 g_application_set_application_id (GApplication *application,
983 const gchar *application_id)
985 g_return_if_fail (G_IS_APPLICATION (application));
987 if (g_strcmp0 (application->priv->id, application_id) != 0)
989 g_return_if_fail (g_application_id_is_valid (application_id));
990 g_return_if_fail (!application->priv->is_registered);
992 g_free (application->priv->id);
993 application->priv->id = g_strdup (application_id);
995 g_object_notify (G_OBJECT (application), "application-id");
1000 * g_application_get_flags:
1001 * @application: a #GApplication
1003 * Gets the flags for @application.
1005 * See #GApplicationFlags.
1007 * Returns: the flags for @application
1012 g_application_get_flags (GApplication *application)
1014 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1016 return application->priv->flags;
1020 * g_application_set_flags:
1021 * @application: a #GApplication
1022 * @flags: the flags for @application
1024 * Sets the flags for @application.
1026 * The flags can only be modified if @application has not yet been
1029 * See #GApplicationFlags.
1034 g_application_set_flags (GApplication *application,
1035 GApplicationFlags flags)
1037 g_return_if_fail (G_IS_APPLICATION (application));
1039 if (application->priv->flags != flags)
1041 g_return_if_fail (!application->priv->is_registered);
1043 application->priv->flags = flags;
1045 g_object_notify (G_OBJECT (application), "flags");
1050 * g_application_get_inactivity_timeout:
1051 * @application: a #GApplication
1053 * Gets the current inactivity timeout for the application.
1055 * This is the amount of time (in milliseconds) after the last call to
1056 * g_application_release() before the application stops running.
1058 * Returns: the timeout, in milliseconds
1063 g_application_get_inactivity_timeout (GApplication *application)
1065 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1067 return application->priv->inactivity_timeout;
1071 * g_application_set_inactivity_timeout:
1072 * @application: a #GApplication
1073 * @inactivity_timeout: the timeout, in milliseconds
1075 * Sets the current inactivity timeout for the application.
1077 * This is the amount of time (in milliseconds) after the last call to
1078 * g_application_release() before the application stops running.
1080 * This call has no side effects of its own. The value set here is only
1081 * used for next time g_application_release() drops the use count to
1082 * zero. Any timeouts currently in progress are not impacted.
1087 g_application_set_inactivity_timeout (GApplication *application,
1088 guint inactivity_timeout)
1090 g_return_if_fail (G_IS_APPLICATION (application));
1092 if (application->priv->inactivity_timeout != inactivity_timeout)
1094 application->priv->inactivity_timeout = inactivity_timeout;
1096 g_object_notify (G_OBJECT (application), "inactivity-timeout");
1099 /* Read-only property getters (is registered, is remote) {{{1 */
1101 * g_application_get_is_registered:
1102 * @application: a #GApplication
1104 * Checks if @application is registered.
1106 * An application is registered if g_application_register() has been
1107 * successfully called.
1109 * Returns: %TRUE if @application is registered
1114 g_application_get_is_registered (GApplication *application)
1116 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1118 return application->priv->is_registered;
1122 * g_application_get_is_remote:
1123 * @application: a #GApplication
1125 * Checks if @application is remote.
1127 * If @application is remote then it means that another instance of
1128 * application already exists (the 'primary' instance). Calls to
1129 * perform actions on @application will result in the actions being
1130 * performed by the primary instance.
1132 * The value of this property cannot be accessed before
1133 * g_application_register() has been called. See
1134 * g_application_get_is_registered().
1136 * Returns: %TRUE if @application is remote
1141 g_application_get_is_remote (GApplication *application)
1143 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1144 g_return_val_if_fail (application->priv->is_registered, FALSE);
1146 return application->priv->is_remote;
1151 * g_application_register:
1152 * @application: a #GApplication
1153 * @cancellable: a #GCancellable, or %NULL
1154 * @error: a pointer to a NULL #GError, or %NULL
1156 * Attempts registration of the application.
1158 * This is the point at which the application discovers if it is the
1159 * primary instance or merely acting as a remote for an already-existing
1160 * primary instance. This is implemented by attempting to acquire the
1161 * application identifier as a unique bus name on the session bus using
1164 * Due to the internal architecture of GDBus, method calls can be
1165 * dispatched at any time (even if a main loop is not running). For
1166 * this reason, you must ensure that any object paths that you wish to
1167 * register are registered before calling this function.
1169 * If the application has already been registered then %TRUE is
1170 * returned with no work performed.
1172 * The #GApplication::startup signal is emitted if registration succeeds
1173 * and @application is the primary instance.
1175 * In the event of an error (such as @cancellable being cancelled, or a
1176 * failure to connect to the session bus), %FALSE is returned and @error
1177 * is set appropriately.
1179 * Note: the return value of this function is not an indicator that this
1180 * instance is or is not the primary instance of the application. See
1181 * g_application_get_is_remote() for that.
1183 * Returns: %TRUE if registration succeeded
1188 g_application_register (GApplication *application,
1189 GCancellable *cancellable,
1192 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1194 if (!application->priv->is_registered)
1196 if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1198 application->priv->impl =
1199 g_application_impl_register (application, application->priv->id,
1200 application->priv->flags,
1201 &application->priv->remote_actions,
1202 cancellable, error);
1204 if (application->priv->impl == NULL)
1208 application->priv->is_remote = application->priv->remote_actions != NULL;
1209 application->priv->is_registered = TRUE;
1211 g_object_notify (G_OBJECT (application), "is-registered");
1213 if (!application->priv->is_remote)
1215 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1217 if (!application->priv->did_startup)
1218 g_critical ("GApplication subclass '%s' failed to chain up on"
1219 " ::startup (from start of override function)",
1220 G_OBJECT_TYPE_NAME (application));
1227 /* Hold/release {{{1 */
1229 * g_application_hold:
1230 * @application: a #GApplication
1232 * Increases the use count of @application.
1234 * Use this function to indicate that the application has a reason to
1235 * continue to run. For example, g_application_hold() is called by GTK+
1236 * when a toplevel window is on the screen.
1238 * To cancel the hold, call g_application_release().
1241 g_application_hold (GApplication *application)
1243 if (application->priv->inactivity_timeout_id)
1245 g_source_remove (application->priv->inactivity_timeout_id);
1246 application->priv->inactivity_timeout_id = 0;
1249 application->priv->use_count++;
1253 inactivity_timeout_expired (gpointer data)
1255 GApplication *application = G_APPLICATION (data);
1257 application->priv->inactivity_timeout_id = 0;
1264 * g_application_release:
1265 * @application: a #GApplication
1267 * Decrease the use count of @application.
1269 * When the use count reaches zero, the application will stop running.
1271 * Never call this function except to cancel the effect of a previous
1272 * call to g_application_hold().
1275 g_application_release (GApplication *application)
1277 application->priv->use_count--;
1279 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1280 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1281 inactivity_timeout_expired, application);
1284 /* Activate, Open {{{1 */
1286 * g_application_activate:
1287 * @application: a #GApplication
1289 * Activates the application.
1291 * In essence, this results in the #GApplication::activate() signal being
1292 * emitted in the primary instance.
1294 * The application must be registered before calling this function.
1299 g_application_activate (GApplication *application)
1301 g_return_if_fail (G_IS_APPLICATION (application));
1302 g_return_if_fail (application->priv->is_registered);
1304 if (application->priv->is_remote)
1305 g_application_impl_activate (application->priv->impl,
1306 get_platform_data (application));
1309 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1313 * g_application_open:
1314 * @application: a #GApplication
1315 * @files: (array length=n_files): an array of #GFiles to open
1316 * @n_files: the length of the @files array
1317 * @hint: a hint (or ""), but never %NULL
1319 * Opens the given files.
1321 * In essence, this results in the #GApplication::open signal being emitted
1322 * in the primary instance.
1324 * @n_files must be greater than zero.
1326 * @hint is simply passed through to the ::open signal. It is
1327 * intended to be used by applications that have multiple modes for
1328 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1329 * for this functionality, you should use "".
1331 * The application must be registered before calling this function
1332 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1337 g_application_open (GApplication *application,
1342 g_return_if_fail (G_IS_APPLICATION (application));
1343 g_return_if_fail (application->priv->flags &
1344 G_APPLICATION_HANDLES_OPEN);
1345 g_return_if_fail (application->priv->is_registered);
1347 if (application->priv->is_remote)
1348 g_application_impl_open (application->priv->impl,
1349 files, n_files, hint,
1350 get_platform_data (application));
1353 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1354 0, files, n_files, hint);
1359 * g_application_run:
1360 * @application: a #GApplication
1361 * @argc: the argc from main() (or 0 if @argv is %NULL)
1362 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1364 * Runs the application.
1366 * This function is intended to be run from main() and its return value
1367 * is intended to be returned by main(). Although you are expected to pass
1368 * the @argc, @argv parameters from main() to this function, it is possible
1369 * to pass %NULL if @argv is not available or commandline handling is not
1372 * First, the local_command_line() virtual function is invoked.
1373 * This function always runs on the local instance. It gets passed a pointer
1374 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1375 * that it handled (shifting up remaining arguments). See
1376 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1377 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1378 * after setting <literal>argc = g_strv_length (argv);</literal>.
1380 * The last argument to local_command_line() is a pointer to the @status
1381 * variable which can used to set the exit status that is returned from
1382 * g_application_run().
1384 * If local_command_line() returns %TRUE, the command line is expected
1385 * to be completely handled, including possibly registering as the primary
1386 * instance, calling g_application_activate() or g_application_open(), etc.
1388 * If local_command_line() returns %FALSE then the application is registered
1389 * and the #GApplication::command-line signal is emitted in the primary
1390 * instance (which may or may not be this instance). The signal handler
1391 * gets passed a #GApplicationCommandline object that (among other things)
1392 * contains the remaining commandline arguments that have not been handled
1393 * by local_command_line().
1395 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1396 * flag set then the default implementation of local_command_line()
1397 * always returns %FALSE immediately, resulting in the commandline
1398 * always being handled in the primary instance.
1400 * Otherwise, the default implementation of local_command_line() tries
1401 * to do a couple of things that are probably reasonable for most
1402 * applications. First, g_application_register() is called to attempt
1403 * to register the application. If that works, then the command line
1404 * arguments are inspected. If no commandline arguments are given, then
1405 * g_application_activate() is called. If commandline arguments are
1406 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1407 * are assumed to be filenames and g_application_open() is called.
1409 * If you need to handle commandline arguments that are not filenames,
1410 * and you don't mind commandline handling to happen in the primary
1411 * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1412 * process the commandline arguments in your #GApplication::command-line
1413 * signal handler, either manually or using the #GOptionContext API.
1415 * If you are interested in doing more complicated local handling of the
1416 * commandline then you should implement your own #GApplication subclass
1417 * and override local_command_line(). In this case, you most likely want
1418 * to return %TRUE from your local_command_line() implementation to
1419 * suppress the default handling. See
1420 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1422 * If, after the above is done, the use count of the application is zero
1423 * then the exit status is returned immediately. If the use count is
1424 * non-zero then the default main context is iterated until the use count
1425 * falls to zero, at which point 0 is returned.
1427 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1428 * use count of zero is delayed for a while (ie: the instance stays
1429 * around to provide its <emphasis>service</emphasis> to others).
1431 * Returns: the exit status
1436 g_application_run (GApplication *application,
1444 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1445 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1447 arguments = g_new (gchar *, argc + 1);
1448 for (i = 0; i < argc; i++)
1449 arguments[i] = g_strdup (argv[i]);
1450 arguments[i] = NULL;
1452 if (g_get_prgname () == NULL && argc > 0)
1456 prgname = g_path_get_basename (argv[0]);
1457 g_set_prgname (prgname);
1461 if (!G_APPLICATION_GET_CLASS (application)
1462 ->local_command_line (application, &arguments, &status))
1464 GError *error = NULL;
1466 if (!g_application_register (application, NULL, &error))
1468 g_printerr ("%s", error->message);
1469 g_error_free (error);
1473 if (application->priv->is_remote)
1475 GVariant *platform_data;
1477 platform_data = get_platform_data (application);
1478 status = g_application_impl_command_line (application->priv->impl,
1479 arguments, platform_data);
1483 GApplicationCommandLine *cmdline;
1486 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1487 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1488 "arguments", v, NULL);
1489 g_signal_emit (application,
1490 g_application_signals[SIGNAL_COMMAND_LINE],
1491 0, cmdline, &status);
1492 g_object_unref (cmdline);
1496 g_strfreev (arguments);
1498 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1499 application->priv->is_registered &&
1500 !application->priv->use_count &&
1501 !application->priv->inactivity_timeout_id)
1503 application->priv->inactivity_timeout_id =
1504 g_timeout_add (10000, inactivity_timeout_expired, application);
1507 while (application->priv->use_count || application->priv->inactivity_timeout_id)
1509 g_main_context_iteration (NULL, TRUE);
1513 if (!application->priv->is_remote)
1515 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1517 if (!application->priv->did_shutdown)
1518 g_critical ("GApplication subclass '%s' failed to chain up on"
1519 " ::shutdown (from end of override function)",
1520 G_OBJECT_TYPE_NAME (application));
1523 if (application->priv->impl)
1524 g_application_impl_flush (application->priv->impl);
1532 g_application_list_actions (GActionGroup *action_group)
1534 GApplication *application = G_APPLICATION (action_group);
1536 g_return_val_if_fail (application->priv->is_registered, NULL);
1538 if (application->priv->remote_actions != NULL)
1539 return g_action_group_list_actions (application->priv->remote_actions);
1541 else if (application->priv->actions != NULL)
1542 return g_action_group_list_actions (application->priv->actions);
1545 /* empty string array */
1546 return g_new0 (gchar *, 1);
1550 g_application_query_action (GActionGroup *group,
1551 const gchar *action_name,
1553 const GVariantType **parameter_type,
1554 const GVariantType **state_type,
1555 GVariant **state_hint,
1558 GApplication *application = G_APPLICATION (group);
1560 g_return_val_if_fail (application->priv->is_registered, FALSE);
1562 if (application->priv->remote_actions != NULL)
1563 return g_action_group_query_action (application->priv->remote_actions,
1571 if (application->priv->actions != NULL)
1572 return g_action_group_query_action (application->priv->actions,
1584 g_application_change_action_state (GActionGroup *action_group,
1585 const gchar *action_name,
1588 GApplication *application = G_APPLICATION (action_group);
1590 g_return_if_fail (application->priv->is_remote ||
1591 application->priv->actions != NULL);
1592 g_return_if_fail (application->priv->is_registered);
1594 if (application->priv->remote_actions)
1595 return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
1598 g_action_group_change_action_state (application->priv->actions,
1599 action_name, value);
1603 g_application_activate_action (GActionGroup *action_group,
1604 const gchar *action_name,
1605 GVariant *parameter)
1607 GApplication *application = G_APPLICATION (action_group);
1609 g_return_if_fail (application->priv->is_remote ||
1610 application->priv->actions != NULL);
1611 g_return_if_fail (application->priv->is_registered);
1613 if (application->priv->remote_actions)
1614 return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
1617 g_action_group_activate_action (application->priv->actions,
1618 action_name, parameter);
1622 g_application_lookup_action (GActionMap *action_map,
1623 const gchar *action_name)
1625 GApplication *application = G_APPLICATION (action_map);
1627 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions), NULL);
1629 return g_simple_action_group_lookup (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1633 g_application_add_action (GActionMap *action_map,
1636 GApplication *application = G_APPLICATION (action_map);
1638 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1640 g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (application->priv->actions), action);
1644 g_application_remove_action (GActionMap *action_map,
1645 const gchar *action_name)
1647 GApplication *application = G_APPLICATION (action_map);
1649 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
1651 g_simple_action_group_remove (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
1655 g_application_action_group_iface_init (GActionGroupInterface *iface)
1657 iface->list_actions = g_application_list_actions;
1658 iface->query_action = g_application_query_action;
1659 iface->change_action_state = g_application_change_action_state;
1660 iface->activate_action = g_application_activate_action;
1664 g_application_action_map_iface_init (GActionMapInterface *iface)
1666 iface->lookup_action = g_application_lookup_action;
1667 iface->add_action = g_application_add_action;
1668 iface->remove_action = g_application_remove_action;
1671 /* Default Application {{{1 */
1673 static GApplication *default_app;
1676 * g_application_get_default:
1677 * @returns: (transfer none): the default application for this process, or %NULL
1679 * Returns the default #GApplication instance for this process.
1681 * Normally there is only one #GApplication per process and it becomes
1682 * the default when it is created. You can exercise more control over
1683 * this by using g_application_set_default().
1685 * If there is no default application then %NULL is returned.
1690 g_application_get_default (void)
1696 * g_application_set_default:
1697 * @application: the application to set as default, or %NULL
1699 * Sets or unsets the default application for the process, as returned
1700 * by g_application_get_default().
1702 * This function does not take its own reference on @application. If
1703 * @application is destroyed then the default application will revert
1709 g_application_set_default (GApplication *application)
1711 default_app = application;
1715 /* vim:set foldmethod=marker: */