]> icculus.org git repositories - dana/openbox.git/blob - obt/parse.c
Merge branch 'backport' into work
[dana/openbox.git] / obt / parse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    obt/parse.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/parse.h"
20 #include "obt/paths.h"
21
22 #include <glib.h>
23
24 #ifdef HAVE_STDLIB_H
25 #  include <stdlib.h>
26 #endif
27 #ifdef HAVE_SYS_STAT_H
28 #  include <sys/stat.h>
29 #endif
30 #ifdef HAVE_SYS_TYPES_H
31 #  include <sys/types.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #  include <unistd.h>
35 #endif
36
37 struct Callback {
38     gchar *tag;
39     ObtParseCallback func;
40     gpointer data;
41 };
42
43 struct _ObtParseInst {
44     gint ref;
45     ObtPaths *xdg_paths;
46     GHashTable *callbacks;
47     xmlDocPtr doc;
48     xmlNodePtr root;
49     gchar *path;
50 };
51
52 static void destfunc(struct Callback *c)
53 {
54     g_free(c->tag);
55     g_free(c);
56 }
57
58 ObtParseInst* obt_parse_instance_new(void)
59 {
60     ObtParseInst *i = g_new(ObtParseInst, 1);
61     i->ref = 1;
62     i->xdg_paths = obt_paths_new();
63     i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
64                                          (GDestroyNotify)destfunc);
65     i->doc = NULL;
66     i->root = NULL;
67     i->path = NULL;
68     return i;
69 }
70
71 void obt_parse_instance_ref(ObtParseInst *i)
72 {
73     ++i->ref;
74 }
75
76 void obt_parse_instance_unref(ObtParseInst *i)
77 {
78     if (i && --i->ref == 0) {
79         obt_paths_unref(i->xdg_paths);
80         g_hash_table_destroy(i->callbacks);
81         g_free(i);
82     }
83 }
84
85 xmlDocPtr obt_parse_doc(ObtParseInst *i)
86 {
87     g_assert(i->doc); /* a doc is open? */
88     return i->doc;
89 }
90
91 xmlNodePtr obt_parse_root(ObtParseInst *i)
92 {
93     g_assert(i->doc); /* a doc is open? */
94     return i->root;
95 }
96
97 void obt_parse_register(ObtParseInst *i, const gchar *tag,
98                         ObtParseCallback func, gpointer data)
99 {
100     struct Callback *c;
101
102     if (g_hash_table_lookup(i->callbacks, tag)) {
103         g_error("Tag '%s' already registered", tag);
104         return;
105     }
106
107     c = g_new(struct Callback, 1);
108     c->tag = g_strdup(tag);
109     c->func = func;
110     c->data = data;
111     g_hash_table_insert(i->callbacks, c->tag, c);
112 }
113
114 static gboolean load_file(ObtParseInst *i,
115                           const gchar *domain,
116                           const gchar *filename,
117                           const gchar *root_node,
118                           GSList *paths)
119 {
120     GSList *it;
121     gboolean r = FALSE;
122
123     g_assert(i->doc == NULL); /* another doc isn't open already? */
124
125     for (it = paths; !r && it; it = g_slist_next(it)) {
126         gchar *path;
127         struct stat s;
128
129         if (!domain && !filename) /* given a full path to the file */
130             path = g_strdup(it->data);
131         else
132             path = g_build_filename(it->data, domain, filename, NULL);
133
134         if (stat(path, &s) >= 0) {
135             /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
136                with extra nodes in it. */
137             i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
138                                               XML_PARSE_RECOVER));
139             if (i->doc) {
140                 i->root = xmlDocGetRootElement(i->doc);
141                 if (!i->root) {
142                     xmlFreeDoc(i->doc);
143                     i->doc = NULL;
144                     g_message("%s is an empty XML document", path);
145                 }
146                 else if (xmlStrcmp(i->root->name,
147                                    (const xmlChar*)root_node)) {
148                     xmlFreeDoc(i->doc);
149                     i->doc = NULL;
150                     i->root = NULL;
151                     g_message("XML document %s is of wrong type. Root "
152                               "node is not '%s'", path, root_node);
153                 }
154                 else {
155                     i->path = g_strdup(path);
156                     r = TRUE; /* ok! */
157                 }
158             }
159         }
160
161         g_free(path);
162     }
163
164     return r;
165 }
166
167 gboolean obt_parse_load_file(ObtParseInst *i,
168                              const gchar *path,
169                              const gchar *root_node)
170 {
171     GSList *paths;
172     gboolean r;
173
174     paths = g_slist_append(NULL, g_strdup(path));
175
176     r = load_file(i, NULL, NULL, root_node, paths);
177
178     while (paths) {
179         g_free(paths->data);
180         paths = g_slist_delete_link(paths, paths);
181     }
182     return r;
183 }
184
185 gboolean obt_parse_load_config_file(ObtParseInst *i,
186                                     const gchar *domain,
187                                     const gchar *filename,
188                                     const gchar *root_node)
189 {
190     GSList *it, *paths = NULL;
191     gboolean r;
192
193     for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
194         paths = g_slist_append(paths, g_strdup(it->data));
195
196     r = load_file(i, domain, filename, root_node, paths);
197
198     while (paths) {
199         g_free(paths->data);
200         paths = g_slist_delete_link(paths, paths);
201     }
202     return r;
203 }
204
205 gboolean obt_parse_load_data_file(ObtParseInst *i,
206                                   const gchar *domain,
207                                   const gchar *filename,
208                                   const gchar *root_node)
209 {
210     GSList *it, *paths = NULL;
211     gboolean r;
212
213     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
214         paths = g_slist_append(paths, g_strdup(it->data));
215
216     r = load_file(i, domain, filename, root_node, paths);
217
218     while (paths) {
219         g_free(paths->data);
220         paths = g_slist_delete_link(paths, paths);
221     }
222     return r;
223 }
224
225 gboolean obt_parse_load_theme_file(ObtParseInst *i,
226                                    const gchar *theme,
227                                    const gchar *domain,
228                                    const gchar *filename,
229                                    const gchar *root_node)
230 {
231     GSList *it, *paths = NULL;
232     gboolean r;
233
234     /* use ~/.themes for backwards compatibility */
235     paths = g_slist_append
236         (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
237
238     for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
239         paths = g_slist_append
240             (paths, g_build_filename(it->data, "themes", theme, NULL));
241
242     r = load_file(i, domain, filename, root_node, paths);
243
244     while (paths) {
245         g_free(paths->data);
246         paths = g_slist_delete_link(paths, paths);
247     }
248     return r;
249 }
250
251
252 gboolean obt_parse_load_mem(ObtParseInst *i,
253                             gpointer data, guint len, const gchar *root_node)
254 {
255     gboolean r = FALSE;
256
257     g_assert(i->doc == NULL); /* another doc isn't open already? */
258
259     i->doc = xmlParseMemory(data, len);
260     if (i) {
261         i->root = xmlDocGetRootElement(i->doc);
262         if (!i->root) {
263             xmlFreeDoc(i->doc);
264             i->doc = NULL;
265             g_message("Given memory is an empty document");
266         }
267         else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
268             xmlFreeDoc(i->doc);
269             i->doc = NULL;
270             i->root = NULL;
271             g_message("XML Document in given memory is of wrong "
272                       "type. Root node is not '%s'\n", root_node);
273         }
274         else
275             r = TRUE; /* ok ! */
276     }
277     return r;
278 }
279
280 void obt_parse_close(ObtParseInst *i)
281 {
282     if (i && i->doc) {
283         xmlFreeDoc(i->doc);
284         g_free(i->path);
285         i->doc = NULL;
286         i->root = NULL;
287         i->path = NULL;
288     }
289 }
290
291 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
292 {
293     g_assert(i->doc); /* a doc is open? */
294
295     while (node) {
296         struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
297         if (c) c->func(node, c->data);
298         node = node->next;
299     }
300 }
301
302 void obt_parse_tree_from_root(ObtParseInst *i)
303 {
304     obt_parse_tree(i, i->root->children);
305 }
306
307 gchar *obt_parse_node_string(xmlNodePtr node)
308 {
309     xmlChar *c = xmlNodeGetContent(node);
310     gchar *s;
311     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
312     s = g_strdup(c ? (gchar*)c : "");
313     xmlFree(c);
314     return s;
315 }
316
317 gint obt_parse_node_int(xmlNodePtr node)
318 {
319     xmlChar *c = xmlNodeGetContent(node);
320     gint i;
321     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
322     i = c ? atoi((gchar*)c) : 0;
323     xmlFree(c);
324     return i;
325 }
326
327 gboolean obt_parse_node_bool(xmlNodePtr node)
328 {
329     xmlChar *c = xmlNodeGetContent(node);
330     gboolean b = FALSE;
331     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
332     if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
333         b = TRUE;
334     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
335         b = TRUE;
336     else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
337         b = TRUE;
338     xmlFree(c);
339     return b;
340 }
341
342 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
343 {
344     xmlChar *c = xmlNodeGetContent(node);
345     gboolean r;
346     if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
347     r = !xmlStrcasecmp(c, (const xmlChar*) val);
348     xmlFree(c);
349     return r;
350 }
351
352 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
353 {
354     while (node) {
355         if (!xmlStrcmp(node->name, (const xmlChar*) tag))
356             return node;
357         node = node->next;
358     }
359     return NULL;
360 }
361
362 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
363                              gboolean *value)
364 {
365     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
366     gboolean r = FALSE;
367     if (c) {
368         g_strstrip((char*)c); /* strip leading/trailing whitespace */
369         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
370             *value = TRUE, r = TRUE;
371         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
372             *value = TRUE, r = TRUE;
373         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
374             *value = TRUE, r = TRUE;
375         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
376             *value = FALSE, r = TRUE;
377         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
378             *value = FALSE, r = TRUE;
379         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
380             *value = FALSE, r = TRUE;
381     }
382     xmlFree(c);
383     return r;
384 }
385
386 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
387 {
388     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
389     gboolean r = FALSE;
390     if (c) {
391         g_strstrip((char*)c); /* strip leading/trailing whitespace */
392         *value = atoi((gchar*)c);
393         r = TRUE;
394     }
395     xmlFree(c);
396     return r;
397 }
398
399 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
400                                gchar **value)
401 {
402     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
403     gboolean r = FALSE;
404     if (c) {
405         g_strstrip((char*)c); /* strip leading/trailing whitespace */
406         *value = g_strdup((gchar*)c);
407         r = TRUE;
408     }
409     xmlFree(c);
410     return r;
411 }
412
413 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
414                                  const gchar *val)
415 {
416     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
417     gboolean r = FALSE;
418     if (c) {
419         g_strstrip((char*)c); /* strip leading/trailing whitespace */
420         r = !xmlStrcasecmp(c, (const xmlChar*) val);
421     }
422     xmlFree(c);
423     return r;
424 }