GMenuModel exporter: remove workaround
[glib.git] / gio / gmenuproxy.c
blob92bf8115556cf3ecd0b87b36008d807dc18f2424
1 /*
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,
17 * USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gmenuproxy.h"
24 #include "gmenumodel.h"
26 /* Prelude {{{1 */
28 /**
29 * SECTION:gmenuproxy
30 * @title: GMenuProxy
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:
42 * - GMenuProxyPath
43 * - GMenuProxyGroup
44 * - GMenuProxy
46 * Each of these classes exists as a parameterised singleton keyed to a
47 * particular thing:
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
53 * GMenuProxyPath.
55 * - GMenuProxy represents a particular menu within a particular
56 * GMenuProxyGroup.
58 * There are also two (and a half) utility structs:
60 * - PathIdentifier and ConstPathIdentifier
61 * - GMenuProxyItem
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
71 * to GMenuProxyPath
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
92 * GMenuProxyPath.
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
107 * monitoring.
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
114 * 1 to 0.
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. Both map strings to GVariant
132 * instances. In the case of links, the GVariant has type '(uu)' and is
133 * turned into a GMenuProxy at the point that the user pulls it through
134 * the API.
136 * Following the "empty is the same as non-existent" rule, the hashtable
137 * of GSequence of GMenuProxyItem holds NULL for empty menus.
139 * GMenuProxy contains very little functionality of its own. It holds a
140 * (weak) reference to the GSequence of GMenuProxyItem contained in the
141 * GMenuProxyGroup. It uses this GSequence to implement the GMenuModel
142 * interface. It also emits the "items-changed" signal if it is active
143 * and it was told that the contents of the GSequence changed.
146 typedef struct _GMenuProxyGroup GMenuProxyGroup;
147 typedef struct _GMenuProxyPath GMenuProxyPath;
149 static void g_menu_proxy_group_changed (GMenuProxyGroup *group,
150 guint menu_id,
151 gint position,
152 gint removed,
153 GVariant *added);
154 static void g_menu_proxy_changed (GMenuProxy *proxy,
155 GSequence *items,
156 gint position,
157 gint removed,
158 gint added);
159 static GMenuProxyGroup * g_menu_proxy_group_get_from_path (GMenuProxyPath *path,
160 guint group_id);
161 static GMenuProxy * g_menu_proxy_get_from_group (GMenuProxyGroup *group,
162 guint menu_id);
164 /* PathIdentifier {{{1 */
165 typedef struct
167 GDBusConnection *connection;
168 gchar *bus_name;
169 gchar *object_path;
170 } PathIdentifier;
172 typedef const struct
174 GDBusConnection *connection;
175 const gchar *bus_name;
176 const gchar *object_path;
177 } ConstPathIdentifier;
179 static guint
180 path_identifier_hash (gconstpointer data)
182 ConstPathIdentifier *id = data;
184 return g_str_hash (id->object_path);
187 static gboolean
188 path_identifier_equal (gconstpointer a,
189 gconstpointer b)
191 ConstPathIdentifier *id_a = a;
192 ConstPathIdentifier *id_b = b;
194 return id_a->connection == id_b->connection &&
195 g_str_equal (id_a->bus_name, id_b->bus_name) &&
196 g_str_equal (id_a->object_path, id_b->object_path);
199 static void
200 path_identifier_free (PathIdentifier *id)
202 g_object_unref (id->connection);
203 g_free (id->bus_name);
204 g_free (id->object_path);
206 g_slice_free (PathIdentifier, id);
209 static PathIdentifier *
210 path_identifier_new (ConstPathIdentifier *cid)
212 PathIdentifier *id;
214 id = g_slice_new (PathIdentifier);
215 id->connection = g_object_ref (cid->connection);
216 id->bus_name = g_strdup (cid->bus_name);
217 id->object_path = g_strdup (cid->object_path);
219 return id;
222 /* GMenuProxyPath {{{1 */
224 struct _GMenuProxyPath
226 PathIdentifier *id;
227 gint ref_count;
229 GHashTable *groups;
230 gint active;
231 guint watch_id;
234 static GHashTable *g_menu_proxy_paths;
236 static GMenuProxyPath *
237 g_menu_proxy_path_ref (GMenuProxyPath *path)
239 path->ref_count++;
241 return path;
244 static void
245 g_menu_proxy_path_unref (GMenuProxyPath *path)
247 if (--path->ref_count == 0)
249 g_hash_table_remove (g_menu_proxy_paths, path->id);
250 g_hash_table_unref (path->groups);
251 path_identifier_free (path->id);
253 g_slice_free (GMenuProxyPath, path);
257 static void
258 g_menu_proxy_path_signal (GDBusConnection *connection,
259 const gchar *sender_name,
260 const gchar *object_path,
261 const gchar *interface_name,
262 const gchar *signal_name,
263 GVariant *parameters,
264 gpointer user_data)
266 GMenuProxyPath *path = user_data;
267 GVariantIter *iter;
268 guint group_id;
269 guint menu_id;
270 guint position;
271 guint removes;
272 GVariant *adds;
274 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))")))
275 return;
277 g_variant_get (parameters, "(a(uuuuaa{sv}))", &iter);
278 while (g_variant_iter_loop (iter, "(uuuu@aa{sv})", &group_id, &menu_id, &position, &removes, &adds))
280 GMenuProxyGroup *group;
282 group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
284 if (group != NULL)
285 g_menu_proxy_group_changed (group, menu_id, position, removes, adds);
287 g_variant_iter_free (iter);
290 static void
291 g_menu_proxy_path_activate (GMenuProxyPath *path)
293 if (path->active++ == 0)
294 path->watch_id = g_dbus_connection_signal_subscribe (path->id->connection, path->id->bus_name,
295 "org.gtk.Menus", "Changed", path->id->object_path,
296 NULL, G_DBUS_SIGNAL_FLAGS_NONE,
297 g_menu_proxy_path_signal, path, NULL);
300 static void
301 g_menu_proxy_path_deactivate (GMenuProxyPath *path)
303 if (--path->active == 0)
304 g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
307 static GMenuProxyPath *
308 g_menu_proxy_path_get (GDBusConnection *connection,
309 const gchar *bus_name,
310 const gchar *object_path)
312 ConstPathIdentifier cid = { connection, bus_name, object_path };
313 GMenuProxyPath *path;
315 if (g_menu_proxy_paths == NULL)
316 g_menu_proxy_paths = g_hash_table_new (path_identifier_hash, path_identifier_equal);
318 path = g_hash_table_lookup (g_menu_proxy_paths, &cid);
320 if (path == NULL)
322 path = g_slice_new (GMenuProxyPath);
323 path->id = path_identifier_new (&cid);
324 path->groups = g_hash_table_new (NULL, NULL);
325 path->ref_count = 0;
326 path->active = 0;
328 g_hash_table_insert (g_menu_proxy_paths, path->id, path);
331 return g_menu_proxy_path_ref (path);
334 /* GMenuProxyGroup, GMenuProxyItem {{{1 */
335 typedef enum
337 GROUP_OFFLINE,
338 GROUP_PENDING,
339 GROUP_ONLINE
340 } GroupStatus;
342 struct _GMenuProxyGroup
344 GMenuProxyPath *path;
345 guint id;
347 GHashTable *proxies; /* uint -> unowned GMenuProxy */
348 GHashTable *menus; /* uint -> owned GSequence */
349 gint ref_count;
350 GroupStatus state;
351 gint active;
354 typedef struct
356 GHashTable *attributes;
357 GHashTable *links;
358 } GMenuProxyItem;
360 static GMenuProxyGroup *
361 g_menu_proxy_group_ref (GMenuProxyGroup *group)
363 group->ref_count++;
365 return group;
368 static void
369 g_menu_proxy_group_unref (GMenuProxyGroup *group)
371 if (--group->ref_count == 0)
373 g_assert (group->state == GROUP_OFFLINE);
374 g_assert (group->active == 0);
376 g_hash_table_remove (group->path->groups, GINT_TO_POINTER (group->id));
377 g_hash_table_unref (group->proxies);
379 g_menu_proxy_path_unref (group->path);
381 g_slice_free (GMenuProxyGroup, group);
385 static void
386 g_menu_proxy_item_free (gpointer data)
388 GMenuProxyItem *item = data;
390 g_hash_table_unref (item->attributes);
391 g_hash_table_unref (item->links);
393 g_slice_free (GMenuProxyItem, item);
396 static GMenuProxyItem *
397 g_menu_proxy_group_create_item (GVariant *description)
399 GMenuProxyItem *item;
400 GVariantIter iter;
401 const gchar *key;
402 GVariant *value;
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_variant_unref);
408 g_variant_iter_init (&iter, description);
409 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
410 if (key[0] == ':')
411 /* key + 1 to skip the ':' */
412 g_hash_table_insert (item->links, g_strdup (key + 1), g_variant_ref (value));
413 else
414 g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));
416 return item;
420 * GMenuProxyGroup can be in three states:
422 * OFFLINE: not subscribed to this group
423 * PENDING: we made the call to subscribe to this group, but the result
424 * has not come back yet
425 * ONLINE: we are fully subscribed
427 * We can get into some nasty situations where we make a call due to an
428 * activation request but receive a deactivation request before the call
429 * returns. If another activation request occurs then we could risk
430 * sending a Start request even though one is already in progress. For
431 * this reason, we have to carefully consider what to do in each of the
432 * three states for each of the following situations:
434 * - activation requested
435 * - deactivation requested
436 * - Start call finishes
438 * To simplify things a bit, we do not have a callback for the Stop
439 * call. We just send it and assume that it takes effect immediately.
441 * Activation requested:
442 * OFFLINE: make the Start call and transition to PENDING
443 * PENDING: do nothing -- call is already in progress.
444 * ONLINE: this should not be possible
446 * Deactivation requested:
447 * OFFLINE: this should not be possible
448 * PENDING: do nothing -- handle it when the Start call finishes
449 * ONLINE: send the Stop call and move to OFFLINE immediately
451 * Start call finishes:
452 * OFFLINE: this should not be possible
453 * PENDING:
454 * If we should be active (ie: active count > 0): move to ONLINE
455 * If not: send Stop call and move to OFFLINE immediately
456 * ONLINE: this should not be possible
458 * We have to take care with regards to signal subscriptions (ie:
459 * activation of the GMenuProxyPath). The signal subscription is always
460 * established when transitioning from OFFLINE to PENDING and taken down
461 * when transitioning to OFFLINE (from either PENDING or ONLINE).
463 * Since there are two places where we transition to OFFLINE, we split
464 * that code out into a separate function.
466 static void
467 g_menu_proxy_group_go_offline (GMenuProxyGroup *group)
469 g_menu_proxy_path_deactivate (group->path);
470 g_dbus_connection_call (group->path->id->connection,
471 group->path->id->bus_name,
472 group->path->id->object_path,
473 "org.gtk.Menus", "End",
474 g_variant_new_parsed ("([ %u ],)", group->id),
475 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
476 NULL, NULL, NULL);
477 group->state = GROUP_OFFLINE;
481 static void
482 g_menu_proxy_group_start_ready (GObject *source_object,
483 GAsyncResult *result,
484 gpointer user_data)
486 GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
487 GMenuProxyGroup *group = user_data;
488 GVariant *reply;
490 g_assert (group->state == GROUP_PENDING);
492 reply = g_dbus_connection_call_finish (connection, result, NULL);
494 if (group->active)
496 group->state = GROUP_ONLINE;
498 /* If we receive no reply, just act like we got an empty reply. */
499 if (reply)
501 GVariantIter *iter;
502 GVariant *items;
503 guint group_id;
504 guint menu_id;
506 g_variant_get (reply, "(a(uuaa{sv}))", &iter);
507 while (g_variant_iter_loop (iter, "(uu@aa{sv})", &group_id, &menu_id, &items))
508 if (group_id == group->id)
509 g_menu_proxy_group_changed (group, menu_id, 0, 0, items);
510 g_variant_iter_free (iter);
513 else
514 g_menu_proxy_group_go_offline (group);
516 if (reply)
517 g_variant_unref (reply);
519 g_menu_proxy_group_unref (group);
522 static void
523 g_menu_proxy_group_activate (GMenuProxyGroup *group)
525 if (group->active++ == 0)
527 g_assert (group->state != GROUP_ONLINE);
529 if (group->state == GROUP_OFFLINE)
531 g_menu_proxy_path_activate (group->path);
533 g_dbus_connection_call (group->path->id->connection,
534 group->path->id->bus_name,
535 group->path->id->object_path,
536 "org.gtk.Menus", "Start",
537 g_variant_new_parsed ("([ %u ],)", group->id),
538 G_VARIANT_TYPE ("(a(uuaa{sv}))"),
539 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
540 g_menu_proxy_group_start_ready,
541 g_menu_proxy_group_ref (group));
542 group->state = GROUP_PENDING;
547 static void
548 g_menu_proxy_group_deactivate (GMenuProxyGroup *group)
550 if (--group->active == 0)
552 g_assert (group->state != GROUP_OFFLINE);
554 if (group->state == GROUP_ONLINE)
556 /* We are here because nobody is watching, so just free
557 * everything and don't bother with the notifications.
559 g_hash_table_remove_all (group->menus);
561 g_menu_proxy_group_go_offline (group);
566 static void
567 g_menu_proxy_group_changed (GMenuProxyGroup *group,
568 guint menu_id,
569 gint position,
570 gint removed,
571 GVariant *added)
573 GSequenceIter *point;
574 GVariantIter iter;
575 GMenuProxy *proxy;
576 GSequence *items;
577 GVariant *item;
578 gint n_added;
580 /* We could have signals coming to us when we're not active (due to
581 * some other process having subscribed to this group) or when we're
582 * pending. In both of those cases, we want to ignore the signal
583 * since we'll get our own information when we call "Start" for
584 * ourselves.
586 if (group->state != GROUP_ONLINE)
587 return;
589 items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
591 if (items == NULL)
593 items = g_sequence_new (g_menu_proxy_item_free);
594 g_hash_table_insert (group->menus, GINT_TO_POINTER (menu_id), items);
597 point = g_sequence_get_iter_at_pos (items, position + removed);
599 g_return_if_fail (point != NULL);
601 if (removed)
603 GSequenceIter *start;
605 start = g_sequence_get_iter_at_pos (items, position);
606 g_sequence_remove_range (start, point);
609 n_added = g_variant_iter_init (&iter, added);
610 while (g_variant_iter_loop (&iter, "@a{sv}", &item))
611 g_sequence_insert_before (point, g_menu_proxy_group_create_item (item));
613 if (g_sequence_get_length (items) == 0)
615 g_hash_table_remove (group->menus, GINT_TO_POINTER (menu_id));
616 items = NULL;
619 if ((proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id))))
620 g_menu_proxy_changed (proxy, items, position, removed, n_added);
623 static GMenuProxyGroup *
624 g_menu_proxy_group_get_from_path (GMenuProxyPath *path,
625 guint group_id)
627 GMenuProxyGroup *group;
629 group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
631 if (group == NULL)
633 group = g_slice_new (GMenuProxyGroup);
634 group->path = g_menu_proxy_path_ref (path);
635 group->id = group_id;
636 group->proxies = g_hash_table_new (NULL, NULL);
637 group->menus = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_sequence_free);
638 group->state = GROUP_OFFLINE;
639 group->active = 0;
640 group->ref_count = 0;
642 g_hash_table_insert (path->groups, GINT_TO_POINTER (group->id), group);
645 return g_menu_proxy_group_ref (group);
648 static GMenuProxyGroup *
649 g_menu_proxy_group_get (GDBusConnection *connection,
650 const gchar *bus_name,
651 const gchar *object_path,
652 guint group_id)
654 GMenuProxyGroup *group;
655 GMenuProxyPath *path;
657 path = g_menu_proxy_path_get (connection, bus_name, object_path);
658 group = g_menu_proxy_group_get_from_path (path, group_id);
659 g_menu_proxy_path_unref (path);
661 return group;
664 /* GMenuProxy {{{1 */
666 typedef GMenuModelClass GMenuProxyClass;
667 struct _GMenuProxy
669 GMenuModel parent;
671 GMenuProxyGroup *group;
672 guint id;
674 GSequence *items; /* unowned */
675 gboolean active;
678 G_DEFINE_TYPE (GMenuProxy, g_menu_proxy, G_TYPE_MENU_MODEL)
680 static gboolean
681 g_menu_proxy_is_mutable (GMenuModel *model)
683 return TRUE;
686 static gint
687 g_menu_proxy_get_n_items (GMenuModel *model)
689 GMenuProxy *proxy = G_MENU_PROXY (model);
691 if (!proxy->active)
693 g_menu_proxy_group_activate (proxy->group);
694 proxy->active = TRUE;
697 return proxy->items ? g_sequence_get_length (proxy->items) : 0;
700 static void
701 g_menu_proxy_get_item_attributes (GMenuModel *model,
702 gint item_index,
703 GHashTable **table)
705 GMenuProxy *proxy = G_MENU_PROXY (model);
706 GMenuProxyItem *item;
707 GSequenceIter *iter;
709 g_return_if_fail (proxy->active);
710 g_return_if_fail (proxy->items);
712 iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
713 g_return_if_fail (iter);
715 item = g_sequence_get (iter);
716 g_return_if_fail (item);
718 *table = g_hash_table_ref (item->attributes);
721 static void
722 g_menu_proxy_get_item_links (GMenuModel *model,
723 gint item_index,
724 GHashTable **table)
726 GMenuProxy *proxy = G_MENU_PROXY (model);
727 GMenuProxyItem *item;
728 GSequenceIter *iter;
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_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
742 GHashTableIter tmp;
743 gpointer key;
744 gpointer value;
746 g_hash_table_iter_init (&tmp, item->links);
747 while (g_hash_table_iter_next (&tmp, &key, &value))
749 if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(uu)")))
751 guint group_id, menu_id;
752 GMenuProxyGroup *group;
753 GMenuProxy *link;
755 g_variant_get (value, "(uu)", &group_id, &menu_id);
757 /* save the hash lookup in a relatively common case */
758 if (proxy->group->id != group_id)
759 group = g_menu_proxy_group_get_from_path (proxy->group->path, group_id);
760 else
761 group = g_menu_proxy_group_ref (proxy->group);
763 link = g_menu_proxy_get_from_group (group, menu_id);
765 g_hash_table_insert (*table, g_strdup (key), link);
767 g_menu_proxy_group_unref (group);
773 static void
774 g_menu_proxy_finalize (GObject *object)
776 GMenuProxy *proxy = G_MENU_PROXY (object);
778 if (proxy->active)
779 g_menu_proxy_group_deactivate (proxy->group);
781 g_hash_table_remove (proxy->group->proxies, GINT_TO_POINTER (proxy->id));
782 g_menu_proxy_group_unref (proxy->group);
784 G_OBJECT_CLASS (g_menu_proxy_parent_class)
785 ->finalize (object);
788 static void
789 g_menu_proxy_init (GMenuProxy *proxy)
793 static void
794 g_menu_proxy_class_init (GMenuProxyClass *class)
796 GObjectClass *object_class = G_OBJECT_CLASS (class);
798 class->is_mutable = g_menu_proxy_is_mutable;
799 class->get_n_items = g_menu_proxy_get_n_items;
800 class->get_item_attributes = g_menu_proxy_get_item_attributes;
801 class->get_item_links = g_menu_proxy_get_item_links;
803 object_class->finalize = g_menu_proxy_finalize;
806 static void
807 g_menu_proxy_changed (GMenuProxy *proxy,
808 GSequence *items,
809 gint position,
810 gint removed,
811 gint added)
813 proxy->items = items;
815 if (proxy->active && (removed || added))
816 g_menu_model_items_changed (G_MENU_MODEL (proxy), position, removed, added);
819 static GMenuProxy *
820 g_menu_proxy_get_from_group (GMenuProxyGroup *group,
821 guint menu_id)
823 GMenuProxy *proxy;
825 proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id));
826 if (proxy)
827 g_object_ref (proxy);
829 if (proxy == NULL)
831 proxy = g_object_new (G_TYPE_MENU_PROXY, NULL);
832 proxy->items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
833 g_hash_table_insert (group->proxies, GINT_TO_POINTER (menu_id), proxy);
834 proxy->group = g_menu_proxy_group_ref (group);
835 proxy->id = menu_id;
838 return proxy;
842 * g_menu_proxy_get:
843 * @connection: a #GDBusConnection
844 * @bus_name: the bus name which exports the menu model
845 * @object_path: the object path at which the menu model is exported
847 * Obtains a #GMenuProxy for the menu model which is exported
848 * at the given @bus_name and @object_path.
850 * Returns: (transfer full): a #GMenuProxy object. Free with g_object_unref().
852 GMenuProxy *
853 g_menu_proxy_get (GDBusConnection *connection,
854 const gchar *bus_name,
855 const gchar *object_path)
857 GMenuProxyGroup *group;
858 GMenuProxy *proxy;
860 group = g_menu_proxy_group_get (connection, bus_name, object_path, 0);
861 proxy = g_menu_proxy_get_from_group (group, 0);
862 g_menu_proxy_group_unref (group);
864 return proxy;
867 #if 0
868 static void
869 dump_proxy (gpointer key, gpointer value, gpointer data)
871 GMenuProxy *proxy = value;
873 g_print (" menu %d refcount %d active %d\n",
874 proxy->id, G_OBJECT (proxy)->ref_count, proxy->active);
877 static void
878 dump_group (gpointer key, gpointer value, gpointer data)
880 GMenuProxyGroup *group = value;
882 g_print (" group %d refcount %d state %d active %d\n",
883 group->id, group->ref_count, group->state, group->active);
885 g_hash_table_foreach (group->proxies, dump_proxy, NULL);
888 static void
889 dump_path (gpointer key, gpointer value, gpointer data)
891 PathIdentifier *pid = key;
892 GMenuProxyPath *path = value;
894 g_print ("%s active %d\n", pid->object_path, path->active);
895 g_hash_table_foreach (path->groups, dump_group, NULL);
898 void
899 g_menu_proxy_dump (void)
901 g_hash_table_foreach (g_menu_proxy_paths, dump_path, NULL);
904 #endif
906 /* Epilogue {{{1 */
907 /* vim:set foldmethod=marker: */