[glib/wip/menus: 26/61] Improve GMenuMarkup debug output



commit 48fdf2db3639a03cf153b3cb090591614b751ce7
Author: Ryan Lortie <desrt desrt ca>
Date:   Sat Jul 16 20:19:04 2011 +0200

    Improve GMenuMarkup debug output

 gio/gmenumarkup.c |   81 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 53 insertions(+), 28 deletions(-)
---
diff --git a/gio/gmenumarkup.c b/gio/gmenumarkup.c
index c6591d9..453ab51 100644
--- a/gio/gmenumarkup.c
+++ b/gio/gmenumarkup.c
@@ -469,6 +469,7 @@ g_menu_markup_print_string (GString    *string,
                             gint        indent,
                             gint        tabstop)
 {
+  gboolean need_nl = FALSE;
   gint i, n;
 
   if G_UNLIKELY (string == NULL)
@@ -481,36 +482,42 @@ g_menu_markup_print_string (GString    *string,
       GMenuAttributeIter *attr_iter;
       GMenuLinkIter *link_iter;
       GMenuModelItem item;
-      gboolean need_nl;
-
-      if (i > 0)
-        g_string_append (string, "\n");
+      GString *contents;
+      GString *attrs;
 
       g_menu_model_get_item (model, i, &item);
 
-      indent_string (string, indent);
-      g_string_append (string, "<item>\n");
-
       attr_iter = g_menu_model_item_iterate_attributes (&item);
       link_iter = g_menu_model_item_iterate_links (&item);
-      need_nl = FALSE;
+      contents = g_string_new (NULL);
+      attrs = g_string_new (NULL);
 
       while (g_menu_attribute_iter_next (attr_iter))
         {
           const gchar *name = g_menu_attribute_iter_get_name (attr_iter);
           GVariant *value = g_menu_attribute_iter_get_value (attr_iter);
-          gchar *printed;
-          gchar *str;
 
-          printed = g_variant_print (value, TRUE);
-          str = g_markup_printf_escaped ("<attribute name='%s'>%s</attribute>\n", name, printed);
-          indent_string (string, indent + tabstop);
-          g_string_append (string, str);
-          g_variant_unref (value);
-          g_free (printed);
-          g_free (str);
+          if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
+            {
+              gchar *str;
+              str = g_markup_printf_escaped (" %s='%s'", name, g_variant_get_string (value, NULL));
+              g_string_append (attrs, str);
+              g_free (str);
+            }
 
-          need_nl = TRUE;
+          else
+            {
+              gchar *printed;
+              gchar *str;
+
+              printed = g_variant_print (value, TRUE);
+              str = g_markup_printf_escaped ("<attribute name='%s'>%s</attribute>\n", name, printed);
+              indent_string (contents, indent + tabstop);
+              g_string_append (contents, str);
+              g_variant_unref (value);
+              g_free (printed);
+              g_free (str);
+            }
         }
       g_object_unref (attr_iter);
 
@@ -520,25 +527,43 @@ g_menu_markup_print_string (GString    *string,
           GMenuModel *menu = g_menu_link_iter_get_value (link_iter);
           gchar *str;
 
-          if (need_nl)
-            g_string_append_c (string, '\n');
+          if (contents->str[0])
+            g_string_append_c (contents, '\n');
 
           str = g_markup_printf_escaped ("<link name='%s'>\n", name);
-          indent_string (string, indent + tabstop);
-          g_string_append (string, str);
+          indent_string (contents, indent + tabstop);
+          g_string_append (contents, str);
           g_free (str);
 
-          g_menu_markup_print_string (string, menu, indent + 2 * tabstop, tabstop);
+          g_menu_markup_print_string (contents, menu, indent + 2 * tabstop, tabstop);
 
-          indent_string (string, indent + tabstop);
-          g_string_append (string, "</link>\n");
+          indent_string (contents, indent + tabstop);
+          g_string_append (contents, "</link>\n");
+        }
+      g_object_unref (link_iter);
 
+      if (contents->str[0])
+        {
+          indent_string (string, indent);
+          g_string_append_printf (string, "<item%s>\n", attrs->str);
+          g_string_append (string, contents->str);
+          indent_string (string, indent);
+          g_string_append (string, "</item>\n");
           need_nl = TRUE;
         }
-      g_object_unref (link_iter);
 
-      indent_string (string, indent);
-      g_string_append (string, "</item>\n");
+      else
+        {
+          if (need_nl)
+            g_string_append_c (string, '\n');
+
+          indent_string (string, indent);
+          g_string_append_printf (string, "<item%s/>\n", attrs->str);
+          need_nl = FALSE;
+        }
+
+      g_string_free (contents, TRUE);
+      g_string_free (attrs, TRUE);
     }
 
   return string;



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