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.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 G_GNUC_INTERNAL gboolean
41 g_dbus_action_group_sync (GDBusActionGroup *group,
42 GCancellable *cancellable,
46 /* DBus Interface definition {{{1 */
48 /* For documentation of these interfaces, see
49 * http://live.gnome.org/GTK+/GApplication-dbus-apis
51 static const gchar org_gtk_Application_xml[] =
53 "<interface name='org.gtk.Application'>"
54 "<method name='Activate'>"
55 "<arg type='a{sv}' name='platform-data' direction='in'/>"
57 "<method name='Open'>"
58 "<arg type='as' name='uris' direction='in'/>"
59 "<arg type='s' name='hint' direction='in'/>"
60 "<arg type='a{sv}' name='platform-data' direction='in'/>"
62 "<method name='CommandLine'>"
63 "<arg type='o' name='path' direction='in'/>"
64 "<arg type='aay' name='arguments' direction='in'/>"
65 "<arg type='a{sv}' name='platform-data' direction='in'/>"
66 "<arg type='i' name='exit-status' direction='out'/>"
71 static GDBusInterfaceInfo *org_gtk_Application;
73 static const gchar org_gtk_private_CommandLine_xml[] =
75 "<interface name='org.gtk.private.CommandLine'>"
76 "<method name='Print'>"
77 "<arg type='s' name='message' direction='in'/>"
79 "<method name='PrintError'>"
80 "<arg type='s' name='message' direction='in'/>"
85 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
87 /* GApplication implementation {{{1 */
88 struct _GApplicationImpl
90 GDBusConnection *session_bus;
91 GActionGroup *exported_actions;
92 const gchar *bus_name;
98 gboolean properties_live;
104 static GApplicationCommandLine *
105 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
109 g_application_impl_method_call (GDBusConnection *connection,
111 const gchar *object_path,
112 const gchar *interface_name,
113 const gchar *method_name,
114 GVariant *parameters,
115 GDBusMethodInvocation *invocation,
118 GApplicationImpl *impl = user_data;
119 GApplicationClass *class;
121 class = G_APPLICATION_GET_CLASS (impl->app);
123 if (strcmp (method_name, "Activate") == 0)
125 GVariant *platform_data;
127 g_variant_get (parameters, "(@a{sv})", &platform_data);
128 class->before_emit (impl->app, platform_data);
129 g_signal_emit_by_name (impl->app, "activate");
130 class->after_emit (impl->app, platform_data);
131 g_variant_unref (platform_data);
133 g_dbus_method_invocation_return_value (invocation, NULL);
136 else if (strcmp (method_name, "Open") == 0)
138 GVariant *platform_data;
144 g_variant_get (parameters, "(@ass@a{sv})",
145 &array, &hint, &platform_data);
147 n = g_variant_n_children (array);
148 files = g_new (GFile *, n + 1);
150 for (i = 0; i < n; i++)
154 g_variant_get_child (array, i, "&s", &uri);
155 files[i] = g_file_new_for_uri (uri);
157 g_variant_unref (array);
160 class->before_emit (impl->app, platform_data);
161 g_signal_emit_by_name (impl->app, "open", files, n, hint);
162 class->after_emit (impl->app, platform_data);
164 g_variant_unref (platform_data);
166 for (i = 0; i < n; i++)
167 g_object_unref (files[i]);
170 g_dbus_method_invocation_return_value (invocation, NULL);
173 else if (strcmp (method_name, "CommandLine") == 0)
175 GApplicationCommandLine *cmdline;
176 GVariant *platform_data;
179 cmdline = g_dbus_command_line_new (invocation);
180 platform_data = g_variant_get_child_value (parameters, 2);
181 class->before_emit (impl->app, platform_data);
182 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
183 g_application_command_line_set_exit_status (cmdline, status);
184 class->after_emit (impl->app, platform_data);
185 g_variant_unref (platform_data);
186 g_object_unref (cmdline);
189 g_assert_not_reached ();
193 application_path_from_appid (const gchar *appid)
195 gchar *appid_path, *iter;
198 /* this is a private implementation detail */
199 return g_strdup ("/org/gtk/Application/anonymous");
201 appid_path = g_strconcat ("/", appid, NULL);
202 for (iter = appid_path; *iter; iter++)
214 /* Attempt to become the primary instance.
216 * Returns %TRUE if everything went OK, regardless of if we became the
217 * primary instance or not. %FALSE is reserved for when something went
218 * seriously wrong (and @error will be set too, in that case).
220 * After a %TRUE return, impl->primary will be TRUE if we were
224 g_application_impl_attempt_primary (GApplicationImpl *impl,
225 GCancellable *cancellable,
228 const static GDBusInterfaceVTable vtable = {
229 g_application_impl_method_call,
234 if (org_gtk_Application == NULL)
236 GError *error = NULL;
239 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
240 if G_UNLIKELY (info == NULL)
241 g_error ("%s", error->message);
242 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
243 g_assert (org_gtk_Application != NULL);
244 g_dbus_interface_info_ref (org_gtk_Application);
245 g_dbus_node_info_unref (info);
248 /* We could possibly have been D-Bus activated as a result of incoming
249 * requests on either the application or actiongroup interfaces.
250 * Because of how GDBus dispatches messages, we need to ensure that
251 * both of those things are registered before we attempt to request
254 * The action group need not be populated yet, as long as it happens
255 * before we return to the mainloop. The reason for that is because
256 * GDBus does the check to make sure the object exists from the worker
257 * thread but doesn't actually dispatch the action invocation until we
258 * hit the mainloop in this thread. There is also no danger of
259 * receiving 'activate' or 'open' signals until after 'startup' runs,
260 * for the same reason.
262 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
263 org_gtk_Application, &vtable, impl, NULL, error);
265 if (impl->object_id == 0)
268 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
269 impl->exported_actions, error);
271 if (impl->actions_id == 0)
274 if (impl->bus_name == NULL)
276 /* If this is a non-unique application then it is sufficient to
277 * have our object paths registered. We can return now.
279 * Note: non-unique applications always act as primary-instance.
281 impl->primary = TRUE;
285 /* If this is a unique application then we need to attempt to own
286 * the well-known name and fall back to remote mode (!is_primary)
287 * in the case that we can't do that.
289 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
290 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
291 "org.freedesktop.DBus", "RequestName",
292 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
293 0, -1, cancellable, error);
298 g_variant_get (reply, "(u)", &rval);
299 g_variant_unref (reply);
301 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
302 impl->primary = (rval != 3);
307 /* Stop doing the things that the primary instance does.
309 * This should be called if attempting to become the primary instance
310 * failed (in order to clean up any partial success) and should also
311 * be called when freeing the GApplication.
313 * It is safe to call this multiple times.
316 g_application_impl_stop_primary (GApplicationImpl *impl)
320 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
324 if (impl->actions_id)
326 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
327 impl->actions_id = 0;
330 if (impl->primary && impl->bus_name)
332 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
333 "/org/freedesktop/DBus", "org.freedesktop.DBus",
334 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
335 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
336 impl->primary = FALSE;
341 g_application_impl_destroy (GApplicationImpl *impl)
343 g_application_impl_stop_primary (impl);
345 if (impl->session_bus)
346 g_object_unref (impl->session_bus);
348 g_free (impl->object_path);
350 g_slice_free (GApplicationImpl, impl);
354 g_application_impl_register (GApplication *application,
356 GApplicationFlags flags,
357 GActionGroup *exported_actions,
358 GRemoteActionGroup **remote_actions,
359 GCancellable *cancellable,
362 GDBusActionGroup *actions;
363 GApplicationImpl *impl;
365 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
367 impl = g_slice_new0 (GApplicationImpl);
369 impl->app = application;
370 impl->exported_actions = exported_actions;
372 /* non-unique applications do not attempt to acquire a bus name */
373 if (~flags & G_APPLICATION_NON_UNIQUE)
374 impl->bus_name = appid;
376 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
378 if (impl->session_bus == NULL)
380 /* If we can't connect to the session bus, proceed as a normal
381 * non-unique application.
383 *remote_actions = NULL;
387 impl->object_path = application_path_from_appid (appid);
389 /* Only try to be the primary instance if
390 * G_APPLICATION_IS_LAUNCHER was not specified.
392 if (~flags & G_APPLICATION_IS_LAUNCHER)
394 if (!g_application_impl_attempt_primary (impl, cancellable, error))
396 g_application_impl_destroy (impl);
403 /* We didn't make it. Drop our service-side stuff. */
404 g_application_impl_stop_primary (impl);
406 if (flags & G_APPLICATION_IS_SERVICE)
408 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
409 "Unable to acquire bus name `%s'", appid);
410 g_application_impl_destroy (impl);
416 /* We are non-primary. Try to get the primary's list of actions.
417 * This also serves as a mechanism to ensure that the primary exists
418 * (ie: DBus service files installed correctly, etc).
420 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
421 if (!g_dbus_action_group_sync (actions, cancellable, error))
423 /* The primary appears not to exist. Fail the registration. */
424 g_application_impl_destroy (impl);
425 g_object_unref (actions);
430 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
436 g_application_impl_activate (GApplicationImpl *impl,
437 GVariant *platform_data)
439 g_dbus_connection_call (impl->session_bus,
442 "org.gtk.Application",
444 g_variant_new ("(@a{sv})", platform_data),
445 NULL, 0, -1, NULL, NULL, NULL);
449 g_application_impl_open (GApplicationImpl *impl,
453 GVariant *platform_data)
455 GVariantBuilder builder;
458 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
459 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
460 for (i = 0; i < n_files; i++)
462 gchar *uri = g_file_get_uri (files[i]);
463 g_variant_builder_add (&builder, "s", uri);
466 g_variant_builder_close (&builder);
467 g_variant_builder_add (&builder, "s", hint);
468 g_variant_builder_add_value (&builder, platform_data);
470 g_dbus_connection_call (impl->session_bus,
473 "org.gtk.Application",
475 g_variant_builder_end (&builder),
476 NULL, 0, -1, NULL, NULL, NULL);
480 g_application_impl_cmdline_method_call (GDBusConnection *connection,
482 const gchar *object_path,
483 const gchar *interface_name,
484 const gchar *method_name,
485 GVariant *parameters,
486 GDBusMethodInvocation *invocation,
489 const gchar *message;
491 g_variant_get_child (parameters, 0, "&s", &message);
493 if (strcmp (method_name, "Print") == 0)
494 g_print ("%s", message);
495 else if (strcmp (method_name, "PrintError") == 0)
496 g_printerr ("%s", message);
498 g_assert_not_reached ();
500 g_dbus_method_invocation_return_value (invocation, NULL);
510 g_application_impl_cmdline_done (GObject *source,
511 GAsyncResult *result,
514 CommandLineData *data = user_data;
515 GError *error = NULL;
518 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
523 g_variant_get (reply, "(i)", &data->status);
524 g_variant_unref (reply);
529 g_printerr ("%s\n", error->message);
530 g_error_free (error);
534 g_main_loop_quit (data->loop);
538 g_application_impl_command_line (GApplicationImpl *impl,
540 GVariant *platform_data)
542 const static GDBusInterfaceVTable vtable = {
543 g_application_impl_cmdline_method_call
545 const gchar *object_path = "/org/gtk/Application/CommandLine";
546 GMainContext *context;
547 CommandLineData data;
550 context = g_main_context_new ();
551 data.loop = g_main_loop_new (context, FALSE);
552 g_main_context_push_thread_default (context);
554 if (org_gtk_private_CommandLine == NULL)
556 GError *error = NULL;
559 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
560 if G_UNLIKELY (info == NULL)
561 g_error ("%s", error->message);
562 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
563 g_assert (org_gtk_private_CommandLine != NULL);
564 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
565 g_dbus_node_info_unref (info);
568 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
569 org_gtk_private_CommandLine,
570 &vtable, &data, NULL, NULL);
571 /* In theory we should try other paths... */
572 g_assert (object_id != 0);
574 g_dbus_connection_call (impl->session_bus,
577 "org.gtk.Application",
579 g_variant_new ("(o^aay@a{sv})", object_path,
580 arguments, platform_data),
581 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
582 g_application_impl_cmdline_done, &data);
584 g_main_loop_run (data.loop);
586 g_main_context_pop_thread_default (context);
587 g_main_context_unref (context);
588 g_main_loop_unref (data.loop);
594 g_application_impl_flush (GApplicationImpl *impl)
596 if (impl->session_bus)
597 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
601 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
603 return impl->session_bus;
607 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
609 return impl->object_path;
612 /* GDBusCommandLine implementation {{{1 */
614 typedef GApplicationCommandLineClass GDBusCommandLineClass;
615 static GType g_dbus_command_line_get_type (void);
618 GApplicationCommandLine parent_instance;
619 GDBusMethodInvocation *invocation;
621 GDBusConnection *connection;
622 const gchar *bus_name;
623 const gchar *object_path;
627 G_DEFINE_TYPE (GDBusCommandLine,
629 G_TYPE_APPLICATION_COMMAND_LINE)
632 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
633 const gchar *message)
635 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
637 g_dbus_connection_call (gdbcl->connection,
640 "org.gtk.private.CommandLine", "Print",
641 g_variant_new ("(s)", message),
642 NULL, 0, -1, NULL, NULL, NULL);
646 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
647 const gchar *message)
649 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
651 g_dbus_connection_call (gdbcl->connection,
654 "org.gtk.private.CommandLine", "PrintError",
655 g_variant_new ("(s)", message),
656 NULL, 0, -1, NULL, NULL, NULL);
660 g_dbus_command_line_finalize (GObject *object)
662 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
663 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
666 status = g_application_command_line_get_exit_status (cmdline);
668 g_dbus_method_invocation_return_value (gdbcl->invocation,
669 g_variant_new ("(i)", status));
670 g_object_unref (gdbcl->invocation);
672 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
677 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
682 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
684 GObjectClass *object_class = G_OBJECT_CLASS (class);
686 object_class->finalize = g_dbus_command_line_finalize;
687 class->printerr_literal = g_dbus_command_line_printerr_literal;
688 class->print_literal = g_dbus_command_line_print_literal;
691 static GApplicationCommandLine *
692 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
694 GDBusCommandLine *gdbcl;
697 args = g_dbus_method_invocation_get_parameters (invocation);
699 gdbcl = g_object_new (g_dbus_command_line_get_type (),
700 "arguments", g_variant_get_child_value (args, 1),
701 "platform-data", g_variant_get_child_value (args, 2),
703 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
704 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
705 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
706 gdbcl->invocation = g_object_ref (invocation);
708 return G_APPLICATION_COMMAND_LINE (gdbcl);
713 /* vim:set foldmethod=marker: */