]> Dogcows Code - chaz/openbox/blob - src/Configuration.cc
changing the number of workspaces in the rc does something without restarting now!
[chaz/openbox] / src / Configuration.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Configuration.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 #include "../config.h"
24
25 #include "Configuration.hh"
26 #include "Util.hh"
27
28 #include <algorithm>
29
30 #ifdef HAVE_STDLIB_H
31 # include <stdlib.h>
32 #endif // HAVE_STDLIB_H
33
34 using std::string;
35
36 bool Configuration::m_initialized = False;
37
38 Configuration::Configuration(const string &file, bool autosave) {
39 setFile(file);
40 m_modified = False;
41 m_database = NULL;
42 m_autosave = autosave;
43 if (! m_initialized) {
44 XrmInitialize();
45 m_initialized = True;
46 }
47 }
48
49 Configuration::Configuration(bool autosave) {
50 m_modified = False;
51 m_database = NULL;
52 m_autosave = autosave;
53 if (! m_initialized) {
54 XrmInitialize();
55 m_initialized = True;
56 }
57 }
58
59 Configuration::~Configuration() {
60 if (m_database != NULL)
61 XrmDestroyDatabase(m_database);
62 }
63
64 void Configuration::setFile(const string &file) {
65 m_file = file;
66 }
67
68 void Configuration::setAutoSave(bool autosave) {
69 m_autosave = autosave;
70 }
71
72 void Configuration::save() {
73 assert(m_database != NULL);
74 XrmPutFileDatabase(m_database, m_file.c_str());
75 m_modified = False;
76 }
77
78 bool Configuration::load() {
79 if (m_database != NULL)
80 XrmDestroyDatabase(m_database);
81 m_modified = False;
82 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
83 return False;
84 return True;
85 }
86
87 bool Configuration::merge(const string &file, bool overwrite) {
88 if (XrmCombineFileDatabase(file.c_str(), &m_database, overwrite) == 0)
89 return False;
90 m_modified = True;
91 if (m_autosave)
92 save();
93 return True;
94 }
95
96 void Configuration::create() {
97 if (m_database != NULL)
98 XrmDestroyDatabase(m_database);
99 m_modified = False;
100 assert(NULL != (m_database = XrmGetStringDatabase("")));
101 }
102
103 void Configuration::setValue(const string &rname, bool value) {
104 assert(m_database != NULL);
105
106 const char *val = (value ? "True" : "False");
107 string rc_string = rname + ": " + val;
108 XrmPutLineResource(&m_database, rc_string.c_str());
109
110 m_modified = True;
111 if (m_autosave)
112 save();
113 }
114
115 void Configuration::setValue(const string &rname, unsigned long value) {
116 assert(m_database != NULL);
117
118 string rc_string = rname + ": " + itostring(value);
119 XrmPutLineResource(&m_database, rc_string.c_str());
120
121 m_modified = True;
122 if (m_autosave)
123 save();
124 }
125
126 void Configuration::setValue(const string &rname, long value) {
127 assert(m_database != NULL);
128
129 string rc_string = rname + ": " + itostring(value);
130 XrmPutLineResource(&m_database, rc_string.c_str());
131
132 m_modified = True;
133 if (m_autosave)
134 save();
135 }
136
137 void Configuration::setValue(const string &rname, const char *value) {
138 assert(m_database != NULL);
139 assert(value != NULL);
140
141 string rc_string = rname + ": " + value;
142 XrmPutLineResource(&m_database, rc_string.c_str());
143
144 m_modified = True;
145 if (m_autosave)
146 save();
147 }
148
149 void Configuration::setValue(const string &rname, const string &value) {
150 assert(m_database != NULL);
151
152 string rc_string = rname + ": " + value;
153 XrmPutLineResource(&m_database, rc_string.c_str());
154
155 m_modified = True;
156 if (m_autosave)
157 save();
158 }
159
160 bool Configuration::getValue(const string &rname, bool &value) const {
161 assert(m_database != NULL);
162
163 string rclass = createClassName(rname);
164
165 char *rettype;
166 XrmValue retvalue;
167 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
168 &rettype, &retvalue) || retvalue.addr == NULL)
169 return False;
170 string val = retvalue.addr;
171 if (val == "True" || val == "True")
172 value = True;
173 else
174 value = False;
175 return True;
176 }
177
178 bool Configuration::getValue(const string &rname, long &value) const {
179 assert(m_database != NULL);
180
181 string rclass = createClassName(rname);
182
183 char *rettype;
184 XrmValue retvalue;
185 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
186 &rettype, &retvalue) || retvalue.addr == NULL)
187 return False;
188 char *end;
189 value = strtol(retvalue.addr, &end, 10);
190 if (end == retvalue.addr)
191 return False;
192 return True;
193 }
194
195 bool Configuration::getValue(const string &rname, unsigned long &value) const {
196 assert(m_database != NULL);
197
198 string rclass = createClassName(rname);
199
200 char *rettype;
201 XrmValue retvalue;
202 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
203 &rettype, &retvalue) || retvalue.addr == NULL)
204 return False;
205 char *end;
206 value = strtoul(retvalue.addr, &end, 10);
207 if (end == retvalue.addr)
208 return False;
209 return True;
210 }
211
212 bool Configuration::getValue(const string &rname,
213 string &value) const {
214 assert(m_database != NULL);
215
216 string rclass = createClassName(rname);
217
218 char *rettype;
219 XrmValue retvalue;
220 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
221 &rettype, &retvalue) || retvalue.addr == NULL)
222 return False;
223 value = retvalue.addr;
224 return True;
225 }
226
227
228 string Configuration::createClassName(const string &rname) const {
229 string rclass(rname);
230
231 string::iterator it = rclass.begin(), end = rclass.end();
232 while (True) {
233 *it = toUpper(*it);
234 ++it;
235 if (it == end) break;
236 it = std::find(it, rclass.end(), '.');
237 if (it == end) break;
238 ++it;
239 if (it == end) break;
240 }
241 return rclass;
242 }
243
244
245 char Configuration::toUpper(char c) const {
246 if (c >= 'a' && c <= 'z')
247 return c - 'a' + 'A';
248 return c;
249 }
This page took 0.044087 seconds and 4 git commands to generate.