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 "gapplication.h"
28 #include "gdbusconnection.h"
29 #include "gdbusintrospection.h"
30 #include "gdbuserror.h"
35 #include "gapplicationcommandline.h"
36 #include "gdbusmethodinvocation.h"
38 /* DBus Interface definition {{{1 */
39 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
41 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
42 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
44 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
45 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
47 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
48 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
49 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
51 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
52 static const GDBusArgInfo *activate_out[] = { NULL };
54 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
55 static const GDBusArgInfo *open_out[] = { NULL };
57 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
58 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
60 static const GDBusMethodInfo activate_method = {
61 -1, (gchar *) "Activate",
62 (GDBusArgInfo **) activate_in,
63 (GDBusArgInfo **) activate_out
66 static const GDBusMethodInfo open_method = {
68 (GDBusArgInfo **) open_in,
69 (GDBusArgInfo **) open_out
72 static const GDBusMethodInfo command_line_method = {
73 -1, (gchar *) "CommandLine",
74 (GDBusArgInfo **) cmdline_in,
75 (GDBusArgInfo **) cmdline_out
78 static const GDBusMethodInfo *application_methods[] = {
79 &activate_method, &open_method, &command_line_method, NULL
82 const GDBusInterfaceInfo org_gtk_Application = {
83 -1, (gchar *) "org.gtk.Application",
84 (GDBusMethodInfo **) application_methods
87 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
88 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
89 static const GDBusArgInfo *print_out[] = { NULL };
91 static const GDBusMethodInfo stdout_method = {
92 -1, (gchar *) "Print",
93 (GDBusArgInfo **) print_in,
94 (GDBusArgInfo **) print_out
97 static const GDBusMethodInfo stderr_method = {
98 -1, (gchar *) "PrintError",
99 (GDBusArgInfo **) print_in,
100 (GDBusArgInfo **) print_out
103 static const GDBusMethodInfo *cmdline_methods[] = {
104 &stdout_method, &stderr_method, NULL
107 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
108 -1, (gchar *) "org.gtk.private.CommandLine",
109 (GDBusMethodInfo **) cmdline_methods
112 /* GApplication implementation {{{1 */
113 struct _GApplicationImpl
115 GDBusConnection *session_bus;
116 const gchar *bus_name;
119 gboolean actions_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_exporter_stop (impl->app);
244 g_dbus_connection_call (impl->session_bus,
245 "org.freedesktop.DBus",
246 "/org/freedesktop/DBus",
247 "org.freedesktop.DBus",
249 g_variant_new ("(s)",
252 G_DBUS_CALL_FLAGS_NONE,
253 -1, NULL, NULL, NULL);
255 g_object_unref (impl->session_bus);
256 g_free (impl->object_path);
260 g_assert (impl->object_path == NULL);
261 g_assert (impl->object_id == 0);
264 g_slice_free (GApplicationImpl, impl);
268 unwrap_fake_maybe (GVariant **value)
272 if (g_variant_n_children (*value))
273 g_variant_get_child (*value, 0, "v", &tmp);
277 g_variant_unref (*value);
281 static RemoteActionInfo *
282 remote_action_info_new_from_iter (GVariantIter *iter)
284 RemoteActionInfo *info;
285 GVariant *param_type;
290 if (!g_variant_iter_next (iter, "(s@avb@av)", &name,
291 ¶m_type, &enabled, &state))
294 unwrap_fake_maybe (¶m_type);
295 unwrap_fake_maybe (&state);
297 info = g_slice_new (RemoteActionInfo);
299 info->enabled = enabled;
302 if (param_type != NULL)
304 info->parameter_type = g_variant_type_copy (
305 g_variant_type_element (
306 g_variant_get_type (param_type)));
307 g_variant_unref (param_type);
310 info->parameter_type = NULL;
316 g_application_impl_action_signal (GDBusConnection *connection,
317 const gchar *sender_name,
318 const gchar *object_path,
319 const gchar *interface_name,
320 const gchar *signal_name,
321 GVariant *parameters,
324 GApplicationImpl *impl = user_data;
325 GActionGroup *action_group;
327 action_group = G_ACTION_GROUP (impl->app);
329 if (strcmp (signal_name, "Added") == 0 &&
330 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(savbav))")))
332 RemoteActionInfo *info;
335 g_variant_get_child (parameters, 0, "a(savbav)", &iter);
337 while ((info = remote_action_info_new_from_iter (iter)))
339 g_hash_table_replace (impl->actions, info->name, info);
340 g_action_group_action_added (action_group, info->name);
343 g_variant_iter_free (iter);
346 else if (strcmp (signal_name, "Removed") == 0 &&
347 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(as)")))
352 g_variant_get_child (parameters, 0, "as", &iter);
353 while (g_variant_iter_next (iter, "&s", &name))
354 if (g_hash_table_remove (impl->actions, name))
355 g_action_group_action_removed (action_group, name);
356 g_variant_iter_free (iter);
359 else if (strcmp (signal_name, "EnabledChanged") == 0 &&
360 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sb)")))
362 RemoteActionInfo *info;
366 g_variant_get (parameters, "(&sb)", &name, &enabled);
367 info = g_hash_table_lookup (impl->actions, name);
369 if (info && enabled != info->enabled)
371 info->enabled = enabled;
372 g_action_group_action_enabled_changed (action_group,
378 else if (strcmp (signal_name, "StateChanged") == 0 &&
379 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sv)")))
381 RemoteActionInfo *info;
385 g_variant_get (parameters, "(&sv)", &name, &state);
386 info = g_hash_table_lookup (impl->actions, name);
388 if (info && info->state &&
389 g_variant_is_of_type (state, g_variant_get_type (info->state)) &&
390 !g_variant_equal (state, info->state))
392 g_variant_unref (info->state);
393 info->state = g_variant_ref (state);
394 g_action_group_action_state_changed (action_group,
398 g_variant_unref (state);
403 g_application_impl_register (GApplication *application,
405 GApplicationFlags flags,
406 GHashTable **remote_actions,
407 GCancellable *cancellable,
410 const static GDBusInterfaceVTable vtable = {
411 g_application_impl_method_call
413 GApplicationImpl *impl;
417 impl = g_slice_new0 (GApplicationImpl);
419 impl->app = application;
420 impl->bus_name = appid;
422 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
424 if (impl->session_bus == NULL)
426 /* If we can't connect to the session bus, proceed as a normal
427 * non-unique application.
429 *remote_actions = NULL;
433 impl->object_path = application_path_from_appid (appid);
435 /* Only try to be the primary instance if
436 * G_APPLICATION_IS_LAUNCHER was not specified.
438 if (~flags & G_APPLICATION_IS_LAUNCHER)
440 /* Attempt to become primary instance. */
442 g_dbus_connection_register_object (impl->session_bus,
444 (GDBusInterfaceInfo *)
445 &org_gtk_Application,
446 &vtable, impl, NULL, error);
448 if (impl->object_id == 0)
450 g_object_unref (impl->session_bus);
451 g_free (impl->object_path);
452 impl->session_bus = NULL;
453 impl->object_path = NULL;
455 g_slice_free (GApplicationImpl, impl);
459 if (!g_action_group_exporter_export (impl->session_bus,
463 g_dbus_connection_unregister_object (impl->session_bus,
466 g_object_unref (impl->session_bus);
467 g_free (impl->object_path);
468 impl->session_bus = NULL;
469 impl->object_path = NULL;
471 g_slice_free (GApplicationImpl, impl);
474 impl->actions_exported = TRUE;
476 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
477 reply = g_dbus_connection_call_sync (impl->session_bus,
478 "org.freedesktop.DBus",
479 "/org/freedesktop/DBus",
480 "org.freedesktop.DBus",
482 g_variant_new ("(su)",
485 G_VARIANT_TYPE ("(u)"),
486 0, -1, cancellable, error);
490 g_dbus_connection_unregister_object (impl->session_bus,
494 g_action_group_exporter_stop (impl->app);
495 impl->actions_exported = FALSE;
497 g_object_unref (impl->session_bus);
498 g_free (impl->object_path);
499 impl->session_bus = NULL;
500 impl->object_path = NULL;
502 g_slice_free (GApplicationImpl, impl);
506 g_variant_get (reply, "(u)", &rval);
507 g_variant_unref (reply);
509 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
512 /* We are the primary instance. */
513 g_dbus_connection_emit_signal (impl->session_bus,
516 "org.gtk.Application",
518 g_variant_new ("(s)",
521 *remote_actions = NULL;
525 /* We didn't make it. Drop our service-side stuff. */
526 g_dbus_connection_unregister_object (impl->session_bus,
529 g_action_group_exporter_stop (impl->app);
530 impl->actions_exported = FALSE;
532 if (flags & G_APPLICATION_IS_SERVICE)
534 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
535 "Unable to acquire bus name `%s'", appid);
536 g_object_unref (impl->session_bus);
537 g_free (impl->object_path);
539 g_slice_free (GApplicationImpl, impl);
544 /* We are non-primary. Try to get the primary's list of actions.
545 * This also serves as a mechanism to ensure that the primary exists
546 * (ie: DBus service files installed correctly, etc).
549 g_dbus_connection_signal_subscribe (impl->session_bus, impl->bus_name,
550 "org.gtk.Actions", NULL,
551 impl->object_path, NULL,
552 G_DBUS_SIGNAL_FLAGS_NONE,
553 g_application_impl_action_signal,
556 reply = g_dbus_connection_call_sync (impl->session_bus, impl->bus_name,
557 impl->object_path, "org.gtk.Actions",
559 G_VARIANT_TYPE ("(a(savbav))"),
560 G_DBUS_CALL_FLAGS_NONE, -1,
565 /* The primary appears not to exist. Fail the registration. */
566 g_object_unref (impl->session_bus);
567 g_free (impl->object_path);
568 impl->session_bus = NULL;
569 impl->object_path = NULL;
571 g_slice_free (GApplicationImpl, impl);
575 /* Create and populate the hashtable */
577 RemoteActionInfo *info;
578 GVariant *descriptions;
581 *remote_actions = g_hash_table_new (g_str_hash, g_str_equal);
582 descriptions = g_variant_get_child_value (reply, 0);
583 g_variant_iter_init (&iter, descriptions);
585 while ((info = remote_action_info_new_from_iter (&iter)))
586 g_hash_table_insert (*remote_actions, info->name, info);
588 g_variant_unref (descriptions);
596 g_application_impl_activate (GApplicationImpl *impl,
597 GVariant *platform_data)
599 g_dbus_connection_call (impl->session_bus,
602 "org.gtk.Application",
604 g_variant_new ("(@a{sv})", platform_data),
605 NULL, 0, -1, NULL, NULL, NULL);
609 g_application_impl_open (GApplicationImpl *impl,
613 GVariant *platform_data)
615 GVariantBuilder builder;
618 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
619 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
620 for (i = 0; i < n_files; i++)
622 gchar *uri = g_file_get_uri (files[i]);
623 g_variant_builder_add (&builder, "s", uri);
626 g_variant_builder_close (&builder);
627 g_variant_builder_add (&builder, "s", hint);
628 g_variant_builder_add_value (&builder, platform_data);
630 g_dbus_connection_call (impl->session_bus,
633 "org.gtk.Application",
635 g_variant_builder_end (&builder),
636 NULL, 0, -1, NULL, NULL, NULL);
640 g_application_impl_cmdline_method_call (GDBusConnection *connection,
642 const gchar *object_path,
643 const gchar *interface_name,
644 const gchar *method_name,
645 GVariant *parameters,
646 GDBusMethodInvocation *invocation,
649 const gchar *message;
651 g_variant_get_child (parameters, 0, "&s", &message);
653 if (strcmp (method_name, "Print") == 0)
654 g_print ("%s", message);
655 else if (strcmp (method_name, "PrintError") == 0)
656 g_printerr ("%s", message);
658 g_assert_not_reached ();
660 g_dbus_method_invocation_return_value (invocation, NULL);
670 g_application_impl_cmdline_done (GObject *source,
671 GAsyncResult *result,
674 CommandLineData *data = user_data;
675 GError *error = NULL;
678 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
683 g_variant_get (reply, "(i)", &data->status);
684 g_variant_unref (reply);
689 g_printerr ("%s\n", error->message);
690 g_error_free (error);
694 g_main_loop_quit (data->loop);
698 g_application_impl_command_line (GApplicationImpl *impl,
700 GVariant *platform_data)
702 const static GDBusInterfaceVTable vtable = {
703 g_application_impl_cmdline_method_call
705 const gchar *object_path = "/org/gtk/Application/CommandLine";
706 GMainContext *context;
707 CommandLineData data;
710 context = g_main_context_new ();
711 data.loop = g_main_loop_new (context, FALSE);
712 g_main_context_push_thread_default (context);
714 object_id = g_dbus_connection_register_object (impl->session_bus,
716 (GDBusInterfaceInfo *)
717 &org_gtk_private_Cmdline,
718 &vtable, &data, NULL, NULL);
719 /* In theory we should try other paths... */
720 g_assert (object_id != 0);
722 g_dbus_connection_call (impl->session_bus,
725 "org.gtk.Application",
727 g_variant_new ("(o^aay@a{sv})", object_path,
728 arguments, platform_data),
729 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
730 g_application_impl_cmdline_done, &data);
732 g_main_loop_run (data.loop);
734 g_main_context_pop_thread_default (context);
735 g_main_context_unref (context);
736 g_main_loop_unref (data.loop);
742 g_application_impl_change_action_state (GApplicationImpl *impl,
743 const gchar *action_name,
745 GVariant *platform_data)
747 g_dbus_connection_call (impl->session_bus,
752 g_variant_new ("(sv@a{sv})", action_name,
753 value, platform_data),
754 NULL, 0, -1, NULL, NULL, NULL);
758 g_application_impl_activate_action (GApplicationImpl *impl,
759 const gchar *action_name,
761 GVariant *platform_data)
766 parameter = g_variant_new_variant (parameter);
768 param = g_variant_new_array (G_VARIANT_TYPE_VARIANT,
769 ¶meter, parameter != NULL);
771 g_dbus_connection_call (impl->session_bus,
776 g_variant_new ("(s@av@a{sv})", action_name,
777 param, platform_data),
778 NULL, 0, -1, NULL, NULL, NULL);
782 g_application_impl_flush (GApplicationImpl *impl)
784 if (impl->session_bus)
785 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
789 /* GDBusCommandLine implementation {{{1 */
791 typedef GApplicationCommandLineClass GDBusCommandLineClass;
792 static GType g_dbus_command_line_get_type (void);
795 GApplicationCommandLine parent_instance;
796 GDBusMethodInvocation *invocation;
798 GDBusConnection *connection;
799 const gchar *bus_name;
800 const gchar *object_path;
804 G_DEFINE_TYPE (GDBusCommandLine,
806 G_TYPE_APPLICATION_COMMAND_LINE)
809 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
810 const gchar *message)
812 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
814 g_dbus_connection_call (gdbcl->connection,
817 "org.gtk.private.CommandLine", "Print",
818 g_variant_new ("(s)", message),
819 NULL, 0, -1, NULL, NULL, NULL);
823 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
824 const gchar *message)
826 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
828 g_dbus_connection_call (gdbcl->connection,
831 "org.gtk.private.CommandLine", "PrintError",
832 g_variant_new ("(s)", message),
833 NULL, 0, -1, NULL, NULL, NULL);
837 g_dbus_command_line_finalize (GObject *object)
839 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
840 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
843 status = g_application_command_line_get_exit_status (cmdline);
845 g_dbus_method_invocation_return_value (gdbcl->invocation,
846 g_variant_new ("(i)", status));
847 g_object_unref (gdbcl->invocation);
849 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
854 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
859 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
861 GObjectClass *object_class = G_OBJECT_CLASS (class);
863 object_class->finalize = g_dbus_command_line_finalize;
864 class->printerr_literal = g_dbus_command_line_printerr_literal;
865 class->print_literal = g_dbus_command_line_print_literal;
868 static GApplicationCommandLine *
869 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
871 GDBusCommandLine *gdbcl;
874 args = g_dbus_method_invocation_get_parameters (invocation);
876 gdbcl = g_object_new (g_dbus_command_line_get_type (),
877 "arguments", g_variant_get_child_value (args, 1),
878 "platform-data", g_variant_get_child_value (args, 2),
880 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
881 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
882 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
883 gdbcl->invocation = g_object_ref (invocation);
885 return G_APPLICATION_COMMAND_LINE (gdbcl);
890 /* vim:set foldmethod=marker: */