]> icculus.org git repositories - dana/openbox.git/blob - obt/xml.c
Use ObConfigValue to parse gravity coords, remove parse special functions from config...
[dana/openbox.git] / 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 void obt_xml_unregister(ObtXmlInst *i, const gchar *tag)
116 {
117     g_hash_table_remove(i->callbacks, tag);
118 }
119
120 void obt_xml_new_file(ObtXmlInst *i,
121                       const gchar *root_node)
122 {
123     xmlNodePtr old;
124
125     g_assert(i->doc == NULL); /* another doc isn't open already? */
126
127     i->doc = xmlNewDoc((xmlChar*)"1.0");
128     i->root = xmlNewDocRawNode(i->doc, NULL, (xmlChar*)root_node, NULL);
129     old = xmlDocSetRootElement(i->doc, i->root);
130     if (old) xmlFreeNode(old);
131 }
132
133 static gboolean load_file(ObtXmlInst *i,
134                           const gchar *domain,
135                           const gchar *filename,
136                           const gchar *root_node,
137                           GSList *paths)
138 {
139     GSList *it;
140     gboolean r = FALSE;
141
142     g_assert(i->doc == NULL); /* another doc isn't open already? */
143
144     for (it = paths; !r && it; it = g_slist_next(it)) {
145         gchar *path;
146         struct stat s;
147
148         if (!domain && !filename) /* given a full path to the file */
149             path = g_strdup(it->data);
150         else
151             path = g_build_filename(it->data, domain, filename, NULL);
152
153         if (stat(path, &s) >= 0) {
154             /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
155                with extra nodes in it. */
156             i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
157                                               XML_PARSE_RECOVER));
158             xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS |
159                                              XML_PARSE_RECOVER));
160             if (i->doc) {
161                 i->root = xmlDocGetRootElement(i->doc);
162                 if (!i->root) {
163                     xmlFreeDoc(i->doc);
164                     i->doc = NULL;
165                     g_message("%s is an empty XML document", path);
166                 }
167                 else if (xmlStrcmp(i->root->name,
168                                    (const xmlChar*)root_node)) {
169                     xmlFreeDoc(i->doc);
170                     i->doc = NULL;
171                     i->root = NULL;
172                     g_message("XML document %s is of wrong type. Root "
173                               "node is not '%s'", path, root_node);
174                 }
175                 else {
176                     i->path = g_strdup(path);
177                     r = TRUE; /* ok! */
178                 }
179             }
180         }
181
182         g_free(path);
183     }
184
185     return r;
186 }
187
188 gboolean obt_xml_load_file(ObtXmlInst *i,
189                            const gchar *path,
190                            const gchar *root_node)
191 {
192     GSList *paths;
193     gboolean r;
194
195     paths = g_slist_append(NULL, g_strdup(path));
196
197     r = load_file(i, NULL, NULL, root_node, paths);
198
199     while (paths) {
200         g_free(paths->data);
201         paths = g_slist_delete_link(paths, paths);
202     }
203     return r;
204 }
205
206 gboolean obt_xml_load_config_file(ObtXmlInst *i,
207                                   const gchar *domain,
208                                   const gchar *filename,
209                                   const gchar *root_node)
210 {
211     GSList *it, *paths = NULL;
212     gboolean r;
213
214     for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
215         paths = g_slist_append(paths, g_strdup(it->data));
216
217     r = load_file(i, domain, filename, root_node, paths);
218
219     while (paths) {
220         g_free(paths->data);
221         paths = g_slist_delete_link(paths, paths);
222     }
223     return r;
224 }
225
226 gboolean obt_xml_load_data_file(ObtXmlInst *i,
227                                 const gchar *domain,
228                                 const gchar *filename,
229                                 const gchar *root_node)
230 {
231     GSList *it, *paths = NULL;
232     gboolean r;
233
234     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
235         paths = g_slist_append(paths, g_strdup(it->data));
236
237     r = load_file(i, domain, filename, root_node, paths);
238
239     while (paths) {
240         g_free(paths->data);
241         paths = g_slist_delete_link(paths, paths);
242     }
243     return r;
244 }
245
246 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
247                                  const gchar *theme,
248                                  const gchar *domain,
249                                  const gchar *filename,
250                                  const gchar *root_node)
251 {
252     GSList *it, *paths = NULL;
253     gboolean r;
254
255     /* use ~/.themes for backwards compatibility */
256     paths = g_slist_append
257         (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
258
259     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
260         paths = g_slist_append
261             (paths, g_build_filename(it->data, "themes", theme, NULL));
262
263     r = load_file(i, domain, filename, root_node, paths);
264
265     while (paths) {
266         g_free(paths->data);
267         paths = g_slist_delete_link(paths, paths);
268     }
269     return r;
270 }
271
272
273 gboolean obt_xml_load_mem(ObtXmlInst *i,
274                           gpointer data, guint len, const gchar *root_node)
275 {
276     gboolean r = FALSE;
277
278     g_assert(i->doc == NULL); /* another doc isn't open already? */
279
280     i->doc = xmlParseMemory(data, len);
281     if (i) {
282         i->root = xmlDocGetRootElement(i->doc);
283         if (!i->root) {
284             xmlFreeDoc(i->doc);
285             i->doc = NULL;
286             g_message("Given memory is an empty document");
287         }
288         else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
289             xmlFreeDoc(i->doc);
290             i->doc = NULL;
291             i->root = NULL;
292             g_message("XML Document in given memory is of wrong "
293                       "type. Root node is not '%s'\n", root_node);
294         }
295         else
296             r = TRUE; /* ok ! */
297     }
298     return r;
299 }
300
301 gboolean obt_xml_save_file(ObtXmlInst *inst,
302                            const gchar *path,
303                            gboolean pretty)
304 {
305     return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
306 }
307
308 void obt_xml_close(ObtXmlInst *i)
309 {
310     if (i && i->doc) {
311         xmlFreeDoc(i->doc);
312         g_free(i->path);
313         i->doc = NULL;
314         i->root = NULL;
315         i->path = NULL;
316     }
317 }
318
319 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
320 {
321     g_assert(i->doc); /* a doc is open? */
322
323     while (node) {
324         if (node->name) {
325             struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
326             if (c) c->func(node, c->data);
327         }
328         node = node->next;
329     }
330 }
331
332 void obt_xml_tree_from_root(ObtXmlInst *i)
333 {
334     obt_xml_tree(i, i->root->children);
335 }
336
337 gchar *obt_xml_node_string(xmlNodePtr node)
338 {
339     xmlChar *c = xmlNodeGetContent(node);
340     gchar *s;
341     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
342     s = g_strdup(c ? (gchar*)c : "");
343     xmlFree(c);
344     return s;
345 }
346
347 gint obt_xml_node_int(xmlNodePtr node)
348 {
349     xmlChar *c = xmlNodeGetContent(node);
350     gint i;
351     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
352     i = c ? atoi((gchar*)c) : 0;
353     xmlFree(c);
354     return i;
355 }
356
357 gboolean obt_xml_node_bool(xmlNodePtr node)
358 {
359     xmlChar *c = xmlNodeGetContent(node);
360     gboolean b = FALSE;
361     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
362     if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
363         b = TRUE;
364     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
365         b = TRUE;
366     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
367         b = TRUE;
368     xmlFree(c);
369     return b;
370 }
371
372 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
373 {
374     xmlChar *c = xmlNodeGetContent(node);
375     gboolean r;
376     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
377     r = !xmlStrcasecmp(c, (const xmlChar*) val);
378     xmlFree(c);
379     return r;
380 }
381
382 xmlNodePtr obt_xml_find_sibling(xmlNodePtr node, const gchar *tag)
383 {
384     while (node) {
385         if (!xmlStrcmp(node->name, (const xmlChar*) tag))
386             return node;
387         node = node->next;
388     }
389     return NULL;
390 }
391
392 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
393                            gboolean *value)
394 {
395     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
396     gboolean r = FALSE;
397     if (c) {
398         g_strstrip((char*)c); /* strip leading/trailing whitespace */
399         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
400             *value = TRUE, r = TRUE;
401         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
402             *value = TRUE, r = TRUE;
403         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
404             *value = TRUE, r = TRUE;
405         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
406             *value = FALSE, r = TRUE;
407         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
408             *value = FALSE, r = TRUE;
409         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
410             *value = FALSE, r = TRUE;
411     }
412     xmlFree(c);
413     return r;
414 }
415
416 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
417 {
418     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
419     gboolean r = FALSE;
420     if (c) {
421         g_strstrip((char*)c); /* strip leading/trailing whitespace */
422         *value = atoi((gchar*)c);
423         r = TRUE;
424     }
425     xmlFree(c);
426     return r;
427 }
428
429 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
430                              gchar **value)
431 {
432     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
433     gboolean r = FALSE;
434     if (c) {
435         g_strstrip((char*)c); /* strip leading/trailing whitespace */
436         *value = g_strdup((gchar*)c);
437         r = TRUE;
438     }
439     xmlFree(c);
440     return r;
441 }
442
443 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
444                                const gchar *val)
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         r = !xmlStrcasecmp(c, (const xmlChar*) val);
451     }
452     xmlFree(c);
453     return r;
454 }
455
456 xmlNodePtr obt_xml_path_get_node(xmlNodePtr subtree, const gchar *path,
457                                  const gchar *default_value)
458 {
459     xmlNodePtr n, c;
460     gchar **nodes;
461     gchar **it, **next;
462
463     g_return_val_if_fail(subtree != NULL, NULL);
464
465     n = subtree;
466
467     nodes = g_strsplit(path, "/", 0);
468     for (it = nodes; *it; it = next) {
469         gchar **attrs;
470         gboolean ok = FALSE;
471
472         attrs = g_strsplit(*it, ":", 0);
473         next = it + 1;
474
475         /* match attributes */
476         c = obt_xml_find_sibling(n->children, attrs[0]);
477         while (c && !ok) {
478             gint i;
479
480             ok = TRUE;
481             for (i = 1; attrs[i]; ++i) {
482                 gchar **eq = g_strsplit(attrs[i], "=", 2);
483                 if (eq[1] && !obt_xml_attr_contains(c, eq[0], eq[1]))
484                     ok = FALSE;
485                 g_strfreev(eq);
486             }
487             if (!ok)
488                 c = obt_xml_find_sibling(c->next, attrs[0]);
489         }
490
491         if (!c) {
492             gint i;
493
494             c = xmlNewTextChild(n, NULL, (xmlChar*)attrs[0],
495                                 *next ? NULL : (xmlChar*)default_value);
496
497             for (i = 1; attrs[i]; ++i) {
498                 gchar **eq = g_strsplit(attrs[i], "=", 2);
499                 if (eq[1])
500                     xmlNewProp(c, (xmlChar*)eq[0], (xmlChar*)eq[1]);
501                 g_strfreev(eq);
502             }
503         }
504         n = c;
505
506         g_strfreev(attrs);
507     }
508
509     g_strfreev(nodes);
510
511     return n;
512 }
513
514 void obt_xml_path_delete_node(xmlNodePtr subtree, const gchar *path)
515 {
516     xmlNodePtr n;
517
518     n = obt_xml_path_get_node(subtree, path, NULL);
519     xmlUnlinkNode(n);
520     xmlFreeNode(n);
521 }