41c208530052ca7e7daad99bbc04e28089233d8b
[src/app-framework-main.git] / wgt-config-xml.c
1 /*
2  Copyright 2015 IoT.bzh
3
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
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #include <string.h>
18 #include <syslog.h>
19 #include <assert.h>
20 #include <errno.h>
21
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <libxml/uri.h>
25
26
27 #include "wgt.h"
28
29 static xmlDocPtr configxml = NULL;
30
31 static xmlNodePtr next(xmlNodePtr node, const char *type)
32 {
33         while (node && node->type != XML_ELEMENT_NODE && strcmp(type, node->name))
34                 node = node->next;
35         return node;
36 }
37
38 static xmlNodePtr first(const char *type)
39 {
40         xmlNodePtr root;
41         if (configxml) {
42                 root = xmlDocGetRootElement(configxml);
43                 if (root)
44                         return next(root->children, type);
45         }
46         return NULL;
47 }
48
49 static xmlNodePtr element_based_localisation(const char *type)
50 {
51         xmlNodePtr resu;
52         char *lang;
53
54         resu = first(type);
55         while (resu) {
56                 lang = xmlNodeGetLang(resu);
57                 if (lang) {
58                         xmlFree(lang);
59                 }
60                 resu = next(resu->next, type);
61         }
62         return resu;
63 }
64
65 void confixml_close()
66 {
67         if (configxml) {
68                 xmlFreeDoc(configxml);
69                 configxml = NULL;
70         }
71 }
72
73 int confixml_open()
74 {
75         assert(!configxml);
76         configxml = xmlReadFile(_config_xml_, NULL, 0);
77         if (configxml == NULL) {
78                 syslog(LOG_ERR, "xml parse of config file %s failed", _config_xml_);
79                 return -1;
80         }
81         return 0;
82 }
83
84 /* elements based on localisation */
85 xmlNodePtr confixml_name()
86 {
87         return element_based_localisation(_name_);
88 }
89
90 xmlNodePtr confixml_description()
91 {
92         return element_based_localisation(_description_);
93 }
94
95 xmlNodePtr confixml_license()
96 {
97         return element_based_localisation(_license_);
98 }
99
100 /* elements based on path localisation */
101 xmlNodePtr confixml_author()
102 {
103         return first(_author_);
104 }
105
106 xmlNodePtr confixml_content()
107 {
108         return first(_content_);
109 }
110
111 /* element multiple */
112
113 xmlNodePtr confixml_first_feature()
114 {
115         return first(_feature_);
116 }
117
118 xmlNodePtr confixml_next_feature(xmlNodePtr node)
119 {
120         return next(node->next, _feature_);
121 }
122
123 xmlNodePtr confixml_first_preference()
124 {
125         return first(_preference_);
126 }
127
128 xmlNodePtr confixml_next_preference(xmlNodePtr node)
129 {
130         return next(node->next, _preference_);
131 }
132
133 xmlNodePtr confixml_first_icon()
134 {
135         return first(_icon_);
136 }
137
138 xmlNodePtr confixml_next_icon(xmlNodePtr node)
139 {
140         return next(node->next, _icon_);
141 }
142
143 /* best sized icon */
144
145 static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
146 {
147         int r, iref, ix;
148         char *sref, *sx;
149
150         sref = xmlGetProp(ref, dim);
151         if (sref) {
152                 iref = atoi(sref);
153                 xmlFree(sref);
154                 sx = xmlGetProp(x, dim);
155                 if (sx) {
156                         /* sref && sx */
157                         ix = atoi(sx);
158                         xmlFree(sx);
159                         if (ix >= request) {
160                                 if (iref >= request)
161                                         r = ix - iref;
162                                 else
163                                         r = request - ix;
164                         } else {
165                                 if (iref >= request)
166                                         r = iref - request;
167                                 else
168                                         r = iref - ix;
169                         }
170                 } else {
171                         /* sref && !sx */
172                         if (iref >= request)
173                                 r = iref - request;
174                         else
175                                 r = 0;
176                 }
177         } else {
178                 sx = xmlGetProp(x, dim);
179                 if (sx) {
180                         /* !sref && sx */
181                         ix = atoi(sx);
182                         xmlFree(sx);
183                         if (ix >= request)
184                                 r = request - ix;
185                         else
186                                 r = 0;
187                 } else {
188                         /* !sref && !sx */
189                         r = 0;
190                 }
191         }
192         return r;
193 }
194
195 static int better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
196 {
197         int sw = score_dim(ref, x, _width_, width);
198         int sh = score_dim(ref, x, _height_, height);
199         return sw+sh < 0;
200 }
201
202 xmlNodePtr confixml_icon(int width, int height)
203 {
204         xmlNodePtr resu, icon;
205
206         resu = confixml_first_icon();
207         icon = confixml_next_icon(resu);
208         while (icon) {
209                 if (better_icon(resu, icon, width, height))
210                         resu = icon;
211                 icon = confixml_next_icon(icon);
212         }
213         return resu;
214 }
215