/src/glib/gio/gdbusactiongroup.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2010 Codethink Limited |
3 | | * Copyright © 2011 Canonical Limited |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Authors: Ryan Lortie <desrt@desrt.ca> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include "gdbusactiongroup-private.h" |
26 | | |
27 | | #include "gremoteactiongroup.h" |
28 | | #include "gdbusconnection.h" |
29 | | #include "gactiongroup.h" |
30 | | |
31 | | /** |
32 | | * SECTION:gdbusactiongroup |
33 | | * @title: GDBusActionGroup |
34 | | * @short_description: A D-Bus GActionGroup implementation |
35 | | * @include: gio/gio.h |
36 | | * @see_also: [GActionGroup exporter][gio-GActionGroup-exporter] |
37 | | * |
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(). |
41 | | */ |
42 | | |
43 | | /** |
44 | | * GDBusActionGroup: |
45 | | * |
46 | | * #GDBusActionGroup is an opaque data structure and can only be accessed |
47 | | * using the following functions. |
48 | | */ |
49 | | |
50 | | struct _GDBusActionGroup |
51 | | { |
52 | | GObject parent_instance; |
53 | | |
54 | | GDBusConnection *connection; |
55 | | gchar *bus_name; |
56 | | gchar *object_path; |
57 | | guint subscription_id; |
58 | | GHashTable *actions; |
59 | | |
60 | | /* The 'strict' flag indicates that the non-existence of at least one |
61 | | * action has potentially been observed through the API. This means |
62 | | * that we should always emit 'action-added' signals for all new |
63 | | * actions. |
64 | | * |
65 | | * The user can observe the non-existence of an action by listing the |
66 | | * actions or by performing a query (such as parameter type) on a |
67 | | * non-existent action. |
68 | | * |
69 | | * If the user has no way of knowing that a given action didn't |
70 | | * already exist then we can skip emitting 'action-added' signals |
71 | | * since they have no way of knowing that it wasn't there from the |
72 | | * start. |
73 | | */ |
74 | | gboolean strict; |
75 | | }; |
76 | | |
77 | | typedef GObjectClass GDBusActionGroupClass; |
78 | | |
79 | | typedef struct |
80 | | { |
81 | | gchar *name; |
82 | | GVariantType *parameter_type; |
83 | | gboolean enabled; |
84 | | GVariant *state; |
85 | | } ActionInfo; |
86 | | |
87 | | static void |
88 | | action_info_free (gpointer user_data) |
89 | 0 | { |
90 | 0 | ActionInfo *info = user_data; |
91 | |
|
92 | 0 | g_free (info->name); |
93 | |
|
94 | 0 | if (info->state) |
95 | 0 | g_variant_unref (info->state); |
96 | |
|
97 | 0 | if (info->parameter_type) |
98 | 0 | g_variant_type_free (info->parameter_type); |
99 | |
|
100 | 0 | g_slice_free (ActionInfo, info); |
101 | 0 | } |
102 | | |
103 | | static ActionInfo * |
104 | | action_info_new_from_iter (GVariantIter *iter) |
105 | 0 | { |
106 | 0 | const gchar *param_str; |
107 | 0 | ActionInfo *info; |
108 | 0 | gboolean enabled; |
109 | 0 | GVariant *state; |
110 | 0 | gchar *name; |
111 | |
|
112 | 0 | if (!g_variant_iter_next (iter, "{s(b&g@av)}", &name, |
113 | 0 | &enabled, ¶m_str, &state)) |
114 | 0 | return NULL; |
115 | | |
116 | 0 | info = g_slice_new (ActionInfo); |
117 | 0 | info->name = name; |
118 | 0 | info->enabled = enabled; |
119 | |
|
120 | 0 | if (g_variant_n_children (state)) |
121 | 0 | g_variant_get_child (state, 0, "v", &info->state); |
122 | 0 | else |
123 | 0 | info->state = NULL; |
124 | 0 | g_variant_unref (state); |
125 | |
|
126 | 0 | if (param_str[0]) |
127 | 0 | info->parameter_type = g_variant_type_copy ((GVariantType *) param_str); |
128 | 0 | else |
129 | 0 | info->parameter_type = NULL; |
130 | |
|
131 | 0 | return info; |
132 | 0 | } |
133 | | |
134 | | static void g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface); |
135 | | static void g_dbus_action_group_iface_init (GActionGroupInterface *iface); |
136 | | G_DEFINE_TYPE_WITH_CODE (GDBusActionGroup, g_dbus_action_group, G_TYPE_OBJECT, |
137 | | G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_dbus_action_group_iface_init) |
138 | | G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_dbus_action_group_remote_iface_init)) |
139 | | |
140 | | static void |
141 | | g_dbus_action_group_changed (GDBusConnection *connection, |
142 | | const gchar *sender, |
143 | | const gchar *object_path, |
144 | | const gchar *interface_name, |
145 | | const gchar *signal_name, |
146 | | GVariant *parameters, |
147 | | gpointer user_data) |
148 | 0 | { |
149 | 0 | GDBusActionGroup *group = user_data; |
150 | 0 | GActionGroup *g_group = user_data; |
151 | | |
152 | | /* make sure that we've been fully initialised */ |
153 | 0 | if (group->actions == NULL) |
154 | 0 | return; |
155 | | |
156 | 0 | if (g_str_equal (signal_name, "Changed") && |
157 | 0 | g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})"))) |
158 | 0 | { |
159 | | /* Removes */ |
160 | 0 | { |
161 | 0 | GVariantIter *iter; |
162 | 0 | const gchar *name; |
163 | |
|
164 | 0 | g_variant_get_child (parameters, 0, "as", &iter); |
165 | 0 | while (g_variant_iter_next (iter, "&s", &name)) |
166 | 0 | { |
167 | 0 | if (g_hash_table_lookup (group->actions, name)) |
168 | 0 | { |
169 | 0 | g_hash_table_remove (group->actions, name); |
170 | 0 | g_action_group_action_removed (g_group, name); |
171 | 0 | } |
172 | 0 | } |
173 | 0 | g_variant_iter_free (iter); |
174 | 0 | } |
175 | | |
176 | | /* Enable changes */ |
177 | 0 | { |
178 | 0 | GVariantIter *iter; |
179 | 0 | const gchar *name; |
180 | 0 | gboolean enabled; |
181 | |
|
182 | 0 | g_variant_get_child (parameters, 1, "a{sb}", &iter); |
183 | 0 | while (g_variant_iter_next (iter, "{&sb}", &name, &enabled)) |
184 | 0 | { |
185 | 0 | ActionInfo *info; |
186 | |
|
187 | 0 | info = g_hash_table_lookup (group->actions, name); |
188 | |
|
189 | 0 | if (info && info->enabled != enabled) |
190 | 0 | { |
191 | 0 | info->enabled = enabled; |
192 | 0 | g_action_group_action_enabled_changed (g_group, name, enabled); |
193 | 0 | } |
194 | 0 | } |
195 | 0 | g_variant_iter_free (iter); |
196 | 0 | } |
197 | | |
198 | | /* State changes */ |
199 | 0 | { |
200 | 0 | GVariantIter *iter; |
201 | 0 | const gchar *name; |
202 | 0 | GVariant *state; |
203 | |
|
204 | 0 | g_variant_get_child (parameters, 2, "a{sv}", &iter); |
205 | 0 | while (g_variant_iter_next (iter, "{&sv}", &name, &state)) |
206 | 0 | { |
207 | 0 | ActionInfo *info; |
208 | |
|
209 | 0 | info = g_hash_table_lookup (group->actions, name); |
210 | |
|
211 | 0 | if (info && info->state && !g_variant_equal (state, info->state) && |
212 | 0 | g_variant_is_of_type (state, g_variant_get_type (info->state))) |
213 | 0 | { |
214 | 0 | g_variant_unref (info->state); |
215 | 0 | info->state = g_variant_ref (state); |
216 | |
|
217 | 0 | g_action_group_action_state_changed (g_group, name, state); |
218 | 0 | } |
219 | |
|
220 | 0 | g_variant_unref (state); |
221 | 0 | } |
222 | 0 | g_variant_iter_free (iter); |
223 | 0 | } |
224 | | |
225 | | /* Additions */ |
226 | 0 | { |
227 | 0 | GVariantIter *iter; |
228 | 0 | ActionInfo *info; |
229 | |
|
230 | 0 | g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter); |
231 | 0 | while ((info = action_info_new_from_iter (iter))) |
232 | 0 | { |
233 | 0 | if (!g_hash_table_lookup (group->actions, info->name)) |
234 | 0 | { |
235 | 0 | g_hash_table_insert (group->actions, info->name, info); |
236 | |
|
237 | 0 | if (group->strict) |
238 | 0 | g_action_group_action_added (g_group, info->name); |
239 | 0 | } |
240 | 0 | else |
241 | 0 | action_info_free (info); |
242 | 0 | } |
243 | 0 | g_variant_iter_free (iter); |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | |
249 | | static void |
250 | | g_dbus_action_group_describe_all_done (GObject *source, |
251 | | GAsyncResult *result, |
252 | | gpointer user_data) |
253 | 0 | { |
254 | 0 | GDBusActionGroup *group= user_data; |
255 | 0 | GVariant *reply; |
256 | |
|
257 | 0 | g_assert (group->actions == NULL); |
258 | 0 | group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free); |
259 | |
|
260 | 0 | g_assert (group->connection == (gpointer) source); |
261 | 0 | reply = g_dbus_connection_call_finish (group->connection, result, NULL); |
262 | |
|
263 | 0 | if (reply != NULL) |
264 | 0 | { |
265 | 0 | GVariantIter *iter; |
266 | 0 | ActionInfo *action; |
267 | |
|
268 | 0 | g_variant_get (reply, "(a{s(bgav)})", &iter); |
269 | 0 | while ((action = action_info_new_from_iter (iter))) |
270 | 0 | { |
271 | 0 | g_hash_table_insert (group->actions, action->name, action); |
272 | |
|
273 | 0 | if (group->strict) |
274 | 0 | g_action_group_action_added (G_ACTION_GROUP (group), action->name); |
275 | 0 | } |
276 | 0 | g_variant_iter_free (iter); |
277 | 0 | g_variant_unref (reply); |
278 | 0 | } |
279 | |
|
280 | 0 | g_object_unref (group); |
281 | 0 | } |
282 | | |
283 | | |
284 | | static void |
285 | | g_dbus_action_group_async_init (GDBusActionGroup *group) |
286 | 0 | { |
287 | 0 | if (group->subscription_id != 0) |
288 | 0 | return; |
289 | | |
290 | 0 | group->subscription_id = |
291 | 0 | g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path, |
292 | 0 | NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL); |
293 | |
|
294 | 0 | g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "DescribeAll", NULL, |
295 | 0 | G_VARIANT_TYPE ("(a{s(bgav)})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, |
296 | 0 | g_dbus_action_group_describe_all_done, g_object_ref (group)); |
297 | 0 | } |
298 | | |
299 | | static gchar ** |
300 | | g_dbus_action_group_list_actions (GActionGroup *g_group) |
301 | 0 | { |
302 | 0 | GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group); |
303 | 0 | gchar **keys; |
304 | |
|
305 | 0 | if (group->actions != NULL) |
306 | 0 | { |
307 | 0 | GHashTableIter iter; |
308 | 0 | gint n, i = 0; |
309 | 0 | gpointer key; |
310 | |
|
311 | 0 | n = g_hash_table_size (group->actions); |
312 | 0 | keys = g_new (gchar *, n + 1); |
313 | |
|
314 | 0 | g_hash_table_iter_init (&iter, group->actions); |
315 | 0 | while (g_hash_table_iter_next (&iter, &key, NULL)) |
316 | 0 | keys[i++] = g_strdup (key); |
317 | 0 | g_assert_cmpint (i, ==, n); |
318 | 0 | keys[n] = NULL; |
319 | 0 | } |
320 | 0 | else |
321 | 0 | { |
322 | 0 | g_dbus_action_group_async_init (group); |
323 | 0 | keys = g_new0 (gchar *, 1); |
324 | 0 | } |
325 | |
|
326 | 0 | group->strict = TRUE; |
327 | |
|
328 | 0 | return keys; |
329 | 0 | } |
330 | | |
331 | | static gboolean |
332 | | g_dbus_action_group_query_action (GActionGroup *g_group, |
333 | | const gchar *action_name, |
334 | | gboolean *enabled, |
335 | | const GVariantType **parameter_type, |
336 | | const GVariantType **state_type, |
337 | | GVariant **state_hint, |
338 | | GVariant **state) |
339 | 0 | { |
340 | 0 | GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group); |
341 | 0 | ActionInfo *info; |
342 | |
|
343 | 0 | if (group->actions != NULL) |
344 | 0 | { |
345 | 0 | info = g_hash_table_lookup (group->actions, action_name); |
346 | |
|
347 | 0 | if (info == NULL) |
348 | 0 | { |
349 | 0 | group->strict = TRUE; |
350 | 0 | return FALSE; |
351 | 0 | } |
352 | | |
353 | 0 | if (enabled) |
354 | 0 | *enabled = info->enabled; |
355 | |
|
356 | 0 | if (parameter_type) |
357 | 0 | *parameter_type = info->parameter_type; |
358 | |
|
359 | 0 | if (state_type) |
360 | 0 | *state_type = info->state ? g_variant_get_type (info->state) : NULL; |
361 | |
|
362 | 0 | if (state_hint) |
363 | 0 | *state_hint = NULL; |
364 | |
|
365 | 0 | if (state) |
366 | 0 | *state = info->state ? g_variant_ref (info->state) : NULL; |
367 | |
|
368 | 0 | return TRUE; |
369 | 0 | } |
370 | 0 | else |
371 | 0 | { |
372 | 0 | g_dbus_action_group_async_init (group); |
373 | 0 | group->strict = TRUE; |
374 | |
|
375 | 0 | return FALSE; |
376 | 0 | } |
377 | 0 | } |
378 | | |
379 | | static void |
380 | | g_dbus_action_group_activate_action_full (GRemoteActionGroup *remote, |
381 | | const gchar *action_name, |
382 | | GVariant *parameter, |
383 | | GVariant *platform_data) |
384 | 0 | { |
385 | 0 | GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote); |
386 | 0 | GVariantBuilder builder; |
387 | |
|
388 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("av")); |
389 | |
|
390 | 0 | if (parameter) |
391 | 0 | g_variant_builder_add (&builder, "v", parameter); |
392 | |
|
393 | 0 | g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "Activate", |
394 | 0 | g_variant_new ("(sav@a{sv})", action_name, &builder, platform_data), |
395 | 0 | NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); |
396 | 0 | } |
397 | | |
398 | | static void |
399 | | g_dbus_action_group_change_action_state_full (GRemoteActionGroup *remote, |
400 | | const gchar *action_name, |
401 | | GVariant *value, |
402 | | GVariant *platform_data) |
403 | 0 | { |
404 | 0 | GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote); |
405 | |
|
406 | 0 | g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "SetState", |
407 | 0 | g_variant_new ("(sv@a{sv})", action_name, value, platform_data), |
408 | 0 | NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); |
409 | 0 | } |
410 | | |
411 | | static void |
412 | | g_dbus_action_group_change_state (GActionGroup *group, |
413 | | const gchar *action_name, |
414 | | GVariant *value) |
415 | 0 | { |
416 | 0 | g_dbus_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (group), |
417 | 0 | action_name, value, g_variant_new ("a{sv}", NULL)); |
418 | 0 | } |
419 | | |
420 | | static void |
421 | | g_dbus_action_group_activate (GActionGroup *group, |
422 | | const gchar *action_name, |
423 | | GVariant *parameter) |
424 | 0 | { |
425 | 0 | g_dbus_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (group), |
426 | 0 | action_name, parameter, g_variant_new ("a{sv}", NULL)); |
427 | 0 | } |
428 | | |
429 | | static void |
430 | | g_dbus_action_group_finalize (GObject *object) |
431 | 0 | { |
432 | 0 | GDBusActionGroup *group = G_DBUS_ACTION_GROUP (object); |
433 | |
|
434 | 0 | if (group->subscription_id) |
435 | 0 | g_dbus_connection_signal_unsubscribe (group->connection, group->subscription_id); |
436 | |
|
437 | 0 | if (group->actions) |
438 | 0 | g_hash_table_unref (group->actions); |
439 | |
|
440 | 0 | g_object_unref (group->connection); |
441 | 0 | g_free (group->object_path); |
442 | 0 | g_free (group->bus_name); |
443 | |
|
444 | 0 | G_OBJECT_CLASS (g_dbus_action_group_parent_class) |
445 | 0 | ->finalize (object); |
446 | 0 | } |
447 | | |
448 | | static void |
449 | | g_dbus_action_group_init (GDBusActionGroup *group) |
450 | 0 | { |
451 | 0 | } |
452 | | |
453 | | static void |
454 | | g_dbus_action_group_class_init (GDBusActionGroupClass *class) |
455 | 0 | { |
456 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (class); |
457 | |
|
458 | 0 | object_class->finalize = g_dbus_action_group_finalize; |
459 | 0 | } |
460 | | |
461 | | static void |
462 | | g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface) |
463 | 0 | { |
464 | 0 | iface->activate_action_full = g_dbus_action_group_activate_action_full; |
465 | 0 | iface->change_action_state_full = g_dbus_action_group_change_action_state_full; |
466 | 0 | } |
467 | | |
468 | | static void |
469 | | g_dbus_action_group_iface_init (GActionGroupInterface *iface) |
470 | 0 | { |
471 | 0 | iface->list_actions = g_dbus_action_group_list_actions; |
472 | 0 | iface->query_action = g_dbus_action_group_query_action; |
473 | 0 | iface->change_action_state = g_dbus_action_group_change_state; |
474 | 0 | iface->activate_action = g_dbus_action_group_activate; |
475 | 0 | } |
476 | | |
477 | | /** |
478 | | * g_dbus_action_group_get: |
479 | | * @connection: A #GDBusConnection |
480 | | * @bus_name: (nullable): the bus name which exports the action |
481 | | * group or %NULL if @connection is not a message bus connection |
482 | | * @object_path: the object path at which the action group is exported |
483 | | * |
484 | | * Obtains a #GDBusActionGroup for the action group which is exported at |
485 | | * the given @bus_name and @object_path. |
486 | | * |
487 | | * The thread default main context is taken at the time of this call. |
488 | | * All signals on the menu model (and any linked models) are reported |
489 | | * with respect to this context. All calls on the returned menu model |
490 | | * (and linked models) must also originate from this same context, with |
491 | | * the thread default main context unchanged. |
492 | | * |
493 | | * This call is non-blocking. The returned action group may or may not |
494 | | * already be filled in. The correct thing to do is connect the signals |
495 | | * for the action group to monitor for changes and then to call |
496 | | * g_action_group_list_actions() to get the initial list. |
497 | | * |
498 | | * Returns: (transfer full): a #GDBusActionGroup |
499 | | * |
500 | | * Since: 2.32 |
501 | | */ |
502 | | GDBusActionGroup * |
503 | | g_dbus_action_group_get (GDBusConnection *connection, |
504 | | const gchar *bus_name, |
505 | | const gchar *object_path) |
506 | 0 | { |
507 | 0 | GDBusActionGroup *group; |
508 | |
|
509 | 0 | g_return_val_if_fail (bus_name != NULL || g_dbus_connection_get_unique_name (connection) == NULL, NULL); |
510 | | |
511 | 0 | group = g_object_new (G_TYPE_DBUS_ACTION_GROUP, NULL); |
512 | 0 | group->connection = g_object_ref (connection); |
513 | 0 | group->bus_name = g_strdup (bus_name); |
514 | 0 | group->object_path = g_strdup (object_path); |
515 | |
|
516 | 0 | return group; |
517 | 0 | } |
518 | | |
519 | | gboolean |
520 | | g_dbus_action_group_sync (GDBusActionGroup *group, |
521 | | GCancellable *cancellable, |
522 | | GError **error) |
523 | 0 | { |
524 | 0 | GVariant *reply; |
525 | |
|
526 | 0 | g_assert (group->subscription_id == 0); |
527 | | |
528 | 0 | group->subscription_id = |
529 | 0 | g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path, |
530 | 0 | NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL); |
531 | |
|
532 | 0 | reply = g_dbus_connection_call_sync (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", |
533 | 0 | "DescribeAll", NULL, G_VARIANT_TYPE ("(a{s(bgav)})"), |
534 | 0 | G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error); |
535 | |
|
536 | 0 | if (reply != NULL) |
537 | 0 | { |
538 | 0 | GVariantIter *iter; |
539 | 0 | ActionInfo *action; |
540 | |
|
541 | 0 | g_assert (group->actions == NULL); |
542 | 0 | group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free); |
543 | |
|
544 | 0 | g_variant_get (reply, "(a{s(bgav)})", &iter); |
545 | 0 | while ((action = action_info_new_from_iter (iter))) |
546 | 0 | g_hash_table_insert (group->actions, action->name, action); |
547 | 0 | g_variant_iter_free (iter); |
548 | 0 | g_variant_unref (reply); |
549 | 0 | } |
550 | | |
551 | 0 | return reply != NULL; |
552 | 0 | } |