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