2 Copyright (C) 2015-2019 IoT.bzh
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"
31 #include "wgt-strings.h"
34 static struct wgt *configwgt = NULL;
35 static xmlDocPtr configxml = NULL;
37 static xmlNodePtr next(xmlNodePtr node, const char *type)
39 while (node && (node->type != XML_ELEMENT_NODE || strcmp(type, node->name)))
44 static xmlNodePtr first(const char *type)
47 assert(xmlDocGetRootElement(configxml));
48 return next(xmlDocGetRootElement(configxml)->children, type);
51 static unsigned int scorelang(xmlNodePtr node)
53 char *lang = xmlNodeGetLang(node);
54 unsigned int score = wgt_locales_score(configwgt, lang);
59 static xmlNodePtr element_based_localisation(const char *type)
61 xmlNodePtr resu, elem;
67 elem = next(resu->next, type);
74 elem = next(elem->next, type);
80 void wgt_config_close()
83 xmlFreeDoc(configxml);
89 int wgt_config_open(struct wgt *wgt)
93 fd = wgt_open_read(wgt, string_config_dot_xml);
95 ERROR("can't open config file %s", string_config_dot_xml);
98 configxml = xmlReadFd(fd, string_config_dot_xml, NULL, 0);
100 if (configxml == NULL) {
101 ERROR("xml parse of config file %s failed", string_config_dot_xml);
104 assert(xmlDocGetRootElement(configxml));
109 xmlNodePtr wgt_config_widget()
113 root = xmlDocGetRootElement(configxml);
114 return strcmp(string_widget, root->name) ? NULL : root;
117 /* elements based on localisation */
118 xmlNodePtr wgt_config_name()
121 return element_based_localisation(string_name);
124 xmlNodePtr wgt_config_description()
127 return element_based_localisation(string_description);
130 xmlNodePtr wgt_config_license()
133 return element_based_localisation(string_license);
136 /* elements based on path localisation */
137 xmlNodePtr wgt_config_author()
140 return first(string_author);
143 xmlNodePtr wgt_config_content()
146 return first(string_content);
149 /* element multiple */
151 xmlNodePtr wgt_config_first_feature()
154 return first(string_feature);
157 xmlNodePtr wgt_config_next_feature(xmlNodePtr node)
161 return next(node->next, string_feature);
164 xmlNodePtr wgt_config_first_preference()
167 return first(string_preference);
170 xmlNodePtr wgt_config_next_preference(xmlNodePtr node)
174 return next(node->next, string_preference);
177 xmlNodePtr wgt_config_first_icon()
180 return first(string_icon);
183 xmlNodePtr wgt_config_next_icon(xmlNodePtr node)
187 return next(node->next, string_icon);
190 xmlNodePtr wgt_config_first_param(xmlNodePtr node)
194 return next(node->children, string_param);
197 xmlNodePtr wgt_config_next_param(xmlNodePtr node)
201 return next(node->next, string_param);
204 /* best sized icon */
206 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
211 sref = xmlGetProp(ref, dim);
215 sx = xmlGetProp(x, dim);
239 sx = xmlGetProp(x, dim);
256 static int is_better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
258 int sw = score_dim(ref, x, string_width, width);
259 int sh = score_dim(ref, x, string_height, height);
263 xmlNodePtr wgt_config_icon(int width, int height)
266 xmlNodePtr resu, icon;
268 resu = wgt_config_first_icon();
269 icon = wgt_config_next_icon(resu);
271 if (is_better_icon(resu, icon, width, height))
273 icon = wgt_config_next_icon(icon);