]> Dogcows Code - chaz/openbox/blob - src/Configuration.cc
be0cd096e7aa7de15ffb04c0fc6e0e9cc88d906e
[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) {
39 setFile(file);
40 m_modified = false;
41 m_database = NULL;
42 m_autosave = true;
43 if (! m_initialized) {
44 XrmInitialize();
45 m_initialized = true;
46 }
47 }
48
49 Configuration::Configuration() {
50 m_modified = false;
51 m_database = NULL;
52 m_autosave = true;
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 void Configuration::create() {
88 if (m_database != NULL)
89 XrmDestroyDatabase(m_database);
90 m_modified = false;
91 assert(NULL != (m_database = XrmGetStringDatabase("")));
92 }
93
94 void Configuration::setValue(const string &rname, bool value) {
95 assert(m_database != NULL);
96
97 const char *val = (value ? "True" : "False");
98 string rc_string = rname + ": " + val;
99 XrmPutLineResource(&m_database, rc_string.c_str());
100
101 m_modified = true;
102 if (m_autosave)
103 save();
104 }
105
106 void Configuration::setValue(const string &rname, unsigned long value) {
107 assert(m_database != NULL);
108
109 string rc_string = rname + ": " + itostring(value);
110 XrmPutLineResource(&m_database, rc_string.c_str());
111
112 m_modified = true;
113 if (m_autosave)
114 save();
115 }
116
117 void Configuration::setValue(const string &rname, long value) {
118 assert(m_database != NULL);
119
120 string rc_string = rname + ": " + itostring(value);
121 XrmPutLineResource(&m_database, rc_string.c_str());
122
123 m_modified = true;
124 if (m_autosave)
125 save();
126 }
127
128 void Configuration::setValue(const string &rname, const char *value) {
129 assert(m_database != NULL);
130 assert(value != NULL);
131
132 string rc_string = rname + ": " + value;
133 XrmPutLineResource(&m_database, rc_string.c_str());
134
135 m_modified = true;
136 if (m_autosave)
137 save();
138 }
139
140 void Configuration::setValue(const string &rname, const string &value) {
141 assert(m_database != NULL);
142
143 string rc_string = rname + ": " + value;
144 XrmPutLineResource(&m_database, rc_string.c_str());
145
146 m_modified = true;
147 if (m_autosave)
148 save();
149 }
150
151 bool Configuration::getValue(const string &rname, bool &value) const {
152 assert(m_database != NULL);
153
154 string rclass = createClassName(rname);
155
156 char *rettype;
157 XrmValue retvalue;
158 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
159 &rettype, &retvalue) || retvalue.addr == NULL)
160 return false;
161 string val = retvalue.addr;
162 if (val == "true" || val == "True")
163 value = true;
164 else
165 value = false;
166 return true;
167 }
168
169 bool Configuration::getValue(const string &rname, long &value) const {
170 assert(m_database != NULL);
171
172 string rclass = createClassName(rname);
173
174 char *rettype;
175 XrmValue retvalue;
176 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
177 &rettype, &retvalue) || retvalue.addr == NULL)
178 return false;
179 char *end;
180 value = strtol(retvalue.addr, &end, 10);
181 if (end == retvalue.addr)
182 return false;
183 return true;
184 }
185
186 bool Configuration::getValue(const string &rname, unsigned long &value) const {
187 assert(m_database != NULL);
188
189 string rclass = createClassName(rname);
190
191 char *rettype;
192 XrmValue retvalue;
193 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
194 &rettype, &retvalue) || retvalue.addr == NULL)
195 return false;
196 char *end;
197 value = strtoul(retvalue.addr, &end, 10);
198 if (end == retvalue.addr)
199 return false;
200 return true;
201 }
202
203 bool Configuration::getValue(const string &rname,
204 string &value) const {
205 assert(m_database != NULL);
206
207 string rclass = createClassName(rname);
208
209 char *rettype;
210 XrmValue retvalue;
211 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
212 &rettype, &retvalue) || retvalue.addr == NULL)
213 return false;
214 value = retvalue.addr;
215 return true;
216 }
217
218
219 string Configuration::createClassName(const string &rname) const {
220 string rclass(rname);
221
222 string::iterator it = rclass.begin(), end = rclass.end();
223 while (true) {
224 *it = toUpper(*it);
225 ++it;
226 if (it == end) break;
227 it = std::find(it, rclass.end(), '.');
228 if (it == end) break;
229 ++it;
230 if (it == end) break;
231 }
232 return rclass;
233 }
234
235
236 char Configuration::toUpper(char c) const {
237 if (c >= 'a' && c <= 'z')
238 return c - 'a' + 'A';
239 return c;
240 }
This page took 0.049901 seconds and 4 git commands to generate.