2 * Copyright © 2011 Canonical Ltd.
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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"
32 * SECTION:gmenuexporter
33 * @title: GMenuModel exporter
34 * @short_description: Export GMenuModels on D-Bus
35 * @see_also: #GMenuModel, #GDBusMenuModel
37 * These functions support exporting a #GMenuModel on D-Bus.
38 * The D-Bus interface that is used is a private implementation
41 * To access an exported #GMenuModel remotely, use
42 * g_dbus_menu_model_get() to obtain a #GDBusMenuModel.
45 /* {{{1 D-Bus Interface description */
47 /* For documentation of this interface, see
48 * http://live.gnome.org/GTK+/GApplication-dbus-apis
51 static GDBusInterfaceInfo *
52 org_gtk_Menus_get_interface (void)
54 static GDBusInterfaceInfo *interface_info;
56 if (interface_info == NULL)
61 info = g_dbus_node_info_new_for_xml ("<node>"
62 " <interface name='org.gtk.Menus'>"
63 " <method name='Start'>"
64 " <arg type='au' name='groups' direction='in'/>"
65 " <arg type='a(uuaa{sv})' name='content' direction='out'/>"
67 " <method name='End'>"
68 " <arg type='au' name='groups' direction='in'/>"
70 " <signal name='Changed'>"
71 " arg type='a(uuuuaa{sv})' name='changes'/>"
76 g_error ("%s\n", error->message);
77 interface_info = g_dbus_node_info_lookup_interface (info, "org.gtk.Menus");
78 g_assert (interface_info != NULL);
79 g_dbus_interface_info_ref (interface_info);
80 g_dbus_node_info_unref (info);
83 return interface_info;
86 /* {{{1 Forward declarations */
87 typedef struct _GMenuExporterMenu GMenuExporterMenu;
88 typedef struct _GMenuExporterLink GMenuExporterLink;
89 typedef struct _GMenuExporterGroup GMenuExporterGroup;
90 typedef struct _GMenuExporterRemote GMenuExporterRemote;
91 typedef struct _GMenuExporterWatch GMenuExporterWatch;
92 typedef struct _GMenuExporter GMenuExporter;
94 static gboolean g_menu_exporter_group_is_subscribed (GMenuExporterGroup *group);
95 static guint g_menu_exporter_group_get_id (GMenuExporterGroup *group);
96 static GMenuExporter * g_menu_exporter_group_get_exporter (GMenuExporterGroup *group);
97 static GMenuExporterMenu * g_menu_exporter_group_add_menu (GMenuExporterGroup *group,
99 static void g_menu_exporter_group_remove_menu (GMenuExporterGroup *group,
102 static GMenuExporterGroup * g_menu_exporter_create_group (GMenuExporter *exporter);
103 static GMenuExporterGroup * g_menu_exporter_lookup_group (GMenuExporter *exporter,
105 static void g_menu_exporter_report (GMenuExporter *exporter,
107 static void g_menu_exporter_remove_group (GMenuExporter *exporter,
110 /* {{{1 GMenuExporterLink, GMenuExporterMenu */
112 struct _GMenuExporterMenu
114 GMenuExporterGroup *group;
119 GSequence *item_links;
122 struct _GMenuExporterLink
125 GMenuExporterMenu *menu;
126 GMenuExporterLink *next;
130 g_menu_exporter_menu_free (GMenuExporterMenu *menu)
132 g_menu_exporter_group_remove_menu (menu->group, menu->id);
134 if (menu->handler_id != 0)
135 g_signal_handler_disconnect (menu->model, menu->handler_id);
137 if (menu->item_links != NULL)
138 g_sequence_free (menu->item_links);
140 g_object_unref (menu->model);
142 g_slice_free (GMenuExporterMenu, menu);
146 g_menu_exporter_link_free (gpointer data)
148 GMenuExporterLink *link = data;
152 GMenuExporterLink *tmp = link;
155 g_menu_exporter_menu_free (tmp->menu);
158 g_slice_free (GMenuExporterLink, tmp);
162 static GMenuExporterLink *
163 g_menu_exporter_menu_create_links (GMenuExporterMenu *menu,
166 GMenuExporterLink *list = NULL;
171 iter = g_menu_model_iterate_item_links (menu->model, position);
173 while (g_menu_link_iter_get_next (iter, &name, &model))
175 GMenuExporterGroup *group;
176 GMenuExporterLink *tmp;
178 /* keep sections in the same group, but create new groups
181 if (!g_str_equal (name, "section"))
182 group = g_menu_exporter_create_group (g_menu_exporter_group_get_exporter (menu->group));
186 tmp = g_slice_new (GMenuExporterLink);
187 tmp->name = g_strconcat (":", name, NULL);
188 tmp->menu = g_menu_exporter_group_add_menu (group, model);
192 g_object_unref (model);
195 g_object_unref (iter);
201 g_menu_exporter_menu_describe_item (GMenuExporterMenu *menu,
204 GMenuAttributeIter *attr_iter;
205 GVariantBuilder builder;
207 GMenuExporterLink *link;
211 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
213 attr_iter = g_menu_model_iterate_item_attributes (menu->model, position);
214 while (g_menu_attribute_iter_get_next (attr_iter, &name, &value))
216 g_variant_builder_add (&builder, "{sv}", name, value);
217 g_variant_unref (value);
219 g_object_unref (attr_iter);
221 iter = g_sequence_get_iter_at_pos (menu->item_links, position);
222 for (link = g_sequence_get (iter); link; link = link->next)
223 g_variant_builder_add (&builder, "{sv}", link->name,
224 g_variant_new ("(uu)", g_menu_exporter_group_get_id (link->menu->group), link->menu->id));
226 return g_variant_builder_end (&builder);
230 g_menu_exporter_menu_list (GMenuExporterMenu *menu)
232 GVariantBuilder builder;
235 g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
237 n = g_sequence_get_length (menu->item_links);
238 for (i = 0; i < n; i++)
239 g_variant_builder_add_value (&builder, g_menu_exporter_menu_describe_item (menu, i));
241 return g_variant_builder_end (&builder);
245 g_menu_exporter_menu_items_changed (GMenuModel *model,
251 GMenuExporterMenu *menu = user_data;
252 GSequenceIter *point;
255 g_assert (menu->model == model);
256 g_assert (menu->item_links != NULL);
257 g_assert (position + removed <= g_sequence_get_length (menu->item_links));
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_get_length (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 g_bus_unwatch_name (remote->watch_id);
530 g_hash_table_unref (remote->watches);
532 g_slice_free (GMenuExporterRemote, remote);
535 static GMenuExporterRemote *
536 g_menu_exporter_remote_new (GMenuExporter *exporter,
539 GMenuExporterRemote *remote;
541 remote = g_slice_new0 (GMenuExporterRemote);
542 remote->exporter = exporter;
543 remote->watches = g_hash_table_new (NULL, NULL);
544 remote->watch_id = watch_id;
549 /* {{{1 GMenuExporter */
551 struct _GMenuExporter
553 GDBusConnection *connection;
555 guint registration_id;
559 GMenuExporterMenu *root;
564 g_menu_exporter_name_vanished (GDBusConnection *connection,
568 GMenuExporter *exporter = user_data;
570 /* connection == NULL when we get called because the connection closed */
571 g_assert (exporter->connection == connection || connection == NULL);
573 g_hash_table_remove (exporter->remotes, name);
577 g_menu_exporter_subscribe (GMenuExporter *exporter,
581 GMenuExporterRemote *remote;
582 GVariantBuilder builder;
586 remote = g_hash_table_lookup (exporter->remotes, sender);
592 watch_id = g_bus_watch_name_on_connection (exporter->connection, sender, G_BUS_NAME_WATCHER_FLAGS_NONE,
593 NULL, g_menu_exporter_name_vanished, exporter, NULL);
594 remote = g_menu_exporter_remote_new (exporter, watch_id);
595 g_hash_table_insert (exporter->remotes, g_strdup (sender), remote);
598 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a(uuaa{sv}))"));
600 g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(uuaa{sv})"));
602 g_variant_iter_init (&iter, group_ids);
603 while (g_variant_iter_next (&iter, "u", &id))
604 g_menu_exporter_remote_subscribe (remote, id, &builder);
606 g_variant_builder_close (&builder);
608 return g_variant_builder_end (&builder);
612 g_menu_exporter_unsubscribe (GMenuExporter *exporter,
616 GMenuExporterRemote *remote;
620 remote = g_hash_table_lookup (exporter->remotes, sender);
625 g_variant_iter_init (&iter, group_ids);
626 while (g_variant_iter_next (&iter, "u", &id))
627 g_menu_exporter_remote_unsubscribe (remote, id);
629 if (!g_menu_exporter_remote_has_subscriptions (remote))
630 g_hash_table_remove (exporter->remotes, sender);
634 g_menu_exporter_report (GMenuExporter *exporter,
637 GVariantBuilder builder;
639 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
640 g_variant_builder_open (&builder, G_VARIANT_TYPE_ARRAY);
641 g_variant_builder_add_value (&builder, report);
642 g_variant_builder_close (&builder);
644 g_dbus_connection_emit_signal (exporter->connection,
646 exporter->object_path,
647 "org.gtk.Menus", "Changed",
648 g_variant_builder_end (&builder),
653 g_menu_exporter_remove_group (GMenuExporter *exporter,
656 g_hash_table_remove (exporter->groups, GINT_TO_POINTER (id));
659 static GMenuExporterGroup *
660 g_menu_exporter_lookup_group (GMenuExporter *exporter,
663 GMenuExporterGroup *group;
665 group = g_hash_table_lookup (exporter->groups, GINT_TO_POINTER (group_id));
669 group = g_menu_exporter_group_new (exporter, group_id);
670 g_hash_table_insert (exporter->groups, GINT_TO_POINTER (group_id), group);
676 static GMenuExporterGroup *
677 g_menu_exporter_create_group (GMenuExporter *exporter)
679 GMenuExporterGroup *group;
682 id = exporter->next_group_id++;
683 group = g_menu_exporter_group_new (exporter, id);
684 g_hash_table_insert (exporter->groups, GINT_TO_POINTER (id), group);
690 g_menu_exporter_free (gpointer user_data)
692 GMenuExporter *exporter = user_data;
694 g_menu_exporter_menu_free (exporter->root);
695 g_hash_table_unref (exporter->remotes);
696 g_hash_table_unref (exporter->groups);
697 g_object_unref (exporter->connection);
698 g_free (exporter->object_path);
700 g_slice_free (GMenuExporter, exporter);
704 g_menu_exporter_method_call (GDBusConnection *connection,
706 const gchar *object_path,
707 const gchar *interface_name,
708 const gchar *method_name,
709 GVariant *parameters,
710 GDBusMethodInvocation *invocation,
713 GMenuExporter *exporter = user_data;
716 group_ids = g_variant_get_child_value (parameters, 0);
718 if (g_str_equal (method_name, "Start"))
719 g_dbus_method_invocation_return_value (invocation, g_menu_exporter_subscribe (exporter, sender, group_ids));
721 else if (g_str_equal (method_name, "End"))
723 g_menu_exporter_unsubscribe (exporter, sender, group_ids);
724 g_dbus_method_invocation_return_value (invocation, NULL);
728 g_assert_not_reached ();
730 g_variant_unref (group_ids);
733 /* {{{1 Public API */
736 * g_dbus_connection_export_menu_model:
737 * @connection: a #GDBusConnection
738 * @object_path: a D-Bus object path
739 * @menu: a #GMenuModel
740 * @error: return location for an error, or %NULL
742 * Exports @menu on @connection at @object_path.
744 * The implemented D-Bus API should be considered private.
745 * It is subject to change in the future.
747 * An object path can only have one menu model exported on it. If this
748 * constraint is violated, the export will fail and 0 will be
749 * returned (with @error set accordingly).
751 * You can unexport the menu model using
752 * g_dbus_connection_unexport_menu_model() with the return value of
755 * Returns: the ID of the export (never zero), or 0 in case of failure
760 g_dbus_connection_export_menu_model (GDBusConnection *connection,
761 const gchar *object_path,
765 const GDBusInterfaceVTable vtable = {
766 g_menu_exporter_method_call,
768 GMenuExporter *exporter;
771 exporter = g_slice_new0 (GMenuExporter);
773 id = g_dbus_connection_register_object (connection, object_path, org_gtk_Menus_get_interface (),
774 &vtable, exporter, g_menu_exporter_free, error);
778 g_slice_free (GMenuExporter, exporter);
782 exporter->connection = g_object_ref (connection);
783 exporter->object_path = g_strdup (object_path);
784 exporter->groups = g_hash_table_new (NULL, NULL);
785 exporter->remotes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_menu_exporter_remote_free);
786 exporter->root = g_menu_exporter_group_add_menu (g_menu_exporter_create_group (exporter), menu);
792 * g_dbus_connection_unexport_menu_model:
793 * @connection: a #GDBusConnection
794 * @export_id: the ID from g_dbus_connection_export_menu_model()
796 * Reverses the effect of a previous call to
797 * g_dbus_connection_export_menu_model().
799 * It is an error to call this function with an ID that wasn't returned
800 * from g_dbus_connection_export_menu_model() or to call it with the
801 * same ID more than once.
806 g_dbus_connection_unexport_menu_model (GDBusConnection *connection,
809 g_dbus_connection_unregister_object (connection, export_id);
813 /* vim:set foldmethod=marker: */