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 "gapplicationimpl.h"
29 #include "gactiongroup.h"
30 #include "gmenumodel.h"
31 #include "gsettings.h"
33 #include "gioenumtypes.h"
42 * SECTION:gapplication
43 * @title: GApplication
44 * @short_description: Core application class
46 * A #GApplication is the foundation of an application, unique for a
47 * given application identifier. The GApplication class wraps some
48 * low-level platform-specific services and is intended to act as the
49 * foundation for higher-level application classes such as
50 * #GtkApplication or #MxApplication. In general, you should not use
51 * this class outside of a higher level framework.
53 * One of the core features that GApplication provides is process
54 * uniqueness, in the context of a "session". The session concept is
55 * platform-dependent, but corresponds roughly to a graphical desktop
56 * login. When your application is launched again, its arguments
57 * are passed through platform communication to the already running
58 * program. The already running instance of the program is called the
59 * <firstterm>primary instance</firstterm>.
61 * Before using GApplication, you must choose an "application identifier".
62 * The expected form of an application identifier is very close to that of
63 * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
64 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
65 * For details on valid application identifiers, see
66 * g_application_id_is_valid().
68 * The application identifier is claimed by the application as a
69 * well-known bus name on the user's session bus. This means that the
70 * uniqueness of your application is scoped to the current session. It
71 * also means that your application may provide additional services
72 * (through registration of other object paths) at that bus name.
74 * The registration of these object paths should be done with the shared
75 * GDBus session bus. Note that due to the internal architecture of
76 * GDBus, method calls can be dispatched at any time (even if a main
77 * loop is not running). For this reason, you must ensure that any
78 * object paths that you wish to register are registered before
79 * #GApplication attempts to acquire the bus name of your application
80 * (which happens in g_application_register()). Unfortunately, this
81 * means that you cannot use g_application_get_is_remote() to decide if
82 * you want to register object paths.
84 * GApplication provides convenient life cycle management by maintaining
85 * a <firstterm>use count</firstterm> for the primary application instance.
86 * The use count can be changed using g_application_hold() and
87 * g_application_release(). If it drops to zero, the application exits.
89 * GApplication also implements the #GActionGroup interface and lets you
90 * easily export actions by adding them with g_application_set_action_group().
91 * When invoking an action by calling g_action_group_activate_action() on
92 * the application, it is always invoked in the primary instance. The actions
93 * are also exported on the session bus, and GIO provides the #GDBusActionGroup
94 * wrapper to conveniently access them remotely. Additionally,
95 * g_application_set_menu() can be used to export representation data
96 * for the actions, in the form of a #GMenuModel.
98 * There is a number of different entry points into a #GApplication:
100 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
101 * <listitem>via 'Open' (i.e. opening some files)</listitem>
102 * <listitem>by handling a command-line</listitem>
103 * <listitem>via activating an action</listitem>
105 * The #GApplication::startup signal lets you handle the application
106 * initialization for all of these in a single place.
108 * Regardless of which of these entry points is used to start the application,
109 * GApplication passes some <firstterm id="platform-data">platform
110 * data</firstterm> from the launching instance to the primary instance,
111 * in the form of a #GVariant dictionary mapping strings to variants.
112 * To use platform data, override the @before_emit or @after_emit virtual
113 * functions in your #GApplication subclass. When dealing with
114 * #GApplicationCommandline objects, the platform data is directly
115 * available via g_application_command_line_get_cwd(),
116 * g_application_command_line_get_environ() and
117 * g_application_command_line_get_platform_data().
119 * As the name indicates, the platform data may vary depending on the
120 * operating system, but it always includes the current directory (key
121 * "cwd"), and optionally the environment (ie the set of environment
122 * variables and their values) of the calling process (key "environ").
123 * The environment is only added to the platform data if the
124 * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
125 * can add their own platform data by overriding the @add_platform_data
126 * virtual function. For instance, #GtkApplication adds startup notification
129 * To parse commandline arguments you may handle the
130 * #GApplication::command-line signal or override the local_command_line()
131 * vfunc, to parse them in either the primary instance or the local instance,
134 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
136 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
137 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
142 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
144 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
145 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
151 struct _GApplicationPrivate
153 GApplicationFlags flags;
156 GActionGroup *actions;
159 guint inactivity_timeout_id;
160 guint inactivity_timeout;
163 guint is_registered : 1;
165 guint did_startup : 1;
166 guint did_shutdown : 1;
168 GActionGroup *remote_actions;
169 GApplicationImpl *impl;
179 PROP_INACTIVITY_TIMEOUT,
195 static guint g_application_signals[NR_SIGNALS];
197 static void g_application_action_group_iface_init (GActionGroupInterface *);
198 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
199 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
200 g_application_action_group_iface_init))
202 /* vfunc defaults {{{1 */
204 g_application_real_before_emit (GApplication *application,
205 GVariant *platform_data)
210 g_application_real_after_emit (GApplication *application,
211 GVariant *platform_data)
216 g_application_real_startup (GApplication *application)
218 application->priv->did_startup = TRUE;
222 g_application_real_shutdown (GApplication *application)
224 application->priv->did_shutdown = TRUE;
228 g_application_real_activate (GApplication *application)
230 if (!g_signal_has_handler_pending (application,
231 g_application_signals[SIGNAL_ACTIVATE],
233 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
235 static gboolean warned;
240 g_warning ("Your application does not implement "
241 "g_application_activate() and has no handlers connected "
242 "to the 'activate' signal. It should do one of these.");
248 g_application_real_open (GApplication *application,
253 if (!g_signal_has_handler_pending (application,
254 g_application_signals[SIGNAL_OPEN],
256 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
258 static gboolean warned;
263 g_warning ("Your application claims to support opening files "
264 "but does not implement g_application_open() and has no "
265 "handlers connected to the 'open' signal.");
271 g_application_real_command_line (GApplication *application,
272 GApplicationCommandLine *cmdline)
274 if (!g_signal_has_handler_pending (application,
275 g_application_signals[SIGNAL_COMMAND_LINE],
277 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
279 static gboolean warned;
284 g_warning ("Your application claims to support custom command line "
285 "handling but does not implement g_application_command_line() "
286 "and has no handlers connected to the 'command-line' signal.");
295 g_application_real_local_command_line (GApplication *application,
299 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
304 GError *error = NULL;
307 if (!g_application_register (application, NULL, &error))
309 g_critical ("%s", error->message);
310 g_error_free (error);
315 n_args = g_strv_length (*arguments);
317 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
319 if ((*exit_status = n_args > 1))
321 g_printerr ("GApplication service mode takes no arguments.\n");
322 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
330 g_application_activate (application);
336 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
338 g_critical ("This application can not open files.");
347 n_files = n_args - 1;
348 files = g_new (GFile *, n_files);
350 for (i = 0; i < n_files; i++)
351 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
353 g_application_open (application, files, n_files, "");
355 for (i = 0; i < n_files; i++)
356 g_object_unref (files[i]);
368 g_application_real_add_platform_data (GApplication *application,
369 GVariantBuilder *builder)
373 /* GObject implementation stuff {{{1 */
375 g_application_set_property (GObject *object,
380 GApplication *application = G_APPLICATION (object);
384 case PROP_APPLICATION_ID:
385 g_application_set_application_id (application,
386 g_value_get_string (value));
390 g_application_set_flags (application, g_value_get_flags (value));
393 case PROP_INACTIVITY_TIMEOUT:
394 g_application_set_inactivity_timeout (application,
395 g_value_get_uint (value));
398 case PROP_ACTION_GROUP:
399 g_application_set_action_group (application,
400 g_value_get_object (value));
404 g_application_set_menu (application,
405 g_value_get_object (value));
409 g_assert_not_reached ();
414 * g_application_set_action_group:
415 * @application: a #GApplication
416 * @action_group: (allow-none): a #GActionGroup, or %NULL
418 * Sets or unsets the group of actions associated with the application.
420 * These actions can be invoked remotely.
422 * It is an error to call this function after the application has been
428 g_application_set_action_group (GApplication *application,
429 GActionGroup *action_group)
431 g_return_if_fail (G_IS_APPLICATION (application));
432 g_return_if_fail (!application->priv->is_registered);
434 if (application->priv->actions != NULL)
435 g_object_unref (application->priv->actions);
437 application->priv->actions = action_group;
439 if (application->priv->actions != NULL)
440 g_object_ref (application->priv->actions);
444 * g_application_set_menu:
445 * @application: a #GApplication
446 * @menu: (allow-none): a #GMenuModel, or %NULL
448 * Sets or unsets the menu associated with the application. The menu
449 * provides representation data for the exported actions of @application.
451 * It is an error to call this function after the application has been
457 g_application_set_menu (GApplication *application,
460 g_return_if_fail (G_IS_APPLICATION (application));
461 g_return_if_fail (!application->priv->is_registered);
463 if (application->priv->menu != NULL)
464 g_object_unref (application->priv->menu);
466 application->priv->menu = menu;
468 if (application->priv->menu != NULL)
469 g_object_ref (application->priv->menu);
473 * g_application_get_menu:
474 * @application: a #GApplication
476 * Returns the menu model that has been set
477 * with g_application_set_menu().
479 * Returns: the #GMenuModel associated with @application
484 g_application_get_menu (GApplication *application)
486 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
488 return application->priv->menu;
492 g_application_get_property (GObject *object,
497 GApplication *application = G_APPLICATION (object);
501 case PROP_APPLICATION_ID:
502 g_value_set_string (value,
503 g_application_get_application_id (application));
507 g_value_set_flags (value,
508 g_application_get_flags (application));
511 case PROP_IS_REGISTERED:
512 g_value_set_boolean (value,
513 g_application_get_is_registered (application));
517 g_value_set_boolean (value,
518 g_application_get_is_remote (application));
521 case PROP_INACTIVITY_TIMEOUT:
522 g_value_set_uint (value,
523 g_application_get_inactivity_timeout (application));
527 g_assert_not_reached ();
532 g_application_constructed (GObject *object)
534 GApplication *application = G_APPLICATION (object);
536 g_assert (application->priv->id != NULL);
538 if (g_application_get_default () == NULL)
539 g_application_set_default (application);
543 g_application_finalize (GObject *object)
545 GApplication *application = G_APPLICATION (object);
547 if (application->priv->impl)
548 g_application_impl_destroy (application->priv->impl);
549 g_free (application->priv->id);
551 if (g_application_get_default () == application)
552 g_application_set_default (NULL);
554 G_OBJECT_CLASS (g_application_parent_class)
559 g_application_init (GApplication *application)
561 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
563 GApplicationPrivate);
567 g_application_class_init (GApplicationClass *class)
569 GObjectClass *object_class = G_OBJECT_CLASS (class);
571 object_class->constructed = g_application_constructed;
572 object_class->finalize = g_application_finalize;
573 object_class->get_property = g_application_get_property;
574 object_class->set_property = g_application_set_property;
576 class->before_emit = g_application_real_before_emit;
577 class->after_emit = g_application_real_after_emit;
578 class->startup = g_application_real_startup;
579 class->shutdown = g_application_real_shutdown;
580 class->activate = g_application_real_activate;
581 class->open = g_application_real_open;
582 class->command_line = g_application_real_command_line;
583 class->local_command_line = g_application_real_local_command_line;
584 class->add_platform_data = g_application_real_add_platform_data;
586 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
587 g_param_spec_string ("application-id",
588 P_("Application identifier"),
589 P_("The unique identifier for the application"),
590 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
591 G_PARAM_STATIC_STRINGS));
593 g_object_class_install_property (object_class, PROP_FLAGS,
594 g_param_spec_flags ("flags",
595 P_("Application flags"),
596 P_("Flags specifying the behaviour of the application"),
597 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
598 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
601 g_param_spec_boolean ("is-registered",
603 P_("If g_application_register() has been called"),
604 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
606 g_object_class_install_property (object_class, PROP_IS_REMOTE,
607 g_param_spec_boolean ("is-remote",
609 P_("If this application instance is remote"),
610 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
612 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
613 g_param_spec_uint ("inactivity-timeout",
614 P_("Inactivity timeout"),
615 P_("Time (ms) to stay alive after becoming idle"),
617 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
619 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
620 g_param_spec_object ("action-group",
622 P_("The group of actions that the application exports"),
624 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (object_class, PROP_MENU,
627 g_param_spec_object ("menu",
629 P_("The menu that the application exports"),
631 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
634 * GApplication::startup:
635 * @application: the application
637 * The ::startup signal is emitted on the primary instance immediately
638 * after registration. See g_application_register().
640 g_application_signals[SIGNAL_STARTUP] =
641 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
642 G_STRUCT_OFFSET (GApplicationClass, startup),
643 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
646 * GApplication::shutdown:
647 * @application: the application
649 * The ::shutdown signal is emitted only on the registered primary instance
650 * immediately after the main loop terminates.
652 g_application_signals[SIGNAL_SHUTDOWN] =
653 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
654 G_STRUCT_OFFSET (GApplicationClass, shutdown),
655 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
658 * GApplication::activate:
659 * @application: the application
661 * The ::activate signal is emitted on the primary instance when an
662 * activation occurs. See g_application_activate().
664 g_application_signals[SIGNAL_ACTIVATE] =
665 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
666 G_STRUCT_OFFSET (GApplicationClass, activate),
667 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
671 * GApplication::open:
672 * @application: the application
673 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
674 * @n_files: the length of @files
675 * @hint: a hint provided by the calling instance
677 * The ::open signal is emitted on the primary instance when there are
678 * files to open. See g_application_open() for more information.
680 g_application_signals[SIGNAL_OPEN] =
681 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
682 G_STRUCT_OFFSET (GApplicationClass, open),
684 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
687 * GApplication::command-line:
688 * @application: the application
689 * @command_line: a #GApplicationCommandLine representing the
692 * The ::command-line signal is emitted on the primary instance when
693 * a commandline is not handled locally. See g_application_run() and
694 * the #GApplicationCommandline documentation for more information.
696 * Returns: An integer that is set as the exit status for the calling
697 * process. See g_application_command_line_set_exit_status().
699 g_application_signals[SIGNAL_COMMAND_LINE] =
700 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
701 G_STRUCT_OFFSET (GApplicationClass, command_line),
702 g_signal_accumulator_first_wins, NULL,
704 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
706 g_type_class_add_private (class, sizeof (GApplicationPrivate));
710 get_platform_data (GApplication *application)
712 GVariantBuilder *builder;
715 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
718 gchar *cwd = g_get_current_dir ();
719 g_variant_builder_add (builder, "{sv}", "cwd",
720 g_variant_new_bytestring (cwd));
724 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
729 envp = g_get_environ ();
730 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
733 g_variant_builder_add (builder, "{sv}", "environ", array);
736 G_APPLICATION_GET_CLASS (application)->
737 add_platform_data (application, builder);
739 result = g_variant_builder_end (builder);
740 g_variant_builder_unref (builder);
745 /* Application ID validity {{{1 */
748 * g_application_id_is_valid:
749 * @application_id: a potential application identifier
751 * Checks if @application_id is a valid application identifier.
753 * A valid ID is required for calls to g_application_new() and
754 * g_application_set_application_id().
756 * For convenience, the restrictions on application identifiers are
759 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
760 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
761 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
762 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
763 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
766 * Returns: %TRUE if @application_id is valid
769 g_application_id_is_valid (const gchar *application_id)
775 len = strlen (application_id);
780 if (!g_ascii_isalpha (application_id[0]))
783 if (application_id[len-1] == '.')
789 for (; *application_id; application_id++)
791 if (g_ascii_isalnum (*application_id) ||
792 (*application_id == '-') ||
793 (*application_id == '_'))
797 else if (allow_dot && *application_id == '.')
812 /* Public Constructor {{{1 */
815 * @application_id: the application id
816 * @flags: the application flags
818 * Creates a new #GApplication instance.
820 * This function calls g_type_init() for you.
822 * The application id must be valid. See g_application_id_is_valid().
824 * Returns: a new #GApplication instance
827 g_application_new (const gchar *application_id,
828 GApplicationFlags flags)
830 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
834 return g_object_new (G_TYPE_APPLICATION,
835 "application-id", application_id,
840 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
842 * g_application_get_application_id:
843 * @application: a #GApplication
845 * Gets the unique identifier for @application.
847 * Returns: the identifier for @application, owned by @application
852 g_application_get_application_id (GApplication *application)
854 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
856 return application->priv->id;
860 * g_application_set_application_id:
861 * @application: a #GApplication
862 * @application_id: the identifier for @application
864 * Sets the unique identifier for @application.
866 * The application id can only be modified if @application has not yet
869 * The application id must be valid. See g_application_id_is_valid().
874 g_application_set_application_id (GApplication *application,
875 const gchar *application_id)
877 g_return_if_fail (G_IS_APPLICATION (application));
879 if (g_strcmp0 (application->priv->id, application_id) != 0)
881 g_return_if_fail (g_application_id_is_valid (application_id));
882 g_return_if_fail (!application->priv->is_registered);
884 g_free (application->priv->id);
885 application->priv->id = g_strdup (application_id);
887 g_object_notify (G_OBJECT (application), "application-id");
892 * g_application_get_flags:
893 * @application: a #GApplication
895 * Gets the flags for @application.
897 * See #GApplicationFlags.
899 * Returns: the flags for @application
904 g_application_get_flags (GApplication *application)
906 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
908 return application->priv->flags;
912 * g_application_set_flags:
913 * @application: a #GApplication
914 * @flags: the flags for @application
916 * Sets the flags for @application.
918 * The flags can only be modified if @application has not yet been
921 * See #GApplicationFlags.
926 g_application_set_flags (GApplication *application,
927 GApplicationFlags flags)
929 g_return_if_fail (G_IS_APPLICATION (application));
931 if (application->priv->flags != flags)
933 g_return_if_fail (!application->priv->is_registered);
935 application->priv->flags = flags;
937 g_object_notify (G_OBJECT (application), "flags");
942 * g_application_get_inactivity_timeout:
943 * @application: a #GApplication
945 * Gets the current inactivity timeout for the application.
947 * This is the amount of time (in milliseconds) after the last call to
948 * g_application_release() before the application stops running.
950 * Returns: the timeout, in milliseconds
955 g_application_get_inactivity_timeout (GApplication *application)
957 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
959 return application->priv->inactivity_timeout;
963 * g_application_set_inactivity_timeout:
964 * @application: a #GApplication
965 * @inactivity_timeout: the timeout, in milliseconds
967 * Sets the current inactivity timeout for the application.
969 * This is the amount of time (in milliseconds) after the last call to
970 * g_application_release() before the application stops running.
972 * This call has no side effects of its own. The value set here is only
973 * used for next time g_application_release() drops the use count to
974 * zero. Any timeouts currently in progress are not impacted.
979 g_application_set_inactivity_timeout (GApplication *application,
980 guint inactivity_timeout)
982 g_return_if_fail (G_IS_APPLICATION (application));
984 if (application->priv->inactivity_timeout != inactivity_timeout)
986 application->priv->inactivity_timeout = inactivity_timeout;
988 g_object_notify (G_OBJECT (application), "inactivity-timeout");
991 /* Read-only property getters (is registered, is remote) {{{1 */
993 * g_application_get_is_registered:
994 * @application: a #GApplication
996 * Checks if @application is registered.
998 * An application is registered if g_application_register() has been
999 * successfully called.
1001 * Returns: %TRUE if @application is registered
1006 g_application_get_is_registered (GApplication *application)
1008 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1010 return application->priv->is_registered;
1014 * g_application_get_is_remote:
1015 * @application: a #GApplication
1017 * Checks if @application is remote.
1019 * If @application is remote then it means that another instance of
1020 * application already exists (the 'primary' instance). Calls to
1021 * perform actions on @application will result in the actions being
1022 * performed by the primary instance.
1024 * The value of this property cannot be accessed before
1025 * g_application_register() has been called. See
1026 * g_application_get_is_registered().
1028 * Returns: %TRUE if @application is remote
1033 g_application_get_is_remote (GApplication *application)
1035 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1036 g_return_val_if_fail (application->priv->is_registered, FALSE);
1038 return application->priv->is_remote;
1043 * g_application_register:
1044 * @application: a #GApplication
1045 * @cancellable: a #GCancellable, or %NULL
1046 * @error: a pointer to a NULL #GError, or %NULL
1048 * Attempts registration of the application.
1050 * This is the point at which the application discovers if it is the
1051 * primary instance or merely acting as a remote for an already-existing
1052 * primary instance. This is implemented by attempting to acquire the
1053 * application identifier as a unique bus name on the session bus using
1056 * Due to the internal architecture of GDBus, method calls can be
1057 * dispatched at any time (even if a main loop is not running). For
1058 * this reason, you must ensure that any object paths that you wish to
1059 * register are registered before calling this function.
1061 * If the application has already been registered then %TRUE is
1062 * returned with no work performed.
1064 * The #GApplication::startup signal is emitted if registration succeeds
1065 * and @application is the primary instance.
1067 * In the event of an error (such as @cancellable being cancelled, or a
1068 * failure to connect to the session bus), %FALSE is returned and @error
1069 * is set appropriately.
1071 * Note: the return value of this function is not an indicator that this
1072 * instance is or is not the primary instance of the application. See
1073 * g_application_get_is_remote() for that.
1075 * Returns: %TRUE if registration succeeded
1080 g_application_register (GApplication *application,
1081 GCancellable *cancellable,
1084 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1086 if (!application->priv->is_registered)
1088 if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1090 application->priv->impl =
1091 g_application_impl_register (application, application->priv->id,
1092 application->priv->flags,
1093 &application->priv->remote_actions,
1094 cancellable, error);
1096 if (application->priv->impl == NULL)
1100 application->priv->is_remote = application->priv->remote_actions != NULL;
1101 application->priv->is_registered = TRUE;
1103 g_object_notify (G_OBJECT (application), "is-registered");
1105 if (!application->priv->is_remote)
1107 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1109 if (!application->priv->did_startup)
1110 g_critical ("GApplication subclass '%s' failed to chain up on"
1111 " ::startup (from start of override function)",
1112 G_OBJECT_TYPE_NAME (application));
1119 /* Hold/release {{{1 */
1121 * g_application_hold:
1122 * @application: a #GApplication
1124 * Increases the use count of @application.
1126 * Use this function to indicate that the application has a reason to
1127 * continue to run. For example, g_application_hold() is called by GTK+
1128 * when a toplevel window is on the screen.
1130 * To cancel the hold, call g_application_release().
1133 g_application_hold (GApplication *application)
1135 if (application->priv->inactivity_timeout_id)
1137 g_source_remove (application->priv->inactivity_timeout_id);
1138 application->priv->inactivity_timeout_id = 0;
1141 application->priv->use_count++;
1145 inactivity_timeout_expired (gpointer data)
1147 GApplication *application = G_APPLICATION (data);
1149 application->priv->inactivity_timeout_id = 0;
1156 * g_application_release:
1157 * @application: a #GApplication
1159 * Decrease the use count of @application.
1161 * When the use count reaches zero, the application will stop running.
1163 * Never call this function except to cancel the effect of a previous
1164 * call to g_application_hold().
1167 g_application_release (GApplication *application)
1169 application->priv->use_count--;
1171 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1172 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1173 inactivity_timeout_expired, application);
1176 /* Activate, Open {{{1 */
1178 * g_application_activate:
1179 * @application: a #GApplication
1181 * Activates the application.
1183 * In essence, this results in the #GApplication::activate() signal being
1184 * emitted in the primary instance.
1186 * The application must be registered before calling this function.
1191 g_application_activate (GApplication *application)
1193 g_return_if_fail (G_IS_APPLICATION (application));
1194 g_return_if_fail (application->priv->is_registered);
1196 if (application->priv->is_remote)
1197 g_application_impl_activate (application->priv->impl,
1198 get_platform_data (application));
1201 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1205 * g_application_open:
1206 * @application: a #GApplication
1207 * @files: (array length=n_files): an array of #GFiles to open
1208 * @n_files: the length of the @files array
1209 * @hint: a hint (or ""), but never %NULL
1211 * Opens the given files.
1213 * In essence, this results in the #GApplication::open signal being emitted
1214 * in the primary instance.
1216 * @n_files must be greater than zero.
1218 * @hint is simply passed through to the ::open signal. It is
1219 * intended to be used by applications that have multiple modes for
1220 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1221 * for this functionality, you should use "".
1223 * The application must be registered before calling this function
1224 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1229 g_application_open (GApplication *application,
1234 g_return_if_fail (G_IS_APPLICATION (application));
1235 g_return_if_fail (application->priv->flags &
1236 G_APPLICATION_HANDLES_OPEN);
1237 g_return_if_fail (application->priv->is_registered);
1239 if (application->priv->is_remote)
1240 g_application_impl_open (application->priv->impl,
1241 files, n_files, hint,
1242 get_platform_data (application));
1245 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1246 0, files, n_files, hint);
1251 * g_application_run:
1252 * @application: a #GApplication
1253 * @argc: the argc from main() (or 0 if @argv is %NULL)
1254 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1256 * Runs the application.
1258 * This function is intended to be run from main() and its return value
1259 * is intended to be returned by main(). Although you are expected to pass
1260 * the @argc, @argv parameters from main() to this function, it is possible
1261 * to pass %NULL if @argv is not available or commandline handling is not
1264 * First, the local_command_line() virtual function is invoked.
1265 * This function always runs on the local instance. It gets passed a pointer
1266 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1267 * that it handled (shifting up remaining arguments). See
1268 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1269 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1270 * after setting <literal>argc = g_strv_length (argv);</literal>.
1272 * The last argument to local_command_line() is a pointer to the @status
1273 * variable which can used to set the exit status that is returned from
1274 * g_application_run().
1276 * If local_command_line() returns %TRUE, the command line is expected
1277 * to be completely handled, including possibly registering as the primary
1278 * instance, calling g_application_activate() or g_application_open(), etc.
1280 * If local_command_line() returns %FALSE then the application is registered
1281 * and the #GApplication::command-line signal is emitted in the primary
1282 * instance (which may or may not be this instance). The signal handler
1283 * gets passed a #GApplicationCommandline object that (among other things)
1284 * contains the remaining commandline arguments that have not been handled
1285 * by local_command_line().
1287 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1288 * flag set then the default implementation of local_command_line()
1289 * always returns %FALSE immediately, resulting in the commandline
1290 * always being handled in the primary instance.
1292 * Otherwise, the default implementation of local_command_line() tries
1293 * to do a couple of things that are probably reasonable for most
1294 * applications. First, g_application_register() is called to attempt
1295 * to register the application. If that works, then the command line
1296 * arguments are inspected. If no commandline arguments are given, then
1297 * g_application_activate() is called. If commandline arguments are
1298 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1299 * are assumed to be filenames and g_application_open() is called.
1301 * If you need to handle commandline arguments that are not filenames,
1302 * and you don't mind commandline handling to happen in the primary
1303 * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1304 * process the commandline arguments in your #GApplication::command-line
1305 * signal handler, either manually or using the #GOptionContext API.
1307 * If you are interested in doing more complicated local handling of the
1308 * commandline then you should implement your own #GApplication subclass
1309 * and override local_command_line(). In this case, you most likely want
1310 * to return %TRUE from your local_command_line() implementation to
1311 * suppress the default handling. See
1312 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1314 * If, after the above is done, the use count of the application is zero
1315 * then the exit status is returned immediately. If the use count is
1316 * non-zero then the default main context is iterated until the use count
1317 * falls to zero, at which point 0 is returned.
1319 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1320 * use count of zero is delayed for a while (ie: the instance stays
1321 * around to provide its <emphasis>service</emphasis> to others).
1323 * Returns: the exit status
1328 g_application_run (GApplication *application,
1336 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1337 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1339 arguments = g_new (gchar *, argc + 1);
1340 for (i = 0; i < argc; i++)
1341 arguments[i] = g_strdup (argv[i]);
1342 arguments[i] = NULL;
1344 if (g_get_prgname () == NULL && argc > 0)
1348 prgname = g_path_get_basename (argv[0]);
1349 g_set_prgname (prgname);
1353 if (!G_APPLICATION_GET_CLASS (application)
1354 ->local_command_line (application, &arguments, &status))
1356 GError *error = NULL;
1358 if (!g_application_register (application, NULL, &error))
1360 g_printerr ("%s", error->message);
1361 g_error_free (error);
1365 if (application->priv->is_remote)
1367 GVariant *platform_data;
1369 platform_data = get_platform_data (application);
1370 status = g_application_impl_command_line (application->priv->impl,
1371 arguments, platform_data);
1375 GApplicationCommandLine *cmdline;
1378 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1379 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1380 "arguments", v, NULL);
1381 g_signal_emit (application,
1382 g_application_signals[SIGNAL_COMMAND_LINE],
1383 0, cmdline, &status);
1384 g_object_unref (cmdline);
1388 g_strfreev (arguments);
1390 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1391 application->priv->is_registered &&
1392 !application->priv->use_count &&
1393 !application->priv->inactivity_timeout_id)
1395 application->priv->inactivity_timeout_id =
1396 g_timeout_add (10000, inactivity_timeout_expired, application);
1399 while (application->priv->use_count || application->priv->inactivity_timeout_id)
1401 g_main_context_iteration (NULL, TRUE);
1405 if (!application->priv->is_remote)
1407 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1409 if (!application->priv->did_shutdown)
1410 g_critical ("GApplication subclass '%s' failed to chain up on"
1411 " ::shutdown (from end of override function)",
1412 G_OBJECT_TYPE_NAME (application));
1415 if (application->priv->impl)
1416 g_application_impl_flush (application->priv->impl);
1424 g_application_list_actions (GActionGroup *action_group)
1426 GApplication *application = G_APPLICATION (action_group);
1428 g_return_val_if_fail (application->priv->is_registered, NULL);
1430 if (application->priv->remote_actions != NULL)
1431 return g_action_group_list_actions (application->priv->remote_actions);
1433 else if (application->priv->actions != NULL)
1434 return g_action_group_list_actions (application->priv->actions);
1437 /* empty string array */
1438 return g_new0 (gchar *, 1);
1442 g_application_query_action (GActionGroup *group,
1443 const gchar *action_name,
1445 const GVariantType **parameter_type,
1446 const GVariantType **state_type,
1447 GVariant **state_hint,
1450 GApplication *application = G_APPLICATION (group);
1452 g_return_val_if_fail (application->priv->is_registered, FALSE);
1454 if (application->priv->remote_actions != NULL)
1455 return g_action_group_query_action (application->priv->remote_actions,
1463 if (application->priv->actions != NULL)
1464 return g_action_group_query_action (application->priv->actions,
1476 g_application_change_action_state (GActionGroup *action_group,
1477 const gchar *action_name,
1480 GApplication *application = G_APPLICATION (action_group);
1482 g_return_if_fail (application->priv->is_remote ||
1483 application->priv->actions != NULL);
1484 g_return_if_fail (application->priv->is_registered);
1486 if (application->priv->remote_actions)
1487 return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
1490 g_action_group_change_action_state (application->priv->actions,
1491 action_name, value);
1495 g_application_activate_action (GActionGroup *action_group,
1496 const gchar *action_name,
1497 GVariant *parameter)
1499 GApplication *application = G_APPLICATION (action_group);
1501 g_return_if_fail (application->priv->is_remote ||
1502 application->priv->actions != NULL);
1503 g_return_if_fail (application->priv->is_registered);
1505 if (application->priv->remote_actions)
1506 return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
1509 g_action_group_activate_action (application->priv->actions,
1510 action_name, parameter);
1514 g_application_action_group_iface_init (GActionGroupInterface *iface)
1516 iface->list_actions = g_application_list_actions;
1517 iface->query_action = g_application_query_action;
1518 iface->change_action_state = g_application_change_action_state;
1519 iface->activate_action = g_application_activate_action;
1522 /* Default Application {{{1 */
1524 static GApplication *default_app;
1527 * g_application_get_default:
1528 * @returns: (transfer none): the default application for this process, or %NULL
1530 * Returns the default #GApplication instance for this process.
1532 * Normally there is only one #GApplication per process and it becomes
1533 * the default when it is created. You can exercise more control over
1534 * this by using g_application_set_default().
1536 * If there is no default application then %NULL is returned.
1541 g_application_get_default (void)
1547 * g_application_set_default:
1548 * @application: the application to set as default, or %NULL
1550 * Sets or unsets the default application for the process, as returned
1551 * by g_application_get_default().
1553 * This function does not take its own reference on @application. If
1554 * @application is destroyed then the default application will revert
1560 g_application_set_default (GApplication *application)
1562 default_app = application;
1566 /* vim:set foldmethod=marker: */