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 "gsettings.h"
32 #include "gioenumtypes.h"
41 * SECTION:gapplication
42 * @title: GApplication
43 * @short_description: Core application class
45 * A #GApplication is the foundation of an application, unique for a
46 * given application identifier. The GApplication class wraps some
47 * low-level platform-specific services and is intended to act as the
48 * foundation for higher-level application classes such as
49 * #GtkApplication or #MxApplication. In general, you should not use
50 * this class outside of a higher level framework.
52 * One of the core features that GApplication provides is process
53 * uniqueness, in the context of a "session". The session concept is
54 * platform-dependent, but corresponds roughly to a graphical desktop
55 * login. When your application is launched again, its arguments
56 * are passed through platform communication to the already running
57 * program. The already running instance of the program is called the
58 * <firstterm>primary instance</firstterm>.
60 * Before using GApplication, you must choose an "application identifier".
61 * The expected form of an application identifier is very close to that of
62 * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
63 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
64 * For details on valid application identifiers, see
65 * g_application_id_is_valid().
67 * The application identifier is claimed by the application as a
68 * well-known bus name on the user's session bus. This means that the
69 * uniqueness of your application is scoped to the current session. It
70 * also means that your application may provide additional services
71 * (through registration of other object paths) at that bus name.
73 * The registration of these object paths should be done with the shared
74 * GDBus session bus. Note that due to the internal architecture of
75 * GDBus, method calls can be dispatched at any time (even if a main
76 * loop is not running). For this reason, you must ensure that any
77 * object paths that you wish to register are registered before
78 * #GApplication attempts to acquire the bus name of your application
79 * (which happens in g_application_register()). Unfortunately, this
80 * means that you cannot use g_application_get_is_remote() to decide if
81 * you want to register object paths.
83 * GApplication provides convenient life cycle management by maintaining
84 * a <firstterm>use count</firstterm> for the primary application instance.
85 * The use count can be changed using g_application_hold() and
86 * g_application_release(). If it drops to zero, the application exits.
88 * GApplication also implements the #GActionGroup interface and lets you
89 * easily export actions by adding them with g_application_set_action_group().
90 * When invoking an action by calling g_action_group_activate_action() on
91 * the application, it is always invoked in the primary instance.
93 * There is a number of different entry points into a #GApplication:
95 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
96 * <listitem>via 'Open' (i.e. opening some files)</listitem>
97 * <listitem>by handling a command-line</listitem>
98 * <listitem>via activating an action</listitem>
100 * The #GApplication::startup signal lets you handle the application
101 * initialization for all of these in a single place.
103 * Regardless of which of these entry points is used to start the application,
104 * GApplication passes some <firstterm id="platform-data">platform
105 * data</firstterm> from the launching instance to the primary instance,
106 * in the form of a #GVariant dictionary mapping strings to variants.
107 * To use platform data, override the @before_emit or @after_emit virtual
108 * functions in your #GApplication subclass. When dealing with
109 * #GApplicationCommandline objects, the platform data is directly
110 * available via g_application_command_line_get_cwd(),
111 * g_application_command_line_get_environ() and
112 * g_application_command_line_get_platform_data().
114 * As the name indicates, the platform data may vary depending on the
115 * operating system, but it always includes the current directory (key
116 * "cwd"), and optionally the environment (ie the set of environment
117 * variables and their values) of the calling process (key "environ").
118 * The environment is only added to the platform data if the
119 * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
120 * can add their own platform data by overriding the @add_platform_data
121 * virtual function. For instance, #GtkApplication adds startup notification
124 * To parse commandline arguments you may handle the
125 * #GApplication::command-line signal or override the local_command_line()
126 * vfunc, to parse them in either the primary instance or the local instance,
129 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
131 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
132 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
137 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
139 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
140 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
146 struct _GApplicationPrivate
148 GApplicationFlags flags;
151 GActionGroup *actions;
153 guint inactivity_timeout_id;
154 guint inactivity_timeout;
157 guint is_registered : 1;
159 guint did_startup : 1;
160 guint did_shutdown : 1;
162 GActionGroup *remote_actions;
163 GApplicationImpl *impl;
173 PROP_INACTIVITY_TIMEOUT,
188 static guint g_application_signals[NR_SIGNALS];
190 static void g_application_action_group_iface_init (GActionGroupInterface *);
191 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
192 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
193 g_application_action_group_iface_init))
195 /* vfunc defaults {{{1 */
197 g_application_real_before_emit (GApplication *application,
198 GVariant *platform_data)
203 g_application_real_after_emit (GApplication *application,
204 GVariant *platform_data)
209 g_application_real_startup (GApplication *application)
211 application->priv->did_startup = TRUE;
215 g_application_real_shutdown (GApplication *application)
217 application->priv->did_shutdown = TRUE;
221 g_application_real_activate (GApplication *application)
223 if (!g_signal_has_handler_pending (application,
224 g_application_signals[SIGNAL_ACTIVATE],
226 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
228 static gboolean warned;
233 g_warning ("Your application does not implement "
234 "g_application_activate() and has no handlers connected "
235 "to the 'activate' signal. It should do one of these.");
241 g_application_real_open (GApplication *application,
246 if (!g_signal_has_handler_pending (application,
247 g_application_signals[SIGNAL_OPEN],
249 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
251 static gboolean warned;
256 g_warning ("Your application claims to support opening files "
257 "but does not implement g_application_open() and has no "
258 "handlers connected to the 'open' signal.");
264 g_application_real_command_line (GApplication *application,
265 GApplicationCommandLine *cmdline)
267 if (!g_signal_has_handler_pending (application,
268 g_application_signals[SIGNAL_COMMAND_LINE],
270 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
272 static gboolean warned;
277 g_warning ("Your application claims to support custom command line "
278 "handling but does not implement g_application_command_line() "
279 "and has no handlers connected to the 'command-line' signal.");
288 g_application_real_local_command_line (GApplication *application,
292 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
297 GError *error = NULL;
300 if (!g_application_register (application, NULL, &error))
302 g_critical ("%s", error->message);
303 g_error_free (error);
308 n_args = g_strv_length (*arguments);
310 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
312 if ((*exit_status = n_args > 1))
314 g_printerr ("GApplication service mode takes no arguments.\n");
315 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
323 g_application_activate (application);
329 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
331 g_critical ("This application can not open files.");
340 n_files = n_args - 1;
341 files = g_new (GFile *, n_files);
343 for (i = 0; i < n_files; i++)
344 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
346 g_application_open (application, files, n_files, "");
348 for (i = 0; i < n_files; i++)
349 g_object_unref (files[i]);
361 g_application_real_add_platform_data (GApplication *application,
362 GVariantBuilder *builder)
366 /* GObject implementation stuff {{{1 */
368 g_application_set_property (GObject *object,
373 GApplication *application = G_APPLICATION (object);
377 case PROP_APPLICATION_ID:
378 g_application_set_application_id (application,
379 g_value_get_string (value));
383 g_application_set_flags (application, g_value_get_flags (value));
386 case PROP_INACTIVITY_TIMEOUT:
387 g_application_set_inactivity_timeout (application,
388 g_value_get_uint (value));
391 case PROP_ACTION_GROUP:
392 g_application_set_action_group (application,
393 g_value_get_object (value));
397 g_assert_not_reached ();
402 * g_application_set_action_group:
403 * @application: a #GApplication
404 * @action_group: (allow-none): a #GActionGroup, or %NULL
406 * Sets or unsets the group of actions associated with the application.
408 * These actions are the actions that can be remotely invoked.
410 * It is an error to call this function after the application has been
416 g_application_set_action_group (GApplication *application,
417 GActionGroup *action_group)
419 g_return_if_fail (G_IS_APPLICATION (application));
420 g_return_if_fail (!application->priv->is_registered);
422 if (application->priv->actions != NULL)
423 g_object_unref (application->priv->actions);
425 application->priv->actions = action_group;
427 if (application->priv->actions != NULL)
428 g_object_ref (application->priv->actions);
432 g_application_get_property (GObject *object,
437 GApplication *application = G_APPLICATION (object);
441 case PROP_APPLICATION_ID:
442 g_value_set_string (value,
443 g_application_get_application_id (application));
447 g_value_set_flags (value,
448 g_application_get_flags (application));
451 case PROP_IS_REGISTERED:
452 g_value_set_boolean (value,
453 g_application_get_is_registered (application));
457 g_value_set_boolean (value,
458 g_application_get_is_remote (application));
461 case PROP_INACTIVITY_TIMEOUT:
462 g_value_set_uint (value,
463 g_application_get_inactivity_timeout (application));
467 g_assert_not_reached ();
472 g_application_constructed (GObject *object)
474 GApplication *application = G_APPLICATION (object);
476 g_assert (application->priv->id != NULL);
478 if (g_application_get_default () == NULL)
479 g_application_set_default (application);
483 g_application_finalize (GObject *object)
485 GApplication *application = G_APPLICATION (object);
487 if (application->priv->impl)
488 g_application_impl_destroy (application->priv->impl);
489 g_free (application->priv->id);
491 if (g_application_get_default () == application)
492 g_application_set_default (NULL);
494 G_OBJECT_CLASS (g_application_parent_class)
499 g_application_init (GApplication *application)
501 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
503 GApplicationPrivate);
507 g_application_class_init (GApplicationClass *class)
509 GObjectClass *object_class = G_OBJECT_CLASS (class);
511 object_class->constructed = g_application_constructed;
512 object_class->finalize = g_application_finalize;
513 object_class->get_property = g_application_get_property;
514 object_class->set_property = g_application_set_property;
516 class->before_emit = g_application_real_before_emit;
517 class->after_emit = g_application_real_after_emit;
518 class->startup = g_application_real_startup;
519 class->shutdown = g_application_real_shutdown;
520 class->activate = g_application_real_activate;
521 class->open = g_application_real_open;
522 class->command_line = g_application_real_command_line;
523 class->local_command_line = g_application_real_local_command_line;
524 class->add_platform_data = g_application_real_add_platform_data;
526 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
527 g_param_spec_string ("application-id",
528 P_("Application identifier"),
529 P_("The unique identifier for the application"),
530 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
531 G_PARAM_STATIC_STRINGS));
533 g_object_class_install_property (object_class, PROP_FLAGS,
534 g_param_spec_flags ("flags",
535 P_("Application flags"),
536 P_("Flags specifying the behaviour of the application"),
537 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
541 g_param_spec_boolean ("is-registered",
543 P_("If g_application_register() has been called"),
544 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
546 g_object_class_install_property (object_class, PROP_IS_REMOTE,
547 g_param_spec_boolean ("is-remote",
549 P_("If this application instance is remote"),
550 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
552 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
553 g_param_spec_uint ("inactivity-timeout",
554 P_("Inactivity timeout"),
555 P_("Time (ms) to stay alive after becoming idle"),
557 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
559 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
560 g_param_spec_object ("action-group",
562 P_("The group of actions that the application exports"),
564 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
567 * GApplication::startup:
568 * @application: the application
570 * The ::startup signal is emitted on the primary instance immediately
571 * after registration. See g_application_register().
573 g_application_signals[SIGNAL_STARTUP] =
574 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
575 G_STRUCT_OFFSET (GApplicationClass, startup),
576 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
579 * GApplication::shutdown:
580 * @application: the application
582 * The ::shutdown signal is emitted only on the registered primary instance
583 * immediately after the main loop terminates.
585 g_application_signals[SIGNAL_SHUTDOWN] =
586 g_signal_new ("shutdown", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
587 G_STRUCT_OFFSET (GApplicationClass, shutdown),
588 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
591 * GApplication::activate:
592 * @application: the application
594 * The ::activate signal is emitted on the primary instance when an
595 * activation occurs. See g_application_activate().
597 g_application_signals[SIGNAL_ACTIVATE] =
598 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
599 G_STRUCT_OFFSET (GApplicationClass, activate),
600 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
604 * GApplication::open:
605 * @application: the application
606 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
607 * @n_files: the length of @files
608 * @hint: a hint provided by the calling instance
610 * The ::open signal is emitted on the primary instance when there are
611 * files to open. See g_application_open() for more information.
613 g_application_signals[SIGNAL_OPEN] =
614 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
615 G_STRUCT_OFFSET (GApplicationClass, open),
617 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
620 * GApplication::command-line:
621 * @application: the application
622 * @command_line: a #GApplicationCommandLine representing the
625 * The ::command-line signal is emitted on the primary instance when
626 * a commandline is not handled locally. See g_application_run() and
627 * the #GApplicationCommandline documentation for more information.
629 * Returns: An integer that is set as the exit status for the calling
630 * process. See g_application_command_line_set_exit_status().
632 g_application_signals[SIGNAL_COMMAND_LINE] =
633 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
634 G_STRUCT_OFFSET (GApplicationClass, command_line),
635 g_signal_accumulator_first_wins, NULL,
637 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
639 g_type_class_add_private (class, sizeof (GApplicationPrivate));
643 get_platform_data (GApplication *application)
645 GVariantBuilder *builder;
648 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
651 gchar *cwd = g_get_current_dir ();
652 g_variant_builder_add (builder, "{sv}", "cwd",
653 g_variant_new_bytestring (cwd));
657 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
662 envp = g_get_environ ();
663 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
666 g_variant_builder_add (builder, "{sv}", "environ", array);
669 G_APPLICATION_GET_CLASS (application)->
670 add_platform_data (application, builder);
672 result = g_variant_builder_end (builder);
673 g_variant_builder_unref (builder);
678 /* Application ID validity {{{1 */
681 * g_application_id_is_valid:
682 * @application_id: a potential application identifier
684 * Checks if @application_id is a valid application identifier.
686 * A valid ID is required for calls to g_application_new() and
687 * g_application_set_application_id().
689 * For convenience, the restrictions on application identifiers are
692 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
693 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
694 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
695 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
696 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
699 * Returns: %TRUE if @application_id is valid
702 g_application_id_is_valid (const gchar *application_id)
708 len = strlen (application_id);
713 if (!g_ascii_isalpha (application_id[0]))
716 if (application_id[len-1] == '.')
722 for (; *application_id; application_id++)
724 if (g_ascii_isalnum (*application_id) ||
725 (*application_id == '-') ||
726 (*application_id == '_'))
730 else if (allow_dot && *application_id == '.')
745 /* Public Constructor {{{1 */
748 * @application_id: the application id
749 * @flags: the application flags
751 * Creates a new #GApplication instance.
753 * This function calls g_type_init() for you.
755 * The application id must be valid. See g_application_id_is_valid().
757 * Returns: a new #GApplication instance
760 g_application_new (const gchar *application_id,
761 GApplicationFlags flags)
763 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
767 return g_object_new (G_TYPE_APPLICATION,
768 "application-id", application_id,
773 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
775 * g_application_get_application_id:
776 * @application: a #GApplication
778 * Gets the unique identifier for @application.
780 * Returns: the identifier for @application, owned by @application
785 g_application_get_application_id (GApplication *application)
787 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
789 return application->priv->id;
793 * g_application_set_application_id:
794 * @application: a #GApplication
795 * @application_id: the identifier for @application
797 * Sets the unique identifier for @application.
799 * The application id can only be modified if @application has not yet
802 * The application id must be valid. See g_application_id_is_valid().
807 g_application_set_application_id (GApplication *application,
808 const gchar *application_id)
810 g_return_if_fail (G_IS_APPLICATION (application));
812 if (g_strcmp0 (application->priv->id, application_id) != 0)
814 g_return_if_fail (g_application_id_is_valid (application_id));
815 g_return_if_fail (!application->priv->is_registered);
817 g_free (application->priv->id);
818 application->priv->id = g_strdup (application_id);
820 g_object_notify (G_OBJECT (application), "application-id");
825 * g_application_get_flags:
826 * @application: a #GApplication
828 * Gets the flags for @application.
830 * See #GApplicationFlags.
832 * Returns: the flags for @application
837 g_application_get_flags (GApplication *application)
839 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
841 return application->priv->flags;
845 * g_application_set_flags:
846 * @application: a #GApplication
847 * @flags: the flags for @application
849 * Sets the flags for @application.
851 * The flags can only be modified if @application has not yet been
854 * See #GApplicationFlags.
859 g_application_set_flags (GApplication *application,
860 GApplicationFlags flags)
862 g_return_if_fail (G_IS_APPLICATION (application));
864 if (application->priv->flags != flags)
866 g_return_if_fail (!application->priv->is_registered);
868 application->priv->flags = flags;
870 g_object_notify (G_OBJECT (application), "flags");
875 * g_application_get_inactivity_timeout:
876 * @application: a #GApplication
878 * Gets the current inactivity timeout for the application.
880 * This is the amount of time (in milliseconds) after the last call to
881 * g_application_release() before the application stops running.
883 * Returns: the timeout, in milliseconds
888 g_application_get_inactivity_timeout (GApplication *application)
890 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
892 return application->priv->inactivity_timeout;
896 * g_application_set_inactivity_timeout:
897 * @application: a #GApplication
898 * @inactivity_timeout: the timeout, in milliseconds
900 * Sets the current inactivity timeout for the application.
902 * This is the amount of time (in milliseconds) after the last call to
903 * g_application_release() before the application stops running.
905 * This call has no side effects of its own. The value set here is only
906 * used for next time g_application_release() drops the use count to
907 * zero. Any timeouts currently in progress are not impacted.
912 g_application_set_inactivity_timeout (GApplication *application,
913 guint inactivity_timeout)
915 g_return_if_fail (G_IS_APPLICATION (application));
917 if (application->priv->inactivity_timeout != inactivity_timeout)
919 application->priv->inactivity_timeout = inactivity_timeout;
921 g_object_notify (G_OBJECT (application), "inactivity-timeout");
924 /* Read-only property getters (is registered, is remote) {{{1 */
926 * g_application_get_is_registered:
927 * @application: a #GApplication
929 * Checks if @application is registered.
931 * An application is registered if g_application_register() has been
932 * successfully called.
934 * Returns: %TRUE if @application is registered
939 g_application_get_is_registered (GApplication *application)
941 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
943 return application->priv->is_registered;
947 * g_application_get_is_remote:
948 * @application: a #GApplication
950 * Checks if @application is remote.
952 * If @application is remote then it means that another instance of
953 * application already exists (the 'primary' instance). Calls to
954 * perform actions on @application will result in the actions being
955 * performed by the primary instance.
957 * The value of this property cannot be accessed before
958 * g_application_register() has been called. See
959 * g_application_get_is_registered().
961 * Returns: %TRUE if @application is remote
966 g_application_get_is_remote (GApplication *application)
968 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
969 g_return_val_if_fail (application->priv->is_registered, FALSE);
971 return application->priv->is_remote;
976 * g_application_register:
977 * @application: a #GApplication
978 * @cancellable: a #GCancellable, or %NULL
979 * @error: a pointer to a NULL #GError, or %NULL
981 * Attempts registration of the application.
983 * This is the point at which the application discovers if it is the
984 * primary instance or merely acting as a remote for an already-existing
985 * primary instance. This is implemented by attempting to acquire the
986 * application identifier as a unique bus name on the session bus using
989 * Due to the internal architecture of GDBus, method calls can be
990 * dispatched at any time (even if a main loop is not running). For
991 * this reason, you must ensure that any object paths that you wish to
992 * register are registered before calling this function.
994 * If the application has already been registered then %TRUE is
995 * returned with no work performed.
997 * The #GApplication::startup signal is emitted if registration succeeds
998 * and @application is the primary instance.
1000 * In the event of an error (such as @cancellable being cancelled, or a
1001 * failure to connect to the session bus), %FALSE is returned and @error
1002 * is set appropriately.
1004 * Note: the return value of this function is not an indicator that this
1005 * instance is or is not the primary instance of the application. See
1006 * g_application_get_is_remote() for that.
1008 * Returns: %TRUE if registration succeeded
1013 g_application_register (GApplication *application,
1014 GCancellable *cancellable,
1017 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1019 if (!application->priv->is_registered)
1021 if (~application->priv->flags & G_APPLICATION_NON_UNIQUE)
1023 application->priv->impl =
1024 g_application_impl_register (application, application->priv->id,
1025 application->priv->flags,
1026 &application->priv->remote_actions,
1027 cancellable, error);
1029 if (application->priv->impl == NULL)
1033 application->priv->is_remote = application->priv->remote_actions != NULL;
1034 application->priv->is_registered = TRUE;
1036 g_object_notify (G_OBJECT (application), "is-registered");
1038 if (!application->priv->is_remote)
1040 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1042 if (!application->priv->did_startup)
1043 g_critical ("GApplication subclass '%s' failed to chain up on"
1044 " ::startup (from start of override function)",
1045 G_OBJECT_TYPE_NAME (application));
1052 /* Hold/release {{{1 */
1054 * g_application_hold:
1055 * @application: a #GApplication
1057 * Increases the use count of @application.
1059 * Use this function to indicate that the application has a reason to
1060 * continue to run. For example, g_application_hold() is called by GTK+
1061 * when a toplevel window is on the screen.
1063 * To cancel the hold, call g_application_release().
1066 g_application_hold (GApplication *application)
1068 if (application->priv->inactivity_timeout_id)
1070 g_source_remove (application->priv->inactivity_timeout_id);
1071 application->priv->inactivity_timeout_id = 0;
1074 application->priv->use_count++;
1078 inactivity_timeout_expired (gpointer data)
1080 GApplication *application = G_APPLICATION (data);
1082 application->priv->inactivity_timeout_id = 0;
1089 * g_application_release:
1090 * @application: a #GApplication
1092 * Decrease the use count of @application.
1094 * When the use count reaches zero, the application will stop running.
1096 * Never call this function except to cancel the effect of a previous
1097 * call to g_application_hold().
1100 g_application_release (GApplication *application)
1102 application->priv->use_count--;
1104 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
1105 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
1106 inactivity_timeout_expired, application);
1109 /* Activate, Open {{{1 */
1111 * g_application_activate:
1112 * @application: a #GApplication
1114 * Activates the application.
1116 * In essence, this results in the #GApplication::activate() signal being
1117 * emitted in the primary instance.
1119 * The application must be registered before calling this function.
1124 g_application_activate (GApplication *application)
1126 g_return_if_fail (G_IS_APPLICATION (application));
1127 g_return_if_fail (application->priv->is_registered);
1129 if (application->priv->is_remote)
1130 g_application_impl_activate (application->priv->impl,
1131 get_platform_data (application));
1134 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1138 * g_application_open:
1139 * @application: a #GApplication
1140 * @files: (array length=n_files): an array of #GFiles to open
1141 * @n_files: the length of the @files array
1142 * @hint: a hint (or ""), but never %NULL
1144 * Opens the given files.
1146 * In essence, this results in the #GApplication::open signal being emitted
1147 * in the primary instance.
1149 * @n_files must be greater than zero.
1151 * @hint is simply passed through to the ::open signal. It is
1152 * intended to be used by applications that have multiple modes for
1153 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1154 * for this functionality, you should use "".
1156 * The application must be registered before calling this function
1157 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1162 g_application_open (GApplication *application,
1167 g_return_if_fail (G_IS_APPLICATION (application));
1168 g_return_if_fail (application->priv->flags &
1169 G_APPLICATION_HANDLES_OPEN);
1170 g_return_if_fail (application->priv->is_registered);
1172 if (application->priv->is_remote)
1173 g_application_impl_open (application->priv->impl,
1174 files, n_files, hint,
1175 get_platform_data (application));
1178 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1179 0, files, n_files, hint);
1184 * g_application_run:
1185 * @application: a #GApplication
1186 * @argc: the argc from main() (or 0 if @argv is %NULL)
1187 * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
1189 * Runs the application.
1191 * This function is intended to be run from main() and its return value
1192 * is intended to be returned by main(). Although you are expected to pass
1193 * the @argc, @argv parameters from main() to this function, it is possible
1194 * to pass %NULL if @argv is not available or commandline handling is not
1197 * First, the local_command_line() virtual function is invoked.
1198 * This function always runs on the local instance. It gets passed a pointer
1199 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1200 * that it handled (shifting up remaining arguments). See
1201 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1202 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1203 * after setting <literal>argc = g_strv_length (argv);</literal>.
1205 * The last argument to local_command_line() is a pointer to the @status
1206 * variable which can used to set the exit status that is returned from
1207 * g_application_run().
1209 * If local_command_line() returns %TRUE, the command line is expected
1210 * to be completely handled, including possibly registering as the primary
1211 * instance, calling g_application_activate() or g_application_open(), etc.
1213 * If local_command_line() returns %FALSE then the application is registered
1214 * and the #GApplication::command-line signal is emitted in the primary
1215 * instance (which may or may not be this instance). The signal handler
1216 * gets passed a #GApplicationCommandline object that (among other things)
1217 * contains the remaining commandline arguments that have not been handled
1218 * by local_command_line().
1220 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1221 * flag set then the default implementation of local_command_line()
1222 * always returns %FALSE immediately, resulting in the commandline
1223 * always being handled in the primary instance.
1225 * Otherwise, the default implementation of local_command_line() tries
1226 * to do a couple of things that are probably reasonable for most
1227 * applications. First, g_application_register() is called to attempt
1228 * to register the application. If that works, then the command line
1229 * arguments are inspected. If no commandline arguments are given, then
1230 * g_application_activate() is called. If commandline arguments are
1231 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1232 * are assumed to be filenames and g_application_open() is called.
1234 * If you need to handle commandline arguments that are not filenames,
1235 * and you don't mind commandline handling to happen in the primary
1236 * instance, you should set %G_APPLICATION_HANDLED_COMMAND_LINE and
1237 * process the commandline arguments in your #GApplication::command-line
1238 * signal handler, either manually or using the #GOptionContext API.
1240 * If you are interested in doing more complicated local handling of the
1241 * commandline then you should implement your own #GApplication subclass
1242 * and override local_command_line(). In this case, you most likely want
1243 * to return %TRUE from your local_command_line() implementation to
1244 * suppress the default handling. See
1245 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1247 * If, after the above is done, the use count of the application is zero
1248 * then the exit status is returned immediately. If the use count is
1249 * non-zero then the default main context is iterated until the use count
1250 * falls to zero, at which point 0 is returned.
1252 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1253 * use count of zero is delayed for a while (ie: the instance stays
1254 * around to provide its <emphasis>service</emphasis> to others).
1256 * Returns: the exit status
1261 g_application_run (GApplication *application,
1269 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1270 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1272 arguments = g_new (gchar *, argc + 1);
1273 for (i = 0; i < argc; i++)
1274 arguments[i] = g_strdup (argv[i]);
1275 arguments[i] = NULL;
1277 if (g_get_prgname () == NULL && argc > 0)
1281 prgname = g_path_get_basename (argv[0]);
1282 g_set_prgname (prgname);
1286 if (!G_APPLICATION_GET_CLASS (application)
1287 ->local_command_line (application, &arguments, &status))
1289 GError *error = NULL;
1291 if (!g_application_register (application, NULL, &error))
1293 g_printerr ("%s", error->message);
1294 g_error_free (error);
1298 if (application->priv->is_remote)
1300 GVariant *platform_data;
1302 platform_data = get_platform_data (application);
1303 status = g_application_impl_command_line (application->priv->impl,
1304 arguments, platform_data);
1308 GApplicationCommandLine *cmdline;
1311 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1312 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1313 "arguments", v, NULL);
1314 g_signal_emit (application,
1315 g_application_signals[SIGNAL_COMMAND_LINE],
1316 0, cmdline, &status);
1317 g_object_unref (cmdline);
1321 g_strfreev (arguments);
1323 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1324 application->priv->is_registered &&
1325 !application->priv->use_count &&
1326 !application->priv->inactivity_timeout_id)
1328 application->priv->inactivity_timeout_id =
1329 g_timeout_add (10000, inactivity_timeout_expired, application);
1332 while (application->priv->use_count || application->priv->inactivity_timeout_id)
1334 g_main_context_iteration (NULL, TRUE);
1338 if (!application->priv->is_remote)
1340 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
1342 if (!application->priv->did_shutdown)
1343 g_critical ("GApplication subclass '%s' failed to chain up on"
1344 " ::shutdown (from end of override function)",
1345 G_OBJECT_TYPE_NAME (application));
1348 if (application->priv->impl)
1349 g_application_impl_flush (application->priv->impl);
1357 g_application_list_actions (GActionGroup *action_group)
1359 GApplication *application = G_APPLICATION (action_group);
1361 g_return_val_if_fail (application->priv->is_registered, NULL);
1363 if (application->priv->remote_actions != NULL)
1364 return g_action_group_list_actions (application->priv->remote_actions);
1366 else if (application->priv->actions != NULL)
1367 return g_action_group_list_actions (application->priv->actions);
1370 /* empty string array */
1371 return g_new0 (gchar *, 1);
1375 g_application_query_action (GActionGroup *group,
1376 const gchar *action_name,
1378 const GVariantType **parameter_type,
1379 const GVariantType **state_type,
1380 GVariant **state_hint,
1383 GApplication *application = G_APPLICATION (group);
1385 g_return_val_if_fail (application->priv->is_registered, FALSE);
1387 if (application->priv->remote_actions != NULL)
1388 return g_action_group_query_action (application->priv->remote_actions,
1396 if (application->priv->actions != NULL)
1397 return g_action_group_query_action (application->priv->actions,
1409 g_application_change_action_state (GActionGroup *action_group,
1410 const gchar *action_name,
1413 GApplication *application = G_APPLICATION (action_group);
1415 g_return_if_fail (application->priv->is_remote ||
1416 application->priv->actions != NULL);
1417 g_return_if_fail (application->priv->is_registered);
1419 if (application->priv->remote_actions)
1420 return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
1423 g_action_group_change_action_state (application->priv->actions,
1424 action_name, value);
1428 g_application_activate_action (GActionGroup *action_group,
1429 const gchar *action_name,
1430 GVariant *parameter)
1432 GApplication *application = G_APPLICATION (action_group);
1434 g_return_if_fail (application->priv->is_remote ||
1435 application->priv->actions != NULL);
1436 g_return_if_fail (application->priv->is_registered);
1438 if (application->priv->remote_actions)
1439 return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
1442 g_action_group_activate_action (application->priv->actions,
1443 action_name, parameter);
1447 g_application_action_group_iface_init (GActionGroupInterface *iface)
1449 iface->list_actions = g_application_list_actions;
1450 iface->query_action = g_application_query_action;
1451 iface->change_action_state = g_application_change_action_state;
1452 iface->activate_action = g_application_activate_action;
1455 /* Default Application {{{1 */
1457 static GApplication *default_app;
1460 * g_application_get_default:
1461 * @returns: (transfer none): the default application for this process, or %NULL
1463 * Returns the default #GApplication instance for this process.
1465 * Normally there is only one #GApplication per process and it becomes
1466 * the default when it is created. You can exercise more control over
1467 * this by using g_application_set_default().
1469 * If there is no default application then %NULL is returned.
1474 g_application_get_default (void)
1480 * g_application_set_default:
1481 * @application: the application to set as default, or %NULL
1483 * Sets or unsets the default application for the process, as returned
1484 * by g_application_get_default().
1486 * This function does not take its own reference on @application. If
1487 * @application is destroyed then the default application will revert
1493 g_application_set_default (GApplication *application)
1495 default_app = application;
1499 /* vim:set foldmethod=marker: */