From 25b9f6cae85a17313aaff9b7ff21e7e40a586f19 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 27 Feb 2018 15:24:00 +0800 Subject: use GSettings in class Config --- src/ZYConfig.cc | 46 ++++++++++------------- src/ZYConfig.h | 32 ++++++++++------ src/ZYMain.cc | 2 +- src/ZYZConfig.cc | 112 +++++++++++++++++++++++++++++-------------------------- src/ZYZConfig.h | 12 ++---- 5 files changed, 104 insertions(+), 100 deletions(-) diff --git a/src/ZYConfig.cc b/src/ZYConfig.cc index f8b965b..5467073 100644 --- a/src/ZYConfig.cc +++ b/src/ZYConfig.cc @@ -26,15 +26,11 @@ namespace ZY { -Config::Config (Bus & bus, const std::string & name) - : Object (ibus_bus_get_config (bus)), - m_section ("engine/" + name) +Config::Config (const std::string & name) + : m_schema_id (name) { + m_settings = NULL; initDefaultValues (); - g_signal_connect (get (), - "value-changed", - G_CALLBACK (valueChangedCallback), - this); } Config::~Config (void) @@ -73,15 +69,12 @@ Config::read (const gchar * name, bool defval) { GVariant *value = NULL; - if ((value = ibus_config_get_value (get (), m_section.c_str (), name)) != NULL) { + if ((value = g_settings_get_value (m_settings, name)) != NULL) { if (g_variant_classify (value) == G_VARIANT_CLASS_BOOLEAN) return g_variant_get_boolean (value); } - // write default value to config - value = g_variant_new ("b", defval); - ibus_config_set_value (get (), m_section.c_str (), name, value); - + g_warn_if_reached (); return defval; } @@ -90,15 +83,12 @@ Config::read (const gchar * name, gint defval) { GVariant *value = NULL; - if ((value = ibus_config_get_value (get (), m_section.c_str (), name)) != NULL) { + if ((value = g_settings_get_value (m_settings, name)) != NULL) { if (g_variant_classify (value) == G_VARIANT_CLASS_INT32) return g_variant_get_int32 (value); } - // write default value to config - value = g_variant_new ("i", defval); - ibus_config_set_value (get (), m_section.c_str (), name, value); - + g_warn_if_reached (); return defval; } @@ -107,20 +97,17 @@ Config::read (const gchar * name, const gchar * defval) { GVariant *value = NULL; - if ((value = ibus_config_get_value (get (), m_section.c_str (), name)) != NULL) { + if ((value = g_settings_get_value (m_settings, name)) != NULL) { if (g_variant_classify (value) == G_VARIANT_CLASS_STRING) return g_variant_get_string (value, NULL); } - // write default value to config - value = g_variant_new ("s", defval); - ibus_config_set_value (get (), m_section.c_str (), name, value); - + g_warn_if_reached (); return defval; } gboolean -Config::valueChanged (const std::string §ion, +Config::valueChanged (const std::string &schema_id, const std::string &name, GVariant *value) { @@ -128,13 +115,18 @@ Config::valueChanged (const std::string §ion, } void -Config::valueChangedCallback (IBusConfig *config, - const gchar *section, +Config::valueChangedCallback (GSettings *settings, const gchar *name, - GVariant *value, Config *self) { - self->valueChanged (section, name, value); + gchar * property = NULL; + g_object_get (settings, "schema-id", &property, NULL); + std::string schema_id (property); + g_free (property); + + GVariant * value = g_settings_get_value (settings, name); + self->valueChanged (schema_id, name, value); + g_variant_unref (value); } }; diff --git a/src/ZYConfig.h b/src/ZYConfig.h index ae28efa..13711f4 100644 --- a/src/ZYConfig.h +++ b/src/ZYConfig.h @@ -26,6 +26,7 @@ #endif #include +#include #include #include #include "ZYUtil.h" @@ -33,11 +34,9 @@ namespace ZY { -class Bus; - -class Config : public Object { +class Config { protected: - Config (Bus & bus, const std::string & name); + Config (const std::string & name); virtual ~Config (void); public: @@ -65,18 +64,18 @@ protected: void initDefaultValues (void); virtual void readDefaultValues (void); - virtual gboolean valueChanged (const std::string §ion, + virtual gboolean valueChanged (const std::string &schema_id, const std::string &name, GVariant *value); private: - static void valueChangedCallback (IBusConfig *config, - const gchar *section, + static void valueChangedCallback (GSettings *settings, const gchar *name, - GVariant *value, Config *self); protected: - std::string m_section; + GSettings * m_settings; + std::string m_schema_id; + zhuyin_option_t m_option; zhuyin_option_t m_option_mask; @@ -104,24 +103,33 @@ protected: static inline bool normalizeGVariant (GVariant *value, bool defval) { - if (value == NULL || g_variant_classify (value) != G_VARIANT_CLASS_BOOLEAN) + if (value == NULL || + g_variant_classify (value) != G_VARIANT_CLASS_BOOLEAN) { + g_warn_if_reached (); return defval; + } return g_variant_get_boolean (value); } static inline gint normalizeGVariant (GVariant *value, gint defval) { - if (value == NULL || g_variant_classify (value) != G_VARIANT_CLASS_INT32) + if (value == NULL || + g_variant_classify (value) != G_VARIANT_CLASS_INT32) { + g_warn_if_reached (); return defval; + } return g_variant_get_int32 (value); } static inline std::string normalizeGVariant (GVariant *value, const std::string &defval) { - if (value == NULL || g_variant_classify (value) != G_VARIANT_CLASS_STRING) + if (value == NULL || + g_variant_classify (value) != G_VARIANT_CLASS_STRING) { + g_warn_if_reached (); return defval; + } return g_variant_get_string (value, NULL); } diff --git a/src/ZYMain.cc b/src/ZYMain.cc index ef9956b..37986a1 100644 --- a/src/ZYMain.cc +++ b/src/ZYMain.cc @@ -90,7 +90,7 @@ start_component (void) LibZhuyinBackEnd::init (); - ZhuyinConfig::init (bus); + ZhuyinConfig::init (); g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL); diff --git a/src/ZYZConfig.cc b/src/ZYZConfig.cc index 8f8ad6c..1579fec 100644 --- a/src/ZYZConfig.cc +++ b/src/ZYZConfig.cc @@ -24,27 +24,29 @@ #include "ZYLibZhuyin.h" #include +#define USE_G_SETTINGS_LIST_CHILDREN 1 + namespace ZY { -const gchar * const CONFIG_FUZZY_ZHUYIN = "fuzzy_zhuyin"; -const gchar * const CONFIG_ORIENTATION = "lookup_table_orientation"; -const gchar * const CONFIG_PAGE_SIZE = "candidate_num"; +const gchar * const CONFIG_FUZZY_ZHUYIN = "fuzzy-zhuyin"; +const gchar * const CONFIG_ORIENTATION = "lookup-table-orientation"; +const gchar * const CONFIG_PAGE_SIZE = "candidate-num"; -const gchar * const CONFIG_INIT_CHINESE = "chinese_mode"; -const gchar * const CONFIG_INIT_FULL_WIDTH = "full_half_width"; -const gchar * const CONFIG_INIT_TRAD_CHINESE = "traditional_chinese"; -const gchar * const CONFIG_ALWAYS_INPUT_NUMBERS = "always_input_num"; -const gchar * const CONFIG_SPACE_SHOW_CANDIDATES = "space_show_candidates"; -const gchar * const CONFIG_CANDIDATES_AFTER_CURSOR = "candidates_after_cursor"; +const gchar * const CONFIG_INIT_CHINESE = "chinese-mode"; +const gchar * const CONFIG_INIT_FULL_WIDTH = "full-half-width"; +const gchar * const CONFIG_INIT_TRAD_CHINESE = "traditional-chinese"; +const gchar * const CONFIG_ALWAYS_INPUT_NUMBERS = "always-input-num"; +const gchar * const CONFIG_SPACE_SHOW_CANDIDATES = "space-show-candidates"; +const gchar * const CONFIG_CANDIDATES_AFTER_CURSOR = "candidates-after-cursor"; -const gchar * const CONFIG_KEYBOARD_LAYOUT = "keyboard_layout"; -const gchar * const CONFIG_CANDIDATE_KEYS = "candidate_keys"; +const gchar * const CONFIG_KEYBOARD_LAYOUT = "keyboard-layout"; +const gchar * const CONFIG_CANDIDATE_KEYS = "candidate-keys"; -const gchar * const CONFIG_EASY_SYMBOL = "easy_symbol"; -const gchar * const CONFIG_USER_SYMBOL = "user_symbol"; +const gchar * const CONFIG_EASY_SYMBOL = "easy-symbol"; +const gchar * const CONFIG_USER_SYMBOL = "user-symbol"; -const gchar * const CONFIG_IMPORT_DICTIONARY = "import_dictionary"; -const gchar * const CONFIG_CLEAR_USER_DATA = "clear_user_data"; +const gchar * const CONFIG_IMPORT_DICTIONARY = "import-dictionary"; +const gchar * const CONFIG_CLEAR_USER_DATA = "clear-user-data"; const zhuyin_option_t ZHUYIN_DEFAULT_OPTION = USE_TONE | @@ -54,12 +56,13 @@ const zhuyin_option_t ZHUYIN_DEFAULT_OPTION = std::unique_ptr ZhuyinConfig::m_instance; -ZhuyinConfig::ZhuyinConfig (Bus & bus) - : Config (bus, "zhuyin") +ZhuyinConfig::ZhuyinConfig () + : Config ("com.github.libzhuyin.ibus-libzhuyin") { + m_settings = g_settings_new (m_schema_id.c_str ()); initDefaultValues (); - g_signal_connect (get (), - "value-changed", + g_signal_connect (m_settings, + "changed", G_CALLBACK (valueChangedCallback), this); @@ -67,13 +70,15 @@ ZhuyinConfig::ZhuyinConfig (Bus & bus) ZhuyinConfig::~ZhuyinConfig (void) { + g_object_unref (m_settings); + m_settings = NULL; } void -ZhuyinConfig::init (Bus & bus) +ZhuyinConfig::init () { if (m_instance.get () == NULL) { - m_instance.reset (new ZhuyinConfig (bus)); + m_instance.reset (new ZhuyinConfig ()); m_instance->readDefaultValues (); } } @@ -135,16 +140,16 @@ static const struct { guint option; } fuzzy_zhuyin_options [] = { /* fuzzy pinyin */ - { "fuzzy_zhuyin_c_ch", ZHUYIN_AMB_C_CH }, - { "fuzzy_zhuyin_z_zh", ZHUYIN_AMB_Z_ZH }, - { "fuzzy_zhuyin_s_sh", ZHUYIN_AMB_S_SH }, - { "fuzzy_zhuyin_l_n", ZHUYIN_AMB_L_N }, - { "fuzzy_zhuyin_f_h", ZHUYIN_AMB_F_H }, - { "fuzzy_zhuyin_l_r", ZHUYIN_AMB_L_R }, - { "fuzzy_zhuyin_g_k", ZHUYIN_AMB_G_K }, - { "fuzzy_zhuyin_an_ang", ZHUYIN_AMB_AN_ANG }, - { "fuzzy_zhuyin_en_eng", ZHUYIN_AMB_EN_ENG }, - { "fuzzy_zhuyin_in_ing", ZHUYIN_AMB_IN_ING }, + { "fuzzy-zhuyin-c-ch", ZHUYIN_AMB_C_CH }, + { "fuzzy-zhuyin-z-zh", ZHUYIN_AMB_Z_ZH }, + { "fuzzy-zhuyin-s-sh", ZHUYIN_AMB_S_SH }, + { "fuzzy-zhuyin-l-n", ZHUYIN_AMB_L_N }, + { "fuzzy-zhuyin-f-h", ZHUYIN_AMB_F_H }, + { "fuzzy-zhuyin-l-r", ZHUYIN_AMB_L_R }, + { "fuzzy-zhuyin-g-k", ZHUYIN_AMB_G_K }, + { "fuzzy-zhuyin-an-ang", ZHUYIN_AMB_AN_ANG }, + { "fuzzy-zhuyin-en-eng", ZHUYIN_AMB_EN_ENG }, + { "fuzzy-zhuyin-in-ing", ZHUYIN_AMB_IN_ING }, }; void @@ -153,15 +158,12 @@ 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)) { + gchar **keys = g_settings_list_children (m_settings); + g_return_if_fail (keys != NULL); + + for (gchar **iter = keys; *iter != NULL; ++iter) { + gchar *name = *iter; + /* skip signals here. */ if (0 == strcmp(CONFIG_IMPORT_DICTIONARY, name)) continue; @@ -169,11 +171,12 @@ ZhuyinConfig::readDefaultValues (void) if (0 == strcmp(CONFIG_CLEAR_USER_DATA, name)) continue; - valueChanged (m_section, name, value); - g_free (name); + GVariant *value = g_settings_get_value (m_settings, name); + valueChanged (m_schema_id, name, value); g_variant_unref (value); } - g_variant_unref (values); + + g_strfreev (keys); #else /* others */ m_orientation = read (CONFIG_ORIENTATION, IBUS_ORIENTATION_VERTICAL); @@ -242,14 +245,14 @@ ZhuyinConfig::readDefaultValues (void) } gboolean -ZhuyinConfig::valueChanged (const std::string §ion, +ZhuyinConfig::valueChanged (const std::string &schema_id, const std::string &name, GVariant *value) { - if (m_section != section) + if (m_schema_id != schema_id) return FALSE; - if (Config::valueChanged (section, name, value)) + if (Config::valueChanged (schema_id, name, value)) return TRUE; /* init states */ @@ -341,18 +344,23 @@ ZhuyinConfig::valueChanged (const std::string §ion, } void -ZhuyinConfig::valueChangedCallback (IBusConfig *config, - const gchar *section, +ZhuyinConfig::valueChangedCallback (GSettings *settings, const gchar *name, - GVariant *value, ZhuyinConfig *self) { - if (self->m_section != section) + gchar * property = NULL; + g_object_get (settings, "schema-id", &property, NULL); + std::string schema_id (property); + g_free (property); + + if (self->m_schema_id != schema_id) return; - self->valueChanged (section, name, value); + GVariant * value = g_settings_get_value (settings, name); + self->valueChanged (self->m_schema_id, name, value); + g_variant_unref (value); - if (self->m_section == "engine/zhuyin") + if (self->m_schema_id == "com.github.libzhuyin.ibus-libzhuyin") LibZhuyinBackEnd::instance ().setZhuyinOptions (self); } diff --git a/src/ZYZConfig.h b/src/ZYZConfig.h index e9f9d42..37400df 100644 --- a/src/ZYZConfig.h +++ b/src/ZYZConfig.h @@ -32,15 +32,13 @@ namespace ZY { -class Bus; - class ZhuyinConfig : public Config { public: - static void init (Bus & bus); + static void init (); static ZhuyinConfig & instance (void) { return *m_instance; } protected: - ZhuyinConfig (Bus & bus); + ZhuyinConfig (); public: virtual ~ZhuyinConfig (void); @@ -49,14 +47,12 @@ protected: void initDefaultValues (void); virtual void readDefaultValues (void); - virtual gboolean valueChanged (const std::string §ion, + virtual gboolean valueChanged (const std::string &schema_id, const std::string &name, GVariant *value); private: - static void valueChangedCallback (IBusConfig *config, - const gchar *section, + static void valueChangedCallback (GSettings *settings, const gchar *name, - GVariant *value, ZhuyinConfig *self); private: -- cgit