]> icculus.org git repositories - dana/openbox.git/blob - obt/xml.c
Make the obt_watch functionality work without inotify (via manual refreshes).
[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_cache_file(ObtXmlInst *i,
207                                  const gchar *domain,
208                                  const gchar *filename,
209                                  const gchar *root_node)
210 {
211     GSList *paths = NULL;
212     gboolean r;
213
214     paths = g_slist_append(paths,
215                            g_strdup(obt_paths_cache_home(i->xdg_paths)));
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_config_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_config_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_data_file(ObtXmlInst *i,
247                                 const gchar *domain,
248                                 const gchar *filename,
249                                 const gchar *root_node)
250 {
251     GSList *it, *paths = NULL;
252     gboolean r;
253
254     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
255         paths = g_slist_append(paths, g_strdup(it->data));
256
257     r = load_file(i, domain, filename, root_node, paths);
258
259     while (paths) {
260         g_free(paths->data);
261         paths = g_slist_delete_link(paths, paths);
262     }
263     return r;
264 }
265
266 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
267                                  const gchar *theme,
268                                  const gchar *domain,
269                                  const gchar *filename,
270                                  const gchar *root_node)
271 {
272     GSList *it, *paths = NULL;
273     gboolean r;
274
275     /* use ~/.themes for backwards compatibility */
276     paths = g_slist_append
277         (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
278
279     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
280         paths = g_slist_append
281             (paths, g_build_filename(it->data, "themes", theme, NULL));
282
283     r = load_file(i, domain, filename, root_node, paths);
284
285     while (paths) {
286         g_free(paths->data);
287         paths = g_slist_delete_link(paths, paths);
288     }
289     return r;
290 }
291
292
293 gboolean obt_xml_load_mem(ObtXmlInst *i,
294                           gpointer data, guint len, const gchar *root_node)
295 {
296     gboolean r = FALSE;
297
298     g_assert(i->doc == NULL); /* another doc isn't open already? */
299
300     i->doc = xmlParseMemory(data, len);
301     if (i) {
302         i->root = xmlDocGetRootElement(i->doc);
303         if (!i->root) {
304             xmlFreeDoc(i->doc);
305             i->doc = NULL;
306             g_message("Given memory is an empty document");
307         }
308         else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
309             xmlFreeDoc(i->doc);
310             i->doc = NULL;
311             i->root = NULL;
312             g_message("XML Document in given memory is of wrong "
313                       "type. Root node is not '%s'\n", root_node);
314         }
315         else
316             r = TRUE; /* ok ! */
317     }
318     return r;
319 }
320
321 gboolean obt_xml_save_file(ObtXmlInst *inst,
322                            const gchar *path,
323                            gboolean pretty)
324 {
325     return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
326 }
327
328 gboolean obt_xml_save_cache_file(ObtXmlInst *inst,
329                                  const gchar *domain,
330                                  const gchar *filename,
331                                  gboolean pretty)
332 {
333     gchar *dpath, *fpath;
334     gboolean ok;
335
336     dpath = g_build_filename(obt_paths_cache_home(inst->xdg_paths),
337                              domain, NULL);
338     fpath = g_build_filename(dpath, filename, NULL);
339
340     ok = obt_paths_mkdir_path(dpath, 0700);
341     ok = ok && obt_xml_save_file(inst, fpath, pretty);
342
343     g_free(fpath);
344     g_free(dpath);
345     return ok;
346 }
347
348 const gchar* obt_xml_file_path(ObtXmlInst *inst)
349 {
350     return inst->path;
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 guint obt_xml_node_line(xmlNodePtr node)
383 {
384     return XML_GET_LINE(node);
385 }
386
387 gchar *obt_xml_node_string(xmlNodePtr node)
388 {
389     xmlChar *c;
390     gchar *s;
391     c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
392     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
393     s = g_strdup(c ? (gchar*)c : "");
394     xmlFree(c);
395     return s;
396 }
397
398 gchar *obt_xml_node_string_raw(xmlNodePtr node)
399 {
400     xmlChar *c;
401     gchar *s;
402     c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
403     s = g_strdup(c ? (gchar*)c : "");
404     xmlFree(c);
405     return s;
406 }
407
408 gint obt_xml_node_int(xmlNodePtr node)
409 {
410     xmlChar *c;
411     gint i;
412     c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
413     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
414     i = c ? atoi((gchar*)c) : 0;
415     xmlFree(c);
416     return i;
417 }
418
419 gboolean obt_xml_node_bool(xmlNodePtr node)
420 {
421     xmlChar *c;
422     gboolean b = FALSE;
423     c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
424     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
425     if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
426         b = TRUE;
427     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
428         b = TRUE;
429     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
430         b = TRUE;
431     xmlFree(c);
432     return b;
433 }
434
435 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
436 {
437     xmlChar *c;
438     gboolean r;
439     c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
440     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
441     r = !xmlStrcasecmp(c, (const xmlChar*) val);
442     xmlFree(c);
443     return r;
444 }
445
446 xmlNodePtr obt_xml_find_sibling(xmlNodePtr node, const gchar *tag)
447 {
448     while (node) {
449         if (!xmlStrcmp(node->name, (const xmlChar*) tag))
450             return node;
451         node = node->next;
452     }
453     return NULL;
454 }
455
456 void obt_xml_node_set_string(xmlNodePtr node, const gchar *s)
457 {
458     xmlNodeSetContent(node, (const xmlChar*)s);
459 }
460
461 void obt_xml_node_set_int(xmlNodePtr node, gint i)
462 {
463     gchar *s = g_strdup_printf("%d", i);
464     obt_xml_node_set_string(node, s);
465     g_free(s);
466 }
467
468 void obt_xml_node_set_bool(xmlNodePtr node, gboolean b)
469 {
470     obt_xml_node_set_string(node, b ? "yes" : "no");
471 }
472
473 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
474                            gboolean *value)
475 {
476     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
477     gboolean r = FALSE;
478     if (c) {
479         g_strstrip((char*)c); /* strip leading/trailing whitespace */
480         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
481             *value = TRUE, r = TRUE;
482         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
483             *value = TRUE, r = TRUE;
484         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
485             *value = TRUE, r = TRUE;
486         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
487             *value = FALSE, r = TRUE;
488         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
489             *value = FALSE, r = TRUE;
490         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
491             *value = FALSE, r = TRUE;
492     }
493     xmlFree(c);
494     return r;
495 }
496
497 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
498 {
499     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
500     gboolean r = FALSE;
501     if (c) {
502         g_strstrip((char*)c); /* strip leading/trailing whitespace */
503         *value = atoi((gchar*)c);
504         r = TRUE;
505     }
506     xmlFree(c);
507     return r;
508 }
509
510 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
511                              gchar **value)
512 {
513     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
514     gboolean r = FALSE;
515     if (c) {
516         g_strstrip((char*)c); /* strip leading/trailing whitespace */
517         *value = g_strdup((gchar*)c);
518         r = TRUE;
519     }
520     xmlFree(c);
521     return r;
522 }
523
524 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
525                                const gchar *val)
526 {
527     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
528     gboolean r = FALSE;
529     if (c) {
530         g_strstrip((char*)c); /* strip leading/trailing whitespace */
531         r = !xmlStrcasecmp(c, (const xmlChar*) val);
532     }
533     xmlFree(c);
534     return r;
535 }
536
537 /*! Finds a sibling which matches a name and attributes.
538   @first The first sibling in the list.
539   @attrs NULL-terminated array of strings.  The first string is the name of
540     the node.  The remaining values are key,value pairs for attributes the
541     node should have.
542 */
543 static xmlNodePtr find_sibling(xmlNodePtr first, gchar **attrs)
544 {
545     xmlNodePtr c;
546     gboolean ok;
547
548     ok = FALSE;
549     c = obt_xml_find_sibling(first, attrs[0]);
550     while (c && !ok) {
551         gint i;
552
553         ok = TRUE;
554         for (i = 1; attrs[i]; ++i) {
555             gchar **eq = g_strsplit(attrs[i], "=", 2);
556             if (eq[1] && !obt_xml_attr_contains(c, eq[0], eq[1]))
557                 ok = FALSE;
558             g_strfreev(eq);
559         }
560         if (!ok)
561             c = obt_xml_find_sibling(c->next, attrs[0]);
562     }
563     return ok ? c : NULL;
564 }
565
566 static xmlNodePtr create_child(xmlNodePtr parent, gchar *const*attrs,
567                                const gchar *value)
568 {
569     xmlNodePtr c;
570     gint i;
571
572     c = xmlNewTextChild(parent, NULL, (xmlChar*)attrs[0], (xmlChar*)value);
573
574     for (i = 1; attrs[i]; ++i) {
575         gchar **eq = g_strsplit(attrs[i], "=", 2);
576         if (eq[1])
577             xmlNewProp(c, (xmlChar*)eq[0], (xmlChar*)eq[1]);
578         g_strfreev(eq);
579     }
580     return c;
581 }
582
583 xmlNodePtr obt_xml_path_get_node(xmlNodePtr subtree, const gchar *path,
584                                  const gchar *default_value)
585 {
586     xmlNodePtr n, c;
587     gchar **nodes;
588     gchar **it, **next;
589
590     g_return_val_if_fail(subtree != NULL, NULL);
591
592     n = subtree;
593
594     nodes = g_strsplit(path, "/", 0);
595     for (it = nodes; *it; it = next) {
596         gchar **attrs;
597
598         attrs = g_strsplit(*it, ":", 0);
599         next = it + 1;
600
601         /* match attributes */
602         c = find_sibling(n->children, attrs);
603
604         if (!c) {
605             if (*next)
606                 c = create_child(n, attrs, NULL);
607             else if (default_value)
608                 c = create_child(n, attrs, default_value);
609         }
610         n = c;
611
612         g_strfreev(attrs);
613     }
614
615     g_strfreev(nodes);
616
617     return n;
618 }
619
620 GList* obt_xml_path_get_list(xmlNodePtr subtree, const gchar *path)
621 {
622     xmlNodePtr n, c;
623     gchar **nodes;
624     gchar **it, **next;
625     GList *list;
626
627     g_return_val_if_fail(subtree != NULL, NULL);
628
629     n = subtree;
630     list = NULL;
631
632     nodes = g_strsplit(path, "/", 0);
633     if (nodes[0]) {
634         gchar **attrs;
635
636         for (it = nodes, next = it + 1; *next; it = next) {
637             attrs = g_strsplit(*it, ":", 0);
638             next = it + 1;
639
640             /* match attributes */
641             c = find_sibling(n->children, attrs);
642             if (!c) c = create_child(n, attrs, NULL);
643             n = c;
644
645             g_strfreev(attrs);
646         }
647
648         attrs = g_strsplit(*next, ":", 0);
649         c = n->children;
650         while (c && (c = find_sibling(c, attrs))) {
651             list = g_list_append(list, c);
652             c = c->next;
653         }
654         g_strfreev(attrs);
655     }
656
657     g_strfreev(nodes);
658
659     return list;
660 }
661
662
663 void obt_xml_path_delete_node(xmlNodePtr subtree, const gchar *path)
664 {
665     xmlNodePtr n;
666
667     n = obt_xml_path_get_node(subtree, path, NULL);
668     xmlUnlinkNode(n);
669     xmlFreeNode(n);
670 }
671
672 gchar* obt_xml_path_string(xmlNodePtr subtree, const gchar *path,
673                           const gchar *default_value)
674 {
675     xmlNodePtr n;
676
677     n = obt_xml_path_get_node(subtree, path, default_value);
678     return n ? obt_xml_node_string(n) : NULL;
679 }
680
681 int obt_xml_path_int(xmlNodePtr subtree, const gchar *path,
682                      const gchar *default_value)
683 {
684     xmlNodePtr n;
685
686     n = obt_xml_path_get_node(subtree, path, default_value);
687     return n ? obt_xml_node_int(n) : 0;
688 }
689
690 gboolean obt_xml_path_bool(xmlNodePtr subtree, const gchar *path,
691                            const gchar *default_value)
692 {
693     xmlNodePtr n;
694
695     n = obt_xml_path_get_node(subtree, path, default_value);
696     return n ? obt_xml_node_bool(n) : FALSE;
697 }
698
699 void obt_xml_path_set_string(xmlNodePtr subtree, const gchar *path,
700                              const gchar *value)
701 {
702     xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
703     obt_xml_node_set_string(n, value);
704 }
705
706 void obt_xml_path_set_int(xmlNodePtr subtree, const gchar *path,
707                           gint value)
708 {
709     xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
710     obt_xml_node_set_int(n, value);
711 }
712
713 void obt_xml_path_set_bool(xmlNodePtr subtree, const gchar *path,
714                            gboolean value)
715 {
716     xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
717     obt_xml_node_set_bool(n, value);
718 }