Debian Codegraph @

src/sunpinyin_config.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2009 Kov Chai <[email protected]>
3 *
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
13 * following notice:
14 *
15 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16 * (CDDL)
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.
22 *
23 * Contributor(s):
24 *
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.
34 */
35
36 #include <cassert>
37 #include <sunpinyin.h>
38 #include "sunpinyin_config_keys.h"
39 #include "sunpinyin_config.h"
40
41 using namespace std;
42
43 struct ConfigItem
44 {
45 string section;
46 string name;
47 ConfigItem(const string& key)
48 {
49 section = "engine/SunPinyin/";
50 size_t pos = key.rfind('/');
51 if (pos != key.npos) {
52 section += key.substr(0, pos);
53 pos += 1;
54 } else {
55 pos = 0;
56 }
57 name = key.substr(pos);
58 }
59 };
60
61 IBusConfig* SunPinyinConfig::m_config;
62
63 SunPinyinConfig::SunPinyinConfig()
64 {
65 m_scheme_names["QuanPin"] = CSunpinyinSessionFactory::QUANPIN;
66 m_scheme_names["ShuangPin"] = CSunpinyinSessionFactory::SHUANGPIN;
67 }
68
69 SunPinyinConfig::~SunPinyinConfig()
70 {}
71
72 void
73 SunPinyinConfig::set_config(IBusConfig* config)
74 {
75 assert(config);
76 m_config = config;
77 listen_on_changed();
78 }
79
80 CSunpinyinSessionFactory::EPyScheme
81 SunPinyinConfig::get_py_scheme(CSunpinyinSessionFactory::EPyScheme scheme)
82 {
83 string default_name =
84 get_scheme_name(scheme);
85 string name = get(PINYIN_SCHEME, default_name);
86 return get_scheme(name);
87 }
88
89 bool
90 SunPinyinConfig::is_initial_mode_cn()
91 {
92 string init_mode("Chinese");
93 init_mode = get(CONFIG_GENERAL_INITIAL_MODE, init_mode);
94 return (init_mode == "Chinese");
95 }
96
97 bool
98 SunPinyinConfig::is_initial_punct_full()
99 {
100 string init_punct("Full");
101 init_punct = get(CONFIG_GENERAL_INITIAL_PUNCT, init_punct);
102 return (init_punct == "Full");
103 }
104
105 bool
106 SunPinyinConfig::is_initial_letter_full()
107 {
108 string init_letter("Half");
109 init_letter = get(CONFIG_GENERAL_INITIAL_LETTER, init_letter);
110 return (init_letter == "Full");
111 }
112
113 void
114 SunPinyinConfig::listen_on_changed()
115 {
116 assert(m_config != NULL);
117 g_signal_connect(m_config, "value-changed",
118 G_CALLBACK(on_config_value_changed), 0);
119 }
120
121 std::string
122 SunPinyinConfig::get_scheme_name(CSunpinyinSessionFactory::EPyScheme scheme)
123 {
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)
128 val = it->first;
129 }
130 return val;
131 }
132
133 CSunpinyinSessionFactory::EPyScheme
134 SunPinyinConfig::get_scheme(const std::string& name)
135 {
136 CSunpinyinSessionFactory::EPyScheme val = CSunpinyinSessionFactory::SHUANGPIN;
137 SchemeNames::iterator it = m_scheme_names.find(name);
138 if (it != m_scheme_names.end()) {
139 val = it->second;
140 }
141 return val;
142 }
143
144 #if IBUS_CHECK_VERSION (1, 3, 99)
145 static vector<string> get_strings_from_gvariant(GVariant *value);
146
147 bool
148 SunPinyinConfig::get(const char* key, bool val)
149 {
150 assert(m_config != NULL);
151
152 ConfigItem item(key);
153 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
154 bool result = val;
155 if (g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) {
156 result = (g_variant_get_boolean(value) == TRUE);
157 }
158 return result;
159 }
160
161 void
162 SunPinyinConfig::set(const char* key, bool val)
163 {
164 assert(m_config != NULL);
165
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);
169 }
170
171 std::string
172 SunPinyinConfig::get(const char* key, const std::string& val)
173 {
174 assert(m_config != NULL);
175
176 ConfigItem item(key);
177 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
178 string result = val;
179 if (g_variant_classify(value) == G_VARIANT_CLASS_STRING) {
180 result = string(g_variant_get_string(value, NULL));
181 }
182 return result;
183 }
184
185 void
186 SunPinyinConfig::set(const char* key, const std::string& val)
187 {
188 assert(m_config != NULL);
189
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);
193 }
194
195 int
196 SunPinyinConfig::get(const char* key, int val)
197 {
198 assert(m_config != NULL);
199
200 ConfigItem item(key);
201 GVariant* value = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str());
202 int result = val;
203 if (g_variant_classify(value) == G_VARIANT_CLASS_INT32) {
204 result = g_variant_get_int32(value);
205 }
206 return result;
207 }
208
209 void
210 SunPinyinConfig::set(const char* key, int val)
211 {
212 assert(m_config != NULL);
213
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);
217 }
218
219 std::vector<std::string>
220 SunPinyinConfig::get(const char* key, const std::vector<std::string>& val)
221 {
222 assert(m_config != NULL);
223
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);
229 }
230 return result;
231 }
232
233 vector<string>
234 get_strings_from_gvariant(GVariant *value)
235 {
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]);
241 }
242 return strings;
243 }
244
245 static COptionEvent
246 g_value_to_event(const gchar *section, const gchar *name, GVariant *value)
247 {
248 string event_name;
249
250 if (strlen(section) == 0) {
251 event_name = name;
252 } else {
253 event_name = string(section) + "/" + string(name);
254 }
255
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);
264 default:
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);
270 }
271 }
272
273 void
274 SunPinyinConfig::on_config_value_changed(IBusConfig *config,
275 const gchar *section,
276 const gchar *name,
277 GVariant *value,
278 SunPinyinConfig* thiz)
279 {
280 static const char* prefix = "engine/SunPinyin/";
281 if (!strstr(section, prefix))
282 return;
283 const char *sub_section = section + strlen(prefix);
284 COptionEvent event = g_value_to_event(sub_section, name, value);
285 AOptionEventBus::instance().publishEvent(event);
286 }
287
288 #else /* for versions lower than 1.3.99 (or 1.4) */
289
290 static vector<string> get_strings_from_gvalue(GValue* value);
291
292 bool
293 SunPinyinConfig::get(const char* key, bool val)
294 {
295 assert(m_config != NULL);
296
297 GValue v = {0};
298 gboolean got;
299 ConfigItem item(key);
300 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
301 bool result = val;
302 if (got && G_VALUE_TYPE(&v) == G_TYPE_BOOLEAN) {
303 result = (g_value_get_boolean(&v) == TRUE);
304 }
305 return result;
306 }
307
308 void
309 SunPinyinConfig::set(const char* key, bool value)
310 {
311 assert(m_config != NULL);
312
313 GValue v = {0};
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);
318 }
319
320 std::string
321 SunPinyinConfig::get(const char* key, const std::string& val)
322 {
323 assert(m_config != NULL);
324
325 GValue v = {0};
326 gboolean got;
327 ConfigItem item(key);
328 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
329 string result = val;
330 if (got && G_VALUE_TYPE(&v) == G_TYPE_STRING) {
331 result = string(g_value_get_string(&v));
332 }
333 return result;
334 }
335
336 void
337 SunPinyinConfig::set(const char* key, const std::string& val)
338 {
339 assert(m_config != NULL);
340
341 GValue v = {0};
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);
346 }
347
348 int
349 SunPinyinConfig::get(const char* key, int val)
350 {
351 assert(m_config != NULL);
352
353 GValue v = {0};
354 gboolean got;
355 ConfigItem item(key);
356 got = ibus_config_get_value(m_config, item.section.c_str(), item.name.c_str(), &v);
357 int result = val;
358 if (got && G_VALUE_TYPE(&v) == G_TYPE_INT) {
359 result = g_value_get_int(&v);
360 }
361 return result;
362 }
363
364 void
365 SunPinyinConfig::set(const char* key, int value)
366 {
367 assert(m_config != NULL);
368
369 GValue v = {0};
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);
374 }
375
376 std::vector<std::string>
377 SunPinyinConfig::get(const char *key, const std::vector<std::string>& val)
378 {
379 assert(m_config != NULL);
380
381 GValue v = {0};
382 gboolean got;
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);
388 }
389 return result;
390 }
391
392 vector<string>
393 get_strings_from_gvalue(GValue *value)
394 {
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));
402 }
403 return strings;
404 }
405
406 static COptionEvent
407 g_value_to_event(const gchar *section, const gchar *name, GValue *value)
408 {
409 string event_name;
410
411 if (strlen(section) == 0) {
412 event_name = name;
413 } else {
414 event_name = string(section) + "/" + string(name);
415 }
416
417 switch (G_VALUE_TYPE(value)) {
418 case G_TYPE_INT:
419 return COptionEvent(event_name, g_value_get_int(value));
420 case G_TYPE_STRING:
421 return COptionEvent(event_name, g_value_get_string(value));
422 case G_TYPE_BOOLEAN:
423 return COptionEvent(event_name,
424 g_value_get_boolean(value)?true:false);
425 default:
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);
431 }
432 }
433
434 void
435 SunPinyinConfig::on_config_value_changed(IBusConfig *config,
436 const gchar *section,
437 const gchar *name,
438 GValue *value,
439 SunPinyinConfig* thiz)
440 {
441 static const char* prefix = "engine/SunPinyin/";
442 if (!strstr(section, prefix))
443 return;
444 const char *sub_section = section + strlen(prefix);
445 COptionEvent event = g_value_to_event(sub_section, name, value);
446 AOptionEventBus::instance().publishEvent(event);
447 }
448 #endif
449
450 // -*- indent-tabs-mode: nil -*- vim:et:ts=4