aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTed Gould <ted@gould.cx>2010-07-01 08:54:59 -0500
committerTed Gould <ted@gould.cx>2010-07-01 08:54:59 -0500
commita79f1b295ca5a6a0f20cc899db9043a46aff22e9 (patch)
treea39d4941b1009a201bdaf128d8f090be84f1c18b /tools
parent1a54d0070d99cca1322995bb74ab6027ee1dc434 (diff)
parent235abfa96562b5ba4d1876dc99e86551160ee34c (diff)
downloadlibdbusmenu-a79f1b295ca5a6a0f20cc899db9043a46aff22e9.tar.gz
libdbusmenu-a79f1b295ca5a6a0f20cc899db9043a46aff22e9.tar.bz2
libdbusmenu-a79f1b295ca5a6a0f20cc899db9043a46aff22e9.zip
Import upstream version 0.3.4
Diffstat (limited to 'tools')
-rw-r--r--tools/dbusmenu-dumper.c111
1 files changed, 106 insertions, 5 deletions
diff --git a/tools/dbusmenu-dumper.c b/tools/dbusmenu-dumper.c
index 55d631e..6ce9655 100644
--- a/tools/dbusmenu-dumper.c
+++ b/tools/dbusmenu-dumper.c
@@ -25,8 +25,110 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include <libdbusmenu-glib/client.h>
#include <libdbusmenu-glib/menuitem.h>
+#include <dbus/dbus-gtype-specialized.h>
+
static GMainLoop * mainloop = NULL;
+static gchar * value2string (const GValue * value, int depth);
+
+static gchar *
+strv_dumper(const GValue * value)
+{
+ gchar ** strv = (gchar **)g_value_get_boxed(value);
+
+ gchar * joined = g_strjoinv("\", \"", strv);
+ gchar * retval = g_strdup_printf("[\"%s\"]", joined);
+ g_free(joined);
+ return retval;
+}
+
+typedef struct _collection_iterator_t collection_iterator_t;
+struct _collection_iterator_t {
+ gchar * space;
+ GPtrArray * array;
+ gboolean first;
+ int depth;
+};
+
+static void
+collection_iterate (const GValue * value, gpointer user_data)
+{
+ collection_iterator_t * iter = (collection_iterator_t *)user_data;
+
+ gchar * str = value2string(value, iter->depth);
+ gchar * retval = NULL;
+
+ if (iter->first) {
+ iter->first = FALSE;
+ retval = g_strdup_printf("\n%s%s", iter->space, str);
+ } else {
+ retval = g_strdup_printf(",\n%s%s", iter->space, str);
+ }
+
+ g_ptr_array_add(iter->array, retval);
+ g_free(str);
+
+ return;
+}
+
+static gchar *
+collection_dumper (const GValue * value, int depth)
+{
+ gchar * space = g_strnfill(depth, ' ');
+ GPtrArray * array = g_ptr_array_new_with_free_func(g_free);
+
+ g_ptr_array_add(array, g_strdup("["));
+
+ collection_iterator_t iter;
+ iter.space = space;
+ iter.array = array;
+ iter.first = TRUE;
+ iter.depth = depth + 2;
+
+ dbus_g_type_collection_value_iterate(value, collection_iterate, &iter);
+
+ g_ptr_array_add(array, g_strdup_printf("\n%s]", space));
+
+ g_free(space);
+
+ gchar * retstr = NULL;
+ if (array->len == 3) {
+ retstr = g_strdup_printf("[%s]", ((gchar *)array->pdata[1]) + depth + 1/*for newline*/);
+ } else {
+ retstr = g_strjoinv(NULL, (gchar **)array->pdata);
+ }
+
+ g_ptr_array_free(array, TRUE);
+
+ return retstr;
+}
+
+static gchar *
+value2string (const GValue * value, int depth)
+{
+ gchar * str = NULL;
+
+ if (value == NULL) {
+ return g_strdup("(null)");
+ }
+
+ if (dbus_g_type_is_collection(G_VALUE_TYPE(value))) {
+ str = collection_dumper(value, depth);
+ } else if (G_VALUE_TYPE(value) == G_TYPE_STRV) {
+ str = strv_dumper(value);
+ } else if (G_VALUE_TYPE(value) == G_TYPE_BOOLEAN) {
+ if (g_value_get_boolean(value)) {
+ str = g_strdup("true");
+ } else {
+ str = g_strdup("false");
+ }
+ } else {
+ str = g_strdup_value_contents(value);
+ }
+
+ return str;
+}
+
static void
print_menuitem (DbusmenuMenuitem * item, int depth)
{
@@ -36,11 +138,10 @@ print_menuitem (DbusmenuMenuitem * item, int depth)
GList * properties = dbusmenu_menuitem_properties_list(item);
GList * property;
for (property = properties; property != NULL; property = g_list_next(property)) {
- GValue value = {0};
- g_value_init(&value, G_TYPE_STRING);
- g_value_transform(dbusmenu_menuitem_property_get_value(item, (gchar *)property->data), &value);
- g_print(",\n%s\"%s\": \"%s\"", space, (gchar *)property->data, g_value_get_string(&value));
- g_value_unset(&value);
+ const GValue * value = dbusmenu_menuitem_property_get_value(item, (gchar *)property->data);
+ gchar * str = value2string(value, depth + g_utf8_strlen((gchar *)property->data, -1) + 2 /*quotes*/ + 2 /*: */);
+ g_print(",\n%s\"%s\": %s", space, (gchar *)property->data, str);
+ g_free(str);
}
g_list_free(properties);