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;
218 guint actions_changed_id;
222 GDBusConnection *session_bus;
226 static GApplication *primary_application = NULL;
227 static GHashTable *instances_for_appid = NULL;
229 static gboolean initable_init (GInitable *initable,
230 GCancellable *cancellable,
233 static gboolean _g_application_platform_init (GApplication *app,
234 GCancellable *cancellable,
236 static gboolean _g_application_platform_register (GApplication *app,
238 GCancellable *cancellable,
241 static void _g_application_platform_remote_invoke_action (GApplication *app,
243 GVariant *platform_data);
244 static void _g_application_platform_remote_quit (GApplication *app,
245 GVariant *platform_data);
246 static void _g_application_platform_on_actions_changed (GApplication *app);
249 initable_iface_init (GInitableIface *initable_iface)
251 initable_iface->init = initable_init;
255 #include "gdbusapplication.c"
257 #include "gnullapplication.c"
261 _g_application_validate_id (const char *id)
265 if (strlen (id) > 255)
268 if (!g_ascii_isalpha (*id))
275 if (g_ascii_isalnum (*id) || (*id == '-') || (*id == '_'))
277 else if (allow_dot && *id == '.')
286 init_appid_statics (gpointer data)
288 instances_for_appid = g_hash_table_new (g_str_hash, g_str_equal);
292 static GApplication *
293 application_for_appid (const char *appid)
295 static GOnce appid_once = G_ONCE_INIT;
297 g_once (&appid_once, init_appid_statics, NULL);
299 return g_hash_table_lookup (instances_for_appid, appid);
303 g_application_default_quit_with_data (GApplication *application,
304 GVariant *platform_data)
306 g_return_val_if_fail (application->priv->mainloop != NULL, FALSE);
307 g_main_loop_quit (application->priv->mainloop);
313 g_application_default_run (GApplication *application)
315 if (application->priv->mainloop == NULL)
316 application->priv->mainloop = g_main_loop_new (NULL, TRUE);
318 g_main_loop_run (application->priv->mainloop);
322 append_cwd_to_platform_data (GVariant *platform_data)
324 GVariantBuilder builder;
328 cwd = g_get_current_dir ();
330 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
332 g_variant_builder_add (&builder, "{sv}",
334 g_variant_new_bytestring (cwd));
342 g_variant_iter_init (&iter, platform_data);
343 while (g_variant_iter_next (&iter, "@{sv}", &item))
345 g_variant_builder_add_value (&builder, item);
346 g_variant_unref (item);
349 result = g_variant_builder_end (&builder);
354 timeout_handle_actions_changed (gpointer user_data)
356 GApplication *application = user_data;
358 application->priv->actions_changed_id = 0;
360 _g_application_platform_on_actions_changed (application);
366 queue_actions_change_notification (GApplication *application)
368 GApplicationPrivate *priv = application->priv;
370 if (priv->actions_changed_id == 0)
371 priv->actions_changed_id = g_timeout_add (0, timeout_handle_actions_changed, application);
375 initable_init (GInitable *initable,
376 GCancellable *cancellable,
379 GApplication *app = G_APPLICATION (initable);
382 if (!_g_application_platform_init (app, cancellable, error))
385 if (app->priv->do_register &&
386 !_g_application_platform_register (app, &unique, cancellable ,error))
393 g_application_action_free (gpointer data)
395 if (G_LIKELY (data != NULL))
397 GApplicationAction *action = data;
399 g_free (action->name);
400 g_free (action->description);
402 g_slice_free (GApplicationAction, action);
408 * @appid: System-dependent application identifier
409 * @argc: Number of arguments in @argv
410 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
412 * Create a new #GApplication. This uses a platform-specific
413 * mechanism to ensure the current process is the unique owner of the
414 * application (as defined by the @appid). If successful, the
415 * #GApplication:is-remote property will be %FALSE, and it is safe to
416 * continue creating other resources such as graphics windows.
418 * If the given @appid is already running in another process, the the
419 * GApplication::activate_with_data signal will be emitted in the
420 * remote process, with the data from @argv and other
421 * platform-specific data available. Subsequently the
422 * #GApplication:default-quit property will be evaluated. If it's
423 * %TRUE, then the current process will terminate. If %FALSE, then
424 * the application remains in the #GApplication:is-remote state, and
425 * you can e.g. call g_application_invoke_action(). Note that proxy
426 * instances should not call g_application_add_action().
428 * This function may do synchronous I/O to obtain unique ownership
429 * of the application id, and will block the calling thread in this
432 * If the environment does not support the basic functionality of
433 * #GApplication, this function will invoke g_error(), which by
434 * default is a fatal operation. This may arise for example on
435 * UNIX systems using D-Bus when the session bus is not available.
437 * As a convenience, this function is defined to call g_type_init() as
438 * its very first action.
440 * Returns: (transfer full): An application instance
445 g_application_new (const gchar *appid,
449 const gchar * const *args = (const gchar **) argv;
451 GError *error = NULL;
452 GVariant *argv_variant;
456 g_return_val_if_fail (appid != NULL, NULL);
458 argv_variant = g_variant_new_bytestring_array (args, argc);
460 app = g_initable_new (G_TYPE_APPLICATION,
463 "application-id", appid,
464 "argv", argv_variant,
468 g_error ("%s", error->message);
469 g_clear_error (&error);
472 return G_APPLICATION (app);
476 * g_application_try_new:
477 * @appid: System-dependent application identifier
478 * @argc: Number of arguments in @argv
479 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
482 * This function is similar to g_application_new(), but allows for
483 * more graceful fallback if the environment doesn't support the
484 * basic #GApplication functionality.
486 * Returns: (transfer full): An application instance
491 g_application_try_new (const gchar *appid,
496 const gchar * const *args = (const gchar **) argv;
497 GVariant *argv_variant;
501 g_return_val_if_fail (appid != NULL, NULL);
503 argv_variant = g_variant_new_bytestring_array (args, argc);
505 return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION,
508 "application-id", appid,
509 "argv", argv_variant,
514 * g_application_unregistered_try_new:
515 * @appid: System-dependent application identifier
516 * @argc: Number of arguments in @argv
517 * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main()
520 * This function is similar to g_application_try_new(), but also
521 * sets the GApplication:register property to %FALSE. You can later
522 * call g_application_register() to complete initialization.
524 * Returns: (transfer full): An application instance
529 g_application_unregistered_try_new (const gchar *appid,
534 const gchar * const *args = (const gchar **) argv;
535 GVariant *argv_variant;
539 g_return_val_if_fail (appid != NULL, NULL);
541 argv_variant = g_variant_new_bytestring_array (args, argc);
543 return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION,
546 "application-id", appid,
547 "argv", argv_variant,
553 * g_application_register:
554 * @application: a #GApplication
556 * By default, #GApplication ensures process uniqueness when
557 * initialized, but this behavior is controlled by the
558 * GApplication:register property. If it was given as %FALSE at
559 * construction time, this function allows you to later attempt
560 * to ensure uniqueness.
562 * Returns: %TRUE if registration was successful
565 g_application_register (GApplication *application)
569 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
570 g_return_val_if_fail (application->priv->is_remote, FALSE);
572 if (!_g_application_platform_register (application, &unique, NULL, NULL))
578 * g_application_add_action:
579 * @application: a #GApplication
580 * @name: the action name
581 * @description: the action description; can be a translatable
584 * Adds an action @name to the list of exported actions of @application.
586 * It is an error to call this function if @application is a proxy for
587 * a remote application.
589 * You can invoke an action using g_application_invoke_action().
591 * The newly added action is enabled by default; you can call
592 * g_application_set_action_enabled() to disable it.
597 g_application_add_action (GApplication *application,
599 const gchar *description)
601 GApplicationPrivate *priv;
602 GApplicationAction *action;
604 g_return_if_fail (G_IS_APPLICATION (application));
605 g_return_if_fail (name != NULL && *name != '\0');
606 g_return_if_fail (!application->priv->is_remote);
608 priv = application->priv;
610 g_return_if_fail (g_hash_table_lookup (priv->actions, name) == NULL);
612 action = g_slice_new (GApplicationAction);
613 action->name = g_strdup (name);
614 action->description = g_strdup (description);
615 action->enabled = TRUE;
617 g_hash_table_insert (priv->actions, action->name, action);
618 queue_actions_change_notification (application);
622 * g_application_remove_action:
623 * @application: a #GApplication
624 * @name: the name of the action to remove
626 * Removes the action @name from the list of exported actions of @application.
628 * It is an error to call this function if @application is a proxy for
629 * a remote application.
634 g_application_remove_action (GApplication *application,
637 GApplicationPrivate *priv;
639 g_return_if_fail (G_IS_APPLICATION (application));
640 g_return_if_fail (name != NULL && *name != '\0');
641 g_return_if_fail (!application->priv->is_remote);
643 priv = application->priv;
645 g_return_if_fail (g_hash_table_lookup (priv->actions, name) != NULL);
647 g_hash_table_remove (priv->actions, name);
648 queue_actions_change_notification (application);
652 * g_application_invoke_action:
653 * @application: a #GApplication
654 * @name: the name of the action to invoke
655 * @platform_data: (allow-none): platform-specific event data
657 * Invokes the action @name of the passed #GApplication.
659 * This function has different behavior depending on whether @application
660 * is acting as a proxy for another process. In the normal case where
661 * the current process is hosting the application, and the specified
662 * action exists and is enabled, the #GApplication::action signal will
665 * If @application is a proxy, then the specified action will be invoked
666 * in the remote process. It is not necessary to call
667 * g_application_add_action() in the current process in order to invoke
673 g_application_invoke_action (GApplication *application,
675 GVariant *platform_data)
677 GApplicationPrivate *priv;
678 GApplicationAction *action;
680 g_return_if_fail (G_IS_APPLICATION (application));
681 g_return_if_fail (name != NULL);
682 g_return_if_fail (platform_data == NULL
683 || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")));
685 if (platform_data == NULL)
686 platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
688 g_variant_ref (platform_data);
690 priv = application->priv;
694 _g_application_platform_remote_invoke_action (application, name, platform_data);
698 action = g_hash_table_lookup (priv->actions, name);
699 g_return_if_fail (action != NULL);
700 if (!action->enabled)
703 g_signal_emit (application, application_signals[ACTION_WITH_DATA],
704 g_quark_from_string (name),
709 g_variant_unref (platform_data);
713 * g_application_list_actions:
714 * @application: a #GApplication
716 * Retrieves the list of action names currently exported by @application.
718 * It is an error to call this function if @application is a proxy for
719 * a remote application.
721 * Return value: (transfer full): a newly allocation, %NULL-terminated array
722 * of strings containing action names; use g_strfreev() to free the
723 * resources used by the returned array
728 g_application_list_actions (GApplication *application)
730 GApplicationPrivate *priv;
736 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
737 g_return_val_if_fail (!application->priv->is_remote, NULL);
739 priv = application->priv;
741 retval = g_new (gchar*, g_hash_table_size (priv->actions));
744 g_hash_table_iter_init (&iter, priv->actions);
745 while (g_hash_table_iter_next (&iter, &key, NULL))
746 retval[i++] = g_strdup (key);
754 * g_application_set_action_enabled:
755 * @application: a #GApplication
756 * @name: the name of the application
757 * @enabled: whether to enable or disable the action @name
759 * Sets whether the action @name inside @application should be enabled
762 * It is an error to call this function if @application is a proxy for
763 * a remote application.
765 * Invoking a disabled action will not result in the #GApplication::action
766 * signal being emitted.
771 g_application_set_action_enabled (GApplication *application,
775 GApplicationAction *action;
777 g_return_if_fail (G_IS_APPLICATION (application));
778 g_return_if_fail (name != NULL);
779 g_return_if_fail (!application->priv->is_remote);
783 action = g_hash_table_lookup (application->priv->actions, name);
784 g_return_if_fail (action != NULL);
785 if (action->enabled == enabled)
788 action->enabled = enabled;
790 queue_actions_change_notification (application);
795 * g_application_get_action_description:
796 * @application: a #GApplication
799 * Gets the description of the action @name.
801 * It is an error to call this function if @application is a proxy for
802 * a remote application.
804 * Returns: Description for the given action named @name
808 G_CONST_RETURN gchar *
809 g_application_get_action_description (GApplication *application,
812 GApplicationAction *action;
814 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
815 g_return_val_if_fail (name != NULL, NULL);
816 g_return_val_if_fail (!application->priv->is_remote, NULL);
818 action = g_hash_table_lookup (application->priv->actions, name);
819 g_return_val_if_fail (action != NULL, NULL);
821 return action->description;
826 * g_application_get_action_enabled:
827 * @application: a #GApplication
828 * @name: the name of the action
830 * Retrieves whether the action @name is enabled or not.
832 * See g_application_set_action_enabled().
834 * It is an error to call this function if @application is a proxy for
835 * a remote application.
837 * Return value: %TRUE if the action was enabled, and %FALSE otherwise
842 g_application_get_action_enabled (GApplication *application,
845 GApplicationAction *action;
847 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
848 g_return_val_if_fail (name != NULL, FALSE);
849 g_return_val_if_fail (!application->priv->is_remote, FALSE);
851 action = g_hash_table_lookup (application->priv->actions, name);
852 g_return_val_if_fail (action != NULL, FALSE);
854 return action->enabled;
859 * @application: a #GApplication
861 * Starts the application.
863 * The default implementation of this virtual function will simply run
866 * It is an error to call this function if @application is a proxy for
867 * a remote application.
872 g_application_run (GApplication *application)
874 g_return_if_fail (G_IS_APPLICATION (application));
875 g_return_if_fail (!application->priv->is_remote);
877 G_APPLICATION_GET_CLASS (application)->run (application);
881 * g_application_quit_with_data:
882 * @application: a #GApplication
883 * @platform_data: (allow-none): platform-specific data
885 * Request that the application quits.
887 * This function has different behavior depending on whether @application
888 * is acting as a proxy for another process. In the normal case where
889 * the current process is hosting the application, the default
890 * implementation will quit the main loop created by g_application_run().
892 * If @application is a proxy, then the remote process will be asked
895 * Returns: %TRUE if the application accepted the request, %FALSE otherwise
900 g_application_quit_with_data (GApplication *application,
901 GVariant *platform_data)
903 gboolean retval = FALSE;
905 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
906 g_return_val_if_fail (platform_data == NULL
907 || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")), FALSE);
909 if (platform_data == NULL)
910 platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
912 g_variant_ref (platform_data);
914 if (application->priv->is_remote)
916 _g_application_platform_remote_quit (application, platform_data);
920 g_signal_emit (application, application_signals[QUIT_WITH_DATA], 0, platform_data, &retval);
922 g_variant_unref (platform_data);
928 * g_application_get_instance:
930 * In the normal case where there is exactly one #GApplication instance
931 * in this process, return that instance. If there are multiple, the
932 * first one created will be returned. Otherwise, return %NULL.
934 * Returns: (transfer none): The primary instance of #GApplication,
935 * or %NULL if none is set
940 g_application_get_instance (void)
942 return primary_application;
946 * g_application_get_id:
947 * @application: a #GApplication
949 * Retrieves the platform-specific identifier for the #GApplication.
951 * Return value: The platform-specific identifier. The returned string
952 * is owned by the #GApplication instance and it should never be
957 G_CONST_RETURN gchar *
958 g_application_get_id (GApplication *application)
960 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
962 return application->priv->appid;
966 * g_application_is_remote:
967 * @application: a #GApplication
969 * Returns whether the object represents a proxy for a remote application.
971 * Returns: %TRUE if this object represents a proxy for a remote application.
974 g_application_is_remote (GApplication *application)
976 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
978 return application->priv->is_remote;
982 g_application_init (GApplication *app)
984 app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
986 GApplicationPrivate);
988 app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
990 g_application_action_free);
991 app->priv->default_quit = TRUE;
992 app->priv->do_register = TRUE;
993 app->priv->is_remote = TRUE;
994 app->priv->platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
998 g_application_get_property (GObject *object,
1003 GApplication *app = G_APPLICATION (object);
1007 case PROP_APPLICATION_ID:
1008 g_value_set_string (value, g_application_get_id (app));
1011 case PROP_DEFAULT_QUIT:
1012 g_value_set_boolean (value, app->priv->default_quit);
1015 case PROP_IS_REMOTE:
1016 g_value_set_boolean (value, g_application_is_remote (app));
1020 g_value_set_boolean (value, app->priv->do_register);
1024 g_value_set_variant (value, app->priv->argv);
1027 case PROP_PLATFORM_DATA:
1028 g_value_set_variant (value, app->priv->platform_data);
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1037 g_application_set_property (GObject *object,
1039 const GValue *value,
1042 GApplication *app = G_APPLICATION (object);
1046 case PROP_APPLICATION_ID:
1047 g_return_if_fail (_g_application_validate_id (g_value_get_string (value)));
1048 app->priv->appid = g_value_dup_string (value);
1051 case PROP_DEFAULT_QUIT:
1052 app->priv->default_quit = g_value_get_boolean (value);
1056 app->priv->do_register = g_value_get_boolean (value);
1060 app->priv->argv = g_value_dup_variant (value);
1063 case PROP_PLATFORM_DATA:
1065 GVariant *platform_data = g_value_get_variant (value);
1066 if (app->priv->platform_data)
1067 g_variant_unref (app->priv->platform_data);
1068 app->priv->platform_data = g_variant_ref_sink (append_cwd_to_platform_data (platform_data));
1073 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1078 g_application_constructor (GType type,
1079 guint n_construct_properties,
1080 GObjectConstructParam *construct_params)
1084 const char *appid = NULL;
1087 for (i = 0; i < n_construct_properties; i++)
1089 GObjectConstructParam *param = &construct_params[i];
1090 if (strcmp (param->pspec->name, "application-id") == 0)
1091 appid = g_value_get_string (param->value);
1094 g_return_val_if_fail (appid != NULL, NULL);
1096 app = application_for_appid (appid);
1098 return g_object_ref (app);
1100 object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
1101 n_construct_properties,
1103 app = G_APPLICATION (object);
1105 if (primary_application == NULL)
1106 primary_application = app;
1107 g_hash_table_insert (instances_for_appid, g_strdup (appid), app);
1113 g_application_finalize (GObject *object)
1115 GApplication *app = G_APPLICATION (object);
1117 g_free (app->priv->appid);
1118 if (app->priv->actions)
1119 g_hash_table_unref (app->priv->actions);
1120 if (app->priv->actions_changed_id)
1121 g_source_remove (app->priv->actions_changed_id);
1122 if (app->priv->mainloop)
1123 g_main_loop_unref (app->priv->mainloop);
1126 g_free (app->priv->dbus_path);
1127 if (app->priv->session_bus)
1128 g_object_unref (app->priv->session_bus);
1131 G_OBJECT_CLASS (g_application_parent_class)->finalize (object);
1135 g_application_class_init (GApplicationClass *klass)
1137 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
1139 g_type_class_add_private (klass, sizeof (GApplicationPrivate));
1141 gobject_class->constructor = g_application_constructor;
1142 gobject_class->set_property = g_application_set_property;
1143 gobject_class->get_property = g_application_get_property;
1145 gobject_class->finalize = g_application_finalize;
1147 klass->run = g_application_default_run;
1148 klass->quit_with_data = g_application_default_quit_with_data;
1151 * GApplication::quit-with-data:
1152 * @application: the object on which the signal is emitted
1153 * @platform_data: Platform-specific data, or %NULL
1155 * This signal is emitted when the Quit action is invoked on the
1158 * The default handler for this signal exits the mainloop of the
1161 * Returns: %TRUE if the signal has been handled, %FALSE to continue
1164 application_signals[QUIT_WITH_DATA] =
1165 g_signal_new (g_intern_static_string ("quit-with-data"),
1166 G_OBJECT_CLASS_TYPE (klass),
1168 G_STRUCT_OFFSET (GApplicationClass, quit_with_data),
1169 g_signal_accumulator_true_handled, NULL,
1170 _gio_marshal_BOOLEAN__VARIANT,
1175 * GApplication::action-with-data:
1176 * @application: the object on which the signal is emitted
1177 * @name: The name of the activated action
1178 * @platform_data: Platform-specific data, or %NULL
1180 * This signal is emitted when an action is activated. The action name
1181 * is passed as the first argument, but also as signal detail, so it
1182 * is possible to connect to this signal for individual actions.
1184 * The signal is never emitted for disabled actions.
1186 application_signals[ACTION_WITH_DATA] =
1187 g_signal_new (g_intern_static_string ("action-with-data"),
1188 G_OBJECT_CLASS_TYPE (klass),
1189 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
1190 G_STRUCT_OFFSET (GApplicationClass, action_with_data),
1192 _gio_marshal_VOID__STRING_VARIANT,
1198 * GApplication::prepare-activation:
1199 * @application: the object on which the signal is emitted
1200 * @arguments: A #GVariant with the signature "aay"
1201 * @platform_data: A #GVariant with the signature "a{sv}", or %NULL
1203 * This signal is emitted when a non-primary process for a given
1204 * application is invoked while your application is running; for
1205 * example, when a file browser launches your program to open a
1206 * file. The raw operating system arguments are passed in the
1207 * @arguments variant. Additional platform-dependent data is
1208 * stored in @platform_data.
1210 application_signals[PREPARE_ACTIVATION] =
1211 g_signal_new (g_intern_static_string ("prepare-activation"),
1212 G_OBJECT_CLASS_TYPE (klass),
1214 G_STRUCT_OFFSET (GApplicationClass, prepare_activation),
1216 _gio_marshal_VOID__VARIANT_VARIANT,
1222 * GApplication:application-id:
1224 * The unique identifier for this application. See the documentation for
1225 * #GApplication for more information about this property.
1228 g_object_class_install_property (gobject_class,
1229 PROP_APPLICATION_ID,
1230 g_param_spec_string ("application-id",
1231 P_("Application ID"),
1232 P_("Identifier for this application"),
1235 G_PARAM_CONSTRUCT_ONLY |
1236 G_PARAM_STATIC_STRINGS));
1239 * GApplication:argv:
1241 * The argument vector given to this application. It must be a #GVariant
1242 * with a type signature "aay".
1245 g_object_class_install_property (gobject_class,
1247 g_param_spec_variant ("argv",
1248 P_("Argument vector"),
1249 P_("System argument vector with type signature aay"),
1250 G_VARIANT_TYPE ("aay"),
1253 G_PARAM_CONSTRUCT_ONLY |
1254 G_PARAM_STATIC_STRINGS));
1257 * GApplication:platform-data:
1259 * Platform-specific data retrieved from the operating system
1260 * environment. It must be a #GVariant with type signature "a{sv}".
1263 g_object_class_install_property (gobject_class,
1265 g_param_spec_variant ("platform-data",
1266 P_("Platform data"),
1267 P_("Environmental data, must have type signature a{sv}"),
1268 G_VARIANT_TYPE ("a{sv}"),
1271 G_PARAM_CONSTRUCT_ONLY |
1272 G_PARAM_STATIC_STRINGS));
1275 * GApplication:default-quit:
1277 * By default, if a different process is running this application, the
1278 * process will be exited. Set this property to %FALSE to allow custom
1279 * interaction with the remote process.
1282 g_object_class_install_property (gobject_class,
1284 g_param_spec_boolean ("default-quit",
1286 P_("Exit the process by default"),
1289 G_PARAM_CONSTRUCT_ONLY |
1290 G_PARAM_STATIC_STRINGS));
1294 * GApplication:is-remote:
1296 * This property is %TRUE if this application instance represents a proxy
1297 * to the instance of this application in another process.
1300 g_object_class_install_property (gobject_class,
1302 g_param_spec_boolean ("is-remote",
1304 P_("Whether this application is a proxy for another process"),
1306 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1309 * GApplication:register:
1311 * If this property is %FALSE, the application construction will not attempt
1312 * to ensure process uniqueness, and the application is guaranteed to be in the
1313 * remote state. See GApplication:is-remote.
1315 g_object_class_install_property (gobject_class,
1317 g_param_spec_boolean ("register",
1319 P_("If false, do not "),
1322 G_PARAM_CONSTRUCT_ONLY |
1323 G_PARAM_STATIC_STRINGS));