]> Dogcows Code - chaz/openbox/blob - obt/xml.c
animateIconify was accidentally duplicated in the schema
[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 };
52
53 static void destfunc(struct Callback *c)
54 {
55 g_free(c->tag);
56 g_slice_free(struct Callback, c);
57 }
58
59 ObtXmlInst* obt_xml_instance_new(void)
60 {
61 ObtXmlInst *i = g_slice_new(ObtXmlInst);
62 i->ref = 1;
63 i->xdg_paths = obt_paths_new();
64 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
65 (GDestroyNotify)destfunc);
66 i->doc = NULL;
67 i->root = NULL;
68 i->path = NULL;
69 return i;
70 }
71
72 void obt_xml_instance_ref(ObtXmlInst *i)
73 {
74 ++i->ref;
75 }
76
77 void obt_xml_instance_unref(ObtXmlInst *i)
78 {
79 if (i && --i->ref == 0) {
80 obt_paths_unref(i->xdg_paths);
81 g_hash_table_destroy(i->callbacks);
82 g_slice_free(ObtXmlInst, i);
83 }
84 }
85
86 xmlDocPtr obt_xml_doc(ObtXmlInst *i)
87 {
88 g_assert(i->doc); /* a doc is open? */
89 return i->doc;
90 }
91
92 xmlNodePtr obt_xml_root(ObtXmlInst *i)
93 {
94 g_assert(i->doc); /* a doc is open? */
95 return i->root;
96 }
97
98 void obt_xml_register(ObtXmlInst *i, const gchar *tag,
99 ObtXmlCallback func, gpointer data)
100 {
101 struct Callback *c;
102
103 if (g_hash_table_lookup(i->callbacks, tag)) {
104 g_error("Tag '%s' already registered", tag);
105 return;
106 }
107
108 c = g_slice_new(struct Callback);
109 c->tag = g_strdup(tag);
110 c->func = func;
111 c->data = data;
112 g_hash_table_insert(i->callbacks, c->tag, c);
113 }
114
115 static gboolean load_file(ObtXmlInst *i,
116 const gchar *domain,
117 const gchar *filename,
118 const gchar *root_node,
119 GSList *paths)
120 {
121 GSList *it;
122 gboolean r = FALSE;
123
124 g_assert(i->doc == NULL); /* another doc isn't open already? */
125
126 for (it = paths; !r && it; it = g_slist_next(it)) {
127 gchar *path;
128 struct stat s;
129
130 if (!domain && !filename) /* given a full path to the file */
131 path = g_strdup(it->data);
132 else
133 path = g_build_filename(it->data, domain, filename, NULL);
134
135 if (stat(path, &s) >= 0) {
136 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
137 with extra nodes in it. */
138 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
139 XML_PARSE_RECOVER));
140 xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS |
141 XML_PARSE_RECOVER));
142 if (i->doc) {
143 i->root = xmlDocGetRootElement(i->doc);
144 if (!i->root) {
145 xmlFreeDoc(i->doc);
146 i->doc = NULL;
147 g_message("%s is an empty XML document", path);
148 }
149 else if (xmlStrcmp(i->root->name,
150 (const xmlChar*)root_node)) {
151 xmlFreeDoc(i->doc);
152 i->doc = NULL;
153 i->root = NULL;
154 g_message("XML document %s is of wrong type. Root "
155 "node is not '%s'", path, root_node);
156 }
157 else {
158 i->path = g_strdup(path);
159 r = TRUE; /* ok! */
160 }
161 }
162 }
163
164 g_free(path);
165 }
166
167 return r;
168 }
169
170 gboolean obt_xml_load_file(ObtXmlInst *i,
171 const gchar *path,
172 const gchar *root_node)
173 {
174 GSList *paths;
175 gboolean r;
176
177 paths = g_slist_append(NULL, g_strdup(path));
178
179 r = load_file(i, NULL, NULL, root_node, paths);
180
181 while (paths) {
182 g_free(paths->data);
183 paths = g_slist_delete_link(paths, paths);
184 }
185 return r;
186 }
187
188 gboolean obt_xml_load_config_file(ObtXmlInst *i,
189 const gchar *domain,
190 const gchar *filename,
191 const gchar *root_node)
192 {
193 GSList *it, *paths = NULL;
194 gboolean r;
195
196 for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
197 paths = g_slist_append(paths, g_strdup(it->data));
198
199 r = load_file(i, domain, filename, root_node, paths);
200
201 while (paths) {
202 g_free(paths->data);
203 paths = g_slist_delete_link(paths, paths);
204 }
205 return r;
206 }
207
208 gboolean obt_xml_load_data_file(ObtXmlInst *i,
209 const gchar *domain,
210 const gchar *filename,
211 const gchar *root_node)
212 {
213 GSList *it, *paths = NULL;
214 gboolean r;
215
216 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
217 paths = g_slist_append(paths, g_strdup(it->data));
218
219 r = load_file(i, domain, filename, root_node, paths);
220
221 while (paths) {
222 g_free(paths->data);
223 paths = g_slist_delete_link(paths, paths);
224 }
225 return r;
226 }
227
228 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
229 const gchar *theme,
230 const gchar *domain,
231 const gchar *filename,
232 const gchar *root_node)
233 {
234 GSList *it, *paths = NULL;
235 gboolean r;
236
237 /* use ~/.themes for backwards compatibility */
238 paths = g_slist_append
239 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
240
241 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
242 paths = g_slist_append
243 (paths, g_build_filename(it->data, "themes", theme, NULL));
244
245 r = load_file(i, domain, filename, root_node, paths);
246
247 while (paths) {
248 g_free(paths->data);
249 paths = g_slist_delete_link(paths, paths);
250 }
251 return r;
252 }
253
254
255 gboolean obt_xml_load_mem(ObtXmlInst *i,
256 gpointer data, guint len, const gchar *root_node)
257 {
258 gboolean r = FALSE;
259
260 g_assert(i->doc == NULL); /* another doc isn't open already? */
261
262 i->doc = xmlParseMemory(data, len);
263 if (i) {
264 i->root = xmlDocGetRootElement(i->doc);
265 if (!i->root) {
266 xmlFreeDoc(i->doc);
267 i->doc = NULL;
268 g_message("Given memory is an empty document");
269 }
270 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
271 xmlFreeDoc(i->doc);
272 i->doc = NULL;
273 i->root = NULL;
274 g_message("XML Document in given memory is of wrong "
275 "type. Root node is not '%s'\n", root_node);
276 }
277 else
278 r = TRUE; /* ok ! */
279 }
280 return r;
281 }
282
283 gboolean obt_xml_save_file(ObtXmlInst *inst,
284 const gchar *path,
285 gboolean pretty)
286 {
287 return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
288 }
289
290 void obt_xml_close(ObtXmlInst *i)
291 {
292 if (i && i->doc) {
293 xmlFreeDoc(i->doc);
294 g_free(i->path);
295 i->doc = NULL;
296 i->root = NULL;
297 i->path = NULL;
298 }
299 }
300
301 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
302 {
303 g_assert(i->doc); /* a doc is open? */
304
305 while (node) {
306 if (node->name) {
307 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
308 if (c) c->func(node, c->data);
309 }
310 node = node->next;
311 }
312 }
313
314 void obt_xml_tree_from_root(ObtXmlInst *i)
315 {
316 obt_xml_tree(i, i->root->children);
317 }
318
319 gchar *obt_xml_node_string(xmlNodePtr node)
320 {
321 xmlChar *c = xmlNodeGetContent(node);
322 gchar *s;
323 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
324 s = g_strdup(c ? (gchar*)c : "");
325 xmlFree(c);
326 return s;
327 }
328
329 gint obt_xml_node_int(xmlNodePtr node)
330 {
331 xmlChar *c = xmlNodeGetContent(node);
332 gint i;
333 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
334 i = c ? atoi((gchar*)c) : 0;
335 xmlFree(c);
336 return i;
337 }
338
339 gboolean obt_xml_node_bool(xmlNodePtr node)
340 {
341 xmlChar *c = xmlNodeGetContent(node);
342 gboolean b = FALSE;
343 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
344 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
345 b = TRUE;
346 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
347 b = TRUE;
348 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
349 b = TRUE;
350 xmlFree(c);
351 return b;
352 }
353
354 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
355 {
356 xmlChar *c = xmlNodeGetContent(node);
357 gboolean r;
358 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
359 r = !xmlStrcasecmp(c, (const xmlChar*) val);
360 xmlFree(c);
361 return r;
362 }
363
364 xmlNodePtr obt_xml_find_node(xmlNodePtr node, const gchar *tag)
365 {
366 while (node) {
367 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
368 return node;
369 node = node->next;
370 }
371 return NULL;
372 }
373
374 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
375 gboolean *value)
376 {
377 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
378 gboolean r = FALSE;
379 if (c) {
380 g_strstrip((char*)c); /* strip leading/trailing whitespace */
381 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
382 *value = TRUE, r = TRUE;
383 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
384 *value = TRUE, r = TRUE;
385 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
386 *value = TRUE, r = TRUE;
387 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
388 *value = FALSE, r = TRUE;
389 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
390 *value = FALSE, r = TRUE;
391 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
392 *value = FALSE, r = TRUE;
393 }
394 xmlFree(c);
395 return r;
396 }
397
398 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
399 {
400 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
401 gboolean r = FALSE;
402 if (c) {
403 g_strstrip((char*)c); /* strip leading/trailing whitespace */
404 *value = atoi((gchar*)c);
405 r = TRUE;
406 }
407 xmlFree(c);
408 return r;
409 }
410
411 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
412 gchar **value)
413 {
414 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
415 gboolean r = FALSE;
416 if (c) {
417 g_strstrip((char*)c); /* strip leading/trailing whitespace */
418 *value = g_strdup((gchar*)c);
419 r = TRUE;
420 }
421 xmlFree(c);
422 return r;
423 }
424
425 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
426 const gchar *val)
427 {
428 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
429 gboolean r = FALSE;
430 if (c) {
431 g_strstrip((char*)c); /* strip leading/trailing whitespace */
432 r = !xmlStrcasecmp(c, (const xmlChar*) val);
433 }
434 xmlFree(c);
435 return r;
436 }
This page took 0.057003 seconds and 4 git commands to generate.