]> icculus.org git repositories - dana/obconf.git/blob - src/tree.c
add obconf.gladep tht glade makes
[dana/obconf.git] / src / tree.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    tree.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2003        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 "tree.h"
20 #include "main.h"
21 #include "openbox/parse.h"
22
23 #include <sys/types.h>
24 #include <signal.h>
25
26 xmlNodePtr tree_get_node(const gchar *path, const gchar *def)
27 {
28     xmlNodePtr n, c;
29     gchar **nodes;
30     gchar **it, **next;
31
32     n = root;
33
34     nodes = g_strsplit(path, "/", 0);
35     for (it = nodes; *it; it = next) {
36         gchar **attrs;
37         gboolean ok = FALSE;
38
39         attrs = g_strsplit(*it, ":", 0);
40         next = it + 1;
41
42         /* match attributes */
43         c = parse_find_node(attrs[0], n->children);
44         while (c && !ok) {
45             gint i;
46
47             ok = TRUE;
48             for (i = 1; attrs[i]; ++i) {
49                 gchar **eq = g_strsplit(attrs[i], "=", 2);
50                 if (eq[1] && !parse_attr_contains(eq[1], c, eq[0]))
51                     ok = FALSE;
52                 g_strfreev(eq);
53             }
54             if (!ok)
55                 c = parse_find_node(attrs[0], c->next);
56         }
57
58         if (!c) {
59             gint i;
60
61             c = xmlNewTextChild(n, NULL, *it, *next ? NULL : def);
62
63             for (i = 1; attrs[i]; ++i) {
64                 gchar **eq = g_strsplit(attrs[i], "=", 2);
65                 if (eq[1])
66                     xmlNewProp(c, eq[0], eq[1]);
67                 g_strfreev(eq);
68             }
69         }
70         n = c;
71
72         g_strfreev(attrs);
73     }
74
75     g_strfreev(nodes);
76
77     return n;
78 }
79
80 void tree_apply()
81 {
82     gchar *p;
83     gboolean err;
84
85     p = g_build_filename(parse_xdg_config_home_path(), "openbox", NULL);
86     parse_mkdir_path(p, 0700);
87     g_free(p);
88
89     p = g_build_filename(parse_xdg_config_home_path(), "openbox",
90                          "rc.xml", NULL);
91     err = xmlSaveFormatFile(p, doc, 1) == -1;
92     if (err) {
93         gchar *s;
94         s = g_strdup_printf("An error occured while saving the "
95                             "config file '%s'", p);
96         obconf_error(s);
97         g_free(s);
98     }
99     g_free(p);
100
101     if (!err) {
102         GdkAtom type;
103         gint format;
104         gint length;
105         guint *pid;
106
107         if (gdk_property_get
108             (gdk_screen_get_root_window(gdk_screen_get_default()),
109              gdk_atom_intern("_OPENBOX_PID", FALSE),
110              gdk_atom_intern("CARDINAL", FALSE),
111              0, 4, FALSE, &type, &format, &length, (guchar**)&pid)) {
112             kill(*pid, SIGUSR2);
113             g_free(pid);
114         }
115     }
116 }
117
118 void tree_set_string(const gchar *node, const gchar *value)
119 {
120     xmlNodePtr n;
121
122     n = tree_get_node(node, NULL);
123     xmlNodeSetContent(n, (const xmlChar*) value);
124
125     tree_apply();
126 }
127
128 void tree_set_int(const gchar *node, const gint value)
129 {
130     xmlNodePtr n;
131     gchar *s;
132
133     n = tree_get_node(node, NULL);
134     s = g_strdup_printf("%d", value);
135     xmlNodeSetContent(n, (const xmlChar*) s);
136     g_free(s);
137
138     tree_apply();
139 }
140
141 void tree_set_bool(const gchar *node, const gboolean value)
142 {
143     xmlNodePtr n;
144
145     n = tree_get_node(node, NULL);
146     xmlNodeSetContent(n, (const xmlChar*) (value ? "yes" : "no"));
147
148     tree_apply();
149 }
150
151 gchar* tree_get_string(const gchar *node, const gchar *def)
152 {
153     xmlNodePtr n;
154
155     n = tree_get_node(node, def);
156     return parse_string(doc, n);
157 }
158
159 gint tree_get_int(const gchar *node, gint def)
160 {
161     xmlNodePtr n;
162     gchar *d;
163
164     d = g_strdup_printf("%d", def);
165     n = tree_get_node(node, d);
166     g_free(d);
167     return parse_int(doc, n);
168 }
169
170 gboolean tree_get_bool(const gchar *node, gboolean def)
171 {
172     xmlNodePtr n;
173
174     n = tree_get_node(node, (def ? "yes" : "no"));
175     return parse_bool(doc, n);
176 }