diff options
author | Eduardo Lima (Etrunko) <eduardo.lima@intel.com> | 2013-09-12 16:35:22 -0300 |
---|---|---|
committer | Eduardo Lima (Etrunko) <eduardo.lima@intel.com> | 2013-09-19 19:45:26 -0300 |
commit | 9892ad0f9423bacce14839992e344ebe58f41982 (patch) | |
tree | 814710fefefb4a361721c37f449a520f2db39d6f | |
parent | 0929dc5e622d26e0930492e1fc726221b5a0146d (diff) | |
download | weekeyboard-9892ad0f9423bacce14839992e344ebe58f41982.tar.gz weekeyboard-9892ad0f9423bacce14839992e344ebe58f41982.tar.bz2 weekeyboard-9892ad0f9423bacce14839992e344ebe58f41982.zip |
Introduce struct _config_key
With this new struct we simplify the config_section implementation
and remove from it the responsibility for setting/getting values.
Signed-off-by: Eduardo Lima (Etrunko) <eduardo.lima@intel.com>
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/wkb-ibus-config-eet.c | 309 | ||||
-rw-r--r-- | src/wkb-ibus-config-key.c | 210 | ||||
-rw-r--r-- | src/wkb-ibus-config-key.h | 34 |
4 files changed, 342 insertions, 215 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5d53bff..2d30b50 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,6 +25,8 @@ noinst_PROGRAMS = \ weekeyboard-config-eet-test weekeyboard_config_eet_test_SOURCES = \ + wkb-ibus-config-key.c \ + wkb-ibus-config-key.h \ wkb-ibus-config-eet.c \ wkb-ibus-config-eet.h \ wkb-ibus-config-eet-test.c @@ -37,6 +39,8 @@ weekeyboard_ibus_test_SOURCES = \ wkb-ibus.c \ wkb-ibus-panel.c \ wkb-ibus-config.c \ + wkb-ibus-config-key.c \ + wkb-ibus-config-key.h \ wkb-ibus-config-eet.c \ wkb-ibus-config-eet.h \ wkb-ibus-test.c diff --git a/src/wkb-ibus-config-eet.c b/src/wkb-ibus-config-eet.c index 529586b..cbed045 100644 --- a/src/wkb-ibus-config-eet.c +++ b/src/wkb-ibus-config-eet.c @@ -10,7 +10,7 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the eetific language governing permissions and + * See the License for the specific language governing permissions and * limitations under the License. */ @@ -18,11 +18,14 @@ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include <string.h> #include <Eina.h> #include <Eet.h> +#include <Eldbus.h> #include "wkb-ibus-config-eet.h" +#include "wkb-ibus-config-key.h" /* * Base struct for all config types @@ -30,42 +33,30 @@ struct _config_section { const char *id; + Eina_List *keys; Eina_List *subsections; - void (*free)(struct _config_section *); void (*set_defaults)(struct _config_section *); - Eina_Bool (*set_value)(struct _config_section *, const char *, const char *, Eldbus_Message_Iter *); - void *(*get_value)(struct _config_section *, const char *, const char *); - void *(*get_values)(struct _config_section *, const char *); }; -#define _config_section_init(_id) \ - do { \ - base->id = eina_stringshare_add(#_id); \ - base->free = _config_ ## _id ## _free; \ - base->set_defaults = _config_ ## _id ## _set_defaults; \ - base->set_value = _config_ ## _id ## _set_value; \ - base->get_value = _config_ ## _id ## _get_value; \ - base->get_values = _config_ ## _id ## _get_values; \ - if (parent) \ - parent->subsections = eina_list_append(parent->subsections, base); \ - } while (0) - static void _config_section_free(struct _config_section *base) { + struct wkb_config_key *key; struct _config_section *sub; eina_stringshare_del(base->id); + EINA_LIST_FREE(base->keys, key) + wkb_config_key_free(key); + + eina_list_free(base->keys); + EINA_LIST_FREE(base->subsections, sub) _config_section_free(sub); eina_list_free(base->subsections); - if (base->free) - base->free(base); - free(base); } @@ -87,44 +78,48 @@ _config_section_set_defaults(struct _config_section *base) static Eina_Bool _config_section_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) { - if (!base->set_value) - return EINA_FALSE; - - return base->set_value(base, section, name, value); } static void * _config_section_get_value(struct _config_section *base, const char *section, const char *name) { - if (!base->get_value) - return NULL; - - return base->get_value(base, section, name); } static void * _config_section_get_values(struct _config_section *base, const char *section) { - if (!base->get_values) - return NULL; - - return base->get_values(base, section); } -/* - * Helpers for manipulating list of strings - */ -static void -_config_string_list_free(Eina_List *list) -{ - const char *str; +#define _config_section_init(_section, _id) \ + do { \ + _section->id = eina_stringshare_add(#_id); \ + _section->set_defaults = _config_ ## _id ## _set_defaults; \ + if (parent) \ + parent->subsections = eina_list_append(parent->subsections, _section); \ + } while (0) - EINA_LIST_FREE(list, str) - eina_stringshare_del(str); +#define _config_section_add_key(_section, _section_id, _key_type, _field) \ + do { \ + struct _config_ ## _section_id *__conf = (struct _config_ ## _section_id *) _section; \ + struct wkb_config_key *__key = wkb_config_key_ ## _key_type(#_field, &__conf->_field); \ + _section->keys = eina_list_append(_section->keys, __key); \ + } while (0) - eina_list_free(list); -} +#define _config_section_add_key_int(_section, _section_id, _field) \ + _config_section_add_key(_section, _section_id, int, _field) +#define _config_section_add_key_bool(_section, _section_id, _field) \ + _config_section_add_key(_section, _section_id, bool, _field) + +#define _config_section_add_key_string(_section, _section_id, _field) \ + _config_section_add_key(_section, _section_id, string, _field) + +#define _config_section_add_key_string_list(_section, _section_id, _field) \ + _config_section_add_key(_section, _section_id, string_list, _field) + +/* + * Helpers + */ static Eina_List * _config_string_list_new(const char **strs) { @@ -137,6 +132,22 @@ _config_string_list_new(const char **strs) return list; } +static char * +_config_string_sanitize(const char *str) +{ + char *s, *sane = strdup(str); + + for (s = sane; *s; s++) + { + if (*s == '-') + *s = '_'; + else if (*s >= 'A' && *s <= 'Z') + *s += ('a' - 'A'); + } + + return sane; +} + /* * <schema path="/desktop/ibus/general/hotkey/" id="org.freedesktop.ibus.general.hotkey"> * <key type="as" name="trigger"> @@ -241,42 +252,17 @@ _config_hotkey_set_defaults(struct _config_section *base) } static void -_config_hotkey_free(struct _config_section *base) -{ - struct _config_hotkey *hotkey = (struct _config_hotkey *) base; - - _config_string_list_free(hotkey->trigger); - _config_string_list_free(hotkey->triggers); - _config_string_list_free(hotkey->enable_unconditional); - _config_string_list_free(hotkey->disable_unconditional); - _config_string_list_free(hotkey->next_engine); - _config_string_list_free(hotkey->next_engine_in_menu); - _config_string_list_free(hotkey->prev_engine); - _config_string_list_free(hotkey->previous_engine); -} - -static Eina_Bool -_config_hotkey_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_hotkey_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_hotkey_get_values(struct _config_section *base, const char *section) -{ - return NULL; -} - -static void _config_hotkey_section_init(struct _config_section *base, struct _config_section *parent) { - _config_section_init(hotkey); + _config_section_init(base, hotkey); + _config_section_add_key_string_list(base, hotkey, trigger); + _config_section_add_key_string_list(base, hotkey, triggers); + _config_section_add_key_string_list(base, hotkey, enable_unconditional); + _config_section_add_key_string_list(base, hotkey, disable_unconditional); + _config_section_add_key_string_list(base, hotkey, next_engine); + _config_section_add_key_string_list(base, hotkey, next_engine_in_menu); + _config_section_add_key_string_list(base, hotkey, prev_engine); + _config_section_add_key_string_list(base, hotkey, previous_engine); } static struct _config_section * @@ -400,37 +386,6 @@ _config_general_set_defaults(struct _config_section *base) general->use_global_engine = EINA_FALSE; general->enable_by_default = EINA_FALSE; general->dconf_preserve_name_prefixes = _config_string_list_new(dconf_preserve_name_prefixes); - -} - -static void -_config_general_free(struct _config_section *base) -{ - struct _config_general *general = (struct _config_general *) base; - - _config_string_list_free(general->preload_engines); - _config_string_list_free(general->engines_order); - _config_string_list_free(general->dconf_preserve_name_prefixes); - - eina_stringshare_del(general->version); -} - -static Eina_Bool -_config_general_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_general_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_general_get_values(struct _config_section *base, const char *section) -{ - return NULL; } static void @@ -438,10 +393,20 @@ _config_general_section_init(struct _config_section *base, struct _config_sectio { struct _config_general *conf = (struct _config_general *) base; - _config_section_init(general); - if (conf->hotkey) _config_hotkey_section_init(conf->hotkey, base); + + _config_section_init(base, general); + _config_section_add_key_string_list(base, general, preload_engines); + _config_section_add_key_string_list(base, general, engines_order); + _config_section_add_key_int(base, general, switcher_delay_time); + _config_section_add_key_string(base, general, version); + _config_section_add_key_bool(base, general, use_system_keyboard_layout); + _config_section_add_key_bool(base, general, embed_preedit_text); + _config_section_add_key_bool(base, general, use_global_engine); + _config_section_add_key_bool(base, general, enable_by_default); + _config_section_add_key_string_list(base, general, dconf_preserve_name_prefixes); + } static struct _config_section * @@ -548,35 +513,17 @@ _config_panel_set_defaults(struct _config_section *base) } static void -_config_panel_free(struct _config_section *base) -{ - struct _config_panel *panel = (struct _config_panel *) base; - - eina_stringshare_del(panel->custom_font); -} - -static Eina_Bool -_config_panel_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_panel_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_panel_get_values(struct _config_section *base, const char *section) -{ - return NULL; -} - -static void _config_panel_section_init(struct _config_section *base, struct _config_section *parent) { - _config_section_init(panel); + _config_section_init(base, panel); + _config_section_add_key_int(base, panel, show); + _config_section_add_key_int(base, panel, x); + _config_section_add_key_int(base, panel, y); + _config_section_add_key_int(base, panel, lookup_table_orientation); + _config_section_add_key_bool(base, panel, show_icon_in_systray); + _config_section_add_key_bool(base, panel, show_im_name); + _config_section_add_key_bool(base, panel, use_custom_font); + _config_section_add_key_string(base, panel, custom_font); } static struct _config_section * @@ -632,36 +579,13 @@ _config_hangul_set_defaults(struct _config_section *base) } static void -_config_hangul_free(struct _config_section *base) -{ - struct _config_hangul *hangul = (struct _config_hangul *) base; - - eina_stringshare_del(hangul->hangul_keyboard); - _config_string_list_free(hangul->hanja_keys); -} - -static Eina_Bool -_config_hangul_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_hangul_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_hangul_get_values(struct _config_section *base, const char *section) -{ - return NULL; -} - -static void _config_hangul_section_init(struct _config_section *base, struct _config_section *parent) { - _config_section_init(hangul); + _config_section_init(base, hangul); + _config_section_add_key_string(base, hangul, hangul_keyboard); + _config_section_add_key_string_list(base, hangul, hanja_keys); + _config_section_add_key_bool(base, hangul, word_commit); + _config_section_add_key_bool(base, hangul, auto_reorder); } static struct _config_section * @@ -704,37 +628,14 @@ _config_engine_set_defaults(struct _config_section *base) } static void -_config_engine_free(struct _config_section *base) -{ -} - -static Eina_Bool -_config_engine_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_engine_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_engine_get_values(struct _config_section *base, const char *section) -{ - return NULL; -} - -static void _config_engine_section_init(struct _config_section *base, struct _config_section *parent) { struct _config_engine *conf= (struct _config_engine *) base; - _config_section_init(engine); - if (conf->hangul) _config_hangul_section_init(conf->hangul, base); + + _config_section_init(base, engine); } static struct _config_section * @@ -785,35 +686,10 @@ _config_ibus_set_defaults(struct _config_section *base) } static void -_config_ibus_free(struct _config_section *base) -{ -} - -static Eina_Bool -_config_ibus_set_value(struct _config_section *base, const char *section, const char *name, Eldbus_Message_Iter *value) -{ - return EINA_FALSE; -} - -static void * -_config_ibus_get_value(struct _config_section *base, const char *section, const char *name) -{ - return NULL; -} - -static void * -_config_ibus_get_values(struct _config_section *base, const char *section) -{ - return NULL; -} - -static void _config_ibus_section_init(struct _config_section *base, struct _config_section *parent) { struct _config_ibus *conf= (struct _config_ibus *) base; - _config_section_init(ibus); - if (conf->general) _config_general_section_init(conf->general, base); @@ -822,6 +698,9 @@ _config_ibus_section_init(struct _config_section *base, struct _config_section * if (conf->engine) _config_engine_section_init(conf->engine, base); + + _config_section_init(base, ibus); + } static struct _config_section * diff --git a/src/wkb-ibus-config-key.c b/src/wkb-ibus-config-key.c new file mode 100644 index 0000000..13b6f1f --- /dev/null +++ b/src/wkb-ibus-config-key.c @@ -0,0 +1,210 @@ +/* + * Copyright © 2013 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <string.h> + +#include <Eina.h> + +#include "wkb-ibus-config-key.h" + +typedef void (*key_free_cb) (void *); +typedef Eina_Bool (*key_set_cb) (struct wkb_config_key *, Eldbus_Message_Iter *); +typedef void *(*key_get_cb) (struct wkb_config_key *); + +struct wkb_config_key +{ + const char *id; + void *field; /* pointer to the actual struct field */ + + key_free_cb free; + key_set_cb set; + key_get_cb get; +}; + +static struct wkb_config_key * +_key_new(const char *id, void *field, key_free_cb free_cb, key_set_cb set_cb, key_get_cb get_cb) +{ + struct wkb_config_key *key = calloc(1, sizeof(*key)); + key->id = eina_stringshare_add(id); + key->field = field; + key->free = free_cb; + key->set = set_cb; + key->get = get_cb; + return key; +} + +#define _key_basic_set(_type, _dtype) \ + do { \ + _type __value = 0; \ + _type *__field = (_type *) key->field; \ + if (!eldbus_message_iter_arguments_get(iter, _dtype, &__value)) \ + { \ + printf("Error decoding " #_type " value using '" _dtype "'\n"); \ + return EINA_FALSE; \ + } \ + *__field = __value; \ + return EINA_TRUE; \ + } while (0) + +#define _key_basic_get(_type, _key) \ + do { \ + _type *__field = (_type *) _key->field; \ + return (void *) *__field; \ + } while (0) + +static Eina_Bool +_key_int_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter) +{ + _key_basic_set(int, "i"); +} + +static void * +_key_int_get(struct wkb_config_key *key) +{ + _key_basic_get(int, key); +} + +static Eina_Bool +_key_bool_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter) +{ + _key_basic_set(Eina_Bool, "b"); +} + +static void * +_key_bool_get(struct wkb_config_key *key) +{ + _key_basic_get(Eina_Bool, key); +} + +static void +_key_string_free(const char **str) +{ + if (*str) + eina_stringshare_del(*str); +} + +static Eina_Bool +_key_string_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter) +{ + const char *str; + const char **field; + + if (!eldbus_message_iter_arguments_get(iter, "s", &str)) + { + printf("Error decoding string value using 's'\n"); + return EINA_FALSE; + } + + if ((*field = (const char *) key->field) != NULL) + eina_stringshare_del(*field); + + if (str && strlen(str)) + *field = eina_stringshare_add(str); + else + *field = NULL; + + return EINA_TRUE; +} + +static void * +_key_string_get(struct wkb_config_key *key) +{ + return NULL; +} + +static void +_key_string_list_free(Eina_List **list) +{ + const char *str; + + EINA_LIST_FREE(*list, str) + eina_stringshare_del(str); + + eina_list_free(*list); +} + +static Eina_Bool +_key_string_list_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter) +{ + return EINA_TRUE; +} + +static void * +_key_string_list_get(struct wkb_config_key *key) +{ + return NULL; +} + +/* + * PUBLIC FUNCTIONS + */ + +struct wkb_config_key * +wkb_config_key_int(const char *id, void *field) +{ + return _key_new(id, field, NULL, _key_int_set, _key_int_get); +} + +struct wkb_config_key * +wkb_config_key_bool(const char *id, void *field) +{ + return _key_new(id, field, NULL, _key_bool_set, _key_bool_get); +} + +struct wkb_config_key * +wkb_config_key_string(const char *id, void *field) +{ + return _key_new(id, field, (key_free_cb) _key_string_free, _key_string_set, _key_string_get); +} + +struct wkb_config_key * +wkb_config_key_string_list(const char *id, void *field) +{ + return _key_new(id, field, (key_free_cb) _key_string_list_free, _key_string_list_set, _key_string_list_get); +} + +void +wkb_config_key_free(struct wkb_config_key *key) +{ + if (key->free && key->field) + key->free(key->field); + + eina_stringshare_del(key->id); + free(key); +} + +static Eina_Bool +wkb_config_key_set_value(struct wkb_config_key * key, Eldbus_Message_Iter *iter) +{ + if (!key->field || !key->set) + return EINA_FALSE; + + return key->set(key, iter); +} + +void * +wkb_config_key_get_value(struct wkb_config_key *key) +{ + if (!key->field || !key->get) + return NULL; + + return key->get(key); +} + diff --git a/src/wkb-ibus-config-key.h b/src/wkb-ibus-config-key.h new file mode 100644 index 0000000..709421e --- /dev/null +++ b/src/wkb-ibus-config-key.h @@ -0,0 +1,34 @@ +/* + * Copyright © 2013 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _WKB_IBUS_CONFIG_KEY_H_ +#define _WKB_IBUS_CONFIG_KEY_H_ + +#include <Eina.h> +#include <Eldbus.h> + +struct wkb_config_key; + +struct wkb_config_key *wkb_config_key_int(const char *id, void *field); +struct wkb_config_key *wkb_config_key_bool(const char *id, void *field); +struct wkb_config_key *wkb_config_key_string(const char *id, void *field); +struct wkb_config_key *wkb_config_key_string_list(const char *id, void *field); + +void wkb_config_key_free(struct wkb_config_key *key); +Eina_Bool wkb_config_key_set(struct wkb_config_key * key, Eldbus_Message_Iter *iter); +void * wkb_config_key_get(struct wkb_config_key *key); + +#endif /* _WKB_IBUS_CONFIG_KEY_H_ */ |