Git init
[framework/uifw/isf.git] / ism / src / scim_config_base.cpp
1 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
2
3 /*
4  * Smart Common Input Method
5  *
6  * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
7  *
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this program; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  * Boston, MA  02111-1307  USA
23  *
24  * $Id: scim_config_base.cpp,v 1.19 2005/04/09 15:38:39 suzhe Exp $
25  */
26
27 #define Uses_SCIM_CONFIG_BASE
28 #define Uses_SCIM_CONFIG_PATH
29 #define Uses_SCIM_CONFIG_MODULE
30 #include "scim_private.h"
31 #include "scim.h"
32
33 namespace scim {
34
35 static ConfigPointer _scim_default_config (0);
36 static ConfigModule _scim_default_config_module;
37
38 static ConfigPointer
39 _create_config (const String &default_module)
40 {
41     if (!_scim_default_config_module.valid ()) {
42
43         String module;
44
45         if (!default_module.length ())
46             module = scim_global_config_read (SCIM_GLOBAL_CONFIG_DEFAULT_CONFIG_MODULE, String ("simple"));
47         else
48             module = default_module;
49
50         _scim_default_config_module.load (module);
51     }
52
53     if (_scim_default_config_module.valid ()) {
54         _scim_default_config = _scim_default_config_module.create_config ();
55         return _scim_default_config;
56     }
57
58     return ConfigPointer (0);
59 }
60
61 ConfigBase::ConfigBase ()
62 {
63 }
64
65 ConfigBase::~ConfigBase ()
66 {
67 }
68
69 bool
70 ConfigBase::valid () const
71 {
72     return true;
73 }
74
75 String
76 ConfigBase::read (const String& key, const String& defVal) const
77 {
78     String tmp;
79     if (!read (key, &tmp)) {
80         SCIM_DEBUG_CONFIG(1) << "Warning : No default scim::String value for key \"" << key << "\", "
81                              << "using default value.\n";
82         return defVal;
83     }
84     return tmp;
85 }
86
87 int
88 ConfigBase::read (const String& key, int defVal) const
89 {
90     int tmp = 0;
91     if (!read (key, &tmp)) {
92         SCIM_DEBUG_CONFIG(1) << "Warning : No default int value for key \"" << key << "\", "
93                              << "using default value.\n";
94         return defVal;
95     }
96     return tmp;
97 }
98
99 double
100 ConfigBase::read (const String& key, double defVal) const
101 {
102     double tmp = 0;
103     if (!read (key, &tmp)) {
104         SCIM_DEBUG_CONFIG(1) << "Warning : No default float value for key \"" << key << "\", "
105                              << "using default value.\n";
106         return defVal;
107     }
108     return tmp;
109 }
110
111 bool
112 ConfigBase::read (const String& key, bool defVal) const
113 {
114     bool tmp = false;
115     if (!read (key, &tmp)) {
116         SCIM_DEBUG_CONFIG(1) << "Warning : No default bool value for key \"" << key << "\", "
117                              << "using default value.\n";
118         return defVal;
119     }
120     return tmp;
121 }
122
123 std::vector <String>
124 ConfigBase::read (const String& key, const std::vector <String>& defVal) const
125 {
126     std::vector <String> tmp;
127     if (!read (key, &tmp)) {
128         SCIM_DEBUG_CONFIG(1) << "Warning : No default scim::String list value for key \"" << key << "\", "
129                              << "using default value.\n";
130         return defVal;
131     }
132     return tmp;
133 }
134
135 std::vector <int>
136 ConfigBase::read (const String& key, const std::vector <int>& defVal) const
137 {
138     std::vector <int> tmp;
139     if (!read (key, &tmp)) {
140         SCIM_DEBUG_CONFIG(1) << "Warning : No default int list value for key \"" << key << "\", "
141                              << "using default value.\n";
142         return defVal;
143     }
144     return tmp;
145 }
146
147 bool
148 ConfigBase::reload ()
149 {
150     if (!ConfigBase::valid ()) return false;
151
152     m_signal_reload.emit (this);
153
154     return true;
155 }
156
157 Connection
158 ConfigBase::signal_connect_reload (ConfigSlotVoid *slot)
159 {
160     return m_signal_reload.connect (slot);
161 }
162
163 ConfigPointer
164 ConfigBase::set (const ConfigPointer &p_config)
165 {
166     ConfigPointer old = _scim_default_config;
167
168     _scim_default_config = p_config;
169
170     return old;
171 }
172
173 ConfigPointer
174 ConfigBase::get (bool create_on_demand, const String &default_module)
175 {
176     if (create_on_demand && _scim_default_config.null ())
177         _create_config (default_module);
178     return _scim_default_config;
179 }
180
181 /*
182  * Implementation of DummyConfig
183  */
184
185 DummyConfig::DummyConfig ()
186 {
187 }
188
189 DummyConfig::~DummyConfig ()
190 {
191 }
192
193 bool
194 DummyConfig::valid () const
195 {
196     return ConfigBase::valid();
197 }
198
199 String
200 DummyConfig::get_name () const
201 {
202     return "dummy";
203 }
204
205 // String
206 bool
207 DummyConfig::read (const String& key, String *pStr) const
208 {
209     return false;
210 }
211
212 // int
213 bool
214 DummyConfig::read (const String& key, int *pl) const
215 {
216     return false;
217 }
218
219 // double
220 bool
221 DummyConfig::read (const String& key, double* val) const
222 {
223     return false;
224 }
225
226 // bool
227 bool
228 DummyConfig::read (const String& key, bool* val) const
229 {
230     return false;
231 }
232
233 //String list
234 bool
235 DummyConfig::read (const String& key, std::vector <String>* val) const
236 {
237     return false;
238 }
239
240 //int list
241 bool
242 DummyConfig::read (const String& key, std::vector <int>* val) const
243 {
244     return false;
245 }
246
247 // write the value (return true on success)
248 bool
249 DummyConfig::write (const String& key, const String& value)
250 {
251     return true;
252 }
253
254 bool
255 DummyConfig::write (const String& key, int value)
256 {
257     return true;
258 }
259
260 bool
261 DummyConfig::write (const String& key, double value)
262 {
263     return true;
264 }
265
266 bool
267 DummyConfig::write (const String& key, bool value)
268 {
269     return true;
270 }
271
272 bool
273 DummyConfig::write (const String& key, const std::vector <String>& value)
274 {
275     return true;
276 }
277
278 bool
279 DummyConfig::write (const String& key, const std::vector <int>& value)
280 {
281     return true;
282 }
283
284
285 // permanently writes all changes
286 bool
287 DummyConfig::flush()
288 {
289     return true;
290 }
291
292 // delete entries
293 bool
294 DummyConfig::erase (const String& key)
295 {
296     return true;
297 }
298
299 // reload the configurations.
300 bool
301 DummyConfig::reload ()
302 {
303     return ConfigBase::reload ();
304 }
305
306 } // namespace scim
307
308 /*
309 vi:ts=4:nowrap:ai:expandtab
310 */