2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Ryan Lortie <desrt@desrt.ca>
23 #include "gactiongroupexporter.h"
25 #include "gdbusmethodinvocation.h"
26 #include "gremoteactiongroup.h"
27 #include "gdbusintrospection.h"
28 #include "gdbusconnection.h"
29 #include "gactiongroup.h"
30 #include "gdbuserror.h"
33 * SECTION:gactiongroupexporter
34 * @title: GActionGroup exporter
36 * @short_description: Export GActionGroups on D-Bus
37 * @see_also: #GActionGroup, #GDBusActionGroup
39 * These functions support exporting a #GActionGroup on D-Bus.
40 * The D-Bus interface that is used is a private implementation
43 * To access an exported #GActionGroup remotely, use
44 * g_dbus_action_group_get() to obtain a #GDBusActionGroup.
48 g_action_group_describe_action (GActionGroup *action_group,
51 const GVariantType *type;
52 GVariantBuilder builder;
56 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(bgav)"));
58 enabled = g_action_group_get_action_enabled (action_group, name);
59 g_variant_builder_add (&builder, "b", enabled);
61 if ((type = g_action_group_get_action_parameter_type (action_group, name)))
63 gchar *str = g_variant_type_dup_string (type);
64 g_variant_builder_add (&builder, "g", str);
68 g_variant_builder_add (&builder, "g", "");
70 g_variant_builder_open (&builder, G_VARIANT_TYPE ("av"));
71 if ((state = g_action_group_get_action_state (action_group, name)))
73 g_variant_builder_add (&builder, "v", state);
74 g_variant_unref (state);
76 g_variant_builder_close (&builder);
78 return g_variant_builder_end (&builder);
81 /* Using XML saves us dozens of relocations vs. using the introspection
82 * structure types. We only need to burn cycles and memory if we
83 * actually use the exporter -- not in every single app using GIO.
85 * It's also a lot easier to read. :)
87 * For documentation of this interface, see
88 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
90 const char org_gtk_Actions_xml[] =
92 " <interface name='org.gtk.Actions'>"
93 " <method name='List'>"
94 " <arg type='as' name='list' direction='out'/>"
96 " <method name='Describe'>"
97 " <arg type='s' name='action_name' direction='in'/>"
98 " <arg type='(bgav)' name='description' direction='out'/>"
100 " <method name='DescribeAll'>"
101 " <arg type='a{s(bgav)}' name='descriptions' direction='out'/>"
103 " <method name='Activate'>"
104 " <arg type='s' name='action_name' direction='in'/>"
105 " <arg type='av' name='parameter' direction='in'/>"
106 " <arg type='a{sv}' name='platform_data' direction='in'/>"
108 " <method name='SetState'>"
109 " <arg type='s' name='action_name' direction='in'/>"
110 " <arg type='v' name='value' direction='in'/>"
111 " <arg type='a{sv}' name='platform_data' direction='in'/>"
113 " <signal name='Changed'>"
114 " <arg type='as' name='removals'/>"
115 " <arg type='a{sb}' name='enable_changes'/>"
116 " <arg type='a{sv}' name='state_changes'/>"
117 " <arg type='a{s(bgav)}' name='additions'/>"
122 static GDBusInterfaceInfo *org_gtk_Actions;
126 GActionGroup *action_group;
127 GDBusConnection *connection;
128 GMainContext *context;
130 GHashTable *pending_changes;
131 GSource *pending_source;
132 } GActionGroupExporter;
134 #define ACTION_ADDED_EVENT (1u<<0)
135 #define ACTION_REMOVED_EVENT (1u<<1)
136 #define ACTION_STATE_CHANGED_EVENT (1u<<2)
137 #define ACTION_ENABLED_CHANGED_EVENT (1u<<3)
140 g_action_group_exporter_dispatch_events (gpointer user_data)
142 GActionGroupExporter *exporter = user_data;
143 GVariantBuilder removes;
144 GVariantBuilder enabled_changes;
145 GVariantBuilder state_changes;
146 GVariantBuilder adds;
151 g_variant_builder_init (&removes, G_VARIANT_TYPE_STRING_ARRAY);
152 g_variant_builder_init (&enabled_changes, G_VARIANT_TYPE ("a{sb}"));
153 g_variant_builder_init (&state_changes, G_VARIANT_TYPE ("a{sv}"));
154 g_variant_builder_init (&adds, G_VARIANT_TYPE ("a{s(bgav)}"));
156 g_hash_table_iter_init (&iter, exporter->pending_changes);
157 while (g_hash_table_iter_next (&iter, &key, &value))
159 guint events = GPOINTER_TO_INT (value);
160 const gchar *name = key;
162 /* Adds and removes are incompatible with enabled or state
163 * changes, but we must report at least one event.
165 g_assert (((events & (ACTION_ENABLED_CHANGED_EVENT | ACTION_STATE_CHANGED_EVENT)) == 0) !=
166 ((events & (ACTION_REMOVED_EVENT | ACTION_ADDED_EVENT)) == 0));
168 if (events & ACTION_REMOVED_EVENT)
169 g_variant_builder_add (&removes, "s", name);
171 if (events & ACTION_ENABLED_CHANGED_EVENT)
175 enabled = g_action_group_get_action_enabled (exporter->action_group, name);
176 g_variant_builder_add (&enabled_changes, "{sb}", name, enabled);
179 if (events & ACTION_STATE_CHANGED_EVENT)
183 state = g_action_group_get_action_state (exporter->action_group, name);
184 g_variant_builder_add (&state_changes, "{sv}", name, state);
185 g_variant_unref (state);
188 if (events & ACTION_ADDED_EVENT)
190 GVariant *description;
192 description = g_action_group_describe_action (exporter->action_group, name);
193 g_variant_builder_add (&adds, "{s@(bgav)}", name, description);
197 g_hash_table_remove_all (exporter->pending_changes);
199 g_dbus_connection_emit_signal (exporter->connection, NULL, exporter->object_path,
200 "org.gtk.Actions", "Changed",
201 g_variant_new ("(asa{sb}a{sv}a{s(bgav)})",
202 &removes, &enabled_changes,
203 &state_changes, &adds),
206 exporter->pending_source = NULL;
212 g_action_group_exporter_get_events (GActionGroupExporter *exporter,
215 return (gsize) g_hash_table_lookup (exporter->pending_changes, name);
219 g_action_group_exporter_set_events (GActionGroupExporter *exporter,
223 gboolean have_events;
227 g_hash_table_insert (exporter->pending_changes, g_strdup (name), GINT_TO_POINTER (events));
229 g_hash_table_remove (exporter->pending_changes, name);
231 have_events = g_hash_table_size (exporter->pending_changes) > 0;
232 is_queued = exporter->pending_source != NULL;
234 if (have_events && !is_queued)
238 source = g_idle_source_new ();
239 exporter->pending_source = source;
240 g_source_set_callback (source, g_action_group_exporter_dispatch_events, exporter, NULL);
241 g_source_set_name (source, "[gio] g_action_group_exporter_dispatch_events");
242 g_source_attach (source, exporter->context);
243 g_source_unref (source);
246 if (!have_events && is_queued)
248 g_source_destroy (exporter->pending_source);
249 exporter->pending_source = NULL;
254 g_action_group_exporter_action_added (GActionGroup *action_group,
255 const gchar *action_name,
258 GActionGroupExporter *exporter = user_data;
261 event_mask = g_action_group_exporter_get_events (exporter, action_name);
263 /* The action is new, so we should not have any pending
264 * enabled-changed or state-changed signals for it.
266 g_assert (~event_mask & (ACTION_STATE_CHANGED_EVENT |
267 ACTION_ENABLED_CHANGED_EVENT));
269 event_mask |= ACTION_ADDED_EVENT;
271 g_action_group_exporter_set_events (exporter, action_name, event_mask);
275 g_action_group_exporter_action_removed (GActionGroup *action_group,
276 const gchar *action_name,
279 GActionGroupExporter *exporter = user_data;
282 event_mask = g_action_group_exporter_get_events (exporter, action_name);
284 /* If the add event for this is still queued then just cancel it since
287 * If the event was freshly added, there should not have been any
288 * enabled or state changed events.
290 if (event_mask & ACTION_ADDED_EVENT)
292 g_assert (~event_mask & ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT));
293 event_mask &= ~ACTION_ADDED_EVENT;
296 /* Otherwise, queue a remove event. Drop any state or enabled changes
297 * that were queued before the remove. */
300 event_mask |= ACTION_REMOVED_EVENT;
301 event_mask &= ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT);
304 g_action_group_exporter_set_events (exporter, action_name, event_mask);
308 g_action_group_exporter_action_state_changed (GActionGroup *action_group,
309 const gchar *action_name,
313 GActionGroupExporter *exporter = user_data;
316 event_mask = g_action_group_exporter_get_events (exporter, action_name);
318 /* If it was removed, it must have been added back. Otherwise, why
319 * are we hearing about changes?
321 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
322 event_mask & ACTION_ADDED_EVENT);
324 /* If it is freshly added, don't also bother with the state change
325 * signal since the updated state will be sent as part of the pending
328 if (~event_mask & ACTION_ADDED_EVENT)
329 event_mask |= ACTION_STATE_CHANGED_EVENT;
331 g_action_group_exporter_set_events (exporter, action_name, event_mask);
335 g_action_group_exporter_action_enabled_changed (GActionGroup *action_group,
336 const gchar *action_name,
340 GActionGroupExporter *exporter = user_data;
343 event_mask = g_action_group_exporter_get_events (exporter, action_name);
345 /* Reasoning as above. */
346 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
347 event_mask & ACTION_ADDED_EVENT);
349 if (~event_mask & ACTION_ADDED_EVENT)
350 event_mask |= ACTION_ENABLED_CHANGED_EVENT;
352 g_action_group_exporter_set_events (exporter, action_name, event_mask);
356 org_gtk_Actions_method_call (GDBusConnection *connection,
358 const gchar *object_path,
359 const gchar *interface_name,
360 const gchar *method_name,
361 GVariant *parameters,
362 GDBusMethodInvocation *invocation,
365 GActionGroupExporter *exporter = user_data;
366 GVariant *result = NULL;
368 if (g_str_equal (method_name, "List"))
372 list = g_action_group_list_actions (exporter->action_group);
373 result = g_variant_new ("(^as)", list);
377 else if (g_str_equal (method_name, "Describe"))
382 g_variant_get (parameters, "(&s)", &name);
384 if (!g_action_group_has_action (exporter->action_group, name))
386 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
387 "The named action ('%s') does not exist.", name);
391 desc = g_action_group_describe_action (exporter->action_group, name);
392 result = g_variant_new ("(@(bgav))", desc);
395 else if (g_str_equal (method_name, "DescribeAll"))
397 GVariantBuilder builder;
401 list = g_action_group_list_actions (exporter->action_group);
402 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{s(bgav)}"));
403 for (i = 0; list[i]; i++)
405 const gchar *name = list[i];
406 GVariant *description;
408 description = g_action_group_describe_action (exporter->action_group, name);
409 g_variant_builder_add (&builder, "{s@(bgav)}", name, description);
411 result = g_variant_new ("(a{s(bgav)})", &builder);
415 else if (g_str_equal (method_name, "Activate"))
417 GVariant *parameter = NULL;
418 GVariant *platform_data;
422 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
423 g_variant_iter_next (iter, "v", ¶meter);
424 g_variant_iter_free (iter);
426 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
427 g_remote_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
428 name, parameter, platform_data);
430 g_action_group_activate_action (exporter->action_group, name, parameter);
433 g_variant_unref (parameter);
435 g_variant_unref (platform_data);
438 else if (g_str_equal (method_name, "SetState"))
440 GVariant *platform_data;
444 g_variant_get (parameters, "(&sv@a{sv})", &name, &state, &platform_data);
446 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
447 g_remote_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
448 name, state, platform_data);
450 g_action_group_change_action_state (exporter->action_group, name, state);
452 g_variant_unref (platform_data);
453 g_variant_unref (state);
457 g_assert_not_reached ();
459 g_dbus_method_invocation_return_value (invocation, result);
463 g_action_group_exporter_free (gpointer user_data)
465 GActionGroupExporter *exporter = user_data;
467 g_signal_handlers_disconnect_by_func (exporter->action_group,
468 g_action_group_exporter_action_added, exporter);
469 g_signal_handlers_disconnect_by_func (exporter->action_group,
470 g_action_group_exporter_action_enabled_changed, exporter);
471 g_signal_handlers_disconnect_by_func (exporter->action_group,
472 g_action_group_exporter_action_state_changed, exporter);
473 g_signal_handlers_disconnect_by_func (exporter->action_group,
474 g_action_group_exporter_action_removed, exporter);
476 g_hash_table_unref (exporter->pending_changes);
477 if (exporter->pending_source)
478 g_source_destroy (exporter->pending_source);
480 g_main_context_unref (exporter->context);
481 g_object_unref (exporter->connection);
482 g_object_unref (exporter->action_group);
483 g_free (exporter->object_path);
485 g_slice_free (GActionGroupExporter, exporter);
489 * g_dbus_connection_export_action_group:
490 * @connection: a #GDBusConnection
491 * @object_path: a D-Bus object path
492 * @action_group: a #GActionGroup
493 * @error: a pointer to a %NULL #GError, or %NULL
495 * Exports @action_group on @connection at @object_path.
497 * The implemented D-Bus API should be considered private. It is
498 * subject to change in the future.
500 * A given object path can only have one action group exported on it.
501 * If this constraint is violated, the export will fail and 0 will be
502 * returned (with @error set accordingly).
504 * You can unexport the action group using
505 * g_dbus_connection_unexport_action_group() with the return value of
508 * The thread default main context is taken at the time of this call.
509 * All incoming action activations and state change requests are
510 * reported from this context. Any changes on the action group that
511 * cause it to emit signals must also come from this same context.
512 * Since incoming action activations and state change requests are
513 * rather likely to cause changes on the action group, this effectively
514 * limits a given action group to being exported from only one main
517 * Returns: the ID of the export (never zero), or 0 in case of failure
522 g_dbus_connection_export_action_group (GDBusConnection *connection,
523 const gchar *object_path,
524 GActionGroup *action_group,
527 const GDBusInterfaceVTable vtable = {
528 org_gtk_Actions_method_call
530 GActionGroupExporter *exporter;
533 if G_UNLIKELY (org_gtk_Actions == NULL)
535 GError *error = NULL;
538 info = g_dbus_node_info_new_for_xml (org_gtk_Actions_xml, &error);
539 if G_UNLIKELY (info == NULL)
540 g_error ("%s", error->message);
541 org_gtk_Actions = g_dbus_node_info_lookup_interface (info, "org.gtk.Actions");
542 g_assert (org_gtk_Actions != NULL);
543 g_dbus_interface_info_ref (org_gtk_Actions);
544 g_dbus_node_info_unref (info);
547 exporter = g_slice_new (GActionGroupExporter);
548 id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
549 exporter, g_action_group_exporter_free, error);
553 g_slice_free (GActionGroupExporter, exporter);
557 exporter->context = g_main_context_ref_thread_default ();
558 exporter->pending_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
559 exporter->pending_source = NULL;
560 exporter->action_group = g_object_ref (action_group);
561 exporter->connection = g_object_ref (connection);
562 exporter->object_path = g_strdup (object_path);
564 g_signal_connect (action_group, "action-added",
565 G_CALLBACK (g_action_group_exporter_action_added), exporter);
566 g_signal_connect (action_group, "action-removed",
567 G_CALLBACK (g_action_group_exporter_action_removed), exporter);
568 g_signal_connect (action_group, "action-state-changed",
569 G_CALLBACK (g_action_group_exporter_action_state_changed), exporter);
570 g_signal_connect (action_group, "action-enabled-changed",
571 G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
577 * g_dbus_connection_unexport_action_group:
578 * @connection: a #GDBusConnection
579 * @export_id: the ID from g_dbus_connection_export_action_group()
581 * Reverses the effect of a previous call to
582 * g_dbus_connection_export_action_group().
584 * It is an error to call this function with an ID that wasn't returned
585 * from g_dbus_connection_export_action_group() or to call it with the
586 * same ID more than once.
591 g_dbus_connection_unexport_action_group (GDBusConnection *connection,
594 g_dbus_connection_unregister_object (connection, export_id);