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"
39 #include "gapplicationcommandline.h"
40 #include "gdbusmethodinvocation.h"
43 #include "gunixinputstream.h"
44 #include "gunixfdlist.h"
47 /* DBus Interface definition {{{1 */
49 /* For documentation of these interfaces, see
50 * http://live.gnome.org/GTK+/GApplication-dbus-apis
52 static const gchar org_gtk_Application_xml[] =
54 "<interface name='org.gtk.Application'>"
55 "<method name='Activate'>"
56 "<arg type='a{sv}' name='platform-data' direction='in'/>"
58 "<method name='Open'>"
59 "<arg type='as' name='uris' direction='in'/>"
60 "<arg type='s' name='hint' direction='in'/>"
61 "<arg type='a{sv}' name='platform-data' direction='in'/>"
63 "<method name='CommandLine'>"
64 "<arg type='o' name='path' direction='in'/>"
65 "<arg type='aay' name='arguments' direction='in'/>"
66 "<arg type='a{sv}' name='platform-data' direction='in'/>"
67 "<arg type='i' name='exit-status' direction='out'/>"
72 static GDBusInterfaceInfo *org_gtk_Application;
74 static const gchar org_gtk_private_CommandLine_xml[] =
76 "<interface name='org.gtk.private.CommandLine'>"
77 "<method name='Print'>"
78 "<arg type='s' name='message' direction='in'/>"
80 "<method name='PrintError'>"
81 "<arg type='s' name='message' direction='in'/>"
86 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
88 /* GApplication implementation {{{1 */
89 struct _GApplicationImpl
91 GDBusConnection *session_bus;
92 GActionGroup *exported_actions;
93 const gchar *bus_name;
99 gboolean properties_live;
105 static GApplicationCommandLine *
106 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
110 g_application_impl_method_call (GDBusConnection *connection,
112 const gchar *object_path,
113 const gchar *interface_name,
114 const gchar *method_name,
115 GVariant *parameters,
116 GDBusMethodInvocation *invocation,
119 GApplicationImpl *impl = user_data;
120 GApplicationClass *class;
122 class = G_APPLICATION_GET_CLASS (impl->app);
124 if (strcmp (method_name, "Activate") == 0)
126 GVariant *platform_data;
128 g_variant_get (parameters, "(@a{sv})", &platform_data);
129 class->before_emit (impl->app, platform_data);
130 g_signal_emit_by_name (impl->app, "activate");
131 class->after_emit (impl->app, platform_data);
132 g_variant_unref (platform_data);
134 g_dbus_method_invocation_return_value (invocation, NULL);
137 else if (strcmp (method_name, "Open") == 0)
139 GVariant *platform_data;
145 g_variant_get (parameters, "(@ass@a{sv})",
146 &array, &hint, &platform_data);
148 n = g_variant_n_children (array);
149 files = g_new (GFile *, n + 1);
151 for (i = 0; i < n; i++)
155 g_variant_get_child (array, i, "&s", &uri);
156 files[i] = g_file_new_for_uri (uri);
158 g_variant_unref (array);
161 class->before_emit (impl->app, platform_data);
162 g_signal_emit_by_name (impl->app, "open", files, n, hint);
163 class->after_emit (impl->app, platform_data);
165 g_variant_unref (platform_data);
167 for (i = 0; i < n; i++)
168 g_object_unref (files[i]);
171 g_dbus_method_invocation_return_value (invocation, NULL);
174 else if (strcmp (method_name, "CommandLine") == 0)
176 GApplicationCommandLine *cmdline;
177 GVariant *platform_data;
180 cmdline = g_dbus_command_line_new (invocation);
181 platform_data = g_variant_get_child_value (parameters, 2);
182 class->before_emit (impl->app, platform_data);
183 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
184 g_application_command_line_set_exit_status (cmdline, status);
185 class->after_emit (impl->app, platform_data);
186 g_variant_unref (platform_data);
187 g_object_unref (cmdline);
190 g_assert_not_reached ();
194 application_path_from_appid (const gchar *appid)
196 gchar *appid_path, *iter;
199 /* this is a private implementation detail */
200 return g_strdup ("/org/gtk/Application/anonymous");
202 appid_path = g_strconcat ("/", appid, NULL);
203 for (iter = appid_path; *iter; iter++)
215 /* Attempt to become the primary instance.
217 * Returns %TRUE if everything went OK, regardless of if we became the
218 * primary instance or not. %FALSE is reserved for when something went
219 * seriously wrong (and @error will be set too, in that case).
221 * After a %TRUE return, impl->primary will be TRUE if we were
225 g_application_impl_attempt_primary (GApplicationImpl *impl,
226 GCancellable *cancellable,
229 const static GDBusInterfaceVTable vtable = {
230 g_application_impl_method_call,
232 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
236 if (org_gtk_Application == NULL)
238 GError *error = NULL;
241 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
242 if G_UNLIKELY (info == NULL)
243 g_error ("%s", error->message);
244 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
245 g_assert (org_gtk_Application != NULL);
246 g_dbus_interface_info_ref (org_gtk_Application);
247 g_dbus_node_info_unref (info);
250 /* We could possibly have been D-Bus activated as a result of incoming
251 * requests on either the application or actiongroup interfaces.
252 * Because of how GDBus dispatches messages, we need to ensure that
253 * both of those things are registered before we attempt to request
256 * The action group need not be populated yet, as long as it happens
257 * before we return to the mainloop. The reason for that is because
258 * GDBus does the check to make sure the object exists from the worker
259 * thread but doesn't actually dispatch the action invocation until we
260 * hit the mainloop in this thread. There is also no danger of
261 * receiving 'activate' or 'open' signals until after 'startup' runs,
262 * for the same reason.
264 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
265 org_gtk_Application, &vtable, impl, NULL, error);
267 if (impl->object_id == 0)
270 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
271 impl->exported_actions, error);
273 if (impl->actions_id == 0)
276 if (!app_class->dbus_register (impl->app,
282 if (impl->bus_name == NULL)
284 /* If this is a non-unique application then it is sufficient to
285 * have our object paths registered. We can return now.
287 * Note: non-unique applications always act as primary-instance.
289 impl->primary = TRUE;
293 /* If this is a unique application then we need to attempt to own
294 * the well-known name and fall back to remote mode (!is_primary)
295 * in the case that we can't do that.
297 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
298 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
299 "org.freedesktop.DBus", "RequestName",
300 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
301 0, -1, cancellable, error);
306 g_variant_get (reply, "(u)", &rval);
307 g_variant_unref (reply);
309 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
310 impl->primary = (rval != 3);
315 /* Stop doing the things that the primary instance does.
317 * This should be called if attempting to become the primary instance
318 * failed (in order to clean up any partial success) and should also
319 * be called when freeing the GApplication.
321 * It is safe to call this multiple times.
324 g_application_impl_stop_primary (GApplicationImpl *impl)
326 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
328 app_class->dbus_unregister (impl->app,
334 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
338 if (impl->actions_id)
340 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
341 impl->actions_id = 0;
344 if (impl->primary && impl->bus_name)
346 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
347 "/org/freedesktop/DBus", "org.freedesktop.DBus",
348 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
349 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
350 impl->primary = FALSE;
355 g_application_impl_destroy (GApplicationImpl *impl)
357 g_application_impl_stop_primary (impl);
359 if (impl->session_bus)
360 g_object_unref (impl->session_bus);
362 g_free (impl->object_path);
364 g_slice_free (GApplicationImpl, impl);
368 g_application_impl_register (GApplication *application,
370 GApplicationFlags flags,
371 GActionGroup *exported_actions,
372 GRemoteActionGroup **remote_actions,
373 GCancellable *cancellable,
376 GDBusActionGroup *actions;
377 GApplicationImpl *impl;
379 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
381 impl = g_slice_new0 (GApplicationImpl);
383 impl->app = application;
384 impl->exported_actions = exported_actions;
386 /* non-unique applications do not attempt to acquire a bus name */
387 if (~flags & G_APPLICATION_NON_UNIQUE)
388 impl->bus_name = appid;
390 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
392 if (impl->session_bus == NULL)
394 /* If we can't connect to the session bus, proceed as a normal
395 * non-unique application.
397 *remote_actions = NULL;
401 impl->object_path = application_path_from_appid (appid);
403 /* Only try to be the primary instance if
404 * G_APPLICATION_IS_LAUNCHER was not specified.
406 if (~flags & G_APPLICATION_IS_LAUNCHER)
408 if (!g_application_impl_attempt_primary (impl, cancellable, error))
410 g_application_impl_destroy (impl);
417 /* We didn't make it. Drop our service-side stuff. */
418 g_application_impl_stop_primary (impl);
420 if (flags & G_APPLICATION_IS_SERVICE)
422 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
423 "Unable to acquire bus name `%s'", appid);
424 g_application_impl_destroy (impl);
430 /* We are non-primary. Try to get the primary's list of actions.
431 * This also serves as a mechanism to ensure that the primary exists
432 * (ie: DBus service files installed correctly, etc).
434 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
435 if (!g_dbus_action_group_sync (actions, cancellable, error))
437 /* The primary appears not to exist. Fail the registration. */
438 g_application_impl_destroy (impl);
439 g_object_unref (actions);
444 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
450 g_application_impl_activate (GApplicationImpl *impl,
451 GVariant *platform_data)
453 g_dbus_connection_call (impl->session_bus,
456 "org.gtk.Application",
458 g_variant_new ("(@a{sv})", platform_data),
459 NULL, 0, -1, NULL, NULL, NULL);
463 g_application_impl_open (GApplicationImpl *impl,
467 GVariant *platform_data)
469 GVariantBuilder builder;
472 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
473 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
474 for (i = 0; i < n_files; i++)
476 gchar *uri = g_file_get_uri (files[i]);
477 g_variant_builder_add (&builder, "s", uri);
480 g_variant_builder_close (&builder);
481 g_variant_builder_add (&builder, "s", hint);
482 g_variant_builder_add_value (&builder, platform_data);
484 g_dbus_connection_call (impl->session_bus,
487 "org.gtk.Application",
489 g_variant_builder_end (&builder),
490 NULL, 0, -1, NULL, NULL, NULL);
494 g_application_impl_cmdline_method_call (GDBusConnection *connection,
496 const gchar *object_path,
497 const gchar *interface_name,
498 const gchar *method_name,
499 GVariant *parameters,
500 GDBusMethodInvocation *invocation,
503 const gchar *message;
505 g_variant_get_child (parameters, 0, "&s", &message);
507 if (strcmp (method_name, "Print") == 0)
508 g_print ("%s", message);
509 else if (strcmp (method_name, "PrintError") == 0)
510 g_printerr ("%s", message);
512 g_assert_not_reached ();
514 g_dbus_method_invocation_return_value (invocation, NULL);
524 g_application_impl_cmdline_done (GObject *source,
525 GAsyncResult *result,
528 CommandLineData *data = user_data;
529 GError *error = NULL;
533 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
535 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
541 g_variant_get (reply, "(i)", &data->status);
542 g_variant_unref (reply);
547 g_printerr ("%s\n", error->message);
548 g_error_free (error);
552 g_main_loop_quit (data->loop);
556 g_application_impl_command_line (GApplicationImpl *impl,
558 GVariant *platform_data)
560 const static GDBusInterfaceVTable vtable = {
561 g_application_impl_cmdline_method_call
563 const gchar *object_path = "/org/gtk/Application/CommandLine";
564 GMainContext *context;
565 CommandLineData data;
568 context = g_main_context_new ();
569 data.loop = g_main_loop_new (context, FALSE);
570 g_main_context_push_thread_default (context);
572 if (org_gtk_private_CommandLine == NULL)
574 GError *error = NULL;
577 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
578 if G_UNLIKELY (info == NULL)
579 g_error ("%s", error->message);
580 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
581 g_assert (org_gtk_private_CommandLine != NULL);
582 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
583 g_dbus_node_info_unref (info);
586 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
587 org_gtk_private_CommandLine,
588 &vtable, &data, NULL, NULL);
589 /* In theory we should try other paths... */
590 g_assert (object_id != 0);
594 GError *error = NULL;
595 GUnixFDList *fd_list;
597 /* send along the stdin in case
598 * g_application_command_line_get_stdin_data() is called
600 fd_list = g_unix_fd_list_new ();
601 g_unix_fd_list_append (fd_list, 0, &error);
602 g_assert_no_error (error);
604 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
605 "org.gtk.Application", "CommandLine",
606 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
607 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
608 g_application_impl_cmdline_done, &data);
611 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
612 "org.gtk.Application", "CommandLine",
613 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
614 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
615 g_application_impl_cmdline_done, &data);
618 g_main_loop_run (data.loop);
620 g_main_context_pop_thread_default (context);
621 g_main_context_unref (context);
622 g_main_loop_unref (data.loop);
628 g_application_impl_flush (GApplicationImpl *impl)
630 if (impl->session_bus)
631 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
635 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
637 return impl->session_bus;
641 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
643 return impl->object_path;
646 /* GDBusCommandLine implementation {{{1 */
648 typedef GApplicationCommandLineClass GDBusCommandLineClass;
649 static GType g_dbus_command_line_get_type (void);
652 GApplicationCommandLine parent_instance;
653 GDBusMethodInvocation *invocation;
655 GDBusConnection *connection;
656 const gchar *bus_name;
657 const gchar *object_path;
661 G_DEFINE_TYPE (GDBusCommandLine,
663 G_TYPE_APPLICATION_COMMAND_LINE)
666 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
667 const gchar *message)
669 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
671 g_dbus_connection_call (gdbcl->connection,
674 "org.gtk.private.CommandLine", "Print",
675 g_variant_new ("(s)", message),
676 NULL, 0, -1, NULL, NULL, NULL);
680 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
681 const gchar *message)
683 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
685 g_dbus_connection_call (gdbcl->connection,
688 "org.gtk.private.CommandLine", "PrintError",
689 g_variant_new ("(s)", message),
690 NULL, 0, -1, NULL, NULL, NULL);
693 static GInputStream *
694 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
697 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
698 GInputStream *result = NULL;
699 GDBusMessage *message;
700 GUnixFDList *fd_list;
702 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
703 fd_list = g_dbus_message_get_unix_fd_list (message);
705 if (fd_list && g_unix_fd_list_get_length (fd_list))
709 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
710 result = g_unix_input_stream_new (fds[0], TRUE);
711 for (i = 1; i < n_fds; i++)
723 g_dbus_command_line_finalize (GObject *object)
725 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
726 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
729 status = g_application_command_line_get_exit_status (cmdline);
731 g_dbus_method_invocation_return_value (gdbcl->invocation,
732 g_variant_new ("(i)", status));
733 g_object_unref (gdbcl->invocation);
735 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
740 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
745 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
747 GObjectClass *object_class = G_OBJECT_CLASS (class);
749 object_class->finalize = g_dbus_command_line_finalize;
750 class->printerr_literal = g_dbus_command_line_printerr_literal;
751 class->print_literal = g_dbus_command_line_print_literal;
752 class->get_stdin = g_dbus_command_line_get_stdin;
755 static GApplicationCommandLine *
756 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
758 GDBusCommandLine *gdbcl;
761 args = g_dbus_method_invocation_get_parameters (invocation);
763 gdbcl = g_object_new (g_dbus_command_line_get_type (),
764 "arguments", g_variant_get_child_value (args, 1),
765 "platform-data", g_variant_get_child_value (args, 2),
767 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
768 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
769 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
770 gdbcl->invocation = g_object_ref (invocation);
772 return G_APPLICATION_COMMAND_LINE (gdbcl);
777 /* vim:set foldmethod=marker: */