2 * Copyright (c) 2009 Kov Chai <tchaikov@gmail.com>
4 * The contents of this file are subject to the terms of either the GNU Lesser
5 * General Public License Version 2.1 only ("LGPL") or the Common Development and
6 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7 * file except in compliance with the License. You can obtain a copy of the CDDL at
8 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10 * specific language governing permissions and limitations under the License. When
11 * distributing the software, include this License Header Notice in each file and
12 * include the full text of the License in the License file as well as the
15 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
17 * For Covered Software in this distribution, this License shall be governed by the
18 * laws of the State of California (excluding conflict-of-law provisions).
19 * Any litigation relating to this License shall be subject to the jurisdiction of
20 * the Federal Courts of the Northern District of California and the state courts
21 * of the State of California, with venue lying in Santa Clara County, California.
25 * If you wish your version of this file to be governed by only the CDDL or only
26 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28 * license." If you don't indicate a single choice of license, a recipient has the
29 * option to distribute your version of this file under either the CDDL or the LGPL
30 * Version 2.1, or to extend the choice of license to its licensees as provided
31 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32 * Version 2 license, then the option applies only if the new code is made subject
33 * to such option by the copyright holder.
37 #include <sunpinyin.h>
38 #include "sunpinyin_config_keys.h"
39 #include "sunpinyin_config.h"
47 ConfigItem(const string& key)
49 section = "engine/SunPinyin/";
50 size_t pos = key.rfind('/');
51 if (pos != key.npos) {
52 section += key.substr(0, pos);
57 name = key.substr(pos);
61 IBusConfig* SunPinyinConfig::m_config;
63 SunPinyinConfig::SunPinyinConfig()
65 m_scheme_names["QuanPin"] = CSunpinyinSessionFactory::QUANPIN;
66 m_scheme_names["ShuangPin"] = CSunpinyinSessionFactory::SHUANGPIN;
69 SunPinyinConfig::~SunPinyinConfig()
73 SunPinyinConfig::set_config(IBusConfig* config)
80 CSunpinyinSessionFactory::EPyScheme
81 SunPinyinConfig::get_py_scheme(CSunpinyinSessionFactory::EPyScheme scheme)
84 get_scheme_name(scheme);
85 string name = get(PINYIN_SCHEME, default_name);
86 return get_scheme(name);
90 SunPinyinConfig::is_initial_mode_cn()
92 string init_mode("Chinese");
93 init_mode = get(CONFIG_GENERAL_INITIAL_MODE, init_mode);
94 return (init_mode == "Chinese");
98 SunPinyinConfig::is_initial_punct_full()
100 string init_punct("Full");
101 init_punct = get(CONFIG_GENERAL_INITIAL_PUNCT, init_punct);
102 return (init_punct == "Full");
106 SunPinyinConfig::is_initial_letter_full()
108 string init_letter("Half");
109 init_letter = get(CONFIG_GENERAL_INITIAL_LETTER, init_letter);
110 return (init_letter == "Full");
114 SunPinyinConfig::listen_on_changed()
116 assert(m_config != NULL);
117 g_signal_connect(m_config, "value-changed",
118 G_CALLBACK(on_config_value_changed), 0);
122 SunPinyinConfig::get_scheme_name(CSunpinyinSessionFactory::EPyScheme scheme)
124 string val = "ShuangPin";
125 for (SchemeNames::iterator it = m_scheme_names.begin();
126 it != m_scheme_names.end(); ++it) {
127 if (it->second == scheme)
133 CSunpinyinSessionFactory::EPyScheme
134 SunPinyinConfig::get_scheme(const std::string& name)
136 CSunpinyinSessionFactory::EPyScheme val = CSunpinyinSessionFactory::SHUANGPIN;
137 SchemeNames::iterator it = m_scheme_names.find(name);
138 if (it != m_scheme_names.end()) {
144 #if IBUS_CHECK_VERSION (1, 3, 99)
145 static vector<string> get_strings_from_gvariant(GVariant *value);
148 SunPinyinConfig::get(const char* key, bool val)
150 assert(m_config != NULL);
152 ConfigItem item(key);
153 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
155 if (g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) {
156 result = (g_variant_get_boolean(value) == TRUE);
162 SunPinyinConfig::set(const char* key, bool val)
164 assert(m_config != NULL);
166 GVariant * value = g_variant_new_boolean(val);
167 ConfigItem item(key);
168 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), value);
172 SunPinyinConfig::get(const char* key, const std::string& val)
174 assert(m_config != NULL);
176 ConfigItem item(key);
177 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
179 if (g_variant_classify(value) == G_VARIANT_CLASS_STRING) {
180 result = string(g_variant_get_string(value, NULL));
186 SunPinyinConfig::set(const char* key, const std::string& val)
188 assert(m_config != NULL);
190 GVariant* value = g_variant_new_string(val.c_str());
191 ConfigItem item(key);
192 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), value);
196 SunPinyinConfig::get(const char* key, int val)
198 assert(m_config != NULL);
200 ConfigItem item(key);
201 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
203 if (g_variant_classify(value) == G_VARIANT_CLASS_INT32) {
204 result = g_variant_get_int32(value);
210 SunPinyinConfig::set(const char* key, int val)
212 assert(m_config != NULL);
214 GVariant* value = g_variant_new_int32(val);
215 ConfigItem item(key);
216 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), value);
219 std::vector<std::string>
220 SunPinyinConfig::get(const char* key, const std::vector<std::string>& val)
222 assert(m_config != NULL);
224 ConfigItem item(key);
225 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
226 vector<string> result(val);
227 if (g_variant_classify(value) == G_VARIANT_CLASS_ARRAY) {
228 result = get_strings_from_gvariant(value);
234 get_strings_from_gvariant(GVariant *value)
236 const gchar **array = g_variant_get_strv(value, NULL);
237 assert(array != NULL);
238 vector<string> strings;
239 for (unsigned i = 0; array[i]; ++i) {
240 strings.push_back(array[i]);
246 g_value_to_event(const gchar *section, const gchar *name, GVariant *value)
250 if (strlen(section) == 0) {
253 event_name = string(section) + "/" + string(name);
256 switch (g_variant_classify(value)) {
257 case G_VARIANT_CLASS_INT32:
258 return COptionEvent(event_name, g_variant_get_int32(value));
259 case G_VARIANT_CLASS_STRING:
260 return COptionEvent(event_name, g_variant_get_string(value, NULL));
261 case G_VARIANT_CLASS_BOOLEAN:
262 return COptionEvent(event_name,
263 g_variant_get_boolean(value)?true:false);
265 // G_TYPE_VALUE_ARRAY() not a constant
266 if (G_VARIANT_CLASS_ARRAY == g_variant_classify(value))
267 return COptionEvent(event_name, get_strings_from_gvariant(value));
268 assert(false && "unknown gvalue");
269 return COptionEvent(event_name, 0);
274 SunPinyinConfig::on_config_value_changed(IBusConfig *config,
275 const gchar *section,
278 SunPinyinConfig* thiz)
280 static const char* prefix = "engine/SunPinyin/";
281 if (!strstr(section, prefix))
283 const char *sub_section = section + strlen(prefix);
284 COptionEvent event = g_value_to_event(sub_section, name, value);
285 AOptionEventBus::instance().publishEvent(event);
288 #else /* for versions lower than 1.3.99 (or 1.4) */
290 static vector<string> get_strings_from_gvalue(GValue* value);
293 SunPinyinConfig::get(const char* key, bool val)
295 assert(m_config != NULL);
299 ConfigItem item(key);
300 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
302 if (got && G_VALUE_TYPE(&v) == G_TYPE_BOOLEAN) {
303 result = (g_value_get_boolean(&v) == TRUE);
309 SunPinyinConfig::set(const char* key, bool value)
311 assert(m_config != NULL);
314 g_value_init(&v, G_TYPE_BOOLEAN);
315 g_value_set_boolean(&v, value?TRUE:FALSE);
316 ConfigItem item(key);
317 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), &v);
321 SunPinyinConfig::get(const char* key, const std::string& val)
323 assert(m_config != NULL);
327 ConfigItem item(key);
328 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
330 if (got && G_VALUE_TYPE(&v) == G_TYPE_STRING) {
331 result = string(g_value_get_string(&v));
337 SunPinyinConfig::set(const char* key, const std::string& val)
339 assert(m_config != NULL);
342 g_value_init(&v, G_TYPE_STRING);
343 g_value_set_string(&v, val.c_str());
344 ConfigItem item(key);
345 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), &v);
349 SunPinyinConfig::get(const char* key, int val)
351 assert(m_config != NULL);
355 ConfigItem item(key);
356 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
358 if (got && G_VALUE_TYPE(&v) == G_TYPE_INT) {
359 result = g_value_get_int(&v);
365 SunPinyinConfig::set(const char* key, int value)
367 assert(m_config != NULL);
370 g_value_init(&v, G_TYPE_INT);
371 g_value_set_int(&v, value);
372 ConfigItem item(key);
373 ibus_config_set_value(m_config, item.section.c_str(), item.name.c_str(), &v);
376 std::vector<std::string>
377 SunPinyinConfig::get(const char *key, const std::vector<std::string>& val)
379 assert(m_config != NULL);
383 ConfigItem item(key);
384 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
385 vector<string> result(val);
386 if (got && G_VALUE_TYPE(&v) == G_TYPE_VALUE_ARRAY) {
387 result = get_strings_from_gvalue(&v);
393 get_strings_from_gvalue(GValue *value)
395 GValueArray *array = (GValueArray *)g_value_get_boxed(value);
396 assert(array != NULL);
397 vector<string> strings;
398 for (unsigned i = 0; i < array->n_values; ++i) {
399 GValue *element = &(array->values[i]);
400 assert (G_VALUE_TYPE(element) == G_TYPE_STRING && "only array of string is supported");
401 strings.push_back(g_value_get_string(element));
407 g_value_to_event(const gchar *section, const gchar *name, GValue *value)
411 if (strlen(section) == 0) {
414 event_name = string(section) + "/" + string(name);
417 switch (G_VALUE_TYPE(value)) {
419 return COptionEvent(event_name, g_value_get_int(value));
421 return COptionEvent(event_name, g_value_get_string(value));
423 return COptionEvent(event_name,
424 g_value_get_boolean(value)?true:false);
426 // G_TYPE_VALUE_ARRAY() not a constant
427 if (G_TYPE_VALUE_ARRAY == G_VALUE_TYPE(value))
428 return COptionEvent(event_name, get_strings_from_gvalue(value));
429 assert(false && "unknown gvalue");
430 return COptionEvent(event_name, 0);
435 SunPinyinConfig::on_config_value_changed(IBusConfig *config,
436 const gchar *section,
439 SunPinyinConfig* thiz)
441 static const char* prefix = "engine/SunPinyin/";
442 if (!strstr(section, prefix))
444 const char *sub_section = section + strlen(prefix);
445 COptionEvent event = g_value_to_event(sub_section, name, value);
446 AOptionEventBus::instance().publishEvent(event);