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>
22 #include "gmenuproxy.h"
24 #include "gmenumodel.h"
31 * @short_description: A D-Bus GMenuModel implementation
32 * @see_also: <link linkend="gio-GMenuModel-exporter">GMenuModel Exporter</link>
34 * #GMenuProxy is an implementation of #GMenuModel that can be used
35 * as a proxy for a menu model that is exported over D-Bus with
36 * g_menu_model_dbus_export_start().
40 * There are 3 main (quasi-)classes involved here:
46 * Each of these classes exists as a parameterised singleton keyed to a
49 * - GMenuProxyPath represents a D-Bus object path on a particular
50 * unique bus name on a particular GDBusConnection.
52 * - GMenuProxyGroup represents a particular group on a particular
55 * - GMenuProxy represents a particular menu within a particular
58 * There are also two (and a half) utility structs:
60 * - PathIdentifier and ConstPathIdentifier
63 * PathIdentifier is the triplet of (GDBusConnection, unique name,
64 * object path) that uniquely identifies a particular GMenuProxyPath.
65 * It holds ownership on each of these things, so we have a
66 * ConstPathIdentifier variant that does not.
68 * We have a 3-level hierarchy of hashtables:
70 * - a global hashtable (g_menu_proxy_paths) maps from PathIdentifier
73 * - each GMenuProxyPath has a hashtable mapping from guint (group
74 * number) to GMenuProxyGroup
76 * - each GMenuProxyGroup has a hashtable mapping from guint (menu
77 * number) to GMenuProxy.
79 * In this way, each quintuplet of (connection, bus name, object path,
80 * group id, menu id) maps to a single GMenuProxy instance that can be
81 * located via 3 hashtable lookups.
83 * All of the 3 classes are refcounted (GMenuProxyPath and
84 * GMenuProxyGroup manually, and GMenuProxy by virtue of being a
85 * GObject). The hashtables do not hold references -- rather, when the
86 * last reference is dropped, the object is removed from the hashtable.
88 * The hard references go in the other direction: GMenuProxy is created
89 * as the user requests it and only exists as long as the user holds a
90 * reference on it. GMenuProxy holds a reference on the GMenuProxyGroup
91 * from which it came. GMenuProxyGroup holds a reference on
94 * In addition to refcounts, each object has an 'active' variable (ints
95 * for GMenuProxyPath and GMenuProxyGroup, boolean for GMenuProxy).
97 * - GMenuProxy is inactive when created and becomes active only when
98 * first queried for information. This prevents extra work from
99 * happening just by someone acquiring a GMenuProxy (and not
100 * actually trying to display it yet).
102 * - The active count on GMenuProxyGroup is equal to the number of
103 * GMenuProxy instances in that group that are active. When the
104 * active count transitions from 0 to 1, the group calls the 'Start'
105 * method on the service to begin monitoring that group. When it
106 * drops from 1 to 0, the group calls the 'End' method to stop
109 * - The active count on GMenuProxyPath is equal to the number of
110 * GMenuProxyGroup instances on that path with a non-zero active
111 * count. When the active count transitions from 0 to 1, the path
112 * sets up a signal subscription to monitor any changes. The signal
113 * subscription is taken down when the active count transitions from
116 * When active, GMenuProxyPath gets incoming signals when changes occur.
117 * If the change signal mentions a group for which we currently have an
118 * active GMenuProxyGroup, the change signal is passed along to that
119 * group. If the group is inactive, the change signal is ignored.
121 * Most of the "work" occurs in GMenuProxyGroup. In addition to the
122 * hashtable of GMenuProxy instances, it keeps a hashtable of the actual
123 * menu contents, each encoded as GSequence of GMenuProxyItem. It
124 * initially populates this table with the results of the "Start" method
125 * call and then updates it according to incoming change signals. If
126 * the change signal mentions a menu for which we current have an active
127 * GMenuProxy, the change signal is passed along to that proxy. If the
128 * proxy is inactive, the change signal is ignored.
130 * GMenuProxyItem is just a pair of hashtables, one for the attributes
131 * and one for the links of the item (mapping strings to
132 * other GMenuProxy instances). XXX reconsider this since it means the
133 * submenu proxies stay around forever.....
135 * Following the "empty is the same as non-existent" rule, the hashtable
136 * of GSequence of GMenuProxyItem holds NULL for empty menus.
138 * GMenuProxy contains very little functionality of its own. It holds a
139 * (weak) reference to the GSequence of GMenuProxyItem contained in the
140 * GMenuProxyGroup. It uses this GSequence to implement the GMenuModel
141 * interface. It also emits the "items-changed" signal if it is active
142 * and it was told that the contents of the GSequence changed.
145 typedef struct _GMenuProxyGroup GMenuProxyGroup;
146 typedef struct _GMenuProxyPath GMenuProxyPath;
148 static void g_menu_proxy_group_changed (GMenuProxyGroup *group,
153 static void g_menu_proxy_changed (GMenuProxy *proxy,
158 static GMenuProxyGroup * g_menu_proxy_group_get_from_path (GMenuProxyPath *path,
160 static GMenuProxy * g_menu_proxy_get_from_group (GMenuProxyGroup *group,
163 /* PathIdentifier {{{1 */
166 GDBusConnection *connection;
173 GDBusConnection *connection;
174 const gchar *bus_name;
175 const gchar *object_path;
176 } ConstPathIdentifier;
179 path_identifier_hash (gconstpointer data)
181 ConstPathIdentifier *id = data;
183 return g_str_hash (id->object_path);
187 path_identifier_equal (gconstpointer a,
190 ConstPathIdentifier *id_a = a;
191 ConstPathIdentifier *id_b = b;
193 return id_a->connection == id_b->connection &&
194 g_str_equal (id_a->bus_name, id_b->bus_name) &&
195 g_str_equal (id_a->object_path, id_b->object_path);
199 path_identifier_free (PathIdentifier *id)
201 g_object_unref (id->connection);
202 g_free (id->bus_name);
203 g_free (id->object_path);
205 g_slice_free (PathIdentifier, id);
208 static PathIdentifier *
209 path_identifier_new (ConstPathIdentifier *cid)
213 id = g_slice_new (PathIdentifier);
214 id->connection = g_object_ref (cid->connection);
215 id->bus_name = g_strdup (cid->bus_name);
216 id->object_path = g_strdup (cid->object_path);
221 /* GMenuProxyPath {{{1 */
223 struct _GMenuProxyPath
233 static GHashTable *g_menu_proxy_paths;
235 static GMenuProxyPath *
236 g_menu_proxy_path_ref (GMenuProxyPath *path)
244 g_menu_proxy_path_unref (GMenuProxyPath *path)
246 if (--path->ref_count == 0)
248 g_hash_table_remove (g_menu_proxy_paths, path->id);
249 g_hash_table_unref (path->groups);
250 path_identifier_free (path->id);
252 g_slice_free (GMenuProxyPath, path);
257 g_menu_proxy_path_signal (GDBusConnection *connection,
258 const gchar *sender_name,
259 const gchar *object_path,
260 const gchar *interface_name,
261 const gchar *signal_name,
262 GVariant *parameters,
265 GMenuProxyPath *path = user_data;
273 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))")))
276 g_variant_get (parameters, "(a(uuuuaa{sv}))", &iter);
277 while (g_variant_iter_loop (iter, "(uuuu@aa{sv})", &group_id, &menu_id, &position, &removes, &adds))
279 GMenuProxyGroup *group;
281 group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
284 g_menu_proxy_group_changed (group, menu_id, position, removes, adds);
286 g_variant_iter_free (iter);
290 g_menu_proxy_path_activate (GMenuProxyPath *path)
292 if (path->active++ == 0)
293 path->watch_id = g_dbus_connection_signal_subscribe (path->id->connection, path->id->bus_name,
294 "org.gtk.Menus", "Changed", path->id->object_path,
295 NULL, G_DBUS_SIGNAL_FLAGS_NONE,
296 g_menu_proxy_path_signal, path, NULL);
300 g_menu_proxy_path_deactivate (GMenuProxyPath *path)
302 if (--path->active == 0)
303 g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
306 static GMenuProxyPath *
307 g_menu_proxy_path_get (GDBusConnection *connection,
308 const gchar *bus_name,
309 const gchar *object_path)
311 ConstPathIdentifier cid = { connection, bus_name, object_path };
312 GMenuProxyPath *path;
314 if (g_menu_proxy_paths == NULL)
315 g_menu_proxy_paths = g_hash_table_new (path_identifier_hash, path_identifier_equal);
317 path = g_hash_table_lookup (g_menu_proxy_paths, &cid);
321 path = g_slice_new (GMenuProxyPath);
322 path->id = path_identifier_new (&cid);
323 path->groups = g_hash_table_new (NULL, NULL);
327 g_hash_table_insert (g_menu_proxy_paths, path->id, path);
330 return g_menu_proxy_path_ref (path);
333 /* GMenuProxyGroup, GMenuProxyItem {{{1 */
341 struct _GMenuProxyGroup
343 GMenuProxyPath *path;
346 GHashTable *proxies; /* uint -> unowned GMenuProxy */
347 GHashTable *menus; /* uint -> owned GSequence */
355 GHashTable *attributes;
359 static GMenuProxyGroup *
360 g_menu_proxy_group_ref (GMenuProxyGroup *group)
368 g_menu_proxy_group_unref (GMenuProxyGroup *group)
370 if (--group->ref_count == 0)
372 g_assert (group->state == GROUP_OFFLINE);
373 g_assert (group->active == 0);
375 g_hash_table_remove (group->path->groups, GINT_TO_POINTER (group->id));
376 g_hash_table_unref (group->proxies);
378 g_menu_proxy_path_unref (group->path);
380 g_slice_free (GMenuProxyGroup, group);
385 g_menu_proxy_item_free (gpointer data)
387 GMenuProxyItem *item = data;
389 g_hash_table_unref (item->attributes);
390 g_hash_table_unref (item->links);
392 g_slice_free (GMenuProxyItem, item);
395 static GMenuProxyItem *
396 g_menu_proxy_group_create_item (GMenuProxyGroup *context,
397 GVariant *description)
399 GMenuProxyItem *item;
404 item = g_slice_new (GMenuProxyItem);
405 item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
406 item->links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
408 g_variant_iter_init (&iter, description);
409 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
412 if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(uu)")))
414 guint group_id, menu_id;
415 GMenuProxyGroup *group;
418 g_variant_get (value, "(uu)", &group_id, &menu_id);
420 /* save the hash lookup in a relatively common case */
421 if (context->id != group_id)
422 group = g_menu_proxy_group_get_from_path (context->path, group_id);
424 group = g_menu_proxy_group_ref (context);
426 proxy = g_menu_proxy_get_from_group (group, menu_id);
428 /* key + 1 to skip the ':' */
429 g_hash_table_insert (item->links, g_strdup (key + 1), proxy);
431 g_menu_proxy_group_unref (group);
435 g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));
441 * GMenuProxyGroup can be in three states:
443 * OFFLINE: not subscribed to this group
444 * PENDING: we made the call to subscribe to this group, but the result
445 * has not come back yet
446 * ONLINE: we are fully subscribed
448 * We can get into some nasty situations where we make a call due to an
449 * activation request but receive a deactivation request before the call
450 * returns. If another activation request occurs then we could risk
451 * sending a Start request even though one is already in progress. For
452 * this reason, we have to carefully consider what to do in each of the
453 * three states for each of the following situations:
455 * - activation requested
456 * - deactivation requested
457 * - Start call finishes
459 * To simplify things a bit, we do not have a callback for the Stop
460 * call. We just send it and assume that it takes effect immediately.
462 * Activation requested:
463 * OFFLINE: make the Start call and transition to PENDING
464 * PENDING: do nothing -- call is already in progress.
465 * ONLINE: this should not be possible
467 * Deactivation requested:
468 * OFFLINE: this should not be possible
469 * PENDING: do nothing -- handle it when the Start call finishes
470 * ONLINE: send the Stop call and move to OFFLINE immediately
472 * Start call finishes:
473 * OFFLINE: this should not be possible
475 * If we should be active (ie: active count > 0): move to ONLINE
476 * If not: send Stop call and move to OFFLINE immediately
477 * ONLINE: this should not be possible
479 * We have to take care with regards to signal subscriptions (ie:
480 * activation of the GMenuProxyPath). The signal subscription is always
481 * established when transitioning from OFFLINE to PENDING and taken down
482 * when transitioning to OFFLINE (from either PENDING or ONLINE).
484 * Since there are two places where we transition to OFFLINE, we split
485 * that code out into a separate function.
488 g_menu_proxy_group_go_offline (GMenuProxyGroup *group)
490 g_menu_proxy_path_deactivate (group->path);
491 g_dbus_connection_call (group->path->id->connection,
492 group->path->id->bus_name,
493 group->path->id->object_path,
494 "org.gtk.Menus", "End",
495 g_variant_new_parsed ("([ %u ],)", group->id),
496 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
498 group->state = GROUP_OFFLINE;
503 g_menu_proxy_group_start_ready (GObject *source_object,
504 GAsyncResult *result,
507 GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
508 GMenuProxyGroup *group = user_data;
511 g_assert (group->state == GROUP_PENDING);
513 reply = g_dbus_connection_call_finish (connection, result, NULL);
517 group->state = GROUP_ONLINE;
519 /* If we receive no reply, just act like we got an empty reply. */
527 g_variant_get (reply, "(a(uuaa{sv}))", &iter);
528 while (g_variant_iter_loop (iter, "(uu@aa{sv})", &group_id, &menu_id, &items))
529 if (group_id == group->id)
530 g_menu_proxy_group_changed (group, menu_id, 0, 0, items);
531 g_variant_iter_free (iter);
535 g_menu_proxy_group_go_offline (group);
538 g_variant_unref (reply);
540 g_menu_proxy_group_unref (group);
544 g_menu_proxy_group_activate (GMenuProxyGroup *group)
546 if (group->active++ == 0)
548 g_assert (group->state != GROUP_ONLINE);
550 if (group->state == GROUP_OFFLINE)
552 g_menu_proxy_path_activate (group->path);
554 g_dbus_connection_call (group->path->id->connection,
555 group->path->id->bus_name,
556 group->path->id->object_path,
557 "org.gtk.Menus", "Start",
558 g_variant_new_parsed ("([ %u ],)", group->id),
559 G_VARIANT_TYPE ("(a(uuaa{sv}))"),
560 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
561 g_menu_proxy_group_start_ready,
562 g_menu_proxy_group_ref (group));
563 group->state = GROUP_PENDING;
569 g_menu_proxy_group_deactivate (GMenuProxyGroup *group)
571 if (--group->active == 0)
573 g_assert (group->state != GROUP_OFFLINE);
575 if (group->state == GROUP_ONLINE)
577 /* We are here because nobody is watching, so just free
578 * everything and don't bother with the notifications.
580 g_hash_table_remove_all (group->menus);
582 g_menu_proxy_group_go_offline (group);
588 g_menu_proxy_group_changed (GMenuProxyGroup *group,
594 GSequenceIter *point;
601 /* We could have signals coming to us when we're not active (due to
602 * some other process having subscribed to this group) or when we're
603 * pending. In both of those cases, we want to ignore the signal
604 * since we'll get our own information when we call "Start" for
607 if (group->state != GROUP_ONLINE)
610 items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
614 items = g_sequence_new (g_menu_proxy_item_free);
615 g_hash_table_insert (group->menus, GINT_TO_POINTER (menu_id), items);
618 point = g_sequence_get_iter_at_pos (items, position + removed);
620 g_return_if_fail (point != NULL);
624 GSequenceIter *start;
626 start = g_sequence_get_iter_at_pos (items, position);
627 g_sequence_remove_range (start, point);
630 n_added = g_variant_iter_init (&iter, added);
631 while (g_variant_iter_loop (&iter, "@a{sv}", &item))
632 g_sequence_insert_before (point, g_menu_proxy_group_create_item (group, item));
634 if (g_sequence_get_length (items) == 0)
636 g_hash_table_remove (group->menus, GINT_TO_POINTER (menu_id));
640 if ((proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id))))
641 g_menu_proxy_changed (proxy, items, position, removed, n_added);
644 static GMenuProxyGroup *
645 g_menu_proxy_group_get_from_path (GMenuProxyPath *path,
648 GMenuProxyGroup *group;
650 group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
654 group = g_slice_new (GMenuProxyGroup);
655 group->path = g_menu_proxy_path_ref (path);
656 group->id = group_id;
657 group->proxies = g_hash_table_new (NULL, NULL);
658 group->menus = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_sequence_free);
659 group->state = GROUP_OFFLINE;
661 group->ref_count = 0;
663 g_hash_table_insert (path->groups, GINT_TO_POINTER (group->id), group);
666 return g_menu_proxy_group_ref (group);
669 static GMenuProxyGroup *
670 g_menu_proxy_group_get (GDBusConnection *connection,
671 const gchar *bus_name,
672 const gchar *object_path,
675 GMenuProxyGroup *group;
676 GMenuProxyPath *path;
678 path = g_menu_proxy_path_get (connection, bus_name, object_path);
679 group = g_menu_proxy_group_get_from_path (path, group_id);
680 g_menu_proxy_path_unref (path);
685 /* GMenuProxy {{{1 */
687 typedef GMenuModelClass GMenuProxyClass;
692 GMenuProxyGroup *group;
695 GSequence *items; /* unowned */
699 G_DEFINE_TYPE (GMenuProxy, g_menu_proxy, G_TYPE_MENU_MODEL)
702 g_menu_proxy_is_mutable (GMenuModel *model)
708 g_menu_proxy_get_n_items (GMenuModel *model)
710 GMenuProxy *proxy = G_MENU_PROXY (model);
714 g_menu_proxy_group_activate (proxy->group);
715 proxy->active = TRUE;
718 return proxy->items ? g_sequence_get_length (proxy->items) : 0;
722 g_menu_proxy_get_item_attributes (GMenuModel *model,
726 GMenuProxy *proxy = G_MENU_PROXY (model);
727 GMenuProxyItem *item;
730 g_return_if_fail (proxy->active);
731 g_return_if_fail (proxy->items);
733 iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
734 g_return_if_fail (iter);
736 item = g_sequence_get (iter);
737 g_return_if_fail (item);
739 *table = g_hash_table_ref (item->attributes);
743 g_menu_proxy_get_item_links (GMenuModel *model,
747 GMenuProxy *proxy = G_MENU_PROXY (model);
748 GMenuProxyItem *item;
751 g_return_if_fail (proxy->active);
752 g_return_if_fail (proxy->items);
754 iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
755 g_return_if_fail (iter);
757 item = g_sequence_get (iter);
758 g_return_if_fail (item);
760 *table = g_hash_table_ref (item->links);
764 g_menu_proxy_finalize (GObject *object)
766 GMenuProxy *proxy = G_MENU_PROXY (object);
769 g_menu_proxy_group_deactivate (proxy->group);
771 g_hash_table_remove (proxy->group->proxies, GINT_TO_POINTER (proxy->id));
772 g_menu_proxy_group_unref (proxy->group);
774 G_OBJECT_CLASS (g_menu_proxy_parent_class)
779 g_menu_proxy_init (GMenuProxy *proxy)
784 g_menu_proxy_class_init (GMenuProxyClass *class)
786 GObjectClass *object_class = G_OBJECT_CLASS (class);
788 class->is_mutable = g_menu_proxy_is_mutable;
789 class->get_n_items = g_menu_proxy_get_n_items;
790 class->get_item_attributes = g_menu_proxy_get_item_attributes;
791 class->get_item_links = g_menu_proxy_get_item_links;
793 object_class->finalize = g_menu_proxy_finalize;
797 g_menu_proxy_changed (GMenuProxy *proxy,
803 proxy->items = items;
805 if (proxy->active && (removed || added))
806 g_menu_model_items_changed (G_MENU_MODEL (proxy), position, removed, added);
810 g_menu_proxy_get_from_group (GMenuProxyGroup *group,
815 proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id));
817 g_object_ref (proxy);
821 proxy = g_object_new (G_TYPE_MENU_PROXY, NULL);
822 proxy->items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
823 g_hash_table_insert (group->proxies, GINT_TO_POINTER (menu_id), proxy);
824 proxy->group = g_menu_proxy_group_ref (group);
833 * @connection: a #GDBusConnection
834 * @bus_name: the bus name which exports the menu model
835 * @object_path: the object path at which the menu model is exported
837 * Obtains a #GMenuProxy for the menu model which is exported
838 * at the given @bus_name and @object_path.
840 * Returns: (transfer full): a #GMenuProxy object. Free with g_object_unref().
843 g_menu_proxy_get (GDBusConnection *connection,
844 const gchar *bus_name,
845 const gchar *object_path)
847 GMenuProxyGroup *group;
850 group = g_menu_proxy_group_get (connection, bus_name, object_path, 0);
851 proxy = g_menu_proxy_get_from_group (group, 0);
852 g_menu_proxy_group_unref (group);
858 dump_proxy (gpointer key, gpointer value, gpointer data)
860 GMenuProxy *proxy = value;
862 g_print (" menu %d refcount %d active %d\n",
863 proxy->id, G_OBJECT (proxy)->ref_count, proxy->active);
867 dump_group (gpointer key, gpointer value, gpointer data)
869 GMenuProxyGroup *group = value;
871 g_print (" group %d refcount %d state %d active %d\n",
872 group->id, group->ref_count, group->state, group->active);
874 g_hash_table_foreach (group->proxies, dump_proxy, NULL);
878 dump_path (gpointer key, gpointer value, gpointer data)
880 PathIdentifier *pid = key;
881 GMenuProxyPath *path = value;
883 g_print ("%s active %d\n", pid->object_path, path->active);
884 g_hash_table_foreach (path->groups, dump_group, NULL);
888 g_menu_proxy_dump (void)
890 g_hash_table_foreach (g_menu_proxy_paths, dump_path, NULL);
895 /* vim:set foldmethod=marker: */