Add GMenuModel
[platform/upstream/glib.git] / gio / gmenumodel.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 "gmenumodel.h"
23
24 /**
25  * SECTION:gmenumodel
26  * @title: GMenuModel
27  * @short_description: An abstract class representing the contents of a menu
28  * @see_also: #GActionGroup
29  *
30  * #GMenuModel represents the contents of a menu -- an ordered list of
31  * menu items. The items are associated with actions, which can be
32  * activated through them. Items can be grouped in sections, and may
33  * have submenus associated with them. Both items and sections usually
34  * have some representation data, such as labels or icons. The type of
35  * the associated action (ie whether it is stateful, and what kind of
36  * state it has) can influence the representation of the item.
37  *
38  * The conceptual model of menus in #GMenuModel is hierarchical:
39  * sections and submenus are again represented by #GMenuModels.
40  * Menus themselves do not define their own roles. Rather, the role
41  * of a particular #GMenuModel is defined by the item that references
42  * it (or, in the case of the 'root' menu, is defined by the context
43  * in which it is used).
44  *
45  * As an example, consider the visible portions of the menu in
46  * <xref linkend="menu-example"/>.
47  *
48  * <figure id="menu-example">
49  *   <title>An example menu</title>
50  *   <graphic fileref="menu-example.png" format="PNG"></graphic>
51  * </figure>
52  *
53  * There are 8 "menus" visible in the screenshot: one menubar, two
54  * submenus and 5 sections:
55  * <itemizedlist>
56  * <listitem>the toplevel menubar (containing 4 items)</listitem>
57  * <listitem>the View submenu (containing 3 sections)</listitem>
58  * <listitem>the first section of the View submenu (containing 2 items)</listitem>
59  * <listitem>the second section of the View submenu (containing 1 item)</listitem>
60  * <listitem>the final section of the View submenu (containing 1 item)</listitem>
61  * <listitem>the Highlight Mode submenu (containing 2 sections)</listitem>
62  * <listitem>the Sources section (containing 2 items)</listitem>
63  * <listitem>the Markup section (containing 2 items)</listitem>
64  * </itemizedlist>
65  *
66  * <xref linkend="menu-model"/> illustrates the conceptual connection between
67  * these 8 menus. Each large block in the figure represents a menu and the
68  * smaller blocks within the large block represent items in that menu. Some
69  * items contain references to other menus.
70  *
71  * <figure id="menu-model">
72  *   <title>A menu model</title>
73  *   <graphic fileref="menu-model.png" format="PNG"></graphic>
74  * </figure>
75  *
76  * Notice that the separators visible in <xref linkend="menu-example"/>
77  * appear nowhere in <xref linkend="menu-model"/>. This is because
78  * separators are not explicitly represented in the menu model. Instead,
79  * a separator is inserted between any two non-empty sections of a menu.
80  * Section items can have labels just like any other item. In that case,
81  * a display system may show a section header instead of a separator.
82  *
83  * The motivation for this abstract model of application controls is
84  * that modern user interfaces tend to make these controls available
85  * outside the application. Examples include global menus, jumplists,
86  * dash boards, etc. To support such uses, it is necessary to 'export'
87  * information about actions and their representation in menus, which
88  * is exactly what the
89  * <link linkend="gio-GActionGroup-exporter">GActionGroup exporter</link>
90  * and the
91  * <link linkend="gio-GMenuModel-exporter">GMenuModel exporter</link>
92  * do for #GActionGroup and #GMenuModel. The client-side counterparts
93  * to make use of the exported information are #GDBusActionGroup and
94  * #GMenuProxy.
95  *
96  * The API of #GMenuModel is very generic, with iterators for the
97  * attributes and links of an item, see g_menu_model_iterate_item_attributes()
98  * and g_menu_model_iterate_item_links(). The 'standard' attributes and
99  * link types have predefined names: %G_MENU_ATTRIBUTE_LABEL,
100  * %G_MENU_ATTRIBUTE_ACTION, %G_MENU_ATTRIBUTE_TARGET, %G_MENU_LINK_SECTION
101  * and %G_MENU_LINK_SUBMENU.
102  *
103  * FIXME: explain how items are associated with actions.
104  *
105  * While a wide variety of stateful actions is possible, the following
106  * is the minimum that is expected to be supported by all users of exported
107  * menu information:
108  * <itemizedlist>
109  * <listitem>an action with no parameter type and no state</listitem>
110  * <listitem>an action with no parameter type and boolean state</listitem>
111  * <listitem>an action with string parameter type and string state</listitem>
112  * </itemizedlist>
113  *
114  * <formalpara><title>Stateless</title>
115  * <para>
116  * A stateless action typically corresponds to an ordinary menu item.
117  * </para>
118  * <para>
119  * Selecting such a menu item will activate the action (with no parameter).
120  * </para>
121  * </formalpara>
122  *
123  * <formalpara><title>Boolean State</title>
124  * <para>
125  * An action with a boolean state will most typically be used with a "toggle"
126  * or "switch" menu item. The state can be set directly, but activating the
127  * action (with no parameter) results in the state being toggled.
128  * </para>
129  * <para>
130  * Selecting a toggle menu item will activate the action. The menu item should
131  * be rendered as "checked" when the state is true.
132  * </para>
133  * </formalpara>
134  *
135  * <formalpara><title>String Parameter and State</title>
136  * <para>
137  * Actions with string parameters and state will most typically be used to
138  * represent an enumerated choice over the items available for a group of
139  * radio menu items. Activating the action with a string parameter is
140  * equivalent to setting that parameter as the state.
141  * </para>
142  * <para>
143  * Radio menu items, in addition to being associated with the action, will
144  * have a target value. Selecting that menu item will result in activation
145  * of the action with the target value as the parameter. The menu item should
146  * be rendered as "selected" when the state of the action is equal to the
147  * target value of the menu item.
148  * </para>
149  * </formalpara>
150  */
151
152 /**
153  * GMenuModel:
154  *
155  * #GMenuModel is an opaque structure type.  You must access it using the
156  * functions below.
157  **/
158
159 /**
160  * GMenuAttributeIter:
161  *
162  * #GMenuAttributeIter is an opaque structure type.  You must access it
163  * using the functions below.
164  **/
165
166 /**
167  * GMenuLinkIter:
168  *
169  * #GMenuLinkIter is an opaque structure type.  You must access it using
170  * the functions below.
171  **/
172
173 typedef struct
174 {
175   GMenuLinkIter parent_instance;
176   GHashTableIter iter;
177   GHashTable *table;
178 } GMenuLinkHashIter;
179
180 typedef GMenuLinkIterClass GMenuLinkHashIterClass;
181
182 G_DEFINE_TYPE (GMenuLinkHashIter, g_menu_link_hash_iter, G_TYPE_MENU_LINK_ITER)
183
184 static gboolean
185 g_menu_link_hash_iter_get_next (GMenuLinkIter  *link_iter,
186                                 const gchar   **out_name,
187                                 GMenuModel    **value)
188 {
189   GMenuLinkHashIter *iter = (GMenuLinkHashIter *) link_iter;
190   gpointer keyptr, valueptr;
191
192   if (!g_hash_table_iter_next (&iter->iter, &keyptr, &valueptr))
193     return FALSE;
194
195   *out_name = keyptr;
196   *value = g_object_ref (valueptr);
197
198   return TRUE;
199 }
200
201 static void
202 g_menu_link_hash_iter_finalize (GObject *object)
203 {
204   GMenuLinkHashIter *iter = (GMenuLinkHashIter *) object;
205
206   g_hash_table_unref (iter->table);
207
208   G_OBJECT_CLASS (g_menu_link_hash_iter_parent_class)
209     ->finalize (object);
210 }
211
212 static void
213 g_menu_link_hash_iter_init (GMenuLinkHashIter *iter)
214 {
215 }
216
217 static void
218 g_menu_link_hash_iter_class_init (GMenuLinkHashIterClass *class)
219 {
220   GObjectClass *object_class = G_OBJECT_CLASS (class);
221
222   object_class->finalize = g_menu_link_hash_iter_finalize;
223   class->get_next = g_menu_link_hash_iter_get_next;
224 }
225
226
227 typedef struct
228 {
229   GMenuAttributeIter parent_instance;
230   GHashTableIter iter;
231   GHashTable *table;
232 } GMenuAttributeHashIter;
233
234 typedef GMenuAttributeIterClass GMenuAttributeHashIterClass;
235
236 G_DEFINE_TYPE (GMenuAttributeHashIter, g_menu_attribute_hash_iter, G_TYPE_MENU_ATTRIBUTE_ITER)
237
238 static gboolean
239 g_menu_attribute_hash_iter_get_next (GMenuAttributeIter  *attr_iter,
240                                      const gchar        **name,
241                                      GVariant           **value)
242 {
243   GMenuAttributeHashIter *iter = (GMenuAttributeHashIter *) attr_iter;
244   gpointer keyptr, valueptr;
245
246   if (!g_hash_table_iter_next (&iter->iter, &keyptr, &valueptr))
247     return FALSE;
248
249   *name = keyptr;
250
251   *value = g_variant_ref (valueptr);
252
253   return TRUE;
254 }
255
256 static void
257 g_menu_attribute_hash_iter_finalize (GObject *object)
258 {
259   GMenuAttributeHashIter *iter = (GMenuAttributeHashIter *) object;
260
261   g_hash_table_unref (iter->table);
262
263   G_OBJECT_CLASS (g_menu_attribute_hash_iter_parent_class)
264     ->finalize (object);
265 }
266
267 static void
268 g_menu_attribute_hash_iter_init (GMenuAttributeHashIter *iter)
269 {
270 }
271
272 static void
273 g_menu_attribute_hash_iter_class_init (GMenuAttributeHashIterClass *class)
274 {
275   GObjectClass *object_class = G_OBJECT_CLASS (class);
276
277   object_class->finalize = g_menu_attribute_hash_iter_finalize;
278   class->get_next = g_menu_attribute_hash_iter_get_next;
279 }
280
281 G_DEFINE_ABSTRACT_TYPE (GMenuModel, g_menu_model, G_TYPE_OBJECT)
282
283
284 guint g_menu_model_items_changed_signal;
285
286 static GMenuAttributeIter *
287 g_menu_model_real_iterate_item_attributes (GMenuModel *model,
288                                            gint        item_index)
289 {
290   GHashTable *table = NULL;
291   GMenuAttributeIter *result;
292
293   G_MENU_MODEL_GET_CLASS (model)->get_item_attributes (model, item_index, &table);
294
295   if (table)
296     {
297       GMenuAttributeHashIter *iter = g_object_new (g_menu_attribute_hash_iter_get_type (), NULL);
298       g_hash_table_iter_init (&iter->iter, table);
299       iter->table = g_hash_table_ref (table);
300       result = G_MENU_ATTRIBUTE_ITER (iter);
301     }
302   else
303     {
304       g_critical ("GMenuModel implementation '%s' doesn't override iterate_item_attributes() "
305                   "and fails to return sane values from get_item_attributes()",
306                   G_OBJECT_TYPE_NAME (model));
307       result = NULL;
308     }
309
310   if (table != NULL)
311     g_hash_table_unref (table);
312
313   return result;
314 }
315
316 static GVariant *
317 g_menu_model_real_get_item_attribute_value (GMenuModel         *model,
318                                             gint                item_index,
319                                             const gchar        *attribute,
320                                             const GVariantType *expected_type)
321 {
322   GHashTable *table = NULL;
323   GVariant *value = NULL;
324
325   G_MENU_MODEL_GET_CLASS (model)
326     ->get_item_attributes (model, item_index, &table);
327
328   if (table != NULL)
329     {
330       value = g_hash_table_lookup (table, attribute);
331
332       if (value != NULL)
333         {
334           if (expected_type == NULL || g_variant_is_of_type (value, expected_type))
335             value = g_variant_ref (value);
336           else
337             value = NULL;
338         }
339     }
340   else
341     g_assert_not_reached ();
342
343   if (table != NULL)
344     g_hash_table_unref (table);
345
346   return value;
347 }
348
349 static GMenuLinkIter *
350 g_menu_model_real_iterate_item_links (GMenuModel *model,
351                                       gint        item_index)
352 {
353   GHashTable *table = NULL;
354   GMenuLinkIter *result;
355
356   G_MENU_MODEL_GET_CLASS (model)
357     ->get_item_links (model, item_index, &table);
358
359   if (table)
360     {
361       GMenuLinkHashIter *iter = g_object_new (g_menu_link_hash_iter_get_type (), NULL);
362       g_hash_table_iter_init (&iter->iter, table);
363       iter->table = g_hash_table_ref (table);
364       result = G_MENU_LINK_ITER (iter);
365     }
366   else
367     {
368       g_critical ("GMenuModel implementation '%s' doesn't override iterate_item_links() "
369                   "and fails to return sane values from get_item_links()",
370                   G_OBJECT_TYPE_NAME (model));
371       result = NULL;
372     }
373
374   if (table != NULL)
375     g_hash_table_unref (table);
376
377   return result;
378 }
379
380 static GMenuModel *
381 g_menu_model_real_get_item_link (GMenuModel  *model,
382                                  gint         item_index,
383                                  const gchar *link)
384 {
385   GHashTable *table = NULL;
386   GMenuModel *value = NULL;
387
388   G_MENU_MODEL_GET_CLASS (model)
389     ->get_item_links (model, item_index, &table);
390
391   if (table != NULL)
392     value = g_hash_table_lookup (table, link);
393   else
394     g_assert_not_reached ();
395
396   if (table != NULL)
397     g_hash_table_unref (table);
398
399   return value ? g_object_ref (value) : NULL;
400 }
401
402 static void
403 g_menu_model_init (GMenuModel *model)
404 {
405 }
406
407 static void
408 g_menu_model_class_init (GMenuModelClass *class)
409 {
410   class->iterate_item_attributes = g_menu_model_real_iterate_item_attributes;
411   class->get_item_attribute_value = g_menu_model_real_get_item_attribute_value;
412   class->iterate_item_links = g_menu_model_real_iterate_item_links;
413   class->get_item_link = g_menu_model_real_get_item_link;
414
415   /**
416    * GMenuModel::items-changed:
417    * @model: the #GMenuModel that is changing
418    * @position: the position of the change
419    * @removed: the number of items removed
420    * @added: the number of items added
421    *
422    * Emitted when a change has occured to the menu.
423    *
424    * The only changes that can occur to a menu is that items are removed
425    * or added.  Items may not change (except by being removed and added
426    * back in the same location).  This signal is capable of describing
427    * both of those changes (at the same time).
428    *
429    * The signal means that starting at the index @position, @removed
430    * items were removed and @added items were added in their place.  If
431    * @removed is zero then only items were added.  If @added is zero
432    * then only items were removed.
433    *
434    * As an example, if the menu contains items a, b, c, d (in that
435    * order) and the signal (2, 1, 3) occurs then the new composition of
436    * the menu will be a, b, _, _, _, d (with each _ representing some
437    * new item).
438    *
439    * Signal handlers may query the model (particularly the added items)
440    * and expect to see the results of the modification that is being
441    * reported.  The signal is emitted after the modification.
442    **/
443   g_menu_model_items_changed_signal =
444     g_signal_new ("items-changed", G_TYPE_MENU_MODEL,
445                   G_SIGNAL_RUN_LAST, 0, NULL, NULL,
446                   g_cclosure_marshal_generic, G_TYPE_NONE,
447                   3, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
448 }
449
450 /**
451  * g_menu_model_is_mutable:
452  * @model: a #GMenuModel
453  *
454  * Queries if @model is mutable.
455  *
456  * An immutable #GMenuModel will never emit the #GMenuModel::items-changed
457  * signal. Consumers of the model may make optimisations accordingly.
458  *
459  * Returns: %TRUE if the model is mutable (ie: "items-changed" may be
460  *          emitted).
461  **/
462 gboolean
463 g_menu_model_is_mutable (GMenuModel *model)
464 {
465   return G_MENU_MODEL_GET_CLASS (model)
466     ->is_mutable (model);
467 }
468
469 /**
470  * g_menu_model_get_n_items:
471  * @model: a #GMenuModel
472  *
473  * Query the number of items in @model.
474  *
475  * Returns: the number of items
476  **/
477 gint
478 g_menu_model_get_n_items (GMenuModel *model)
479 {
480   return G_MENU_MODEL_GET_CLASS (model)
481     ->get_n_items (model);
482 }
483
484 /**
485  * g_menu_model_iterate_item_attributes:
486  * @model: a #GMenuModel
487  * @item_index: the index of the item
488  *
489  * Creates a #GMenuAttributeIter to iterate over the attributes of
490  * the item at position @item_index in @model.
491  *
492  * You must free the iterator with g_object_unref() when you are done.
493  *
494  * Returns: (transfer full): a new #GMenuAttributeIter
495  */
496 GMenuAttributeIter *
497 g_menu_model_iterate_item_attributes (GMenuModel *model,
498                                       gint        item_index)
499 {
500   return G_MENU_MODEL_GET_CLASS (model)
501     ->iterate_item_attributes (model, item_index);
502 }
503
504 /**
505  * g_menu_model_get_item_attribute_value:
506  * @model: a #GMenuModel
507  * @item_index: the index of the item
508  * @attribute: the attribute to query
509  * @expected_type: (allow-none): the expected type of the attribute, or
510  *     %NULL
511  *
512  * Queries the item at position @item_index in @model for the attribute
513  * specified by @attribute.
514  *
515  * If @expected_type is non-%NULL then it specifies the expected type of
516  * the attribute.  If it is %NULL then any type will be accepted.
517  *
518  * If the attribute exists and matches @expected_type (or if the
519  * expected type is unspecified) then the value is returned.
520  *
521  * If the attribute does not exist, or does not match the expected type
522  * then %NULL is returned.
523  *
524  * Returns: (transfer full): the value of the attribute
525  **/
526 GVariant *
527 g_menu_model_get_item_attribute_value (GMenuModel         *model,
528                                        gint                item_index,
529                                        const gchar        *attribute,
530                                        const GVariantType *expected_type)
531 {
532   return G_MENU_MODEL_GET_CLASS (model)
533     ->get_item_attribute_value (model, item_index, attribute, expected_type);
534 }
535
536 /**
537  * g_menu_model_get_item_attribute:
538  * @model: a #GMenuModel
539  * @item_index: the index of the item
540  * @attribute: the attribute to query
541  * @format_string: a #GVariant format string
542  * @...: positional parameters, as per @format_string
543  *
544  * Queries item at position @item_index in @model for the attribute
545  * specified by @attribute.
546  *
547  * If the attribute exists and matches the #GVariantType corresponding
548  * to @format_string then @format_string is used to deconstruct the
549  * value into the positional parameters and %TRUE is returned.
550  *
551  * If the attribute does not exist, or it does exist but has the wrong
552  * type, then the positional parameters are ignored and %FALSE is
553  * returned.
554  *
555  * Returns: %TRUE if the named attribute was found with the expected
556  *          type
557  **/
558 gboolean
559 g_menu_model_get_item_attribute (GMenuModel  *model,
560                                  gint         item_index,
561                                  const gchar *attribute,
562                                  const gchar *format_string,
563                                  ...)
564 {
565   const GVariantType *expected_type;
566   GVariant *value;
567   va_list ap;
568
569   expected_type = NULL; /* XXX devine the type, ensure no '&' */
570
571   value = g_menu_model_get_item_attribute_value (model, item_index, attribute, expected_type);
572   if (value == NULL)
573     return FALSE;
574
575   va_start (ap, format_string);
576   g_variant_get_va (value, format_string, NULL, &ap);
577   g_variant_unref (value);
578   va_end (ap);
579
580   return TRUE;
581 }
582
583 /**
584  * g_menu_model_iterate_item_links:
585  * @model: a #GMenuModel
586  * @item_index: the index of the item
587  *
588  * Creates a #GMenuLinkIter to iterate over the links of the item at
589  * position @item_index in @model.
590  *
591  * You must free the iterator with g_object_unref() when you are done.
592  *
593  * Returns: (transfer full): a new #GMenuLinkIter
594  **/
595 GMenuLinkIter *
596 g_menu_model_iterate_item_links (GMenuModel *model,
597                                  gint        item_index)
598 {
599   return G_MENU_MODEL_GET_CLASS (model)
600     ->iterate_item_links (model, item_index);
601 }
602
603 /**
604  * g_menu_model_get_item_link:
605  * @model: a #GMenuModel
606  * @item_index: the index of the item
607  * @link: the link to query
608  *
609  * Queries the item at position @item_index in @model for the link
610  * specified by @link.
611  *
612  * If the link exists, the linked #GMenuModel is returned.  If the link
613  * does not exist, %NULL is returned.
614  *
615  * Returns: (transfer full): the linked #GMenuModel, or %NULL
616  **/
617 GMenuModel *
618 g_menu_model_get_item_link (GMenuModel *model,
619                             gint        item_index,
620                             const gchar *link)
621 {
622   return G_MENU_MODEL_GET_CLASS (model)
623     ->get_item_link (model, item_index, link);
624 }
625
626 /**
627  * g_menu_model_items_changed:
628  * @model: a #GMenuModel
629  * @position: the position of the change
630  * @removed: the number of items removed
631  * @added: the number of items added
632  *
633  * Requests emission of the #GMenuMode::items-changed signal on @model.
634  *
635  * This function should never be called except by #GMenuModel
636  * subclasses.  Any other calls to this function will very likely lead
637  * to a violation of the interface of the model.
638  *
639  * The implementation should update its internal representation of the
640  * menu before emitting the signal.  The implementation should further
641  * expect to receive queries about the new state of the menu (and
642  * particularly added menu items) while signal handlers are running.
643  *
644  * The implementation must dispatch this call directly from a mainloop
645  * entry and not in response to calls -- particularly those from the
646  * #GMenuModel API.  Said another way: the menu must not change while
647  * user code is running without returning to the mainloop.
648  **/
649 void
650 g_menu_model_items_changed (GMenuModel *model,
651                             gint        position,
652                             gint        removed,
653                             gint        added)
654 {
655   g_signal_emit (model, g_menu_model_items_changed_signal, 0, position, removed, added);
656 }
657
658 G_DEFINE_ABSTRACT_TYPE (GMenuAttributeIter, g_menu_attribute_iter, G_TYPE_OBJECT)
659
660 struct _GMenuAttributeIterPrivate
661 {
662   GQuark name;
663   GVariant *value;
664   gboolean valid;
665 };
666
667 /**
668  * g_menu_attribute_iter_get_next:
669  * @iter: a #GMenuAttributeIter
670  * @out_name: (out) (allow-none) (transfer none): the type of the attribute
671  * @value: (out) (allow-none) (transfer full): the attribute value
672  *
673  * This function combines g_menu_attribute_iter_next() with
674  * g_menu_attribute_iter_get_name() and g_menu_attribute_iter_get_value().
675  *
676  * First the iterator is advanced to the next (possibly first) attribute.
677  * If that fails, then %FALSE is returned and there are no other
678  * effects.
679  *
680  * If successful, @name and @value are set to the name and value of the
681  * attribute that has just been advanced to.  At this point,
682  * g_menu_item_get_name() and g_menu_item_get_value() will return the
683  * same values again.
684  *
685  * The value returned in @name remains valid for as long as the iterator
686  * remains at the current position.  The value returned in @value must
687  * be unreffed using g_variant_unref() when it is no longer in use.
688  *
689  * Returns: %TRUE on success, or %FALSE if there is no additional
690  *          attribute
691  **/
692 gboolean
693 g_menu_attribute_iter_get_next (GMenuAttributeIter  *iter,
694                                 const gchar        **out_name,
695                                 GVariant           **value)
696 {
697   const gchar *name;
698
699   if (iter->priv->value)
700     {
701       g_variant_unref (iter->priv->value);
702       iter->priv->value = NULL;
703     }
704
705   iter->priv->valid = G_MENU_ATTRIBUTE_ITER_GET_CLASS (iter)
706     ->get_next (iter, &name, &iter->priv->value);
707
708   if (iter->priv->valid)
709     {
710       iter->priv->name = g_quark_from_string (name);
711       if (out_name)
712         *out_name = g_quark_to_string (iter->priv->name);
713
714       if (value)
715         *value = g_variant_ref (iter->priv->value);
716     }
717
718   return iter->priv->valid;
719 }
720
721 /**
722  * g_menu_attribute_iter_next:
723  * @iter: a #GMenuAttributeIter
724  *
725  * Attempts to advance the iterator to the next (possibly first)
726  * attribute.
727  *
728  * %TRUE is returned on success, or %FALSE if there are no more
729  * attributes.
730  *
731  * You must call this function when you first acquire the iterator
732  * to advance it to the first attribute (and determine if the first
733  * attribute exists at all).
734  *
735  * Returns: %TRUE on success, or %FALSE when there are no more attributes
736  **/
737 gboolean
738 g_menu_attribute_iter_next (GMenuAttributeIter *iter)
739 {
740   return g_menu_attribute_iter_get_next (iter, NULL, NULL);
741 }
742
743 /**
744  * g_menu_attribute_iter_get_name:
745  * @iter: a #GMenuAttributeIter
746  *
747  * Gets the name of the attribute at the current iterator position, as
748  * a string.
749  *
750  * The iterator is not advanced.
751  *
752  * Returns: the name of the attribute
753  **/
754 const gchar *
755 g_menu_attribute_iter_get_name (GMenuAttributeIter *iter)
756 {
757   g_return_val_if_fail (iter->priv->valid, 0);
758
759   return g_quark_to_string (iter->priv->name);
760 }
761
762 /**
763  * g_menu_attribute_iter_get_value:
764  * @iter: a #GMenuAttributeIter
765  *
766  * Gets the value of the attribute at the current iterator position.
767  *
768  * The iterator is not advanced.
769  *
770  * Returns: (transfer full): the value of the current attribute
771  **/
772 GVariant *
773 g_menu_attribute_iter_get_value (GMenuAttributeIter *iter)
774 {
775   g_return_val_if_fail (iter->priv->valid, NULL);
776
777   return g_variant_ref (iter->priv->value);
778 }
779
780 static void
781 g_menu_attribute_iter_finalize (GObject *object)
782 {
783   GMenuAttributeIter *iter = G_MENU_ATTRIBUTE_ITER (object);
784
785   if (iter->priv->value)
786     g_variant_unref (iter->priv->value);
787
788   G_OBJECT_CLASS (g_menu_attribute_iter_parent_class)
789     ->finalize (object);
790 }
791
792 static void
793 g_menu_attribute_iter_init (GMenuAttributeIter *iter)
794 {
795   iter->priv = G_TYPE_INSTANCE_GET_PRIVATE (iter, G_TYPE_MENU_ATTRIBUTE_ITER, GMenuAttributeIterPrivate);
796 }
797
798 static void
799 g_menu_attribute_iter_class_init (GMenuAttributeIterClass *class)
800 {
801   GObjectClass *object_class = G_OBJECT_CLASS (class);
802
803   object_class->finalize = g_menu_attribute_iter_finalize;
804
805   g_type_class_add_private (class, sizeof (GMenuAttributeIterPrivate));
806 }
807
808 G_DEFINE_ABSTRACT_TYPE (GMenuLinkIter, g_menu_link_iter, G_TYPE_OBJECT)
809
810 struct _GMenuLinkIterPrivate
811 {
812   GQuark name;
813   GMenuModel *value;
814   gboolean valid;
815 };
816
817 /**
818  * g_menu_link_iter_get_next:
819  * @iter: a #GMenuLinkIter
820  * @out_name: (out) (allow-none) (transfer none): the name of the link
821  * @value: (out) (allow-none) (transfer full): the linked #GMenuModel
822  *
823  * This function combines g_menu_link_iter_next() with
824  * g_menu_link_iter_get_name() and g_menu_link_iter_get_value().
825  *
826  * First the iterator is advanced to the next (possibly first) link.  If
827  * that fails, then %FALSE is returned and there are no other effects.
828  *
829  * If successful, @out_name and @value are set to the name and #GMenuModel
830  * of the link that has just been advanced to.  At this point,
831  * g_menu_item_get_name() and g_menu_item_get_value() will return the
832  * same values again.
833  *
834  * The value returned in @out_name remains valid for as long as the iterator
835  * remains at the current position.  The value returned in @value must
836  * be unreffed using g_object_unref() when it is no longer in use.
837  *
838  * Returns: %TRUE on success, or %FALSE if there is no additional link
839  **/
840 gboolean
841 g_menu_link_iter_get_next (GMenuLinkIter  *iter,
842                            const gchar   **out_name,
843                            GMenuModel    **value)
844 {
845   const gchar *name;
846
847   if (iter->priv->value)
848     {
849       g_object_unref (iter->priv->value);
850       iter->priv->value = NULL;
851     }
852
853   iter->priv->valid = G_MENU_LINK_ITER_GET_CLASS (iter)
854     ->get_next (iter, &name, &iter->priv->value);
855
856   if (iter->priv->valid)
857     {
858       g_assert (name != NULL);
859
860       iter->priv->name = g_quark_from_string (name);
861       if (out_name)
862         *out_name = g_quark_to_string (iter->priv->name);
863
864       if (value)
865         *value = g_object_ref (iter->priv->value);
866     }
867
868   return iter->priv->valid;
869 }
870
871 /**
872  * g_menu_link_iter_next:
873  * @iter: a #GMenuLinkIter
874  *
875  * Attempts to advance the iterator to the next (possibly first)
876  * link.
877  *
878  * %TRUE is returned on success, or %FALSE if there are no more links.
879  *
880  * You must call this function when you first acquire the iterator to
881  * advance it to the first link (and determine if the first link exists
882  * at all).
883  *
884  * Returns: %TRUE on success, or %FALSE when there are no more links
885  **/
886 gboolean
887 g_menu_link_iter_next (GMenuLinkIter *iter)
888 {
889   return g_menu_link_iter_get_next (iter, NULL, NULL);
890 }
891
892 /**
893  * g_menu_link_iter_get_name:
894  * @iter: a #GMenuLinkIter
895  *
896  * Gets the name of the link at the current iterator position.
897  *
898  * The iterator is not advanced.
899  *
900  * Returns: the type of the link
901  **/
902 const gchar *
903 g_menu_link_iter_get_name (GMenuLinkIter *iter)
904 {
905   g_return_val_if_fail (iter->priv->valid, 0);
906
907   return g_quark_to_string (iter->priv->name);
908 }
909
910 /**
911  * g_menu_link_iter_get_value:
912  * @iter: a #GMenuLinkIter
913  *
914  * Gets the linked #GMenuModel at the current iterator position.
915  *
916  * The iterator is not advanced.
917  *
918  * Returns: (transfer full): the #GMenuModel that is linked to
919  **/
920 GMenuModel *
921 g_menu_link_iter_get_value (GMenuLinkIter *iter)
922 {
923   g_return_val_if_fail (iter->priv->valid, NULL);
924
925   return g_object_ref (iter->priv->value);
926 }
927
928 static void
929 g_menu_link_iter_finalize (GObject *object)
930 {
931   GMenuLinkIter *iter = G_MENU_LINK_ITER (object);
932
933   if (iter->priv->value)
934     g_object_unref (iter->priv->value);
935
936   G_OBJECT_CLASS (g_menu_link_iter_parent_class)
937     ->finalize (object);
938 }
939
940 static void
941 g_menu_link_iter_init (GMenuLinkIter *iter)
942 {
943   iter->priv = G_TYPE_INSTANCE_GET_PRIVATE (iter, G_TYPE_MENU_LINK_ITER, GMenuLinkIterPrivate);
944 }
945
946 static void
947 g_menu_link_iter_class_init (GMenuLinkIterClass *class)
948 {
949   GObjectClass *object_class = G_OBJECT_CLASS (class);
950
951   object_class->finalize = g_menu_link_iter_finalize;
952
953   g_type_class_add_private (class, sizeof (GMenuLinkIterPrivate));
954 }