1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "client.h"
#include "dbusmenu-client.h"
/* Properties */
enum {
PROP_0,
PROP_DBUSOBJECT,
PROP_DBUSNAME
};
typedef struct _DbusmenuClientPrivate DbusmenuClientPrivate;
struct _DbusmenuClientPrivate
{
DbusmenuMenuitem * root;
gchar * dbus_object;
gchar * dbus_name;
DBusGConnection * session_bus;
DBusGProxy * menuproxy;
DBusGProxy * propproxy;
DBusGProxyCall * layoutcall;
};
#define DBUSMENU_CLIENT_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), DBUSMENU_TYPE_CLIENT, DbusmenuClientPrivate))
/* GObject Stuff */
static void dbusmenu_client_class_init (DbusmenuClientClass *klass);
static void dbusmenu_client_init (DbusmenuClient *self);
static void dbusmenu_client_dispose (GObject *object);
static void dbusmenu_client_finalize (GObject *object);
static void set_property (GObject * obj, guint id, const GValue * value, GParamSpec * pspec);
static void get_property (GObject * obj, guint id, GValue * value, GParamSpec * pspec);
/* Private Funcs */
static void build_proxies (DbusmenuClient * client);
/* Build a type */
G_DEFINE_TYPE (DbusmenuClient, dbusmenu_client, G_TYPE_OBJECT);
static void
dbusmenu_client_class_init (DbusmenuClientClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (DbusmenuClientPrivate));
object_class->dispose = dbusmenu_client_dispose;
object_class->finalize = dbusmenu_client_finalize;
object_class->set_property = set_property;
object_class->get_property = get_property;
g_object_class_install_property (object_class, PROP_DBUSOBJECT,
g_param_spec_string(DBUSMENU_CLIENT_PROP_DBUS_OBJECT, "DBus Object we represent",
"The Object on the client that we're getting our data from.",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_DBUSNAME,
g_param_spec_string(DBUSMENU_CLIENT_PROP_DBUS_NAME, "DBus Client we connect to",
"Name of the DBus client we're connecting to.",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
return;
}
static void
dbusmenu_client_init (DbusmenuClient *self)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(self);
priv->root = NULL;
priv->dbus_object = NULL;
priv->dbus_name = NULL;
priv->session_bus = NULL;
priv->menuproxy = NULL;
priv->propproxy = NULL;
priv->layoutcall = NULL;
return;
}
static void
dbusmenu_client_dispose (GObject *object)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(object);
if (priv->layoutcall != NULL) {
dbus_g_proxy_cancel_call(priv->propproxy, priv->layoutcall);
priv->layoutcall = NULL;
}
if (priv->menuproxy != NULL) {
g_object_unref(G_OBJECT(priv->menuproxy));
priv->menuproxy = NULL;
}
if (priv->propproxy != NULL) {
g_object_unref(G_OBJECT(priv->propproxy));
priv->propproxy = NULL;
}
priv->session_bus = NULL;
G_OBJECT_CLASS (dbusmenu_client_parent_class)->dispose (object);
return;
}
static void
dbusmenu_client_finalize (GObject *object)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(object);
g_free(priv->dbus_name);
g_free(priv->dbus_object);
G_OBJECT_CLASS (dbusmenu_client_parent_class)->finalize (object);
return;
}
static void
set_property (GObject * obj, guint id, const GValue * value, GParamSpec * pspec)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(obj);
switch (id) {
case PROP_DBUSNAME:
priv->dbus_name = g_value_dup_string(value);
if (priv->dbus_name != NULL && priv->dbus_object != NULL) {
build_proxies(DBUSMENU_CLIENT(obj));
}
break;
case PROP_DBUSOBJECT:
priv->dbus_object = g_value_dup_string(value);
if (priv->dbus_name != NULL && priv->dbus_object != NULL) {
build_proxies(DBUSMENU_CLIENT(obj));
}
break;
default:
g_warning("Unknown property %d.", id);
return;
}
return;
}
static void
get_property (GObject * obj, guint id, GValue * value, GParamSpec * pspec)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(obj);
switch (id) {
case PROP_DBUSNAME:
g_value_set_string(value, priv->dbus_name);
break;
case PROP_DBUSOBJECT:
g_value_set_string(value, priv->dbus_object);
break;
default:
g_warning("Unknown property %d.", id);
return;
}
return;
}
/* Internal funcs */
/* When we have a name and an object, build the two proxies and get the
first version of the layout */
static void
build_proxies (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
GError * error = NULL;
g_return_if_fail(priv->dbus_object != NULL);
g_return_if_fail(priv->dbus_name != NULL);
priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
if (error != NULL) {
g_error("Unable to get session bus: %s", error->message);
g_error_free(error);
return;
}
priv->propproxy = dbus_g_proxy_new_for_name_owner(priv->session_bus,
priv->dbus_name,
priv->dbus_object,
DBUS_INTERFACE_PROPERTIES,
&error);
if (error != NULL) {
g_error("Unable to get property proxy for %s on %s: %s", priv->dbus_name, priv->dbus_object, error->message);
g_error_free(error);
return;
}
priv->menuproxy = dbus_g_proxy_new_for_name_owner(priv->session_bus,
priv->dbus_name,
priv->dbus_object,
"org.freedesktop.dbusmenu",
&error);
if (error != NULL) {
g_error("Unable to get dbusmenu proxy for %s on %s: %s", priv->dbus_name, priv->dbus_object, error->message);
g_error_free(error);
return;
}
return;
}
/* Get the ID attribute of the node, parse it and
return it. Also we're checking to ensure the node
is a 'menu' here. */
static guint
parse_node_get_id (xmlNodePtr node)
{
if (g_strcmp0((gchar *)node->name, "menu") != 0) {
/* This kills some nodes early */
return 0;
}
xmlAttrPtr attrib;
for (attrib = node->properties; node != NULL; node = node->next) {
if (g_strcmp0((gchar *)node->name, "id") == 0) {
if (node->children != NULL) {
return (guint)g_ascii_strtoull((gchar *)attrib->children->content, NULL, 10);
}
break;
}
}
return 0;
}
/* Parse recursively through the XML and make it into
objects as need be */
static DbusmenuMenuitem *
parse_layout_xml(xmlNodePtr node, DbusmenuMenuitem * item, DbusmenuMenuitem * parent)
{
guint id = parse_node_get_id(node);
if (item == NULL || dbusmenu_menuitem_get_id(item) != id) {
if (item != NULL) {
if (parent != NULL) {
dbusmenu_menuitem_child_delete(parent, item);
}
g_object_unref(G_OBJECT(item));
}
/* Build a new item */
item = dbusmenu_menuitem_new_with_id(id);
}
xmlNodePtr children;
guint position;
for (children = node->children, position = 0; children != NULL; children = children->next, position++) {
guint childid = parse_node_get_id(children);
DbusmenuMenuitem * childmi = dbusmenu_menuitem_child_find(item, childid);
childmi = parse_layout_xml(children, childmi, item);
dbusmenu_menuitem_child_add_position(item, childmi, position);
}
return item;
}
/* Take the layout passed to us over DBus and turn it into
a set of beautiful objects */
static void
parse_layout (DbusmenuClient * client, const gchar * layout)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
xmlDocPtr xmldoc;
xmldoc = xmlReadMemory(layout, g_utf8_strlen(layout, 16*1024), "dbusmenu.xml", NULL, 0);
xmlNodePtr root = xmlDocGetRootElement(xmldoc);
priv->root = parse_layout_xml(root, priv->root, NULL);
if (priv->root == NULL) {
g_warning("Unable to parse layout on client %s object %s", priv->dbus_name, priv->dbus_object);
}
xmlFreeDoc(xmldoc);
return;
}
/* When the layout property returns, here's where we take care of that. */
static void
update_layout_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
{
DbusmenuClient * client = DBUSMENU_CLIENT(data);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
GError * error = NULL;
GValue value = {0};
g_value_init(&value, G_TYPE_STRING);
if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
g_warning("Getting layout failed on client %s object %s: %s", priv->dbus_name, priv->dbus_object, error->message);
priv->layoutcall = NULL;
g_error_free(error);
return;
}
const gchar * xml = g_value_get_string(&value);
parse_layout(client, xml);
priv->layoutcall = NULL;
return;
}
/* Call the property on the server we're connected to and set it up to
be async back to _update_layout_cb */
static void
update_layout (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (priv->layoutcall != NULL) {
return;
}
priv->layoutcall = dbus_g_proxy_begin_call (priv->propproxy,
"Get",
update_layout_cb,
client,
NULL,
G_TYPE_STRING, "org.freedesktop.dbusmenu",
G_TYPE_STRING, "layout",
G_TYPE_INVALID, G_TYPE_VALUE, G_TYPE_INVALID);
return;
}
/* Public API */
DbusmenuClient *
dbusmenu_client_new (const gchar * name, const gchar * object)
{
DbusmenuClient * self = g_object_new(DBUSMENU_TYPE_CLIENT,
DBUSMENU_CLIENT_PROP_DBUS_NAME, name,
DBUSMENU_CLIENT_PROP_DBUS_OBJECT, object,
NULL);
update_layout(self);
return self;
}
DbusmenuMenuitem *
dbusmenu_client_get_root (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (priv->layoutcall != NULL) {
/* Will end the current call and block on it's completion */
update_layout_cb(priv->propproxy, priv->layoutcall, client);
}
return priv->root;
}
|