2 * Copyright © 2011 Canonical Ltd.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gmenuexporter.h"
26 #include "gdbusmethodinvocation.h"
27 #include "gdbusintrospection.h"
28 #include "gdbusnamewatching.h"
29 #include "gdbuserror.h"
31 /* {{{1 D-Bus Interface description */
33 /* For documentation of this interface, see
34 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
37 static GDBusInterfaceInfo *
38 org_gtk_Menus_get_interface (void)
40 static GDBusInterfaceInfo *interface_info;
41 static gsize interface_info_initialized = 0;
43 if (g_once_init_enter (&interface_info_initialized))
48 info = g_dbus_node_info_new_for_xml ("<node>"
49 " <interface name='org.gtk.Menus'>"
50 " <method name='Start'>"
51 " <arg type='au' name='groups' direction='in'/>"
52 " <arg type='a(uuaa{sv})' name='content' direction='out'/>"
54 " <method name='End'>"
55 " <arg type='au' name='groups' direction='in'/>"
57 " <signal name='Changed'>"
58 " arg type='a(uuuuaa{sv})' name='changes'/>"
63 g_error ("%s", error->message);
64 interface_info = g_dbus_node_info_lookup_interface (info, "org.gtk.Menus");
65 g_assert (interface_info != NULL);
66 g_dbus_interface_info_ref (interface_info);
67 g_dbus_node_info_unref (info);
69 g_once_init_leave (&interface_info_initialized, 1);
72 return interface_info;
75 /* {{{1 Forward declarations */
76 typedef struct _GMenuExporterMenu GMenuExporterMenu;
77 typedef struct _GMenuExporterLink GMenuExporterLink;
78 typedef struct _GMenuExporterGroup GMenuExporterGroup;
79 typedef struct _GMenuExporterRemote GMenuExporterRemote;
80 typedef struct _GMenuExporterWatch GMenuExporterWatch;
81 typedef struct _GMenuExporter GMenuExporter;
83 static gboolean g_menu_exporter_group_is_subscribed (GMenuExporterGroup *group);
84 static guint g_menu_exporter_group_get_id (GMenuExporterGroup *group);
85 static GMenuExporter * g_menu_exporter_group_get_exporter (GMenuExporterGroup *group);
86 static GMenuExporterMenu * g_menu_exporter_group_add_menu (GMenuExporterGroup *group,
88 static void g_menu_exporter_group_remove_menu (GMenuExporterGroup *group,
91 static GMenuExporterGroup * g_menu_exporter_create_group (GMenuExporter *exporter);
92 static GMenuExporterGroup * g_menu_exporter_lookup_group (GMenuExporter *exporter,
94 static void g_menu_exporter_report (GMenuExporter *exporter,
96 static void g_menu_exporter_remove_group (GMenuExporter *exporter,
99 /* {{{1 GMenuExporterLink, GMenuExporterMenu */
101 struct _GMenuExporterMenu
103 GMenuExporterGroup *group;
108 GSequence *item_links;
111 struct _GMenuExporterLink
114 GMenuExporterMenu *menu;
115 GMenuExporterLink *next;
119 g_menu_exporter_menu_free (GMenuExporterMenu *menu)
121 g_menu_exporter_group_remove_menu (menu->group, menu->id);
123 if (menu->handler_id != 0)
124 g_signal_handler_disconnect (menu->model, menu->handler_id);
126 if (menu->item_links != NULL)
127 g_sequence_free (menu->item_links);
129 g_object_unref (menu->model);
131 g_slice_free (GMenuExporterMenu, menu);
135 g_menu_exporter_link_free (gpointer data)
137 GMenuExporterLink *link = data;
141 GMenuExporterLink *tmp = link;
144 g_menu_exporter_menu_free (tmp->menu);
147 g_slice_free (GMenuExporterLink, tmp);
151 static GMenuExporterLink *
152 g_menu_exporter_menu_create_links (GMenuExporterMenu *menu,
155 GMenuExporterLink *list = NULL;
160 iter = g_menu_model_iterate_item_links (menu->model, position);
162 while (g_menu_link_iter_get_next (iter, &name, &model))
164 GMenuExporterGroup *group;
165 GMenuExporterLink *tmp;
167 /* keep sections in the same group, but create new groups
170 if (!g_str_equal (name, "section"))
171 group = g_menu_exporter_create_group (g_menu_exporter_group_get_exporter (menu->group));
175 tmp = g_slice_new (GMenuExporterLink);
176 tmp->name = g_strconcat (":", name, NULL);
177 tmp->menu = g_menu_exporter_group_add_menu (group, model);
181 g_object_unref (model);
184 g_object_unref (iter);
190 g_menu_exporter_menu_describe_item (GMenuExporterMenu *menu,
193 GMenuAttributeIter *attr_iter;
194 GVariantBuilder builder;
196 GMenuExporterLink *link;
200 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
202 attr_iter = g_menu_model_iterate_item_attributes (menu->model, position);
203 while (g_menu_attribute_iter_get_next (attr_iter, &name, &value))
205 g_variant_builder_add (&builder, "{sv}", name, value);
206 g_variant_unref (value);
208 g_object_unref (attr_iter);
210 iter = g_sequence_get_iter_at_pos (menu->item_links, position);
211 for (link = g_sequence_get (iter); link; link = link->next)
212 g_variant_builder_add (&builder, "{sv}", link->name,
213 g_variant_new ("(uu)", g_menu_exporter_group_get_id (link->menu->group), link->menu->id));
215 return g_variant_builder_end (&builder);
219 g_menu_exporter_menu_list (GMenuExporterMenu *menu)
221 GVariantBuilder builder;
224 g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
226 n = g_sequence_get_length (menu->item_links);
227 for (i = 0; i < n; i++)
228 g_variant_builder_add_value (&builder, g_menu_exporter_menu_describe_item (menu, i));
230 return g_variant_builder_end (&builder);
234 g_menu_exporter_menu_items_changed (GMenuModel *model,
240 GMenuExporterMenu *menu = user_data;
241 GSequenceIter *point;
243 #ifndef G_DISABLE_ASSERT
247 g_assert (menu->model == model);
248 g_assert (menu->item_links != NULL);
250 #ifndef G_DISABLE_ASSERT
251 n_items = g_sequence_get_length (menu->item_links);
253 g_assert (position >= 0 && position < G_MENU_EXPORTER_MAX_SECTION_SIZE);
254 g_assert (removed >= 0 && removed < G_MENU_EXPORTER_MAX_SECTION_SIZE);
255 g_assert (added < G_MENU_EXPORTER_MAX_SECTION_SIZE);
256 g_assert (position + removed <= n_items);
257 g_assert (n_items - removed + added < G_MENU_EXPORTER_MAX_SECTION_SIZE);
259 point = g_sequence_get_iter_at_pos (menu->item_links, position + removed);
260 g_sequence_remove_range (g_sequence_get_iter_at_pos (menu->item_links, position), point);
262 for (i = position; i < position + added; i++)
263 g_sequence_insert_before (point, g_menu_exporter_menu_create_links (menu, i));
265 if (g_menu_exporter_group_is_subscribed (menu->group))
267 GVariantBuilder builder;
269 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(uuuuaa{sv})"));
270 g_variant_builder_add (&builder, "u", g_menu_exporter_group_get_id (menu->group));
271 g_variant_builder_add (&builder, "u", menu->id);
272 g_variant_builder_add (&builder, "u", position);
273 g_variant_builder_add (&builder, "u", removed);
275 g_variant_builder_open (&builder, G_VARIANT_TYPE ("aa{sv}"));
276 for (i = position; i < position + added; i++)
277 g_variant_builder_add_value (&builder, g_menu_exporter_menu_describe_item (menu, i));
278 g_variant_builder_close (&builder);
280 g_menu_exporter_report (g_menu_exporter_group_get_exporter (menu->group), g_variant_builder_end (&builder));
285 g_menu_exporter_menu_prepare (GMenuExporterMenu *menu)
289 g_assert (menu->item_links == NULL);
291 if (g_menu_model_is_mutable (menu->model))
292 menu->handler_id = g_signal_connect (menu->model, "items-changed",
293 G_CALLBACK (g_menu_exporter_menu_items_changed), menu);
295 menu->item_links = g_sequence_new (g_menu_exporter_link_free);
297 n_items = g_menu_model_get_n_items (menu->model);
299 g_menu_exporter_menu_items_changed (menu->model, 0, 0, n_items, menu);
302 static GMenuExporterMenu *
303 g_menu_exporter_menu_new (GMenuExporterGroup *group,
307 GMenuExporterMenu *menu;
309 menu = g_slice_new0 (GMenuExporterMenu);
312 menu->model = g_object_ref (model);
317 /* {{{1 GMenuExporterGroup */
319 struct _GMenuExporterGroup
321 GMenuExporter *exporter;
332 g_menu_exporter_group_check_if_useless (GMenuExporterGroup *group)
334 if (g_hash_table_size (group->menus) == 0 && group->subscribed == 0)
336 g_menu_exporter_remove_group (group->exporter, group->id);
338 g_hash_table_unref (group->menus);
340 g_slice_free (GMenuExporterGroup, group);
345 g_menu_exporter_group_subscribe (GMenuExporterGroup *group,
346 GVariantBuilder *builder)
351 if (!group->prepared)
353 GMenuExporterMenu *menu;
355 /* set this first, so that any menus created during the
356 * preparation of the first menu also end up in the prepared
359 group->prepared = TRUE;
361 menu = g_hash_table_lookup (group->menus, 0);
363 /* If the group was created by a subscription and does not yet
364 * exist, it won't have a root menu...
366 * That menu will be prepared if it is ever added (due to
367 * group->prepared == TRUE).
370 g_menu_exporter_menu_prepare (menu);
375 g_hash_table_iter_init (&iter, group->menus);
376 while (g_hash_table_iter_next (&iter, &key, &val))
378 guint id = GPOINTER_TO_INT (key);
379 GMenuExporterMenu *menu = val;
381 if (!g_sequence_is_empty (menu->item_links))
383 g_variant_builder_open (builder, G_VARIANT_TYPE ("(uuaa{sv})"));
384 g_variant_builder_add (builder, "u", group->id);
385 g_variant_builder_add (builder, "u", id);
386 g_variant_builder_add_value (builder, g_menu_exporter_menu_list (menu));
387 g_variant_builder_close (builder);
393 g_menu_exporter_group_unsubscribe (GMenuExporterGroup *group,
396 g_assert (group->subscribed >= count);
398 group->subscribed -= count;
400 g_menu_exporter_group_check_if_useless (group);
403 static GMenuExporter *
404 g_menu_exporter_group_get_exporter (GMenuExporterGroup *group)
406 return group->exporter;
410 g_menu_exporter_group_is_subscribed (GMenuExporterGroup *group)
412 return group->subscribed > 0;
416 g_menu_exporter_group_get_id (GMenuExporterGroup *group)
422 g_menu_exporter_group_remove_menu (GMenuExporterGroup *group,
425 g_hash_table_remove (group->menus, GINT_TO_POINTER (id));
427 g_menu_exporter_group_check_if_useless (group);
430 static GMenuExporterMenu *
431 g_menu_exporter_group_add_menu (GMenuExporterGroup *group,
434 GMenuExporterMenu *menu;
437 id = group->next_menu_id++;
438 menu = g_menu_exporter_menu_new (group, id, model);
439 g_hash_table_insert (group->menus, GINT_TO_POINTER (id), menu);
442 g_menu_exporter_menu_prepare (menu);
447 static GMenuExporterGroup *
448 g_menu_exporter_group_new (GMenuExporter *exporter,
451 GMenuExporterGroup *group;
453 group = g_slice_new0 (GMenuExporterGroup);
454 group->menus = g_hash_table_new (NULL, NULL);
455 group->exporter = exporter;
461 /* {{{1 GMenuExporterRemote */
463 struct _GMenuExporterRemote
465 GMenuExporter *exporter;
471 g_menu_exporter_remote_subscribe (GMenuExporterRemote *remote,
473 GVariantBuilder *builder)
475 GMenuExporterGroup *group;
478 count = (gsize) g_hash_table_lookup (remote->watches, GINT_TO_POINTER (group_id));
479 g_hash_table_insert (remote->watches, GINT_TO_POINTER (group_id), GINT_TO_POINTER (count + 1));
481 /* Group will be created (as empty/unsubscribed if it does not exist) */
482 group = g_menu_exporter_lookup_group (remote->exporter, group_id);
483 g_menu_exporter_group_subscribe (group, builder);
487 g_menu_exporter_remote_unsubscribe (GMenuExporterRemote *remote,
490 GMenuExporterGroup *group;
493 count = (gsize) g_hash_table_lookup (remote->watches, GINT_TO_POINTER (group_id));
499 g_hash_table_insert (remote->watches, GINT_TO_POINTER (group_id), GINT_TO_POINTER (count - 1));
501 g_hash_table_remove (remote->watches, GINT_TO_POINTER (group_id));
503 group = g_menu_exporter_lookup_group (remote->exporter, group_id);
504 g_menu_exporter_group_unsubscribe (group, 1);
508 g_menu_exporter_remote_has_subscriptions (GMenuExporterRemote *remote)
510 return g_hash_table_size (remote->watches) != 0;
514 g_menu_exporter_remote_free (gpointer data)
516 GMenuExporterRemote *remote = data;
520 g_hash_table_iter_init (&iter, remote->watches);
521 while (g_hash_table_iter_next (&iter, &key, &val))
523 GMenuExporterGroup *group;
525 group = g_menu_exporter_lookup_group (remote->exporter, GPOINTER_TO_INT (key));
526 g_menu_exporter_group_unsubscribe (group, GPOINTER_TO_INT (val));
529 if (remote->watch_id > 0)
530 g_bus_unwatch_name (remote->watch_id);
532 g_hash_table_unref (remote->watches);
534 g_slice_free (GMenuExporterRemote, remote);
537 static GMenuExporterRemote *
538 g_menu_exporter_remote_new (GMenuExporter *exporter,
541 GMenuExporterRemote *remote;
543 remote = g_slice_new0 (GMenuExporterRemote);
544 remote->exporter = exporter;
545 remote->watches = g_hash_table_new (NULL, NULL);
546 remote->watch_id = watch_id;
551 /* {{{1 GMenuExporter */
553 struct _GMenuExporter
555 GDBusConnection *connection;
557 guint registration_id;
561 GMenuExporterMenu *root;
562 GMenuExporterRemote *peer_remote;
567 g_menu_exporter_name_vanished (GDBusConnection *connection,
571 GMenuExporter *exporter = user_data;
573 /* connection == NULL when we get called because the connection closed */
574 g_assert (exporter->connection == connection || connection == NULL);
576 g_hash_table_remove (exporter->remotes, name);
580 g_menu_exporter_subscribe (GMenuExporter *exporter,
584 GMenuExporterRemote *remote;
585 GVariantBuilder builder;
590 remote = g_hash_table_lookup (exporter->remotes, sender);
592 remote = exporter->peer_remote;
600 watch_id = g_bus_watch_name_on_connection (exporter->connection, sender, G_BUS_NAME_WATCHER_FLAGS_NONE,
601 NULL, g_menu_exporter_name_vanished, exporter, NULL);
602 remote = g_menu_exporter_remote_new (exporter, watch_id);
603 g_hash_table_insert (exporter->remotes, g_strdup (sender), remote);
606 remote = exporter->peer_remote =
607 g_menu_exporter_remote_new (exporter, 0);
610 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a(uuaa{sv}))"));
612 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(uuaa{sv})"));
614 g_variant_iter_init (&iter, group_ids);
615 while (g_variant_iter_next (&iter, "u", &id))
616 g_menu_exporter_remote_subscribe (remote, id, &builder);
618 g_variant_builder_close (&builder);
620 return g_variant_builder_end (&builder);
624 g_menu_exporter_unsubscribe (GMenuExporter *exporter,
628 GMenuExporterRemote *remote;
633 remote = g_hash_table_lookup (exporter->remotes, sender);
635 remote = exporter->peer_remote;
640 g_variant_iter_init (&iter, group_ids);
641 while (g_variant_iter_next (&iter, "u", &id))
642 g_menu_exporter_remote_unsubscribe (remote, id);
644 if (!g_menu_exporter_remote_has_subscriptions (remote))
647 g_hash_table_remove (exporter->remotes, sender);
649 g_clear_pointer (&exporter->peer_remote, g_menu_exporter_remote_free);
654 g_menu_exporter_report (GMenuExporter *exporter,
657 GVariantBuilder builder;
659 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
660 g_variant_builder_open (&builder, G_VARIANT_TYPE_ARRAY);
661 g_variant_builder_add_value (&builder, report);
662 g_variant_builder_close (&builder);
664 g_dbus_connection_emit_signal (exporter->connection,
666 exporter->object_path,
667 "org.gtk.Menus", "Changed",
668 g_variant_builder_end (&builder),
673 g_menu_exporter_remove_group (GMenuExporter *exporter,
676 g_hash_table_remove (exporter->groups, GINT_TO_POINTER (id));
679 static GMenuExporterGroup *
680 g_menu_exporter_lookup_group (GMenuExporter *exporter,
683 GMenuExporterGroup *group;
685 group = g_hash_table_lookup (exporter->groups, GINT_TO_POINTER (group_id));
689 group = g_menu_exporter_group_new (exporter, group_id);
690 g_hash_table_insert (exporter->groups, GINT_TO_POINTER (group_id), group);
696 static GMenuExporterGroup *
697 g_menu_exporter_create_group (GMenuExporter *exporter)
699 GMenuExporterGroup *group;
702 id = exporter->next_group_id++;
703 group = g_menu_exporter_group_new (exporter, id);
704 g_hash_table_insert (exporter->groups, GINT_TO_POINTER (id), group);
710 g_menu_exporter_free (GMenuExporter *exporter)
712 g_clear_pointer (&exporter->root, g_menu_exporter_menu_free);
713 g_clear_pointer (&exporter->peer_remote, g_menu_exporter_remote_free);
714 g_hash_table_unref (exporter->remotes);
715 g_hash_table_unref (exporter->groups);
716 g_object_unref (exporter->connection);
717 g_free (exporter->object_path);
719 g_slice_free (GMenuExporter, exporter);
723 g_menu_exporter_method_call (GDBusConnection *connection,
725 const gchar *object_path,
726 const gchar *interface_name,
727 const gchar *method_name,
728 GVariant *parameters,
729 GDBusMethodInvocation *invocation,
732 GMenuExporter *exporter = user_data;
735 group_ids = g_variant_get_child_value (parameters, 0);
737 if (g_str_equal (method_name, "Start"))
738 g_dbus_method_invocation_return_value (invocation, g_menu_exporter_subscribe (exporter, sender, group_ids));
740 else if (g_str_equal (method_name, "End"))
742 g_menu_exporter_unsubscribe (exporter, sender, group_ids);
743 g_dbus_method_invocation_return_value (invocation, NULL);
747 g_assert_not_reached ();
749 g_variant_unref (group_ids);
752 /* {{{1 Public API */
755 * g_dbus_connection_export_menu_model:
756 * @connection: a #GDBusConnection
757 * @object_path: a D-Bus object path
758 * @menu: a #GMenuModel
759 * @error: return location for an error, or %NULL
761 * Exports @menu on @connection at @object_path.
763 * The implemented D-Bus API should be considered private.
764 * It is subject to change in the future.
766 * An object path can only have one menu model exported on it. If this
767 * constraint is violated, the export will fail and 0 will be
768 * returned (with @error set accordingly).
770 * Exporting menus with sections containing more than
771 * %G_MENU_EXPORTER_MAX_SECTION_SIZE items is not supported and results in
772 * undefined behavior.
774 * You can unexport the menu model using
775 * g_dbus_connection_unexport_menu_model() with the return value of
778 * Returns: the ID of the export (never zero), or 0 in case of failure
783 g_dbus_connection_export_menu_model (GDBusConnection *connection,
784 const gchar *object_path,
788 const GDBusInterfaceVTable vtable = {
789 g_menu_exporter_method_call, NULL, NULL, { 0 }
791 GMenuExporter *exporter;
794 exporter = g_slice_new0 (GMenuExporter);
795 exporter->connection = g_object_ref (connection);
796 exporter->object_path = g_strdup (object_path);
797 exporter->groups = g_hash_table_new (NULL, NULL);
798 exporter->remotes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_menu_exporter_remote_free);
800 id = g_dbus_connection_register_object (connection, object_path, org_gtk_Menus_get_interface (),
801 &vtable, exporter, (GDestroyNotify) g_menu_exporter_free, error);
804 exporter->root = g_menu_exporter_group_add_menu (g_menu_exporter_create_group (exporter), menu);
810 * g_dbus_connection_unexport_menu_model:
811 * @connection: a #GDBusConnection
812 * @export_id: the ID from g_dbus_connection_export_menu_model()
814 * Reverses the effect of a previous call to
815 * g_dbus_connection_export_menu_model().
817 * It is an error to call this function with an ID that wasn't returned
818 * from g_dbus_connection_export_menu_model() or to call it with the
819 * same ID more than once.
824 g_dbus_connection_unexport_menu_model (GDBusConnection *connection,
827 g_dbus_connection_unregister_object (connection, export_id);
831 /* vim:set foldmethod=marker: */