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>
28 #include "wgt-config.h"
30 const char wgt_config_string_xml_file[] = "config.xml";
31 const char wgt_config_string_name[] = "name";
32 const char wgt_config_string_description[] = "description";
33 const char wgt_config_string_author[] = "author";
34 const char wgt_config_string_license[] = "license";
35 const char wgt_config_string_icon[] = "icon";
36 const char wgt_config_string_content[] = "content";
37 const char wgt_config_string_feature[] = "feature";
38 const char wgt_config_string_preference[] = "preference";
39 const char wgt_config_string_width[] = "width";
40 const char wgt_config_string_height[] = "height";
43 static struct wgt *configwgt = NULL;
44 static xmlDocPtr configxml = NULL;
46 static xmlNodePtr next(xmlNodePtr node, const char *type)
48 while (node && node->type != XML_ELEMENT_NODE && strcmp(type, node->name))
53 static xmlNodePtr first(const char *type)
57 root = xmlDocGetRootElement(configxml);
59 return next(root->children, type);
64 static int scorelang(xmlNodePtr node)
66 char *lang = xmlNodeGetLang(node);
67 int score = wgt_locales_score(configwgt, lang);
72 static xmlNodePtr element_based_localisation(const char *type)
74 xmlNodePtr resu, elem;
80 elem = next(resu->next, type);
87 elem = next(elem->next, type);
93 void wgt_config_close()
96 xmlFreeDoc(configxml);
102 int wgt_config_open(struct wgt *wgt)
106 fd = wgt_open_read(wgt, wgt_config_string_xml_file);
108 syslog(LOG_ERR, "can't open config file %s", wgt_config_string_xml_file);
111 configxml = xmlReadFd(fd, wgt_config_string_xml_file, NULL, 0);
113 if (configxml == NULL) {
114 syslog(LOG_ERR, "xml parse of config file %s failed", wgt_config_string_xml_file);
121 /* elements based on localisation */
122 xmlNodePtr wgt_config_name()
124 return element_based_localisation(wgt_config_string_name);
127 xmlNodePtr wgt_config_description()
129 return element_based_localisation(wgt_config_string_description);
132 xmlNodePtr wgt_config_license()
134 return element_based_localisation(wgt_config_string_license);
137 /* elements based on path localisation */
138 xmlNodePtr wgt_config_author()
140 return first(wgt_config_string_author);
143 xmlNodePtr wgt_config_content()
145 return first(wgt_config_string_content);
148 /* element multiple */
150 xmlNodePtr wgt_config_first_feature()
152 return first(wgt_config_string_feature);
155 xmlNodePtr wgt_config_next_feature(xmlNodePtr node)
157 return next(node->next, wgt_config_string_feature);
160 xmlNodePtr wgt_config_first_preference()
162 return first(wgt_config_string_preference);
165 xmlNodePtr wgt_config_next_preference(xmlNodePtr node)
167 return next(node->next, wgt_config_string_preference);
170 xmlNodePtr wgt_config_first_icon()
172 return first(wgt_config_string_icon);
175 xmlNodePtr wgt_config_next_icon(xmlNodePtr node)
177 return next(node->next, wgt_config_string_icon);
180 /* best sized icon */
182 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
187 sref = xmlGetProp(ref, dim);
191 sx = xmlGetProp(x, dim);
215 sx = xmlGetProp(x, dim);
232 static int better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
234 int sw = score_dim(ref, x, wgt_config_string_width, width);
235 int sh = score_dim(ref, x, wgt_config_string_height, height);
239 xmlNodePtr wgt_config_icon(int width, int height)
241 xmlNodePtr resu, icon;
243 resu = wgt_config_first_icon();
244 icon = wgt_config_next_icon(resu);
246 if (better_icon(resu, icon, width, height))
248 icon = wgt_config_next_icon(icon);