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 "gdbusactiongroup.h"
27 #include "gdbusconnection.h"
28 #include "gactiongroup.h"
31 * SECTION:gdbusactiongroup
32 * @title: GDBusActionGroup
33 * @short_description: A D-Bus GActionGroup implementation
34 * @see_also: <link linkend="gio-GActionGroup-exporter">GActionGroup exporter</link>
36 * #GDBusActionGroup is an implementation of the #GActionGroup
37 * interface that can be used as a proxy for an action group
38 * that is exported over D-Bus with g_dbus_connection_export_action_group().
41 struct _GDBusActionGroup
43 GObject parent_instance;
45 GDBusConnection *connection;
48 guint subscription_id;
51 /* The 'strict' flag indicates that the non-existence of at least one
52 * action has potentially been observed through the API. This means
53 * that we should always emit 'action-added' signals for all new
56 * The user can observe the non-existence of an action by listing the
57 * actions or by performing a query (such as parameter type) on a
58 * non-existent action.
60 * If the user has no way of knowing that a given action didn't
61 * already exist then we can skip emitting 'action-added' signals
62 * since they have no way of knowing that it wasn't there from the
68 typedef GObjectClass GDBusActionGroupClass;
73 GVariantType *parameter_type;
79 action_info_free (gpointer user_data)
81 ActionInfo *info = user_data;
86 g_variant_unref (info->state);
88 if (info->parameter_type)
89 g_variant_type_free (info->parameter_type);
91 g_slice_free (ActionInfo, info);
95 action_info_new_from_iter (GVariantIter *iter)
97 const gchar *param_str;
103 if (!g_variant_iter_next (iter, "{s(b&g@av)}", &name,
104 &enabled, ¶m_str, &state))
107 info = g_slice_new (ActionInfo);
109 info->enabled = enabled;
111 if (g_variant_n_children (state))
112 g_variant_get_child (state, 0, "v", &info->state);
115 g_variant_unref (state);
118 info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
120 info->parameter_type = NULL;
125 static void g_dbus_action_group_iface_init (GActionGroupInterface *);
126 G_DEFINE_TYPE_WITH_CODE (GDBusActionGroup, g_dbus_action_group, G_TYPE_OBJECT,
127 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_dbus_action_group_iface_init))
130 g_dbus_action_group_changed (GDBusConnection *connection,
132 const gchar *object_path,
133 const gchar *interface_name,
134 const gchar *signal_name,
135 GVariant *parameters,
138 GDBusActionGroup *group = user_data;
139 GActionGroup *g_group = user_data;
141 /* make sure that we've been fully initialised */
142 if (group->actions == NULL)
145 if (g_str_equal (signal_name, "Changed") &&
146 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
153 g_variant_get_child (parameters, 0, "as", &iter);
154 while (g_variant_iter_next (iter, "&s", &name))
156 if (g_hash_table_lookup (group->actions, name))
158 g_hash_table_remove (group->actions, name);
159 g_action_group_action_removed (g_group, name);
162 g_variant_iter_free (iter);
171 g_variant_get_child (parameters, 1, "a{sb}", &iter);
172 while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
176 info = g_hash_table_lookup (group->actions, name);
178 if (info && info->enabled != enabled)
180 info->enabled = enabled;
181 g_action_group_action_enabled_changed (g_group, name, enabled);
184 g_variant_iter_free (iter);
193 g_variant_get_child (parameters, 2, "a{sv}", &iter);
194 while (g_variant_iter_next (iter, "{&sv}", &name, &state))
198 info = g_hash_table_lookup (group->actions, name);
200 if (info && info->state && !g_variant_equal (state, info->state) &&
201 g_variant_is_of_type (state, g_variant_get_type (info->state)))
203 g_variant_unref (info->state);
204 info->state = g_variant_ref (state);
206 g_action_group_action_state_changed (g_group, name, state);
209 g_variant_unref (state);
211 g_variant_iter_free (iter);
219 g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
220 while ((info = action_info_new_from_iter (iter)))
222 if (!g_hash_table_lookup (group->actions, info->name))
224 g_hash_table_insert (group->actions, info->name, info);
227 g_action_group_action_added (g_group, info->name);
230 action_info_free (info);
232 g_variant_iter_free (iter);
239 g_dbus_action_group_describe_all_done (GObject *source,
240 GAsyncResult *result,
243 GDBusActionGroup *group= user_data;
246 g_assert (group->actions == NULL);
247 group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
249 g_assert (group->connection == (gpointer) source);
250 reply = g_dbus_connection_call_finish (group->connection, result, NULL);
257 g_variant_get (reply, "(a{s(bgav)})", &iter);
258 while ((action = action_info_new_from_iter (iter)))
260 g_hash_table_insert (group->actions, action->name, action);
263 g_action_group_action_added (G_ACTION_GROUP (group), action->name);
265 g_variant_iter_free (iter);
266 g_variant_unref (reply);
272 g_dbus_action_group_async_init (GDBusActionGroup *group)
274 if (group->subscription_id != 0)
277 group->subscription_id =
278 g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
279 NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
281 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "DescribeAll", NULL,
282 G_VARIANT_TYPE ("(a{s(bgav)})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
283 g_dbus_action_group_describe_all_done, group);
287 g_dbus_action_group_list_actions (GActionGroup *g_group)
289 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
292 if (group->actions != NULL)
298 n = g_hash_table_size (group->actions);
299 keys = g_new (gchar *, n + 1);
301 g_hash_table_iter_init (&iter, group->actions);
302 while (g_hash_table_iter_next (&iter, &key, NULL))
303 keys[i++] = g_strdup (key);
304 g_assert_cmpint (i, ==, n);
309 g_dbus_action_group_async_init (group);
310 keys = g_new0 (gchar *, 1);
313 group->strict = TRUE;
319 g_dbus_action_group_query_action (GActionGroup *g_group,
320 const gchar *action_name,
322 const GVariantType **parameter_type,
323 const GVariantType **state_type,
324 GVariant **state_hint,
327 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
330 if (group->actions != NULL)
332 info = g_hash_table_lookup (group->actions, action_name);
336 group->strict = TRUE;
341 *enabled = info->enabled;
344 *parameter_type = info->parameter_type;
347 *state_type = info->state ? g_variant_get_type (info->state) : NULL;
353 *state = info->state ? g_variant_ref (info->state) : NULL;
359 g_dbus_action_group_async_init (group);
366 g_dbus_action_group_change_state (GActionGroup *g_group,
367 const gchar *action_name,
370 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
372 /* Don't bother with the checks. The other side will do it again. */
373 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "SetState",
374 g_variant_new ("(sva{sv})", action_name, value, NULL),
375 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
379 g_dbus_action_group_activate (GActionGroup *g_group,
380 const gchar *action_name,
383 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
384 GVariantBuilder builder;
386 g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
389 g_variant_builder_add (&builder, "v", parameter);
391 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "Activate",
392 g_variant_new ("(sava{sv})", action_name, &builder, NULL),
393 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
397 g_dbus_action_group_finalize (GObject *object)
399 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (object);
401 if (group->subscription_id)
402 g_dbus_connection_signal_unsubscribe (group->connection, group->subscription_id);
405 g_hash_table_unref (group->actions);
407 g_object_unref (group->connection);
408 g_free (group->object_path);
409 g_free (group->bus_name);
411 G_OBJECT_CLASS (g_dbus_action_group_parent_class)
416 g_dbus_action_group_init (GDBusActionGroup *group)
421 g_dbus_action_group_class_init (GDBusActionGroupClass *class)
423 GObjectClass *object_class = G_OBJECT_CLASS (class);
425 object_class->finalize = g_dbus_action_group_finalize;
429 g_dbus_action_group_iface_init (GActionGroupInterface *iface)
431 iface->list_actions = g_dbus_action_group_list_actions;
432 iface->query_action = g_dbus_action_group_query_action;
433 iface->change_action_state = g_dbus_action_group_change_state;
434 iface->activate_action = g_dbus_action_group_activate;
438 * g_dbus_action_group_get:
439 * @connection: A #GDBusConnection
440 * @bus_name: the bus name which exports the action group
441 * @object_path: the object path at which the action group is exported
443 * Obtains a #GDBusActionGroup for the action group which is exported at
444 * the given @bus_name and @object_path.
446 * The thread default main context is taken at the time of this call.
447 * All signals on the menu model (and any linked models) are reported
448 * with respect to this context. All calls on the returned menu model
449 * (and linked models) must also originate from this same context, with
450 * the thread default main context unchanged.
452 * This call is non-blocking. The returned action group may or may not
453 * already be filled in. The correct thing to do is connect the signals
454 * for the action group to monitor for changes and then to call
455 * g_action_group_list_actions() to get the initial list.
457 * Returns: (transfer full): a #GDBusActionGroup
462 g_dbus_action_group_get (GDBusConnection *connection,
463 const gchar *bus_name,
464 const gchar *object_path)
466 GDBusActionGroup *group;
468 group = g_object_new (G_TYPE_DBUS_ACTION_GROUP, NULL);
469 group->connection = g_object_ref (connection);
470 group->bus_name = g_strdup (bus_name);
471 group->object_path = g_strdup (object_path);
476 G_GNUC_INTERNAL gboolean
477 g_dbus_action_group_sync (GDBusActionGroup *group,
478 GCancellable *cancellable,
483 g_assert (group->subscription_id == 0);
485 group->subscription_id =
486 g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
487 NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
489 reply = g_dbus_connection_call_sync (group->connection, group->bus_name, group->object_path, "org.gtk.Actions",
490 "DescribeAll", NULL, G_VARIANT_TYPE ("(a{s(bgav)})"),
491 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);
498 g_assert (group->actions == NULL);
499 group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
501 g_variant_get (reply, "(a{s(bgav)})", &iter);
502 while ((action = action_info_new_from_iter (iter)))
503 g_hash_table_insert (group->actions, action->name, action);
504 g_variant_iter_free (iter);
505 g_variant_unref (reply);
508 return reply != NULL;