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 remote_action_info_free (gpointer user_data)
270 RemoteActionInfo *info = user_data;
275 g_variant_unref (info->state);
277 if (info->parameter_type)
278 g_variant_type_free (info->parameter_type);
280 g_slice_free (RemoteActionInfo, info);
283 static RemoteActionInfo *
284 remote_action_info_new_from_iter (GVariantIter *iter)
286 RemoteActionInfo *info;
287 const gchar *param_str;
292 if (!g_variant_iter_next (iter, "{&s(bg@av)}", &name,
293 &enabled, ¶m_str, &state))
296 info = g_slice_new (RemoteActionInfo);
298 info->enabled = enabled;
300 if (g_variant_n_children (state))
301 g_variant_get_child (state, 0, "v", &info->state);
304 g_variant_unref (state);
307 info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
309 info->parameter_type = NULL;
315 g_application_impl_action_signal (GDBusConnection *connection,
316 const gchar *sender_name,
317 const gchar *object_path,
318 const gchar *interface_name,
319 const gchar *signal_name,
320 GVariant *parameters,
323 GApplicationImpl *impl = user_data;
324 GActionGroup *action_group;
326 action_group = G_ACTION_GROUP (impl->app);
328 if (g_str_equal (signal_name, "Changed") &&
329 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
336 g_variant_get_child (parameters, 0, "as", &iter);
337 while (g_variant_iter_next (iter, "&s", &name))
339 if (g_hash_table_lookup (impl->actions, name))
341 g_hash_table_remove (impl->actions, name);
342 g_action_group_action_removed (action_group, name);
345 g_variant_iter_free (iter);
354 g_variant_get_child (parameters, 1, "a{sb}", &iter);
355 while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
357 RemoteActionInfo *info;
359 info = g_hash_table_lookup (impl->actions, name);
361 if (info && info->enabled != enabled)
363 info->enabled = enabled;
364 g_action_group_action_enabled_changed (action_group,
368 g_variant_iter_free (iter);
377 g_variant_get_child (parameters, 2, "a{sv}", &iter);
378 while (g_variant_iter_next (iter, "{&sv}", &name, &state))
380 RemoteActionInfo *info;
382 info = g_hash_table_lookup (impl->actions, name);
384 if (info && info->state && !g_variant_equal (state, info->state) &&
385 g_variant_is_of_type (state, g_variant_get_type (info->state)))
387 g_variant_unref (info->state);
388 info->state = g_variant_ref (state);
390 g_action_group_action_state_changed (action_group, name, state);
393 g_variant_unref (state);
395 g_variant_iter_free (iter);
400 RemoteActionInfo *info;
403 g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
404 while ((info = remote_action_info_new_from_iter (iter)))
406 if (!g_hash_table_lookup (impl->actions, info->name))
407 g_hash_table_replace (impl->actions, info->name, info);
409 remote_action_info_free (info);
416 g_application_impl_register (GApplication *application,
418 GApplicationFlags flags,
419 GHashTable **remote_actions,
420 GCancellable *cancellable,
423 const static GDBusInterfaceVTable vtable = {
424 g_application_impl_method_call
426 GApplicationImpl *impl;
430 impl = g_slice_new0 (GApplicationImpl);
432 impl->app = application;
433 impl->bus_name = appid;
435 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
437 if (impl->session_bus == NULL)
439 /* If we can't connect to the session bus, proceed as a normal
440 * non-unique application.
442 *remote_actions = NULL;
446 impl->object_path = application_path_from_appid (appid);
448 /* Only try to be the primary instance if
449 * G_APPLICATION_IS_LAUNCHER was not specified.
451 if (~flags & G_APPLICATION_IS_LAUNCHER)
453 /* Attempt to become primary instance. */
455 g_dbus_connection_register_object (impl->session_bus,
457 (GDBusInterfaceInfo *)
458 &org_gtk_Application,
459 &vtable, impl, NULL, error);
461 if (impl->object_id == 0)
463 g_object_unref (impl->session_bus);
464 g_free (impl->object_path);
465 impl->session_bus = NULL;
466 impl->object_path = NULL;
468 g_slice_free (GApplicationImpl, impl);
472 if (!g_action_group_exporter_export (impl->session_bus,
476 g_dbus_connection_unregister_object (impl->session_bus,
479 g_object_unref (impl->session_bus);
480 g_free (impl->object_path);
481 impl->session_bus = NULL;
482 impl->object_path = NULL;
484 g_slice_free (GApplicationImpl, impl);
487 impl->actions_exported = TRUE;
489 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
490 reply = g_dbus_connection_call_sync (impl->session_bus,
491 "org.freedesktop.DBus",
492 "/org/freedesktop/DBus",
493 "org.freedesktop.DBus",
495 g_variant_new ("(su)",
498 G_VARIANT_TYPE ("(u)"),
499 0, -1, cancellable, error);
503 g_dbus_connection_unregister_object (impl->session_bus,
507 g_action_group_exporter_stop (impl->app);
508 impl->actions_exported = FALSE;
510 g_object_unref (impl->session_bus);
511 g_free (impl->object_path);
512 impl->session_bus = NULL;
513 impl->object_path = NULL;
515 g_slice_free (GApplicationImpl, impl);
519 g_variant_get (reply, "(u)", &rval);
520 g_variant_unref (reply);
522 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
525 /* We are the primary instance. */
526 g_dbus_connection_emit_signal (impl->session_bus,
529 "org.gtk.Application",
531 g_variant_new ("(s)",
534 *remote_actions = NULL;
538 /* We didn't make it. Drop our service-side stuff. */
539 g_dbus_connection_unregister_object (impl->session_bus,
542 g_action_group_exporter_stop (impl->app);
543 impl->actions_exported = FALSE;
545 if (flags & G_APPLICATION_IS_SERVICE)
547 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
548 "Unable to acquire bus name `%s'", appid);
549 g_object_unref (impl->session_bus);
550 g_free (impl->object_path);
552 g_slice_free (GApplicationImpl, impl);
557 /* We are non-primary. Try to get the primary's list of actions.
558 * This also serves as a mechanism to ensure that the primary exists
559 * (ie: DBus service files installed correctly, etc).
562 g_dbus_connection_signal_subscribe (impl->session_bus, impl->bus_name,
563 "org.gtk.Actions", NULL,
564 impl->object_path, NULL,
565 G_DBUS_SIGNAL_FLAGS_NONE,
566 g_application_impl_action_signal,
569 reply = g_dbus_connection_call_sync (impl->session_bus, impl->bus_name,
570 impl->object_path, "org.gtk.Actions",
572 G_VARIANT_TYPE ("(a{s(bgav)})"),
573 G_DBUS_CALL_FLAGS_NONE, -1,
578 /* The primary appears not to exist. Fail the registration. */
579 g_object_unref (impl->session_bus);
580 g_free (impl->object_path);
581 impl->session_bus = NULL;
582 impl->object_path = NULL;
584 g_slice_free (GApplicationImpl, impl);
588 /* Create and populate the hashtable */
590 RemoteActionInfo *info;
591 GVariant *descriptions;
594 impl->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
595 NULL, remote_action_info_free);
596 descriptions = g_variant_get_child_value (reply, 0);
597 g_variant_iter_init (&iter, descriptions);
599 while ((info = remote_action_info_new_from_iter (&iter)))
600 g_hash_table_replace (impl->actions, info->name, info);
602 g_variant_unref (descriptions);
604 *remote_actions = impl->actions;
611 g_application_impl_activate (GApplicationImpl *impl,
612 GVariant *platform_data)
614 g_dbus_connection_call (impl->session_bus,
617 "org.gtk.Application",
619 g_variant_new ("(@a{sv})", platform_data),
620 NULL, 0, -1, NULL, NULL, NULL);
624 g_application_impl_open (GApplicationImpl *impl,
628 GVariant *platform_data)
630 GVariantBuilder builder;
633 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
634 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
635 for (i = 0; i < n_files; i++)
637 gchar *uri = g_file_get_uri (files[i]);
638 g_variant_builder_add (&builder, "s", uri);
641 g_variant_builder_close (&builder);
642 g_variant_builder_add (&builder, "s", hint);
643 g_variant_builder_add_value (&builder, platform_data);
645 g_dbus_connection_call (impl->session_bus,
648 "org.gtk.Application",
650 g_variant_builder_end (&builder),
651 NULL, 0, -1, NULL, NULL, NULL);
655 g_application_impl_cmdline_method_call (GDBusConnection *connection,
657 const gchar *object_path,
658 const gchar *interface_name,
659 const gchar *method_name,
660 GVariant *parameters,
661 GDBusMethodInvocation *invocation,
664 const gchar *message;
666 g_variant_get_child (parameters, 0, "&s", &message);
668 if (strcmp (method_name, "Print") == 0)
669 g_print ("%s", message);
670 else if (strcmp (method_name, "PrintError") == 0)
671 g_printerr ("%s", message);
673 g_assert_not_reached ();
675 g_dbus_method_invocation_return_value (invocation, NULL);
685 g_application_impl_cmdline_done (GObject *source,
686 GAsyncResult *result,
689 CommandLineData *data = user_data;
690 GError *error = NULL;
693 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
698 g_variant_get (reply, "(i)", &data->status);
699 g_variant_unref (reply);
704 g_printerr ("%s\n", error->message);
705 g_error_free (error);
709 g_main_loop_quit (data->loop);
713 g_application_impl_command_line (GApplicationImpl *impl,
715 GVariant *platform_data)
717 const static GDBusInterfaceVTable vtable = {
718 g_application_impl_cmdline_method_call
720 const gchar *object_path = "/org/gtk/Application/CommandLine";
721 GMainContext *context;
722 CommandLineData data;
725 context = g_main_context_new ();
726 data.loop = g_main_loop_new (context, FALSE);
727 g_main_context_push_thread_default (context);
729 object_id = g_dbus_connection_register_object (impl->session_bus,
731 (GDBusInterfaceInfo *)
732 &org_gtk_private_Cmdline,
733 &vtable, &data, NULL, NULL);
734 /* In theory we should try other paths... */
735 g_assert (object_id != 0);
737 g_dbus_connection_call (impl->session_bus,
740 "org.gtk.Application",
742 g_variant_new ("(o^aay@a{sv})", object_path,
743 arguments, platform_data),
744 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
745 g_application_impl_cmdline_done, &data);
747 g_main_loop_run (data.loop);
749 g_main_context_pop_thread_default (context);
750 g_main_context_unref (context);
751 g_main_loop_unref (data.loop);
757 g_application_impl_change_action_state (GApplicationImpl *impl,
758 const gchar *action_name,
760 GVariant *platform_data)
762 g_dbus_connection_call (impl->session_bus,
767 g_variant_new ("(sv@a{sv})", action_name,
768 value, platform_data),
769 NULL, 0, -1, NULL, NULL, NULL);
773 g_application_impl_activate_action (GApplicationImpl *impl,
774 const gchar *action_name,
776 GVariant *platform_data)
781 parameter = g_variant_new_variant (parameter);
783 param = g_variant_new_array (G_VARIANT_TYPE_VARIANT,
784 ¶meter, parameter != NULL);
786 g_dbus_connection_call (impl->session_bus,
791 g_variant_new ("(s@av@a{sv})", action_name,
792 param, platform_data),
793 NULL, 0, -1, NULL, NULL, NULL);
797 g_application_impl_flush (GApplicationImpl *impl)
799 if (impl->session_bus)
800 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
804 /* GDBusCommandLine implementation {{{1 */
806 typedef GApplicationCommandLineClass GDBusCommandLineClass;
807 static GType g_dbus_command_line_get_type (void);
810 GApplicationCommandLine parent_instance;
811 GDBusMethodInvocation *invocation;
813 GDBusConnection *connection;
814 const gchar *bus_name;
815 const gchar *object_path;
819 G_DEFINE_TYPE (GDBusCommandLine,
821 G_TYPE_APPLICATION_COMMAND_LINE)
824 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
825 const gchar *message)
827 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
829 g_dbus_connection_call (gdbcl->connection,
832 "org.gtk.private.CommandLine", "Print",
833 g_variant_new ("(s)", message),
834 NULL, 0, -1, NULL, NULL, NULL);
838 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
839 const gchar *message)
841 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
843 g_dbus_connection_call (gdbcl->connection,
846 "org.gtk.private.CommandLine", "PrintError",
847 g_variant_new ("(s)", message),
848 NULL, 0, -1, NULL, NULL, NULL);
852 g_dbus_command_line_finalize (GObject *object)
854 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
855 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
858 status = g_application_command_line_get_exit_status (cmdline);
860 g_dbus_method_invocation_return_value (gdbcl->invocation,
861 g_variant_new ("(i)", status));
862 g_object_unref (gdbcl->invocation);
864 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
869 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
874 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
876 GObjectClass *object_class = G_OBJECT_CLASS (class);
878 object_class->finalize = g_dbus_command_line_finalize;
879 class->printerr_literal = g_dbus_command_line_printerr_literal;
880 class->print_literal = g_dbus_command_line_print_literal;
883 static GApplicationCommandLine *
884 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
886 GDBusCommandLine *gdbcl;
889 args = g_dbus_method_invocation_get_parameters (invocation);
891 gdbcl = g_object_new (g_dbus_command_line_get_type (),
892 "arguments", g_variant_get_child_value (args, 1),
893 "platform-data", g_variant_get_child_value (args, 2),
895 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
896 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
897 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
898 gdbcl->invocation = g_object_ref (invocation);
900 return G_APPLICATION_COMMAND_LINE (gdbcl);
905 /* vim:set foldmethod=marker: */