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 Eina_Bool (*key_get_cb) (struct wkb_config_key *, Eldbus_Message_Iter *);
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(_key, _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(_key, _type, _dtype, _iter) \
68 _type *__field = (_type *) _key->field; \
69 eldbus_message_iter_basic_append(_iter, _dtype, *__field); \
74 _key_int_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
76 _key_basic_set(key, int, "i");
80 _key_int_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
82 _key_basic_get(key, int, 'i', reply);
86 _key_bool_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
88 _key_basic_set(key, Eina_Bool, "b");
92 _key_bool_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
94 _key_basic_get(key, Eina_Bool, 'b', reply);
98 _key_string_free(const char **str)
101 eina_stringshare_del(*str);
105 _key_string_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
110 if (!eldbus_message_iter_arguments_get(iter, "s", &str))
112 printf("Error decoding string value using 's'\n");
116 if ((field = (const char **) key->field) && *field)
117 eina_stringshare_del(*field);
119 if (str && strlen(str))
120 *field = eina_stringshare_add(str);
128 _key_string_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
130 _key_basic_get(key, const char *, 's', reply);
134 _key_string_list_free(Eina_List **list)
138 EINA_LIST_FREE(*list, str)
139 eina_stringshare_del(str);
141 eina_list_free(*list);
145 _key_string_list_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
148 Eina_List *list = NULL;
151 while (eldbus_message_iter_get_and_next(iter, 's', &str))
152 list = eina_list_append(list,eina_stringshare_add(str));
154 if ((field = (Eina_List **) key->field) && *field)
155 _key_string_list_free(field);
163 _key_string_list_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
171 struct wkb_config_key *
172 wkb_config_key_int(const char *id, void *field)
174 return _key_new(id, field, NULL, _key_int_set, _key_int_get);
177 struct wkb_config_key *
178 wkb_config_key_bool(const char *id, void *field)
180 return _key_new(id, field, NULL, _key_bool_set, _key_bool_get);
183 struct wkb_config_key *
184 wkb_config_key_string(const char *id, void *field)
186 return _key_new(id, field, (key_free_cb) _key_string_free, _key_string_set, _key_string_get);
189 struct wkb_config_key *
190 wkb_config_key_string_list(const char *id, void *field)
192 return _key_new(id, field, (key_free_cb) _key_string_list_free, _key_string_list_set, _key_string_list_get);
196 wkb_config_key_free(struct wkb_config_key *key)
198 if (key->free && key->field)
199 key->free(key->field);
201 eina_stringshare_del(key->id);
206 wkb_config_key_id(struct wkb_config_key *key)
212 wkb_config_key_set(struct wkb_config_key * key, Eldbus_Message_Iter *iter)
214 if (!key->field || !key->set)
217 return key->set(key, iter);
221 wkb_config_key_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
223 if (!key->field || !key->get)
226 return key->get(key, reply);