4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
24 #include <libxml/tree.h>
28 #include "wgt-config.h"
37 static int getpropbool(xmlNodePtr node, const char *prop, int def)
40 char *val = xmlGetProp(node, prop);
44 if (!strcmp(val, "true"))
46 else if (!strcmp(val, "false"))
55 static int getpropnum(xmlNodePtr node, const char *prop, int def)
58 char *val = xmlGetProp(node, prop);
68 static xmlChar *optprop(xmlNodePtr node, const char *prop)
70 return node ? xmlGetProp(node, prop) : NULL;
73 static xmlChar *optcontent(xmlNodePtr node)
75 return node ? xmlNodeGetContent(node) : NULL;
78 static char *mkidaver(char *id, char *version)
84 c = version[lver = 0];
85 while(c && c != ' ') {
91 } while (c && c != ' ' && c != '.');
95 r = malloc(2 + lid + lver);
99 memcpy(r + lid + 1, version, lver + 1);
106 static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, int want_preferences)
108 xmlNodePtr node, pnode;
109 struct wgt_desc_icon *icon, **icontail;
110 struct wgt_desc_feature *feature, **featuretail;
111 struct wgt_desc_preference *preference, **preferencetail;
112 struct wgt_desc_param *param, **paramtail;
114 node = wgt_config_widget();
116 WARNING("no widget");
120 desc->id = xmlGetProp(node, wgt_config_string_id);
121 desc->version = xmlGetProp(node, wgt_config_string_version);
122 desc->idaver = mkidaver(desc->id, desc->version);
123 desc->width = getpropnum(node, wgt_config_string_width, 0);
124 desc->height = getpropnum(node, wgt_config_string_height, 0);
125 desc->viewmodes = xmlGetProp(node, wgt_config_string_viewmodes);
126 desc->defaultlocale = xmlGetProp(node, wgt_config_string_defaultlocale);
128 node = wgt_config_name();
129 desc->name = optcontent(node);
130 desc->name_short = optprop(node, wgt_config_string_short);
132 node = wgt_config_description();
133 desc->description = optcontent(node);
135 node = wgt_config_author();
136 desc->author = optcontent(node);
137 desc->author_href = optprop(node, wgt_config_string_href);
138 desc->author_email = optprop(node, wgt_config_string_email);
140 node = wgt_config_license();
141 desc->license = optcontent(node);
142 desc->license_href = optprop(node, wgt_config_string_href);
144 node = wgt_config_content();
145 desc->content_src = optprop(node, wgt_config_string_src);
146 if (node && desc->content_src == NULL) {
147 WARNING("content without src");
151 desc->content_type = optprop(node, wgt_config_string_type);
152 desc->content_encoding = optprop(node, wgt_config_string_encoding);
155 icontail = &desc->icons;
156 node = wgt_config_first_icon();
158 icon = malloc(sizeof * icon);
163 icon->src = xmlGetProp(node, wgt_config_string_src);
164 icon->width = getpropnum(node, wgt_config_string_width, 0);
165 icon->height = getpropnum(node, wgt_config_string_height, 0);
170 if (icon->src == NULL) {
171 WARNING("icon without src");
175 icontail = &icon->next;
176 node = wgt_config_next_icon(node);
181 featuretail = &desc->features;
182 node = wgt_config_first_feature();
184 feature = malloc(sizeof * feature);
185 if (feature == NULL) {
189 feature->name = xmlGetProp(node, wgt_config_string_name);
190 feature->required = getpropbool(node, wgt_config_string_required, 1);
191 feature->params = NULL;
193 feature->next = NULL;
194 *featuretail = feature;
196 if (feature->name == NULL) {
197 WARNING("feature without name");
202 paramtail = &feature->params;
203 pnode = wgt_config_first_param(node);
205 param = malloc(sizeof * param);
210 param->name = xmlGetProp(pnode, wgt_config_string_name);
211 param->value = xmlGetProp(pnode, wgt_config_string_value);
216 if (param->name == NULL || param->value == NULL) {
217 WARNING("param without name or value");
222 paramtail = ¶m->next;
223 pnode = wgt_config_next_param(pnode);
226 featuretail = &feature->next;
227 node = wgt_config_next_feature(node);
231 if (want_preferences) {
232 preferencetail = &desc->preferences;
233 node = wgt_config_first_preference();
235 preference = malloc(sizeof * preference);
236 if (preference == NULL) {
240 preference->name = xmlGetProp(node, wgt_config_string_name);
241 preference->value = xmlGetProp(node, wgt_config_string_value);
242 preference->readonly = getpropbool(node, wgt_config_string_readonly, 0);
244 *preferencetail = preference;
245 preference->next = NULL;
247 if (preference->name == NULL) {
248 WARNING("preference without name");
253 preferencetail = &preference->next;
254 node = wgt_config_next_preference(node);
260 static void free_desc(struct wgt_desc *desc)
262 struct wgt_desc_icon *icon;
263 struct wgt_desc_feature *feature;
264 struct wgt_desc_preference *preference;
265 struct wgt_desc_param *param;
268 xmlFree(desc->version);
270 xmlFree(desc->viewmodes);
271 xmlFree(desc->defaultlocale);
273 xmlFree(desc->name_short);
274 xmlFree(desc->description);
275 xmlFree(desc->author);
276 xmlFree(desc->author_href);
277 xmlFree(desc->author_email);
278 xmlFree(desc->license);
279 xmlFree(desc->license_href);
280 xmlFree(desc->content_src);
281 xmlFree(desc->content_type);
282 xmlFree(desc->content_encoding);
286 desc->icons = icon->next;
291 while(desc->features) {
292 feature = desc->features;
293 desc->features = feature->next;
294 xmlFree(feature->name);
295 while(feature->params) {
296 param = feature->params;
297 feature->params = param->next;
298 xmlFree(param->name);
299 xmlFree(param->value);
305 while(desc->preferences) {
306 preference = desc->preferences;
307 desc->preferences = preference->next;
308 xmlFree(preference->name);
309 xmlFree(preference->value);
314 static void dump_desc(struct wgt_desc *desc, FILE *f, const char *prefix)
316 struct wgt_desc_icon *icon;
317 struct wgt_desc_feature *feature;
318 struct wgt_desc_preference *preference;
319 struct wgt_desc_param *param;
321 if (desc->id) fprintf(f, "%sid: %s\n", prefix, desc->id);
322 if (desc->version) fprintf(f, "%sversion: %s\n", prefix, desc->version);
323 if (desc->idaver) fprintf(f, "%sidaver: %s\n", prefix, desc->idaver);
324 if (desc->width) fprintf(f, "%swidth: %d\n", prefix, desc->width);
325 if (desc->height) fprintf(f, "%sheight: %d\n", prefix, desc->height);
326 if (desc->viewmodes) fprintf(f, "%sviewmodes: %s\n", prefix, desc->viewmodes);
327 if (desc->defaultlocale) fprintf(f, "%sdefaultlocale: %s\n", prefix, desc->defaultlocale);
328 if (desc->name) fprintf(f, "%sname: %s\n", prefix, desc->name);
329 if (desc->name_short) fprintf(f, "%sname_short: %s\n", prefix, desc->name_short);
330 if (desc->description) fprintf(f, "%sdescription: %s\n", prefix, desc->description);
331 if (desc->author) fprintf(f, "%sauthor: %s\n", prefix, desc->author);
332 if (desc->author_href) fprintf(f, "%sauthor_href: %s\n", prefix, desc->author_href);
333 if (desc->author_email) fprintf(f, "%sauthor_email: %s\n", prefix, desc->author_email);
334 if (desc->license) fprintf(f, "%slicense: %s\n", prefix, desc->license);
335 if (desc->license_href) fprintf(f, "%slicense_href: %s\n", prefix, desc->license_href);
336 if (desc->content_src) fprintf(f, "%scontent_src: %s\n", prefix, desc->content_src);
337 if (desc->content_type) fprintf(f, "%scontent_type: %s\n", prefix, desc->content_type);
338 if (desc->content_encoding) fprintf(f, "%scontent_encoding: %s\n", prefix, desc->content_encoding);
342 fprintf(f, "%s+ icon src: %s\n", prefix, icon->src);
343 if (icon->width) fprintf(f, "%s width: %d\n", prefix, icon->width);
344 if (icon->height) fprintf(f, "%s height: %d\n", prefix, icon->height);
348 feature = desc->features;
350 fprintf(f, "%s+ feature name: %s\n", prefix, feature->name);
351 fprintf(f, "%s required: %s\n", prefix, feature->required ? "true" : "false");
352 param = feature->params;
354 fprintf(f, "%s + param name: %s\n", prefix, param->name);
355 fprintf(f, "%s value: %s\n", prefix, param->value);
358 feature = feature->next;
361 preference = desc->preferences;
363 fprintf(f, "%s+ preference name: %s\n", prefix, preference->name);
364 if (preference->value) fprintf(f, "%s value: %s\n", prefix, preference->value);
365 fprintf(f, "%s readonly: %s\n", prefix, preference->readonly ? "true" : "false");
366 preference = preference->next;
370 struct wgt_info *wgt_info_create(struct wgt *wgt, int icons, int features, int preferences)
373 struct wgt_info *result;
376 assert(wgt_is_connected(wgt));
377 rc = wgt_config_open(wgt);
383 result = calloc(sizeof * result, 1);
389 result->refcount = 1;
393 rc = fill_desc(&result->desc, icons, features, preferences);
396 wgt_info_unref(result);
402 struct wgt_info *wgt_info_createat(int dirfd, const char *pathname, int icons, int features, int preferences)
404 struct wgt_info *result = NULL;
405 struct wgt *wgt = wgt_createat(dirfd, pathname);
407 result = wgt_info_create(wgt, icons, features, preferences);
413 const struct wgt_desc *wgt_info_desc(struct wgt_info *ifo)
419 struct wgt *wgt_info_wgt(struct wgt_info *ifo)
426 void wgt_info_addref(struct wgt_info *ifo)
429 assert(ifo->refcount > 0);
433 void wgt_info_unref(struct wgt_info *ifo)
436 assert(ifo->refcount > 0);
440 free_desc(&ifo->desc);
445 void wgt_info_dump(struct wgt_info *ifo, int fd, const char *prefix)
452 WARNING("can't fdopen in wgt_info_dump");
454 dump_desc(&ifo->desc, f, prefix);
459 const struct wgt_desc_feature *wgt_info_feature(struct wgt_info *ifo, const char *name)
461 const struct wgt_desc_feature *result = ifo->desc.features;
462 while(result && strcmp(result->name, name))
463 result = result->next;
467 const char *wgt_info_param(const struct wgt_desc_feature *feature, const char *name)
469 const struct wgt_desc_param *param = feature->params;
471 if (0 == strcmp(name, param->name))