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.
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 #include <libxml/uri.h>
29 #include "wgt-config.h"
31 const char wgt_config_string_author[] = "author";
32 const char wgt_config_string_content[] = "content";
33 const char wgt_config_string_defaultlocale[] = "defaultlocale";
34 const char wgt_config_string_description[] = "description";
35 const char wgt_config_string_email[] = "email";
36 const char wgt_config_string_encoding[] = "encoding";
37 const char wgt_config_string_feature[] = "feature";
38 const char wgt_config_string_height[] = "height";
39 const char wgt_config_string_href[] = "href";
40 const char wgt_config_string_icon[] = "icon";
41 const char wgt_config_string_id[] = "id";
42 const char wgt_config_string_license[] = "license";
43 const char wgt_config_string_name[] = "name";
44 const char wgt_config_string_param[] = "param";
45 const char wgt_config_string_preference[] = "preference";
46 const char wgt_config_string_readonly[] = "readonly";
47 const char wgt_config_string_required[] = "required";
48 const char wgt_config_string_short[] = "short";
49 const char wgt_config_string_src[] = "src";
50 const char wgt_config_string_type[] = "type";
51 const char wgt_config_string_value[] = "value";
52 const char wgt_config_string_version[] = "version";
53 const char wgt_config_string_viewmodes[] = "viewmodes";
54 const char wgt_config_string_widget[] = "widget";
55 const char wgt_config_string_width[] = "width";
56 const char wgt_config_string_xml_file[] = "config.xml";
58 static struct wgt *configwgt = NULL;
59 static xmlDocPtr configxml = NULL;
61 static xmlNodePtr next(xmlNodePtr node, const char *type)
63 while (node && (node->type != XML_ELEMENT_NODE || strcmp(type, node->name)))
68 static xmlNodePtr first(const char *type)
71 assert(xmlDocGetRootElement(configxml));
72 return next(xmlDocGetRootElement(configxml)->children, type);
75 static int scorelang(xmlNodePtr node)
77 char *lang = xmlNodeGetLang(node);
78 int score = wgt_locales_score(configwgt, lang);
83 static xmlNodePtr element_based_localisation(const char *type)
85 xmlNodePtr resu, elem;
91 elem = next(resu->next, type);
98 elem = next(elem->next, type);
104 void wgt_config_close()
107 xmlFreeDoc(configxml);
113 int wgt_config_open(struct wgt *wgt)
117 fd = wgt_open_read(wgt, wgt_config_string_xml_file);
119 ERROR("can't open config file %s", wgt_config_string_xml_file);
122 configxml = xmlReadFd(fd, wgt_config_string_xml_file, NULL, 0);
124 if (configxml == NULL) {
125 ERROR("xml parse of config file %s failed", wgt_config_string_xml_file);
128 assert(xmlDocGetRootElement(configxml));
133 xmlNodePtr wgt_config_widget()
137 root = xmlDocGetRootElement(configxml);
138 return strcmp(wgt_config_string_widget, root->name) ? NULL : root;
141 /* elements based on localisation */
142 xmlNodePtr wgt_config_name()
145 return element_based_localisation(wgt_config_string_name);
148 xmlNodePtr wgt_config_description()
151 return element_based_localisation(wgt_config_string_description);
154 xmlNodePtr wgt_config_license()
157 return element_based_localisation(wgt_config_string_license);
160 /* elements based on path localisation */
161 xmlNodePtr wgt_config_author()
164 return first(wgt_config_string_author);
167 xmlNodePtr wgt_config_content()
170 return first(wgt_config_string_content);
173 /* element multiple */
175 xmlNodePtr wgt_config_first_feature()
178 return first(wgt_config_string_feature);
181 xmlNodePtr wgt_config_next_feature(xmlNodePtr node)
185 return next(node->next, wgt_config_string_feature);
188 xmlNodePtr wgt_config_first_preference()
191 return first(wgt_config_string_preference);
194 xmlNodePtr wgt_config_next_preference(xmlNodePtr node)
198 return next(node->next, wgt_config_string_preference);
201 xmlNodePtr wgt_config_first_icon()
204 return first(wgt_config_string_icon);
207 xmlNodePtr wgt_config_next_icon(xmlNodePtr node)
211 return next(node->next, wgt_config_string_icon);
214 xmlNodePtr wgt_config_first_param(xmlNodePtr node)
218 return next(node->children, wgt_config_string_param);
221 xmlNodePtr wgt_config_next_param(xmlNodePtr node)
225 return next(node->next, wgt_config_string_param);
228 /* best sized icon */
230 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
235 sref = xmlGetProp(ref, dim);
239 sx = xmlGetProp(x, dim);
263 sx = xmlGetProp(x, dim);
280 static int is_better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
282 int sw = score_dim(ref, x, wgt_config_string_width, width);
283 int sh = score_dim(ref, x, wgt_config_string_height, height);
287 xmlNodePtr wgt_config_icon(int width, int height)
290 xmlNodePtr resu, icon;
292 resu = wgt_config_first_icon();
293 icon = wgt_config_next_icon(resu);
295 if (is_better_icon(resu, icon, width, height))
297 icon = wgt_config_next_icon(icon);