[glib/wip/menuitem-api: 3/4] GMenuItem: add getter APIs



commit e725e76aafe2874cb9400cbd1431b9a1ce93c623
Author: Ryan Lortie <desrt desrt ca>
Date:   Sat Aug 18 14:20:59 2012 -0400

    GMenuItem: add getter APIs
    
    GMenuItem has been write-only up to this point.  Add some APIs for
    reading back values as well.

 docs/reference/gio/gio-sections.txt |    3 +
 gio/gio.symbols                     |    3 +
 gio/gmenu.c                         |  118 +++++++++++++++++++++++++++++++++++
 gio/gmenu.h                         |   13 ++++
 4 files changed, 137 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 36a0c30..afae789 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -3754,6 +3754,9 @@ g_menu_item_set_section
 g_menu_item_set_submenu
 
 <SUBSECTION>
+g_menu_item_get_attribute_value
+g_menu_item_get_attribute
+g_menu_item_get_link
 g_menu_item_set_attribute_value
 g_menu_item_set_attribute
 g_menu_item_set_link
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 26558ea..f9d4d42 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1681,6 +1681,9 @@ g_menu_insert
 g_menu_insert_item
 g_menu_insert_section
 g_menu_insert_submenu
+g_menu_item_get_attribute
+g_menu_item_get_attribute_value
+g_menu_item_get_link
 g_menu_item_get_type
 g_menu_item_new
 g_menu_item_new_section
diff --git a/gio/gmenu.c b/gio/gmenu.c
index c88fd7c..841ea71 100644
--- a/gio/gmenu.c
+++ b/gio/gmenu.c
@@ -745,6 +745,124 @@ g_menu_item_set_link (GMenuItem   *menu_item,
 }
 
 /**
+ * g_menu_item_get_attribute_value:
+ * @menu_item: a #GMenuItem
+ * @attribute: the attribute name to query
+ * @expected_type: (allow none): the expected type of the attribute
+ *
+ * Queries the named @attribute on @menu_item.
+ *
+ * If @expected_type is specified and the attribute does not have this
+ * type, %NULL is returned.  %NULL is also returned if the attribute
+ * simply does not exist.
+ *
+ * Returns: (transfer full): the attribute value, or %NULL
+ *
+ * Since: 2.34
+ */
+GVariant *
+g_menu_item_get_attribute_value (GMenuItem          *menu_item,
+                                 const gchar        *attribute,
+                                 const GVariantType *expected_type)
+{
+  GVariant *value;
+
+  g_return_val_if_fail (G_IS_MENU_ITEM (menu_item), NULL);
+  g_return_val_if_fail (attribute != NULL, NULL);
+
+  value = g_hash_table_lookup (menu_item->attributes, attribute);
+
+  if (value != NULL)
+    {
+      if (expected_type == NULL || g_variant_is_of_type (value, expected_type))
+        g_variant_ref (value);
+      else
+        value = NULL;
+    }
+
+  return value;
+}
+
+/**
+ * g_menu_item_get_attribute:
+ * @menu_item: a #GMenuItem
+ * @attribute: the attribute name to query
+ * @format_string: a #GVariant format string
+ * @...: positional parameters, as per @format_string
+ *
+ * Queries the named @attribute on @menu_item.
+ *
+ * If the attribute exists and matches the #GVariantType corresponding
+ * to @format_string then @format_string is used to deconstruct the
+ * value into the positional parameters and %TRUE is returned.
+ *
+ * If the attribute does not exist, or it does exist but has the wrong
+ * type, then the positional parameters are ignored and %FALSE is
+ * returned.
+ *
+ * Returns: %TRUE if the named attribute was found with the expected
+ *     type
+ *
+ * Since: 2.34
+ */
+gboolean
+g_menu_item_get_attribute (GMenuItem   *menu_item,
+                           const gchar *attribute,
+                           const gchar *format_string,
+                           ...)
+{
+  GVariant *value;
+  va_list ap;
+
+  g_return_val_if_fail (G_IS_MENU_ITEM (menu_item), FALSE);
+  g_return_val_if_fail (attribute != NULL, FALSE);
+  g_return_val_if_fail (format_string != NULL, FALSE);
+
+  value = g_hash_table_lookup (menu_item->attributes, attribute);
+
+  if (value == NULL)
+    return FALSE;
+
+  if (!g_variant_check_format_string (value, format_string, FALSE))
+    return FALSE;
+
+  va_start (ap, format_string);
+  g_variant_get_va (value, format_string, NULL, &ap);
+  va_end (ap);
+
+  return TRUE;
+}
+
+/**
+ * g_menu_item_get_link:
+ * @menu_item: a #GMenuItem
+ * @link: the link name to query
+ *
+ * Queries the named @link on @menu_item.
+ *
+ * Returns: (transfer full): the link, or %NULL
+ *
+ * Since: 2.34
+ */
+GMenuModel *
+g_menu_item_get_link (GMenuItem   *menu_item,
+                      const gchar *link)
+{
+  GMenuModel *model;
+
+  g_return_val_if_fail (G_IS_MENU_ITEM (menu_item), NULL);
+  g_return_val_if_fail (link != NULL, NULL);
+  g_return_val_if_fail (valid_attribute_name (link), NULL);
+
+  model = g_hash_table_lookup (menu_item->links, link);
+
+  if (model)
+    g_object_ref (model);
+
+  return model;
+}
+
+/**
  * g_menu_item_set_label:
  * @menu_item: a #GMenuItem
  * @label: (allow-none): the label to set, or %NULL to unset
diff --git a/gio/gmenu.h b/gio/gmenu.h
index b7eafd1..472118f 100644
--- a/gio/gmenu.h
+++ b/gio/gmenu.h
@@ -102,6 +102,19 @@ GMenuItem * g_menu_item_new_submenu                 (const gchar *label,
 GMenuItem * g_menu_item_new_section                 (const gchar *label,
                                                      GMenuModel  *section);
 
+GLIB_AVAILABLE_IN_2_34
+GVariant *  g_menu_item_get_attribute_value         (GMenuItem   *menu_item,
+                                                     const gchar *attribute,
+                                                     const GVariantType *expected_value);
+GLIB_AVAILABLE_IN_2_34
+gboolean    g_menu_item_get_attribute               (GMenuItem   *menu_item,
+                                                     const gchar *attribute,
+                                                     const gchar *format_string,
+                                                     ...);
+GLIB_AVAILABLE_IN_2_34
+GMenuModel *g_menu_item_get_link                    (GMenuItem   *menu_item,
+                                                     const gchar *link);
+
 void        g_menu_item_set_attribute_value         (GMenuItem   *menu_item,
                                                      const gchar *attribute,
                                                      GVariant    *value);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]