1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 tree.c for ObConf, the configuration tool for Openbox
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.
25 xmlNodePtr tree_get_node(const gchar *path, const gchar *def)
31 n = obt_xml_root(parse_i);
33 nodes = g_strsplit(path, "/", 0);
34 for (it = nodes; *it; it = next) {
38 attrs = g_strsplit(*it, ":", 0);
41 /* match attributes */
42 c = obt_xml_find_node(n->children, attrs[0]);
47 for (i = 1; attrs[i]; ++i) {
48 gchar **eq = g_strsplit(attrs[i], "=", 2);
49 if (eq[1] && !obt_xml_attr_contains(c, eq[0], eq[1]))
54 c = obt_xml_find_node(c->next, attrs[0]);
60 c = xmlNewTextChild(n, NULL, attrs[0], *next ? NULL : def);
62 for (i = 1; attrs[i]; ++i) {
63 gchar **eq = g_strsplit(attrs[i], "=", 2);
65 xmlNewProp(c, eq[0], eq[1]);
79 void tree_delete_node(const gchar *path)
83 n = tree_get_node(path, NULL);
94 p = g_strdup(obc_config_file);
96 p = g_build_filename(obt_paths_config_home(paths), "openbox",
99 d = g_path_get_dirname(p);
100 obt_paths_mkdir_path(d, 0700);
103 if (!obt_xml_save_file(parse_i, p, TRUE)) {
105 s = g_strdup_printf("An error occured while saving the "
106 "config file '%s'", p);
107 obconf_error(s, FALSE);
115 ce.xclient.type = ClientMessage;
116 ce.xclient.message_type = gdk_x11_get_xatom_by_name("_OB_CONTROL");
117 ce.xclient.display = GDK_DISPLAY();
118 ce.xclient.window = GDK_ROOT_WINDOW();
119 ce.xclient.format = 32;
120 ce.xclient.data.l[0] = 1; /* reconfigure */
121 ce.xclient.data.l[1] = 0;
122 ce.xclient.data.l[2] = 0;
123 ce.xclient.data.l[3] = 0;
124 ce.xclient.data.l[4] = 0;
125 XSendEvent(GDK_DISPLAY(), GDK_ROOT_WINDOW(), FALSE,
126 SubstructureNotifyMask | SubstructureRedirectMask,
131 void tree_set_string(const gchar *node, const gchar *value)
135 n = tree_get_node(node, NULL);
136 xmlNodeSetContent(n, (const xmlChar*) value);
141 void tree_set_int(const gchar *node, const gint value)
146 n = tree_get_node(node, NULL);
147 s = g_strdup_printf("%d", value);
148 xmlNodeSetContent(n, (const xmlChar*) s);
154 void tree_set_bool(const gchar *node, const gboolean value)
158 n = tree_get_node(node, NULL);
159 xmlNodeSetContent(n, (const xmlChar*) (value ? "yes" : "no"));
164 gchar* tree_get_string(const gchar *node, const gchar *def)
168 n = tree_get_node(node, def);
169 return obt_xml_node_string(n);
172 gint tree_get_int(const gchar *node, gint def)
177 d = g_strdup_printf("%d", def);
178 n = tree_get_node(node, d);
180 return obt_xml_node_int(n);
183 gboolean tree_get_bool(const gchar *node, gboolean def)
187 n = tree_get_node(node, (def ? "yes" : "no"));
188 return obt_xml_node_bool(n);