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