Add debug code
[platform/upstream/glib.git] / gio / gmenuproxy.c
1 /*
2  * Copyright © 2011 Canonical Ltd.
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "gmenuproxy.h"
23
24 #include "gmenumodel.h"
25
26 /* Prelude {{{1 */
27
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>
33  *
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().
37  */
38
39 /*
40  * There are 3 main (quasi-)classes involved here:
41  *
42  *   - GMenuProxyPath
43  *   - GMenuProxyGroup
44  *   - GMenuProxy
45  *
46  * Each of these classes exists as a parameterised singleton keyed to a
47  * particular thing:
48  *
49  *   - GMenuProxyPath represents a D-Bus object path on a particular
50  *     unique bus name on a particular GDBusConnection.
51  *
52  *   - GMenuProxyGroup represents a particular group on a particular
53  *     GMenuProxyPath.
54  *
55  *   - GMenuProxy represents a particular menu within a particular
56  *     GMenuProxyGroup.
57  *
58  * There are also two (and a half) utility structs:
59  *
60  *  - PathIdentifier and ConstPathIdentifier
61  *  - GMenuProxyItem
62  *
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.
67  *
68  * We have a 3-level hierarchy of hashtables:
69  *
70  *   - a global hashtable (g_menu_proxy_paths) maps from PathIdentifier
71  *     to GMenuProxyPath
72  *
73  *   - each GMenuProxyPath has a hashtable mapping from guint (group
74  *     number) to GMenuProxyGroup
75  *
76  *   - each GMenuProxyGroup has a hashtable mapping from guint (menu
77  *     number) to GMenuProxy.
78  *
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.
82  *
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.
87  *
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.
93  *
94  * In addition to refcounts, each object has an 'active' variable (ints
95  * for GMenuProxyPath and GMenuProxyGroup, boolean for GMenuProxy).
96  *
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).
101  *
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.
108  *
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.
115  *
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.
120  *
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.
129  *
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.....
134  *
135  * Following the "empty is the same as non-existent" rule, the hashtable
136  * of GSequence of GMenuProxyItem holds NULL for empty menus.
137  *
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.
143  */
144
145 typedef struct _GMenuProxyGroup GMenuProxyGroup;
146 typedef struct _GMenuProxyPath GMenuProxyPath;
147
148 static void                     g_menu_proxy_group_changed              (GMenuProxyGroup *group,
149                                                                          guint            menu_id,
150                                                                          gint             position,
151                                                                          gint             removed,
152                                                                          GVariant        *added);
153 static void                     g_menu_proxy_changed                    (GMenuProxy      *proxy,
154                                                                          GSequence       *items,
155                                                                          gint             position,
156                                                                          gint             removed,
157                                                                          gint             added);
158 static GMenuProxyGroup *        g_menu_proxy_group_get_from_path        (GMenuProxyPath  *path,
159                                                                          guint            group_id);
160 static GMenuProxy *             g_menu_proxy_get_from_group             (GMenuProxyGroup *group,
161                                                                          guint            menu_id);
162
163 /* PathIdentifier {{{1 */
164 typedef struct
165 {
166   GDBusConnection *connection;
167   gchar *bus_name;
168   gchar *object_path;
169 } PathIdentifier;
170
171 typedef const struct
172 {
173   GDBusConnection *connection;
174   const gchar *bus_name;
175   const gchar *object_path;
176 } ConstPathIdentifier;
177
178 static guint
179 path_identifier_hash (gconstpointer data)
180 {
181   ConstPathIdentifier *id = data;
182
183   return g_str_hash (id->object_path);
184 }
185
186 static gboolean
187 path_identifier_equal (gconstpointer a,
188                        gconstpointer b)
189 {
190   ConstPathIdentifier *id_a = a;
191   ConstPathIdentifier *id_b = b;
192
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);
196 }
197
198 static void
199 path_identifier_free (PathIdentifier *id)
200 {
201   g_object_unref (id->connection);
202   g_free (id->bus_name);
203   g_free (id->object_path);
204
205   g_slice_free (PathIdentifier, id);
206 }
207
208 static PathIdentifier *
209 path_identifier_new (ConstPathIdentifier *cid)
210 {
211   PathIdentifier *id;
212
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);
217
218   return id;
219 }
220
221 /* GMenuProxyPath {{{1 */
222
223 struct _GMenuProxyPath
224 {
225   PathIdentifier *id;
226   gint ref_count;
227
228   GHashTable *groups;
229   gint active;
230   guint watch_id;
231 };
232
233 static GHashTable *g_menu_proxy_paths;
234
235 static GMenuProxyPath *
236 g_menu_proxy_path_ref (GMenuProxyPath *path)
237 {
238   path->ref_count++;
239
240   return path;
241 }
242
243 static void
244 g_menu_proxy_path_unref (GMenuProxyPath *path)
245 {
246   if (--path->ref_count == 0)
247     {
248       g_hash_table_remove (g_menu_proxy_paths, path->id);
249       g_hash_table_unref (path->groups);
250       path_identifier_free (path->id);
251
252       g_slice_free (GMenuProxyPath, path);
253     }
254 }
255
256 static void
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,
263                           gpointer         user_data)
264 {
265   GMenuProxyPath *path = user_data;
266   GVariantIter *iter;
267   guint group_id;
268   guint menu_id;
269   guint position;
270   guint removes;
271   GVariant *adds;
272
273   if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))")))
274     return;
275
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))
278     {
279       GMenuProxyGroup *group;
280
281       group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
282
283       if (group != NULL)
284         g_menu_proxy_group_changed (group, menu_id, position, removes, adds);
285     }
286   g_variant_iter_free (iter);
287 }
288
289 static void
290 g_menu_proxy_path_activate (GMenuProxyPath *path)
291 {
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);
297 }
298
299 static void
300 g_menu_proxy_path_deactivate (GMenuProxyPath *path)
301 {
302   if (--path->active == 0)
303     g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
304 }
305
306 static GMenuProxyPath *
307 g_menu_proxy_path_get (GDBusConnection *connection,
308                        const gchar     *bus_name,
309                        const gchar     *object_path)
310 {
311   ConstPathIdentifier cid = { connection, bus_name, object_path };
312   GMenuProxyPath *path;
313
314   if (g_menu_proxy_paths == NULL)
315     g_menu_proxy_paths = g_hash_table_new (path_identifier_hash, path_identifier_equal);
316
317   path = g_hash_table_lookup (g_menu_proxy_paths, &cid);
318
319   if (path == NULL)
320     {
321       path = g_slice_new (GMenuProxyPath);
322       path->id = path_identifier_new (&cid);
323       path->groups = g_hash_table_new (NULL, NULL);
324       path->ref_count = 0;
325       path->active = 0;
326
327       g_hash_table_insert (g_menu_proxy_paths, path->id, path);
328     }
329
330   return g_menu_proxy_path_ref (path);
331 }
332
333 /* GMenuProxyGroup, GMenuProxyItem {{{1 */
334 typedef enum
335 {
336   GROUP_OFFLINE,
337   GROUP_PENDING,
338   GROUP_ONLINE
339 } GroupStatus;
340
341 struct _GMenuProxyGroup
342 {
343   GMenuProxyPath *path;
344   guint id;
345
346   GHashTable *proxies; /* uint -> unowned GMenuProxy */
347   GHashTable *menus;   /* uint -> owned GSequence */
348   gint ref_count;
349   GroupStatus state;
350   gint active;
351 };
352
353 typedef struct
354 {
355   GHashTable *attributes;
356   GHashTable *links;
357 } GMenuProxyItem;
358
359 static GMenuProxyGroup *
360 g_menu_proxy_group_ref (GMenuProxyGroup *group)
361 {
362   group->ref_count++;
363
364   return group;
365 }
366
367 static void
368 g_menu_proxy_group_unref (GMenuProxyGroup *group)
369 {
370   if (--group->ref_count == 0)
371     {
372       g_assert (group->state == GROUP_OFFLINE);
373       g_assert (group->active == 0);
374
375       g_hash_table_remove (group->path->groups, GINT_TO_POINTER (group->id));
376       g_hash_table_unref (group->proxies);
377
378       g_menu_proxy_path_unref (group->path);
379
380       g_slice_free (GMenuProxyGroup, group);
381     }
382 }
383
384 static void
385 g_menu_proxy_item_free (gpointer data)
386 {
387   GMenuProxyItem *item = data;
388
389   g_hash_table_unref (item->attributes);
390   g_hash_table_unref (item->links);
391
392   g_slice_free (GMenuProxyItem, item);
393 }
394
395 static GMenuProxyItem *
396 g_menu_proxy_group_create_item (GMenuProxyGroup *context,
397                                 GVariant        *description)
398 {
399   GMenuProxyItem *item;
400   GVariantIter iter;
401   const gchar *key;
402   GVariant *value;
403
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);
407
408   g_variant_iter_init (&iter, description);
409   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
410     if (key[0] == ':')
411       {
412         if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(uu)")))
413           {
414             guint group_id, menu_id;
415             GMenuProxyGroup *group;
416             GMenuProxy *proxy;
417
418             g_variant_get (value, "(uu)", &group_id, &menu_id);
419
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);
423             else
424               group = g_menu_proxy_group_ref (context);
425
426             proxy = g_menu_proxy_get_from_group (group, menu_id);
427
428             /* key + 1 to skip the ':' */
429             g_hash_table_insert (item->links, g_strdup (key + 1), proxy);
430
431             g_menu_proxy_group_unref (group);
432           }
433       }
434     else
435       g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));
436
437   return item;
438 }
439
440 /*
441  * GMenuProxyGroup can be in three states:
442  *
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
447  *
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:
454  *
455  *  - activation requested
456  *  - deactivation requested
457  *  - Start call finishes
458  *
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.
461  *
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
466  *
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
471  *
472  * Start call finishes:
473  *   OFFLINE: this should not be possible
474  *   PENDING:
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
478  *
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).
483  *
484  * Since there are two places where we transition to OFFLINE, we split
485  * that code out into a separate function.
486  */
487 static void
488 g_menu_proxy_group_go_offline (GMenuProxyGroup *group)
489 {
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,
497                           NULL, NULL, NULL);
498   group->state = GROUP_OFFLINE;
499 }
500
501
502 static void
503 g_menu_proxy_group_start_ready (GObject      *source_object,
504                                 GAsyncResult *result,
505                                 gpointer      user_data)
506 {
507   GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
508   GMenuProxyGroup *group = user_data;
509   GVariant *reply;
510
511   g_assert (group->state == GROUP_PENDING);
512
513   reply = g_dbus_connection_call_finish (connection, result, NULL);
514
515   if (group->active)
516     {
517       group->state = GROUP_ONLINE;
518
519       /* If we receive no reply, just act like we got an empty reply. */
520       if (reply)
521         {
522           GVariantIter *iter;
523           GVariant *items;
524           guint group_id;
525           guint menu_id;
526
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);
532         }
533     }
534   else
535     g_menu_proxy_group_go_offline (group);
536
537   if (reply)
538     g_variant_unref (reply);
539
540   g_menu_proxy_group_unref (group);
541 }
542
543 static void
544 g_menu_proxy_group_activate (GMenuProxyGroup *group)
545 {
546   if (group->active++ == 0)
547     {
548       g_assert (group->state != GROUP_ONLINE);
549
550       if (group->state == GROUP_OFFLINE)
551         {
552           g_menu_proxy_path_activate (group->path);
553
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;
564         }
565     }
566 }
567
568 static void
569 g_menu_proxy_group_deactivate (GMenuProxyGroup *group)
570 {
571   if (--group->active == 0)
572     {
573       g_assert (group->state != GROUP_OFFLINE);
574
575       if (group->state == GROUP_ONLINE)
576         {
577           /* We are here because nobody is watching, so just free
578            * everything and don't bother with the notifications.
579            */
580           g_hash_table_remove_all (group->menus);
581
582           g_menu_proxy_group_go_offline (group);
583         }
584     }
585 }
586
587 static void
588 g_menu_proxy_group_changed (GMenuProxyGroup *group,
589                             guint            menu_id,
590                             gint             position,
591                             gint             removed,
592                             GVariant        *added)
593 {
594   GSequenceIter *point;
595   GVariantIter iter;
596   GMenuProxy *proxy;
597   GSequence *items;
598   GVariant *item;
599   gint n_added;
600
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
605    * ourselves.
606    */
607   if (group->state != GROUP_ONLINE)
608     return;
609
610   items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
611
612   if (items == NULL)
613     {
614       items = g_sequence_new (g_menu_proxy_item_free);
615       g_hash_table_insert (group->menus, GINT_TO_POINTER (menu_id), items);
616     }
617
618   point = g_sequence_get_iter_at_pos (items, position + removed);
619
620   g_return_if_fail (point != NULL);
621
622   if (removed)
623     {
624       GSequenceIter *start;
625
626       start = g_sequence_get_iter_at_pos (items, position);
627       g_sequence_remove_range (start, point);
628     }
629
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));
633
634   if (g_sequence_get_length (items) == 0)
635     {
636       g_hash_table_remove (group->menus, GINT_TO_POINTER (menu_id));
637       items = NULL;
638     }
639
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);
642 }
643
644 static GMenuProxyGroup *
645 g_menu_proxy_group_get_from_path (GMenuProxyPath *path,
646                                   guint           group_id)
647 {
648   GMenuProxyGroup *group;
649
650   group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
651
652   if (group == NULL)
653     {
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;
660       group->active = 0;
661       group->ref_count = 0;
662
663       g_hash_table_insert (path->groups, GINT_TO_POINTER (group->id), group);
664     }
665
666   return g_menu_proxy_group_ref (group);
667 }
668
669 static GMenuProxyGroup *
670 g_menu_proxy_group_get (GDBusConnection *connection,
671                         const gchar     *bus_name,
672                         const gchar     *object_path,
673                         guint            group_id)
674 {
675   GMenuProxyGroup *group;
676   GMenuProxyPath *path;
677
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);
681
682   return group;
683 }
684
685 /* GMenuProxy {{{1 */
686
687 typedef GMenuModelClass GMenuProxyClass;
688 struct _GMenuProxy
689 {
690   GMenuModel parent;
691
692   GMenuProxyGroup *group;
693   guint id;
694
695   GSequence *items; /* unowned */
696   gboolean active;
697 };
698
699 G_DEFINE_TYPE (GMenuProxy, g_menu_proxy, G_TYPE_MENU_MODEL)
700
701 static gboolean
702 g_menu_proxy_is_mutable (GMenuModel *model)
703 {
704   return TRUE;
705 }
706
707 static gint
708 g_menu_proxy_get_n_items (GMenuModel *model)
709 {
710   GMenuProxy *proxy = G_MENU_PROXY (model);
711
712   if (!proxy->active)
713     {
714       g_menu_proxy_group_activate (proxy->group);
715       proxy->active = TRUE;
716     }
717
718   return proxy->items ? g_sequence_get_length (proxy->items) : 0;
719 }
720
721 static void
722 g_menu_proxy_get_item_attributes (GMenuModel  *model,
723                                   gint         item_index,
724                                   GHashTable **table)
725 {
726   GMenuProxy *proxy = G_MENU_PROXY (model);
727   GMenuProxyItem *item;
728   GSequenceIter *iter;
729
730   g_return_if_fail (proxy->active);
731   g_return_if_fail (proxy->items);
732
733   iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
734   g_return_if_fail (iter);
735
736   item = g_sequence_get (iter);
737   g_return_if_fail (item);
738
739   *table = g_hash_table_ref (item->attributes);
740 }
741
742 static void
743 g_menu_proxy_get_item_links (GMenuModel  *model,
744                              gint         item_index,
745                              GHashTable **table)
746 {
747   GMenuProxy *proxy = G_MENU_PROXY (model);
748   GMenuProxyItem *item;
749   GSequenceIter *iter;
750
751   g_return_if_fail (proxy->active);
752   g_return_if_fail (proxy->items);
753
754   iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
755   g_return_if_fail (iter);
756
757   item = g_sequence_get (iter);
758   g_return_if_fail (item);
759
760   *table = g_hash_table_ref (item->links);
761 }
762
763 static void
764 g_menu_proxy_finalize (GObject *object)
765 {
766   GMenuProxy *proxy = G_MENU_PROXY (object);
767
768   if (proxy->active)
769     g_menu_proxy_group_deactivate (proxy->group);
770
771   g_hash_table_remove (proxy->group->proxies, GINT_TO_POINTER (proxy->id));
772   g_menu_proxy_group_unref (proxy->group);
773
774   G_OBJECT_CLASS (g_menu_proxy_parent_class)
775     ->finalize (object);
776 }
777
778 static void
779 g_menu_proxy_init (GMenuProxy *proxy)
780 {
781 }
782
783 static void
784 g_menu_proxy_class_init (GMenuProxyClass *class)
785 {
786   GObjectClass *object_class = G_OBJECT_CLASS (class);
787
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;
792
793   object_class->finalize = g_menu_proxy_finalize;
794 }
795
796 static void
797 g_menu_proxy_changed (GMenuProxy *proxy,
798                       GSequence  *items,
799                       gint        position,
800                       gint        removed,
801                       gint        added)
802 {
803   proxy->items = items;
804
805   if (proxy->active && (removed || added))
806     g_menu_model_items_changed (G_MENU_MODEL (proxy), position, removed, added);
807 }
808
809 static GMenuProxy *
810 g_menu_proxy_get_from_group (GMenuProxyGroup *group,
811                              guint            menu_id)
812 {
813   GMenuProxy *proxy;
814
815   proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id));
816   if (proxy)
817     g_object_ref (proxy);
818
819   if (proxy == NULL)
820     {
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);
825       proxy->id = menu_id;
826     }
827
828   return proxy;
829 }
830
831 /**
832  * g_menu_proxy_get:
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
836  *
837  * Obtains a #GMenuProxy for the menu model which is exported
838  * at the given @bus_name and @object_path.
839  *
840  * Returns: (transfer full): a #GMenuProxy object. Free with g_object_unref().
841  */
842 GMenuProxy *
843 g_menu_proxy_get (GDBusConnection *connection,
844                   const gchar     *bus_name,
845                   const gchar     *object_path)
846 {
847   GMenuProxyGroup *group;
848   GMenuProxy *proxy;
849
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);
853
854   return proxy;
855 }
856
857 static void
858 dump_proxy (gpointer key, gpointer value, gpointer data)
859 {
860   GMenuProxy *proxy = value;
861
862   g_print ("    menu %d refcount %d active %d\n",
863            proxy->id, G_OBJECT (proxy)->ref_count, proxy->active);
864 }
865
866 static void
867 dump_group (gpointer key, gpointer value, gpointer data)
868 {
869   GMenuProxyGroup *group = value;
870
871   g_print ("  group %d refcount %d state %d active %d\n",
872            group->id, group->ref_count, group->state, group->active);
873
874   g_hash_table_foreach (group->proxies, dump_proxy, NULL);
875 }
876
877 static void
878 dump_path (gpointer key, gpointer value, gpointer data)
879 {
880   PathIdentifier *pid = key;
881   GMenuProxyPath *path = value;
882
883   g_print ("%s active %d\n", pid->object_path, path->active);
884   g_hash_table_foreach (path->groups, dump_group, NULL);
885 }
886
887 void
888 g_menu_proxy_dump (void)
889 {
890   g_hash_table_foreach (g_menu_proxy_paths, dump_path, NULL);
891 }
892
893
894 /* Epilogue {{{1 */
895 /* vim:set foldmethod=marker: */