2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at 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>
22 #include "gapplicationimpl.h"
24 #include "gactiongroup.h"
25 #include "gactiongroupexporter.h"
26 #include "gdbusactiongroup.h"
27 #include "gapplication.h"
29 #include "gdbusconnection.h"
30 #include "gdbusintrospection.h"
31 #include "gdbuserror.h"
32 #include "gmenuexporter.h"
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
40 /* DBus Interface definition {{{1 */
41 static const gchar org_gtk_Application_xml[] =
43 " <interface name='org.gtk.Application'>"
44 " <method name='Activate'>"
45 " <arg type='a{sv}' name='platform-data' direction='in'/>"
47 " <method name='Open'>"
48 " <arg type='as' name='uris' direction='in'/>"
49 " <arg type='s' name='hint' direction='in'/>"
50 " <arg type='a{sv}' name='platform-data' direction='in'/>"
52 " <method name='CommandLine'>"
53 " <arg type='o' name='path' direction='in'/>"
54 " <arg type='aay' name='arguments' direction='in'/>"
55 " <arg type='a{sv}' name='platform-data' direction='in'/>"
56 " <arg type='i' name='exit-status' direction='out'/>"
58 " <property name='AppMenu' type='ao' access='read'/>"
59 " <property name='MenuBar' type='ao' access='read'/>"
63 static GDBusInterfaceInfo *org_gtk_Application;
65 static const gchar org_gtk_private_CommandLine_xml[] =
67 " <interface name='org.gtk.private.CommandLine'>"
68 " <method name='Print'>"
69 " <arg type='s' name='message' direction='in'/>"
71 " <method name='PrintError'>"
72 " <arg type='s' name='message' direction='in'/>"
77 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
79 /* GApplication implementation {{{1 */
80 struct _GApplicationImpl
82 GDBusConnection *session_bus;
83 const gchar *bus_name;
95 gboolean properties_live;
101 static GApplicationCommandLine *
102 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
106 g_application_impl_method_call (GDBusConnection *connection,
108 const gchar *object_path,
109 const gchar *interface_name,
110 const gchar *method_name,
111 GVariant *parameters,
112 GDBusMethodInvocation *invocation,
115 GApplicationImpl *impl = user_data;
116 GApplicationClass *class;
118 class = G_APPLICATION_GET_CLASS (impl->app);
120 if (strcmp (method_name, "Activate") == 0)
122 GVariant *platform_data;
124 g_variant_get (parameters, "(@a{sv})", &platform_data);
125 class->before_emit (impl->app, platform_data);
126 g_signal_emit_by_name (impl->app, "activate");
127 class->after_emit (impl->app, platform_data);
128 g_variant_unref (platform_data);
130 g_dbus_method_invocation_return_value (invocation, NULL);
133 else if (strcmp (method_name, "Open") == 0)
135 GVariant *platform_data;
141 g_variant_get (parameters, "(@ass@a{sv})",
142 &array, &hint, &platform_data);
144 n = g_variant_n_children (array);
145 files = g_new (GFile *, n + 1);
147 for (i = 0; i < n; i++)
151 g_variant_get_child (array, i, "&s", &uri);
152 files[i] = g_file_new_for_uri (uri);
154 g_variant_unref (array);
157 class->before_emit (impl->app, platform_data);
158 g_signal_emit_by_name (impl->app, "open", files, n, hint);
159 class->after_emit (impl->app, platform_data);
161 g_variant_unref (platform_data);
163 for (i = 0; i < n; i++)
164 g_object_unref (files[i]);
167 g_dbus_method_invocation_return_value (invocation, NULL);
170 else if (strcmp (method_name, "CommandLine") == 0)
172 GApplicationCommandLine *cmdline;
173 GVariant *platform_data;
176 cmdline = g_dbus_command_line_new (invocation);
177 platform_data = g_variant_get_child_value (parameters, 2);
178 class->before_emit (impl->app, platform_data);
179 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
180 g_application_command_line_set_exit_status (cmdline, status);
181 class->after_emit (impl->app, platform_data);
182 g_variant_unref (platform_data);
183 g_object_unref (cmdline);
186 g_assert_not_reached ();
190 g_application_impl_get_property (GDBusConnection *connection,
192 const gchar *object_path,
193 const gchar *interface_name,
194 const gchar *property_name,
198 GApplicationImpl *impl = user_data;
199 GVariantBuilder builder;
201 /* We use this boolean to detect if the properties have ever been
202 * queried before. If they have not been queried, then there is no
203 * point emitting change signals since nobody is watching anyway.
205 impl->properties_live = TRUE;
207 g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
209 if (g_str_equal (property_name, "AppMenu"))
211 if (impl->app_menu_path != NULL)
212 g_variant_builder_add (&builder, "o", impl->app_menu_path);
215 else if (g_str_equal (property_name, "MenuBar"))
217 if (impl->menubar_path != NULL)
218 g_variant_builder_add (&builder, "o", impl->menubar_path);
222 g_assert_not_reached ();
224 return g_variant_builder_end (&builder);
228 application_path_from_appid (const gchar *appid)
230 gchar *appid_path, *iter;
232 appid_path = g_strconcat ("/", appid, NULL);
233 for (iter = appid_path; *iter; iter++)
246 g_application_impl_publish_menu (GApplicationImpl *impl,
254 /* unexport any existing menu */
257 g_dbus_connection_unexport_menu_model (impl->session_bus, *id);
263 /* export the new menu, if there is one */
266 /* try getting the preferred name */
267 *path = g_strconcat (impl->object_path, "/menus/", type, NULL);
268 *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL);
270 /* keep trying until we get a working name... */
271 for (i = 0; *id == 0; i++)
274 *path = g_strdup_printf ("%s/menus/%s%d", impl->object_path, type, i);
275 *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL);
279 /* notify for changes, if needed */
280 if (impl->properties_live)
282 GVariantBuilder builder;
284 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
285 g_variant_builder_add (&builder, "{sv}", type, g_variant_new_objv ((const gchar **) path, *path != NULL));
286 g_dbus_connection_emit_signal (impl->session_bus, NULL, impl->object_path,
287 "org.freedesktop.DBus.Properties", "PropertiesChanged",
288 g_variant_new ("(sa{sv}as)", "org.gtk.Actions", &builder, NULL), NULL);
293 g_application_impl_app_menu_changed (GObject *source,
297 GApplicationImpl *impl = user_data;
299 g_assert (source == impl->app);
301 g_application_impl_publish_menu (impl, "AppMenu", g_application_get_app_menu (impl->app),
302 &impl->app_menu_id, &impl->app_menu_path);
306 g_application_impl_menubar_changed (GObject *source,
310 GApplicationImpl *impl = user_data;
312 g_assert (source == impl->app);
314 g_application_impl_publish_menu (impl, "MenuBar", g_application_get_menubar (impl->app),
315 &impl->menubar_id, &impl->menubar_path);
318 /* Attempt to become the primary instance.
320 * Returns %TRUE if everything went OK, regardless of if we became the
321 * primary instance or not. %FALSE is reserved for when something went
322 * seriously wrong (and @error will be set too, in that case).
324 * After a %TRUE return, impl->primary will be TRUE if we were
328 g_application_impl_attempt_primary (GApplicationImpl *impl,
329 GCancellable *cancellable,
332 const static GDBusInterfaceVTable vtable = {
333 g_application_impl_method_call,
334 g_application_impl_get_property
339 if (org_gtk_Application == NULL)
341 GError *error = NULL;
344 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
345 if G_UNLIKELY (info == NULL)
346 g_error ("%s", error->message);
347 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
348 g_assert (org_gtk_Application != NULL);
349 g_dbus_interface_info_ref (org_gtk_Application);
350 g_dbus_node_info_unref (info);
353 /* We could possibly have been D-Bus activated as a result of incoming
354 * requests on either the application or actiongroup interfaces.
355 * Because of how GDBus dispatches messages, we need to ensure that
356 * both of those things are registered before we attempt to request
359 * The action group need not be populated yet, as long as it happens
360 * before we return to the mainloop. The reason for that is because
361 * GDBus does the check to make sure the object exists from the worker
362 * thread but doesn't actually dispatch the action invocation until we
363 * hit the mainloop in this thread. There is also no danger of
364 * receiving 'activate' or 'open' signals until after 'startup' runs,
365 * for the same reason.
367 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
368 org_gtk_Application, &vtable, impl, NULL, error);
370 if (impl->object_id == 0)
373 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path, impl->app, error);
375 if (impl->actions_id == 0)
378 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
379 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
380 "org.freedesktop.DBus", "RequestName",
381 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
382 0, -1, cancellable, error);
387 g_variant_get (reply, "(u)", &rval);
388 g_variant_unref (reply);
390 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
391 impl->primary = (rval != 3);
395 g_signal_connect (impl->app, "notify::app-menu", G_CALLBACK (g_application_impl_app_menu_changed), impl);
396 g_signal_connect (impl->app, "notify::menubar", G_CALLBACK (g_application_impl_menubar_changed), impl);
397 g_application_impl_app_menu_changed (impl->app, NULL, impl);
398 g_application_impl_menubar_changed (impl->app, NULL, impl);
404 /* Stop doing the things that the primary instance does.
406 * This should be called if attempting to become the primary instance
407 * failed (in order to clean up any partial success) and should also
408 * be called when freeing the GApplication.
410 * It is safe to call this multiple times.
413 g_application_impl_stop_primary (GApplicationImpl *impl)
417 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
421 if (impl->actions_id)
423 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
424 impl->actions_id = 0;
429 g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_app_menu_changed, impl);
430 g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_menubar_changed, impl);
432 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
433 "/org/freedesktop/DBus", "org.freedesktop.DBus",
434 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
435 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
436 impl->primary = FALSE;
439 if (impl->app_menu_id)
441 g_dbus_connection_unexport_menu_model (impl->session_bus, impl->app_menu_id);
442 g_free (impl->app_menu_path);
443 impl->app_menu_path = NULL;
444 impl->app_menu_id = 0;
447 if (impl->menubar_id)
449 g_dbus_connection_unexport_menu_model (impl->session_bus, impl->menubar_id);
450 g_free (impl->menubar_path);
451 impl->menubar_path = NULL;
452 impl->menubar_id = 0;
457 g_application_impl_destroy (GApplicationImpl *impl)
459 g_application_impl_stop_primary (impl);
461 if (impl->session_bus)
462 g_object_unref (impl->session_bus);
464 g_free (impl->object_path);
466 g_slice_free (GApplicationImpl, impl);
470 g_application_impl_register (GApplication *application,
472 GApplicationFlags flags,
473 GActionGroup **remote_actions,
474 GCancellable *cancellable,
477 GDBusActionGroup *actions;
478 GApplicationImpl *impl;
480 impl = g_slice_new0 (GApplicationImpl);
482 impl->app = application;
483 impl->bus_name = appid;
485 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
487 if (impl->session_bus == NULL)
489 /* If we can't connect to the session bus, proceed as a normal
490 * non-unique application.
492 *remote_actions = NULL;
496 impl->object_path = application_path_from_appid (appid);
498 /* Only try to be the primary instance if
499 * G_APPLICATION_IS_LAUNCHER was not specified.
501 if (~flags & G_APPLICATION_IS_LAUNCHER)
503 if (!g_application_impl_attempt_primary (impl, cancellable, error))
505 g_application_impl_destroy (impl);
512 /* We didn't make it. Drop our service-side stuff. */
513 g_application_impl_stop_primary (impl);
515 if (flags & G_APPLICATION_IS_SERVICE)
517 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
518 "Unable to acquire bus name `%s'", appid);
519 g_application_impl_destroy (impl);
525 /* We are non-primary. Try to get the primary's list of actions.
526 * This also serves as a mechanism to ensure that the primary exists
527 * (ie: DBus service files installed correctly, etc).
529 actions = g_dbus_action_group_new_sync (impl->session_bus, impl->bus_name, impl->object_path,
530 G_DBUS_ACTION_GROUP_FLAGS_NONE, cancellable, error);
534 /* The primary appears not to exist. Fail the registration. */
535 g_application_impl_destroy (impl);
540 *remote_actions = G_ACTION_GROUP (actions);
546 g_application_impl_activate (GApplicationImpl *impl,
547 GVariant *platform_data)
549 g_dbus_connection_call (impl->session_bus,
552 "org.gtk.Application",
554 g_variant_new ("(@a{sv})", platform_data),
555 NULL, 0, -1, NULL, NULL, NULL);
559 g_application_impl_open (GApplicationImpl *impl,
563 GVariant *platform_data)
565 GVariantBuilder builder;
568 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
569 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
570 for (i = 0; i < n_files; i++)
572 gchar *uri = g_file_get_uri (files[i]);
573 g_variant_builder_add (&builder, "s", uri);
576 g_variant_builder_close (&builder);
577 g_variant_builder_add (&builder, "s", hint);
578 g_variant_builder_add_value (&builder, platform_data);
580 g_dbus_connection_call (impl->session_bus,
583 "org.gtk.Application",
585 g_variant_builder_end (&builder),
586 NULL, 0, -1, NULL, NULL, NULL);
590 g_application_impl_cmdline_method_call (GDBusConnection *connection,
592 const gchar *object_path,
593 const gchar *interface_name,
594 const gchar *method_name,
595 GVariant *parameters,
596 GDBusMethodInvocation *invocation,
599 const gchar *message;
601 g_variant_get_child (parameters, 0, "&s", &message);
603 if (strcmp (method_name, "Print") == 0)
604 g_print ("%s", message);
605 else if (strcmp (method_name, "PrintError") == 0)
606 g_printerr ("%s", message);
608 g_assert_not_reached ();
610 g_dbus_method_invocation_return_value (invocation, NULL);
620 g_application_impl_cmdline_done (GObject *source,
621 GAsyncResult *result,
624 CommandLineData *data = user_data;
625 GError *error = NULL;
628 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
633 g_variant_get (reply, "(i)", &data->status);
634 g_variant_unref (reply);
639 g_printerr ("%s\n", error->message);
640 g_error_free (error);
644 g_main_loop_quit (data->loop);
648 g_application_impl_command_line (GApplicationImpl *impl,
650 GVariant *platform_data)
652 const static GDBusInterfaceVTable vtable = {
653 g_application_impl_cmdline_method_call
655 const gchar *object_path = "/org/gtk/Application/CommandLine";
656 GMainContext *context;
657 CommandLineData data;
660 context = g_main_context_new ();
661 data.loop = g_main_loop_new (context, FALSE);
662 g_main_context_push_thread_default (context);
664 if (org_gtk_private_CommandLine == NULL)
666 GError *error = NULL;
669 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
670 if G_UNLIKELY (info == NULL)
671 g_error ("%s", error->message);
672 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
673 g_assert (org_gtk_private_CommandLine != NULL);
674 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
675 g_dbus_node_info_unref (info);
678 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
679 org_gtk_private_CommandLine,
680 &vtable, &data, NULL, NULL);
681 /* In theory we should try other paths... */
682 g_assert (object_id != 0);
684 g_dbus_connection_call (impl->session_bus,
687 "org.gtk.Application",
689 g_variant_new ("(o^aay@a{sv})", object_path,
690 arguments, platform_data),
691 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
692 g_application_impl_cmdline_done, &data);
694 g_main_loop_run (data.loop);
696 g_main_context_pop_thread_default (context);
697 g_main_context_unref (context);
698 g_main_loop_unref (data.loop);
704 g_application_impl_flush (GApplicationImpl *impl)
706 if (impl->session_bus)
707 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
711 /* GDBusCommandLine implementation {{{1 */
713 typedef GApplicationCommandLineClass GDBusCommandLineClass;
714 static GType g_dbus_command_line_get_type (void);
717 GApplicationCommandLine parent_instance;
718 GDBusMethodInvocation *invocation;
720 GDBusConnection *connection;
721 const gchar *bus_name;
722 const gchar *object_path;
726 G_DEFINE_TYPE (GDBusCommandLine,
728 G_TYPE_APPLICATION_COMMAND_LINE)
731 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
732 const gchar *message)
734 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
736 g_dbus_connection_call (gdbcl->connection,
739 "org.gtk.private.CommandLine", "Print",
740 g_variant_new ("(s)", message),
741 NULL, 0, -1, NULL, NULL, NULL);
745 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
746 const gchar *message)
748 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
750 g_dbus_connection_call (gdbcl->connection,
753 "org.gtk.private.CommandLine", "PrintError",
754 g_variant_new ("(s)", message),
755 NULL, 0, -1, NULL, NULL, NULL);
759 g_dbus_command_line_finalize (GObject *object)
761 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
762 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
765 status = g_application_command_line_get_exit_status (cmdline);
767 g_dbus_method_invocation_return_value (gdbcl->invocation,
768 g_variant_new ("(i)", status));
769 g_object_unref (gdbcl->invocation);
771 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
776 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
781 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
783 GObjectClass *object_class = G_OBJECT_CLASS (class);
785 object_class->finalize = g_dbus_command_line_finalize;
786 class->printerr_literal = g_dbus_command_line_printerr_literal;
787 class->print_literal = g_dbus_command_line_print_literal;
790 static GApplicationCommandLine *
791 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
793 GDBusCommandLine *gdbcl;
796 args = g_dbus_method_invocation_get_parameters (invocation);
798 gdbcl = g_object_new (g_dbus_command_line_get_type (),
799 "arguments", g_variant_get_child_value (args, 1),
800 "platform-data", g_variant_get_child_value (args, 2),
802 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
803 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
804 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
805 gdbcl->invocation = g_object_ref (invocation);
807 return G_APPLICATION_COMMAND_LINE (gdbcl);
812 /* vim:set foldmethod=marker: */