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 "gdbusactiongroup.h"
27 #include "gapplication.h"
29 #include "gdbusconnection.h"
30 #include "gdbusintrospection.h"
31 #include "gdbuserror.h"
32 #include "gmenuexporter.h"
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
40 /* DBus Interface definition {{{1 */
41 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
43 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
44 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
46 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
47 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
49 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
50 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
51 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
53 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
54 static const GDBusArgInfo *activate_out[] = { NULL };
56 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
57 static const GDBusArgInfo *open_out[] = { NULL };
59 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
60 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
62 static const GDBusMethodInfo activate_method = {
63 -1, (gchar *) "Activate",
64 (GDBusArgInfo **) activate_in,
65 (GDBusArgInfo **) activate_out
68 static const GDBusMethodInfo open_method = {
70 (GDBusArgInfo **) open_in,
71 (GDBusArgInfo **) open_out
74 static const GDBusMethodInfo command_line_method = {
75 -1, (gchar *) "CommandLine",
76 (GDBusArgInfo **) cmdline_in,
77 (GDBusArgInfo **) cmdline_out
80 static const GDBusMethodInfo *application_methods[] = {
81 &activate_method, &open_method, &command_line_method, NULL
84 const GDBusInterfaceInfo org_gtk_Application = {
85 -1, (gchar *) "org.gtk.Application",
86 (GDBusMethodInfo **) application_methods
89 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
90 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
91 static const GDBusArgInfo *print_out[] = { NULL };
93 static const GDBusMethodInfo stdout_method = {
94 -1, (gchar *) "Print",
95 (GDBusArgInfo **) print_in,
96 (GDBusArgInfo **) print_out
99 static const GDBusMethodInfo stderr_method = {
100 -1, (gchar *) "PrintError",
101 (GDBusArgInfo **) print_in,
102 (GDBusArgInfo **) print_out
105 static const GDBusMethodInfo *cmdline_methods[] = {
106 &stdout_method, &stderr_method, NULL
109 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
110 -1, (gchar *) "org.gtk.private.CommandLine",
111 (GDBusMethodInfo **) cmdline_methods
114 /* GApplication implementation {{{1 */
115 struct _GApplicationImpl
117 GDBusConnection *session_bus;
118 const gchar *bus_name;
121 gboolean actions_exported;
122 gboolean menu_exported;
127 static GApplicationCommandLine *
128 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
132 g_application_impl_method_call (GDBusConnection *connection,
134 const gchar *object_path,
135 const gchar *interface_name,
136 const gchar *method_name,
137 GVariant *parameters,
138 GDBusMethodInvocation *invocation,
141 GApplicationImpl *impl = user_data;
142 GApplicationClass *class;
144 class = G_APPLICATION_GET_CLASS (impl->app);
146 if (strcmp (method_name, "Activate") == 0)
148 GVariant *platform_data;
150 g_variant_get (parameters, "(@a{sv})", &platform_data);
151 class->before_emit (impl->app, platform_data);
152 g_signal_emit_by_name (impl->app, "activate");
153 class->after_emit (impl->app, platform_data);
154 g_variant_unref (platform_data);
156 g_dbus_method_invocation_return_value (invocation, NULL);
159 else if (strcmp (method_name, "Open") == 0)
161 GVariant *platform_data;
167 g_variant_get (parameters, "(@ass@a{sv})",
168 &array, &hint, &platform_data);
170 n = g_variant_n_children (array);
171 files = g_new (GFile *, n + 1);
173 for (i = 0; i < n; i++)
177 g_variant_get_child (array, i, "&s", &uri);
178 files[i] = g_file_new_for_uri (uri);
180 g_variant_unref (array);
183 class->before_emit (impl->app, platform_data);
184 g_signal_emit_by_name (impl->app, "open", files, n, hint);
185 class->after_emit (impl->app, platform_data);
187 g_variant_unref (platform_data);
189 for (i = 0; i < n; i++)
190 g_object_unref (files[i]);
193 g_dbus_method_invocation_return_value (invocation, NULL);
196 else if (strcmp (method_name, "CommandLine") == 0)
198 GApplicationCommandLine *cmdline;
199 GVariant *platform_data;
202 cmdline = g_dbus_command_line_new (invocation);
203 platform_data = g_variant_get_child_value (parameters, 2);
204 class->before_emit (impl->app, platform_data);
205 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
206 g_application_command_line_set_exit_status (cmdline, status);
207 class->after_emit (impl->app, platform_data);
208 g_variant_unref (platform_data);
209 g_object_unref (cmdline);
212 g_assert_not_reached ();
216 application_path_from_appid (const gchar *appid)
218 gchar *appid_path, *iter;
220 appid_path = g_strconcat ("/", appid, NULL);
221 for (iter = appid_path; *iter; iter++)
234 g_application_impl_destroy (GApplicationImpl *impl)
236 if (impl->session_bus)
239 g_dbus_connection_unregister_object (impl->session_bus,
241 if (impl->actions_exported)
242 g_action_group_dbus_export_stop (impl->app);
243 if (impl->menu_exported)
244 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
246 g_dbus_connection_call (impl->session_bus,
247 "org.freedesktop.DBus",
248 "/org/freedesktop/DBus",
249 "org.freedesktop.DBus",
251 g_variant_new ("(s)",
254 G_DBUS_CALL_FLAGS_NONE,
255 -1, NULL, NULL, NULL);
257 g_object_unref (impl->session_bus);
258 g_free (impl->object_path);
262 g_assert (impl->object_path == NULL);
263 g_assert (impl->object_id == 0);
266 g_slice_free (GApplicationImpl, impl);
270 g_application_impl_register (GApplication *application,
272 GApplicationFlags flags,
273 GActionGroup **remote_actions,
274 GCancellable *cancellable,
277 const static GDBusInterfaceVTable vtable = {
278 g_application_impl_method_call
280 GDBusActionGroup *actions;
281 GApplicationImpl *impl;
285 impl = g_slice_new0 (GApplicationImpl);
287 impl->app = application;
288 impl->bus_name = appid;
290 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
292 if (impl->session_bus == NULL)
294 /* If we can't connect to the session bus, proceed as a normal
295 * non-unique application.
297 *remote_actions = NULL;
301 impl->object_path = application_path_from_appid (appid);
303 /* Only try to be the primary instance if
304 * G_APPLICATION_IS_LAUNCHER was not specified.
306 if (~flags & G_APPLICATION_IS_LAUNCHER)
308 /* Attempt to become primary instance. */
310 g_dbus_connection_register_object (impl->session_bus,
312 (GDBusInterfaceInfo *)
313 &org_gtk_Application,
314 &vtable, impl, NULL, error);
316 if (impl->object_id == 0)
318 g_object_unref (impl->session_bus);
319 g_free (impl->object_path);
320 impl->session_bus = NULL;
321 impl->object_path = NULL;
323 g_slice_free (GApplicationImpl, impl);
327 if (!g_action_group_dbus_export_start (impl->session_bus,
331 g_dbus_connection_unregister_object (impl->session_bus,
334 g_object_unref (impl->session_bus);
335 g_free (impl->object_path);
336 impl->session_bus = NULL;
337 impl->object_path = NULL;
339 g_slice_free (GApplicationImpl, impl);
342 impl->actions_exported = TRUE;
344 if (g_application_get_app_menu (impl->app))
346 if (!g_menu_model_dbus_export_start (impl->session_bus,
348 g_application_get_app_menu (impl->app),
351 g_action_group_dbus_export_stop (impl->app);
352 impl->actions_exported = FALSE;
354 g_dbus_connection_unregister_object (impl->session_bus,
357 g_object_unref (impl->session_bus);
358 g_free (impl->object_path);
359 impl->session_bus = NULL;
360 impl->object_path = NULL;
362 g_slice_free (GApplicationImpl, impl);
365 impl->menu_exported = TRUE;
368 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
369 reply = g_dbus_connection_call_sync (impl->session_bus,
370 "org.freedesktop.DBus",
371 "/org/freedesktop/DBus",
372 "org.freedesktop.DBus",
374 g_variant_new ("(su)",
377 G_VARIANT_TYPE ("(u)"),
378 0, -1, cancellable, error);
382 g_dbus_connection_unregister_object (impl->session_bus,
386 g_action_group_dbus_export_stop (impl->app);
387 impl->actions_exported = FALSE;
389 if (impl->menu_exported)
391 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
392 impl->menu_exported = FALSE;
395 g_object_unref (impl->session_bus);
396 g_free (impl->object_path);
397 impl->session_bus = NULL;
398 impl->object_path = NULL;
400 g_slice_free (GApplicationImpl, impl);
404 g_variant_get (reply, "(u)", &rval);
405 g_variant_unref (reply);
407 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
410 /* We are the primary instance. */
411 g_dbus_connection_emit_signal (impl->session_bus,
414 "org.gtk.Application",
416 g_variant_new ("(s)",
419 *remote_actions = NULL;
423 /* We didn't make it. Drop our service-side stuff. */
424 g_dbus_connection_unregister_object (impl->session_bus,
427 g_action_group_dbus_export_stop (impl->app);
428 impl->actions_exported = FALSE;
429 if (impl->menu_exported)
431 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
432 impl->menu_exported = FALSE;
435 if (flags & G_APPLICATION_IS_SERVICE)
437 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
438 "Unable to acquire bus name `%s'", appid);
439 g_object_unref (impl->session_bus);
440 g_free (impl->object_path);
442 g_slice_free (GApplicationImpl, impl);
447 /* We are non-primary. Try to get the primary's list of actions.
448 * This also serves as a mechanism to ensure that the primary exists
449 * (ie: DBus service files installed correctly, etc).
451 actions = g_dbus_action_group_new_sync (impl->session_bus, impl->bus_name, impl->object_path,
452 G_DBUS_ACTION_GROUP_FLAGS_NONE, NULL, error);
456 /* The primary appears not to exist. Fail the registration. */
457 g_object_unref (impl->session_bus);
458 g_free (impl->object_path);
459 impl->session_bus = NULL;
460 impl->object_path = NULL;
462 g_slice_free (GApplicationImpl, impl);
466 *remote_actions = G_ACTION_GROUP (actions);
472 g_application_impl_activate (GApplicationImpl *impl,
473 GVariant *platform_data)
475 g_dbus_connection_call (impl->session_bus,
478 "org.gtk.Application",
480 g_variant_new ("(@a{sv})", platform_data),
481 NULL, 0, -1, NULL, NULL, NULL);
485 g_application_impl_open (GApplicationImpl *impl,
489 GVariant *platform_data)
491 GVariantBuilder builder;
494 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
495 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
496 for (i = 0; i < n_files; i++)
498 gchar *uri = g_file_get_uri (files[i]);
499 g_variant_builder_add (&builder, "s", uri);
502 g_variant_builder_close (&builder);
503 g_variant_builder_add (&builder, "s", hint);
504 g_variant_builder_add_value (&builder, platform_data);
506 g_dbus_connection_call (impl->session_bus,
509 "org.gtk.Application",
511 g_variant_builder_end (&builder),
512 NULL, 0, -1, NULL, NULL, NULL);
516 g_application_impl_cmdline_method_call (GDBusConnection *connection,
518 const gchar *object_path,
519 const gchar *interface_name,
520 const gchar *method_name,
521 GVariant *parameters,
522 GDBusMethodInvocation *invocation,
525 const gchar *message;
527 g_variant_get_child (parameters, 0, "&s", &message);
529 if (strcmp (method_name, "Print") == 0)
530 g_print ("%s", message);
531 else if (strcmp (method_name, "PrintError") == 0)
532 g_printerr ("%s", message);
534 g_assert_not_reached ();
536 g_dbus_method_invocation_return_value (invocation, NULL);
546 g_application_impl_cmdline_done (GObject *source,
547 GAsyncResult *result,
550 CommandLineData *data = user_data;
551 GError *error = NULL;
554 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
559 g_variant_get (reply, "(i)", &data->status);
560 g_variant_unref (reply);
565 g_printerr ("%s\n", error->message);
566 g_error_free (error);
570 g_main_loop_quit (data->loop);
574 g_application_impl_command_line (GApplicationImpl *impl,
576 GVariant *platform_data)
578 const static GDBusInterfaceVTable vtable = {
579 g_application_impl_cmdline_method_call
581 const gchar *object_path = "/org/gtk/Application/CommandLine";
582 GMainContext *context;
583 CommandLineData data;
586 context = g_main_context_new ();
587 data.loop = g_main_loop_new (context, FALSE);
588 g_main_context_push_thread_default (context);
590 object_id = g_dbus_connection_register_object (impl->session_bus,
592 (GDBusInterfaceInfo *)
593 &org_gtk_private_Cmdline,
594 &vtable, &data, NULL, NULL);
595 /* In theory we should try other paths... */
596 g_assert (object_id != 0);
598 g_dbus_connection_call (impl->session_bus,
601 "org.gtk.Application",
603 g_variant_new ("(o^aay@a{sv})", object_path,
604 arguments, platform_data),
605 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
606 g_application_impl_cmdline_done, &data);
608 g_main_loop_run (data.loop);
610 g_main_context_pop_thread_default (context);
611 g_main_context_unref (context);
612 g_main_loop_unref (data.loop);
618 g_application_impl_flush (GApplicationImpl *impl)
620 if (impl->session_bus)
621 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
625 /* GDBusCommandLine implementation {{{1 */
627 typedef GApplicationCommandLineClass GDBusCommandLineClass;
628 static GType g_dbus_command_line_get_type (void);
631 GApplicationCommandLine parent_instance;
632 GDBusMethodInvocation *invocation;
634 GDBusConnection *connection;
635 const gchar *bus_name;
636 const gchar *object_path;
640 G_DEFINE_TYPE (GDBusCommandLine,
642 G_TYPE_APPLICATION_COMMAND_LINE)
645 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
646 const gchar *message)
648 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
650 g_dbus_connection_call (gdbcl->connection,
653 "org.gtk.private.CommandLine", "Print",
654 g_variant_new ("(s)", message),
655 NULL, 0, -1, NULL, NULL, NULL);
659 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
660 const gchar *message)
662 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
664 g_dbus_connection_call (gdbcl->connection,
667 "org.gtk.private.CommandLine", "PrintError",
668 g_variant_new ("(s)", message),
669 NULL, 0, -1, NULL, NULL, NULL);
673 g_dbus_command_line_finalize (GObject *object)
675 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
676 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
679 status = g_application_command_line_get_exit_status (cmdline);
681 g_dbus_method_invocation_return_value (gdbcl->invocation,
682 g_variant_new ("(i)", status));
683 g_object_unref (gdbcl->invocation);
685 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
690 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
695 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
697 GObjectClass *object_class = G_OBJECT_CLASS (class);
699 object_class->finalize = g_dbus_command_line_finalize;
700 class->printerr_literal = g_dbus_command_line_printerr_literal;
701 class->print_literal = g_dbus_command_line_print_literal;
704 static GApplicationCommandLine *
705 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
707 GDBusCommandLine *gdbcl;
710 args = g_dbus_method_invocation_get_parameters (invocation);
712 gdbcl = g_object_new (g_dbus_command_line_get_type (),
713 "arguments", g_variant_get_child_value (args, 1),
714 "platform-data", g_variant_get_child_value (args, 2),
716 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
717 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
718 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
719 gdbcl->invocation = g_object_ref (invocation);
721 return G_APPLICATION_COMMAND_LINE (gdbcl);
726 /* vim:set foldmethod=marker: */