~ubuntu-branches/ubuntu/precise/libdbusmenu/precise

« back to all changes in this revision

Viewing changes to tools/dbusmenu-dumper.c

  • Committer: Bazaar Package Importer
  • Author(s): Ted Gould
  • Date: 2010-07-01 09:00:16 UTC
  • mto: (8.1.24 maverick)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20100701090016-4gz0ijjxpyn2gagm
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <libdbusmenu-glib/client.h>
26
26
#include <libdbusmenu-glib/menuitem.h>
27
27
 
 
28
#include <dbus/dbus-gtype-specialized.h>
 
29
 
28
30
static GMainLoop * mainloop = NULL;
29
31
 
 
32
static gchar * value2string (const GValue * value, int depth);
 
33
 
 
34
static gchar *
 
35
strv_dumper(const GValue * value)
 
36
{
 
37
        gchar ** strv = (gchar **)g_value_get_boxed(value);
 
38
 
 
39
        gchar * joined = g_strjoinv("\", \"", strv);
 
40
        gchar * retval = g_strdup_printf("[\"%s\"]", joined);
 
41
        g_free(joined);
 
42
        return retval;
 
43
}
 
44
 
 
45
typedef struct _collection_iterator_t collection_iterator_t;
 
46
struct _collection_iterator_t {
 
47
        gchar * space;
 
48
        GPtrArray * array;
 
49
        gboolean first;
 
50
        int depth;
 
51
};
 
52
 
 
53
static void
 
54
collection_iterate (const GValue * value, gpointer user_data)
 
55
{
 
56
        collection_iterator_t * iter = (collection_iterator_t *)user_data;
 
57
 
 
58
        gchar * str = value2string(value, iter->depth);
 
59
        gchar * retval = NULL;
 
60
 
 
61
        if (iter->first) {
 
62
                iter->first = FALSE;
 
63
                retval = g_strdup_printf("\n%s%s", iter->space, str);
 
64
        } else {
 
65
                retval = g_strdup_printf(",\n%s%s", iter->space, str);
 
66
        }
 
67
 
 
68
        g_ptr_array_add(iter->array, retval);
 
69
        g_free(str);
 
70
 
 
71
        return;
 
72
}
 
73
 
 
74
static gchar *
 
75
collection_dumper (const GValue * value, int depth)
 
76
{
 
77
        gchar * space = g_strnfill(depth, ' ');
 
78
        GPtrArray * array = g_ptr_array_new_with_free_func(g_free);
 
79
 
 
80
        g_ptr_array_add(array, g_strdup("["));
 
81
 
 
82
        collection_iterator_t iter;
 
83
        iter.space = space;
 
84
        iter.array = array;
 
85
        iter.first = TRUE;
 
86
        iter.depth = depth + 2;
 
87
 
 
88
        dbus_g_type_collection_value_iterate(value, collection_iterate, &iter);
 
89
 
 
90
        g_ptr_array_add(array, g_strdup_printf("\n%s]", space));
 
91
 
 
92
        g_free(space);
 
93
 
 
94
        gchar * retstr = NULL;
 
95
        if (array->len == 3) {
 
96
                retstr = g_strdup_printf("[%s]", ((gchar *)array->pdata[1]) + depth + 1/*for newline*/);
 
97
        } else {
 
98
                retstr = g_strjoinv(NULL, (gchar **)array->pdata);
 
99
        }
 
100
 
 
101
        g_ptr_array_free(array, TRUE);
 
102
 
 
103
        return retstr;
 
104
}
 
105
 
 
106
static gchar *
 
107
value2string (const GValue * value, int depth)
 
108
{
 
109
        gchar * str = NULL;
 
110
 
 
111
        if (value == NULL) {
 
112
                return g_strdup("(null)");
 
113
        }
 
114
 
 
115
        if (dbus_g_type_is_collection(G_VALUE_TYPE(value))) {
 
116
                str = collection_dumper(value, depth);
 
117
        } else if (G_VALUE_TYPE(value) == G_TYPE_STRV) {
 
118
                str = strv_dumper(value);
 
119
        } else if (G_VALUE_TYPE(value) == G_TYPE_BOOLEAN) {
 
120
                if (g_value_get_boolean(value)) {
 
121
                        str = g_strdup("true");
 
122
                } else {
 
123
                        str = g_strdup("false");
 
124
                }
 
125
        } else {
 
126
                str = g_strdup_value_contents(value);
 
127
        }
 
128
 
 
129
        return str;
 
130
}
 
131
 
30
132
static void
31
133
print_menuitem (DbusmenuMenuitem * item, int depth)
32
134
{
36
138
        GList * properties = dbusmenu_menuitem_properties_list(item);
37
139
        GList * property;
38
140
        for (property = properties; property != NULL; property = g_list_next(property)) {
39
 
                GValue value = {0};
40
 
                g_value_init(&value, G_TYPE_STRING);
41
 
                g_value_transform(dbusmenu_menuitem_property_get_value(item, (gchar *)property->data), &value);
42
 
                g_print(",\n%s\"%s\": \"%s\"", space, (gchar *)property->data, g_value_get_string(&value));
43
 
                g_value_unset(&value);
 
141
                const GValue * value = dbusmenu_menuitem_property_get_value(item, (gchar *)property->data);
 
142
                gchar * str = value2string(value, depth + g_utf8_strlen((gchar *)property->data, -1) + 2 /*quotes*/ + 2 /*: */);
 
143
                g_print(",\n%s\"%s\": %s", space, (gchar *)property->data, str);
 
144
                g_free(str);
44
145
        }
45
146
        g_list_free(properties);
46
147