* static method 'mergePath' is now normal method 'merge'
[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 GConfStringValue::GConfStringValue(const GConfKey& key, const std::string& name, const std::string& defaultValue):
51   GConfOption(key, name),
52   myDefaultValue(defaultValue) {
53 }
54
55 const std::string& GConfStringValue::value() const {
56   if (!myIsSynchronized) {
57     GConfValue *value = getGConfValue(GCONF_VALUE_STRING);
58
59     if (value == NULL) {
60       myValue = myDefaultValue;
61     } else {
62       myValue = gconf_value_get_string(value);
63
64       gconf_value_free(value);
65     }
66
67     myIsSynchronized = true;
68   }
69
70   return myValue;
71 }
72
73 const std::string& GConfStringValue::setValue(const std::string& newValue) {
74   if (!myIsSynchronized || (myValue != newValue)) {
75     myValue = newValue;
76
77     if (myValue == myDefaultValue) {
78       unsetGConfValue();
79     } else {
80       GConfValue *value = gconf_value_new(GCONF_VALUE_STRING);
81
82       gconf_value_set_string(value, myValue.c_str());
83
84       setGConfValue(value);
85
86       gconf_value_free(value);
87     }
88
89     myIsSynchronized = true;
90   }
91
92   return myValue;
93 }
94
95 GConfBooleanValue::GConfBooleanValue(const GConfKey& key, const std::string& name, bool defaultValue):
96   GConfOption(key, name),
97   myDefaultValue(defaultValue) {
98 }
99
100 bool GConfBooleanValue::value() const {
101   if (!myIsSynchronized) {
102     GConfValue *value = getGConfValue(GCONF_VALUE_BOOL);
103
104     if (value == NULL) {
105       myValue = myDefaultValue;
106     } else {
107       myValue = gconf_value_get_bool(value);
108
109       gconf_value_free(value);
110     }
111
112     myIsSynchronized = true;
113   }
114
115   return myValue;
116 }
117
118 bool GConfBooleanValue::setValue(bool newValue) {
119   if (!myIsSynchronized || (myValue != newValue)) {
120     myValue = newValue;
121
122     if (myValue == myDefaultValue) {
123       unsetGConfValue();
124     } else {
125       GConfValue *value = gconf_value_new(GCONF_VALUE_BOOL);
126
127       gconf_value_set_bool(value, myValue);
128
129       setGConfValue(value);
130
131       gconf_value_free(value);
132     }
133
134     myIsSynchronized = true;
135   }
136
137   return myValue;
138 }
139
140 GConfIntegerValue::GConfIntegerValue(const GConfKey& key, const std::string& name, int defaultValue):
141   GConfOption(key, name),
142   myDefaultValue(defaultValue) {
143 }
144
145 int GConfIntegerValue::value() const {
146   if (!myIsSynchronized) {
147     GConfValue *value = getGConfValue(GCONF_VALUE_INT);
148
149     if (value == NULL) {
150       myValue = myDefaultValue;
151     } else {
152       myValue = gconf_value_get_int(value);
153
154       gconf_value_free(value);
155     }
156
157     myIsSynchronized = true;
158   }
159
160   return myValue;
161 }
162
163 int GConfIntegerValue::setValue(int newValue) {
164   if (!myIsSynchronized || (myValue != newValue)) {
165     myValue = newValue;
166
167     if (myValue == myDefaultValue) {
168       unsetGConfValue();
169     } else {
170       GConfValue *value = gconf_value_new(GCONF_VALUE_INT);
171
172       gconf_value_set_int(value, myValue);
173
174       setGConfValue(value);
175
176       gconf_value_free(value);
177     }
178
179     myIsSynchronized = true;
180   }
181
182   return myValue;
183 }
184
185 // vim:ts=2:sw=2:et