setting padding to 0 (instead of 2)
[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   myValue(defaultValue),
145   myDefaultValue(defaultValue) {
146 }
147
148 bool GConfBooleanOption::value() const {
149   if (!myIsSynchronized) {
150     GConfValue *value = getGConfValue();
151
152     if (value == NULL) {
153       myValue = myDefaultValue;
154     } else {
155       myValue = gconf_value_get_bool(value);
156
157       gconf_value_free(value);
158     }
159
160     myIsSynchronized = true;
161   }
162
163   return myValue;
164 }
165
166 bool GConfBooleanOption::setValue(bool newValue) {
167   if (!myIsSynchronized || (myValue != newValue)) {
168     myValue = newValue;
169
170     if (myValue == myDefaultValue) {
171       unsetGConfValue();
172     } else {
173       GConfValue *value = gconf_value_new(GCONF_VALUE_BOOL);
174
175       gconf_value_set_bool(value, myValue);
176
177       setGConfValue(value);
178
179       gconf_value_free(value);
180     }
181
182     myIsSynchronized = true;
183   }
184
185   return myValue;
186 }
187
188 GConfIntegerOption::GConfIntegerOption(const GConfKey& key, const std::string& name, int defaultValue):
189   GConfOption(key, name),
190   myValue(defaultValue),
191   myDefaultValue(defaultValue) {
192 }
193
194 int GConfIntegerOption::value() const {
195   if (!myIsSynchronized) {
196     GConfValue *value = getGConfValue();
197
198     if (value == NULL) {
199       myValue = myDefaultValue;
200     } else {
201       myValue = gconf_value_get_int(value);
202
203       gconf_value_free(value);
204     }
205
206     myIsSynchronized = true;
207   }
208
209   return myValue;
210 }
211
212 int GConfIntegerOption::setValue(int newValue) {
213   if (!myIsSynchronized || (myValue != newValue)) {
214     myValue = newValue;
215
216     if (myValue == myDefaultValue) {
217       unsetGConfValue();
218     } else {
219       GConfValue *value = gconf_value_new(GCONF_VALUE_INT);
220
221       gconf_value_set_int(value, myValue);
222
223       setGConfValue(value);
224
225       gconf_value_free(value);
226     }
227
228     myIsSynchronized = true;
229   }
230
231   return myValue;
232 }
233
234 // vim:ts=2:sw=2:et