2fbd0f6ddad5696194bcb5200a7142c90bbd1590
[framework/web/wrt-commons.git] / modules / widget_dao / dao / config_parser_data.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        config_parser_data.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22 #include <stddef.h>
23 #include <dpl/wrt-dao-ro/config_parser_data.h>
24 #include <dpl/log/log.h>
25 #include <libxml/xmlreader.h>
26 #include <libxml/xmlstring.h>
27
28 namespace WrtDB {
29 bool IsSpace(const xmlChar* str);
30 bool CopyChar(xmlChar* out, xmlChar* in);
31
32
33 bool IsSpace(const xmlChar* str)
34 {
35     int charlen = xmlUTF8Size(str);
36
37     switch (charlen) {
38     case 1:
39         switch (*str) {
40         case 0x09:
41         case 0x0a:
42         case 0x0b:
43         case 0x0c:
44         case 0x0d:
45         case 0x20:
46             return true;
47
48         default:
49             return false;
50         }
51
52     case 2:
53         switch (*(str + 1)) {
54         case 0x85:
55         case 0xa0:
56             return true;
57
58         default:
59             return false;
60         }
61
62     case 3:
63         switch (*str) {
64         case 0xe1:
65         {
66             unsigned char c2 = *(str + 1);
67             unsigned char c3 = *(str + 2);
68             if ((c2 == 0x9a && c3 == 0x80) || (c2 == 0xa0 && c3 == 0x8e)) {
69                 return true;
70             } else {
71                 return false;
72             }
73         }
74
75         case 0xe2:
76             switch (*(str + 1)) {
77             case 0x80:
78                 switch (*(str + 2)) {
79                 case 0x80:
80                 case 0x81:
81                 case 0x82:
82                 case 0x83:
83                 case 0x84:
84                 case 0x85:
85                 case 0x86:
86                 case 0x87:
87                 case 0x88:
88                 case 0x89:
89                 case 0x8a:
90                 case 0xa8:
91                 case 0xa9:
92                 case 0xaf:
93                     return true;
94                 default:
95                     return false;
96                 }
97             case 0x81:
98                 if (*(str + 2) == 0x9f) {
99                     return true;
100                 } else {
101                     return false;
102                 }
103
104             default:
105                 return false;
106             }
107         case 0xe3:
108             if (*(str + 1) == 0x80 && *(str + 2) == 0x80) {
109                 return true;
110             } else {
111                 return false;
112             }
113
114         default:
115             return false;
116         }
117
118     default:
119         return false;
120     }
121 }
122
123 bool CopyChar(xmlChar* out,
124         xmlChar* in)
125 {
126     int size = xmlUTF8Size(in);
127     switch (size) {
128     case 6:
129         out[5] = in[5];
130     case 5:
131         out[4] = in[4];
132     case 4:
133         out[3] = in[3];
134     case 3:
135         out[2] = in[2];
136     case 2:
137         out[1] = in[1];
138     case 1:
139         out[0] = in[0];
140         return true;
141
142     default:
143         return false;
144     }
145 }
146
147 //TODO temporary fix until commits the rewrite of this functionality.
148 void NormalizeString(DPL::String& str)
149 {
150     DPL::Optional<DPL::String> opt = str;
151     NormalizeString(opt);
152     str = *opt;
153 }
154
155 void NormalizeString (DPL::Optional<DPL::String>& txt)
156 {
157     if (!!txt) {
158         std::string tmp = DPL::ToUTF8String(*txt);
159         const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str());
160         if (!xmlCheckUTF8(str)) {
161             LogError("Not valid UTF8");
162             return;
163         }
164
165         int i = 0;
166         xmlChar* c;
167         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
168             if (!IsSpace(c)) {
169                 break;
170             }
171             ++i;
172         }
173
174         xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1);
175         bool first = false;
176         xmlChar* s = tmpnew;
177         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
178             if (IsSpace(c)) {
179                 first = true;
180                 ++i;
181             } else {
182                 if (c[0] == 0x0) {
183                     break;
184                 }
185                 if (first) {
186                     xmlChar space[6] = { 0x20 };
187                     CopyChar(s, space);
188                     s += xmlUTF8Size(s);
189                     first = false;
190                 }
191                 CopyChar(s, c);
192                 s += xmlUTF8Size(s);
193                 ++i;
194             }
195         }
196         s[0] = 0x0;
197         txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew));
198         xmlFree(tmpnew);
199     }
200 }
201
202 bool ConfigParserData::Param::operator==(const Param& other) const
203 {
204     return name == other.name && value == other.value;
205 }
206
207 bool ConfigParserData::Param::operator!=(const Param& other) const
208 {
209     return name != other.name || value != other.value;
210 }
211
212 bool ConfigParserData::Param::operator >(const Param& other) const
213 {
214     if (name == other.name) {
215         return value > other.value;
216     } else {
217         return name > other.name;
218     }
219 }
220
221 bool ConfigParserData::Param::operator>=(const Param& other) const
222 {
223     if (name >= other.name) {
224         return true;
225     } else {
226         return value >= other.value;
227     }
228 }
229
230 bool ConfigParserData::Param::operator <(const Param& other) const
231 {
232     if (name == other.name) {
233         return value < other.value;
234     } else {
235         return name < other.name;
236     }
237 }
238
239 bool ConfigParserData::Param::operator<=(const Param& other) const
240 {
241     if (name <= other.name) {
242         return true;
243     } else {
244         return value <= other.value;
245     }
246 }
247
248 bool ConfigParserData::Feature::operator==(const Feature& other) const
249 {
250     return name == other.name && paramsList == other.paramsList;
251 }
252
253 bool ConfigParserData::Feature::operator!=(const Feature& other) const
254 {
255     return name != other.name || paramsList != other.paramsList;
256 }
257
258 bool ConfigParserData::Feature::operator >(const Feature& other) const
259 {
260     if (name > other.name) {
261         return true;
262     }
263     if (name < other.name) {
264         return false;
265     }
266     return paramsList > other.paramsList;
267 }
268
269 bool ConfigParserData::Feature::operator>=(const Feature& other) const
270 {
271     if (name > other.name) {
272         return true;
273     }
274     if (name < other.name) {
275         return false;
276     }
277     return paramsList >= other.paramsList;
278 }
279
280 bool ConfigParserData::Feature::operator <(const Feature& other) const
281 {
282     if (name < other.name) {
283         return true;
284     }
285     if (name > other.name) {
286         return false;
287     }
288     return paramsList < other.paramsList;
289 }
290
291 bool ConfigParserData::Feature::operator<=(const Feature& other) const
292 {
293     if (name < other.name) {
294         return true;
295     }
296     if (name > other.name) {
297         return false;
298     }
299     return paramsList <= other.paramsList;
300 }
301
302 bool ConfigParserData::Privilege::operator==(const Privilege& other) const
303 {
304     return name == other.name;
305 }
306
307 bool ConfigParserData::Privilege::operator!=(const Privilege& other) const
308 {
309     return name != other.name;
310 }
311
312 bool ConfigParserData::Privilege::operator >(const Privilege& other) const
313 {
314     return name > other.name;
315 }
316
317 bool ConfigParserData::Privilege::operator>=(const Privilege& other) const
318 {
319     return name >= other.name;
320 }
321
322 bool ConfigParserData::Privilege::operator <(const Privilege& other) const
323 {
324     return name < other.name;
325 }
326
327 bool ConfigParserData::Privilege::operator<=(const Privilege& other) const
328 {
329     return name < other.name;
330 }
331
332 bool ConfigParserData::Icon::operator==(const Icon& other) const
333 {
334     return src == other.src;
335 }
336
337 bool ConfigParserData::Icon::operator!=(const Icon& other) const
338 {
339     return src != other.src;
340 }
341
342 bool ConfigParserData::Icon::operator >(const Icon& other) const
343 {
344     return src > other.src;
345 }
346
347 bool ConfigParserData::Icon::operator>=(const Icon& other) const
348 {
349     return src >= other.src;
350 }
351
352 bool ConfigParserData::Icon::operator <(const Icon& other) const
353 {
354     return src < other.src;
355 }
356
357 bool ConfigParserData::Icon::operator<=(const Icon& other) const
358 {
359     return src <= other.src;
360 }
361
362 bool ConfigParserData::Preference::operator==(const Preference& other) const
363 {
364     return name == other.name;
365 }
366
367 bool ConfigParserData::Preference::operator!=(const Preference& other) const
368 {
369     return name != other.name;
370 }
371
372 bool ConfigParserData::Preference::operator >(const Preference& other) const
373 {
374     return name > other.name;
375 }
376
377 bool ConfigParserData::Preference::operator>=(const Preference& other) const
378 {
379     return name >= other.name;
380 }
381
382 bool ConfigParserData::Preference::operator <(const Preference& other) const
383 {
384     return name < other.name;
385 }
386
387 bool ConfigParserData::Preference::operator<=(const Preference& other) const
388 {
389     return name <= other.name;
390 }
391
392 bool ConfigParserData::AccessInfo::operator== (const AccessInfo& info) const
393 {
394     return m_strIRI == info.m_strIRI && m_bSubDomainAccess ==
395            info.m_bSubDomainAccess;
396 }
397
398 bool ConfigParserData::AccessInfo::operator!= (const AccessInfo& info) const
399 {
400     return m_strIRI != info.m_strIRI || m_bSubDomainAccess !=
401            info.m_bSubDomainAccess;
402 }
403
404 bool ConfigParserData::AccessInfo::operator <(const AccessInfo& info) const
405 {
406     if (m_strIRI == info.m_strIRI) {
407         return m_bSubDomainAccess < info.m_bSubDomainAccess;
408     } else {
409         return m_strIRI < info.m_strIRI;
410     }
411 }
412
413 bool ConfigParserData::Setting::operator==(const Setting& other) const
414 {
415     return m_name == other.m_name &&
416         m_value == other.m_value;
417 }
418
419 bool ConfigParserData::Setting::operator!=(const Setting& other) const
420 {
421     return m_name != other.m_name ||
422         m_value != other.m_value;
423 }
424
425 bool ConfigParserData::Setting::operator >(const Setting& other) const
426 {
427     return m_name > other.m_name;
428 }
429
430 bool ConfigParserData::Setting::operator>=(const Setting& other) const
431 {
432     return m_name >= other.m_name;
433 }
434
435 bool ConfigParserData::Setting::operator <(const Setting& other) const
436 {
437     return m_name < other.m_name;
438 }
439
440 bool ConfigParserData::Setting::operator<=(const Setting& other) const
441 {
442     return m_name <= other.m_name;
443 }
444
445 bool ConfigParserData::ServiceInfo::operator== (const ServiceInfo& info) const
446 {
447     return m_src == info.m_src &&
448     m_operation == info.m_operation &&
449     m_scheme == info.m_scheme &&
450     m_mime == info.m_mime;
451 }
452
453 bool ConfigParserData::ServiceInfo::operator!= (const ServiceInfo& info) const
454 {
455     return m_src != info.m_src &&
456     m_operation != info.m_operation &&
457     m_scheme != info.m_scheme &&
458     m_mime != info.m_mime;
459 }
460
461 bool ConfigParserData::LiveboxInfo::operator==(const LiveboxInfo& other) const
462 {
463     return m_liveboxId == other.m_liveboxId &&
464         m_autoLaunch == other.m_autoLaunch &&
465         m_updatePeriod == other.m_updatePeriod &&
466         m_primary == other.m_primary &&
467         m_label == other.m_label &&
468         m_icon == other.m_icon;
469 }
470
471 bool ConfigParserData::LiveboxInfo::operator!=(const LiveboxInfo& other) const
472 {
473     return m_liveboxId != other.m_liveboxId &&
474          m_autoLaunch != other.m_autoLaunch &&
475         m_updatePeriod != other.m_updatePeriod &&
476         m_primary != other.m_primary &&
477         m_label != other.m_label &&
478         m_icon != other.m_icon;
479 }
480
481 } // namespace WrtDB