2 * Copyright © 2013 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
25 #include "wkb-ibus-config-key.h"
27 typedef void (*key_free_cb) (void *);
28 typedef Eina_Bool (*key_set_cb) (struct wkb_config_key *, Eldbus_Message_Iter *);
29 typedef void *(*key_get_cb) (struct wkb_config_key *);
34 void *field; /* pointer to the actual struct field */
41 static struct wkb_config_key *
42 _key_new(const char *id, void *field, key_free_cb free_cb, key_set_cb set_cb, key_get_cb get_cb)
44 struct wkb_config_key *key = calloc(1, sizeof(*key));
45 key->id = eina_stringshare_add(id);
53 #define _key_basic_set(_type, _dtype) \
56 _type *__field = (_type *) key->field; \
57 if (!eldbus_message_iter_arguments_get(iter, _dtype, &__value)) \
59 printf("Error decoding " #_type " value using '" _dtype "'\n"); \
66 #define _key_basic_get(_type, _key) \
68 _type *__field = (_type *) _key->field; \
69 return (void *) *__field; \
73 _key_int_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
75 _key_basic_set(int, "i");
79 _key_int_get(struct wkb_config_key *key)
81 _key_basic_get(int, key);
85 _key_bool_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
87 _key_basic_set(Eina_Bool, "b");
91 _key_bool_get(struct wkb_config_key *key)
93 _key_basic_get(Eina_Bool, key);
97 _key_string_free(const char **str)
100 eina_stringshare_del(*str);
104 _key_string_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
109 if (!eldbus_message_iter_arguments_get(iter, "s", &str))
111 printf("Error decoding string value using 's'\n");
115 if ((field = (const char *) key->field) && *field)
116 eina_stringshare_del(*field);
118 if (str && strlen(str))
119 *field = eina_stringshare_add(str);
127 _key_string_get(struct wkb_config_key *key)
133 _key_string_list_free(Eina_List **list)
137 EINA_LIST_FREE(*list, str)
138 eina_stringshare_del(str);
140 eina_list_free(*list);
144 _key_string_list_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
147 Eina_List *list = NULL;
150 while (eldbus_message_iter_get_and_next(iter, 's', &str))
151 list = eina_list_append(list,eina_stringshare_add(str));
153 if ((field = (Eina_List **) key->field) && *field)
154 _key_string_list_free(field);
162 _key_string_list_get(struct wkb_config_key *key)
170 struct wkb_config_key *
171 wkb_config_key_int(const char *id, void *field)
173 return _key_new(id, field, NULL, _key_int_set, _key_int_get);
176 struct wkb_config_key *
177 wkb_config_key_bool(const char *id, void *field)
179 return _key_new(id, field, NULL, _key_bool_set, _key_bool_get);
182 struct wkb_config_key *
183 wkb_config_key_string(const char *id, void *field)
185 return _key_new(id, field, (key_free_cb) _key_string_free, _key_string_set, _key_string_get);
188 struct wkb_config_key *
189 wkb_config_key_string_list(const char *id, void *field)
191 return _key_new(id, field, (key_free_cb) _key_string_list_free, _key_string_list_set, _key_string_list_get);
195 wkb_config_key_free(struct wkb_config_key *key)
197 if (key->free && key->field)
198 key->free(key->field);
200 eina_stringshare_del(key->id);
205 wkb_config_key_id(struct wkb_config_key *key)
211 wkb_config_key_set(struct wkb_config_key * key, Eldbus_Message_Iter *iter)
213 if (!key->field || !key->set)
216 return key->set(key, iter);
220 wkb_config_key_get(struct wkb_config_key *key)
222 if (!key->field || !key->get)
225 return key->get(key);