From 93b8c269303109a0bbcd43152d1eb7354af13ffb Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 10 Apr 2014 15:20:57 +0800 Subject: write class ZhuyinConfig --- src/ZYConfig.cc | 2 +- src/ZYConfig.h | 4 +- src/ZYZConfig.cc | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 138 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ZYConfig.cc b/src/ZYConfig.cc index e8a6c64..5e6cf32 100644 --- a/src/ZYConfig.cc +++ b/src/ZYConfig.cc @@ -46,7 +46,7 @@ Config::initDefaultValues (void) { m_page_size = 5; - m_zhuyin_schema = 0; + m_keyboard_layout = 0; m_init_chinese = TRUE; m_init_full_english = FALSE; diff --git a/src/ZYConfig.h b/src/ZYConfig.h index a69a6e3..87613ed 100644 --- a/src/ZYConfig.h +++ b/src/ZYConfig.h @@ -44,7 +44,7 @@ public: zhuyin_option_t option (void) const { return m_option & m_option_mask; } guint orientation (void) const { return m_orientation; } guint pageSize (void) const { return m_page_size; } - gint zhuyinSchema (void) const { return m_zhuyin_schema; } + gint keyboardLayout (void) const { return m_keyboard_layout; } gboolean initChinese (void) const { return m_init_chinese; } gboolean initFullEnglish (void) const { return m_init_full_english; } gboolean initFullPunct (void) const { return m_init_full_punct; } @@ -76,7 +76,7 @@ protected: gint m_orientation; guint m_page_size; - gint m_zhuyin_schema; + gint m_keyboard_layout; gboolean m_init_chinese; gboolean m_init_full_english; diff --git a/src/ZYZConfig.cc b/src/ZYZConfig.cc index 8e34d0f..5725d9e 100644 --- a/src/ZYZConfig.cc +++ b/src/ZYZConfig.cc @@ -58,6 +58,15 @@ ZhuyinConfig::~ZhuyinConfig (void) { } +void +ZhuyinConfig::init (Bus & bus) +{ + if (m_instance.get () == NULL) { + m_instance.reset (new ZhuyinConfig (bus)); + m_instance->readDefaultValues (); + } +} + void ZhuyinConfig::initDefaultValues (void) { @@ -67,7 +76,7 @@ ZhuyinConfig::initDefaultValues (void) m_orientation = IBUS_ORIENTATION_VERTICAL; m_page_size = 10; - m_zhuyin_schema = 0; + m_keyboard_layout = 0; m_init_chinese = TRUE; m_init_full_english = FALSE; @@ -94,5 +103,130 @@ static const struct { { "fuzzyzhuyin_in_ing", ZHUYIN_AMB_IN_ING }, }; +void +ZhuyinConfig::readDefaultValues (void) +{ +#if defined(HAVE_IBUS_CONFIG_GET_VALUES) + /* read all values together */ + initDefaultValues (); + GVariant *values = + ibus_config_get_values (get (), m_section.c_str ()); + g_return_if_fail (values != NULL); + + GVariantIter iter; + gchar *name; + GVariant *value; + g_variant_iter_init (&iter, values); + while (g_variant_iter_next (&iter, "{sv}", &name, &value)) { + valueChanged (m_section, name, value); + g_free (name); + g_variant_unref (value); + } + g_variant_unref (values); +#else + /* others */ + m_orientation = read (CONFIG_ORIENTATION, IBUS_ORIENTATION_VERTICAL); + if (m_orientation != IBUS_ORIENTATION_VERTICAL && + m_orientation != IBUS_ORIENTATION_HORIZONTAL) { + m_orientation = IBUS_ORIENTATION_VERTICAL; + g_warn_if_reached (); + } + m_page_size = read (CONFIG_PAGE_SIZE, 10); + if (m_page_size > 10) { + m_page_size = 10; + g_warn_if_reached (); + } + + m_keyboard_layout = read (CONFIG_KEYBOARD_LAYOUT, 0); + + /* init states */ + m_init_chinese = read (CONFIG_INIT_CHINESE, true); + m_init_full_english = read (CONFIG_INIT_FULL_ENGLISH, false); + m_init_full_punct = read (CONFIG_INIT_FULL_PUNCT, true); + m_init_trad_chinese = read (CONFIG_INIT_TRAD_CHINESE, true); + + m_candidate_keys = read (CONFIG_CANDIDATE_KEYS, "1234567890"); + + /* fuzzy zhuyin */ + if (read (CONFIG_FUZZY_ZHUYIN, false)) + m_option_mask |= ZHUYIN_AMB_ALL; + else + m_option_mask &= ~ZHUYIN_AMB_ALL; + + /* read values */ + for (guint i = 0; i < G_N_ELEMENTS (options); i++) { + if (read (options[i].name, + (options[i].option & PINYIN_DEFAULT_OPTION) != 0)) { + m_option |= options[i].option; + } + else { + m_option &= ~options[i].option; + } + } +#endif +} + +gboolean +ZhuyinConfig::valueChanged (const std::string §ion, + const std::string &name, + GVariant *value) +{ + if (m_section != section) + return FALSE; + + if (Config::valueChanged (section, name, value)) + return TRUE; + + /* init states */ + if (CONFIG_INIT_CHINESE == name) + m_init_chinese = normalizeGVariant (value, true); + else if (CONFIG_INIT_FULL_ENGLISH == name) + m_init_full_english = normalizeGVariant (value, true); + else if (CONFIG_INIT_FULL_PUNCT == name) + m_init_full_punct = normalizeGVariant (value, true); + else if (CONFIG_INIT_TRAD_CHINESE == name) + m_init_trad_chinese = normalizeGVariant (value, false); + else if (CONFIG_KEYBOARD_LAYOUT == name) + m_keyboard_layout = normalizeGVariant (value, 0); + else if (CONFIG_CANDIDATE_KEYS == name) { + m_candidate_keys = normalizeGVariant (value, "1234567890"); + } /* lookup table page size */ + else if (CONFIG_ORIENTATION == name) { + m_orientation = normalizeGVariant (value, IBUS_ORIENTATION_VERTICAL); + if (m_orientation != IBUS_ORIENTATION_VERTICAL && + m_orientation != IBUS_ORIENTATION_HORIZONTAL) { + m_orientation = IBUS_ORIENTATION_VERTICAL; + g_warn_if_reached (); + } + } + else if (CONFIG_PAGE_SIZE == name) { + m_page_size = normalizeGVariant (value, 10); + if (m_page_size > 10) { + m_page_size = 10; + g_warn_if_reached (); + } + } /* fuzzy zhuyin */ + else if (CONFIG_FUZZY_ZHUYIN == name) { + if (normalizeGVariant (value, false)) + m_option_mask |= ZHUYIN_AMB_ALL; + else + m_option_mask &= ~ZHUYIN_AMB_ALL; + } + else { + for (guint i = 0; i < G_N_ELEMENTS (options); i++) { + if (G_LIKELY (options[i].name != name)) + continue; + if (normalizeGVariant (value, + (options[i].option & ZHUYIN_DEFAULT_OPTION) != 0)) + m_option |= options[i].option; + else + m_option &= ~options[i].option; + return TRUE; + } + return FALSE; + } + return TRUE; + +} }; -- cgit