]> Dogcows Code - chaz/openbox/blob - src/Configuration.cc
re-added UnderMouse Placement
[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 #ifdef HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif // HAVE_STDLIB_H
31
32 using std::string;
33
34 bool Configuration::m_initialized = false;
35
36 Configuration::Configuration(const string &file) {
37 setFile(file);
38 m_modified = false;
39 m_database = NULL;
40 m_autosave = true;
41 if (! m_initialized) {
42 XrmInitialize();
43 m_initialized = true;
44 }
45 }
46
47 Configuration::Configuration() {
48 m_modified = false;
49 m_database = NULL;
50 m_autosave = true;
51 if (! m_initialized) {
52 XrmInitialize();
53 m_initialized = true;
54 }
55 }
56
57 Configuration::~Configuration() {
58 if (m_database != NULL)
59 XrmDestroyDatabase(m_database);
60 }
61
62 void Configuration::setFile(const string &file) {
63 m_file = file;
64 }
65
66 void Configuration::setAutoSave(bool autosave) {
67 m_autosave = autosave;
68 }
69
70 void Configuration::save() {
71 assert(m_database != NULL);
72 XrmPutFileDatabase(m_database, m_file.c_str());
73 m_modified = false;
74 }
75
76 bool Configuration::load() {
77 if (m_database != NULL)
78 XrmDestroyDatabase(m_database);
79 m_modified = false;
80 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
81 return false;
82 return true;
83 }
84
85 void Configuration::create() {
86 if (m_database != NULL)
87 XrmDestroyDatabase(m_database);
88 m_modified = false;
89 assert(NULL != (m_database = XrmGetStringDatabase("")));
90 }
91
92 void Configuration::setValue(const string &rname, bool value) {
93 assert(m_database != NULL);
94
95 const char *val = (value ? "True" : "False");
96 string rc_string = rname + ": " + val;
97 XrmPutLineResource(&m_database, rc_string.c_str());
98
99 m_modified = true;
100 if (m_autosave)
101 save();
102 }
103
104 void Configuration::setValue(const string &rname, unsigned long value) {
105 assert(m_database != NULL);
106
107 string rc_string = rname + ": " + itostring(value);
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, 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, const char *value) {
127 assert(m_database != NULL);
128 assert(value != NULL);
129
130 string rc_string = rname + ": " + value;
131 XrmPutLineResource(&m_database, rc_string.c_str());
132
133 m_modified = true;
134 if (m_autosave)
135 save();
136 }
137
138 void Configuration::setValue(const string &rname, const string &value) {
139 assert(m_database != 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 bool Configuration::getValue(const string &rname, bool &value) const {
150 assert(m_database != NULL);
151
152 string rclass = createClassName(rname);
153
154 char *rettype;
155 XrmValue retvalue;
156 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
157 &rettype, &retvalue) || retvalue.addr == NULL)
158 return false;
159 string val = retvalue.addr;
160 if (val == "true" || val == "True")
161 value = true;
162 else
163 value = false;
164 return true;
165 }
166
167 bool Configuration::getValue(const string &rname, long &value) const {
168 assert(m_database != NULL);
169
170 string rclass = createClassName(rname);
171
172 char *rettype;
173 XrmValue retvalue;
174 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
175 &rettype, &retvalue) || retvalue.addr == NULL)
176 return false;
177 char *end;
178 value = strtol(retvalue.addr, &end, 10);
179 if (end == retvalue.addr)
180 return false;
181 return true;
182 }
183
184 bool Configuration::getValue(const string &rname, unsigned long &value) const {
185 assert(m_database != NULL);
186
187 string rclass = createClassName(rname);
188
189 char *rettype;
190 XrmValue retvalue;
191 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
192 &rettype, &retvalue) || retvalue.addr == NULL)
193 return false;
194 char *end;
195 value = strtoul(retvalue.addr, &end, 10);
196 if (end == retvalue.addr)
197 return false;
198 return true;
199 }
200
201 bool Configuration::getValue(const string &rname,
202 string &value) const {
203 assert(m_database != NULL);
204
205 string rclass = createClassName(rname);
206
207 char *rettype;
208 XrmValue retvalue;
209 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
210 &rettype, &retvalue) || retvalue.addr == NULL)
211 return false;
212 value = retvalue.addr;
213 return true;
214 }
215
216
217 string Configuration::createClassName(const string &rname) const {
218 string rclass(rname);
219
220 string::iterator it = rclass.begin(), end = rclass.end();
221 while (true) {
222 *it = toUpper(*it);
223 ++it;
224 if (it == end) break;
225 it = std::find(it, rclass.end(), '.');
226 if (it == end) break;
227 ++it;
228 if (it == end) break;
229 }
230 return rclass;
231 }
232
233
234 char Configuration::toUpper(char c) const {
235 if (c >= 'a' && c <= 'z')
236 return c - 'a' + 'A';
237 return c;
238 }
This page took 0.045222 seconds and 4 git commands to generate.