1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/xml.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
20 #include "obt/paths.h"
22 #include <libxml/xinclude.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
47 GHashTable *callbacks;
53 static void destfunc(struct Callback *c)
56 g_slice_free(struct Callback, c);
59 ObtXmlInst* obt_xml_instance_new(void)
61 ObtXmlInst *i = g_slice_new(ObtXmlInst);
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);
72 void obt_xml_instance_ref(ObtXmlInst *i)
77 void obt_xml_instance_unref(ObtXmlInst *i)
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);
86 xmlDocPtr obt_xml_doc(ObtXmlInst *i)
88 g_assert(i->doc); /* a doc is open? */
92 xmlNodePtr obt_xml_root(ObtXmlInst *i)
94 g_assert(i->doc); /* a doc is open? */
98 void obt_xml_register(ObtXmlInst *i, const gchar *tag,
99 ObtXmlCallback func, gpointer data)
103 if (g_hash_table_lookup(i->callbacks, tag)) {
104 g_error("Tag '%s' already registered", tag);
108 c = g_slice_new(struct Callback);
109 c->tag = g_strdup(tag);
112 g_hash_table_insert(i->callbacks, c->tag, c);
115 void obt_xml_unregister(ObtXmlInst *i, const gchar *tag)
117 g_hash_table_remove(i->callbacks, tag);
120 void obt_xml_new_file(ObtXmlInst *i,
121 const gchar *root_node)
125 g_assert(i->doc == NULL); /* another doc isn't open already? */
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);
133 static gboolean load_file(ObtXmlInst *i,
135 const gchar *filename,
136 const gchar *root_node,
142 g_assert(i->doc == NULL); /* another doc isn't open already? */
144 for (it = paths; !r && it; it = g_slist_next(it)) {
148 if (!domain && !filename) /* given a full path to the file */
149 path = g_strdup(it->data);
151 path = g_build_filename(it->data, domain, filename, NULL);
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 |
158 xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS |
161 i->root = xmlDocGetRootElement(i->doc);
165 g_message("%s is an empty XML document", path);
167 else if (xmlStrcmp(i->root->name,
168 (const xmlChar*)root_node)) {
172 g_message("XML document %s is of wrong type. Root "
173 "node is not '%s'", path, root_node);
176 i->path = g_strdup(path);
188 gboolean obt_xml_load_file(ObtXmlInst *i,
190 const gchar *root_node)
195 paths = g_slist_append(NULL, g_strdup(path));
197 r = load_file(i, NULL, NULL, root_node, paths);
201 paths = g_slist_delete_link(paths, paths);
206 gboolean obt_xml_load_cache_file(ObtXmlInst *i,
208 const gchar *filename,
209 const gchar *root_node)
211 GSList *paths = NULL;
214 paths = g_slist_append(paths,
215 g_strdup(obt_paths_cache_home(i->xdg_paths)));
217 r = load_file(i, domain, filename, root_node, paths);
221 paths = g_slist_delete_link(paths, paths);
226 gboolean obt_xml_load_config_file(ObtXmlInst *i,
228 const gchar *filename,
229 const gchar *root_node)
231 GSList *it, *paths = NULL;
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));
237 r = load_file(i, domain, filename, root_node, paths);
241 paths = g_slist_delete_link(paths, paths);
246 gboolean obt_xml_load_data_file(ObtXmlInst *i,
248 const gchar *filename,
249 const gchar *root_node)
251 GSList *it, *paths = NULL;
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));
257 r = load_file(i, domain, filename, root_node, paths);
261 paths = g_slist_delete_link(paths, paths);
266 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
269 const gchar *filename,
270 const gchar *root_node)
272 GSList *it, *paths = NULL;
275 /* use ~/.themes for backwards compatibility */
276 paths = g_slist_append
277 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
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));
283 r = load_file(i, domain, filename, root_node, paths);
287 paths = g_slist_delete_link(paths, paths);
293 gboolean obt_xml_load_mem(ObtXmlInst *i,
294 gpointer data, guint len, const gchar *root_node)
298 g_assert(i->doc == NULL); /* another doc isn't open already? */
300 i->doc = xmlParseMemory(data, len);
302 i->root = xmlDocGetRootElement(i->doc);
306 g_message("Given memory is an empty document");
308 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
312 g_message("XML Document in given memory is of wrong "
313 "type. Root node is not '%s'\n", root_node);
321 gboolean obt_xml_save_file(ObtXmlInst *inst,
325 return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
328 gboolean obt_xml_save_cache_file(ObtXmlInst *inst,
330 const gchar *filename,
333 gchar *dpath, *fpath;
336 dpath = g_build_filename(obt_paths_cache_home(inst->xdg_paths),
338 fpath = g_build_filename(dpath, filename, NULL);
340 ok = obt_paths_mkdir_path(dpath, 0700);
341 ok = ok && obt_xml_save_file(inst, fpath, pretty);
348 const gchar* obt_xml_file_path(ObtXmlInst *inst)
353 void obt_xml_close(ObtXmlInst *i)
364 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
366 g_assert(i->doc); /* a doc is open? */
370 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
371 if (c) c->func(node, c->data);
377 void obt_xml_tree_from_root(ObtXmlInst *i)
379 obt_xml_tree(i, i->root->children);
382 guint obt_xml_node_line(xmlNodePtr node)
384 return XML_GET_LINE(node);
387 gchar *obt_xml_node_string(xmlNodePtr node)
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 : "");
398 gchar *obt_xml_node_string_raw(xmlNodePtr node)
402 c = xmlNodeIsText(node->children) ? xmlNodeGetContent(node) : NULL;
403 s = g_strdup(c ? (gchar*)c : "");
408 gint obt_xml_node_int(xmlNodePtr node)
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;
419 gboolean obt_xml_node_bool(xmlNodePtr node)
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"))
427 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
429 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
435 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
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);
446 xmlNodePtr obt_xml_find_sibling(xmlNodePtr node, const gchar *tag)
449 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
456 void obt_xml_node_set_string(xmlNodePtr node, const gchar *s)
458 xmlNodeSetContent(node, (const xmlChar*)s);
461 void obt_xml_node_set_int(xmlNodePtr node, gint i)
463 gchar *s = g_strdup_printf("%d", i);
464 obt_xml_node_set_string(node, s);
468 void obt_xml_node_set_bool(xmlNodePtr node, gboolean b)
470 obt_xml_node_set_string(node, b ? "yes" : "no");
473 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
476 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
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;
497 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
499 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
502 g_strstrip((char*)c); /* strip leading/trailing whitespace */
503 *value = atoi((gchar*)c);
510 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
513 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
516 g_strstrip((char*)c); /* strip leading/trailing whitespace */
517 *value = g_strdup((gchar*)c);
524 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
527 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
530 g_strstrip((char*)c); /* strip leading/trailing whitespace */
531 r = !xmlStrcasecmp(c, (const xmlChar*) val);
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
543 static xmlNodePtr find_sibling(xmlNodePtr first, gchar **attrs)
549 c = obt_xml_find_sibling(first, attrs[0]);
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]))
561 c = obt_xml_find_sibling(c->next, attrs[0]);
563 return ok ? c : NULL;
566 static xmlNodePtr create_child(xmlNodePtr parent, gchar *const*attrs,
572 c = xmlNewTextChild(parent, NULL, (xmlChar*)attrs[0], (xmlChar*)value);
574 for (i = 1; attrs[i]; ++i) {
575 gchar **eq = g_strsplit(attrs[i], "=", 2);
577 xmlNewProp(c, (xmlChar*)eq[0], (xmlChar*)eq[1]);
583 xmlNodePtr obt_xml_path_get_node(xmlNodePtr subtree, const gchar *path,
584 const gchar *default_value)
590 g_return_val_if_fail(subtree != NULL, NULL);
594 nodes = g_strsplit(path, "/", 0);
595 for (it = nodes; *it; it = next) {
598 attrs = g_strsplit(*it, ":", 0);
601 /* match attributes */
602 c = find_sibling(n->children, attrs);
606 c = create_child(n, attrs, NULL);
607 else if (default_value)
608 c = create_child(n, attrs, default_value);
620 GList* obt_xml_path_get_list(xmlNodePtr subtree, const gchar *path)
627 g_return_val_if_fail(subtree != NULL, NULL);
632 nodes = g_strsplit(path, "/", 0);
636 for (it = nodes, next = it + 1; *next; it = next) {
637 attrs = g_strsplit(*it, ":", 0);
640 /* match attributes */
641 c = find_sibling(n->children, attrs);
642 if (!c) c = create_child(n, attrs, NULL);
648 attrs = g_strsplit(*next, ":", 0);
650 while (c && (c = find_sibling(c, attrs))) {
651 list = g_list_append(list, c);
663 void obt_xml_path_delete_node(xmlNodePtr subtree, const gchar *path)
667 n = obt_xml_path_get_node(subtree, path, NULL);
672 gchar* obt_xml_path_string(xmlNodePtr subtree, const gchar *path,
673 const gchar *default_value)
677 n = obt_xml_path_get_node(subtree, path, default_value);
678 return n ? obt_xml_node_string(n) : NULL;
681 int obt_xml_path_int(xmlNodePtr subtree, const gchar *path,
682 const gchar *default_value)
686 n = obt_xml_path_get_node(subtree, path, default_value);
687 return n ? obt_xml_node_int(n) : 0;
690 gboolean obt_xml_path_bool(xmlNodePtr subtree, const gchar *path,
691 const gchar *default_value)
695 n = obt_xml_path_get_node(subtree, path, default_value);
696 return n ? obt_xml_node_bool(n) : FALSE;
699 void obt_xml_path_set_string(xmlNodePtr subtree, const gchar *path,
702 xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
703 obt_xml_node_set_string(n, value);
706 void obt_xml_path_set_int(xmlNodePtr subtree, const gchar *path,
709 xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
710 obt_xml_node_set_int(n, value);
713 void obt_xml_path_set_bool(xmlNodePtr subtree, const gchar *path,
716 xmlNodePtr n = obt_xml_path_get_node(subtree, path, "");
717 obt_xml_node_set_bool(n, value);