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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Ryan Lortie <desrt@desrt.ca>
25 #include "gactiongroupexporter.h"
27 #include "gdbusmethodinvocation.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusintrospection.h"
30 #include "gdbusconnection.h"
31 #include "gactiongroup.h"
32 #include "gdbuserror.h"
35 * SECTION:gactiongroupexporter
36 * @title: GActionGroup exporter
38 * @short_description: Export GActionGroups on D-Bus
39 * @see_also: #GActionGroup, #GDBusActionGroup
41 * These functions support exporting a #GActionGroup on D-Bus.
42 * The D-Bus interface that is used is a private implementation
45 * To access an exported #GActionGroup remotely, use
46 * g_dbus_action_group_get() to obtain a #GDBusActionGroup.
50 g_action_group_describe_action (GActionGroup *action_group,
53 const GVariantType *type;
54 GVariantBuilder builder;
58 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(bgav)"));
60 enabled = g_action_group_get_action_enabled (action_group, name);
61 g_variant_builder_add (&builder, "b", enabled);
63 if ((type = g_action_group_get_action_parameter_type (action_group, name)))
65 gchar *str = g_variant_type_dup_string (type);
66 g_variant_builder_add (&builder, "g", str);
70 g_variant_builder_add (&builder, "g", "");
72 g_variant_builder_open (&builder, G_VARIANT_TYPE ("av"));
73 if ((state = g_action_group_get_action_state (action_group, name)))
75 g_variant_builder_add (&builder, "v", state);
76 g_variant_unref (state);
78 g_variant_builder_close (&builder);
80 return g_variant_builder_end (&builder);
83 /* Using XML saves us dozens of relocations vs. using the introspection
84 * structure types. We only need to burn cycles and memory if we
85 * actually use the exporter -- not in every single app using GIO.
87 * It's also a lot easier to read. :)
89 * For documentation of this interface, see
90 * http://live.gnome.org/GTK+/GApplication-dbus-apis
92 const char org_gtk_Actions_xml[] =
94 " <interface name='org.gtk.Actions'>"
95 " <method name='List'>"
96 " <arg type='as' name='list' direction='out'/>"
98 " <method name='Describe'>"
99 " <arg type='s' name='action_name' direction='in'/>"
100 " <arg type='(bgav)' name='description' direction='out'/>"
102 " <method name='DescribeAll'>"
103 " <arg type='a{s(bgav)}' name='descriptions' direction='out'/>"
105 " <method name='Activate'>"
106 " <arg type='s' name='action_name' direction='in'/>"
107 " <arg type='av' name='parameter' direction='in'/>"
108 " <arg type='a{sv}' name='platform_data' direction='in'/>"
110 " <method name='SetState'>"
111 " <arg type='s' name='action_name' direction='in'/>"
112 " <arg type='v' name='value' direction='in'/>"
113 " <arg type='a{sv}' name='platform_data' direction='in'/>"
115 " <signal name='Changed'>"
116 " <arg type='as' name='removals'/>"
117 " <arg type='a{sb}' name='enable_changes'/>"
118 " <arg type='a{sv}' name='state_changes'/>"
119 " <arg type='a{s(bgav)}' name='additions'/>"
124 static GDBusInterfaceInfo *org_gtk_Actions;
128 GActionGroup *action_group;
129 GDBusConnection *connection;
130 GMainContext *context;
132 GHashTable *pending_changes;
133 GSource *pending_source;
134 } GActionGroupExporter;
136 #define ACTION_ADDED_EVENT (1u<<0)
137 #define ACTION_REMOVED_EVENT (1u<<1)
138 #define ACTION_STATE_CHANGED_EVENT (1u<<2)
139 #define ACTION_ENABLED_CHANGED_EVENT (1u<<3)
142 g_action_group_exporter_dispatch_events (gpointer user_data)
144 GActionGroupExporter *exporter = user_data;
145 GVariantBuilder removes;
146 GVariantBuilder enabled_changes;
147 GVariantBuilder state_changes;
148 GVariantBuilder adds;
153 g_variant_builder_init (&removes, G_VARIANT_TYPE_STRING_ARRAY);
154 g_variant_builder_init (&enabled_changes, G_VARIANT_TYPE ("a{sb}"));
155 g_variant_builder_init (&state_changes, G_VARIANT_TYPE ("a{sv}"));
156 g_variant_builder_init (&adds, G_VARIANT_TYPE ("a{s(bgav)}"));
158 g_hash_table_iter_init (&iter, exporter->pending_changes);
159 while (g_hash_table_iter_next (&iter, &key, &value))
161 guint events = GPOINTER_TO_INT (value);
162 const gchar *name = key;
164 /* Adds and removes are incompatible with enabled or state
165 * changes, but we must report at least one event.
167 g_assert (((events & (ACTION_ENABLED_CHANGED_EVENT | ACTION_STATE_CHANGED_EVENT)) == 0) !=
168 ((events & (ACTION_REMOVED_EVENT | ACTION_ADDED_EVENT)) == 0));
170 if (events & ACTION_REMOVED_EVENT)
171 g_variant_builder_add (&removes, "s", name);
173 if (events & ACTION_ENABLED_CHANGED_EVENT)
177 enabled = g_action_group_get_action_enabled (exporter->action_group, name);
178 g_variant_builder_add (&enabled_changes, "{sb}", name, enabled);
181 if (events & ACTION_STATE_CHANGED_EVENT)
185 state = g_action_group_get_action_state (exporter->action_group, name);
186 g_variant_builder_add (&state_changes, "{sv}", name, state);
187 g_variant_unref (state);
190 if (events & ACTION_ADDED_EVENT)
192 GVariant *description;
194 description = g_action_group_describe_action (exporter->action_group, name);
195 g_variant_builder_add (&adds, "{s@(bgav)}", name, description);
199 g_hash_table_remove_all (exporter->pending_changes);
201 g_dbus_connection_emit_signal (exporter->connection, NULL, exporter->object_path,
202 "org.gtk.Actions", "Changed",
203 g_variant_new ("(asa{sb}a{sv}a{s(bgav)})",
204 &removes, &enabled_changes,
205 &state_changes, &adds),
208 exporter->pending_source = NULL;
214 g_action_group_exporter_get_events (GActionGroupExporter *exporter,
217 return (gsize) g_hash_table_lookup (exporter->pending_changes, name);
221 g_action_group_exporter_set_events (GActionGroupExporter *exporter,
225 gboolean have_events;
229 g_hash_table_insert (exporter->pending_changes, g_strdup (name), GINT_TO_POINTER (events));
231 g_hash_table_remove (exporter->pending_changes, name);
233 have_events = g_hash_table_size (exporter->pending_changes) > 0;
234 is_queued = exporter->pending_source != NULL;
236 if (have_events && !is_queued)
240 source = g_idle_source_new ();
241 exporter->pending_source = source;
242 g_source_set_callback (source, g_action_group_exporter_dispatch_events, exporter, NULL);
243 g_source_attach (source, exporter->context);
244 g_source_unref (source);
247 if (!have_events && is_queued)
249 g_source_destroy (exporter->pending_source);
250 exporter->pending_source = NULL;
255 g_action_group_exporter_action_added (GActionGroup *action_group,
256 const gchar *action_name,
259 GActionGroupExporter *exporter = user_data;
262 event_mask = g_action_group_exporter_get_events (exporter, action_name);
264 /* The action is new, so we should not have any pending
265 * enabled-changed or state-changed signals for it.
267 g_assert (~event_mask & (ACTION_STATE_CHANGED_EVENT |
268 ACTION_ENABLED_CHANGED_EVENT));
270 event_mask |= ACTION_ADDED_EVENT;
272 g_action_group_exporter_set_events (exporter, action_name, event_mask);
276 g_action_group_exporter_action_removed (GActionGroup *action_group,
277 const gchar *action_name,
280 GActionGroupExporter *exporter = user_data;
283 event_mask = g_action_group_exporter_get_events (exporter, action_name);
285 /* If the add event for this is still queued then just cancel it since
288 * If the event was freshly added, there should not have been any
289 * enabled or state changed events.
291 if (event_mask & ACTION_ADDED_EVENT)
293 g_assert (~event_mask & ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT));
294 event_mask &= ~ACTION_ADDED_EVENT;
297 /* Otherwise, queue a remove event. Drop any state or enabled changes
298 * that were queued before the remove. */
301 event_mask |= ACTION_REMOVED_EVENT;
302 event_mask &= ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT);
305 g_action_group_exporter_set_events (exporter, action_name, event_mask);
309 g_action_group_exporter_action_state_changed (GActionGroup *action_group,
310 const gchar *action_name,
314 GActionGroupExporter *exporter = user_data;
317 event_mask = g_action_group_exporter_get_events (exporter, action_name);
319 /* If it was removed, it must have been added back. Otherwise, why
320 * are we hearing about changes?
322 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
323 event_mask & ACTION_ADDED_EVENT);
325 /* If it is freshly added, don't also bother with the state change
326 * signal since the updated state will be sent as part of the pending
329 if (~event_mask & ACTION_ADDED_EVENT)
330 event_mask |= ACTION_STATE_CHANGED_EVENT;
332 g_action_group_exporter_set_events (exporter, action_name, event_mask);
336 g_action_group_exporter_action_enabled_changed (GActionGroup *action_group,
337 const gchar *action_name,
341 GActionGroupExporter *exporter = user_data;
344 event_mask = g_action_group_exporter_get_events (exporter, action_name);
346 /* Reasoning as above. */
347 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
348 event_mask & ACTION_ADDED_EVENT);
350 if (~event_mask & ACTION_ADDED_EVENT)
351 event_mask |= ACTION_ENABLED_CHANGED_EVENT;
353 g_action_group_exporter_set_events (exporter, action_name, event_mask);
357 org_gtk_Actions_method_call (GDBusConnection *connection,
359 const gchar *object_path,
360 const gchar *interface_name,
361 const gchar *method_name,
362 GVariant *parameters,
363 GDBusMethodInvocation *invocation,
366 GActionGroupExporter *exporter = user_data;
367 GVariant *result = NULL;
369 if (g_str_equal (method_name, "List"))
373 list = g_action_group_list_actions (exporter->action_group);
374 result = g_variant_new ("(^as)", list);
378 else if (g_str_equal (method_name, "Describe"))
383 g_variant_get (parameters, "(&s)", &name);
385 if (!g_action_group_has_action (exporter->action_group, name))
387 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
388 "The named action ('%s') does not exist.", name);
392 desc = g_action_group_describe_action (exporter->action_group, name);
393 result = g_variant_new ("(@(bgav))", desc);
396 else if (g_str_equal (method_name, "DescribeAll"))
398 GVariantBuilder builder;
402 list = g_action_group_list_actions (exporter->action_group);
403 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{s(bgav)}"));
404 for (i = 0; list[i]; i++)
406 const gchar *name = list[i];
407 GVariant *description;
409 description = g_action_group_describe_action (exporter->action_group, name);
410 g_variant_builder_add (&builder, "{s@(bgav)}", name, description);
412 result = g_variant_new ("(a{s(bgav)})", &builder);
416 else if (g_str_equal (method_name, "Activate"))
418 GVariant *parameter = NULL;
419 GVariant *platform_data;
423 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
424 g_variant_iter_next (iter, "v", ¶meter);
425 g_variant_iter_free (iter);
427 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
428 g_remote_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
429 name, parameter, platform_data);
431 g_action_group_activate_action (exporter->action_group, name, parameter);
434 g_variant_unref (parameter);
436 g_variant_unref (platform_data);
439 else if (g_str_equal (method_name, "SetState"))
441 GVariant *platform_data;
445 g_variant_get (parameters, "(&sv@a{sv})", &name, &state, &platform_data);
447 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
448 g_remote_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
449 name, state, platform_data);
451 g_action_group_change_action_state (exporter->action_group, name, state);
453 g_variant_unref (platform_data);
454 g_variant_unref (state);
458 g_assert_not_reached ();
460 g_dbus_method_invocation_return_value (invocation, result);
464 g_action_group_exporter_free (gpointer user_data)
466 GActionGroupExporter *exporter = user_data;
468 g_signal_handlers_disconnect_by_func (exporter->action_group,
469 g_action_group_exporter_action_added, exporter);
470 g_signal_handlers_disconnect_by_func (exporter->action_group,
471 g_action_group_exporter_action_enabled_changed, exporter);
472 g_signal_handlers_disconnect_by_func (exporter->action_group,
473 g_action_group_exporter_action_state_changed, exporter);
474 g_signal_handlers_disconnect_by_func (exporter->action_group,
475 g_action_group_exporter_action_removed, exporter);
477 g_hash_table_unref (exporter->pending_changes);
478 if (exporter->pending_source)
479 g_source_destroy (exporter->pending_source);
481 g_main_context_unref (exporter->context);
482 g_object_unref (exporter->connection);
483 g_object_unref (exporter->action_group);
484 g_free (exporter->object_path);
486 g_slice_free (GActionGroupExporter, exporter);
490 * g_dbus_connection_export_action_group:
491 * @connection: a #GDBusConnection
492 * @object_path: a D-Bus object path
493 * @action_group: a #GActionGroup
494 * @error: a pointer to a %NULL #GError, or %NULL
496 * Exports @action_group on @connection at @object_path.
498 * The implemented D-Bus API should be considered private. It is
499 * subject to change in the future.
501 * A given object path can only have one action group exported on it.
502 * If this constraint is violated, the export will fail and 0 will be
503 * returned (with @error set accordingly).
505 * You can unexport the action group using
506 * g_dbus_connection_unexport_action_group() with the return value of
509 * The thread default main context is taken at the time of this call.
510 * All incoming action activations and state change requests are
511 * reported from this context. Any changes on the action group that
512 * cause it to emit signals must also come from this same context.
513 * Since incoming action activations and state change requests are
514 * rather likely to cause changes on the action group, this effectively
515 * limits a given action group to being exported from only one main
518 * Returns: the ID of the export (never zero), or 0 in case of failure
523 g_dbus_connection_export_action_group (GDBusConnection *connection,
524 const gchar *object_path,
525 GActionGroup *action_group,
528 const GDBusInterfaceVTable vtable = {
529 org_gtk_Actions_method_call
531 GActionGroupExporter *exporter;
534 if G_UNLIKELY (org_gtk_Actions == NULL)
536 GError *error = NULL;
539 info = g_dbus_node_info_new_for_xml (org_gtk_Actions_xml, &error);
540 if G_UNLIKELY (info == NULL)
541 g_error ("%s", error->message);
542 org_gtk_Actions = g_dbus_node_info_lookup_interface (info, "org.gtk.Actions");
543 g_assert (org_gtk_Actions != NULL);
544 g_dbus_interface_info_ref (org_gtk_Actions);
545 g_dbus_node_info_unref (info);
548 exporter = g_slice_new (GActionGroupExporter);
549 id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
550 exporter, g_action_group_exporter_free, error);
554 g_slice_free (GActionGroupExporter, exporter);
558 exporter->context = g_main_context_ref_thread_default ();
559 exporter->pending_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
560 exporter->pending_source = NULL;
561 exporter->action_group = g_object_ref (action_group);
562 exporter->connection = g_object_ref (connection);
563 exporter->object_path = g_strdup (object_path);
565 g_signal_connect (action_group, "action-added",
566 G_CALLBACK (g_action_group_exporter_action_added), exporter);
567 g_signal_connect (action_group, "action-removed",
568 G_CALLBACK (g_action_group_exporter_action_removed), exporter);
569 g_signal_connect (action_group, "action-state-changed",
570 G_CALLBACK (g_action_group_exporter_action_state_changed), exporter);
571 g_signal_connect (action_group, "action-enabled-changed",
572 G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
578 * g_dbus_connection_unexport_action_group:
579 * @connection: a #GDBusConnection
580 * @export_id: the ID from g_dbus_connection_export_action_group()
582 * Reverses the effect of a previous call to
583 * g_dbus_connection_export_action_group().
585 * It is an error to call this function with an ID that wasn't returned
586 * from g_dbus_connection_export_action_group() or to call it with the
587 * same ID more than once.
592 g_dbus_connection_unexport_action_group (GDBusConnection *connection,
595 g_dbus_connection_unregister_object (connection, export_id);