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 int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, int want_preferences)
80 xmlNodePtr node, pnode;
81 struct wgt_desc_icon *icon, **icontail;
82 struct wgt_desc_feature *feature, **featuretail;
83 struct wgt_desc_preference *preference, **preferencetail;
84 struct wgt_desc_param *param, **paramtail;
86 node = wgt_config_widget();
92 desc->id = xmlGetProp(node, wgt_config_string_id);
93 desc->version = xmlGetProp(node, wgt_config_string_version);
94 desc->width = getpropnum(node, wgt_config_string_width, 0);
95 desc->height = getpropnum(node, wgt_config_string_height, 0);
96 desc->viewmodes = xmlGetProp(node, wgt_config_string_viewmodes);
97 desc->defaultlocale = xmlGetProp(node, wgt_config_string_defaultlocale);
99 node = wgt_config_name();
100 desc->name = optcontent(node);
101 desc->name_short = optprop(node, wgt_config_string_short);
103 node = wgt_config_description();
104 desc->description = optcontent(node);
106 node = wgt_config_author();
107 desc->author = optcontent(node);
108 desc->author_href = optprop(node, wgt_config_string_href);
109 desc->author_email = optprop(node, wgt_config_string_email);
111 node = wgt_config_license();
112 desc->license = optcontent(node);
113 desc->license_href = optprop(node, wgt_config_string_href);
115 node = wgt_config_content();
116 desc->content_src = optprop(node, wgt_config_string_src);
117 if (node && desc->content_src == NULL) {
118 WARNING("content without src");
122 desc->content_type = optprop(node, wgt_config_string_type);
123 desc->content_encoding = optprop(node, wgt_config_string_encoding);
126 icontail = &desc->icons;
127 node = wgt_config_first_icon();
129 icon = malloc(sizeof * icon);
134 icon->src = xmlGetProp(node, wgt_config_string_src);
135 icon->width = getpropnum(node, wgt_config_string_width, 0);
136 icon->height = getpropnum(node, wgt_config_string_height, 0);
141 if (icon->src == NULL) {
142 WARNING("icon without src");
146 icontail = &icon->next;
147 node = wgt_config_next_icon(node);
152 featuretail = &desc->features;
153 node = wgt_config_first_feature();
155 feature = malloc(sizeof * feature);
156 if (feature == NULL) {
160 feature->name = xmlGetProp(node, wgt_config_string_name);
161 feature->required = getpropbool(node, wgt_config_string_required, 1);
162 feature->params = NULL;
164 feature->next = NULL;
165 *featuretail = feature;
167 if (feature->name == NULL) {
168 WARNING("feature without name");
173 paramtail = &feature->params;
174 pnode = wgt_config_first_param(node);
176 param = malloc(sizeof * param);
181 param->name = xmlGetProp(pnode, wgt_config_string_name);
182 param->value = xmlGetProp(pnode, wgt_config_string_value);
187 if (param->name == NULL || param->value == NULL) {
188 WARNING("param without name or value");
193 paramtail = ¶m->next;
194 pnode = wgt_config_next_param(pnode);
197 featuretail = &feature->next;
198 node = wgt_config_next_feature(node);
202 if (want_preferences) {
203 preferencetail = &desc->preferences;
204 node = wgt_config_first_preference();
206 preference = malloc(sizeof * preference);
207 if (preference == NULL) {
211 preference->name = xmlGetProp(node, wgt_config_string_name);
212 preference->value = xmlGetProp(node, wgt_config_string_value);
213 preference->readonly = getpropbool(node, wgt_config_string_readonly, 0);
215 *preferencetail = preference;
216 preference->next = NULL;
218 if (preference->name == NULL) {
219 WARNING("preference without name");
224 preferencetail = &preference->next;
225 node = wgt_config_next_preference(node);
231 static void free_desc(struct wgt_desc *desc)
233 struct wgt_desc_icon *icon;
234 struct wgt_desc_feature *feature;
235 struct wgt_desc_preference *preference;
236 struct wgt_desc_param *param;
239 xmlFree(desc->version);
240 xmlFree(desc->viewmodes);
241 xmlFree(desc->defaultlocale);
243 xmlFree(desc->name_short);
244 xmlFree(desc->description);
245 xmlFree(desc->author);
246 xmlFree(desc->author_href);
247 xmlFree(desc->author_email);
248 xmlFree(desc->license);
249 xmlFree(desc->license_href);
250 xmlFree(desc->content_src);
251 xmlFree(desc->content_type);
252 xmlFree(desc->content_encoding);
256 desc->icons = icon->next;
261 while(desc->features) {
262 feature = desc->features;
263 desc->features = feature->next;
264 xmlFree(feature->name);
265 while(feature->params) {
266 param = feature->params;
267 feature->params = param->next;
268 xmlFree(param->name);
269 xmlFree(param->value);
275 while(desc->preferences) {
276 preference = desc->preferences;
277 desc->preferences = preference->next;
278 xmlFree(preference->name);
279 xmlFree(preference->value);
284 static void dump_desc(struct wgt_desc *desc, FILE *f, const char *prefix)
286 struct wgt_desc_icon *icon;
287 struct wgt_desc_feature *feature;
288 struct wgt_desc_preference *preference;
289 struct wgt_desc_param *param;
291 if (desc->id) fprintf(f, "%sid: %s\n", prefix, desc->id);
292 if (desc->width) fprintf(f, "%swidth: %d\n", prefix, desc->width);
293 if (desc->height) fprintf(f, "%sheight: %d\n", prefix, desc->height);
294 if (desc->version) fprintf(f, "%sversion: %s\n", prefix, desc->version);
295 if (desc->viewmodes) fprintf(f, "%sviewmodes: %s\n", prefix, desc->viewmodes);
296 if (desc->defaultlocale) fprintf(f, "%sdefaultlocale: %s\n", prefix, desc->defaultlocale);
297 if (desc->name) fprintf(f, "%sname: %s\n", prefix, desc->name);
298 if (desc->name_short) fprintf(f, "%sname_short: %s\n", prefix, desc->name_short);
299 if (desc->description) fprintf(f, "%sdescription: %s\n", prefix, desc->description);
300 if (desc->author) fprintf(f, "%sauthor: %s\n", prefix, desc->author);
301 if (desc->author_href) fprintf(f, "%sauthor_href: %s\n", prefix, desc->author_href);
302 if (desc->author_email) fprintf(f, "%sauthor_email: %s\n", prefix, desc->author_email);
303 if (desc->license) fprintf(f, "%slicense: %s\n", prefix, desc->license);
304 if (desc->license_href) fprintf(f, "%slicense_href: %s\n", prefix, desc->license_href);
305 if (desc->content_src) fprintf(f, "%scontent_src: %s\n", prefix, desc->content_src);
306 if (desc->content_type) fprintf(f, "%scontent_type: %s\n", prefix, desc->content_type);
307 if (desc->content_encoding) fprintf(f, "%scontent_encoding: %s\n", prefix, desc->content_encoding);
311 fprintf(f, "%s+ icon src: %s\n", prefix, icon->src);
312 if (icon->width) fprintf(f, "%s width: %d\n", prefix, icon->width);
313 if (icon->height) fprintf(f, "%s height: %d\n", prefix, icon->height);
317 feature = desc->features;
319 fprintf(f, "%s+ feature name: %s\n", prefix, feature->name);
320 fprintf(f, "%s required: %s\n", prefix, feature->required ? "true" : "false");
321 param = feature->params;
323 fprintf(f, "%s + param name: %s\n", prefix, param->name);
324 fprintf(f, "%s value: %s\n", prefix, param->value);
327 feature = feature->next;
330 preference = desc->preferences;
332 fprintf(f, "%s+ preference name: %s\n", prefix, preference->name);
333 if (preference->value) fprintf(f, "%s value: %s\n", prefix, preference->value);
334 fprintf(f, "%s readonly: %s\n", prefix, preference->readonly ? "true" : "false");
335 preference = preference->next;
339 struct wgt_info *wgt_info_create(struct wgt *wgt, int icons, int features, int preferences)
342 struct wgt_info *result;
345 assert(wgt_is_connected(wgt));
346 rc = wgt_config_open(wgt);
352 result = calloc(sizeof * result, 1);
358 result->refcount = 1;
362 rc = fill_desc(&result->desc, icons, features, preferences);
365 wgt_info_unref(result);
371 struct wgt_info *wgt_info_createat(int dirfd, const char *pathname, int icons, int features, int preferences)
373 struct wgt_info *result = NULL;
374 struct wgt *wgt = wgt_createat(dirfd, pathname);
376 result = wgt_info_create(wgt, icons, features, preferences);
382 const struct wgt_desc *wgt_info_desc(struct wgt_info *ifo)
388 struct wgt *wgt_info_wgt(struct wgt_info *ifo)
395 void wgt_info_addref(struct wgt_info *ifo)
398 assert(ifo->refcount > 0);
402 void wgt_info_unref(struct wgt_info *ifo)
405 assert(ifo->refcount > 0);
409 free_desc(&ifo->desc);
414 void wgt_info_dump(struct wgt_info *ifo, int fd, const char *prefix)
421 WARNING("can't fdopen in wgt_info_dump");
423 dump_desc(&ifo->desc, f, prefix);
428 const struct wgt_desc_feature *wgt_info_feature(struct wgt_info *ifo, const char *name)
430 const struct wgt_desc_feature *result = ifo->desc.features;
431 while(result && strcmp(result->name, name))
432 result = result->next;
436 const char *wgt_info_param(const struct wgt_desc_feature *feature, const char *name)
438 const struct wgt_desc_param *param = feature->params;
440 if (0 == strcmp(name, param->name))