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>
24 #include "gapplicationimpl.h"
26 #include "gactiongroup.h"
27 #include "gactiongroupexporter.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusactiongroup-private.h"
30 #include "gapplication.h"
32 #include "gdbusconnection.h"
33 #include "gdbusintrospection.h"
34 #include "gdbuserror.h"
35 #include "glib/gstdio.h"
40 #include "gapplicationcommandline.h"
41 #include "gdbusmethodinvocation.h"
44 #include "gunixinputstream.h"
45 #include "gunixfdlist.h"
48 /* DBus Interface definition {{{1 */
50 /* For documentation of these interfaces, see
51 * http://live.gnome.org/GTK+/GApplication-dbus-apis
53 static const gchar org_gtk_Application_xml[] =
55 "<interface name='org.gtk.Application'>"
56 "<method name='Activate'>"
57 "<arg type='a{sv}' name='platform-data' direction='in'/>"
59 "<method name='Open'>"
60 "<arg type='as' name='uris' direction='in'/>"
61 "<arg type='s' name='hint' direction='in'/>"
62 "<arg type='a{sv}' name='platform-data' direction='in'/>"
64 "<method name='CommandLine'>"
65 "<arg type='o' name='path' direction='in'/>"
66 "<arg type='aay' name='arguments' direction='in'/>"
67 "<arg type='a{sv}' name='platform-data' direction='in'/>"
68 "<arg type='i' name='exit-status' direction='out'/>"
70 "<property name='Busy' type='b' access='read'/>"
74 static GDBusInterfaceInfo *org_gtk_Application;
76 static const gchar org_freedesktop_Application_xml[] =
78 "<interface name='org.freedesktop.Application'>"
79 "<method name='Activate'>"
80 "<arg type='a{sv}' name='platform-data' direction='in'/>"
82 "<method name='Open'>"
83 "<arg type='as' name='uris' direction='in'/>"
84 "<arg type='a{sv}' name='platform-data' direction='in'/>"
86 "<method name='ActivateAction'>"
87 "<arg type='s' name='action-name' direction='in'/>"
88 "<arg type='av' name='parameter' direction='in'/>"
89 "<arg type='a{sv}' name='platform-data' direction='in'/>"
94 static GDBusInterfaceInfo *org_freedesktop_Application;
96 static const gchar org_gtk_private_CommandLine_xml[] =
98 "<interface name='org.gtk.private.CommandLine'>"
99 "<method name='Print'>"
100 "<arg type='s' name='message' direction='in'/>"
102 "<method name='PrintError'>"
103 "<arg type='s' name='message' direction='in'/>"
108 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
110 /* GApplication implementation {{{1 */
111 struct _GApplicationImpl
113 GDBusConnection *session_bus;
114 GActionGroup *exported_actions;
115 const gchar *bus_name;
122 gboolean properties_live;
129 static GApplicationCommandLine *
130 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
133 g_application_impl_get_property (GDBusConnection *connection,
135 const gchar *object_path,
136 const gchar *interface_name,
137 const gchar *property_name,
141 GApplicationImpl *impl = user_data;
143 if (strcmp (property_name, "Busy") == 0)
144 return g_variant_new_boolean (impl->busy);
146 g_assert_not_reached ();
152 send_property_change (GApplicationImpl *impl)
154 GVariantBuilder builder;
156 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
157 g_variant_builder_add (&builder,
159 "Busy", g_variant_new_boolean (impl->busy));
161 g_dbus_connection_emit_signal (impl->session_bus,
164 "org.freedesktop.DBus.Properties",
166 g_variant_new ("(sa{sv}as)",
167 "org.gtk.Application",
174 g_application_impl_method_call (GDBusConnection *connection,
176 const gchar *object_path,
177 const gchar *interface_name,
178 const gchar *method_name,
179 GVariant *parameters,
180 GDBusMethodInvocation *invocation,
183 GApplicationImpl *impl = user_data;
184 GApplicationClass *class;
186 class = G_APPLICATION_GET_CLASS (impl->app);
188 if (strcmp (method_name, "Activate") == 0)
190 GVariant *platform_data;
192 /* Completely the same for both freedesktop and gtk interfaces */
194 g_variant_get (parameters, "(@a{sv})", &platform_data);
196 class->before_emit (impl->app, platform_data);
197 g_signal_emit_by_name (impl->app, "activate");
198 class->after_emit (impl->app, platform_data);
199 g_variant_unref (platform_data);
201 g_dbus_method_invocation_return_value (invocation, NULL);
204 else if (strcmp (method_name, "Open") == 0)
206 GVariant *platform_data;
212 /* freedesktop interface has no hint parameter */
213 if (g_str_equal (interface_name, "org.freedesktop.Application"))
215 g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
219 g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
221 n = g_variant_n_children (array);
222 files = g_new (GFile *, n + 1);
224 for (i = 0; i < n; i++)
228 g_variant_get_child (array, i, "&s", &uri);
229 files[i] = g_file_new_for_uri (uri);
231 g_variant_unref (array);
234 class->before_emit (impl->app, platform_data);
235 g_signal_emit_by_name (impl->app, "open", files, n, hint);
236 class->after_emit (impl->app, platform_data);
238 g_variant_unref (platform_data);
240 for (i = 0; i < n; i++)
241 g_object_unref (files[i]);
244 g_dbus_method_invocation_return_value (invocation, NULL);
247 else if (strcmp (method_name, "CommandLine") == 0)
249 GApplicationCommandLine *cmdline;
250 GVariant *platform_data;
253 /* Only on the GtkApplication interface */
255 cmdline = g_dbus_command_line_new (invocation);
256 platform_data = g_variant_get_child_value (parameters, 2);
257 class->before_emit (impl->app, platform_data);
258 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
259 g_application_command_line_set_exit_status (cmdline, status);
260 class->after_emit (impl->app, platform_data);
261 g_variant_unref (platform_data);
262 g_object_unref (cmdline);
264 else if (g_str_equal (method_name, "ActivateAction"))
266 GVariant *parameter = NULL;
267 GVariant *platform_data;
271 /* Only on the freedesktop interface */
273 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
274 g_variant_iter_next (iter, "v", ¶meter);
275 g_variant_iter_free (iter);
277 class->before_emit (impl->app, platform_data);
278 g_action_group_activate_action (impl->exported_actions, name, parameter);
279 class->after_emit (impl->app, platform_data);
282 g_variant_unref (parameter);
284 g_variant_unref (platform_data);
287 g_assert_not_reached ();
291 application_path_from_appid (const gchar *appid)
293 gchar *appid_path, *iter;
296 /* this is a private implementation detail */
297 return g_strdup ("/org/gtk/Application/anonymous");
299 appid_path = g_strconcat ("/", appid, NULL);
300 for (iter = appid_path; *iter; iter++)
312 /* Attempt to become the primary instance.
314 * Returns %TRUE if everything went OK, regardless of if we became the
315 * primary instance or not. %FALSE is reserved for when something went
316 * seriously wrong (and @error will be set too, in that case).
318 * After a %TRUE return, impl->primary will be TRUE if we were
322 g_application_impl_attempt_primary (GApplicationImpl *impl,
323 GCancellable *cancellable,
326 const static GDBusInterfaceVTable vtable = {
327 g_application_impl_method_call,
328 g_application_impl_get_property,
329 NULL /* set_property */
331 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
335 if (org_gtk_Application == NULL)
337 GError *error = NULL;
340 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
341 if G_UNLIKELY (info == NULL)
342 g_error ("%s", error->message);
343 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
344 g_assert (org_gtk_Application != NULL);
345 g_dbus_interface_info_ref (org_gtk_Application);
346 g_dbus_node_info_unref (info);
348 info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error);
349 if G_UNLIKELY (info == NULL)
350 g_error ("%s", error->message);
351 org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
352 g_assert (org_freedesktop_Application != NULL);
353 g_dbus_interface_info_ref (org_freedesktop_Application);
354 g_dbus_node_info_unref (info);
357 /* We could possibly have been D-Bus activated as a result of incoming
358 * requests on either the application or actiongroup interfaces.
359 * Because of how GDBus dispatches messages, we need to ensure that
360 * both of those things are registered before we attempt to request
363 * The action group need not be populated yet, as long as it happens
364 * before we return to the mainloop. The reason for that is because
365 * GDBus does the check to make sure the object exists from the worker
366 * thread but doesn't actually dispatch the action invocation until we
367 * hit the mainloop in this thread. There is also no danger of
368 * receiving 'activate' or 'open' signals until after 'startup' runs,
369 * for the same reason.
371 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
372 org_gtk_Application, &vtable, impl, NULL, error);
374 if (impl->object_id == 0)
377 impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
378 org_freedesktop_Application, &vtable, impl, NULL, error);
380 if (impl->fdo_object_id == 0)
383 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
384 impl->exported_actions, error);
386 if (impl->actions_id == 0)
389 if (!app_class->dbus_register (impl->app,
395 if (impl->bus_name == NULL)
397 /* If this is a non-unique application then it is sufficient to
398 * have our object paths registered. We can return now.
400 * Note: non-unique applications always act as primary-instance.
402 impl->primary = TRUE;
406 /* If this is a unique application then we need to attempt to own
407 * the well-known name and fall back to remote mode (!is_primary)
408 * in the case that we can't do that.
410 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
411 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
412 "org.freedesktop.DBus", "RequestName",
413 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
414 0, -1, cancellable, error);
419 g_variant_get (reply, "(u)", &rval);
420 g_variant_unref (reply);
422 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
423 impl->primary = (rval != 3);
428 /* Stop doing the things that the primary instance does.
430 * This should be called if attempting to become the primary instance
431 * failed (in order to clean up any partial success) and should also
432 * be called when freeing the GApplication.
434 * It is safe to call this multiple times.
437 g_application_impl_stop_primary (GApplicationImpl *impl)
439 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
441 app_class->dbus_unregister (impl->app,
447 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
451 if (impl->fdo_object_id)
453 g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
454 impl->fdo_object_id = 0;
457 if (impl->actions_id)
459 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
460 impl->actions_id = 0;
463 if (impl->primary && impl->bus_name)
465 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
466 "/org/freedesktop/DBus", "org.freedesktop.DBus",
467 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
468 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
469 impl->primary = FALSE;
474 g_application_impl_set_busy_state (GApplicationImpl *impl,
477 if (impl->busy != busy)
480 send_property_change (impl);
485 g_application_impl_destroy (GApplicationImpl *impl)
487 g_application_impl_stop_primary (impl);
489 if (impl->session_bus)
490 g_object_unref (impl->session_bus);
492 g_free (impl->object_path);
494 g_slice_free (GApplicationImpl, impl);
498 g_application_impl_register (GApplication *application,
500 GApplicationFlags flags,
501 GActionGroup *exported_actions,
502 GRemoteActionGroup **remote_actions,
503 GCancellable *cancellable,
506 GDBusActionGroup *actions;
507 GApplicationImpl *impl;
509 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
511 impl = g_slice_new0 (GApplicationImpl);
513 impl->app = application;
514 impl->exported_actions = exported_actions;
516 /* non-unique applications do not attempt to acquire a bus name */
517 if (~flags & G_APPLICATION_NON_UNIQUE)
518 impl->bus_name = appid;
520 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
522 if (impl->session_bus == NULL)
524 /* If we can't connect to the session bus, proceed as a normal
525 * non-unique application.
527 *remote_actions = NULL;
531 impl->object_path = application_path_from_appid (appid);
533 /* Only try to be the primary instance if
534 * G_APPLICATION_IS_LAUNCHER was not specified.
536 if (~flags & G_APPLICATION_IS_LAUNCHER)
538 if (!g_application_impl_attempt_primary (impl, cancellable, error))
540 g_application_impl_destroy (impl);
547 /* We didn't make it. Drop our service-side stuff. */
548 g_application_impl_stop_primary (impl);
550 if (flags & G_APPLICATION_IS_SERVICE)
552 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
553 "Unable to acquire bus name '%s'", appid);
554 g_application_impl_destroy (impl);
560 /* We are non-primary. Try to get the primary's list of actions.
561 * This also serves as a mechanism to ensure that the primary exists
562 * (ie: DBus service files installed correctly, etc).
564 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
565 if (!g_dbus_action_group_sync (actions, cancellable, error))
567 /* The primary appears not to exist. Fail the registration. */
568 g_application_impl_destroy (impl);
569 g_object_unref (actions);
574 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
580 g_application_impl_activate (GApplicationImpl *impl,
581 GVariant *platform_data)
583 g_dbus_connection_call (impl->session_bus,
586 "org.gtk.Application",
588 g_variant_new ("(@a{sv})", platform_data),
589 NULL, 0, -1, NULL, NULL, NULL);
593 g_application_impl_open (GApplicationImpl *impl,
597 GVariant *platform_data)
599 GVariantBuilder builder;
602 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
603 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
604 for (i = 0; i < n_files; i++)
606 gchar *uri = g_file_get_uri (files[i]);
607 g_variant_builder_add (&builder, "s", uri);
610 g_variant_builder_close (&builder);
611 g_variant_builder_add (&builder, "s", hint);
612 g_variant_builder_add_value (&builder, platform_data);
614 g_dbus_connection_call (impl->session_bus,
617 "org.gtk.Application",
619 g_variant_builder_end (&builder),
620 NULL, 0, -1, NULL, NULL, NULL);
624 g_application_impl_cmdline_method_call (GDBusConnection *connection,
626 const gchar *object_path,
627 const gchar *interface_name,
628 const gchar *method_name,
629 GVariant *parameters,
630 GDBusMethodInvocation *invocation,
633 const gchar *message;
635 g_variant_get_child (parameters, 0, "&s", &message);
637 if (strcmp (method_name, "Print") == 0)
638 g_print ("%s", message);
639 else if (strcmp (method_name, "PrintError") == 0)
640 g_printerr ("%s", message);
642 g_assert_not_reached ();
644 g_dbus_method_invocation_return_value (invocation, NULL);
654 g_application_impl_cmdline_done (GObject *source,
655 GAsyncResult *result,
658 CommandLineData *data = user_data;
659 GError *error = NULL;
663 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
665 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
671 g_variant_get (reply, "(i)", &data->status);
672 g_variant_unref (reply);
677 g_printerr ("%s\n", error->message);
678 g_error_free (error);
682 g_main_loop_quit (data->loop);
686 g_application_impl_command_line (GApplicationImpl *impl,
688 GVariant *platform_data)
690 const static GDBusInterfaceVTable vtable = {
691 g_application_impl_cmdline_method_call
693 const gchar *object_path = "/org/gtk/Application/CommandLine";
694 GMainContext *context;
695 CommandLineData data;
698 context = g_main_context_new ();
699 data.loop = g_main_loop_new (context, FALSE);
700 g_main_context_push_thread_default (context);
702 if (org_gtk_private_CommandLine == NULL)
704 GError *error = NULL;
707 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
708 if G_UNLIKELY (info == NULL)
709 g_error ("%s", error->message);
710 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
711 g_assert (org_gtk_private_CommandLine != NULL);
712 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
713 g_dbus_node_info_unref (info);
716 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
717 org_gtk_private_CommandLine,
718 &vtable, &data, NULL, NULL);
719 /* In theory we should try other paths... */
720 g_assert (object_id != 0);
724 GError *error = NULL;
725 GUnixFDList *fd_list;
727 /* send along the stdin in case
728 * g_application_command_line_get_stdin_data() is called
730 fd_list = g_unix_fd_list_new ();
731 g_unix_fd_list_append (fd_list, 0, &error);
732 g_assert_no_error (error);
734 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
735 "org.gtk.Application", "CommandLine",
736 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
737 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
738 g_application_impl_cmdline_done, &data);
741 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
742 "org.gtk.Application", "CommandLine",
743 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
744 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
745 g_application_impl_cmdline_done, &data);
748 g_main_loop_run (data.loop);
750 g_main_context_pop_thread_default (context);
751 g_main_context_unref (context);
752 g_main_loop_unref (data.loop);
758 g_application_impl_flush (GApplicationImpl *impl)
760 if (impl->session_bus)
761 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
765 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
767 return impl->session_bus;
771 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
773 return impl->object_path;
776 /* GDBusCommandLine implementation {{{1 */
778 typedef GApplicationCommandLineClass GDBusCommandLineClass;
779 static GType g_dbus_command_line_get_type (void);
782 GApplicationCommandLine parent_instance;
783 GDBusMethodInvocation *invocation;
785 GDBusConnection *connection;
786 const gchar *bus_name;
787 const gchar *object_path;
791 G_DEFINE_TYPE (GDBusCommandLine,
793 G_TYPE_APPLICATION_COMMAND_LINE)
796 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
797 const gchar *message)
799 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
801 g_dbus_connection_call (gdbcl->connection,
804 "org.gtk.private.CommandLine", "Print",
805 g_variant_new ("(s)", message),
806 NULL, 0, -1, NULL, NULL, NULL);
810 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
811 const gchar *message)
813 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
815 g_dbus_connection_call (gdbcl->connection,
818 "org.gtk.private.CommandLine", "PrintError",
819 g_variant_new ("(s)", message),
820 NULL, 0, -1, NULL, NULL, NULL);
823 static GInputStream *
824 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
827 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
828 GInputStream *result = NULL;
829 GDBusMessage *message;
830 GUnixFDList *fd_list;
832 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
833 fd_list = g_dbus_message_get_unix_fd_list (message);
835 if (fd_list && g_unix_fd_list_get_length (fd_list))
839 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
840 result = g_unix_input_stream_new (fds[0], TRUE);
841 for (i = 1; i < n_fds; i++)
842 (void) g_close (fds[i], NULL);
853 g_dbus_command_line_finalize (GObject *object)
855 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
856 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
859 status = g_application_command_line_get_exit_status (cmdline);
861 g_dbus_method_invocation_return_value (gdbcl->invocation,
862 g_variant_new ("(i)", status));
863 g_object_unref (gdbcl->invocation);
865 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
870 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
875 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
877 GObjectClass *object_class = G_OBJECT_CLASS (class);
879 object_class->finalize = g_dbus_command_line_finalize;
880 class->printerr_literal = g_dbus_command_line_printerr_literal;
881 class->print_literal = g_dbus_command_line_print_literal;
882 class->get_stdin = g_dbus_command_line_get_stdin;
885 static GApplicationCommandLine *
886 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
888 GDBusCommandLine *gdbcl;
891 args = g_dbus_method_invocation_get_parameters (invocation);
893 gdbcl = g_object_new (g_dbus_command_line_get_type (),
894 "arguments", g_variant_get_child_value (args, 1),
895 "platform-data", g_variant_get_child_value (args, 2),
897 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
898 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
899 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
900 gdbcl->invocation = g_object_ref (invocation);
902 return G_APPLICATION_COMMAND_LINE (gdbcl);
907 /* vim:set foldmethod=marker: */