summaryrefslogtreecommitdiff
path: root/src/wkb-ibus-config-key.c
blob: 78a2db2272eceb62eddc720f0d584ada317745e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright © 2013 Intel Corporation
 * Copyright © 2014 Jaguar Landrover
 *
 * 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 <assert.h>

#include "wkb-ibus-config-key.h"
#include "wkb-log.h"

typedef void (*key_free_cb) (void *);
typedef Eina_Bool (*key_set_cb) (struct wkb_config_key *, Eldbus_Message_Iter *);
typedef Eina_Bool (*key_get_cb) (struct wkb_config_key *, Eldbus_Message_Iter *);

struct wkb_config_key
{
   const char *id;
   const char *section;
   const char *signature;
   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, const char *section, const char *signature, 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->section = eina_stringshare_add(section);
   key->signature = eina_stringshare_add(signature);
   key->field = field;
   key->free = free_cb;
   key->set = set_cb;
   key->get = get_cb;
   return key;
}

#define _key_basic_set(_key, _type) \
   do { \
        _type __value = 0; \
        _type *__field = (_type *) _key->field; \
        if (!eldbus_message_iter_arguments_get(iter, _key->signature, &__value)) \
          { \
             ERR("Error decoding " #_type " value using '%s'", _key->signature); \
             return EINA_FALSE; \
          } \
        *__field = __value; \
        return EINA_TRUE; \
   } while (0)

#define _key_basic_get(_key, _type, _iter) \
   do { \
        _type *__field = (_type *) _key->field; \
       eldbus_message_iter_basic_append(_iter, *_key->signature, *__field); \
       return EINA_TRUE; \
   } while (0)

static Eina_Bool
_key_int_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
{
   _key_basic_set(key, int);
}

static Eina_Bool
_key_int_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
{
   _key_basic_get(key, int, reply);
}

static Eina_Bool
_key_bool_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
{
   _key_basic_set(key, Eina_Bool);
}

static Eina_Bool
_key_bool_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
{
   _key_basic_get(key, Eina_Bool, reply);
}

static void
_key_string_free(const char **str)
{
   if (!*str)
      return;

   eina_stringshare_del(*str);
   *str = NULL;
}

static Eina_Bool
_key_string_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
{
   const char *str = NULL;
   const char **field;

   if (iter && !eldbus_message_iter_arguments_get(iter, "s", &str))
     {
        ERR("Error decoding string value using 's'");
        return EINA_FALSE;
     }

   if ((field = (const char **) key->field))
      _key_string_free(field);

   if (str)
      *field = eina_stringshare_add(str);

   INF("Setting key <%s/%s> to <%s>", key->section, key->id, *field);

   return EINA_TRUE;
}

static Eina_Bool
_key_string_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
{
   _key_basic_get(key, const char *, reply);
}

static void
_key_string_list_free(Eina_List **list)
{
   const char *str;

   if (!*list)
      return;

   EINA_LIST_FREE(*list, str)
      eina_stringshare_del(str);

   eina_list_free(*list);
   *list = NULL;
}

static Eina_Bool
_key_string_list_set(struct wkb_config_key *key, Eldbus_Message_Iter *iter)
{
   const char *str;
   Eina_List *list = NULL;
   Eina_List **field;

   while (iter && eldbus_message_iter_get_and_next(iter, 's', &str))
      list = eina_list_append(list,eina_stringshare_add(str));

   if ((field = (Eina_List **) key->field))
      _key_string_list_free(field);

   *field = list;

   return EINA_TRUE;
}

static Eina_Bool
_key_string_list_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
{
   Eina_List *node, **list = (Eina_List **) key->field;
   const char *str;
   Eldbus_Message_Iter *array;

   array = eldbus_message_iter_container_new(reply, 'a', "s");

   EINA_LIST_FOREACH(*list, node, str)
      eldbus_message_iter_basic_append(array, 's', str);

   eldbus_message_iter_container_close(reply, array);

   return EINA_TRUE;
}

/*
 * PUBLIC FUNCTIONS
 */
struct wkb_config_key *
wkb_config_key_int(const char *id, const char *section, void *field)
{
   return _key_new(id, section, "i", field, NULL, _key_int_set, _key_int_get);
}

struct wkb_config_key *
wkb_config_key_bool(const char *id, const char *section, void *field)
{
   return _key_new(id, section, "b", field, NULL, _key_bool_set, _key_bool_get);
}

struct wkb_config_key *
wkb_config_key_string(const char *id, const char *section, void *field)
{
   return _key_new(id, section, "s", 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, const char *section, void *field)
{
   return _key_new(id, section, "as", 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);
   eina_stringshare_del(key->section);
   eina_stringshare_del(key->signature);
   free(key);
}

const char *
wkb_config_key_id(struct wkb_config_key *key)
{
   return key->id;
}

const char *
wkb_config_key_section(struct wkb_config_key *key)
{
   return key->section;
}

const char *
wkb_config_key_signature(struct wkb_config_key *key)
{
   return key->signature;
}

Eina_Bool
wkb_config_key_set(struct wkb_config_key * key, Eldbus_Message_Iter *iter)
{
   if (!key->field || !key->set)
      return EINA_FALSE;

   return key->set(key, iter);
}

Eina_Bool
wkb_config_key_get(struct wkb_config_key *key, Eldbus_Message_Iter *reply)
{
   Eina_Bool ret = EINA_FALSE;
   Eldbus_Message_Iter *value;

   if (!key->field || !key->get)
      return EINA_FALSE;

   value = eldbus_message_iter_container_new(reply, 'v', key->signature);

   if (!(ret = key->get(key, value)))
     {
        ERR("Unexpected error retrieving value for key: '%s'", key->id);
     }

   eldbus_message_iter_container_close(reply, value);

   return ret;
}

int
wkb_config_key_get_int(struct wkb_config_key* key)
{
   assert(!strcmp(key->signature, "i"));

   return *((int *) key->field);
}

Eina_Bool
wkb_config_key_get_bool(struct wkb_config_key* key)
{
   assert(!strcmp(key->signature, "b"));

   return *((Eina_Bool *) key->field);
}

const char *
wkb_config_key_get_string(struct wkb_config_key* key)
{
   DBG("Found key: id = <%s> signature = <%s> field = 0x%p", key->id, key->signature, key->field);
   DBG("Found key: id = <%s> signature = <%s> field as string = <%s>", key->id, key->signature, *(const char **)key->field);

   assert(!strcmp(key->signature, "s"));

   return *((const char **) key->field);
}

char **
wkb_config_key_get_string_list(struct wkb_config_key *key)
{
   Eina_List *node, **list = (Eina_List **) key->field;
   char *str, **ret;
   int i = 0;

   assert(!strcmp(key->signature, "as"));

   ret = calloc(eina_list_count(*list) + 1, sizeof(char *));
   EINA_LIST_FOREACH(*list, node, str)
      ret[i++] = str;

   return ret;
}