]> Dogcows Code - chaz/openbox/blob - src/Resource.cc
save_rc() using the obResource class to save blackbox's configuration.
[chaz/openbox] / src / Resource.cc
1 // Resource.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "Resource.h"
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 #ifdef HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif // HAVE_STDLIB_H
31
32 #ifdef HAVE_STDIO_H
33 # include <stdio.h>
34 #endif // HAVE_STDIO_H
35
36 #include <assert.h>
37
38 obResource::obResource(const std::string &file) {
39 setFile(file);
40 m_modified = false;
41 m_database = NULL;
42 m_autosave = true;
43 }
44
45 obResource::obResource() {
46 m_modified = false;
47 m_database = NULL;
48 m_autosave = true;
49 }
50
51 obResource::~obResource() {
52 if (m_database != NULL)
53 XrmDestroyDatabase(m_database);
54 }
55
56 void obResource::setFile(const std::string &file) {
57 m_file = file;
58 }
59
60 void obResource::setAutoSave(bool autosave) {
61 m_autosave = autosave;
62 }
63
64 void obResource::save() {
65 assert(m_database != NULL);
66 XrmPutFileDatabase(m_database, m_file.c_str());
67 m_modified = false;
68 }
69
70 bool obResource::load() {
71 if (m_database != NULL)
72 XrmDestroyDatabase(m_database);
73 m_modified = false;
74 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
75 return false;
76 return true;
77 }
78
79 void obResource::setValue(const std::string &rname, bool value) {
80 assert(m_database != NULL);
81
82 const char *val = (value ? "True" : "False");
83 std::string rc_string = rname + ": " + val;
84 XrmPutLineResource(&m_database, rc_string.c_str());
85
86 m_modified = true;
87 if (m_autosave)
88 save();
89 }
90
91 void obResource::setValue(const std::string &rname, int value) {
92 setValue(rname, (long)value);
93 }
94
95 void obResource::setValue(const std::string &rname, long value) {
96 assert(m_database != NULL);
97
98 char val[11];
99 sprintf(val, "%ld", value);
100 std::string rc_string = rname + ": " + val;
101 XrmPutLineResource(&m_database, rc_string.c_str());
102
103 m_modified = true;
104 if (m_autosave)
105 save();
106 }
107
108 void obResource::setValue(const std::string &rname, const char *value) {
109 assert(m_database != NULL);
110
111 std::string rc_string = rname + ": " + value;
112 XrmPutLineResource(&m_database, rc_string.c_str());
113
114 m_modified = true;
115 if (m_autosave)
116 save();
117 }
118
119 void obResource::setValue(const std::string &rname, const std::string &value) {
120 assert(m_database != NULL);
121
122 std::string rc_string = rname + ": " + value;
123 XrmPutLineResource(&m_database, rc_string.c_str());
124
125 m_modified = true;
126 if (m_autosave)
127 save();
128 }
129
130 bool obResource::getValue(const std::string &rname, const std::string &rclass,
131 bool &value) const {
132 assert(rclass.c_str() != NULL);
133 assert(m_database != NULL);
134
135 char *rettype;
136 XrmValue retvalue;
137 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
138 &rettype, &retvalue) || retvalue.addr == NULL)
139 return false;
140 std::string val = retvalue.addr;
141 if (0 == strncasecmp(val.c_str(), "true", val.length()))
142 value = true;
143 else
144 value = false;
145 return true;
146 }
147
148 bool obResource::getValue(const std::string &rname, const std::string &rclass,
149 long &value) const {
150 assert(m_database != NULL);
151
152 char *rettype;
153 XrmValue retvalue;
154 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
155 &rettype, &retvalue) || retvalue.addr == NULL)
156 return false;
157 char *end;
158 value = strtol(retvalue.addr, &end, 10);
159 if (end == retvalue.addr)
160 return false;
161 return true;
162 }
163
164 bool obResource::getValue(const std::string &rname, const std::string &rclass,
165 std::string &value) const {
166 assert(m_database != NULL);
167
168 char *rettype;
169 XrmValue retvalue;
170 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
171 &rettype, &retvalue) || retvalue.addr == NULL)
172 return false;
173 value = retvalue.addr;
174 return true;
175 }
This page took 0.048235 seconds and 5 git commands to generate.