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-private.h"
27 #include "gremoteactiongroup.h"
28 #include "gdbusconnection.h"
29 #include "gactiongroup.h"
32 * SECTION:gdbusactiongroup
33 * @title: GDBusActionGroup
34 * @short_description: A D-Bus GActionGroup implementation
36 * @see_also: <link linkend="gio-GActionGroup-exporter">GActionGroup exporter</link>
38 * #GDBusActionGroup is an implementation of the #GActionGroup
39 * interface that can be used as a proxy for an action group
40 * that is exported over D-Bus with g_dbus_connection_export_action_group().
43 struct _GDBusActionGroup
45 GObject parent_instance;
47 GDBusConnection *connection;
50 guint subscription_id;
53 /* The 'strict' flag indicates that the non-existence of at least one
54 * action has potentially been observed through the API. This means
55 * that we should always emit 'action-added' signals for all new
58 * The user can observe the non-existence of an action by listing the
59 * actions or by performing a query (such as parameter type) on a
60 * non-existent action.
62 * If the user has no way of knowing that a given action didn't
63 * already exist then we can skip emitting 'action-added' signals
64 * since they have no way of knowing that it wasn't there from the
70 typedef GObjectClass GDBusActionGroupClass;
75 GVariantType *parameter_type;
81 action_info_free (gpointer user_data)
83 ActionInfo *info = user_data;
88 g_variant_unref (info->state);
90 if (info->parameter_type)
91 g_variant_type_free (info->parameter_type);
93 g_slice_free (ActionInfo, info);
97 action_info_new_from_iter (GVariantIter *iter)
99 const gchar *param_str;
105 if (!g_variant_iter_next (iter, "{s(b&g@av)}", &name,
106 &enabled, ¶m_str, &state))
109 info = g_slice_new (ActionInfo);
111 info->enabled = enabled;
113 if (g_variant_n_children (state))
114 g_variant_get_child (state, 0, "v", &info->state);
117 g_variant_unref (state);
120 info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
122 info->parameter_type = NULL;
127 static void g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface);
128 static void g_dbus_action_group_iface_init (GActionGroupInterface *iface);
129 G_DEFINE_TYPE_WITH_CODE (GDBusActionGroup, g_dbus_action_group, G_TYPE_OBJECT,
130 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_dbus_action_group_iface_init)
131 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_dbus_action_group_remote_iface_init))
134 g_dbus_action_group_changed (GDBusConnection *connection,
136 const gchar *object_path,
137 const gchar *interface_name,
138 const gchar *signal_name,
139 GVariant *parameters,
142 GDBusActionGroup *group = user_data;
143 GActionGroup *g_group = user_data;
145 /* make sure that we've been fully initialised */
146 if (group->actions == NULL)
149 if (g_str_equal (signal_name, "Changed") &&
150 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
157 g_variant_get_child (parameters, 0, "as", &iter);
158 while (g_variant_iter_next (iter, "&s", &name))
160 if (g_hash_table_lookup (group->actions, name))
162 g_hash_table_remove (group->actions, name);
163 g_action_group_action_removed (g_group, name);
166 g_variant_iter_free (iter);
175 g_variant_get_child (parameters, 1, "a{sb}", &iter);
176 while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
180 info = g_hash_table_lookup (group->actions, name);
182 if (info && info->enabled != enabled)
184 info->enabled = enabled;
185 g_action_group_action_enabled_changed (g_group, name, enabled);
188 g_variant_iter_free (iter);
197 g_variant_get_child (parameters, 2, "a{sv}", &iter);
198 while (g_variant_iter_next (iter, "{&sv}", &name, &state))
202 info = g_hash_table_lookup (group->actions, name);
204 if (info && info->state && !g_variant_equal (state, info->state) &&
205 g_variant_is_of_type (state, g_variant_get_type (info->state)))
207 g_variant_unref (info->state);
208 info->state = g_variant_ref (state);
210 g_action_group_action_state_changed (g_group, name, state);
213 g_variant_unref (state);
215 g_variant_iter_free (iter);
223 g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
224 while ((info = action_info_new_from_iter (iter)))
226 if (!g_hash_table_lookup (group->actions, info->name))
228 g_hash_table_insert (group->actions, info->name, info);
231 g_action_group_action_added (g_group, info->name);
234 action_info_free (info);
236 g_variant_iter_free (iter);
243 g_dbus_action_group_describe_all_done (GObject *source,
244 GAsyncResult *result,
247 GDBusActionGroup *group= user_data;
250 g_assert (group->actions == NULL);
251 group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
253 g_assert (group->connection == (gpointer) source);
254 reply = g_dbus_connection_call_finish (group->connection, result, NULL);
261 g_variant_get (reply, "(a{s(bgav)})", &iter);
262 while ((action = action_info_new_from_iter (iter)))
264 g_hash_table_insert (group->actions, action->name, action);
267 g_action_group_action_added (G_ACTION_GROUP (group), action->name);
269 g_variant_iter_free (iter);
270 g_variant_unref (reply);
273 g_object_unref (group);
278 g_dbus_action_group_async_init (GDBusActionGroup *group)
280 if (group->subscription_id != 0)
283 group->subscription_id =
284 g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
285 NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
287 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "DescribeAll", NULL,
288 G_VARIANT_TYPE ("(a{s(bgav)})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
289 g_dbus_action_group_describe_all_done, g_object_ref (group));
293 g_dbus_action_group_list_actions (GActionGroup *g_group)
295 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
298 if (group->actions != NULL)
304 n = g_hash_table_size (group->actions);
305 keys = g_new (gchar *, n + 1);
307 g_hash_table_iter_init (&iter, group->actions);
308 while (g_hash_table_iter_next (&iter, &key, NULL))
309 keys[i++] = g_strdup (key);
310 g_assert_cmpint (i, ==, n);
315 g_dbus_action_group_async_init (group);
316 keys = g_new0 (gchar *, 1);
319 group->strict = TRUE;
325 g_dbus_action_group_query_action (GActionGroup *g_group,
326 const gchar *action_name,
328 const GVariantType **parameter_type,
329 const GVariantType **state_type,
330 GVariant **state_hint,
333 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
336 if (group->actions != NULL)
338 info = g_hash_table_lookup (group->actions, action_name);
342 group->strict = TRUE;
347 *enabled = info->enabled;
350 *parameter_type = info->parameter_type;
353 *state_type = info->state ? g_variant_get_type (info->state) : NULL;
359 *state = info->state ? g_variant_ref (info->state) : NULL;
365 g_dbus_action_group_async_init (group);
366 group->strict = TRUE;
373 g_dbus_action_group_activate_action_full (GRemoteActionGroup *remote,
374 const gchar *action_name,
376 GVariant *platform_data)
378 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote);
379 GVariantBuilder builder;
381 g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
384 g_variant_builder_add (&builder, "v", parameter);
386 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "Activate",
387 g_variant_new ("(sav@a{sv})", action_name, &builder, platform_data),
388 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
392 g_dbus_action_group_change_action_state_full (GRemoteActionGroup *remote,
393 const gchar *action_name,
395 GVariant *platform_data)
397 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote);
399 g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "SetState",
400 g_variant_new ("(sv@a{sv})", action_name, value, platform_data),
401 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
405 g_dbus_action_group_change_state (GActionGroup *group,
406 const gchar *action_name,
409 g_dbus_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (group),
410 action_name, value, g_variant_new ("a{sv}", NULL));
414 g_dbus_action_group_activate (GActionGroup *group,
415 const gchar *action_name,
418 g_dbus_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (group),
419 action_name, parameter, g_variant_new ("a{sv}", NULL));
423 g_dbus_action_group_finalize (GObject *object)
425 GDBusActionGroup *group = G_DBUS_ACTION_GROUP (object);
427 if (group->subscription_id)
428 g_dbus_connection_signal_unsubscribe (group->connection, group->subscription_id);
431 g_hash_table_unref (group->actions);
433 g_object_unref (group->connection);
434 g_free (group->object_path);
435 g_free (group->bus_name);
437 G_OBJECT_CLASS (g_dbus_action_group_parent_class)
442 g_dbus_action_group_init (GDBusActionGroup *group)
447 g_dbus_action_group_class_init (GDBusActionGroupClass *class)
449 GObjectClass *object_class = G_OBJECT_CLASS (class);
451 object_class->finalize = g_dbus_action_group_finalize;
455 g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface)
457 iface->activate_action_full = g_dbus_action_group_activate_action_full;
458 iface->change_action_state_full = g_dbus_action_group_change_action_state_full;
462 g_dbus_action_group_iface_init (GActionGroupInterface *iface)
464 iface->list_actions = g_dbus_action_group_list_actions;
465 iface->query_action = g_dbus_action_group_query_action;
466 iface->change_action_state = g_dbus_action_group_change_state;
467 iface->activate_action = g_dbus_action_group_activate;
471 * g_dbus_action_group_get:
472 * @connection: A #GDBusConnection
473 * @bus_name: the bus name which exports the action group
474 * @object_path: the object path at which the action group is exported
476 * Obtains a #GDBusActionGroup for the action group which is exported at
477 * the given @bus_name and @object_path.
479 * The thread default main context is taken at the time of this call.
480 * All signals on the menu model (and any linked models) are reported
481 * with respect to this context. All calls on the returned menu model
482 * (and linked models) must also originate from this same context, with
483 * the thread default main context unchanged.
485 * This call is non-blocking. The returned action group may or may not
486 * already be filled in. The correct thing to do is connect the signals
487 * for the action group to monitor for changes and then to call
488 * g_action_group_list_actions() to get the initial list.
490 * Returns: (transfer full): a #GDBusActionGroup
495 g_dbus_action_group_get (GDBusConnection *connection,
496 const gchar *bus_name,
497 const gchar *object_path)
499 GDBusActionGroup *group;
501 group = g_object_new (G_TYPE_DBUS_ACTION_GROUP, NULL);
502 group->connection = g_object_ref (connection);
503 group->bus_name = g_strdup (bus_name);
504 group->object_path = g_strdup (object_path);
510 g_dbus_action_group_sync (GDBusActionGroup *group,
511 GCancellable *cancellable,
516 g_assert (group->subscription_id == 0);
518 group->subscription_id =
519 g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
520 NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
522 reply = g_dbus_connection_call_sync (group->connection, group->bus_name, group->object_path, "org.gtk.Actions",
523 "DescribeAll", NULL, G_VARIANT_TYPE ("(a{s(bgav)})"),
524 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);
531 g_assert (group->actions == NULL);
532 group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
534 g_variant_get (reply, "(a{s(bgav)})", &iter);
535 while ((action = action_info_new_from_iter (iter)))
536 g_hash_table_insert (group->actions, action->name, action);
537 g_variant_iter_free (iter);
538 g_variant_unref (reply);
541 return reply != NULL;