rewrote gconf wrappers (still work in progress and they are not tried yet)
[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   validateClient();
24 }
25
26 void GConfItem::validateClient() {
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 GConfStringValue::GConfStringValue(const GConfKey& key, const std::string& name, const std::string& defaultValue):
36   GConfOption(key, name),
37   myDefaultValue(defaultValue) {
38 }
39
40 const std::string& GConfStringValue::value() const {
41   if (!myIsSynchronized) {
42     GConfValue *value = getGConfValue(GCONF_VALUE_STRING);
43
44     if (value == NULL) {
45       myValue = myDefaultValue;
46     } else {
47       myValue = gconf_value_get_string(value);
48
49       gconf_value_free(value);
50     }
51
52     myIsSynchronized = true;
53   }
54
55   return myValue;
56 }
57
58 const std::string& GConfStringValue::setValue(const std::string& newValue) {
59   if (!myIsSynchronized || (myValue != newValue)) {
60     myValue = newValue;
61
62     if (myValue == myDefaultValue) {
63       unsetGConfValue();
64     } else {
65       GConfValue *value = gconf_value_new(GCONF_VALUE_STRING);
66
67       gconf_value_set_string(value, myValue.c_str());
68
69       setGConfValue(value);
70
71       gconf_value_free(value);
72     }
73
74     myIsSynchronized = true;
75   }
76
77   return myValue;
78 }
79
80 GConfBooleanValue::GConfBooleanValue(const GConfKey& key, const std::string& name, bool defaultValue):
81   GConfOption(key, name),
82   myDefaultValue(defaultValue) {
83 }
84
85 bool GConfBooleanValue::value() const {
86   if (!myIsSynchronized) {
87     GConfValue *value = getGConfValue(GCONF_VALUE_BOOL);
88
89     if (value == NULL) {
90       myValue = myDefaultValue;
91     } else {
92       myValue = gconf_value_get_bool(value);
93
94       gconf_value_free(value);
95     }
96
97     myIsSynchronized = true;
98   }
99
100   return myValue;
101 }
102
103 bool GConfBooleanValue::setValue(bool newValue) {
104   if (!myIsSynchronized || (myValue != newValue)) {
105     myValue = newValue;
106
107     if (myValue == myDefaultValue) {
108       unsetGConfValue();
109     } else {
110       GConfValue *value = gconf_value_new(GCONF_VALUE_BOOL);
111
112       gconf_value_set_bool(value, myValue);
113
114       setGConfValue(value);
115
116       gconf_value_free(value);
117     }
118
119     myIsSynchronized = true;
120   }
121
122   return myValue;
123 }
124
125 GConfIntegerValue::GConfIntegerValue(const GConfKey& key, const std::string& name, int defaultValue):
126   GConfOption(key, name),
127   myDefaultValue(defaultValue) {
128 }
129
130 int GConfIntegerValue::value() const {
131   if (!myIsSynchronized) {
132     GConfValue *value = getGConfValue(GCONF_VALUE_INT);
133
134     if (value == NULL) {
135       myValue = myDefaultValue;
136     } else {
137       myValue = gconf_value_get_int(value);
138
139       gconf_value_free(value);
140     }
141
142     myIsSynchronized = true;
143   }
144
145   return myValue;
146 }
147
148 int GConfIntegerValue::setValue(int newValue) {
149   if (!myIsSynchronized || (myValue != newValue)) {
150     myValue = newValue;
151
152     if (myValue == myDefaultValue) {
153       unsetGConfValue();
154     } else {
155       GConfValue *value = gconf_value_new(GCONF_VALUE_INT);
156
157       gconf_value_set_int(value, myValue);
158
159       setGConfValue(value);
160
161       gconf_value_free(value);
162     }
163
164     myIsSynchronized = true;
165   }
166
167   return myValue;
168 }
169
170 // vim:ts=2:sw=2:et