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/parser.h>
25 #include <libxml/tree.h>
26 #include <libxml/uri.h>
30 #include "wgt-config.h"
32 const char wgt_config_string_author[] = "author";
33 const char wgt_config_string_content[] = "content";
34 const char wgt_config_string_defaultlocale[] = "defaultlocale";
35 const char wgt_config_string_description[] = "description";
36 const char wgt_config_string_email[] = "email";
37 const char wgt_config_string_encoding[] = "encoding";
38 const char wgt_config_string_feature[] = "feature";
39 const char wgt_config_string_height[] = "height";
40 const char wgt_config_string_href[] = "href";
41 const char wgt_config_string_icon[] = "icon";
42 const char wgt_config_string_id[] = "id";
43 const char wgt_config_string_license[] = "license";
44 const char wgt_config_string_name[] = "name";
45 const char wgt_config_string_param[] = "param";
46 const char wgt_config_string_preference[] = "preference";
47 const char wgt_config_string_readonly[] = "readonly";
48 const char wgt_config_string_required[] = "required";
49 const char wgt_config_string_short[] = "short";
50 const char wgt_config_string_src[] = "src";
51 const char wgt_config_string_type[] = "type";
52 const char wgt_config_string_value[] = "value";
53 const char wgt_config_string_version[] = "version";
54 const char wgt_config_string_viewmodes[] = "viewmodes";
55 const char wgt_config_string_widget[] = "widget";
56 const char wgt_config_string_width[] = "width";
57 const char wgt_config_string_xml_file[] = "config.xml";
59 static struct wgt *configwgt = NULL;
60 static xmlDocPtr configxml = NULL;
62 static xmlNodePtr next(xmlNodePtr node, const char *type)
64 while (node && (node->type != XML_ELEMENT_NODE || strcmp(type, node->name)))
69 static xmlNodePtr first(const char *type)
72 assert(xmlDocGetRootElement(configxml));
73 return next(xmlDocGetRootElement(configxml)->children, type);
76 static int scorelang(xmlNodePtr node)
78 char *lang = xmlNodeGetLang(node);
79 int score = wgt_locales_score(configwgt, lang);
84 static xmlNodePtr element_based_localisation(const char *type)
86 xmlNodePtr resu, elem;
92 elem = next(resu->next, type);
99 elem = next(elem->next, type);
105 void wgt_config_close()
108 xmlFreeDoc(configxml);
114 int wgt_config_open(struct wgt *wgt)
118 fd = wgt_open_read(wgt, wgt_config_string_xml_file);
120 ERROR("can't open config file %s", wgt_config_string_xml_file);
123 configxml = xmlReadFd(fd, wgt_config_string_xml_file, NULL, 0);
125 if (configxml == NULL) {
126 ERROR("xml parse of config file %s failed", wgt_config_string_xml_file);
129 assert(xmlDocGetRootElement(configxml));
134 xmlNodePtr wgt_config_widget()
138 root = xmlDocGetRootElement(configxml);
139 return strcmp(wgt_config_string_widget, root->name) ? NULL : root;
142 /* elements based on localisation */
143 xmlNodePtr wgt_config_name()
146 return element_based_localisation(wgt_config_string_name);
149 xmlNodePtr wgt_config_description()
152 return element_based_localisation(wgt_config_string_description);
155 xmlNodePtr wgt_config_license()
158 return element_based_localisation(wgt_config_string_license);
161 /* elements based on path localisation */
162 xmlNodePtr wgt_config_author()
165 return first(wgt_config_string_author);
168 xmlNodePtr wgt_config_content()
171 return first(wgt_config_string_content);
174 /* element multiple */
176 xmlNodePtr wgt_config_first_feature()
179 return first(wgt_config_string_feature);
182 xmlNodePtr wgt_config_next_feature(xmlNodePtr node)
186 return next(node->next, wgt_config_string_feature);
189 xmlNodePtr wgt_config_first_preference()
192 return first(wgt_config_string_preference);
195 xmlNodePtr wgt_config_next_preference(xmlNodePtr node)
199 return next(node->next, wgt_config_string_preference);
202 xmlNodePtr wgt_config_first_icon()
205 return first(wgt_config_string_icon);
208 xmlNodePtr wgt_config_next_icon(xmlNodePtr node)
212 return next(node->next, wgt_config_string_icon);
215 xmlNodePtr wgt_config_first_param(xmlNodePtr node)
219 return next(node->children, wgt_config_string_param);
222 xmlNodePtr wgt_config_next_param(xmlNodePtr node)
226 return next(node->next, wgt_config_string_param);
229 /* best sized icon */
231 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
236 sref = xmlGetProp(ref, dim);
240 sx = xmlGetProp(x, dim);
264 sx = xmlGetProp(x, dim);
281 static int is_better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
283 int sw = score_dim(ref, x, wgt_config_string_width, width);
284 int sh = score_dim(ref, x, wgt_config_string_height, height);
288 xmlNodePtr wgt_config_icon(int width, int height)
291 xmlNodePtr resu, icon;
293 resu = wgt_config_first_icon();
294 icon = wgt_config_next_icon(resu);
296 if (is_better_icon(resu, icon, width, height))
298 icon = wgt_config_next_icon(icon);