do not remove test applications: they are removed in their Makefile
[simple-launcher] / gconf-wrapper.cc
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
4 //
5 // Simple Launcher is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License version 2 as published by
7 // the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 #include "gconf-wrapper.h"
19
20 GConfClient *GConfItem::ourClient = NULL;
21
22 GConfItem::GConfItem() {
23   allocateClient();
24 }
25
26 void GConfItem::allocateClient() {
27   if (ourClient == NULL) {
28     ourClient = gconf_client_get_default();
29   }
30 }
31
32 GConfKey::GConfKey(const std::string& path): myKeyPath(path) {
33 }
34
35 std::string GConfKey::merge(const std::string& path) const {
36   std::string result = myKeyPath;
37
38   if (path.empty()) {
39     // this is actually a bad situation, what to do??
40   } else if (path[0] == '/') {
41     result.append(path);
42   } else {
43     result.append("/");
44     result.append(path);
45   }
46
47   return result;
48 }
49
50 void GConfOption::setGConfValue(const GConfValue *value) {
51   GError *error = NULL;
52
53   gconf_client_set(ourClient, myPath.c_str(), value, &error);
54
55   if (error != NULL) {
56     g_error_free(error);
57   }
58 }
59
60 GConfValue *GConfOption::getGConfValue() const {
61   GConfValue *result = NULL;
62   GError *error = NULL;
63
64   result = gconf_client_get_without_default(ourClient, myPath.c_str(), &error);
65
66   if (error != NULL) {
67     g_error_free(error);
68
69     if (result != NULL) {
70       gconf_value_free(result);
71       result = NULL;
72     }
73   }
74
75   if (result != NULL) {
76     if (result->type != kind()) {
77       gconf_value_free(result);
78
79       result = 0;
80     }
81   }
82
83   return result;
84 }
85
86 void GConfOption::unsetGConfValue() {
87   GError *error = NULL;
88
89   // TODO: should I be picky about errors?
90   gconf_client_unset(ourClient, myPath.c_str(), &error);
91
92   if (error != NULL) {
93     g_error_free(error);
94   }
95 }
96
97 GConfStringOption::GConfStringOption(const GConfKey& key, const std::string& name, const std::string& defaultValue):
98   GConfOption(key, name),
99   myDefaultValue(defaultValue) {
100 }
101
102 const std::string& GConfStringOption::value() const {
103   if (!myIsSynchronized) {
104     GConfValue *value = getGConfValue();
105
106     if (value == NULL) {
107       myValue = myDefaultValue;
108     } else {
109       myValue = gconf_value_get_string(value);
110
111       gconf_value_free(value);
112     }
113
114     myIsSynchronized = true;
115   }
116
117   return myValue;
118 }
119
120 const std::string& GConfStringOption::setValue(const std::string& newValue) {
121   if (!myIsSynchronized || (myValue != newValue)) {
122     myValue = newValue;
123
124     if (myValue == myDefaultValue) {
125       unsetGConfValue();
126     } else {
127       GConfValue *value = gconf_value_new(GCONF_VALUE_STRING);
128
129       gconf_value_set_string(value, myValue.c_str());
130
131       setGConfValue(value);
132
133       gconf_value_free(value);
134     }
135
136     myIsSynchronized = true;
137   }
138
139   return myValue;
140 }
141
142 GConfBooleanOption::GConfBooleanOption(const GConfKey& key, const std::string& name, bool defaultValue):
143   GConfOption(key, name),
144   myDefaultValue(defaultValue) {
145 }
146
147 bool GConfBooleanOption::value() const {
148   if (!myIsSynchronized) {
149     GConfValue *value = getGConfValue();
150
151     if (value == NULL) {
152       myValue = myDefaultValue;
153     } else {
154       myValue = gconf_value_get_bool(value);
155
156       gconf_value_free(value);
157     }
158
159     myIsSynchronized = true;
160   }
161
162   return myValue;
163 }
164
165 bool GConfBooleanOption::setValue(bool newValue) {
166   if (!myIsSynchronized || (myValue != newValue)) {
167     myValue = newValue;
168
169     if (myValue == myDefaultValue) {
170       unsetGConfValue();
171     } else {
172       GConfValue *value = gconf_value_new(GCONF_VALUE_BOOL);
173
174       gconf_value_set_bool(value, myValue);
175
176       setGConfValue(value);
177
178       gconf_value_free(value);
179     }
180
181     myIsSynchronized = true;
182   }
183
184   return myValue;
185 }
186
187 GConfIntegerOption::GConfIntegerOption(const GConfKey& key, const std::string& name, int defaultValue):
188   GConfOption(key, name),
189   myDefaultValue(defaultValue) {
190 }
191
192 int GConfIntegerOption::value() const {
193   if (!myIsSynchronized) {
194     GConfValue *value = getGConfValue();
195
196     if (value == NULL) {
197       myValue = myDefaultValue;
198     } else {
199       myValue = gconf_value_get_int(value);
200
201       gconf_value_free(value);
202     }
203
204     myIsSynchronized = true;
205   }
206
207   return myValue;
208 }
209
210 int GConfIntegerOption::setValue(int newValue) {
211   if (!myIsSynchronized || (myValue != newValue)) {
212     myValue = newValue;
213
214     if (myValue == myDefaultValue) {
215       unsetGConfValue();
216     } else {
217       GConfValue *value = gconf_value_new(GCONF_VALUE_INT);
218
219       gconf_value_set_int(value, myValue);
220
221       setGConfValue(value);
222
223       gconf_value_free(value);
224     }
225
226     myIsSynchronized = true;
227   }
228
229   return myValue;
230 }
231
232 // vim:ts=2:sw=2:et