1 /** @file scim_config_base.h
2 * @brief scim::ConfigBase Interface.
4 * Provide a unified interface to access the configuration data.
5 * All of SCIM objects should use this interface if they have any
9 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
12 * Smart Common Input Method
14 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this program; if not, write to the
29 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
30 * Boston, MA 02111-1307 USA
32 * $Id: scim_config_base.h,v 1.22 2005/04/09 15:38:39 suzhe Exp $
35 #ifndef __SCIM_CONFIG_BASE_H
36 #define __SCIM_CONFIG_BASE_H
41 * @ingroup InputServiceFramework
42 * The base classes for config modules
47 * @brief An exception class to hold Config related errors.
49 * scim::ConfigBase and its derived classes must throw
50 * scim::ConfigError object when error.
52 class ConfigError: public Exception
55 ConfigError (const String& what_arg)
56 : Exception (String("scim::Config: ") + what_arg) { }
61 * @typedef typedef Pointer <ConfigBase> ConfigPointer;
63 * A smart pointer for scim::ConfigBase and its derived classes.
65 typedef Pointer <ConfigBase> ConfigPointer;
68 * @typedef typedef Slot1<void, const ConfigPointer &> ConfigSlotVoid;
70 * The slot type to connect to the coresponding signal.
72 typedef Slot1<void, const ConfigPointer &> ConfigSlotVoid;
75 * @typedef typedef Signal1<void, const ConfigPointer &> ConfigSignalVoid;
77 * The signal type to connect with the ConfigSlotVoid slot type.
79 typedef Signal1<void, const ConfigPointer &> ConfigSignalVoid;
82 * @brief The interface class to access the configuration data.
84 * This is an interface class to access the configuration data.
85 * All of the SCIM objects which have configuration data should
86 * use this interface to store and load them.
88 class ConfigBase : public ReferencedObject
90 ConfigSignalVoid m_signal_reload;
94 * @name Constructor and Destructor.
104 * @brief Virtual destructor
105 * empty but ensures that dtor of all derived classes is virtual
107 virtual ~ConfigBase ();
114 * @name Pure virtual methods which should be implemented in derived classes.
119 * @brief Check if this Config object is valid.
120 * @return true if its valid and ready to work.
122 virtual bool valid () const = 0;
125 * @brief Return the name of this configuration module.
127 * This name must be same as the config module's name.
129 virtual String get_name () const = 0;
132 * @brief Read a string from the given key.
133 * @param key - the key to be read.
134 * @param ret - the result will be stored into *ret.
135 * @return true if the string is read successfully, otherwise return false.
137 virtual bool read (const String& key, String *ret) const = 0;
140 * @brief Read an int value from the given key.
141 * @param key - the key to be read.
142 * @param ret - the result will be stored into *ret.
143 * @return true if the value is read successfully, otherwise return false.
145 virtual bool read (const String& key, int *ret) const = 0;
148 * @brief Read a double value from the given key.
149 * @param key - the key to be read.
150 * @param ret - the result will be stored into *ret.
151 * @return true if the value is read successfully, otherwise return false.
153 virtual bool read (const String& key, double *ret) const = 0;
156 * @brief Read a bool value from the given key.
157 * @param key - the key to be read.
158 * @param ret - the result will be stored into *ret.
159 * @return true if the value is read successfully, otherwise return false.
161 virtual bool read (const String& key, bool *ret) const = 0;
164 * @brief Read a string list from the given key.
165 * @param key - the key to be read.
166 * @param ret - the result will be stored into *ret.
167 * @return true if the string list is read successfully, otherwise return false.
169 virtual bool read (const String& key, std::vector <String> *ret) const = 0;
172 * @brief Read an int list from the given key.
173 * @param key - the key to be read.
174 * @param ret - the result will be stored into *ret.
175 * @return true if the int list is read successfully, otherwise return false.
177 virtual bool read (const String& key, std::vector <int> *ret) const = 0;
180 * @brief Write a string to the given key.
181 * @param key - the key to be written.
182 * @param value - the string to be written to the key.
183 * @return true if success, otherwise false.
185 virtual bool write (const String& key, const String& value) = 0;
188 * @brief Write an int value to the given key.
189 * @param key - the key to be written.
190 * @param value - the int value to be written to the key.
191 * @return true if success, otherwise false.
193 virtual bool write (const String& key, int value) = 0;
196 * @brief Write a double value to the given key.
197 * @param key - the key to be written.
198 * @param value - the double value to be written to the key.
199 * @return true if success, otherwise false.
201 virtual bool write (const String& key, double value) = 0;
204 * @brief Write a bool value to the given key.
205 * @param key - the key to be written.
206 * @param value - the bool value to be written to the key.
207 * @return true if success, otherwise false.
209 virtual bool write (const String& key, bool value) = 0;
212 * @brief Write a string list to the given key.
213 * @param key - the key to be written.
214 * @param value - the string list to be written to the key.
215 * @return true if success, otherwise false.
217 virtual bool write (const String& key, const std::vector <String>& value) = 0;
220 * @brief Write an int list to the given key.
221 * @param key - the key to be written.
222 * @param value - the int list to be written to the key.
223 * @return true if success, otherwise false.
225 virtual bool write (const String& key, const std::vector <int>& value) = 0;
228 * @brief Permanently writes all changes.
229 * @return true if success.
231 virtual bool flush() = 0;
234 * @brief Erase a key and its value
235 * @param key - the key to be erased.
236 * @return true if success.
238 virtual bool erase (const String& key) = 0;
241 * @brief Reload the configurations from storage.
243 * All modified keys after the last flush maybe lost.
245 * The derived method should call this base method
246 * after reload the configurations successfully,
247 * in order to emit the reload signal.
249 * The derived method should have some machanism to avoid
250 * reload again if there is no update after
251 * the previous reload.
253 * @return true if success.
255 virtual bool reload () = 0;
262 * @name Other helper methods.
267 * @brief Read a string from the given key with a default fallback value.
269 * If failed to read from the given key, then return the given default value.
271 * @param key - the key to be read.
272 * @param defVal - the default value to be return if failed to read.
273 * @return The result string.
275 String read (const String& key, const String& defVal = String ()) const;
278 * @brief Read an int value from the given key with a default fallback value.
280 * If failed to read from the given key, then return the given default value.
282 * @param key - the key to be read.
283 * @param defVal - the default value to be return if failed to read.
284 * @return The result int value.
286 int read (const String& key, int defVal) const;
289 * @brief Read a double value from the given key with a default fallback value.
291 * If failed to read from the given key, then return the given default value.
293 * @param key - the key to be read.
294 * @param defVal - the default value to be return if failed to read.
295 * @return The result double value.
297 double read (const String& key, double defVal) const;
300 * @brief Read a bool value from the given key with a default fallback value.
302 * If failed to read from the given key, then return the given default value.
304 * @param key - the key to be read.
305 * @param defVal - the default value to be return if failed to read.
306 * @return The result bool value.
308 bool read (const String& key, bool defVal) const;
311 * @brief Read a string list from the given key with a default fallback value.
313 * If failed to read from the given key, then return the given default value.
315 * @param key - the key to be read.
316 * @param defVal - the default value to be return if failed to read.
317 * @return The result string list.
319 std::vector <String> read (const String& key, const std::vector <String>& defVal) const;
322 * @brief Read an int list from the given key with a default fallback value.
324 * If failed to read from the given key, then return the given default value.
326 * @param key - the key to be read.
327 * @param defVal - the default value to be return if failed to read.
328 * @return The result int list.
330 std::vector <int> read (const String& key, const std::vector <int>& defVal) const;
333 * @brief connect the given slot to the reload signal.
334 * @param slot - the given slot to be connected.
335 * @return the Connection object, can be used to disconnect this slot.
337 Connection signal_connect_reload (ConfigSlotVoid *slot);
343 * @brief Set the default global Config object.
345 * There is only one global Config object in an application.
346 * All other objects should use it by default.
348 * @param p_config - a smart pointer to the Config object.
349 * @return a smart pointer to the old default Config object.
351 static ConfigPointer set (const ConfigPointer &p_config);
354 * @brief Get the default global Config object.
356 * The default global Config object can be set with function ConfigBase::set.
357 * If there is no default object set, a new object can be created if needed.
359 * @param create_on_demand - if it's true then a new object will be created,
360 * if there is no one available.
361 * @param default_module - the Config module should be used to create the
362 * default Config object. If omitted, then use the
363 * default module defined in global config file.
364 * @return a smart pointer to the default global Config object.
366 static ConfigPointer get (bool create_on_demand = true,
367 const String &default_module = String (""));
371 * @brief A dummy implementation of interface class scim::ConfigBase.
373 * The read methods will just return false and the default value (if available).
374 * The write methods will do nothing.
376 class DummyConfig : public ConfigBase
382 virtual ~DummyConfig ();
384 virtual bool valid () const;
385 virtual String get_name () const;
387 virtual bool read (const String& key, String *ret) const;
388 virtual bool read (const String& key, int *ret) const;
389 virtual bool read (const String& key, double *ret) const;
390 virtual bool read (const String& key, bool *ret) const;
391 virtual bool read (const String& key, std::vector <String> *ret) const;
392 virtual bool read (const String& key, std::vector <int> *ret) const;
394 virtual bool write (const String& key, const String& value);
395 virtual bool write (const String& key, int value);
396 virtual bool write (const String& key, double value);
397 virtual bool write (const String& key, bool value);
398 virtual bool write (const String& key, const std::vector <String>& value);
399 virtual bool write (const String& key, const std::vector <int>& value);
401 virtual bool flush();
403 virtual bool erase (const String& key );
405 virtual bool reload ();
412 #endif //__SCIM_CONFIG_BASE_H
414 vi:ts=4:nowrap:ai:expandtab