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"
42 /* DBus Interface definition {{{1 */
44 /* For documentation of these interfaces, see
45 * http://live.gnome.org/GTK+/GApplication-dbus-apis
47 static const gchar org_gtk_Application_xml[] =
49 "<interface name='org.gtk.Application'>"
50 "<method name='Activate'>"
51 "<arg type='a{sv}' name='platform-data' direction='in'/>"
53 "<method name='Open'>"
54 "<arg type='as' name='uris' direction='in'/>"
55 "<arg type='s' name='hint' direction='in'/>"
56 "<arg type='a{sv}' name='platform-data' direction='in'/>"
58 "<method name='CommandLine'>"
59 "<arg type='o' name='path' direction='in'/>"
60 "<arg type='aay' name='arguments' direction='in'/>"
61 "<arg type='a{sv}' name='platform-data' direction='in'/>"
62 "<arg type='i' name='exit-status' direction='out'/>"
67 static GDBusInterfaceInfo *org_gtk_Application;
69 static const gchar org_gtk_private_CommandLine_xml[] =
71 "<interface name='org.gtk.private.CommandLine'>"
72 "<method name='Print'>"
73 "<arg type='s' name='message' direction='in'/>"
75 "<method name='PrintError'>"
76 "<arg type='s' name='message' direction='in'/>"
81 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
83 /* GApplication implementation {{{1 */
84 struct _GApplicationImpl
86 GDBusConnection *session_bus;
87 GActionGroup *exported_actions;
88 const gchar *bus_name;
94 gboolean properties_live;
100 static GApplicationCommandLine *
101 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
105 g_application_impl_method_call (GDBusConnection *connection,
107 const gchar *object_path,
108 const gchar *interface_name,
109 const gchar *method_name,
110 GVariant *parameters,
111 GDBusMethodInvocation *invocation,
114 GApplicationImpl *impl = user_data;
115 GApplicationClass *class;
117 class = G_APPLICATION_GET_CLASS (impl->app);
119 if (strcmp (method_name, "Activate") == 0)
121 GVariant *platform_data;
123 g_variant_get (parameters, "(@a{sv})", &platform_data);
124 class->before_emit (impl->app, platform_data);
125 g_signal_emit_by_name (impl->app, "activate");
126 class->after_emit (impl->app, platform_data);
127 g_variant_unref (platform_data);
129 g_dbus_method_invocation_return_value (invocation, NULL);
132 else if (strcmp (method_name, "Open") == 0)
134 GVariant *platform_data;
140 g_variant_get (parameters, "(@ass@a{sv})",
141 &array, &hint, &platform_data);
143 n = g_variant_n_children (array);
144 files = g_new (GFile *, n + 1);
146 for (i = 0; i < n; i++)
150 g_variant_get_child (array, i, "&s", &uri);
151 files[i] = g_file_new_for_uri (uri);
153 g_variant_unref (array);
156 class->before_emit (impl->app, platform_data);
157 g_signal_emit_by_name (impl->app, "open", files, n, hint);
158 class->after_emit (impl->app, platform_data);
160 g_variant_unref (platform_data);
162 for (i = 0; i < n; i++)
163 g_object_unref (files[i]);
166 g_dbus_method_invocation_return_value (invocation, NULL);
169 else if (strcmp (method_name, "CommandLine") == 0)
171 GApplicationCommandLine *cmdline;
172 GVariant *platform_data;
175 cmdline = g_dbus_command_line_new (invocation);
176 platform_data = g_variant_get_child_value (parameters, 2);
177 class->before_emit (impl->app, platform_data);
178 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
179 g_application_command_line_set_exit_status (cmdline, status);
180 class->after_emit (impl->app, platform_data);
181 g_variant_unref (platform_data);
182 g_object_unref (cmdline);
185 g_assert_not_reached ();
189 application_path_from_appid (const gchar *appid)
191 gchar *appid_path, *iter;
194 /* this is a private implementation detail */
195 return g_strdup ("/org/gtk/Application/anonymous");
197 appid_path = g_strconcat ("/", appid, NULL);
198 for (iter = appid_path; *iter; iter++)
210 /* Attempt to become the primary instance.
212 * Returns %TRUE if everything went OK, regardless of if we became the
213 * primary instance or not. %FALSE is reserved for when something went
214 * seriously wrong (and @error will be set too, in that case).
216 * After a %TRUE return, impl->primary will be TRUE if we were
220 g_application_impl_attempt_primary (GApplicationImpl *impl,
221 GCancellable *cancellable,
224 const static GDBusInterfaceVTable vtable = {
225 g_application_impl_method_call,
227 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
231 if (org_gtk_Application == NULL)
233 GError *error = NULL;
236 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
237 if G_UNLIKELY (info == NULL)
238 g_error ("%s", error->message);
239 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
240 g_assert (org_gtk_Application != NULL);
241 g_dbus_interface_info_ref (org_gtk_Application);
242 g_dbus_node_info_unref (info);
245 /* We could possibly have been D-Bus activated as a result of incoming
246 * requests on either the application or actiongroup interfaces.
247 * Because of how GDBus dispatches messages, we need to ensure that
248 * both of those things are registered before we attempt to request
251 * The action group need not be populated yet, as long as it happens
252 * before we return to the mainloop. The reason for that is because
253 * GDBus does the check to make sure the object exists from the worker
254 * thread but doesn't actually dispatch the action invocation until we
255 * hit the mainloop in this thread. There is also no danger of
256 * receiving 'activate' or 'open' signals until after 'startup' runs,
257 * for the same reason.
259 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
260 org_gtk_Application, &vtable, impl, NULL, error);
262 if (impl->object_id == 0)
265 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
266 impl->exported_actions, error);
268 if (impl->actions_id == 0)
271 if (!app_class->dbus_register (impl->app,
277 if (impl->bus_name == NULL)
279 /* If this is a non-unique application then it is sufficient to
280 * have our object paths registered. We can return now.
282 * Note: non-unique applications always act as primary-instance.
284 impl->primary = TRUE;
288 /* If this is a unique application then we need to attempt to own
289 * the well-known name and fall back to remote mode (!is_primary)
290 * in the case that we can't do that.
292 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
293 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
294 "org.freedesktop.DBus", "RequestName",
295 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
296 0, -1, cancellable, error);
301 g_variant_get (reply, "(u)", &rval);
302 g_variant_unref (reply);
304 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
305 impl->primary = (rval != 3);
310 /* Stop doing the things that the primary instance does.
312 * This should be called if attempting to become the primary instance
313 * failed (in order to clean up any partial success) and should also
314 * be called when freeing the GApplication.
316 * It is safe to call this multiple times.
319 g_application_impl_stop_primary (GApplicationImpl *impl)
321 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
323 app_class->dbus_unregister (impl->app,
329 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
333 if (impl->actions_id)
335 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
336 impl->actions_id = 0;
339 if (impl->primary && impl->bus_name)
341 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
342 "/org/freedesktop/DBus", "org.freedesktop.DBus",
343 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
344 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
345 impl->primary = FALSE;
350 g_application_impl_destroy (GApplicationImpl *impl)
352 g_application_impl_stop_primary (impl);
354 if (impl->session_bus)
355 g_object_unref (impl->session_bus);
357 g_free (impl->object_path);
359 g_slice_free (GApplicationImpl, impl);
363 g_application_impl_register (GApplication *application,
365 GApplicationFlags flags,
366 GActionGroup *exported_actions,
367 GRemoteActionGroup **remote_actions,
368 GCancellable *cancellable,
371 GDBusActionGroup *actions;
372 GApplicationImpl *impl;
374 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
376 impl = g_slice_new0 (GApplicationImpl);
378 impl->app = application;
379 impl->exported_actions = exported_actions;
381 /* non-unique applications do not attempt to acquire a bus name */
382 if (~flags & G_APPLICATION_NON_UNIQUE)
383 impl->bus_name = appid;
385 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
387 if (impl->session_bus == NULL)
389 /* If we can't connect to the session bus, proceed as a normal
390 * non-unique application.
392 *remote_actions = NULL;
396 impl->object_path = application_path_from_appid (appid);
398 /* Only try to be the primary instance if
399 * G_APPLICATION_IS_LAUNCHER was not specified.
401 if (~flags & G_APPLICATION_IS_LAUNCHER)
403 if (!g_application_impl_attempt_primary (impl, cancellable, error))
405 g_application_impl_destroy (impl);
412 /* We didn't make it. Drop our service-side stuff. */
413 g_application_impl_stop_primary (impl);
415 if (flags & G_APPLICATION_IS_SERVICE)
417 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
418 "Unable to acquire bus name `%s'", appid);
419 g_application_impl_destroy (impl);
425 /* We are non-primary. Try to get the primary's list of actions.
426 * This also serves as a mechanism to ensure that the primary exists
427 * (ie: DBus service files installed correctly, etc).
429 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
430 if (!g_dbus_action_group_sync (actions, cancellable, error))
432 /* The primary appears not to exist. Fail the registration. */
433 g_application_impl_destroy (impl);
434 g_object_unref (actions);
439 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
445 g_application_impl_activate (GApplicationImpl *impl,
446 GVariant *platform_data)
448 g_dbus_connection_call (impl->session_bus,
451 "org.gtk.Application",
453 g_variant_new ("(@a{sv})", platform_data),
454 NULL, 0, -1, NULL, NULL, NULL);
458 g_application_impl_open (GApplicationImpl *impl,
462 GVariant *platform_data)
464 GVariantBuilder builder;
467 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
468 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
469 for (i = 0; i < n_files; i++)
471 gchar *uri = g_file_get_uri (files[i]);
472 g_variant_builder_add (&builder, "s", uri);
475 g_variant_builder_close (&builder);
476 g_variant_builder_add (&builder, "s", hint);
477 g_variant_builder_add_value (&builder, platform_data);
479 g_dbus_connection_call (impl->session_bus,
482 "org.gtk.Application",
484 g_variant_builder_end (&builder),
485 NULL, 0, -1, NULL, NULL, NULL);
489 g_application_impl_cmdline_method_call (GDBusConnection *connection,
491 const gchar *object_path,
492 const gchar *interface_name,
493 const gchar *method_name,
494 GVariant *parameters,
495 GDBusMethodInvocation *invocation,
498 const gchar *message;
500 g_variant_get_child (parameters, 0, "&s", &message);
502 if (strcmp (method_name, "Print") == 0)
503 g_print ("%s", message);
504 else if (strcmp (method_name, "PrintError") == 0)
505 g_printerr ("%s", message);
507 g_assert_not_reached ();
509 g_dbus_method_invocation_return_value (invocation, NULL);
519 g_application_impl_cmdline_done (GObject *source,
520 GAsyncResult *result,
523 CommandLineData *data = user_data;
524 GError *error = NULL;
527 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
532 g_variant_get (reply, "(i)", &data->status);
533 g_variant_unref (reply);
538 g_printerr ("%s\n", error->message);
539 g_error_free (error);
543 g_main_loop_quit (data->loop);
547 g_application_impl_command_line (GApplicationImpl *impl,
549 GVariant *platform_data)
551 const static GDBusInterfaceVTable vtable = {
552 g_application_impl_cmdline_method_call
554 const gchar *object_path = "/org/gtk/Application/CommandLine";
555 GMainContext *context;
556 CommandLineData data;
559 context = g_main_context_new ();
560 data.loop = g_main_loop_new (context, FALSE);
561 g_main_context_push_thread_default (context);
563 if (org_gtk_private_CommandLine == NULL)
565 GError *error = NULL;
568 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
569 if G_UNLIKELY (info == NULL)
570 g_error ("%s", error->message);
571 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
572 g_assert (org_gtk_private_CommandLine != NULL);
573 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
574 g_dbus_node_info_unref (info);
577 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
578 org_gtk_private_CommandLine,
579 &vtable, &data, NULL, NULL);
580 /* In theory we should try other paths... */
581 g_assert (object_id != 0);
583 g_dbus_connection_call (impl->session_bus,
586 "org.gtk.Application",
588 g_variant_new ("(o^aay@a{sv})", object_path,
589 arguments, platform_data),
590 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
591 g_application_impl_cmdline_done, &data);
593 g_main_loop_run (data.loop);
595 g_main_context_pop_thread_default (context);
596 g_main_context_unref (context);
597 g_main_loop_unref (data.loop);
603 g_application_impl_flush (GApplicationImpl *impl)
605 if (impl->session_bus)
606 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
610 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
612 return impl->session_bus;
616 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
618 return impl->object_path;
621 /* GDBusCommandLine implementation {{{1 */
623 typedef GApplicationCommandLineClass GDBusCommandLineClass;
624 static GType g_dbus_command_line_get_type (void);
627 GApplicationCommandLine parent_instance;
628 GDBusMethodInvocation *invocation;
630 GDBusConnection *connection;
631 const gchar *bus_name;
632 const gchar *object_path;
636 G_DEFINE_TYPE (GDBusCommandLine,
638 G_TYPE_APPLICATION_COMMAND_LINE)
641 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
642 const gchar *message)
644 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
646 g_dbus_connection_call (gdbcl->connection,
649 "org.gtk.private.CommandLine", "Print",
650 g_variant_new ("(s)", message),
651 NULL, 0, -1, NULL, NULL, NULL);
655 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
656 const gchar *message)
658 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
660 g_dbus_connection_call (gdbcl->connection,
663 "org.gtk.private.CommandLine", "PrintError",
664 g_variant_new ("(s)", message),
665 NULL, 0, -1, NULL, NULL, NULL);
669 g_dbus_command_line_finalize (GObject *object)
671 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
672 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
675 status = g_application_command_line_get_exit_status (cmdline);
677 g_dbus_method_invocation_return_value (gdbcl->invocation,
678 g_variant_new ("(i)", status));
679 g_object_unref (gdbcl->invocation);
681 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
686 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
691 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
693 GObjectClass *object_class = G_OBJECT_CLASS (class);
695 object_class->finalize = g_dbus_command_line_finalize;
696 class->printerr_literal = g_dbus_command_line_printerr_literal;
697 class->print_literal = g_dbus_command_line_print_literal;
700 static GApplicationCommandLine *
701 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
703 GDBusCommandLine *gdbcl;
706 args = g_dbus_method_invocation_get_parameters (invocation);
708 gdbcl = g_object_new (g_dbus_command_line_get_type (),
709 "arguments", g_variant_get_child_value (args, 1),
710 "platform-data", g_variant_get_child_value (args, 2),
712 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
713 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
714 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
715 gdbcl->invocation = g_object_ref (invocation);
717 return G_APPLICATION_COMMAND_LINE (gdbcl);
722 /* vim:set foldmethod=marker: */