6853974b33bca3bc5f07b705efa2bb5bad9f71d2
[profile/ivi/weekeyboard.git] / src / wkb-ibus-config-key.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 #include <Eina.h>
24
25 #include "wkb-ibus-config-key.h"
26
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 *);
30
31 struct wkb_config_key
32 {
33    const char *id;
34    void *field; /* pointer to the actual struct field */
35
36    key_free_cb free;
37    key_set_cb set;
38    key_get_cb get;
39 };
40
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)
43 {
44    struct wkb_config_key *key = calloc(1, sizeof(*key));
45    key->id = eina_stringshare_add(id);
46    key->field = field;
47    key->free = free_cb;
48    key->set = set_cb;
49    key->get = get_cb;
50    return key;
51 }
52
53 #define _key_basic_set(_type, _dtype) \
54    do { \
55         _type __value = 0; \
56         _type *__field = (_type *) key->field; \
57         if (!eldbus_message_iter_arguments_get(iter, _dtype, &__value)) \
58           { \
59              printf("Error decoding " #_type " value using '" _dtype "'\n"); \
60              return EINA_FALSE; \
61           } \
62         *__field = __value; \
63         return EINA_TRUE; \
64    } while (0)
65
66 #define _key_basic_get(_type, _key) \
67    do { \
68         _type *__field = (_type *) _key->field; \
69         return (void *) *__field; \
70    } while (0)
71
72 static Eina_Bool
73 _key_int_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
74 {
75    _key_basic_set(int, "i");
76 }
77
78 static void *
79 _key_int_get(struct wkb_config_key *key)
80 {
81    _key_basic_get(int, key);
82 }
83
84 static Eina_Bool
85 _key_bool_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
86 {
87    _key_basic_set(Eina_Bool, "b");
88 }
89
90 static void *
91 _key_bool_get(struct wkb_config_key *key)
92 {
93    _key_basic_get(Eina_Bool, key);
94 }
95
96 static void
97 _key_string_free(const char **str)
98 {
99    if (*str)
100       eina_stringshare_del(*str);
101 }
102
103 static Eina_Bool
104 _key_string_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
105 {
106    const char *str;
107    const char **field;
108
109    if (!eldbus_message_iter_arguments_get(iter, "s", &str))
110      {
111         printf("Error decoding string value using 's'\n");
112         return EINA_FALSE;
113      }
114
115    if ((*field = (const char *) key->field) != NULL)
116       eina_stringshare_del(*field);
117
118    if (str && strlen(str))
119       *field = eina_stringshare_add(str);
120    else
121       *field = NULL;
122
123    return EINA_TRUE;
124 }
125
126 static void *
127 _key_string_get(struct wkb_config_key *key)
128 {
129    return NULL;
130 }
131
132 static void
133 _key_string_list_free(Eina_List **list)
134 {
135    const char *str;
136
137    EINA_LIST_FREE(*list, str)
138       eina_stringshare_del(str);
139
140    eina_list_free(*list);
141 }
142
143 static Eina_Bool
144 _key_string_list_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
145 {
146    return EINA_TRUE;
147 }
148
149 static void *
150 _key_string_list_get(struct wkb_config_key *key)
151 {
152    return NULL;
153 }
154
155 /*
156  * PUBLIC FUNCTIONS
157  */
158
159 struct wkb_config_key *
160 wkb_config_key_int(const char *id, void *field)
161 {
162    return _key_new(id, field, NULL, _key_int_set, _key_int_get);
163 }
164
165 struct wkb_config_key *
166 wkb_config_key_bool(const char *id, void *field)
167 {
168    return _key_new(id, field, NULL, _key_bool_set, _key_bool_get);
169 }
170
171 struct wkb_config_key *
172 wkb_config_key_string(const char *id, void *field)
173 {
174    return _key_new(id, field, (key_free_cb) _key_string_free, _key_string_set, _key_string_get);
175 }
176
177 struct wkb_config_key *
178 wkb_config_key_string_list(const char *id, void *field)
179 {
180    return _key_new(id, field, (key_free_cb) _key_string_list_free, _key_string_list_set, _key_string_list_get);
181 }
182
183 void
184 wkb_config_key_free(struct wkb_config_key *key)
185 {
186    if (key->free && key->field)
187       key->free(key->field);
188
189    eina_stringshare_del(key->id);
190    free(key);
191 }
192
193 const char *
194 wkb_config_key_id(struct wkb_config_key *key)
195 {
196    return key->id;
197 }
198
199 Eina_Bool
200 wkb_config_key_set(struct wkb_config_key * key, Eldbus_Message_Iter *iter)
201 {
202    if (!key->field || !key->set)
203       return EINA_FALSE;
204
205    return key->set(key, iter);
206 }
207
208 void *
209 wkb_config_key_get(struct wkb_config_key *key)
210 {
211    if (!key->field || !key->get)
212       return NULL;
213
214    return key->get(key);
215 }
216