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 "gremoteactiongroup.h"
27 #include "gdbusactiongroup-private.h"
28 #include "gapplication.h"
30 #include "gdbusconnection.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
40 /* DBus Interface definition {{{1 */
42 /* For documentation of these interfaces, see
43 * http://live.gnome.org/GTK+/GApplication-dbus-apis
45 static const gchar org_gtk_Application_xml[] =
47 "<interface name='org.gtk.Application'>"
48 "<method name='Activate'>"
49 "<arg type='a{sv}' name='platform-data' direction='in'/>"
51 "<method name='Open'>"
52 "<arg type='as' name='uris' direction='in'/>"
53 "<arg type='s' name='hint' direction='in'/>"
54 "<arg type='a{sv}' name='platform-data' direction='in'/>"
56 "<method name='CommandLine'>"
57 "<arg type='o' name='path' direction='in'/>"
58 "<arg type='aay' name='arguments' direction='in'/>"
59 "<arg type='a{sv}' name='platform-data' direction='in'/>"
60 "<arg type='i' name='exit-status' direction='out'/>"
65 static GDBusInterfaceInfo *org_gtk_Application;
67 static const gchar org_gtk_private_CommandLine_xml[] =
69 "<interface name='org.gtk.private.CommandLine'>"
70 "<method name='Print'>"
71 "<arg type='s' name='message' direction='in'/>"
73 "<method name='PrintError'>"
74 "<arg type='s' name='message' direction='in'/>"
79 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
81 /* GApplication implementation {{{1 */
82 struct _GApplicationImpl
84 GDBusConnection *session_bus;
85 GActionGroup *exported_actions;
86 const gchar *bus_name;
92 gboolean properties_live;
98 static GApplicationCommandLine *
99 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
103 g_application_impl_method_call (GDBusConnection *connection,
105 const gchar *object_path,
106 const gchar *interface_name,
107 const gchar *method_name,
108 GVariant *parameters,
109 GDBusMethodInvocation *invocation,
112 GApplicationImpl *impl = user_data;
113 GApplicationClass *class;
115 class = G_APPLICATION_GET_CLASS (impl->app);
117 if (strcmp (method_name, "Activate") == 0)
119 GVariant *platform_data;
121 g_variant_get (parameters, "(@a{sv})", &platform_data);
122 class->before_emit (impl->app, platform_data);
123 g_signal_emit_by_name (impl->app, "activate");
124 class->after_emit (impl->app, platform_data);
125 g_variant_unref (platform_data);
127 g_dbus_method_invocation_return_value (invocation, NULL);
130 else if (strcmp (method_name, "Open") == 0)
132 GVariant *platform_data;
138 g_variant_get (parameters, "(@ass@a{sv})",
139 &array, &hint, &platform_data);
141 n = g_variant_n_children (array);
142 files = g_new (GFile *, n + 1);
144 for (i = 0; i < n; i++)
148 g_variant_get_child (array, i, "&s", &uri);
149 files[i] = g_file_new_for_uri (uri);
151 g_variant_unref (array);
154 class->before_emit (impl->app, platform_data);
155 g_signal_emit_by_name (impl->app, "open", files, n, hint);
156 class->after_emit (impl->app, platform_data);
158 g_variant_unref (platform_data);
160 for (i = 0; i < n; i++)
161 g_object_unref (files[i]);
164 g_dbus_method_invocation_return_value (invocation, NULL);
167 else if (strcmp (method_name, "CommandLine") == 0)
169 GApplicationCommandLine *cmdline;
170 GVariant *platform_data;
173 cmdline = g_dbus_command_line_new (invocation);
174 platform_data = g_variant_get_child_value (parameters, 2);
175 class->before_emit (impl->app, platform_data);
176 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
177 g_application_command_line_set_exit_status (cmdline, status);
178 class->after_emit (impl->app, platform_data);
179 g_variant_unref (platform_data);
180 g_object_unref (cmdline);
183 g_assert_not_reached ();
187 application_path_from_appid (const gchar *appid)
189 gchar *appid_path, *iter;
192 /* this is a private implementation detail */
193 return g_strdup ("/org/gtk/Application/anonymous");
195 appid_path = g_strconcat ("/", appid, NULL);
196 for (iter = appid_path; *iter; iter++)
208 /* Attempt to become the primary instance.
210 * Returns %TRUE if everything went OK, regardless of if we became the
211 * primary instance or not. %FALSE is reserved for when something went
212 * seriously wrong (and @error will be set too, in that case).
214 * After a %TRUE return, impl->primary will be TRUE if we were
218 g_application_impl_attempt_primary (GApplicationImpl *impl,
219 GCancellable *cancellable,
222 const static GDBusInterfaceVTable vtable = {
223 g_application_impl_method_call,
225 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
229 if (org_gtk_Application == NULL)
231 GError *error = NULL;
234 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
235 if G_UNLIKELY (info == NULL)
236 g_error ("%s", error->message);
237 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
238 g_assert (org_gtk_Application != NULL);
239 g_dbus_interface_info_ref (org_gtk_Application);
240 g_dbus_node_info_unref (info);
243 /* We could possibly have been D-Bus activated as a result of incoming
244 * requests on either the application or actiongroup interfaces.
245 * Because of how GDBus dispatches messages, we need to ensure that
246 * both of those things are registered before we attempt to request
249 * The action group need not be populated yet, as long as it happens
250 * before we return to the mainloop. The reason for that is because
251 * GDBus does the check to make sure the object exists from the worker
252 * thread but doesn't actually dispatch the action invocation until we
253 * hit the mainloop in this thread. There is also no danger of
254 * receiving 'activate' or 'open' signals until after 'startup' runs,
255 * for the same reason.
257 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
258 org_gtk_Application, &vtable, impl, NULL, error);
260 if (impl->object_id == 0)
263 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
264 impl->exported_actions, error);
266 if (impl->actions_id == 0)
269 if (!app_class->dbus_register (impl->app,
275 if (impl->bus_name == NULL)
277 /* If this is a non-unique application then it is sufficient to
278 * have our object paths registered. We can return now.
280 * Note: non-unique applications always act as primary-instance.
282 impl->primary = TRUE;
286 /* If this is a unique application then we need to attempt to own
287 * the well-known name and fall back to remote mode (!is_primary)
288 * in the case that we can't do that.
290 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
291 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
292 "org.freedesktop.DBus", "RequestName",
293 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
294 0, -1, cancellable, error);
299 g_variant_get (reply, "(u)", &rval);
300 g_variant_unref (reply);
302 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
303 impl->primary = (rval != 3);
308 /* Stop doing the things that the primary instance does.
310 * This should be called if attempting to become the primary instance
311 * failed (in order to clean up any partial success) and should also
312 * be called when freeing the GApplication.
314 * It is safe to call this multiple times.
317 g_application_impl_stop_primary (GApplicationImpl *impl)
319 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
321 app_class->dbus_unregister (impl->app,
327 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
331 if (impl->actions_id)
333 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
334 impl->actions_id = 0;
337 if (impl->primary && impl->bus_name)
339 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
340 "/org/freedesktop/DBus", "org.freedesktop.DBus",
341 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
342 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
343 impl->primary = FALSE;
348 g_application_impl_destroy (GApplicationImpl *impl)
350 g_application_impl_stop_primary (impl);
352 if (impl->session_bus)
353 g_object_unref (impl->session_bus);
355 g_free (impl->object_path);
357 g_slice_free (GApplicationImpl, impl);
361 g_application_impl_register (GApplication *application,
363 GApplicationFlags flags,
364 GActionGroup *exported_actions,
365 GRemoteActionGroup **remote_actions,
366 GCancellable *cancellable,
369 GDBusActionGroup *actions;
370 GApplicationImpl *impl;
372 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
374 impl = g_slice_new0 (GApplicationImpl);
376 impl->app = application;
377 impl->exported_actions = exported_actions;
379 /* non-unique applications do not attempt to acquire a bus name */
380 if (~flags & G_APPLICATION_NON_UNIQUE)
381 impl->bus_name = appid;
383 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
385 if (impl->session_bus == NULL)
387 /* If we can't connect to the session bus, proceed as a normal
388 * non-unique application.
390 *remote_actions = NULL;
394 impl->object_path = application_path_from_appid (appid);
396 /* Only try to be the primary instance if
397 * G_APPLICATION_IS_LAUNCHER was not specified.
399 if (~flags & G_APPLICATION_IS_LAUNCHER)
401 if (!g_application_impl_attempt_primary (impl, cancellable, error))
403 g_application_impl_destroy (impl);
410 /* We didn't make it. Drop our service-side stuff. */
411 g_application_impl_stop_primary (impl);
413 if (flags & G_APPLICATION_IS_SERVICE)
415 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
416 "Unable to acquire bus name `%s'", appid);
417 g_application_impl_destroy (impl);
423 /* We are non-primary. Try to get the primary's list of actions.
424 * This also serves as a mechanism to ensure that the primary exists
425 * (ie: DBus service files installed correctly, etc).
427 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
428 if (!g_dbus_action_group_sync (actions, cancellable, error))
430 /* The primary appears not to exist. Fail the registration. */
431 g_application_impl_destroy (impl);
432 g_object_unref (actions);
437 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
443 g_application_impl_activate (GApplicationImpl *impl,
444 GVariant *platform_data)
446 g_dbus_connection_call (impl->session_bus,
449 "org.gtk.Application",
451 g_variant_new ("(@a{sv})", platform_data),
452 NULL, 0, -1, NULL, NULL, NULL);
456 g_application_impl_open (GApplicationImpl *impl,
460 GVariant *platform_data)
462 GVariantBuilder builder;
465 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
466 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
467 for (i = 0; i < n_files; i++)
469 gchar *uri = g_file_get_uri (files[i]);
470 g_variant_builder_add (&builder, "s", uri);
473 g_variant_builder_close (&builder);
474 g_variant_builder_add (&builder, "s", hint);
475 g_variant_builder_add_value (&builder, platform_data);
477 g_dbus_connection_call (impl->session_bus,
480 "org.gtk.Application",
482 g_variant_builder_end (&builder),
483 NULL, 0, -1, NULL, NULL, NULL);
487 g_application_impl_cmdline_method_call (GDBusConnection *connection,
489 const gchar *object_path,
490 const gchar *interface_name,
491 const gchar *method_name,
492 GVariant *parameters,
493 GDBusMethodInvocation *invocation,
496 const gchar *message;
498 g_variant_get_child (parameters, 0, "&s", &message);
500 if (strcmp (method_name, "Print") == 0)
501 g_print ("%s", message);
502 else if (strcmp (method_name, "PrintError") == 0)
503 g_printerr ("%s", message);
505 g_assert_not_reached ();
507 g_dbus_method_invocation_return_value (invocation, NULL);
517 g_application_impl_cmdline_done (GObject *source,
518 GAsyncResult *result,
521 CommandLineData *data = user_data;
522 GError *error = NULL;
525 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
530 g_variant_get (reply, "(i)", &data->status);
531 g_variant_unref (reply);
536 g_printerr ("%s\n", error->message);
537 g_error_free (error);
541 g_main_loop_quit (data->loop);
545 g_application_impl_command_line (GApplicationImpl *impl,
547 GVariant *platform_data)
549 const static GDBusInterfaceVTable vtable = {
550 g_application_impl_cmdline_method_call
552 const gchar *object_path = "/org/gtk/Application/CommandLine";
553 GMainContext *context;
554 CommandLineData data;
557 context = g_main_context_new ();
558 data.loop = g_main_loop_new (context, FALSE);
559 g_main_context_push_thread_default (context);
561 if (org_gtk_private_CommandLine == NULL)
563 GError *error = NULL;
566 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
567 if G_UNLIKELY (info == NULL)
568 g_error ("%s", error->message);
569 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
570 g_assert (org_gtk_private_CommandLine != NULL);
571 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
572 g_dbus_node_info_unref (info);
575 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
576 org_gtk_private_CommandLine,
577 &vtable, &data, NULL, NULL);
578 /* In theory we should try other paths... */
579 g_assert (object_id != 0);
581 g_dbus_connection_call (impl->session_bus,
584 "org.gtk.Application",
586 g_variant_new ("(o^aay@a{sv})", object_path,
587 arguments, platform_data),
588 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
589 g_application_impl_cmdline_done, &data);
591 g_main_loop_run (data.loop);
593 g_main_context_pop_thread_default (context);
594 g_main_context_unref (context);
595 g_main_loop_unref (data.loop);
601 g_application_impl_flush (GApplicationImpl *impl)
603 if (impl->session_bus)
604 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
608 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
610 return impl->session_bus;
614 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
616 return impl->object_path;
619 /* GDBusCommandLine implementation {{{1 */
621 typedef GApplicationCommandLineClass GDBusCommandLineClass;
622 static GType g_dbus_command_line_get_type (void);
625 GApplicationCommandLine parent_instance;
626 GDBusMethodInvocation *invocation;
628 GDBusConnection *connection;
629 const gchar *bus_name;
630 const gchar *object_path;
634 G_DEFINE_TYPE (GDBusCommandLine,
636 G_TYPE_APPLICATION_COMMAND_LINE)
639 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
640 const gchar *message)
642 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
644 g_dbus_connection_call (gdbcl->connection,
647 "org.gtk.private.CommandLine", "Print",
648 g_variant_new ("(s)", message),
649 NULL, 0, -1, NULL, NULL, NULL);
653 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
654 const gchar *message)
656 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
658 g_dbus_connection_call (gdbcl->connection,
661 "org.gtk.private.CommandLine", "PrintError",
662 g_variant_new ("(s)", message),
663 NULL, 0, -1, NULL, NULL, NULL);
667 g_dbus_command_line_finalize (GObject *object)
669 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
670 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
673 status = g_application_command_line_get_exit_status (cmdline);
675 g_dbus_method_invocation_return_value (gdbcl->invocation,
676 g_variant_new ("(i)", status));
677 g_object_unref (gdbcl->invocation);
679 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
684 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
689 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
691 GObjectClass *object_class = G_OBJECT_CLASS (class);
693 object_class->finalize = g_dbus_command_line_finalize;
694 class->printerr_literal = g_dbus_command_line_printerr_literal;
695 class->print_literal = g_dbus_command_line_print_literal;
698 static GApplicationCommandLine *
699 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
701 GDBusCommandLine *gdbcl;
704 args = g_dbus_method_invocation_get_parameters (invocation);
706 gdbcl = g_object_new (g_dbus_command_line_get_type (),
707 "arguments", g_variant_get_child_value (args, 1),
708 "platform-data", g_variant_get_child_value (args, 2),
710 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
711 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
712 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
713 gdbcl->invocation = g_object_ref (invocation);
715 return G_APPLICATION_COMMAND_LINE (gdbcl);
720 /* vim:set foldmethod=marker: */