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