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"
31 #include "gioenumtypes.h"
32 #include "gio-marshal.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 can not 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;
154 guint inactivity_timeout_id;
155 guint inactivity_timeout;
158 guint is_registered : 1;
161 GHashTable *remote_actions; /* string -> RemoteActionInfo */
162 GApplicationImpl *impl;
172 PROP_INACTIVITY_TIMEOUT,
186 static guint g_application_signals[NR_SIGNALS];
188 static void g_application_action_group_iface_init (GActionGroupInterface *);
189 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
190 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
191 g_application_action_group_iface_init))
193 /* vfunc defaults {{{1 */
195 g_application_real_before_emit (GApplication *application,
196 GVariant *platform_data)
201 g_application_real_after_emit (GApplication *application,
202 GVariant *platform_data)
207 g_application_real_startup (GApplication *application)
212 g_application_real_activate (GApplication *application)
214 if (!g_signal_has_handler_pending (application,
215 g_application_signals[SIGNAL_ACTIVATE],
217 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
219 static gboolean warned;
224 g_warning ("Your application does not implement "
225 "g_application_activate() and has no handlers connected "
226 "to the 'activate' signal. It should do one of these.");
232 g_application_real_open (GApplication *application,
237 if (!g_signal_has_handler_pending (application,
238 g_application_signals[SIGNAL_OPEN],
240 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
242 static gboolean warned;
247 g_warning ("Your application claims to support opening files "
248 "but does not implement g_application_open() and has no "
249 "handlers connected to the 'open' signal.");
255 g_application_real_command_line (GApplication *application,
256 GApplicationCommandLine *cmdline)
258 static gboolean warned;
263 g_warning ("Your application claims to support custom command line "
264 "handling but does not implement g_application_command_line() "
265 "and has no handlers connected to the 'command-line' signal.");
273 g_application_real_local_command_line (GApplication *application,
277 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
282 GError *error = NULL;
285 if (!g_application_register (application, NULL, &error))
287 g_critical ("%s", error->message);
288 g_error_free (error);
293 n_args = g_strv_length (*arguments);
295 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
297 if ((*exit_status = n_args > 1))
299 g_printerr ("GApplication service mode takes no arguments.\n");
300 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
308 g_application_activate (application);
314 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
316 g_critical ("This application can not open files.");
325 n_files = n_args - 1;
326 files = g_new (GFile *, n_files);
328 for (i = 0; i < n_files; i++)
329 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
331 g_application_open (application, files, n_files, "");
333 for (i = 0; i < n_files; i++)
334 g_object_unref (files[i]);
346 g_application_real_add_platform_data (GApplication *application,
347 GVariantBuilder *builder)
352 g_application_real_quit_mainloop (GApplication *application)
354 if (application->priv->mainloop != NULL)
355 g_main_loop_quit (application->priv->mainloop);
359 g_application_real_run_mainloop (GApplication *application)
361 if (application->priv->mainloop == NULL)
362 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
364 g_main_loop_run (application->priv->mainloop);
367 /* GObject implementation stuff {{{1 */
369 g_application_set_property (GObject *object,
374 GApplication *application = G_APPLICATION (object);
378 case PROP_APPLICATION_ID:
379 g_application_set_application_id (application,
380 g_value_get_string (value));
384 g_application_set_flags (application, g_value_get_flags (value));
387 case PROP_INACTIVITY_TIMEOUT:
388 g_application_set_inactivity_timeout (application,
389 g_value_get_uint (value));
392 case PROP_ACTION_GROUP:
393 g_application_set_action_group (application,
394 g_value_get_object (value));
398 g_assert_not_reached ();
403 * g_application_set_action_group:
404 * @application: a #GApplication
405 * @action_group: (allow-none): a #GActionGroup, or %NULL
407 * Sets or unsets the group of actions associated with the application.
409 * These actions are the actions that can be remotely invoked.
411 * It is an error to call this function after the application has been
417 g_application_set_action_group (GApplication *application,
418 GActionGroup *action_group)
420 g_return_if_fail (G_IS_APPLICATION (application));
421 g_return_if_fail (!application->priv->is_registered);
423 if (application->priv->actions != NULL)
424 g_object_unref (application->priv->actions);
426 application->priv->actions = action_group;
428 if (application->priv->actions != NULL)
429 g_object_ref (application->priv->actions);
433 g_application_get_property (GObject *object,
438 GApplication *application = G_APPLICATION (object);
442 case PROP_APPLICATION_ID:
443 g_value_set_string (value,
444 g_application_get_application_id (application));
448 g_value_set_flags (value,
449 g_application_get_flags (application));
452 case PROP_IS_REGISTERED:
453 g_value_set_boolean (value,
454 g_application_get_is_registered (application));
458 g_value_set_boolean (value,
459 g_application_get_is_remote (application));
462 case PROP_INACTIVITY_TIMEOUT:
463 g_value_set_uint (value,
464 g_application_get_inactivity_timeout (application));
468 g_assert_not_reached ();
473 g_application_constructed (GObject *object)
475 GApplication *application = G_APPLICATION (object);
477 g_assert (application->priv->id != NULL);
481 g_application_finalize (GObject *object)
483 GApplication *application = G_APPLICATION (object);
485 if (application->priv->impl)
486 g_application_impl_destroy (application->priv->impl);
487 g_free (application->priv->id);
489 if (application->priv->mainloop)
490 g_main_loop_unref (application->priv->mainloop);
492 G_OBJECT_CLASS (g_application_parent_class)
497 g_application_init (GApplication *application)
499 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
501 GApplicationPrivate);
505 g_application_class_init (GApplicationClass *class)
507 GObjectClass *object_class = G_OBJECT_CLASS (class);
509 object_class->constructed = g_application_constructed;
510 object_class->finalize = g_application_finalize;
511 object_class->get_property = g_application_get_property;
512 object_class->set_property = g_application_set_property;
514 class->before_emit = g_application_real_before_emit;
515 class->after_emit = g_application_real_after_emit;
516 class->startup = g_application_real_startup;
517 class->activate = g_application_real_activate;
518 class->open = g_application_real_open;
519 class->command_line = g_application_real_command_line;
520 class->local_command_line = g_application_real_local_command_line;
521 class->add_platform_data = g_application_real_add_platform_data;
522 class->quit_mainloop = g_application_real_quit_mainloop;
523 class->run_mainloop = g_application_real_run_mainloop;
525 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
526 g_param_spec_string ("application-id",
527 P_("Application identifier"),
528 P_("The unique identifier for the application"),
529 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
530 G_PARAM_STATIC_STRINGS));
532 g_object_class_install_property (object_class, PROP_FLAGS,
533 g_param_spec_flags ("flags",
534 P_("Application flags"),
535 P_("Flags specifying the behaviour of the application"),
536 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
540 g_param_spec_boolean ("is-registered",
542 P_("If g_application_register() has been called"),
543 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
545 g_object_class_install_property (object_class, PROP_IS_REMOTE,
546 g_param_spec_boolean ("is-remote",
548 P_("If this application instance is remote"),
549 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
551 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
552 g_param_spec_uint ("inactivity-timeout",
553 P_("Inactivity timeout"),
554 P_("Iime (ms) to stay alive after becoming idle"),
556 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
559 g_param_spec_object ("action-group",
561 P_("The group of actions that the application exports"),
563 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
566 * GApplication::startup:
567 * @application: the application
569 * The ::startup signal is emitted on the primary instance immediately
570 * after registration. See g_application_register().
572 g_application_signals[SIGNAL_STARTUP] =
573 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
574 G_STRUCT_OFFSET (GApplicationClass, startup),
575 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
578 * GApplication::activate:
579 * @application: the application
581 * The ::activate signal is emitted on the primary instance when an
582 * activation occurs. See g_application_activate().
584 g_application_signals[SIGNAL_ACTIVATE] =
585 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
586 G_STRUCT_OFFSET (GApplicationClass, activate),
587 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
591 * GApplication::open:
592 * @application: the application
593 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
594 * @n_files: the length of @files
595 * @hint: a hint provided by the calling instance
597 * The ::open signal is emitted on the primary instance when there are
598 * files to open. See g_application_open() for more information.
600 g_application_signals[SIGNAL_OPEN] =
601 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
602 G_STRUCT_OFFSET (GApplicationClass, open),
603 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
604 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
607 * GApplication::command-line:
608 * @application: the application
609 * @command_line: a #GApplicationCommandLine representing the
612 * The ::command-line signal is emitted on the primary instance when
613 * a commandline is not handled locally. See g_application_run() and
614 * the #GApplicationCommandline documentation for more information.
616 * Returns: An integer that is set as the exit status for the calling
617 * process. See g_application_command_line_set_exit_status().
619 g_application_signals[SIGNAL_COMMAND_LINE] =
620 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
621 G_STRUCT_OFFSET (GApplicationClass, command_line),
622 g_signal_accumulator_first_wins, NULL,
623 _gio_marshal_INT__OBJECT,
624 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
626 g_type_class_add_private (class, sizeof (GApplicationPrivate));
630 get_platform_data (GApplication *application)
632 GVariantBuilder *builder;
635 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
638 gchar *cwd = g_get_current_dir ();
639 g_variant_builder_add (builder, "{sv}", "cwd",
640 g_variant_new_bytestring (cwd));
644 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
649 envp = g_get_environ ();
650 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
653 g_variant_builder_add (builder, "{sv}", "environ", array);
656 G_APPLICATION_GET_CLASS (application)->
657 add_platform_data (application, builder);
659 result = g_variant_builder_end (builder);
660 g_variant_builder_unref (builder);
665 /* Application ID validity {{{1 */
668 * g_application_id_is_valid:
669 * @application_id: a potential application identifier
670 * @returns: %TRUE if @application_id is valid
672 * Checks if @application_id is a valid application identifier.
674 * A valid ID is required for calls to g_application_new() and
675 * g_application_set_application_id().
677 * For convenience, the restrictions on application identifiers are
680 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
681 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
682 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
683 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
684 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
688 g_application_id_is_valid (const gchar *application_id)
694 len = strlen (application_id);
699 if (!g_ascii_isalpha (application_id[0]))
702 if (application_id[len-1] == '.')
708 for (; *application_id; application_id++)
710 if (g_ascii_isalnum (*application_id) ||
711 (*application_id == '-') ||
712 (*application_id == '_'))
716 else if (allow_dot && *application_id == '.')
731 /* Public Constructor {{{1 */
734 * @application_id: the application id
735 * @flags: the application flags
736 * @returns: a new #GApplication instance
738 * Creates a new #GApplication instance.
740 * This function calls g_type_init() for you.
742 * The application id must be valid. See g_application_id_is_valid().
745 g_application_new (const gchar *application_id,
746 GApplicationFlags flags)
748 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
752 return g_object_new (G_TYPE_APPLICATION,
753 "application-id", application_id,
758 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
760 * g_application_get_application_id:
761 * @application: a #GApplication
762 * @returns: the identifier for @application, owned by @application
764 * Gets the unique identifier for @application.
769 g_application_get_application_id (GApplication *application)
771 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
773 return application->priv->id;
777 * g_application_set_application_id:
778 * @application: a #GApplication
779 * @application_id: the identifier for @application
781 * Sets the unique identifier for @application.
783 * The application id can only be modified if @application has not yet
786 * The application id must be valid. See g_application_id_is_valid().
791 g_application_set_application_id (GApplication *application,
792 const gchar *application_id)
794 g_return_if_fail (G_IS_APPLICATION (application));
796 if (g_strcmp0 (application->priv->id, application_id) != 0)
798 g_return_if_fail (g_application_id_is_valid (application_id));
799 g_return_if_fail (!application->priv->is_registered);
801 g_free (application->priv->id);
802 application->priv->id = g_strdup (application_id);
804 g_object_notify (G_OBJECT (application), "application-id");
809 * g_application_get_flags:
810 * @application: a #GApplication
811 * @returns: the flags for @application
813 * Gets the flags for @application.
815 * See #GApplicationFlags.
820 g_application_get_flags (GApplication *application)
822 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
824 return application->priv->flags;
828 * g_application_set_flags:
829 * @application: a #GApplication
830 * @flags: the flags for @application
832 * Sets the flags for @application.
834 * The flags can only be modified if @application has not yet been
837 * See #GApplicationFlags.
842 g_application_set_flags (GApplication *application,
843 GApplicationFlags flags)
845 g_return_if_fail (G_IS_APPLICATION (application));
847 if (application->priv->flags != flags)
849 g_return_if_fail (!application->priv->is_registered);
851 application->priv->flags = flags;
853 g_object_notify (G_OBJECT (application), "flags");
858 * g_application_get_inactivity_timeout:
859 * @application: a #GApplication
861 * Gets the current inactivity timeout for the application.
863 * This is the amount of time (in milliseconds) after the last call to
864 * g_application_release() before the application stops running.
866 * Returns: the timeout, in milliseconds
871 g_application_get_inactivity_timeout (GApplication *application)
873 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
875 return application->priv->inactivity_timeout;
879 * g_application_set_inactivity_timeout:
880 * @application: a #GApplication
881 * @inactivity_timeout: the timeout, in milliseconds
883 * Sets the current inactivity timeout for the application.
885 * This is the amount of time (in milliseconds) after the last call to
886 * g_application_release() before the application stops running.
888 * This call has no side effects of its own. The value set here is only
889 * used for next time g_application_release() drops the use count to
890 * zero. Any timeouts currently in progress are not impacted.
892 * Returns: the timeout, in milliseconds
897 g_application_set_inactivity_timeout (GApplication *application,
898 guint inactivity_timeout)
900 g_return_if_fail (G_IS_APPLICATION (application));
902 if (application->priv->inactivity_timeout != inactivity_timeout)
904 application->priv->inactivity_timeout = inactivity_timeout;
906 g_object_notify (G_OBJECT (application), "inactivity-timeout");
909 /* Read-only property getters (is registered, is remote) {{{1 */
911 * g_application_get_is_registered:
912 * @application: a #GApplication
913 * @returns: %TRUE if @application is registered
915 * Checks if @application is registered.
917 * An application is registered if g_application_register() has been
918 * successfully called.
923 g_application_get_is_registered (GApplication *application)
925 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
927 return application->priv->is_registered;
931 * g_application_get_is_remote:
932 * @application: a #GApplication
933 * @returns: %TRUE if @application is remote
935 * Checks if @application is remote.
937 * If @application is remote then it means that another instance of
938 * application already exists (the 'primary' instance). Calls to
939 * perform actions on @application will result in the actions being
940 * performed by the primary instance.
942 * The value of this property can not be accessed before
943 * g_application_register() has been called. See
944 * g_application_get_is_registered().
949 g_application_get_is_remote (GApplication *application)
951 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
952 g_return_val_if_fail (application->priv->is_registered, FALSE);
954 return application->priv->is_remote;
959 * g_application_register:
960 * @application: a #GApplication
961 * @cancellable: a #GCancellable, or %NULL
962 * @error: a pointer to a NULL #GError, or %NULL
963 * @returns: %TRUE if registration succeeded
965 * Attempts registration of the application.
967 * This is the point at which the application discovers if it is the
968 * primary instance or merely acting as a remote for an already-existing
969 * primary instance. This is implemented by attempting to acquire the
970 * application identifier as a unique bus name on the session bus using
973 * Due to the internal architecture of GDBus, method calls can be
974 * dispatched at any time (even if a main loop is not running). For
975 * this reason, you must ensure that any object paths that you wish to
976 * register are registered before calling this function.
978 * If the application has already been registered then %TRUE is
979 * returned with no work performed.
981 * The #GApplication::startup signal is emitted if registration succeeds
982 * and @application is the primary instance.
984 * In the event of an error (such as @cancellable being cancelled, or a
985 * failure to connect to the session bus), %FALSE is returned and @error
986 * is set appropriately.
988 * Note: the return value of this function is not an indicator that this
989 * instance is or is not the primary instance of the application. See
990 * g_application_get_is_remote() for that.
995 g_application_register (GApplication *application,
996 GCancellable *cancellable,
999 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
1001 if (!application->priv->is_registered)
1003 application->priv->impl =
1004 g_application_impl_register (application, application->priv->id,
1005 application->priv->flags,
1006 &application->priv->remote_actions,
1007 cancellable, error);
1009 if (application->priv->impl == NULL)
1012 application->priv->is_remote = application->priv->remote_actions != NULL;
1013 application->priv->is_registered = TRUE;
1015 g_object_notify (G_OBJECT (application), "is-registered");
1017 if (!application->priv->is_remote)
1018 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1024 /* Hold/release {{{1 */
1026 * g_application_hold:
1027 * @application: a #GApplication
1029 * Increases the use count of @application.
1031 * Use this function to indicate that the application has a reason to
1032 * continue to run. For example, g_application_hold() is called by GTK+
1033 * when a toplevel window is on the screen.
1035 * To cancel the hold, call g_application_release().
1038 g_application_hold (GApplication *application)
1040 if (application->priv->inactivity_timeout_id)
1042 g_source_remove (application->priv->inactivity_timeout_id);
1043 application->priv->inactivity_timeout_id = 0;
1046 application->priv->use_count++;
1050 inactivity_timeout_expired (gpointer data)
1052 GApplication *application = G_APPLICATION (data);
1054 G_APPLICATION_GET_CLASS (application)
1055 ->quit_mainloop (application);
1062 * g_application_release:
1063 * @application: a #GApplication
1065 * Decrease the use count of @application.
1067 * When the use count reaches zero, the application will stop running.
1069 * Never call this function except to cancel the effect of a previous
1070 * call to g_application_hold().
1073 g_application_release (GApplication *application)
1075 application->priv->use_count--;
1077 if (application->priv->use_count == 0)
1079 if (application->priv->inactivity_timeout)
1080 application->priv->inactivity_timeout_id =
1081 g_timeout_add (application->priv->inactivity_timeout,
1082 inactivity_timeout_expired, application);
1085 G_APPLICATION_GET_CLASS (application)
1086 ->quit_mainloop (application);
1090 /* Activate, Open {{{1 */
1092 * g_application_activate:
1093 * @application: a #GApplication
1095 * Activates the application.
1097 * In essence, this results in the #GApplication::activate() signal being
1098 * emitted in the primary instance.
1100 * The application must be registered before calling this function.
1105 g_application_activate (GApplication *application)
1107 g_return_if_fail (G_IS_APPLICATION (application));
1108 g_return_if_fail (application->priv->is_registered);
1110 if (application->priv->is_remote)
1111 g_application_impl_activate (application->priv->impl,
1112 get_platform_data (application));
1115 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1119 * g_application_open:
1120 * @application: a #GApplication
1121 * @files: (array length=n_files): an array of #GFiles to open
1122 * @n_files: the length of the @files array
1123 * @hint: a hint (or ""), but never %NULL
1125 * Opens the given files.
1127 * In essence, this results in the #GApplication::open signal being emitted
1128 * in the primary instance.
1130 * @n_files must be greater than zero.
1132 * @hint is simply passed through to the ::open signal. It is
1133 * intended to be used by applications that have multiple modes for
1134 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1135 * for this functionality, you should use "".
1137 * The application must be registered before calling this function
1138 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1143 g_application_open (GApplication *application,
1148 g_return_if_fail (G_IS_APPLICATION (application));
1149 g_return_if_fail (application->priv->flags &
1150 G_APPLICATION_HANDLES_OPEN);
1151 g_return_if_fail (application->priv->is_registered);
1153 if (application->priv->is_remote)
1154 g_application_impl_open (application->priv->impl,
1155 files, n_files, hint,
1156 get_platform_data (application));
1159 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1160 0, files, n_files, hint);
1165 * g_application_run:
1166 * @application: a #GApplication
1167 * @argc: the argc from main() (or 0 if @argv is %NULL)
1168 * @argv: (array length=argc): the argv from main(), or %NULL
1169 * @returns: the exit status
1171 * Runs the application.
1173 * This function is intended to be run from main() and its return value
1174 * is intended to be returned by main(). Although you are expected to pass
1175 * the @argc, @argv parameters from main() to this function, it is possible
1176 * to pass %NULL if @argv is not available or commandline handling is not
1179 * First, the local_command_line() virtual function is invoked.
1180 * This function always runs on the local instance. It gets passed a pointer
1181 * to a %NULL-terminated copy of @argv and is expected to remove the arguments
1182 * that it handled (shifting up remaining arguments). See
1183 * <xref linkend="gapplication-example-cmdline2"/> for an example of
1184 * parsing @argv manually. Alternatively, you may use the #GOptionContext API,
1185 * after setting <literal>argc = g_strv_length (argv);</literal>.
1187 * The last argument to local_command_line() is a pointer to the @status
1188 * variable which can used to set the exit status that is returned from
1189 * g_application_run().
1191 * If local_command_line() returns %TRUE, the command line is expected
1192 * to be completely handled, including possibly registering as the primary
1193 * instance, calling g_application_activate() or g_application_open(), etc.
1195 * If local_command_line() returns %FALSE then the application is registered
1196 * and the #GApplication::command-line signal is emitted in the primary
1197 * instance (which may or may not be this instance).
1199 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1200 * flag set then the default implementation of local_command_line()
1201 * always returns %FALSE immediately, resulting in the commandline
1202 * always being handled in the primary instance.
1204 * Otherwise, the default implementation of local_command_line() tries
1205 * to do a couple of things that are probably reasonable for most
1206 * applications. First, g_application_register() is called to attempt
1207 * to register the application. If that works, then the command line
1208 * arguments are inspected. If no commandline arguments are given, then
1209 * g_application_activate() is called. If commandline arguments are
1210 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1211 * are assumed to be filenames and g_application_open() is called.
1213 * If you are interested in doing more complicated local handling of the
1214 * commandline then you should implement your own #GApplication subclass
1215 * and override local_command_line(). In this case, you most likely want
1216 * to return %TRUE from your local_command_line() implementation to
1217 * suppress the default handling. See
1218 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1220 * If, after the above is done, the use count of the application is zero
1221 * then the exit status is returned immediately. If the use count is
1222 * non-zero then the mainloop is run until the use count falls to zero,
1223 * at which point 0 is returned.
1225 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1226 * use count of zero is delayed for a while (ie: the instance stays
1227 * around to provide its <emphasis>service</emphasis> to others).
1232 g_application_run (GApplication *application,
1240 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1241 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1243 arguments = g_new (gchar *, argc + 1);
1244 for (i = 0; i < argc; i++)
1245 arguments[i] = g_strdup (argv[i]);
1246 arguments[i] = NULL;
1248 if (g_get_prgname () == NULL && argc > 0)
1252 prgname = g_path_get_basename (argv[0]);
1253 g_set_prgname (prgname);
1257 if (!G_APPLICATION_GET_CLASS (application)
1258 ->local_command_line (application, &arguments, &status))
1260 GError *error = NULL;
1262 if (!g_application_register (application, NULL, &error))
1264 g_printerr ("%s", error->message);
1265 g_error_free (error);
1269 if (application->priv->is_remote)
1271 GVariant *platform_data;
1273 platform_data = get_platform_data (application);
1274 status = g_application_impl_command_line (application->priv->impl,
1275 arguments, platform_data);
1279 GApplicationCommandLine *cmdline;
1282 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1283 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1284 "arguments", v, NULL);
1285 g_signal_emit (application,
1286 g_application_signals[SIGNAL_COMMAND_LINE],
1287 0, cmdline, &status);
1288 g_object_unref (cmdline);
1292 g_strfreev (arguments);
1294 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1295 application->priv->is_registered &&
1296 !application->priv->use_count &&
1297 !application->priv->inactivity_timeout_id)
1299 application->priv->inactivity_timeout_id =
1300 g_timeout_add (10000, inactivity_timeout_expired, application);
1303 if (application->priv->use_count ||
1304 application->priv->inactivity_timeout_id)
1306 G_APPLICATION_GET_CLASS (application)
1307 ->run_mainloop (application);
1311 if (application->priv->impl)
1312 g_application_impl_flush (application->priv->impl);
1318 g_application_has_action (GActionGroup *action_group,
1319 const gchar *action_name)
1321 GApplication *application = G_APPLICATION (action_group);
1323 g_return_val_if_fail (application->priv->is_registered, FALSE);
1325 if (application->priv->remote_actions != NULL)
1326 return g_hash_table_lookup (application->priv->remote_actions,
1327 action_name) != NULL;
1329 return application->priv->actions &&
1330 g_action_group_has_action (application->priv->actions, action_name);
1334 g_application_list_actions (GActionGroup *action_group)
1336 GApplication *application = G_APPLICATION (action_group);
1338 g_return_val_if_fail (application->priv->is_registered, NULL);
1340 if (application->priv->remote_actions != NULL)
1342 GHashTableIter iter;
1347 n = g_hash_table_size (application->priv->remote_actions);
1348 keys = g_new (gchar *, n + 1);
1350 g_hash_table_iter_init (&iter, application->priv->remote_actions);
1351 while (g_hash_table_iter_next (&iter, &key, NULL))
1352 keys[i++] = g_strdup (key);
1353 g_assert_cmpint (i, ==, n);
1359 else if (application->priv->actions != NULL)
1360 return g_action_group_list_actions (application->priv->actions);
1363 /* empty string array */
1364 return g_new0 (gchar *, 1);
1368 g_application_get_action_enabled (GActionGroup *action_group,
1369 const gchar *action_name)
1371 GApplication *application = G_APPLICATION (action_group);
1373 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1374 g_return_val_if_fail (application->priv->is_registered, FALSE);
1376 if (application->priv->remote_actions)
1378 RemoteActionInfo *info;
1380 info = g_hash_table_lookup (application->priv->remote_actions,
1383 return info && info->enabled;
1386 return g_action_group_get_action_enabled (application->priv->actions,
1390 static const GVariantType *
1391 g_application_get_action_parameter_type (GActionGroup *action_group,
1392 const gchar *action_name)
1394 GApplication *application = G_APPLICATION (action_group);
1396 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1397 g_return_val_if_fail (application->priv->is_registered, NULL);
1399 if (application->priv->remote_actions)
1401 RemoteActionInfo *info;
1403 info = g_hash_table_lookup (application->priv->remote_actions,
1407 return info->parameter_type;
1412 return g_action_group_get_action_parameter_type (application->priv->actions,
1416 static const GVariantType *
1417 g_application_get_action_state_type (GActionGroup *action_group,
1418 const gchar *action_name)
1420 GApplication *application = G_APPLICATION (action_group);
1422 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1423 g_return_val_if_fail (application->priv->is_registered, NULL);
1425 if (application->priv->remote_actions)
1427 RemoteActionInfo *info;
1429 info = g_hash_table_lookup (application->priv->remote_actions,
1432 if (info && info->state)
1433 return g_variant_get_type (info->state);
1438 return g_action_group_get_action_state_type (application->priv->actions,
1443 g_application_get_action_state (GActionGroup *action_group,
1444 const gchar *action_name)
1446 GApplication *application = G_APPLICATION (action_group);
1448 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1449 g_return_val_if_fail (application->priv->is_registered, NULL);
1451 if (application->priv->remote_actions)
1453 RemoteActionInfo *info;
1455 info = g_hash_table_lookup (application->priv->remote_actions,
1458 if (info && info->state)
1459 return g_variant_ref (info->state);
1464 return g_action_group_get_action_state (application->priv->actions,
1469 g_application_change_action_state (GActionGroup *action_group,
1470 const gchar *action_name,
1473 GApplication *application = G_APPLICATION (action_group);
1475 g_return_if_fail (application->priv->is_registered);
1477 if (application->priv->is_remote)
1478 g_application_impl_change_action_state (application->priv->impl,
1480 get_platform_data (application));
1483 g_action_group_change_action_state (application->priv->actions,
1484 action_name, value);
1488 g_application_activate_action (GActionGroup *action_group,
1489 const gchar *action_name,
1490 GVariant *parameter)
1492 GApplication *application = G_APPLICATION (action_group);
1494 g_return_if_fail (application->priv->is_registered);
1496 if (application->priv->is_remote)
1497 g_application_impl_activate_action (application->priv->impl,
1498 action_name, parameter,
1499 get_platform_data (application));
1502 g_action_group_activate_action (application->priv->actions,
1503 action_name, parameter);
1507 g_application_action_group_iface_init (GActionGroupInterface *iface)
1509 iface->has_action = g_application_has_action;
1510 iface->list_actions = g_application_list_actions;
1512 iface->get_action_enabled = g_application_get_action_enabled;
1513 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1514 iface->get_action_state_type = g_application_get_action_state_type;
1515 iface->get_action_state = g_application_get_action_state;
1516 iface->change_action_state = g_application_change_action_state;
1517 iface->activate_action = g_application_activate_action;
1521 /* vim:set foldmethod=marker: */