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.
21 #include "wkb-ibus-helper.h"
24 struct wkb_ibus_serializable
27 * All messages sent by IBus will start with the sa{sv} signature, but
28 * those fields don't seem useful for us, this struct is used to help
29 * on deserializing those fields
32 Eldbus_Message_Iter *variant;
35 typedef void (*_free_func) (void*);
38 _free_eina_array(Eina_Array *array, _free_func free_cb)
43 while (eina_array_count(array))
44 free_cb(eina_array_pop(array));
46 eina_array_free(array);
49 struct wkb_ibus_attr *
50 wkb_ibus_attr_from_message_iter(Eldbus_Message_Iter *iter)
52 struct wkb_ibus_attr *attr = calloc(1, sizeof(*attr));
54 EINA_SAFETY_ON_NULL_RETURN_VAL(attr, NULL);
56 DBG("Attribute iter signature '%s'", eldbus_message_iter_signature_get(iter));
58 if (!eldbus_message_iter_arguments_get(iter, "uuuu", &attr->type,
59 &attr->value, &attr->start_idx,
62 ERR("Error deserializing IBusAttribute");
63 wkb_ibus_attr_free(attr);
71 wkb_ibus_attr_free(struct wkb_ibus_attr *attr)
77 wkb_ibus_text_free(struct wkb_ibus_text *text)
82 _free_eina_array(text->attrs, (_free_func) free);
87 struct wkb_ibus_text *
88 wkb_ibus_text_from_message_iter(Eldbus_Message_Iter *iter)
90 struct wkb_ibus_serializable ignore = { 0 };
91 struct wkb_ibus_text *text = calloc(1, sizeof(*text));
92 struct wkb_ibus_attr *attr = NULL;
93 Eldbus_Message_Iter *attrs = NULL, *a = NULL;
95 EINA_SAFETY_ON_NULL_RETURN_VAL(text, NULL);
97 DBG("Text iter signature '%s'", eldbus_message_iter_signature_get(iter));
99 if (!eldbus_message_iter_arguments_get(iter, "(sa{sv}sv)", &ignore.text,
100 &ignore.variant, &text->text, &attrs))
102 ERR("Error deserializing IBusText");
108 /* Check for attributes */
111 INF("Text has no attributes");
115 while (eldbus_message_iter_get_and_next(attrs, 'v', &a))
118 text->attrs = eina_array_new(10);
120 if (!(attr = wkb_ibus_attr_from_message_iter(a)))
122 wkb_ibus_text_free(text);
127 eina_array_push(text->attrs, attr);
135 wkb_ibus_lookup_table_free(struct wkb_ibus_lookup_table *table)
140 _free_eina_array(table->candidates, (_free_func) wkb_ibus_text_free);
141 _free_eina_array(table->labels, (_free_func) wkb_ibus_text_free);
145 struct wkb_ibus_lookup_table *
146 wkb_ibus_lookup_table_from_message_iter(Eldbus_Message_Iter *iter)
148 struct wkb_ibus_serializable ignore = { 0 };
149 struct wkb_ibus_lookup_table *table = calloc(1, sizeof(*table));
150 struct wkb_ibus_text *text = NULL;
151 Eldbus_Message_Iter *candidates = NULL, *labels = NULL, *t = NULL;
153 EINA_SAFETY_ON_NULL_RETURN_VAL(table, NULL);
155 DBG("LookupTable iter signature '%s'", eldbus_message_iter_signature_get(iter));
157 if (!eldbus_message_iter_arguments_get(iter, "(sa{sv}uubbiavav)",
158 &ignore.text, &ignore.variant,
159 &table->page_size, &table->cursor_pos,
160 &table->cursor_visible, &table->round,
161 &table->orientation, &candidates,
164 ERR("Error deserializing IBusLookupTable");
170 DBG("Lookup table:");
171 DBG("\tPage size.......: '%d'", table->page_size);
172 DBG("\tCursor position.: '%d'", table->cursor_pos);
173 DBG("\tCursor visible..: '%d'", table->cursor_visible);
174 DBG("\tRound...........: '%d'", table->round);
175 DBG("\tOrientation.....: '%d'", table->orientation);
176 DBG("\tCandidates......: '%p'", candidates);
177 DBG("\tLabels..........: '%p'", labels);
181 INF("Lookup table has no candidates");
185 while (eldbus_message_iter_get_and_next(candidates, 'v', &t))
187 if (!table->candidates)
188 table->candidates = eina_array_new(10);
190 if (!(text = wkb_ibus_text_from_message_iter(t)))
192 wkb_ibus_lookup_table_free(table);
197 DBG("Appending new candidate %s", text->text);
198 eina_array_push(table->candidates, text);
204 INF("Lookup table has no labels");
208 while (eldbus_message_iter_get_and_next(labels, 'v', &t))
211 table->labels = eina_array_new(10);
213 if (!(text = wkb_ibus_text_from_message_iter(t)))
215 wkb_ibus_lookup_table_free(table);
220 DBG("Appending new label %s", text->text);
221 eina_array_push(table->labels, text);
229 wkb_ibus_property_free(struct wkb_ibus_property *property)
235 free(property->icon);
236 wkb_ibus_text_free(property->label);
237 wkb_ibus_text_free(property->symbol);
238 wkb_ibus_text_free(property->tooltip);
239 _free_eina_array(property->sub_properties, (_free_func) wkb_ibus_property_free);
243 struct wkb_ibus_property *
244 wkb_ibus_property_from_message_iter(Eldbus_Message_Iter *iter)
246 struct wkb_ibus_serializable ignore = { 0 };
247 struct wkb_ibus_property *prop = calloc(1, sizeof(*prop));
248 Eldbus_Message_Iter *label = NULL, *symbol = NULL, *tooltip = NULL, *sub_props = NULL;
250 EINA_SAFETY_ON_NULL_RETURN_VAL(prop, NULL);
252 DBG("Property iter signature '%s'", eldbus_message_iter_signature_get(iter));
254 if (!eldbus_message_iter_arguments_get(iter, "(sa{sv}suvsvbbuvv)",
255 &ignore.text, &ignore.variant,
256 &prop->key, &prop->type,
257 &label, &prop->icon, &tooltip,
258 &prop->sensitive, &prop->visible,
259 &prop->state, &sub_props, &symbol))
261 ERR("Error deserializing IBusProperty");
268 DBG("\tKey.............: '%s'", prop->key);
269 DBG("\tType............: '%d'", prop->type);
270 DBG("\tLabel...........: '%p'", label);
271 DBG("\tIcon............: '%s'", prop->icon);
272 DBG("\tTooltip.........: '%p'", tooltip);
273 DBG("\tSensitive.......: '%d'", prop->sensitive);
274 DBG("\tVisible.........: '%d'", prop->visible);
275 DBG("\tState...........: '%d'", prop->state);
276 DBG("\tSub Properties..: '%p'", sub_props);
277 DBG("\tSymbol..........: '%p'", symbol);
281 INF("Property has no label");
285 if (!(prop->label = wkb_ibus_text_from_message_iter(label)))
287 wkb_ibus_property_free(prop);
295 INF("Property has no symbol");
299 if (!(prop->symbol = wkb_ibus_text_from_message_iter(symbol)))
301 wkb_ibus_property_free(prop);
309 INF("Property has no tooltip");
313 if (!(prop->tooltip = wkb_ibus_text_from_message_iter(tooltip)))
315 wkb_ibus_property_free(prop);
323 INF("Property has no sub properties");
327 prop->sub_properties = wkb_ibus_properties_from_message_iter(sub_props);
334 wkb_ibus_properties_free(Eina_Array *properties)
336 _free_eina_array(properties, (_free_func) wkb_ibus_property_free);
340 wkb_ibus_properties_from_message_iter(Eldbus_Message_Iter *iter)
342 Eina_Array *properties = NULL;
343 Eldbus_Message_Iter *props = NULL, *prop = NULL;
344 struct wkb_ibus_serializable ignore = { 0 };
345 struct wkb_ibus_property *property = NULL;
347 DBG("PropList iter signature '%s'", eldbus_message_iter_signature_get(iter));
349 if (!eldbus_message_iter_arguments_get(iter, "(sa{sv}av)", &ignore.text, &ignore.variant, &props))
351 ERR("Error deserializing IBusPropList");
357 INF("PropList has no property");
361 while (eldbus_message_iter_get_and_next(props, 'v', &prop))
364 properties = eina_array_new(10);
366 if (!(property = wkb_ibus_property_from_message_iter(prop)))
368 wkb_ibus_properties_free(properties);
373 DBG("Appending new property %p", property);
374 eina_array_push(properties, property);