Allow moving fullscreen windows between monitors
[openbox.git] / obt / xml.c
blobf200ae7543f53831962e26e054c011df48456802
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.
19 #include "obt/xml.h"
20 #include "obt/paths.h"
22 #include <libxml/xinclude.h>
23 #include <glib.h>
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
38 struct Callback {
39 gchar *tag;
40 ObtXmlCallback func;
41 gpointer data;
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;
56 static void obt_xml_save_last_error(ObtXmlInst* inst);
58 static void destfunc(struct Callback *c)
60 g_free(c->tag);
61 g_slice_free(struct Callback, c);
64 ObtXmlInst* obt_xml_instance_new(void)
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;
80 void obt_xml_instance_ref(ObtXmlInst *i)
82 ++i->ref;
85 void obt_xml_instance_unref(ObtXmlInst *i)
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);
96 xmlDocPtr obt_xml_doc(ObtXmlInst *i)
98 g_assert(i->doc); /* a doc is open? */
99 return i->doc;
102 xmlNodePtr obt_xml_root(ObtXmlInst *i)
104 g_assert(i->doc); /* a doc is open? */
105 return i->root;
108 void obt_xml_register(ObtXmlInst *i, const gchar *tag,
109 ObtXmlCallback func, gpointer data)
111 struct Callback *c;
113 if (g_hash_table_lookup(i->callbacks, tag)) {
114 g_error("Tag '%s' already registered", tag);
115 return;
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);
125 void obt_xml_unregister(ObtXmlInst *i, const gchar *tag)
127 g_hash_table_remove(i->callbacks, tag);
130 static gboolean load_file(ObtXmlInst *i,
131 const gchar *domain,
132 const gchar *filename,
133 const gchar *root_node,
134 GSList *paths)
136 GSList *it;
137 gboolean r = FALSE;
139 g_assert(i->doc == NULL); /* another doc isn't open already? */
141 xmlResetLastError();
143 for (it = paths; !r && it; it = g_slist_next(it)) {
144 gchar *path;
145 struct stat s;
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);
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);
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);
174 else {
175 i->path = g_strdup(path);
176 r = TRUE; /* ok! */
181 g_free(path);
184 obt_xml_save_last_error(i);
186 return r;
189 gboolean obt_xml_load_file(ObtXmlInst *i,
190 const gchar *path,
191 const gchar *root_node)
193 GSList *paths;
194 gboolean r;
196 paths = g_slist_append(NULL, g_strdup(path));
198 r = load_file(i, NULL, NULL, root_node, paths);
200 while (paths) {
201 g_free(paths->data);
202 paths = g_slist_delete_link(paths, paths);
204 return r;
207 gboolean obt_xml_load_config_file(ObtXmlInst *i,
208 const gchar *domain,
209 const gchar *filename,
210 const gchar *root_node)
212 GSList *it, *paths = NULL;
213 gboolean r;
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));
218 r = load_file(i, domain, filename, root_node, paths);
220 while (paths) {
221 g_free(paths->data);
222 paths = g_slist_delete_link(paths, paths);
224 return r;
227 gboolean obt_xml_load_data_file(ObtXmlInst *i,
228 const gchar *domain,
229 const gchar *filename,
230 const gchar *root_node)
232 GSList *it, *paths = NULL;
233 gboolean r;
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));
238 r = load_file(i, domain, filename, root_node, paths);
240 while (paths) {
241 g_free(paths->data);
242 paths = g_slist_delete_link(paths, paths);
244 return r;
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)
253 GSList *it, *paths = NULL;
254 gboolean r;
256 /* use ~/.themes for backwards compatibility */
257 paths = g_slist_append
258 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
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));
264 r = load_file(i, domain, filename, root_node, paths);
266 while (paths) {
267 g_free(paths->data);
268 paths = g_slist_delete_link(paths, paths);
270 return r;
274 gboolean obt_xml_load_mem(ObtXmlInst *i,
275 gpointer data, guint len, const gchar *root_node)
277 gboolean r = FALSE;
279 g_assert(i->doc == NULL); /* another doc isn't open already? */
281 xmlResetLastError();
283 i->doc = xmlParseMemory(data, len);
284 if (i->doc) {
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");
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);
298 else
299 r = TRUE; /* ok ! */
302 obt_xml_save_last_error(i);
304 return r;
307 static void obt_xml_save_last_error(ObtXmlInst* inst)
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);
318 gboolean obt_xml_last_error(ObtXmlInst *inst)
320 return inst->last_error_file &&
321 inst->last_error_line >= 0 &&
322 inst->last_error_message;
325 gchar* obt_xml_last_error_file(ObtXmlInst *inst)
327 if (!obt_xml_last_error(inst))
328 return NULL;
329 return inst->last_error_file;
332 gint obt_xml_last_error_line(ObtXmlInst *inst)
334 if (!obt_xml_last_error(inst))
335 return -1;
336 return inst->last_error_line;
339 gchar* obt_xml_last_error_message(ObtXmlInst *inst)
341 if (!obt_xml_last_error(inst))
342 return NULL;
343 return inst->last_error_message;
346 gboolean obt_xml_save_file(ObtXmlInst *inst,
347 const gchar *path,
348 gboolean pretty)
350 return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
353 void obt_xml_close(ObtXmlInst *i)
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;
364 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
366 g_assert(i->doc); /* a doc is open? */
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);
373 node = node->next;
377 void obt_xml_tree_from_root(ObtXmlInst *i)
379 obt_xml_tree(i, i->root->children);
382 gchar *obt_xml_node_string_unstripped(xmlNodePtr node)
384 xmlChar *c = xmlNodeGetContent(node);
385 gchar *s;
386 s = g_strdup(c ? (gchar*)c : "");
387 xmlFree(c);
388 return s;
391 gchar *obt_xml_node_string(xmlNodePtr node)
393 gchar* result = obt_xml_node_string_unstripped(node);
394 g_strstrip(result); /* strip leading/trailing whitespace */
395 return result;
398 gint obt_xml_node_int(xmlNodePtr node)
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;
408 gboolean obt_xml_node_bool(xmlNodePtr node)
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;
423 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
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;
433 xmlNodePtr obt_xml_find_node(xmlNodePtr node, const gchar *tag)
435 while (node) {
436 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
437 return node;
438 node = node->next;
440 return NULL;
443 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
444 gboolean *value)
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;
463 xmlFree(c);
464 return r;
467 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
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;
476 xmlFree(c);
477 return r;
480 gboolean obt_xml_attr_string_unstripped(xmlNodePtr node, const gchar *name,
481 gchar **value)
483 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
484 gboolean r = FALSE;
485 if (c) {
486 *value = g_strdup((gchar*)c);
487 r = TRUE;
489 xmlFree(c);
490 return r;
493 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
494 gchar **value)
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;
502 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
503 const gchar *val)
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);
511 xmlFree(c);
512 return r;