2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
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 * GActionGroupExporter:
37 * These functions support exporting a [class@Gio.ActionGroup] on D-Bus.
38 * The D-Bus interface that is used is a private implementation
41 * To access an exported `GActionGroup` remotely, use
42 * [method@Gio.DBusActionGroup.get] to obtain a [class@Gio.DBusActionGroup].
46 g_action_group_describe_action (GActionGroup *action_group,
49 const GVariantType *type;
50 GVariantBuilder builder;
54 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(bgav)"));
56 enabled = g_action_group_get_action_enabled (action_group, name);
57 g_variant_builder_add (&builder, "b", enabled);
59 if ((type = g_action_group_get_action_parameter_type (action_group, name)))
61 gchar *str = g_variant_type_dup_string (type);
62 g_variant_builder_add (&builder, "g", str);
66 g_variant_builder_add (&builder, "g", "");
68 g_variant_builder_open (&builder, G_VARIANT_TYPE ("av"));
69 if ((state = g_action_group_get_action_state (action_group, name)))
71 g_variant_builder_add (&builder, "v", state);
72 g_variant_unref (state);
74 g_variant_builder_close (&builder);
76 return g_variant_builder_end (&builder);
79 /* Using XML saves us dozens of relocations vs. using the introspection
80 * structure types. We only need to burn cycles and memory if we
81 * actually use the exporter -- not in every single app using GIO.
83 * It's also a lot easier to read. :)
85 * For documentation of this interface, see
86 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
88 const char org_gtk_Actions_xml[] =
90 " <interface name='org.gtk.Actions'>"
91 " <method name='List'>"
92 " <arg type='as' name='list' direction='out'/>"
94 " <method name='Describe'>"
95 " <arg type='s' name='action_name' direction='in'/>"
96 " <arg type='(bgav)' name='description' direction='out'/>"
98 " <method name='DescribeAll'>"
99 " <arg type='a{s(bgav)}' name='descriptions' direction='out'/>"
101 " <method name='Activate'>"
102 " <arg type='s' name='action_name' direction='in'/>"
103 " <arg type='av' name='parameter' direction='in'/>"
104 " <arg type='a{sv}' name='platform_data' direction='in'/>"
106 " <method name='SetState'>"
107 " <arg type='s' name='action_name' direction='in'/>"
108 " <arg type='v' name='value' direction='in'/>"
109 " <arg type='a{sv}' name='platform_data' direction='in'/>"
111 " <signal name='Changed'>"
112 " <arg type='as' name='removals'/>"
113 " <arg type='a{sb}' name='enable_changes'/>"
114 " <arg type='a{sv}' name='state_changes'/>"
115 " <arg type='a{s(bgav)}' name='additions'/>"
120 static GDBusInterfaceInfo *org_gtk_Actions;
124 GActionGroup *action_group;
125 GDBusConnection *connection;
126 GMainContext *context;
128 GHashTable *pending_changes;
129 GSource *pending_source;
130 } GActionGroupExporter;
132 #define ACTION_ADDED_EVENT (1u<<0)
133 #define ACTION_REMOVED_EVENT (1u<<1)
134 #define ACTION_STATE_CHANGED_EVENT (1u<<2)
135 #define ACTION_ENABLED_CHANGED_EVENT (1u<<3)
138 g_action_group_exporter_dispatch_events (gpointer user_data)
140 GActionGroupExporter *exporter = user_data;
141 GVariantBuilder removes;
142 GVariantBuilder enabled_changes;
143 GVariantBuilder state_changes;
144 GVariantBuilder adds;
149 g_variant_builder_init (&removes, G_VARIANT_TYPE_STRING_ARRAY);
150 g_variant_builder_init (&enabled_changes, G_VARIANT_TYPE ("a{sb}"));
151 g_variant_builder_init (&state_changes, G_VARIANT_TYPE ("a{sv}"));
152 g_variant_builder_init (&adds, G_VARIANT_TYPE ("a{s(bgav)}"));
154 g_hash_table_iter_init (&iter, exporter->pending_changes);
155 while (g_hash_table_iter_next (&iter, &key, &value))
157 guint events = GPOINTER_TO_INT (value);
158 const gchar *name = key;
160 /* Adds and removes are incompatible with enabled or state
161 * changes, but we must report at least one event.
163 g_assert (((events & (ACTION_ENABLED_CHANGED_EVENT | ACTION_STATE_CHANGED_EVENT)) == 0) !=
164 ((events & (ACTION_REMOVED_EVENT | ACTION_ADDED_EVENT)) == 0));
166 if (events & ACTION_REMOVED_EVENT)
167 g_variant_builder_add (&removes, "s", name);
169 if (events & ACTION_ENABLED_CHANGED_EVENT)
173 enabled = g_action_group_get_action_enabled (exporter->action_group, name);
174 g_variant_builder_add (&enabled_changes, "{sb}", name, enabled);
177 if (events & ACTION_STATE_CHANGED_EVENT)
181 state = g_action_group_get_action_state (exporter->action_group, name);
182 g_variant_builder_add (&state_changes, "{sv}", name, state);
183 g_variant_unref (state);
186 if (events & ACTION_ADDED_EVENT)
188 GVariant *description;
190 description = g_action_group_describe_action (exporter->action_group, name);
191 g_variant_builder_add (&adds, "{s@(bgav)}", name, description);
195 g_hash_table_remove_all (exporter->pending_changes);
197 g_dbus_connection_emit_signal (exporter->connection, NULL, exporter->object_path,
198 "org.gtk.Actions", "Changed",
199 g_variant_new ("(asa{sb}a{sv}a{s(bgav)})",
200 &removes, &enabled_changes,
201 &state_changes, &adds),
204 exporter->pending_source = NULL;
210 g_action_group_exporter_flush_queue (GActionGroupExporter *exporter)
212 if (exporter->pending_source)
214 g_source_destroy (exporter->pending_source);
215 g_action_group_exporter_dispatch_events (exporter);
216 g_assert (exporter->pending_source == NULL);
221 g_action_group_exporter_get_events (GActionGroupExporter *exporter,
224 return (gsize) g_hash_table_lookup (exporter->pending_changes, name);
228 g_action_group_exporter_set_events (GActionGroupExporter *exporter,
232 gboolean have_events;
236 g_hash_table_insert (exporter->pending_changes, g_strdup (name), GINT_TO_POINTER (events));
238 g_hash_table_remove (exporter->pending_changes, name);
240 have_events = g_hash_table_size (exporter->pending_changes) > 0;
241 is_queued = exporter->pending_source != NULL;
243 if (have_events && !is_queued)
247 source = g_idle_source_new ();
248 exporter->pending_source = source;
249 g_source_set_callback (source, g_action_group_exporter_dispatch_events, exporter, NULL);
250 g_source_set_static_name (source, "[gio] g_action_group_exporter_dispatch_events");
251 g_source_attach (source, exporter->context);
252 g_source_unref (source);
255 if (!have_events && is_queued)
257 g_source_destroy (exporter->pending_source);
258 exporter->pending_source = NULL;
263 g_action_group_exporter_action_added (GActionGroup *action_group,
264 const gchar *action_name,
267 GActionGroupExporter *exporter = user_data;
270 event_mask = g_action_group_exporter_get_events (exporter, action_name);
272 /* The action is new, so we should not have any pending
273 * enabled-changed or state-changed signals for it.
275 g_assert (~event_mask & (ACTION_STATE_CHANGED_EVENT |
276 ACTION_ENABLED_CHANGED_EVENT));
278 event_mask |= ACTION_ADDED_EVENT;
280 g_action_group_exporter_set_events (exporter, action_name, event_mask);
284 g_action_group_exporter_action_removed (GActionGroup *action_group,
285 const gchar *action_name,
288 GActionGroupExporter *exporter = user_data;
291 event_mask = g_action_group_exporter_get_events (exporter, action_name);
293 /* If the add event for this is still queued then just cancel it since
296 * If the event was freshly added, there should not have been any
297 * enabled or state changed events.
299 if (event_mask & ACTION_ADDED_EVENT)
301 g_assert (~event_mask & ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT));
302 event_mask &= ~ACTION_ADDED_EVENT;
305 /* Otherwise, queue a remove event. Drop any state or enabled changes
306 * that were queued before the remove. */
309 event_mask |= ACTION_REMOVED_EVENT;
310 event_mask &= ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT);
313 g_action_group_exporter_set_events (exporter, action_name, event_mask);
317 g_action_group_exporter_action_state_changed (GActionGroup *action_group,
318 const gchar *action_name,
322 GActionGroupExporter *exporter = user_data;
325 event_mask = g_action_group_exporter_get_events (exporter, action_name);
327 /* If it was removed, it must have been added back. Otherwise, why
328 * are we hearing about changes?
330 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
331 event_mask & ACTION_ADDED_EVENT);
333 /* If it is freshly added, don't also bother with the state change
334 * signal since the updated state will be sent as part of the pending
337 if (~event_mask & ACTION_ADDED_EVENT)
338 event_mask |= ACTION_STATE_CHANGED_EVENT;
340 g_action_group_exporter_set_events (exporter, action_name, event_mask);
344 g_action_group_exporter_action_enabled_changed (GActionGroup *action_group,
345 const gchar *action_name,
349 GActionGroupExporter *exporter = user_data;
352 event_mask = g_action_group_exporter_get_events (exporter, action_name);
354 /* Reasoning as above. */
355 g_assert (~event_mask & ACTION_REMOVED_EVENT ||
356 event_mask & ACTION_ADDED_EVENT);
358 if (~event_mask & ACTION_ADDED_EVENT)
359 event_mask |= ACTION_ENABLED_CHANGED_EVENT;
361 g_action_group_exporter_set_events (exporter, action_name, event_mask);
365 org_gtk_Actions_method_call (GDBusConnection *connection,
367 const gchar *object_path,
368 const gchar *interface_name,
369 const gchar *method_name,
370 GVariant *parameters,
371 GDBusMethodInvocation *invocation,
374 GActionGroupExporter *exporter = user_data;
375 GVariant *result = NULL;
377 g_action_group_exporter_flush_queue (exporter);
379 if (g_str_equal (method_name, "List"))
383 list = g_action_group_list_actions (exporter->action_group);
384 result = g_variant_new ("(^as)", list);
388 else if (g_str_equal (method_name, "Describe"))
393 g_variant_get (parameters, "(&s)", &name);
395 if (!g_action_group_has_action (exporter->action_group, name))
397 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
398 "The named action ('%s') does not exist.", name);
402 desc = g_action_group_describe_action (exporter->action_group, name);
403 result = g_variant_new ("(@(bgav))", desc);
406 else if (g_str_equal (method_name, "DescribeAll"))
408 GVariantBuilder builder;
412 list = g_action_group_list_actions (exporter->action_group);
413 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{s(bgav)}"));
414 for (i = 0; list[i]; i++)
416 const gchar *name = list[i];
417 GVariant *description;
419 description = g_action_group_describe_action (exporter->action_group, name);
420 g_variant_builder_add (&builder, "{s@(bgav)}", name, description);
422 result = g_variant_new ("(a{s(bgav)})", &builder);
426 else if (g_str_equal (method_name, "Activate"))
428 GVariant *parameter = NULL;
429 GVariant *platform_data;
432 const GVariantType *parameter_type = NULL;
434 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
435 g_variant_iter_next (iter, "v", ¶meter);
436 g_variant_iter_free (iter);
438 /* Check the action exists and the parameter type matches. */
439 if (!g_action_group_query_action (exporter->action_group,
440 name, NULL, ¶meter_type,
443 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
444 "Unknown action ‘%s’", name);
445 g_clear_pointer (¶meter, g_variant_unref);
446 g_variant_unref (platform_data);
450 if (!((parameter_type == NULL && parameter == NULL) ||
451 (parameter_type != NULL && parameter != NULL && g_variant_is_of_type (parameter, parameter_type))))
453 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
454 "Invalid parameter for action ‘%s’: expected type %s but got type %s",
456 (parameter_type != NULL) ? (const gchar *) parameter_type : "()",
457 (parameter != NULL) ? g_variant_get_type_string (parameter) : "()");
458 g_clear_pointer (¶meter, g_variant_unref);
459 g_variant_unref (platform_data);
463 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
464 g_remote_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
465 name, parameter, platform_data);
467 g_action_group_activate_action (exporter->action_group, name, parameter);
470 g_variant_unref (parameter);
472 g_variant_unref (platform_data);
475 else if (g_str_equal (method_name, "SetState"))
477 GVariant *platform_data;
480 const GVariantType *state_type = NULL;
482 g_variant_get (parameters, "(&sv@a{sv})", &name, &state, &platform_data);
484 /* Check the action exists and the state type matches. */
485 if (!g_action_group_query_action (exporter->action_group,
487 &state_type, NULL, NULL))
489 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
490 "Unknown action ‘%s’", name);
491 g_variant_unref (state);
492 g_variant_unref (platform_data);
496 if (state_type == NULL)
498 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
499 "Cannot change state of action ‘%s’ as it is stateless", name);
500 g_variant_unref (state);
501 g_variant_unref (platform_data);
505 if (!g_variant_is_of_type (state, state_type))
507 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
508 "Invalid state for action ‘%s’: expected type %s but got type %s",
510 (const gchar *) state_type,
511 g_variant_get_type_string (state));
512 g_variant_unref (state);
513 g_variant_unref (platform_data);
517 if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
518 g_remote_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
519 name, state, platform_data);
521 g_action_group_change_action_state (exporter->action_group, name, state);
523 g_variant_unref (platform_data);
524 g_variant_unref (state);
528 g_assert_not_reached ();
530 g_dbus_method_invocation_return_value (invocation, result);
534 g_action_group_exporter_free (GActionGroupExporter *exporter)
536 g_signal_handlers_disconnect_by_func (exporter->action_group,
537 g_action_group_exporter_action_added, exporter);
538 g_signal_handlers_disconnect_by_func (exporter->action_group,
539 g_action_group_exporter_action_enabled_changed, exporter);
540 g_signal_handlers_disconnect_by_func (exporter->action_group,
541 g_action_group_exporter_action_state_changed, exporter);
542 g_signal_handlers_disconnect_by_func (exporter->action_group,
543 g_action_group_exporter_action_removed, exporter);
545 g_hash_table_unref (exporter->pending_changes);
546 if (exporter->pending_source)
547 g_source_destroy (exporter->pending_source);
549 g_main_context_unref (exporter->context);
550 g_object_unref (exporter->connection);
551 g_object_unref (exporter->action_group);
552 g_free (exporter->object_path);
554 g_slice_free (GActionGroupExporter, exporter);
558 * g_dbus_connection_export_action_group:
559 * @connection: a #GDBusConnection
560 * @object_path: a D-Bus object path
561 * @action_group: a #GActionGroup
562 * @error: a pointer to a %NULL #GError, or %NULL
564 * Exports @action_group on @connection at @object_path.
566 * The implemented D-Bus API should be considered private. It is
567 * subject to change in the future.
569 * A given object path can only have one action group exported on it.
570 * If this constraint is violated, the export will fail and 0 will be
571 * returned (with @error set accordingly).
573 * You can unexport the action group using
574 * g_dbus_connection_unexport_action_group() with the return value of
577 * The thread default main context is taken at the time of this call.
578 * All incoming action activations and state change requests are
579 * reported from this context. Any changes on the action group that
580 * cause it to emit signals must also come from this same context.
581 * Since incoming action activations and state change requests are
582 * rather likely to cause changes on the action group, this effectively
583 * limits a given action group to being exported from only one main
586 * Returns: the ID of the export (never zero), or 0 in case of failure
591 g_dbus_connection_export_action_group (GDBusConnection *connection,
592 const gchar *object_path,
593 GActionGroup *action_group,
596 const GDBusInterfaceVTable vtable = {
597 org_gtk_Actions_method_call, NULL, NULL, { 0 }
599 GActionGroupExporter *exporter;
602 if G_UNLIKELY (org_gtk_Actions == NULL)
604 GError *my_error = NULL;
607 info = g_dbus_node_info_new_for_xml (org_gtk_Actions_xml, &my_error);
608 if G_UNLIKELY (info == NULL)
609 g_error ("%s", my_error->message);
610 org_gtk_Actions = g_dbus_node_info_lookup_interface (info, "org.gtk.Actions");
611 g_assert (org_gtk_Actions != NULL);
612 g_dbus_interface_info_ref (org_gtk_Actions);
613 g_dbus_node_info_unref (info);
616 exporter = g_slice_new (GActionGroupExporter);
617 exporter->context = g_main_context_ref_thread_default ();
618 exporter->pending_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
619 exporter->pending_source = NULL;
620 exporter->action_group = g_object_ref (action_group);
621 exporter->connection = g_object_ref (connection);
622 exporter->object_path = g_strdup (object_path);
624 id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
625 exporter, (GDestroyNotify) g_action_group_exporter_free, error);
629 g_signal_connect (action_group, "action-added",
630 G_CALLBACK (g_action_group_exporter_action_added), exporter);
631 g_signal_connect (action_group, "action-removed",
632 G_CALLBACK (g_action_group_exporter_action_removed), exporter);
633 g_signal_connect (action_group, "action-state-changed",
634 G_CALLBACK (g_action_group_exporter_action_state_changed), exporter);
635 g_signal_connect (action_group, "action-enabled-changed",
636 G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
643 * g_dbus_connection_unexport_action_group:
644 * @connection: a #GDBusConnection
645 * @export_id: the ID from g_dbus_connection_export_action_group()
647 * Reverses the effect of a previous call to
648 * g_dbus_connection_export_action_group().
650 * It is an error to call this function with an ID that wasn't returned
651 * from g_dbus_connection_export_action_group() or to call it with the
652 * same ID more than once.
657 g_dbus_connection_unexport_action_group (GDBusConnection *connection,
660 g_dbus_connection_unregister_object (connection, export_id);