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/parser.h>
23 #include <libxml/tree.h>
24 #include <libxml/uri.h>
28 #include "wgt-config.h"
30 const char wgt_config_string_author[] = "author";
31 const char wgt_config_string_content[] = "content";
32 const char wgt_config_string_defaultlocale[] = "defaultlocale";
33 const char wgt_config_string_description[] = "description";
34 const char wgt_config_string_email[] = "email";
35 const char wgt_config_string_encoding[] = "encoding";
36 const char wgt_config_string_feature[] = "feature";
37 const char wgt_config_string_height[] = "height";
38 const char wgt_config_string_href[] = "href";
39 const char wgt_config_string_icon[] = "icon";
40 const char wgt_config_string_id[] = "id";
41 const char wgt_config_string_license[] = "license";
42 const char wgt_config_string_name[] = "name";
43 const char wgt_config_string_param[] = "param";
44 const char wgt_config_string_preference[] = "preference";
45 const char wgt_config_string_readonly[] = "readonly";
46 const char wgt_config_string_required[] = "required";
47 const char wgt_config_string_short[] = "short";
48 const char wgt_config_string_src[] = "src";
49 const char wgt_config_string_type[] = "type";
50 const char wgt_config_string_value[] = "value";
51 const char wgt_config_string_version[] = "version";
52 const char wgt_config_string_viewmodes[] = "viewmodes";
53 const char wgt_config_string_widget[] = "widget";
54 const char wgt_config_string_width[] = "width";
55 const char wgt_config_string_xml_file[] = "config.xml";
57 static struct wgt *configwgt = NULL;
58 static xmlDocPtr configxml = NULL;
60 static xmlNodePtr next(xmlNodePtr node, const char *type)
62 while (node && (node->type != XML_ELEMENT_NODE || strcmp(type, node->name)))
67 static xmlNodePtr first(const char *type)
70 assert(xmlDocGetRootElement(configxml));
71 return next(xmlDocGetRootElement(configxml)->children, type);
74 static int scorelang(xmlNodePtr node)
76 char *lang = xmlNodeGetLang(node);
77 int score = wgt_locales_score(configwgt, lang);
82 static xmlNodePtr element_based_localisation(const char *type)
84 xmlNodePtr resu, elem;
90 elem = next(resu->next, type);
97 elem = next(elem->next, type);
103 void wgt_config_close()
106 xmlFreeDoc(configxml);
112 int wgt_config_open(struct wgt *wgt)
116 fd = wgt_open_read(wgt, wgt_config_string_xml_file);
118 ERROR("can't open config file %s", wgt_config_string_xml_file);
121 configxml = xmlReadFd(fd, wgt_config_string_xml_file, NULL, 0);
123 if (configxml == NULL) {
124 ERROR("xml parse of config file %s failed", wgt_config_string_xml_file);
127 assert(xmlDocGetRootElement(configxml));
132 xmlNodePtr wgt_config_widget()
136 root = xmlDocGetRootElement(configxml);
137 return strcmp(wgt_config_string_widget, root->name) ? NULL : root;
140 /* elements based on localisation */
141 xmlNodePtr wgt_config_name()
144 return element_based_localisation(wgt_config_string_name);
147 xmlNodePtr wgt_config_description()
150 return element_based_localisation(wgt_config_string_description);
153 xmlNodePtr wgt_config_license()
156 return element_based_localisation(wgt_config_string_license);
159 /* elements based on path localisation */
160 xmlNodePtr wgt_config_author()
163 return first(wgt_config_string_author);
166 xmlNodePtr wgt_config_content()
169 return first(wgt_config_string_content);
172 /* element multiple */
174 xmlNodePtr wgt_config_first_feature()
177 return first(wgt_config_string_feature);
180 xmlNodePtr wgt_config_next_feature(xmlNodePtr node)
184 return next(node->next, wgt_config_string_feature);
187 xmlNodePtr wgt_config_first_preference()
190 return first(wgt_config_string_preference);
193 xmlNodePtr wgt_config_next_preference(xmlNodePtr node)
197 return next(node->next, wgt_config_string_preference);
200 xmlNodePtr wgt_config_first_icon()
203 return first(wgt_config_string_icon);
206 xmlNodePtr wgt_config_next_icon(xmlNodePtr node)
210 return next(node->next, wgt_config_string_icon);
213 xmlNodePtr wgt_config_first_param(xmlNodePtr node)
217 return next(node->children, wgt_config_string_param);
220 xmlNodePtr wgt_config_next_param(xmlNodePtr node)
224 return next(node->next, wgt_config_string_param);
227 /* best sized icon */
229 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
234 sref = xmlGetProp(ref, dim);
238 sx = xmlGetProp(x, dim);
262 sx = xmlGetProp(x, dim);
279 static int is_better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
281 int sw = score_dim(ref, x, wgt_config_string_width, width);
282 int sh = score_dim(ref, x, wgt_config_string_height, height);
286 xmlNodePtr wgt_config_icon(int width, int height)
289 xmlNodePtr resu, icon;
291 resu = wgt_config_first_icon();
292 icon = wgt_config_next_icon(resu);
294 if (is_better_icon(resu, icon, width, height))
296 icon = wgt_config_next_icon(icon);