1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2010 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Colin Walters <walters@verbum.org>
21 * Emmanuele Bassi <ebassi@linux.intel.com>
29 #include <gobject/gvaluecollector.h>
31 #include "gapplication.h"
32 #include "gio-marshal.h"
36 #include "ginitable.h"
38 #include "gdbusconnection.h"
39 #include "gdbusintrospection.h"
40 #include "gdbusmethodinvocation.h"
44 * SECTION: gapplication
45 * @title: GApplication
46 * @short_description: Core application class
48 * A #GApplication is the foundation of an application, unique for a
49 * given application identifier. The #GApplication wraps some
50 * low-level platform-specific services and is intended to act as the
51 * foundation for higher-level application classes such as
52 * #GtkApplication or #MxApplication. In general, you should not use
53 * this class outside of a higher level framework. By default,
54 * g_application_register_with_data() will invoke g_error() if it is
55 * run in a context where it cannot support its core features. Note
56 * that g_error() is by default fatal.
58 * One of the core features that #GApplication provides is process
59 * uniqueness, in the context of a "session". The session concept is
60 * platform-dependent, but corresponds roughly to a graphical desktop
61 * login. When your application is launched again, its arguments
62 * are passed through platform communication to the already running
65 * In addition, #GApplication provides support for 'actions', which
66 * can be presented to the user in a platform-specific way
67 * (e.g. Windows 7 jump lists). Note that these are just simple
68 * actions without parameters. For more flexible scriptability,
69 * implementing a a separate D-Bus interface is recommended, see e.g.
70 * <xref linkend="gdbus-convenience"/>.
72 * Finally, #GApplication acts as a basic lifecycle root; see the
73 * g_application_run() and g_application_quit_with_data() methods.
75 * Before using #GApplication, you must choose an "application identifier".
76 * The expected form of an application identifier is very close to that of
77 * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
78 * Examples include: "com.example.MyApp" "org.example.internal-apps.Calculator"
79 * For convenience, the restrictions on application identifiers are reproduced
82 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
83 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
84 * <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
85 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
88 * <refsect2><title>D-Bus implementation</title>
90 * On UNIX systems using D-Bus, #GApplication is implemented by claiming the
91 * application identifier as a bus name on the session bus. The implementation
92 * exports an object at the object path that is created by replacing '.' with
93 * '/' in the application identifier (e.g. the object path for the
94 * application id 'org.gtk.TestApp' is '/org/gtk/TestApp'). The object
95 * implements the org.gtk.Application interface.
97 * <classsynopsis class="interface">
98 * <ooclass><interfacename>org.gtk.Application</interfacename></ooclass>
101 * <methodname>Activate</methodname>
102 * <methodparam><modifier>in</modifier><type>aay</type><parameter>arguments</parameter></methodparam>
103 * <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
107 * <methodname>InvokeAction</methodname>
108 * <methodparam><modifier>in</modifier><type>s</type><parameter>action</parameter></methodparam>
109 * <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
112 * <type>a{s(sb)}</type>
113 * <methodname>ListActions</methodname>
118 * <methodname>Quit</methodname>
119 * <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
122 * <modifier>Signal</modifier>
124 * <methodname>ActionsChanged</methodname>
129 * The <methodname>Activate</methodname> function is called on the existing
130 * application instance when a second instance fails to take the bus name.
131 * @arguments contains the commandline arguments given to the second instance
132 * and @data contains platform-specific additional data.
134 * On all platforms, @data will have a key "cwd" of type signature
135 * "ay" which contains the working directory of the invoked
136 * executable; this data is defined to be in the default GLib
137 * filesystem encoding for the platform. See g_filename_to_utf8().
141 * The <methodname>InvokeAction</methodname> function can be called to
142 * invoke one of the actions exported by the application. On X11
143 * platforms, the platform_data argument should have a "timestamp"
144 * parameter of type "u" with the server time of the initiating event.
147 * The <methodname>ListActions</methodname> function returns a dictionary
148 * with the exported actions of the application. The keys of the dictionary
149 * are the action names, and the values are structs containing the description
150 * for the action and a boolean that represents if the action is enabled or not.
153 * The <methodname>Quit</methodname> function can be called to
154 * terminate the application. The @data parameter contains
155 * platform-specific data. On X11 platforms, the platform_data
156 * argument should have a "timestamp" parameter of type "u" with the
157 * server time of the initiating event.
160 * The <methodname>ActionsChanged</methodname> signal is emitted when the
161 * exported actions change (i.e. an action is added, removed, enabled,
162 * disabled, or otherwise changed).
165 * #GApplication is supported since Gio 2.26.
170 static void initable_iface_init (GInitableIface *initable_iface);
172 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
173 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
197 static guint application_signals[LAST_SIGNAL] = { 0 };
203 } GApplicationAction;
205 struct _GApplicationPrivate
208 GHashTable *actions; /* name -> GApplicationAction */
212 GVariant *platform_data;
214 guint do_register : 1;
215 guint default_quit : 1;
217 guint registration_tried : 1;
219 guint actions_changed_id;
223 GDBusConnection *session_bus;
227 static GApplication *primary_application = NULL;
228 static GHashTable *instances_for_appid = NULL;
230 static gboolean initable_init (GInitable *initable,
231 GCancellable *cancellable,
234 static gboolean _g_application_platform_init (GApplication *app,
235 GCancellable *cancellable,
237 static gboolean _g_application_platform_register (GApplication *app,
239 GCancellable *cancellable,
242 static void _g_application_platform_remote_invoke_action (GApplication *app,
244 GVariant *platform_data);
245 static void _g_application_platform_remote_quit (GApplication *app,
246 GVariant *platform_data);
247 static void _g_application_platform_on_actions_changed (GApplication *app);
250 initable_iface_init (GInitableIface *initable_iface)
252 initable_iface->init = initable_init;
256 #include "gdbusapplication.c"
258 #include "gnullapplication.c"
262 _g_application_validate_id (const char *id)
266 if (strlen (id) > 255)
269 if (!g_ascii_isalpha (*id))
276 if (g_ascii_isalnum (*id) || (*id == '-') || (*id == '_'))
278 else if (allow_dot && *id == '.')
287 init_appid_statics (gpointer data)
289 instances_for_appid = g_hash_table_new (g_str_hash, g_str_equal);
293 static GApplication *
294 application_for_appid (const char *appid)
296 static GOnce appid_once = G_ONCE_INIT;
298 g_once (&appid_once, init_appid_statics, NULL);
300 return g_hash_table_lookup (instances_for_appid, appid);
304 g_application_default_quit_with_data (GApplication *application,
305 GVariant *platform_data)
307 g_return_val_if_fail (application->priv->mainloop != NULL, FALSE);
308 g_main_loop_quit (application->priv->mainloop);
314 g_application_default_run (GApplication *application)
316 if (application->priv->mainloop == NULL)
317 application->priv->mainloop = g_main_loop_new (NULL, TRUE);
319 g_main_loop_run (application->priv->mainloop);
323 append_cwd_to_platform_data (GVariant *platform_data)
325 GVariantBuilder builder;
329 cwd = g_get_current_dir ();
331 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
333 g_variant_builder_add (&builder, "{sv}",
335 g_variant_new_bytestring (cwd));
343 g_variant_iter_init (&iter, platform_data);
344 while (g_variant_iter_next (&iter, "@{sv}", &item))
346 g_variant_builder_add_value (&builder, item);
347 g_variant_unref (item);
350 result = g_variant_builder_end (&builder);
355 timeout_handle_actions_changed (gpointer user_data)
357 GApplication *application = user_data;
359 application->priv->actions_changed_id = 0;
361 _g_application_platform_on_actions_changed (application);
367 queue_actions_change_notification (GApplication *application)
369 GApplicationPrivate *priv = application->priv;
371 if (priv->actions_changed_id == 0)
372 priv->actions_changed_id = g_timeout_add (0, timeout_handle_actions_changed, application);
376 initable_init (GInitable *initable,
377 GCancellable *cancellable,
380 GApplication *app = G_APPLICATION (initable);
383 if (!_g_application_platform_init (app, cancellable, error))
386 if (app->priv->do_register
387 && !_g_application_platform_register (app, &unique, cancellable ,error))
394 g_application_action_free (gpointer data)
396 if (G_LIKELY (data != NULL))
398 GApplicationAction *action = data;
400 g_free (action->name);
401 g_free (action->description);
403 g_slice_free (GApplicationAction, action);
409 * @appid: System-dependent application identifier
410 * @argc: Number of arguments in @argv
411 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
413 * Create a new #GApplication. This uses a platform-specific
414 * mechanism to ensure the current process is the unique owner of the
415 * application (as defined by the @appid). If successful, the
416 * #GApplication:is-remote property will be %FALSE, and it is safe to
417 * continue creating other resources such as graphics windows.
419 * If the given @appid is already running in another process, the the
420 * GApplication::activate_with_data signal will be emitted in the
421 * remote process, with the data from @argv and other
422 * platform-specific data available. Subsequently the
423 * #GApplication:default-quit property will be evaluated. If it's
424 * %TRUE, then the current process will terminate. If %FALSE, then
425 * the application remains in the #GApplication:is-remote state, and
426 * you can e.g. call g_application_invoke_action(). Note that proxy
427 * instances should not call g_application_add_action().
429 * This function may do synchronous I/O to obtain unique ownership
430 * of the application id, and will block the calling thread in this
433 * If the environment does not support the basic functionality of
434 * #GApplication, this function will invoke g_error(), which by
435 * default is a fatal operation. This may arise for example on
436 * UNIX systems using D-Bus when the session bus is not available.
438 * As a convenience, this function is defined to call g_type_init() as
439 * its very first action.
441 * Returns: (transfer full): An application instance
446 g_application_new (const gchar *appid,
450 const gchar * const *args = (const gchar **) argv;
452 GError *error = NULL;
453 GVariant *argv_variant;
457 g_return_val_if_fail (appid != NULL, NULL);
459 argv_variant = g_variant_new_bytestring_array (args, argc);
461 app = g_initable_new (G_TYPE_APPLICATION,
464 "application-id", appid,
465 "argv", argv_variant,
469 g_error ("%s", error->message);
470 g_clear_error (&error);
473 return G_APPLICATION (app);
477 * g_application_try_new:
478 * @appid: System-dependent application identifier
479 * @argc: Number of arguments in @argv
480 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
483 * This function is similar to g_application_new(), but allows for
484 * more graceful fallback if the environment doesn't support the
485 * basic #GApplication functionality.
487 * Returns: (transfer full): An application instance
492 g_application_try_new (const gchar *appid,
497 const gchar * const *args = (const gchar **) argv;
498 GVariant *argv_variant;
502 g_return_val_if_fail (appid != NULL, NULL);
504 argv_variant = g_variant_new_bytestring_array (args, argc);
506 return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION,
509 "application-id", appid,
510 "argv", argv_variant,
515 * g_application_unregistered_try_new:
516 * @appid: System-dependent application identifier
517 * @argc: Number of arguments in @argv
518 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
521 * This function is similar to g_application_try_new(), but also
522 * sets the GApplication:register property to %FALSE. You can later
523 * call g_application_register() to complete initialization.
525 * Returns: (transfer full): An application instance
530 g_application_unregistered_try_new (const gchar *appid,
535 const gchar * const *args = (const gchar **) argv;
536 GVariant *argv_variant;
540 g_return_val_if_fail (appid != NULL, NULL);
542 argv_variant = g_variant_new_bytestring_array (args, argc);
544 return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION,
547 "application-id", appid,
548 "argv", argv_variant,
554 * g_application_register:
555 * @application: a #GApplication
557 * By default, #GApplication ensures process uniqueness when
558 * initialized, but this behavior is controlled by the
559 * GApplication:register property. If it was given as %FALSE at
560 * construction time, this function allows you to later attempt
561 * to ensure uniqueness. Note that the GApplication:default-quit
562 * property no longer applies at this point; if this function returns
563 * %FALSE, platform activation will occur, but the current process
564 * will not be terminated.
566 * It is an error to call this function more than once. It is
567 * also an error to call this function if the GApplication:register
568 * property was %TRUE at construction time.
570 * Returns: %TRUE if registration was successful
573 g_application_register (GApplication *application)
577 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
578 g_return_val_if_fail (application->priv->is_remote, FALSE);
579 g_return_val_if_fail (!application->priv->registration_tried, FALSE);
581 if (!_g_application_platform_register (application, &unique, NULL, NULL))
587 * g_application_add_action:
588 * @application: a #GApplication
589 * @name: the action name
590 * @description: the action description; can be a translatable
593 * Adds an action @name to the list of exported actions of @application.
595 * It is an error to call this function if @application is a proxy for
596 * a remote application.
598 * You can invoke an action using g_application_invoke_action().
600 * The newly added action is enabled by default; you can call
601 * g_application_set_action_enabled() to disable it.
606 g_application_add_action (GApplication *application,
608 const gchar *description)
610 GApplicationPrivate *priv;
611 GApplicationAction *action;
613 g_return_if_fail (G_IS_APPLICATION (application));
614 g_return_if_fail (name != NULL && *name != '\0');
615 g_return_if_fail (!application->priv->is_remote);
617 priv = application->priv;
619 g_return_if_fail (g_hash_table_lookup (priv->actions, name) == NULL);
621 action = g_slice_new (GApplicationAction);
622 action->name = g_strdup (name);
623 action->description = g_strdup (description);
624 action->enabled = TRUE;
626 g_hash_table_insert (priv->actions, action->name, action);
627 queue_actions_change_notification (application);
631 * g_application_remove_action:
632 * @application: a #GApplication
633 * @name: the name of the action to remove
635 * Removes the action @name from the list of exported actions of @application.
637 * It is an error to call this function if @application is a proxy for
638 * a remote application.
643 g_application_remove_action (GApplication *application,
646 GApplicationPrivate *priv;
648 g_return_if_fail (G_IS_APPLICATION (application));
649 g_return_if_fail (name != NULL && *name != '\0');
650 g_return_if_fail (!application->priv->is_remote);
652 priv = application->priv;
654 g_return_if_fail (g_hash_table_lookup (priv->actions, name) != NULL);
656 g_hash_table_remove (priv->actions, name);
657 queue_actions_change_notification (application);
661 * g_application_invoke_action:
662 * @application: a #GApplication
663 * @name: the name of the action to invoke
664 * @platform_data: (allow-none): platform-specific event data
666 * Invokes the action @name of the passed #GApplication.
668 * This function has different behavior depending on whether @application
669 * is acting as a proxy for another process. In the normal case where
670 * the current process is hosting the application, and the specified
671 * action exists and is enabled, the #GApplication::action signal will
674 * If @application is a proxy, then the specified action will be invoked
675 * in the remote process. It is not necessary to call
676 * g_application_add_action() in the current process in order to invoke
682 g_application_invoke_action (GApplication *application,
684 GVariant *platform_data)
686 GApplicationPrivate *priv;
687 GApplicationAction *action;
689 g_return_if_fail (G_IS_APPLICATION (application));
690 g_return_if_fail (name != NULL);
691 g_return_if_fail (platform_data == NULL
692 || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")));
694 if (platform_data == NULL)
695 platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
697 g_variant_ref (platform_data);
699 priv = application->priv;
703 _g_application_platform_remote_invoke_action (application, name, platform_data);
707 action = g_hash_table_lookup (priv->actions, name);
708 g_return_if_fail (action != NULL);
709 if (!action->enabled)
712 g_signal_emit (application, application_signals[ACTION_WITH_DATA],
713 g_quark_from_string (name),
718 g_variant_unref (platform_data);
722 * g_application_list_actions:
723 * @application: a #GApplication
725 * Retrieves the list of action names currently exported by @application.
727 * It is an error to call this function if @application is a proxy for
728 * a remote application.
730 * Return value: (transfer full): a newly allocation, %NULL-terminated array
731 * of strings containing action names; use g_strfreev() to free the
732 * resources used by the returned array
737 g_application_list_actions (GApplication *application)
739 GApplicationPrivate *priv;
745 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
746 g_return_val_if_fail (!application->priv->is_remote, NULL);
748 priv = application->priv;
750 retval = g_new (gchar*, g_hash_table_size (priv->actions) + 1);
753 g_hash_table_iter_init (&iter, priv->actions);
754 while (g_hash_table_iter_next (&iter, &key, NULL))
755 retval[i++] = g_strdup (key);
763 * g_application_set_action_enabled:
764 * @application: a #GApplication
765 * @name: the name of the application
766 * @enabled: whether to enable or disable the action @name
768 * Sets whether the action @name inside @application should be enabled
771 * It is an error to call this function if @application is a proxy for
772 * a remote application.
774 * Invoking a disabled action will not result in the #GApplication::action
775 * signal being emitted.
780 g_application_set_action_enabled (GApplication *application,
784 GApplicationAction *action;
786 g_return_if_fail (G_IS_APPLICATION (application));
787 g_return_if_fail (name != NULL);
788 g_return_if_fail (!application->priv->is_remote);
792 action = g_hash_table_lookup (application->priv->actions, name);
793 g_return_if_fail (action != NULL);
794 if (action->enabled == enabled)
797 action->enabled = enabled;
799 queue_actions_change_notification (application);
804 * g_application_get_action_description:
805 * @application: a #GApplication
808 * Gets the description of the action @name.
810 * It is an error to call this function if @application is a proxy for
811 * a remote application.
813 * Returns: Description for the given action named @name
817 G_CONST_RETURN gchar *
818 g_application_get_action_description (GApplication *application,
821 GApplicationAction *action;
823 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
824 g_return_val_if_fail (name != NULL, NULL);
825 g_return_val_if_fail (!application->priv->is_remote, NULL);
827 action = g_hash_table_lookup (application->priv->actions, name);
828 g_return_val_if_fail (action != NULL, NULL);
830 return action->description;
835 * g_application_get_action_enabled:
836 * @application: a #GApplication
837 * @name: the name of the action
839 * Retrieves whether the action @name is enabled or not.
841 * See g_application_set_action_enabled().
843 * It is an error to call this function if @application is a proxy for
844 * a remote application.
846 * Return value: %TRUE if the action was enabled, and %FALSE otherwise
851 g_application_get_action_enabled (GApplication *application,
854 GApplicationAction *action;
856 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
857 g_return_val_if_fail (name != NULL, FALSE);
858 g_return_val_if_fail (!application->priv->is_remote, FALSE);
860 action = g_hash_table_lookup (application->priv->actions, name);
861 g_return_val_if_fail (action != NULL, FALSE);
863 return action->enabled;
868 * @application: a #GApplication
870 * Starts the application.
872 * The default implementation of this virtual function will simply run
875 * It is an error to call this function if @application is a proxy for
876 * a remote application.
881 g_application_run (GApplication *application)
883 g_return_if_fail (G_IS_APPLICATION (application));
884 g_return_if_fail (!application->priv->is_remote);
886 G_APPLICATION_GET_CLASS (application)->run (application);
890 * g_application_quit_with_data:
891 * @application: a #GApplication
892 * @platform_data: (allow-none): platform-specific data
894 * Request that the application quits.
896 * This function has different behavior depending on whether @application
897 * is acting as a proxy for another process. In the normal case where
898 * the current process is hosting the application, the default
899 * implementation will quit the main loop created by g_application_run().
901 * If @application is a proxy, then the remote process will be asked
904 * Returns: %TRUE if the application accepted the request, %FALSE otherwise
909 g_application_quit_with_data (GApplication *application,
910 GVariant *platform_data)
912 gboolean retval = FALSE;
914 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
915 g_return_val_if_fail (platform_data == NULL
916 || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")), FALSE);
918 if (platform_data == NULL)
919 platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
921 g_variant_ref (platform_data);
923 if (application->priv->is_remote)
925 _g_application_platform_remote_quit (application, platform_data);
929 g_signal_emit (application, application_signals[QUIT_WITH_DATA], 0, platform_data, &retval);
931 g_variant_unref (platform_data);
937 * g_application_get_instance:
939 * In the normal case where there is exactly one #GApplication instance
940 * in this process, return that instance. If there are multiple, the
941 * first one created will be returned. Otherwise, return %NULL.
943 * Returns: (transfer none): The primary instance of #GApplication,
944 * or %NULL if none is set
949 g_application_get_instance (void)
951 return primary_application;
955 * g_application_get_id:
956 * @application: a #GApplication
958 * Retrieves the platform-specific identifier for the #GApplication.
960 * Return value: The platform-specific identifier. The returned string
961 * is owned by the #GApplication instance and it should never be
966 G_CONST_RETURN gchar *
967 g_application_get_id (GApplication *application)
969 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
971 return application->priv->appid;
975 * g_application_is_remote:
976 * @application: a #GApplication
978 * Returns whether the object represents a proxy for a remote application.
980 * Returns: %TRUE if this object represents a proxy for a remote application.
983 g_application_is_remote (GApplication *application)
985 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
987 return application->priv->is_remote;
991 g_application_init (GApplication *app)
993 app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
995 GApplicationPrivate);
997 app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
999 g_application_action_free);
1000 app->priv->default_quit = TRUE;
1001 app->priv->do_register = TRUE;
1002 app->priv->is_remote = TRUE;
1003 app->priv->platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
1007 g_application_get_property (GObject *object,
1012 GApplication *app = G_APPLICATION (object);
1016 case PROP_APPLICATION_ID:
1017 g_value_set_string (value, g_application_get_id (app));
1020 case PROP_DEFAULT_QUIT:
1021 g_value_set_boolean (value, app->priv->default_quit);
1024 case PROP_IS_REMOTE:
1025 g_value_set_boolean (value, g_application_is_remote (app));
1029 g_value_set_boolean (value, app->priv->do_register);
1033 g_value_set_variant (value, app->priv->argv);
1036 case PROP_PLATFORM_DATA:
1037 g_value_set_variant (value, app->priv->platform_data);
1041 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1046 g_application_set_property (GObject *object,
1048 const GValue *value,
1051 GApplication *app = G_APPLICATION (object);
1055 case PROP_APPLICATION_ID:
1056 g_return_if_fail (_g_application_validate_id (g_value_get_string (value)));
1057 app->priv->appid = g_value_dup_string (value);
1060 case PROP_DEFAULT_QUIT:
1061 app->priv->default_quit = g_value_get_boolean (value);
1065 app->priv->do_register = g_value_get_boolean (value);
1066 /* If we're not registering, the default_quit no longer applies */
1067 if (!app->priv->do_register)
1068 app->priv->default_quit = FALSE;
1072 app->priv->argv = g_value_dup_variant (value);
1075 case PROP_PLATFORM_DATA:
1077 GVariant *platform_data = g_value_get_variant (value);
1078 if (app->priv->platform_data)
1079 g_variant_unref (app->priv->platform_data);
1080 app->priv->platform_data = g_variant_ref_sink (append_cwd_to_platform_data (platform_data));
1085 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1090 g_application_constructor (GType type,
1091 guint n_construct_properties,
1092 GObjectConstructParam *construct_params)
1096 const char *appid = NULL;
1099 for (i = 0; i < n_construct_properties; i++)
1101 GObjectConstructParam *param = &construct_params[i];
1102 if (strcmp (param->pspec->name, "application-id") == 0)
1103 appid = g_value_get_string (param->value);
1106 g_return_val_if_fail (appid != NULL, NULL);
1108 app = application_for_appid (appid);
1110 return g_object_ref (app);
1112 object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
1113 n_construct_properties,
1115 app = G_APPLICATION (object);
1117 if (primary_application == NULL)
1118 primary_application = app;
1119 g_hash_table_insert (instances_for_appid, g_strdup (appid), app);
1125 g_application_finalize (GObject *object)
1127 GApplication *app = G_APPLICATION (object);
1129 g_free (app->priv->appid);
1130 if (app->priv->actions)
1131 g_hash_table_unref (app->priv->actions);
1132 if (app->priv->actions_changed_id)
1133 g_source_remove (app->priv->actions_changed_id);
1134 if (app->priv->mainloop)
1135 g_main_loop_unref (app->priv->mainloop);
1138 g_free (app->priv->dbus_path);
1139 if (app->priv->session_bus)
1140 g_object_unref (app->priv->session_bus);
1143 G_OBJECT_CLASS (g_application_parent_class)->finalize (object);
1147 g_application_class_init (GApplicationClass *klass)
1149 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
1151 g_type_class_add_private (klass, sizeof (GApplicationPrivate));
1153 gobject_class->constructor = g_application_constructor;
1154 gobject_class->set_property = g_application_set_property;
1155 gobject_class->get_property = g_application_get_property;
1157 gobject_class->finalize = g_application_finalize;
1159 klass->run = g_application_default_run;
1160 klass->quit_with_data = g_application_default_quit_with_data;
1163 * GApplication::quit-with-data:
1164 * @application: the object on which the signal is emitted
1165 * @platform_data: Platform-specific data, or %NULL
1167 * This signal is emitted when the Quit action is invoked on the
1170 * The default handler for this signal exits the mainloop of the
1173 * Returns: %TRUE if the signal has been handled, %FALSE to continue
1176 application_signals[QUIT_WITH_DATA] =
1177 g_signal_new (g_intern_static_string ("quit-with-data"),
1178 G_OBJECT_CLASS_TYPE (klass),
1180 G_STRUCT_OFFSET (GApplicationClass, quit_with_data),
1181 g_signal_accumulator_true_handled, NULL,
1182 _gio_marshal_BOOLEAN__VARIANT,
1187 * GApplication::action-with-data:
1188 * @application: the object on which the signal is emitted
1189 * @name: The name of the activated action
1190 * @platform_data: Platform-specific data, or %NULL
1192 * This signal is emitted when an action is activated. The action name
1193 * is passed as the first argument, but also as signal detail, so it
1194 * is possible to connect to this signal for individual actions.
1196 * The signal is never emitted for disabled actions.
1198 application_signals[ACTION_WITH_DATA] =
1199 g_signal_new (g_intern_static_string ("action-with-data"),
1200 G_OBJECT_CLASS_TYPE (klass),
1201 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
1202 G_STRUCT_OFFSET (GApplicationClass, action_with_data),
1204 _gio_marshal_VOID__STRING_VARIANT,
1210 * GApplication::prepare-activation:
1211 * @application: the object on which the signal is emitted
1212 * @arguments: A #GVariant with the signature "aay"
1213 * @platform_data: A #GVariant with the signature "a{sv}", or %NULL
1215 * This signal is emitted when a non-primary process for a given
1216 * application is invoked while your application is running; for
1217 * example, when a file browser launches your program to open a
1218 * file. The raw operating system arguments are passed in the
1219 * @arguments variant. Additional platform-dependent data is
1220 * stored in @platform_data.
1222 application_signals[PREPARE_ACTIVATION] =
1223 g_signal_new (g_intern_static_string ("prepare-activation"),
1224 G_OBJECT_CLASS_TYPE (klass),
1226 G_STRUCT_OFFSET (GApplicationClass, prepare_activation),
1228 _gio_marshal_VOID__VARIANT_VARIANT,
1234 * GApplication:application-id:
1236 * The unique identifier for this application. See the documentation for
1237 * #GApplication for more information about this property.
1240 g_object_class_install_property (gobject_class,
1241 PROP_APPLICATION_ID,
1242 g_param_spec_string ("application-id",
1243 P_("Application ID"),
1244 P_("Identifier for this application"),
1247 G_PARAM_CONSTRUCT_ONLY |
1248 G_PARAM_STATIC_STRINGS));
1251 * GApplication:argv:
1253 * The argument vector given to this application. It must be a #GVariant
1254 * with a type signature "aay".
1257 g_object_class_install_property (gobject_class,
1259 g_param_spec_variant ("argv",
1260 P_("Argument vector"),
1261 P_("System argument vector with type signature aay"),
1262 G_VARIANT_TYPE ("aay"),
1265 G_PARAM_CONSTRUCT_ONLY |
1266 G_PARAM_STATIC_STRINGS));
1269 * GApplication:platform-data:
1271 * Platform-specific data retrieved from the operating system
1272 * environment. It must be a #GVariant with type signature "a{sv}".
1275 g_object_class_install_property (gobject_class,
1277 g_param_spec_variant ("platform-data",
1278 P_("Platform data"),
1279 P_("Environmental data, must have type signature a{sv}"),
1280 G_VARIANT_TYPE ("a{sv}"),
1283 G_PARAM_CONSTRUCT_ONLY |
1284 G_PARAM_STATIC_STRINGS));
1287 * GApplication:default-quit:
1289 * By default, if the GApplication:register property is %TRUE, and a
1290 * different process is running this application, the process will
1291 * be exited. Set this property to %FALSE to allow custom
1292 * interaction with the remote process.
1295 g_object_class_install_property (gobject_class,
1297 g_param_spec_boolean ("default-quit",
1299 P_("Exit the process by default"),
1302 G_PARAM_CONSTRUCT_ONLY |
1303 G_PARAM_STATIC_STRINGS));
1307 * GApplication:is-remote:
1309 * This property is %TRUE if this application instance represents a proxy
1310 * to the instance of this application in another process.
1313 g_object_class_install_property (gobject_class,
1315 g_param_spec_boolean ("is-remote",
1317 P_("Whether this application is a proxy for another process"),
1319 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1322 * GApplication:register:
1324 * If this property is %FALSE, the application construction will not attempt
1325 * to ensure process uniqueness, and the application is guaranteed to be in the
1326 * remote state. See GApplication:is-remote.
1328 g_object_class_install_property (gobject_class,
1330 g_param_spec_boolean ("register",
1332 P_("If false, do not "),
1335 G_PARAM_CONSTRUCT_ONLY |
1336 G_PARAM_STATIC_STRINGS));