[gnome-builder] libide/tweaks: add printf helper for items



commit 5a018b36ffc5db4ba2d4873ae3e8fecccc863dad
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jul 31 04:16:43 2022 -0700

    libide/tweaks: add printf helper for items

 src/libide/tweaks/ide-tweaks-item-private.h | 31 ++++++++++++++++
 src/libide/tweaks/ide-tweaks-item.c         | 56 +++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
---
diff --git a/src/libide/tweaks/ide-tweaks-item-private.h b/src/libide/tweaks/ide-tweaks-item-private.h
new file mode 100644
index 000000000..4b34f67eb
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-item-private.h
@@ -0,0 +1,31 @@
+/* ide-tweaks-item-private.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "ide-tweaks-item-private.h"
+
+G_BEGIN_DECLS
+
+void _ide_tweaks_item_printf (IdeTweaksItem *self,
+                              GString       *string,
+                              guint          level);
+
+G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-item.c b/src/libide/tweaks/ide-tweaks-item.c
index 7e5a7856a..a80b702c7 100644
--- a/src/libide/tweaks/ide-tweaks-item.c
+++ b/src/libide/tweaks/ide-tweaks-item.c
@@ -25,6 +25,7 @@
 #include <gtk/gtk.h>
 
 #include "ide-tweaks-item.h"
+#include "ide-tweaks-item-private.h"
 
 typedef struct
 {
@@ -470,4 +471,59 @@ static void
 buildable_iface_init (GtkBuildableIface *iface)
 {
   iface->add_child = ide_tweaks_item_add_child;
+void
+_ide_tweaks_item_printf (IdeTweaksItem *self,
+                         GString       *string,
+                         guint          level)
+{
+  g_autofree GParamSpec **pspecs = NULL;
+  guint n_pspecs;
+
+  g_return_if_fail (IDE_IS_TWEAKS_ITEM (self));
+  g_return_if_fail (string != NULL);
+
+  for (guint i = 0; i < level; i++)
+    g_string_append (string, "  ");
+  g_string_append_printf (string, "<%s id=\"%s\"",
+                          G_OBJECT_TYPE_NAME (self),
+                          gtk_buildable_get_buildable_id (GTK_BUILDABLE (self)));
+
+  pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (self), &n_pspecs);
+
+  for (guint i = 0; i < n_pspecs; i++)
+    {
+      GParamSpec *pspec = pspecs[i];
+
+      if (pspec->flags & G_PARAM_READABLE &&
+          g_value_type_transformable (pspec->value_type, G_TYPE_STRING))
+        {
+          g_auto(GValue) value = G_VALUE_INIT;
+          g_autofree char *copy = NULL;
+
+          g_value_init (&value, G_TYPE_STRING);
+          g_object_get_property (G_OBJECT (self), pspec->name, &value);
+
+          if (g_value_get_string (&value))
+            copy = g_strescape (g_value_get_string (&value), NULL);
+
+          g_string_append_printf (string, " %s=\"%s\"", pspec->name, copy ? copy : "");
+        }
+    }
+
+  if (ide_tweaks_item_get_first_child (self) == NULL)
+    {
+      g_string_append (string, "/>\n");
+      return;
+    }
+
+  g_string_append (string, ">\n");
+
+  for (IdeTweaksItem *child = ide_tweaks_item_get_first_child (self);
+       child != NULL;
+       child = ide_tweaks_item_get_next_sibling (child))
+    _ide_tweaks_item_printf (child, string, level+1);
+
+  for (guint i = 0; i < level; i++)
+    g_string_append (string, "  ");
+  g_string_append_printf (string, "</%s>\n", G_OBJECT_TYPE_NAME (self));
 }


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