]> Dogcows Code - chaz/openbox/blob - obt/xml.c
Remove unneeded obrender/button.h
[chaz/openbox] / obt / xml.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/xml.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/xml.h"
20 #include "obt/paths.h"
21
22 #include <libxml/xinclude.h>
23 #include <glib.h>
24
25 #ifdef HAVE_STDLIB_H
26 # include <stdlib.h>
27 #endif
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 struct Callback {
39 gchar *tag;
40 ObtXmlCallback func;
41 gpointer data;
42 };
43
44 struct _ObtXmlInst {
45 gint ref;
46 ObtPaths *xdg_paths;
47 GHashTable *callbacks;
48 xmlDocPtr doc;
49 xmlNodePtr root;
50 gchar *path;
51 gchar *last_error_file;
52 gint last_error_line;
53 gchar *last_error_message;
54 };
55
56 static void obt_xml_save_last_error(ObtXmlInst* inst);
57
58 static void destfunc(struct Callback *c)
59 {
60 g_free(c->tag);
61 g_slice_free(struct Callback, c);
62 }
63
64 ObtXmlInst* obt_xml_instance_new(void)
65 {
66 ObtXmlInst *i = g_slice_new(ObtXmlInst);
67 i->ref = 1;
68 i->xdg_paths = obt_paths_new();
69 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify)destfunc);
71 i->doc = NULL;
72 i->root = NULL;
73 i->path = NULL;
74 i->last_error_file = NULL;
75 i->last_error_line = -1;
76 i->last_error_message = NULL;
77 return i;
78 }
79
80 void obt_xml_instance_ref(ObtXmlInst *i)
81 {
82 ++i->ref;
83 }
84
85 void obt_xml_instance_unref(ObtXmlInst *i)
86 {
87 if (i && --i->ref == 0) {
88 obt_paths_unref(i->xdg_paths);
89 g_hash_table_destroy(i->callbacks);
90 g_free(i->last_error_file);
91 g_free(i->last_error_message);
92 g_slice_free(ObtXmlInst, i);
93 }
94 }
95
96 xmlDocPtr obt_xml_doc(ObtXmlInst *i)
97 {
98 g_assert(i->doc); /* a doc is open? */
99 return i->doc;
100 }
101
102 xmlNodePtr obt_xml_root(ObtXmlInst *i)
103 {
104 g_assert(i->doc); /* a doc is open? */
105 return i->root;
106 }
107
108 void obt_xml_register(ObtXmlInst *i, const gchar *tag,
109 ObtXmlCallback func, gpointer data)
110 {
111 struct Callback *c;
112
113 if (g_hash_table_lookup(i->callbacks, tag)) {
114 g_error("Tag '%s' already registered", tag);
115 return;
116 }
117
118 c = g_slice_new(struct Callback);
119 c->tag = g_strdup(tag);
120 c->func = func;
121 c->data = data;
122 g_hash_table_insert(i->callbacks, c->tag, c);
123 }
124
125 void obt_xml_unregister(ObtXmlInst *i, const gchar *tag)
126 {
127 g_hash_table_remove(i->callbacks, tag);
128 }
129
130 static gboolean load_file(ObtXmlInst *i,
131 const gchar *domain,
132 const gchar *filename,
133 const gchar *root_node,
134 GSList *paths)
135 {
136 GSList *it;
137 gboolean r = FALSE;
138
139 g_assert(i->doc == NULL); /* another doc isn't open already? */
140
141 xmlResetLastError();
142
143 for (it = paths; !r && it; it = g_slist_next(it)) {
144 gchar *path;
145 struct stat s;
146
147 if (!domain && !filename) /* given a full path to the file */
148 path = g_strdup(it->data);
149 else
150 path = g_build_filename(it->data, domain, filename, NULL);
151
152 if (stat(path, &s) >= 0) {
153 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
154 with extra nodes in it. */
155 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
156 XML_PARSE_RECOVER));
157 xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS |
158 XML_PARSE_RECOVER));
159 if (i->doc) {
160 i->root = xmlDocGetRootElement(i->doc);
161 if (!i->root) {
162 xmlFreeDoc(i->doc);
163 i->doc = NULL;
164 g_message("%s is an empty XML document", path);
165 }
166 else if (xmlStrcmp(i->root->name,
167 (const xmlChar*)root_node)) {
168 xmlFreeDoc(i->doc);
169 i->doc = NULL;
170 i->root = NULL;
171 g_message("XML document %s is of wrong type. Root "
172 "node is not '%s'", path, root_node);
173 }
174 else {
175 i->path = g_strdup(path);
176 r = TRUE; /* ok! */
177 }
178 }
179 }
180
181 g_free(path);
182 }
183
184 obt_xml_save_last_error(i);
185
186 return r;
187 }
188
189 gboolean obt_xml_load_file(ObtXmlInst *i,
190 const gchar *path,
191 const gchar *root_node)
192 {
193 GSList *paths;
194 gboolean r;
195
196 paths = g_slist_append(NULL, g_strdup(path));
197
198 r = load_file(i, NULL, NULL, root_node, paths);
199
200 while (paths) {
201 g_free(paths->data);
202 paths = g_slist_delete_link(paths, paths);
203 }
204 return r;
205 }
206
207 gboolean obt_xml_load_config_file(ObtXmlInst *i,
208 const gchar *domain,
209 const gchar *filename,
210 const gchar *root_node)
211 {
212 GSList *it, *paths = NULL;
213 gboolean r;
214
215 for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
216 paths = g_slist_append(paths, g_strdup(it->data));
217
218 r = load_file(i, domain, filename, root_node, paths);
219
220 while (paths) {
221 g_free(paths->data);
222 paths = g_slist_delete_link(paths, paths);
223 }
224 return r;
225 }
226
227 gboolean obt_xml_load_data_file(ObtXmlInst *i,
228 const gchar *domain,
229 const gchar *filename,
230 const gchar *root_node)
231 {
232 GSList *it, *paths = NULL;
233 gboolean r;
234
235 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
236 paths = g_slist_append(paths, g_strdup(it->data));
237
238 r = load_file(i, domain, filename, root_node, paths);
239
240 while (paths) {
241 g_free(paths->data);
242 paths = g_slist_delete_link(paths, paths);
243 }
244 return r;
245 }
246
247 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
248 const gchar *theme,
249 const gchar *domain,
250 const gchar *filename,
251 const gchar *root_node)
252 {
253 GSList *it, *paths = NULL;
254 gboolean r;
255
256 /* use ~/.themes for backwards compatibility */
257 paths = g_slist_append
258 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
259
260 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
261 paths = g_slist_append
262 (paths, g_build_filename(it->data, "themes", theme, NULL));
263
264 r = load_file(i, domain, filename, root_node, paths);
265
266 while (paths) {
267 g_free(paths->data);
268 paths = g_slist_delete_link(paths, paths);
269 }
270 return r;
271 }
272
273
274 gboolean obt_xml_load_mem(ObtXmlInst *i,
275 gpointer data, guint len, const gchar *root_node)
276 {
277 gboolean r = FALSE;
278
279 g_assert(i->doc == NULL); /* another doc isn't open already? */
280
281 xmlResetLastError();
282
283 i->doc = xmlParseMemory(data, len);
284 if (i) {
285 i->root = xmlDocGetRootElement(i->doc);
286 if (!i->root) {
287 xmlFreeDoc(i->doc);
288 i->doc = NULL;
289 g_message("Given memory is an empty document");
290 }
291 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
292 xmlFreeDoc(i->doc);
293 i->doc = NULL;
294 i->root = NULL;
295 g_message("XML Document in given memory is of wrong "
296 "type. Root node is not '%s'\n", root_node);
297 }
298 else
299 r = TRUE; /* ok ! */
300 }
301
302 obt_xml_save_last_error(i);
303
304 return r;
305 }
306
307 static void obt_xml_save_last_error(ObtXmlInst* inst)
308 {
309 xmlErrorPtr error = xmlGetLastError();
310 if (error) {
311 inst->last_error_file = g_strdup(error->file);
312 inst->last_error_line = error->line;
313 inst->last_error_message = g_strdup(error->message);
314 xmlResetError(error);
315 }
316 }
317
318 gboolean obt_xml_last_error(ObtXmlInst *inst)
319 {
320 return inst->last_error_file &&
321 inst->last_error_line >= 0 &&
322 inst->last_error_message;
323 }
324
325 gchar* obt_xml_last_error_file(ObtXmlInst *inst)
326 {
327 if (!obt_xml_last_error(inst))
328 return NULL;
329 return inst->last_error_file;
330 }
331
332 gint obt_xml_last_error_line(ObtXmlInst *inst)
333 {
334 if (!obt_xml_last_error(inst))
335 return -1;
336 return inst->last_error_line;
337 }
338
339 gchar* obt_xml_last_error_message(ObtXmlInst *inst)
340 {
341 if (!obt_xml_last_error(inst))
342 return NULL;
343 return inst->last_error_message;
344 }
345
346 gboolean obt_xml_save_file(ObtXmlInst *inst,
347 const gchar *path,
348 gboolean pretty)
349 {
350 return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
351 }
352
353 void obt_xml_close(ObtXmlInst *i)
354 {
355 if (i && i->doc) {
356 xmlFreeDoc(i->doc);
357 g_free(i->path);
358 i->doc = NULL;
359 i->root = NULL;
360 i->path = NULL;
361 }
362 }
363
364 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
365 {
366 g_assert(i->doc); /* a doc is open? */
367
368 while (node) {
369 if (node->name) {
370 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
371 if (c) c->func(node, c->data);
372 }
373 node = node->next;
374 }
375 }
376
377 void obt_xml_tree_from_root(ObtXmlInst *i)
378 {
379 obt_xml_tree(i, i->root->children);
380 }
381
382 gchar *obt_xml_node_string_unstripped(xmlNodePtr node)
383 {
384 xmlChar *c = xmlNodeGetContent(node);
385 gchar *s;
386 s = g_strdup(c ? (gchar*)c : "");
387 xmlFree(c);
388 return s;
389 }
390
391 gchar *obt_xml_node_string(xmlNodePtr node)
392 {
393 gchar* result = obt_xml_node_string_unstripped(node);
394 g_strstrip(result); /* strip leading/trailing whitespace */
395 return result;
396 }
397
398 gint obt_xml_node_int(xmlNodePtr node)
399 {
400 xmlChar *c = xmlNodeGetContent(node);
401 gint i;
402 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
403 i = c ? atoi((gchar*)c) : 0;
404 xmlFree(c);
405 return i;
406 }
407
408 gboolean obt_xml_node_bool(xmlNodePtr node)
409 {
410 xmlChar *c = xmlNodeGetContent(node);
411 gboolean b = FALSE;
412 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
413 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
414 b = TRUE;
415 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
416 b = TRUE;
417 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
418 b = TRUE;
419 xmlFree(c);
420 return b;
421 }
422
423 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
424 {
425 xmlChar *c = xmlNodeGetContent(node);
426 gboolean r;
427 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
428 r = !xmlStrcasecmp(c, (const xmlChar*) val);
429 xmlFree(c);
430 return r;
431 }
432
433 xmlNodePtr obt_xml_find_node(xmlNodePtr node, const gchar *tag)
434 {
435 while (node) {
436 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
437 return node;
438 node = node->next;
439 }
440 return NULL;
441 }
442
443 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
444 gboolean *value)
445 {
446 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
447 gboolean r = FALSE;
448 if (c) {
449 g_strstrip((char*)c); /* strip leading/trailing whitespace */
450 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
451 *value = TRUE, r = TRUE;
452 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
453 *value = TRUE, r = TRUE;
454 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
455 *value = TRUE, r = TRUE;
456 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
457 *value = FALSE, r = TRUE;
458 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
459 *value = FALSE, r = TRUE;
460 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
461 *value = FALSE, r = TRUE;
462 }
463 xmlFree(c);
464 return r;
465 }
466
467 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
468 {
469 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
470 gboolean r = FALSE;
471 if (c) {
472 g_strstrip((char*)c); /* strip leading/trailing whitespace */
473 *value = atoi((gchar*)c);
474 r = TRUE;
475 }
476 xmlFree(c);
477 return r;
478 }
479
480 gboolean obt_xml_attr_string_unstripped(xmlNodePtr node, const gchar *name,
481 gchar **value)
482 {
483 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
484 gboolean r = FALSE;
485 if (c) {
486 *value = g_strdup((gchar*)c);
487 r = TRUE;
488 }
489 xmlFree(c);
490 return r;
491 }
492
493 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
494 gchar **value)
495 {
496 gboolean result = obt_xml_attr_string_unstripped(node, name, value);
497 if (result)
498 g_strstrip(*value); /* strip leading/trailing whitespace */
499 return result;
500 }
501
502 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
503 const gchar *val)
504 {
505 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
506 gboolean r = FALSE;
507 if (c) {
508 g_strstrip((char*)c); /* strip leading/trailing whitespace */
509 r = !xmlStrcasecmp(c, (const xmlChar*) val);
510 }
511 xmlFree(c);
512 return r;
513 }
This page took 0.05834 seconds and 4 git commands to generate.