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