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.
22 #include <libxml/tree.h>
26 #include "wgt-config.h"
35 static int getpropbool(xmlNodePtr node, const char *prop, int def)
38 char *val = xmlGetProp(node, prop);
42 if (!strcmp(val, "true"))
44 else if (!strcmp(val, "false"))
53 static int getpropnum(xmlNodePtr node, const char *prop, int def)
56 char *val = xmlGetProp(node, prop);
66 static xmlChar *optprop(xmlNodePtr node, const char *prop)
68 return node ? xmlGetProp(node, prop) : NULL;
71 static xmlChar *optcontent(xmlNodePtr node)
73 return node ? xmlNodeGetContent(node) : NULL;
76 static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, int want_preferences)
78 xmlNodePtr node, pnode;
79 struct wgt_desc_icon *icon, **icontail;
80 struct wgt_desc_feature *feature, **featuretail;
81 struct wgt_desc_preference *preference, **preferencetail;
82 struct wgt_desc_param *param, **paramtail;
84 node = wgt_config_widget();
90 desc->id = xmlGetProp(node, wgt_config_string_id);
91 desc->version = xmlGetProp(node, wgt_config_string_version);
92 desc->width = getpropnum(node, wgt_config_string_width, 0);
93 desc->height = getpropnum(node, wgt_config_string_height, 0);
94 desc->viewmodes = xmlGetProp(node, wgt_config_string_viewmodes);
95 desc->defaultlocale = xmlGetProp(node, wgt_config_string_defaultlocale);
97 node = wgt_config_name();
98 desc->name = optcontent(node);
99 desc->name_short = optprop(node, wgt_config_string_short);
101 node = wgt_config_description();
102 desc->description = optcontent(node);
104 node = wgt_config_author();
105 desc->author = optcontent(node);
106 desc->author_href = optprop(node, wgt_config_string_href);
107 desc->author_email = optprop(node, wgt_config_string_email);
109 node = wgt_config_license();
110 desc->license = optcontent(node);
111 desc->license_href = optprop(node, wgt_config_string_href);
113 node = wgt_config_content();
114 desc->content_src = optprop(node, wgt_config_string_src);
115 if (node && desc->content_src == NULL) {
116 WARNING("content without src");
120 desc->content_type = optprop(node, wgt_config_string_type);
121 desc->content_encoding = optprop(node, wgt_config_string_encoding);
124 icontail = &desc->icons;
125 node = wgt_config_first_icon();
127 icon = malloc(sizeof * icon);
132 icon->src = xmlGetProp(node, wgt_config_string_src);
133 icon->width = getpropnum(node, wgt_config_string_width, 0);
134 icon->height = getpropnum(node, wgt_config_string_height, 0);
139 if (icon->src == NULL) {
140 WARNING("icon without src");
144 icontail = &icon->next;
145 node = wgt_config_next_icon(node);
150 featuretail = &desc->features;
151 node = wgt_config_first_feature();
153 feature = malloc(sizeof * feature);
154 if (feature == NULL) {
158 feature->name = xmlGetProp(node, wgt_config_string_name);
159 feature->required = getpropbool(node, wgt_config_string_required, 1);
160 feature->params = NULL;
162 feature->next = NULL;
163 *featuretail = feature;
165 if (feature->name == NULL) {
166 WARNING("feature without name");
171 paramtail = &feature->params;
172 pnode = wgt_config_first_param(node);
174 param = malloc(sizeof * param);
179 param->name = xmlGetProp(pnode, wgt_config_string_name);
180 param->value = xmlGetProp(pnode, wgt_config_string_value);
185 if (param->name == NULL || param->value == NULL) {
186 WARNING("param without name or value");
191 paramtail = ¶m->next;
192 pnode = wgt_config_next_param(pnode);
195 featuretail = &feature->next;
196 node = wgt_config_next_feature(node);
200 if (want_preferences) {
201 preferencetail = &desc->preferences;
202 node = wgt_config_first_preference();
204 preference = malloc(sizeof * preference);
205 if (preference == NULL) {
209 preference->name = xmlGetProp(node, wgt_config_string_name);
210 preference->value = xmlGetProp(node, wgt_config_string_value);
211 preference->readonly = getpropbool(node, wgt_config_string_readonly, 0);
213 *preferencetail = preference;
214 preference->next = NULL;
216 if (preference->name == NULL) {
217 WARNING("preference without name");
222 preferencetail = &preference->next;
223 node = wgt_config_next_preference(node);
229 static void free_desc(struct wgt_desc *desc)
231 struct wgt_desc_icon *icon;
232 struct wgt_desc_feature *feature;
233 struct wgt_desc_preference *preference;
234 struct wgt_desc_param *param;
237 xmlFree(desc->version);
238 xmlFree(desc->viewmodes);
239 xmlFree(desc->defaultlocale);
241 xmlFree(desc->name_short);
242 xmlFree(desc->description);
243 xmlFree(desc->author);
244 xmlFree(desc->author_href);
245 xmlFree(desc->author_email);
246 xmlFree(desc->license);
247 xmlFree(desc->license_href);
248 xmlFree(desc->content_src);
249 xmlFree(desc->content_type);
250 xmlFree(desc->content_encoding);
254 desc->icons = icon->next;
259 while(desc->features) {
260 feature = desc->features;
261 desc->features = feature->next;
262 xmlFree(feature->name);
263 while(feature->params) {
264 param = feature->params;
265 feature->params = param->next;
266 xmlFree(param->name);
267 xmlFree(param->value);
273 while(desc->preferences) {
274 preference = desc->preferences;
275 desc->preferences = preference->next;
276 xmlFree(preference->name);
277 xmlFree(preference->value);
282 static void dump_desc(struct wgt_desc *desc, FILE *f, const char *prefix)
284 struct wgt_desc_icon *icon;
285 struct wgt_desc_feature *feature;
286 struct wgt_desc_preference *preference;
287 struct wgt_desc_param *param;
289 if (desc->id) fprintf(f, "%sid: %s\n", prefix, desc->id);
290 if (desc->width) fprintf(f, "%swidth: %d\n", prefix, desc->width);
291 if (desc->height) fprintf(f, "%sheight: %d\n", prefix, desc->height);
292 if (desc->version) fprintf(f, "%sversion: %s\n", prefix, desc->version);
293 if (desc->viewmodes) fprintf(f, "%sviewmodes: %s\n", prefix, desc->viewmodes);
294 if (desc->defaultlocale) fprintf(f, "%sdefaultlocale: %s\n", prefix, desc->defaultlocale);
295 if (desc->name) fprintf(f, "%sname: %s\n", prefix, desc->name);
296 if (desc->name_short) fprintf(f, "%sname_short: %s\n", prefix, desc->name_short);
297 if (desc->description) fprintf(f, "%sdescription: %s\n", prefix, desc->description);
298 if (desc->author) fprintf(f, "%sauthor: %s\n", prefix, desc->author);
299 if (desc->author_href) fprintf(f, "%sauthor_href: %s\n", prefix, desc->author_href);
300 if (desc->author_email) fprintf(f, "%sauthor_email: %s\n", prefix, desc->author_email);
301 if (desc->license) fprintf(f, "%slicense: %s\n", prefix, desc->license);
302 if (desc->license_href) fprintf(f, "%slicense_href: %s\n", prefix, desc->license_href);
303 if (desc->content_src) fprintf(f, "%scontent_src: %s\n", prefix, desc->content_src);
304 if (desc->content_type) fprintf(f, "%scontent_type: %s\n", prefix, desc->content_type);
305 if (desc->content_encoding) fprintf(f, "%scontent_encoding: %s\n", prefix, desc->content_encoding);
309 fprintf(f, "%s+ icon src: %s\n", prefix, icon->src);
310 if (icon->width) fprintf(f, "%s width: %d\n", prefix, icon->width);
311 if (icon->height) fprintf(f, "%s height: %d\n", prefix, icon->height);
315 feature = desc->features;
317 fprintf(f, "%s+ feature name: %s\n", prefix, feature->name);
318 fprintf(f, "%s required: %s\n", prefix, feature->required ? "true" : "false");
319 param = feature->params;
321 fprintf(f, "%s + param name: %s\n", prefix, param->name);
322 fprintf(f, "%s value: %s\n", prefix, param->value);
325 feature = feature->next;
328 preference = desc->preferences;
330 fprintf(f, "%s+ preference name: %s\n", prefix, preference->name);
331 if (preference->value) fprintf(f, "%s value: %s\n", prefix, preference->value);
332 fprintf(f, "%s readonly: %s\n", prefix, preference->readonly ? "true" : "false");
333 preference = preference->next;
337 struct wgt_info *wgt_info_create(struct wgt *wgt, int icons, int features, int preferences)
340 struct wgt_info *result;
343 assert(wgt_is_connected(wgt));
344 rc = wgt_config_open(wgt);
350 result = calloc(sizeof * result, 1);
356 result->refcount = 1;
360 rc = fill_desc(&result->desc, icons, features, preferences);
363 wgt_info_unref(result);
369 struct wgt_info *wgt_info_createat(int dirfd, const char *pathname, int icons, int features, int preferences)
371 struct wgt_info *result = NULL;
372 struct wgt *wgt = wgt_createat(dirfd, pathname);
374 result = wgt_info_create(wgt, icons, features, preferences);
380 const struct wgt_desc *wgt_info_desc(struct wgt_info *ifo)
386 struct wgt *wgt_info_wgt(struct wgt_info *ifo)
393 void wgt_info_addref(struct wgt_info *ifo)
396 assert(ifo->refcount > 0);
400 void wgt_info_unref(struct wgt_info *ifo)
403 assert(ifo->refcount > 0);
407 free_desc(&ifo->desc);
412 void wgt_info_dump(struct wgt_info *ifo, int fd, const char *prefix)
419 WARNING("can't fdopen in wgt_info_dump");
421 dump_desc(&ifo->desc, f, prefix);
426 const struct wgt_desc_feature *wgt_info_feature(struct wgt_info *ifo, const char *name)
428 const struct wgt_desc_feature *result = ifo->desc.features;
429 while(result && strcmp(result->name, name))
430 result = result->next;
434 const char *wgt_info_param(const struct wgt_desc_feature *feature, const char *name)
436 const struct wgt_desc_param *param = feature->params;
438 if (0 == strcmp(name, param->name))