e2fd020c312b5e7980c44178264e29d9cc8399a1
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <dlfcn.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <libxml/parser.h>
28 #include <libxml/xmlreader.h>
29 #include <libxml/xmlschemas.h>
30 #include <vconf.h>
31
32
33 #include "pkgmgr_parser.h"
34 #include "pkgmgr_parser_internal.h"
35 #include "pkgmgr_parser_db.h"
36 #include "pkgmgr-info.h"
37
38 #define MANIFEST_RW_DIRECTORY "/opt/share/packages"
39 #define MANIFEST_RO_DIRECTORY "/usr/share/packages"
40 #define ASCII(s) (const char *)s
41 #define XMLCHAR(s) (const xmlChar *)s
42
43 /* operation_type */
44 typedef enum {
45         ACTION_INSTALL = 0,
46         ACTION_UPGRADE,
47         ACTION_UNINSTALL,
48         ACTION_MAX
49 } ACTION_TYPE;
50
51 char *package;
52
53 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label);
54 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile);
55 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed);
56 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation);
57 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri);
58 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime);
59 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp);
60 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition);
61 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notifiation);
62 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category);
63 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility);
64 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution);
65 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request);
66 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define);
67 static int __ps_process_registry(xmlTextReaderPtr reader, registry_x *registry);
68 static int __ps_process_database(xmlTextReaderPtr reader, database_x *database);
69 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc);
70 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions);
71 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare);
72 static int __ps_process_layout(xmlTextReaderPtr reader, layout_x *layout);
73 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon);
74 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author);
75 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description);
76 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability);
77 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license);
78 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol);
79 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol);
80 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication);
81 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication);
82 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font);
83 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme);
84 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon);
85 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime);
86 static void __ps_free_label(label_x *label);
87 static void __ps_free_deviceprofile(deviceprofile_x * deviceprofile);
88 static void __ps_free_allowed(allowed_x *allowed);
89 static void __ps_free_operation(operation_x *operation);
90 static void __ps_free_uri(uri_x *uri);
91 static void __ps_free_mime(mime_x *mime);
92 static void __ps_free_subapp(subapp_x *subapp);
93 static void __ps_free_condition(condition_x *condition);
94 static void __ps_free_notification(notification_x *notifiation);
95 static void __ps_free_category(category_x *category);
96 static void __ps_free_compatibility(compatibility_x *compatibility);
97 static void __ps_free_resolution(resolution_x *resolution);
98 static void __ps_free_request(request_x *request);
99 static void __ps_free_define(define_x *define);
100 static void __ps_free_registry(registry_x *registry);
101 static void __ps_free_database(database_x *database);
102 static void __ps_free_appsvc(appsvc_x *appsvc);
103 static void __ps_free_launchconditions(launchconditions_x *launchconditions);
104 static void __ps_free_datashare(datashare_x *datashare);
105 static void __ps_free_layout(layout_x *layout);
106 static void __ps_free_icon(icon_x *icon);
107 static void __ps_free_author(author_x *author);
108 static void __ps_free_description(description_x *description);
109 static void __ps_free_capability(capability_x *capability);
110 static void __ps_free_license(license_x *license);
111 static void __ps_free_appcontrol(appcontrol_x *appcontrol);
112 static void __ps_free_datacontrol(datacontrol_x *datacontrol);
113 static void __ps_free_uiapplication(uiapplication_x *uiapplication);
114 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication);
115 static void __ps_free_font(font_x *font);
116 static void __ps_free_theme(theme_x *theme);
117 static void __ps_free_daemon(daemon_x *daemon);
118 static void __ps_free_ime(ime_x *ime);
119 static char *__pkgid_to_manifest(const char *pkgid);
120 static int __next_child_element(xmlTextReaderPtr reader, int depth);
121 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx);
122 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx);
123 static void __str_trim(char *input);
124 static char *__get_parser_plugin(const char *type);
125 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag, ACTION_TYPE action, const char *pkgid);
126 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid);
127 static void __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid);
128 static void __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid);
129 static int __validate_appid(const char *pkgid, const char *appid, char **newappid);
130
131 static void __str_trim(char *input)
132 {
133         char *trim_str = input;
134
135         if (input == NULL)
136                 return;
137
138         while (*input != 0) {
139                 if (!isspace(*input)) {
140                         *trim_str = *input;
141                         trim_str++;
142                 }
143                 input++;
144         }
145
146         *trim_str = 0;
147         return;
148 }
149
150 static int __validate_appid(const char *pkgid, const char *appid, char **newappid)
151 {
152         if (!pkgid || !appid || !newappid) {
153                 DBG("Arg supplied is NULL\n");
154                 return -1;
155         }
156         int pkglen = strlen(pkgid);
157         int applen = strlen(appid);
158         char *ptr = NULL;
159         char *newapp = NULL;
160         int len = 0;
161         if (strncmp(appid, ".", 1) == 0) {
162                 len = pkglen + applen + 1;
163                 newapp = calloc(1,len);
164                 if (newapp == NULL) {
165                         DBG("Malloc failed\n");
166                         return -1;
167                 }
168                 strncpy(newapp, pkgid, pkglen);
169                 strncat(newapp, appid, applen);
170                 DBG("new appid is %s\n", newapp);
171                 *newappid = newapp;
172                 return 0;
173         }
174         if (applen < pkglen) {
175                 DBG("app id is not proper\n");
176                 *newappid = NULL;
177 #ifdef _VALIDATE_APPID_
178                 return -1;
179 #else
180                 return 0;
181 #endif
182         }
183         if (!strcmp(appid, pkgid)) {
184                 DBG("appid is proper\n");
185                 *newappid = NULL;
186                 return 0;
187         }
188         else if (strncmp(appid, pkgid, pkglen) == 0) {
189                 ptr = strstr(appid, pkgid);
190                 ptr = ptr + pkglen;
191                 if (strncmp(ptr, ".", 1) == 0) {
192                         DBG("appid is proper\n");
193                         *newappid = NULL;
194                         return 0;
195                 }
196                 else {
197                         DBG("appid is not proper\n");
198                         *newappid = NULL;
199 #ifdef _VALIDATE_APPID_
200                         return -1;
201 #else
202                         return 0;
203 #endif
204                 }
205         } else {
206                 DBG("appid is not proper\n");
207                 *newappid = NULL;
208 #ifdef _VALIDATE_APPID_
209                 return -1;
210 #else
211                 return 0;
212 #endif
213         }
214         return 0;
215 }
216
217
218 static char *__get_parser_plugin(const char *type)
219 {
220         FILE *fp = NULL;
221         char buffer[1024] = { 0 };
222         char temp_path[1024] = { 0 };
223         char *lib_path = NULL;
224         char *path = NULL;
225
226         if (type == NULL) {
227                 DBGE("invalid argument\n");
228                 return NULL;
229         }
230
231         fp = fopen(PKG_PARSER_CONF_PATH, "r");
232         if (fp == NULL) {
233                 DBGE("no matching backendlib\n");
234                 return NULL;
235         }
236
237         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
238                 if (buffer[0] == '#')
239                         continue;
240
241                 __str_trim(buffer);
242
243                 if ((path = strstr(buffer, PKG_PARSERLIB)) != NULL) {
244                         DBG("[%s]\n", path);
245                         path = path + strlen(PKG_PARSERLIB);
246                         DBG("[%s]\n", path);
247
248                         break;
249                 }
250
251                 memset(buffer, 0x00, 1024);
252         }
253
254         if (fp != NULL)
255                 fclose(fp);
256
257         if (path == NULL) {
258                 DBGE("no matching backendlib\n");
259                 return NULL;
260         }
261
262         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
263
264         return strdup(temp_path);
265 }
266
267 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag,
268                            ACTION_TYPE action, const char *pkgid)
269 {
270         char *lib_path = NULL;
271         void *lib_handle = NULL;
272         int (*plugin_install) (xmlDocPtr, const char *);
273         int ret = -1;
274         char *ac;
275
276         switch (action) {
277         case ACTION_INSTALL:
278                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
279                 break;
280         case ACTION_UPGRADE:
281                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
282                 break;
283         case ACTION_UNINSTALL:
284                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
285                 break;
286         default:
287                 goto END;
288         }
289
290         lib_path = __get_parser_plugin(tag);
291         if (!lib_path) {
292                 goto END;
293         }
294
295         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
296                 DBGE("dlopen is failed lib_path[%s]\n", lib_path);
297                 goto END;
298         }
299
300         if ((plugin_install =
301              dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
302                 DBGE("can not find symbol \n");
303                 goto END;
304         }
305
306         ret = plugin_install(docPtr, pkgid);
307
308  END:
309         if (lib_path)
310                 free(lib_path);
311         if (lib_handle)
312                 dlclose(lib_handle);
313         return ret;
314 }
315
316 static char *__pkgid_to_manifest(const char *pkgid)
317 {
318         char *manifest;
319         int size;
320
321         if (pkgid == NULL) {
322                 DBGE("pkgid is NULL");
323                 return NULL;
324         }
325
326         size = strlen(MANIFEST_RW_DIRECTORY) + strlen(pkgid) + 10;
327         manifest = malloc(size);
328         if (manifest == NULL) {
329                 DBGE("No memory");
330                 return NULL;
331         }
332         memset(manifest, '\0', size);
333         snprintf(manifest, size, MANIFEST_RW_DIRECTORY "/%s.xml", pkgid);
334
335         if (access(manifest, F_OK)) {
336                 snprintf(manifest, size, MANIFEST_RO_DIRECTORY "/%s.xml", pkgid);
337         }
338
339         return manifest;
340 }
341
342 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
343 {
344         int nLoop = 0;
345         int pid = 0;
346         char *parser_cmd = NULL;
347         int ret = -1;
348         const xmlChar *name;
349         char *lib_path = NULL;
350         void *lib_handle = NULL;
351         int (*plugin_install) (xmlDocPtr);
352
353         DBG("__run_parser_prestep");
354
355         if (xmlTextReaderDepth(reader) != 1) {
356                 DBGE("Node depth is not 1");
357                 goto END;
358         }
359
360         if (xmlTextReaderNodeType(reader) != 1) {
361                 DBGE("Node type is not 1");
362                 goto END;
363         }
364
365         const xmlChar *value;
366         name = xmlTextReaderConstName(reader);
367         if (name == NULL) {
368                 DBGE("TEST TEST TES\n");
369                 name = BAD_CAST "--";
370         }
371
372         value = xmlTextReaderConstValue(reader);
373         DBG("%d %d %s %d %d",
374             xmlTextReaderDepth(reader),
375             xmlTextReaderNodeType(reader),
376             name,
377             xmlTextReaderIsEmptyElement(reader), xmlTextReaderHasValue(reader));
378
379         if (value == NULL) {
380                 DBG("ConstValue NULL");
381         } else {
382                 if (xmlStrlen(value) > 40) {
383                         DBG(" %.40s...", value);
384                 } else {
385                         DBG(" %s", value);
386                 }
387         }
388
389         name = xmlTextReaderConstName(reader);
390         if (name == NULL) {
391                 DBGE("TEST TEST TES\n");
392                 name = BAD_CAST "--";
393         }
394
395         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
396         DBG("docPtr->URL %s\n", (char *)docPtr->URL);
397         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
398         if (copyDocPtr == NULL)
399                 return -1;
400         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
401         if (rootElement == NULL)
402                 return -1;
403         xmlNode *cur_node = xmlFirstElementChild(rootElement);
404         if (cur_node == NULL)
405                 return -1;
406         xmlNode *temp = xmlTextReaderExpand(reader);
407         if (temp == NULL)
408                 return -1;
409         xmlNode *next_node = NULL;
410         while(cur_node != NULL)
411         {
412                 if ( (strcmp(temp->name, cur_node->name) == 0) &&
413                         (temp->line == cur_node->line) ) {
414                         break;
415                 }
416                 else {
417                         next_node = xmlNextElementSibling(cur_node);
418                         xmlUnlinkNode(cur_node);
419                         xmlFreeNode(cur_node);
420                         cur_node = next_node;
421                 }
422         }
423         if (cur_node == NULL)
424                 return -1;
425         next_node = xmlNextElementSibling(cur_node);
426         if (next_node) {
427                 cur_node->next = NULL;
428                 next_node->prev = NULL;
429                 xmlFreeNodeList(next_node);
430                 xmlSetTreeDoc(cur_node, copyDocPtr);
431         } else {
432                 xmlSetTreeDoc(cur_node, copyDocPtr);
433         }
434
435 #ifdef __DEBUG__
436
437 //#else
438         DBG("node type: %d, name: %s children->name: %s last->name: %s\n"
439             "parent->name: %s next->name: %s prev->name: %s\n",
440             cur_node->type, cur_node->name,
441             cur_node->children ? cur_node->children->name : "NULL",
442             cur_node->last ? cur_node->last->name : "NULL",
443             cur_node->parent ? cur_node->parent->name : "NULL",
444             cur_node->next ? cur_node->next->name : "NULL",
445             cur_node->prev ? cur_node->prev->name : "NULL");
446
447         FILE *fp = fopen("/opt/share/test.xml", "a");
448         xmlDocDump(fp, copyDocPtr);
449         fprintf(fp, "\n");
450         fclose(fp);
451 #endif
452
453         ret = __ps_run_parser(copyDocPtr, name, action, pkgid);
454  END:
455
456         return ret;
457 }
458
459 static void
460 __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid)
461 {
462         char *tag = NULL;
463         int i = 0;
464
465         switch (xmlTextReaderNodeType(reader)) {
466         case XML_READER_TYPE_END_ELEMENT:
467                 {
468                         //            DBG("XML_READER_TYPE_END_ELEMENT");
469                         break;
470                 }
471
472         case XML_READER_TYPE_ELEMENT:
473                 {
474                         // Elements without closing tag don't receive
475                         // XML_READER_TYPE_END_ELEMENT event.
476
477                         const xmlChar *elementName =
478                             xmlTextReaderLocalName(reader);
479                         if (elementName == NULL) {
480 //                              DBG("elementName %s\n", (char *)elementName);
481                                 break;
482                         }
483
484                         const xmlChar *nameSpace =
485                             xmlTextReaderConstNamespaceUri(reader);
486                         if (nameSpace) {
487 //                              DBG("nameSpace %s\n", (char *)nameSpace);
488                         }
489 /*
490                         DBG("XML_READER_TYPE_ELEMENT %s, %s\n",
491                             elementName ? elementName : "NULL",
492                             nameSpace ? nameSpace : "NULL");
493 */
494                         if (tagv == NULL) {
495                                 DBG("__run_parser_prestep pkgid[%s]\n", pkgid);
496                                 __run_parser_prestep(reader, action, pkgid);
497                         }
498                         else {
499                                 i = 0;
500                                 for (tag = tagv[0]; tag; tag = tagv[++i])
501                                         if (strcmp(tag, elementName) == 0) {
502                                                 DBG("__run_parser_prestep tag[%s] pkgid[%s]\n", tag, pkgid);
503                                                 __run_parser_prestep(reader,
504                                                                      action, pkgid);
505                                                 break;
506                                         }
507                         }
508
509                         break;
510                 }
511         case XML_READER_TYPE_TEXT:
512         case XML_READER_TYPE_CDATA:
513                 {
514                         const xmlChar *value = xmlTextReaderConstValue(reader);
515                         if (value) {
516 //                              DBG("value %s\n", value);
517                         }
518
519                         const xmlChar *lang = xmlTextReaderConstXmlLang(reader);
520                         if (lang) {
521 //                              DBG("lang\n", lang);
522                         }
523
524 /*                      DBG("XML_READER_TYPE_TEXT %s, %s\n",
525                             value ? value : "NULL", lang ? lang : "NULL");
526 */
527                         break;
528                 }
529         default:
530 //              DBG("Ignoring Node of Type: %d", xmlTextReaderNodeType(reader));
531                 break;
532         }
533 }
534
535 static void
536 __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid)
537 {
538         xmlTextReaderPtr reader;
539         xmlDocPtr docPtr;
540         int ret;
541
542         docPtr = xmlReadFile(filename, NULL, 0);
543         reader = xmlReaderWalker(docPtr);
544         if (reader != NULL) {
545                 ret = xmlTextReaderRead(reader);
546                 while (ret == 1) {
547                         __processNode(reader, action, tagv, pkgid);
548                         ret = xmlTextReaderRead(reader);
549                 }
550                 xmlFreeTextReader(reader);
551
552                 if (ret != 0) {
553                         DBGE("%s : failed to parse", filename);
554                 }
555         } else {
556                 DBGE("Unable to open %s", filename);
557         }
558 }
559
560 static int __next_child_element(xmlTextReaderPtr reader, int depth)
561 {
562         int ret = xmlTextReaderRead(reader);
563         int cur = xmlTextReaderDepth(reader);
564         while (ret == 1) {
565
566                 switch (xmlTextReaderNodeType(reader)) {
567                 case XML_READER_TYPE_ELEMENT:
568                         if (cur == depth + 1)
569                                 return 1;
570                         break;
571                 case XML_READER_TYPE_TEXT:
572                         /*text is handled by each function separately*/
573                         if (cur == depth + 1)
574                                 return 0;
575                         break;
576                 case XML_READER_TYPE_END_ELEMENT:
577                         if (cur == depth)
578                                 return 0;
579                         break;
580                 default:
581                         if (cur <= depth)
582                                 return 0;
583                         break;
584                 }
585                 ret = xmlTextReaderRead(reader);
586                 cur = xmlTextReaderDepth(reader);
587         }
588         return ret;
589 }
590
591 static void __ps_free_category(category_x *category)
592 {
593         if (category == NULL)
594                 return;
595         if (category->name) {
596                 free((void *)category->name);
597                 category->name = NULL;
598         }
599         free((void*)category);
600         category = NULL;
601 }
602
603 static void __ps_free_icon(icon_x *icon)
604 {
605         if (icon == NULL)
606                 return;
607         if (icon->text) {
608                 free((void *)icon->text);
609                 icon->text = NULL;
610         }
611         if (icon->lang) {
612                 free((void *)icon->lang);
613                 icon->lang = NULL;
614         }
615         if (icon->name) {
616                 free((void *)icon->name);
617                 icon->name= NULL;
618         }
619         if (icon->section) {
620                 free((void *)icon->section);
621                 icon->section = NULL;
622         }
623         if (icon->size) {
624                 free((void *)icon->size);
625                 icon->size = NULL;
626         }
627         if (icon->resolution) {
628                 free((void *)icon->resolution);
629                 icon->resolution = NULL;
630         }
631         free((void*)icon);
632         icon = NULL;
633 }
634
635 static void __ps_free_operation(operation_x *operation)
636 {
637         if (operation == NULL)
638                 return;
639         if (operation->text) {
640                 free((void *)operation->text);
641                 operation->text = NULL;
642         }
643         free((void*)operation);
644         operation = NULL;
645 }
646
647 static void __ps_free_uri(uri_x *uri)
648 {
649         if (uri == NULL)
650                 return;
651         if (uri->text) {
652                 free((void *)uri->text);
653                 uri->text = NULL;
654         }
655         free((void*)uri);
656         uri = NULL;
657 }
658
659 static void __ps_free_mime(mime_x *mime)
660 {
661         if (mime == NULL)
662                 return;
663         if (mime->text) {
664                 free((void *)mime->text);
665                 mime->text = NULL;
666         }
667         free((void*)mime);
668         mime = NULL;
669 }
670
671 static void __ps_free_subapp(subapp_x *subapp)
672 {
673         if (subapp == NULL)
674                 return;
675         if (subapp->text) {
676                 free((void *)subapp->text);
677                 subapp->text = NULL;
678         }
679         free((void*)subapp);
680         subapp = NULL;
681 }
682
683 static void __ps_free_condition(condition_x *condition)
684 {
685         if (condition == NULL)
686                 return;
687         if (condition->text) {
688                 free((void *)condition->text);
689                 condition->text = NULL;
690         }
691         if (condition->name) {
692                 free((void *)condition->name);
693                 condition->name = NULL;
694         }
695         free((void*)condition);
696         condition = NULL;
697 }
698
699 static void __ps_free_notification(notification_x *notification)
700 {
701         if (notification == NULL)
702                 return;
703         if (notification->text) {
704                 free((void *)notification->text);
705                 notification->text = NULL;
706         }
707         if (notification->name) {
708                 free((void *)notification->name);
709                 notification->name = NULL;
710         }
711         free((void*)notification);
712         notification = NULL;
713 }
714
715 static void __ps_free_compatibility(compatibility_x *compatibility)
716 {
717         if (compatibility == NULL)
718                 return;
719         if (compatibility->text) {
720                 free((void *)compatibility->text);
721                 compatibility->text = NULL;
722         }
723         if (compatibility->name) {
724                 free((void *)compatibility->name);
725                 compatibility->name = NULL;
726         }
727         free((void*)compatibility);
728         compatibility = NULL;
729 }
730
731 static void __ps_free_resolution(resolution_x *resolution)
732 {
733         if (resolution == NULL)
734                 return;
735         if (resolution->mimetype) {
736                 free((void *)resolution->mimetype);
737                 resolution->mimetype = NULL;
738         }
739         if (resolution->urischeme) {
740                 free((void *)resolution->urischeme);
741                 resolution->urischeme = NULL;
742         }
743         free((void*)resolution);
744         resolution = NULL;
745 }
746
747 static void __ps_free_capability(capability_x *capability)
748 {
749         if (capability == NULL)
750                 return;
751         if (capability->operationid) {
752                 free((void *)capability->operationid);
753                 capability->operationid = NULL;
754         }
755         /*Free Resolution*/
756         if (capability->resolution) {
757                 resolution_x *resolution = capability->resolution;
758                 resolution_x *tmp = NULL;
759                 while(resolution != NULL)
760                 {
761                         tmp = resolution->next;
762                         __ps_free_resolution(resolution);
763                         resolution = tmp;
764                 }
765         }
766         free((void*)capability);
767         capability = NULL;
768 }
769
770 static void __ps_free_allowed(allowed_x *allowed)
771 {
772         if (allowed == NULL)
773                 return;
774         if (allowed->name) {
775                 free((void *)allowed->name);
776                 allowed->name = NULL;
777         }
778         if (allowed->text) {
779                 free((void *)allowed->text);
780                 allowed->text = NULL;
781         }
782         free((void*)allowed);
783         allowed = NULL;
784 }
785
786 static void __ps_free_request(request_x *request)
787 {
788         if (request == NULL)
789                 return;
790         if (request->text) {
791                 free((void *)request->text);
792                 request->text = NULL;
793         }
794         free((void*)request);
795         request = NULL;
796 }
797
798 static void __ps_free_datacontrol(datacontrol_x *datacontrol)
799 {
800         if (datacontrol == NULL)
801                 return;
802         if (datacontrol->providerid) {
803                 free((void *)datacontrol->providerid);
804                 datacontrol->providerid = NULL;
805         }
806         /*Free Capability*/
807         if (datacontrol->capability) {
808                 capability_x *capability = datacontrol->capability;
809                 capability_x *tmp = NULL;
810                 while(capability != NULL)
811                 {
812                         tmp = capability->next;
813                         __ps_free_capability(capability);
814                         capability = tmp;
815                 }
816         }
817         free((void*)datacontrol);
818         datacontrol = NULL;
819 }
820
821 static void __ps_free_launchconditions(launchconditions_x *launchconditions)
822 {
823         if (launchconditions == NULL)
824                 return;
825         if (launchconditions->text) {
826                 free((void *)launchconditions->text);
827                 launchconditions->text = NULL;
828         }
829         /*Free Condition*/
830         if (launchconditions->condition) {
831                 condition_x *condition = launchconditions->condition;
832                 condition_x *tmp = NULL;
833                 while(condition != NULL)
834                 {
835                         tmp = condition->next;
836                         __ps_free_condition(condition);
837                         condition = tmp;
838                 }
839         }
840         free((void*)launchconditions);
841         launchconditions = NULL;
842 }
843
844 static void __ps_free_appcontrol(appcontrol_x *appcontrol)
845 {
846         if (appcontrol == NULL)
847                 return;
848         if (appcontrol->text) {
849                 free((void *)appcontrol->text);
850                 appcontrol->text = NULL;
851         }
852         /*Free Operation*/
853         if (appcontrol->operation) {
854                 operation_x *operation = appcontrol->operation;
855                 operation_x *tmp = NULL;
856                 while(operation != NULL)
857                 {
858                         tmp = operation->next;
859                         __ps_free_operation(operation);
860                         operation = tmp;
861                 }
862         }
863         /*Free Uri*/
864         if (appcontrol->uri) {
865                 uri_x *uri = appcontrol->uri;
866                 uri_x *tmp = NULL;
867                 while(uri != NULL)
868                 {
869                         tmp = uri->next;
870                         __ps_free_uri(uri);
871                         uri = tmp;
872                 }
873         }
874         /*Free Mime*/
875         if (appcontrol->mime) {
876                 mime_x *mime = appcontrol->mime;
877                 mime_x *tmp = NULL;
878                 while(mime != NULL)
879                 {
880                         tmp = mime->next;
881                         __ps_free_mime(mime);
882                         mime = tmp;
883                 }
884         }
885         /*Free subapp*/
886         if (appcontrol->subapp) {
887                 subapp_x *subapp = appcontrol->subapp;
888                 subapp_x *tmp = NULL;
889                 while(subapp != NULL)
890                 {
891                         tmp = subapp->next;
892                         __ps_free_subapp(subapp);
893                         subapp = tmp;
894                 }
895         }
896         free((void*)appcontrol);
897         appcontrol = NULL;
898 }
899
900 static void __ps_free_appsvc(appsvc_x *appsvc)
901 {
902         if (appsvc == NULL)
903                 return;
904         if (appsvc->text) {
905                 free((void *)appsvc->text);
906                 appsvc->text = NULL;
907         }
908         /*Free Operation*/
909         if (appsvc->operation) {
910                 operation_x *operation = appsvc->operation;
911                 operation_x *tmp = NULL;
912                 while(operation != NULL)
913                 {
914                         tmp = operation->next;
915                         __ps_free_operation(operation);
916                         operation = tmp;
917                 }
918         }
919         /*Free Uri*/
920         if (appsvc->uri) {
921                 uri_x *uri = appsvc->uri;
922                 uri_x *tmp = NULL;
923                 while(uri != NULL)
924                 {
925                         tmp = uri->next;
926                         __ps_free_uri(uri);
927                         uri = tmp;
928                 }
929         }
930         /*Free Mime*/
931         if (appsvc->mime) {
932                 mime_x *mime = appsvc->mime;
933                 mime_x *tmp = NULL;
934                 while(mime != NULL)
935                 {
936                         tmp = mime->next;
937                         __ps_free_mime(mime);
938                         mime = tmp;
939                 }
940         }
941         /*Free subapp*/
942         if (appsvc->subapp) {
943                 subapp_x *subapp = appsvc->subapp;
944                 subapp_x *tmp = NULL;
945                 while(subapp != NULL)
946                 {
947                         tmp = subapp->next;
948                         __ps_free_subapp(subapp);
949                         subapp = tmp;
950                 }
951         }
952         free((void*)appsvc);
953         appsvc = NULL;
954 }
955
956 static void __ps_free_deviceprofile(deviceprofile_x *deviceprofile)
957 {
958         return;
959 }
960
961 static void __ps_free_define(define_x *define)
962 {
963         if (define == NULL)
964                 return;
965         if (define->path) {
966                 free((void *)define->path);
967                 define->path = NULL;
968         }
969         /*Free Request*/
970         if (define->request) {
971                 request_x *request = define->request;
972                 request_x *tmp = NULL;
973                 while(request != NULL)
974                 {
975                         tmp = request->next;
976                         __ps_free_request(request);
977                         request = tmp;
978                 }
979         }
980         /*Free Allowed*/
981         if (define->allowed) {
982                 allowed_x *allowed = define->allowed;
983                 allowed_x *tmp = NULL;
984                 while(allowed != NULL)
985                 {
986                         tmp = allowed->next;
987                         __ps_free_allowed(allowed);
988                         allowed = tmp;
989                 }
990         }
991         free((void*)define);
992         define = NULL;
993 }
994
995 static void __ps_free_registry(registry_x *registry)
996 {
997         if (registry == NULL)
998                 return;
999         if (registry->name) {
1000                 free((void *)registry->name);
1001                 registry->name = NULL;
1002         }
1003         if (registry->text) {
1004                 free((void *)registry->text);
1005                 registry->text = NULL;
1006         }
1007         free((void*)registry);
1008         registry = NULL;
1009 }
1010
1011 static void __ps_free_database(database_x *database)
1012 {
1013         if (database == NULL)
1014                 return;
1015         if (database->name) {
1016                 free((void *)database->name);
1017                 database->name = NULL;
1018         }
1019         if (database->text) {
1020                 free((void *)database->text);
1021                 database->text = NULL;
1022         }
1023         free((void*)database);
1024         database = NULL;
1025 }
1026
1027 static void __ps_free_datashare(datashare_x *datashare)
1028 {
1029         if (datashare == NULL)
1030                 return;
1031         /*Free Define*/
1032         if (datashare->define) {
1033                 define_x *define =  datashare->define;
1034                 define_x *tmp = NULL;
1035                 while(define != NULL)
1036                 {
1037                         tmp = define->next;
1038                         __ps_free_define(define);
1039                         define = tmp;
1040                 }
1041         }
1042         /*Free Request*/
1043         if (datashare->request) {
1044                 request_x *request = datashare->request;
1045                 request_x *tmp = NULL;
1046                 while(request != NULL)
1047                 {
1048                         tmp = request->next;
1049                         __ps_free_request(request);
1050                         request = tmp;
1051                 }
1052         }
1053         free((void*)datashare);
1054         datashare = NULL;
1055 }
1056
1057 static void __ps_free_layout(layout_x *layout)
1058 {
1059         if (layout == NULL)
1060                 return;
1061         if (layout->name) {
1062                 free((void *)layout->name);
1063                 layout->name = NULL;
1064         }
1065         if (layout->text) {
1066                 free((void *)layout->text);
1067                 layout->text = NULL;
1068         }
1069         free((void*)layout);
1070         layout = NULL;
1071 }
1072
1073 static void __ps_free_label(label_x *label)
1074 {
1075         if (label == NULL)
1076                 return;
1077         if (label->name) {
1078                 free((void *)label->name);
1079                 label->name = NULL;
1080         }
1081         if (label->text) {
1082                 free((void *)label->text);
1083                 label->text = NULL;
1084         }
1085         if (label->lang) {
1086                 free((void *)label->lang);
1087                 label->lang= NULL;
1088         }
1089         free((void*)label);
1090         label = NULL;
1091 }
1092
1093 static void __ps_free_author(author_x *author)
1094 {
1095         if (author == NULL)
1096                 return;
1097         if (author->email) {
1098                 free((void *)author->email);
1099                 author->email = NULL;
1100         }
1101         if (author->text) {
1102                 free((void *)author->text);
1103                 author->text = NULL;
1104         }
1105         if (author->href) {
1106                 free((void *)author->href);
1107                 author->href = NULL;
1108         }
1109         if (author->lang) {
1110                 free((void *)author->lang);
1111                 author->lang = NULL;
1112         }
1113         free((void*)author);
1114         author = NULL;
1115 }
1116
1117 static void __ps_free_description(description_x *description)
1118 {
1119         if (description == NULL)
1120                 return;
1121         if (description->name) {
1122                 free((void *)description->name);
1123                 description->name = NULL;
1124         }
1125         if (description->text) {
1126                 free((void *)description->text);
1127                 description->text = NULL;
1128         }
1129         if (description->lang) {
1130                 free((void *)description->lang);
1131                 description->lang = NULL;
1132         }
1133         free((void*)description);
1134         description = NULL;
1135 }
1136
1137 static void __ps_free_license(license_x *license)
1138 {
1139         if (license == NULL)
1140                 return;
1141         if (license->text) {
1142                 free((void *)license->text);
1143                 license->text = NULL;
1144         }
1145         if (license->lang) {
1146                 free((void *)license->lang);
1147                 license->lang = NULL;
1148         }
1149         free((void*)license);
1150         license = NULL;
1151 }
1152
1153 static void __ps_free_uiapplication(uiapplication_x *uiapplication)
1154 {
1155         if (uiapplication == NULL)
1156                 return;
1157         if (uiapplication->exec) {
1158                 free((void *)uiapplication->exec);
1159                 uiapplication->exec = NULL;
1160         }
1161         if (uiapplication->appid) {
1162                 free((void *)uiapplication->appid);
1163                 uiapplication->appid = NULL;
1164         }
1165         if (uiapplication->nodisplay) {
1166                 free((void *)uiapplication->nodisplay);
1167                 uiapplication->nodisplay = NULL;
1168         }
1169         if (uiapplication->multiple) {
1170                 free((void *)uiapplication->multiple);
1171                 uiapplication->multiple = NULL;
1172         }
1173         if (uiapplication->type) {
1174                 free((void *)uiapplication->type);
1175                 uiapplication->type = NULL;
1176         }
1177         if (uiapplication->categories) {
1178                 free((void *)uiapplication->categories);
1179                 uiapplication->categories = NULL;
1180         }
1181         if (uiapplication->extraid) {
1182                 free((void *)uiapplication->extraid);
1183                 uiapplication->extraid = NULL;
1184         }
1185         if (uiapplication->taskmanage) {
1186                 free((void *)uiapplication->taskmanage);
1187                 uiapplication->taskmanage = NULL;
1188         }
1189         if (uiapplication->hwacceleration) {
1190                 free((void *)uiapplication->hwacceleration);
1191                 uiapplication->hwacceleration = NULL;
1192         }
1193         if (uiapplication->mainapp) {
1194                 free((void *)uiapplication->mainapp);
1195                 uiapplication->mainapp = NULL;
1196         }
1197         if (uiapplication->recentimage) {
1198                 free((void *)uiapplication->recentimage);
1199                 uiapplication->recentimage = NULL;
1200         }
1201         /*Free Label*/
1202         if (uiapplication->label) {
1203                 label_x *label = uiapplication->label;
1204                 label_x *tmp = NULL;
1205                 while(label != NULL)
1206                 {
1207                         tmp = label->next;
1208                         __ps_free_label(label);
1209                         label = tmp;
1210                 }
1211         }
1212         /*Free Icon*/
1213         if (uiapplication->icon) {
1214                 icon_x *icon = uiapplication->icon;
1215                 icon_x *tmp = NULL;
1216                 while(icon != NULL)
1217                 {
1218                         tmp = icon->next;
1219                         __ps_free_icon(icon);
1220                         icon = tmp;
1221                 }
1222         }
1223         /*Free AppControl*/
1224         if (uiapplication->appcontrol) {
1225                 appcontrol_x *appcontrol = uiapplication->appcontrol;
1226                 appcontrol_x *tmp = NULL;
1227                 while(appcontrol != NULL)
1228                 {
1229                         tmp = appcontrol->next;
1230                         __ps_free_appcontrol(appcontrol);
1231                         appcontrol = tmp;
1232                 }
1233         }
1234         /*Free LaunchConditions*/
1235         if (uiapplication->launchconditions) {
1236                 launchconditions_x *launchconditions = uiapplication->launchconditions;
1237                 launchconditions_x *tmp = NULL;
1238                 while(launchconditions != NULL)
1239                 {
1240                         tmp = launchconditions->next;
1241                         __ps_free_launchconditions(launchconditions);
1242                         launchconditions = tmp;
1243                 }
1244         }
1245         /*Free Notification*/
1246         if (uiapplication->notification) {
1247                 notification_x *notification = uiapplication->notification;
1248                 notification_x *tmp = NULL;
1249                 while(notification != NULL)
1250                 {
1251                         tmp = notification->next;
1252                         __ps_free_notification(notification);
1253                         notification = tmp;
1254                 }
1255         }
1256         /*Free DataShare*/
1257         if (uiapplication->datashare) {
1258                 datashare_x *datashare = uiapplication->datashare;
1259                 datashare_x *tmp = NULL;
1260                 while(datashare != NULL)
1261                 {
1262                         tmp = datashare->next;
1263                         __ps_free_datashare(datashare);
1264                         datashare = tmp;
1265                 }
1266         }
1267         /*Free AppSvc*/
1268         if (uiapplication->appsvc) {
1269                 appsvc_x *appsvc = uiapplication->appsvc;
1270                 appsvc_x *tmp = NULL;
1271                 while(appsvc != NULL)
1272                 {
1273                         tmp = appsvc->next;
1274                         __ps_free_appsvc(appsvc);
1275                         appsvc = tmp;
1276                 }
1277         }
1278         /*Free Category*/
1279         if (uiapplication->category) {
1280                 category_x *category = uiapplication->category;
1281                 category_x *tmp = NULL;
1282                 while(category != NULL)
1283                 {
1284                         tmp = category->next;
1285                         __ps_free_category(category);
1286                         category = tmp;
1287                 }
1288         }
1289         free((void*)uiapplication);
1290         uiapplication = NULL;
1291 }
1292
1293 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication)
1294 {
1295         if (serviceapplication == NULL)
1296                 return;
1297         if (serviceapplication->exec) {
1298                 free((void *)serviceapplication->exec);
1299                 serviceapplication->exec = NULL;
1300         }
1301         if (serviceapplication->appid) {
1302                 free((void *)serviceapplication->appid);
1303                 serviceapplication->appid = NULL;
1304         }
1305         if (serviceapplication->onboot) {
1306                 free((void *)serviceapplication->onboot);
1307                 serviceapplication->onboot = NULL;
1308         }
1309         if (serviceapplication->autorestart) {
1310                 free((void *)serviceapplication->autorestart);
1311                 serviceapplication->autorestart = NULL;
1312         }
1313         if (serviceapplication->type) {
1314                 free((void *)serviceapplication->type);
1315                 serviceapplication->type = NULL;
1316         }
1317         /*Free Label*/
1318         if (serviceapplication->label) {
1319                 label_x *label = serviceapplication->label;
1320                 label_x *tmp = NULL;
1321                 while(label != NULL)
1322                 {
1323                         tmp = label->next;
1324                         __ps_free_label(label);
1325                         label = tmp;
1326                 }
1327         }
1328         /*Free Icon*/
1329         if (serviceapplication->icon) {
1330                 icon_x *icon = serviceapplication->icon;
1331                 icon_x *tmp = NULL;
1332                 while(icon != NULL)
1333                 {
1334                         tmp = icon->next;
1335                         __ps_free_icon(icon);
1336                         icon = tmp;
1337                 }
1338         }
1339         /*Free AppControl*/
1340         if (serviceapplication->appcontrol) {
1341                 appcontrol_x *appcontrol = serviceapplication->appcontrol;
1342                 appcontrol_x *tmp = NULL;
1343                 while(appcontrol != NULL)
1344                 {
1345                         tmp = appcontrol->next;
1346                         __ps_free_appcontrol(appcontrol);
1347                         appcontrol = tmp;
1348                 }
1349         }
1350         /*Free DataControl*/
1351         if (serviceapplication->datacontrol) {
1352                 datacontrol_x *datacontrol = serviceapplication->datacontrol;
1353                 datacontrol_x *tmp = NULL;
1354                 while(datacontrol != NULL)
1355                 {
1356                         tmp = datacontrol->next;
1357                         __ps_free_datacontrol(datacontrol);
1358                         datacontrol = tmp;
1359                 }
1360         }
1361         /*Free LaunchConditions*/
1362         if (serviceapplication->launchconditions) {
1363                 launchconditions_x *launchconditions = serviceapplication->launchconditions;
1364                 launchconditions_x *tmp = NULL;
1365                 while(launchconditions != NULL)
1366                 {
1367                         tmp = launchconditions->next;
1368                         __ps_free_launchconditions(launchconditions);
1369                         launchconditions = tmp;
1370                 }
1371         }
1372         /*Free Notification*/
1373         if (serviceapplication->notification) {
1374                 notification_x *notification = serviceapplication->notification;
1375                 notification_x *tmp = NULL;
1376                 while(notification != NULL)
1377                 {
1378                         tmp = notification->next;
1379                         __ps_free_notification(notification);
1380                         notification = tmp;
1381                 }
1382         }
1383         /*Free DataShare*/
1384         if (serviceapplication->datashare) {
1385                 datashare_x *datashare = serviceapplication->datashare;
1386                 datashare_x *tmp = NULL;
1387                 while(datashare != NULL)
1388                 {
1389                         tmp = datashare->next;
1390                         __ps_free_datashare(datashare);
1391                         datashare = tmp;
1392                 }
1393         }
1394         /*Free AppSvc*/
1395         if (serviceapplication->appsvc) {
1396                 appsvc_x *appsvc = serviceapplication->appsvc;
1397                 appsvc_x *tmp = NULL;
1398                 while(appsvc != NULL)
1399                 {
1400                         tmp = appsvc->next;
1401                         __ps_free_appsvc(appsvc);
1402                         appsvc = tmp;
1403                 }
1404         }
1405         /*Free Category*/
1406         if (serviceapplication->category) {
1407                 category_x *category = serviceapplication->category;
1408                 category_x *tmp = NULL;
1409                 while(category != NULL)
1410                 {
1411                         tmp = category->next;
1412                         __ps_free_category(category);
1413                         category = tmp;
1414                 }
1415         }
1416         free((void*)serviceapplication);
1417         serviceapplication = NULL;
1418 }
1419
1420 static void __ps_free_font(font_x *font)
1421 {
1422         if (font == NULL)
1423                 return;
1424         if (font->name) {
1425                 free((void *)font->name);
1426                 font->name = NULL;
1427         }
1428         if (font->text) {
1429                 free((void *)font->text);
1430                 font->text = NULL;
1431         }
1432         free((void*)font);
1433         font = NULL;
1434 }
1435
1436 static void __ps_free_theme(theme_x *theme)
1437 {
1438         if (theme == NULL)
1439                 return;
1440         if (theme->name) {
1441                 free((void *)theme->name);
1442                 theme->name = NULL;
1443         }
1444         if (theme->text) {
1445                 free((void *)theme->text);
1446                 theme->text = NULL;
1447         }
1448         free((void*)theme);
1449         theme = NULL;
1450 }
1451
1452 static void __ps_free_daemon(daemon_x *daemon)
1453 {
1454         if (daemon == NULL)
1455                 return;
1456         if (daemon->name) {
1457                 free((void *)daemon->name);
1458                 daemon->name = NULL;
1459         }
1460         if (daemon->text) {
1461                 free((void *)daemon->text);
1462                 daemon->text = NULL;
1463         }
1464         free((void*)daemon);
1465         daemon = NULL;
1466 }
1467
1468 static void __ps_free_ime(ime_x *ime)
1469 {
1470         if (ime == NULL)
1471                 return;
1472         if (ime->name) {
1473                 free((void *)ime->name);
1474                 ime->name = NULL;
1475         }
1476         if (ime->text) {
1477                 free((void *)ime->text);
1478                 ime->text = NULL;
1479         }
1480         free((void*)ime);
1481         ime = NULL;
1482 }
1483
1484
1485 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
1486 {
1487         xmlTextReaderRead(reader);
1488         if (xmlTextReaderValue(reader))
1489                 allowed->text = ASCII(xmlTextReaderValue(reader));
1490         return 0;
1491 }
1492
1493 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
1494 {
1495         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1496                 operation->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1497 /* Text does not exist. Only attribute exists
1498         xmlTextReaderRead(reader);
1499         if (xmlTextReaderValue(reader))
1500                 operation->text = ASCII(xmlTextReaderValue(reader));
1501 */
1502         return 0;
1503 }
1504
1505 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
1506 {
1507         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1508                 uri->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1509 /* Text does not exist. Only attribute exists
1510         xmlTextReaderRead(reader);
1511         if (xmlTextReaderValue(reader))
1512                 uri->text = ASCII(xmlTextReaderValue(reader));
1513 */
1514         return 0;
1515 }
1516
1517 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
1518 {
1519         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1520                 mime->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1521 /* Text does not exist. Only attribute exists
1522         xmlTextReaderRead(reader);
1523         if (xmlTextReaderValue(reader))
1524                 mime->text = ASCII(xmlTextReaderValue(reader));
1525 */
1526         return 0;
1527 }
1528
1529 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
1530 {
1531         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1532                 subapp->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1533 /* Text does not exist. Only attribute exists
1534         xmlTextReaderRead(reader);
1535         if (xmlTextReaderValue(reader))
1536                 mime->text = ASCII(xmlTextReaderValue(reader));
1537 */
1538         return 0;
1539 }
1540
1541 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
1542 {
1543         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1544                 condition->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1545         xmlTextReaderRead(reader);
1546         if (xmlTextReaderValue(reader))
1547                 condition->text = ASCII(xmlTextReaderValue(reader));
1548         return 0;
1549 }
1550
1551 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
1552 {
1553         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1554                 notification->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1555         xmlTextReaderRead(reader);
1556         if (xmlTextReaderValue(reader))
1557                 notification->text = ASCII(xmlTextReaderValue(reader));
1558         return 0;
1559 }
1560
1561 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
1562 {
1563         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1564                 category->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1565         return 0;
1566 }
1567
1568 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
1569 {
1570         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1571                 compatibility->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1572         xmlTextReaderRead(reader);
1573         if (xmlTextReaderValue(reader))
1574                 compatibility->text = ASCII(xmlTextReaderValue(reader));
1575         return 0;
1576 }
1577
1578 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution)
1579 {
1580         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")))
1581                 resolution->mimetype = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")));
1582         if (xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")))
1583                 resolution->urischeme = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")));
1584         return 0;
1585 }
1586
1587 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
1588 {
1589         xmlTextReaderRead(reader);
1590         if (xmlTextReaderValue(reader))
1591                 request->text = ASCII(xmlTextReaderValue(reader));
1592         return 0;
1593 }
1594
1595 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
1596 {
1597         const xmlChar *node;
1598         int ret = -1;
1599         int depth = -1;
1600         allowed_x *tmp1 = NULL;
1601         request_x *tmp2 = NULL;
1602
1603         if (xmlTextReaderGetAttribute(reader, XMLCHAR("path")))
1604                 define->path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("path")));
1605
1606         depth = xmlTextReaderDepth(reader);
1607         while ((ret = __next_child_element(reader, depth))) {
1608                 node = xmlTextReaderConstName(reader);
1609                 if (!node) {
1610                         DBG("xmlTextReaderConstName value is NULL\n");
1611                         return -1;
1612                 }
1613
1614                 if (!strcmp(ASCII(node), "allowed")) {
1615                         allowed_x *allowed= malloc(sizeof(allowed_x));
1616                         if (allowed == NULL) {
1617                                 DBG("Malloc Failed\n");
1618                                 return -1;
1619                         }
1620                         memset(allowed, '\0', sizeof(allowed_x));
1621                         LISTADD(define->allowed, allowed);
1622                         ret = __ps_process_allowed(reader, allowed);
1623                 } else if (!strcmp(ASCII(node), "request")) {
1624                         request_x *request = malloc(sizeof(request_x));
1625                         if (request == NULL) {
1626                                 DBG("Malloc Failed\n");
1627                                 return -1;
1628                         }
1629                         memset(request, '\0', sizeof(request_x));
1630                         LISTADD(define->request, request);
1631                         ret = __ps_process_request(reader, request);
1632                 } else
1633                         return -1;
1634                 if (ret < 0) {
1635                         DBG("Processing define failed\n");
1636                         return ret;
1637                 }
1638         }
1639         if (define->allowed) {
1640                 LISTHEAD(define->allowed, tmp1);
1641                 define->allowed = tmp1;
1642         }
1643         if (define->request) {
1644                 LISTHEAD(define->request, tmp2);
1645                 define->request = tmp2;
1646         }
1647         return ret;
1648 }
1649
1650 static int __ps_process_registry(xmlTextReaderPtr reader, registry_x *registry)
1651 {
1652         /*TODO: once policy is set*/
1653         return 0;
1654 }
1655
1656 static int __ps_process_database(xmlTextReaderPtr reader, database_x *database)
1657 {
1658         /*TODO: once policy is set*/
1659         return 0;
1660 }
1661
1662 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol)
1663 {
1664         const xmlChar *node;
1665         int ret = -1;
1666         int depth = -1;
1667         operation_x *tmp1 = NULL;
1668         uri_x *tmp2 = NULL;
1669         mime_x *tmp3 = NULL;
1670         subapp_x *tmp4 = NULL;
1671
1672         depth = xmlTextReaderDepth(reader);
1673         while ((ret = __next_child_element(reader, depth))) {
1674                 node = xmlTextReaderConstName(reader);
1675                 if (!node) {
1676                         DBG("xmlTextReaderConstName value is NULL\n");
1677                         return -1;
1678                 }
1679
1680                 if (!strcmp(ASCII(node), "operation")) {
1681                         operation_x *operation = malloc(sizeof(operation_x));
1682                         if (operation == NULL) {
1683                                 DBG("Malloc Failed\n");
1684                                 return -1;
1685                         }
1686                         memset(operation, '\0', sizeof(operation_x));
1687                         LISTADD(appcontrol->operation, operation);
1688                         ret = __ps_process_operation(reader, operation);
1689                         DBG("operation processing\n");
1690                 } else if (!strcmp(ASCII(node), "uri")) {
1691                         uri_x *uri= malloc(sizeof(uri_x));
1692                         if (uri == NULL) {
1693                                 DBG("Malloc Failed\n");
1694                                 return -1;
1695                         }
1696                         memset(uri, '\0', sizeof(uri_x));
1697                         LISTADD(appcontrol->uri, uri);
1698                         ret = __ps_process_uri(reader, uri);
1699                         DBG("uri processing\n");
1700                 } else if (!strcmp(ASCII(node), "mime")) {
1701                         mime_x *mime = malloc(sizeof(mime_x));
1702                         if (mime == NULL) {
1703                                 DBG("Malloc Failed\n");
1704                                 return -1;
1705                         }
1706                         memset(mime, '\0', sizeof(mime_x));
1707                         LISTADD(appcontrol->mime, mime);
1708                         ret = __ps_process_mime(reader, mime);
1709                         DBG("mime processing\n");
1710                 } else if (!strcmp(ASCII(node), "subapp")) {
1711                         subapp_x *subapp = malloc(sizeof(subapp_x));
1712                         if (subapp == NULL) {
1713                                 DBG("Malloc Failed\n");
1714                                 return -1;
1715                         }
1716                         memset(subapp, '\0', sizeof(subapp_x));
1717                         LISTADD(appcontrol->subapp, subapp);
1718                         ret = __ps_process_subapp(reader, subapp);
1719                         DBG("subapp processing\n");
1720                 } else
1721                         return -1;
1722                 if (ret < 0) {
1723                         DBG("Processing appcontrol failed\n");
1724                         return ret;
1725                 }
1726         }
1727         if (appcontrol->operation) {
1728                 LISTHEAD(appcontrol->operation, tmp1);
1729                 appcontrol->operation = tmp1;
1730         }
1731         if (appcontrol->uri) {
1732                 LISTHEAD(appcontrol->uri, tmp2);
1733                 appcontrol->uri = tmp2;
1734         }
1735         if (appcontrol->mime) {
1736                 LISTHEAD(appcontrol->mime, tmp3);
1737                 appcontrol->mime = tmp3;
1738         }
1739         if (appcontrol->subapp) {
1740                 LISTHEAD(appcontrol->subapp, tmp4);
1741                 appcontrol->subapp = tmp4;
1742         }
1743
1744         xmlTextReaderRead(reader);
1745         if (xmlTextReaderValue(reader))
1746                 appcontrol->text = ASCII(xmlTextReaderValue(reader));
1747
1748         return ret;
1749 }
1750
1751 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
1752 {
1753         const xmlChar *node;
1754         int ret = -1;
1755         int depth = -1;
1756         operation_x *tmp1 = NULL;
1757         uri_x *tmp2 = NULL;
1758         mime_x *tmp3 = NULL;
1759         subapp_x *tmp4 = NULL;
1760
1761         depth = xmlTextReaderDepth(reader);
1762         while ((ret = __next_child_element(reader, depth))) {
1763                 node = xmlTextReaderConstName(reader);
1764                 if (!node) {
1765                         DBG("xmlTextReaderConstName value is NULL\n");
1766                         return -1;
1767                 }
1768
1769                 if (!strcmp(ASCII(node), "operation")) {
1770                         operation_x *operation = malloc(sizeof(operation_x));
1771                         if (operation == NULL) {
1772                                 DBG("Malloc Failed\n");
1773                                 return -1;
1774                         }
1775                         memset(operation, '\0', sizeof(operation_x));
1776                         LISTADD(appsvc->operation, operation);
1777                         ret = __ps_process_operation(reader, operation);
1778                         DBG("operation processing\n");
1779                 } else if (!strcmp(ASCII(node), "uri")) {
1780                         uri_x *uri= malloc(sizeof(uri_x));
1781                         if (uri == NULL) {
1782                                 DBG("Malloc Failed\n");
1783                                 return -1;
1784                         }
1785                         memset(uri, '\0', sizeof(uri_x));
1786                         LISTADD(appsvc->uri, uri);
1787                         ret = __ps_process_uri(reader, uri);
1788                         DBG("uri processing\n");
1789                 } else if (!strcmp(ASCII(node), "mime")) {
1790                         mime_x *mime = malloc(sizeof(mime_x));
1791                         if (mime == NULL) {
1792                                 DBG("Malloc Failed\n");
1793                                 return -1;
1794                         }
1795                         memset(mime, '\0', sizeof(mime_x));
1796                         LISTADD(appsvc->mime, mime);
1797                         ret = __ps_process_mime(reader, mime);
1798                         DBG("mime processing\n");
1799                 } else if (!strcmp(ASCII(node), "subapp")) {
1800                         subapp_x *subapp = malloc(sizeof(subapp_x));
1801                         if (subapp == NULL) {
1802                                 DBG("Malloc Failed\n");
1803                                 return -1;
1804                         }
1805                         memset(subapp, '\0', sizeof(subapp_x));
1806                         LISTADD(appsvc->subapp, subapp);
1807                         ret = __ps_process_subapp(reader, subapp);
1808                         DBG("subapp processing\n");
1809                 } else
1810                         return -1;
1811                 if (ret < 0) {
1812                         DBG("Processing appsvc failed\n");
1813                         return ret;
1814                 }
1815         }
1816         if (appsvc->operation) {
1817                 LISTHEAD(appsvc->operation, tmp1);
1818                 appsvc->operation = tmp1;
1819         }
1820         if (appsvc->uri) {
1821                 LISTHEAD(appsvc->uri, tmp2);
1822                 appsvc->uri = tmp2;
1823         }
1824         if (appsvc->mime) {
1825                 LISTHEAD(appsvc->mime, tmp3);
1826                 appsvc->mime = tmp3;
1827         }
1828         if (appsvc->subapp) {
1829                 LISTHEAD(appsvc->subapp, tmp4);
1830                 appsvc->subapp = tmp4;
1831         }
1832
1833         xmlTextReaderRead(reader);
1834         if (xmlTextReaderValue(reader))
1835                 appsvc->text = ASCII(xmlTextReaderValue(reader));
1836
1837         return ret;
1838 }
1839
1840 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
1841 {
1842         const xmlChar *node;
1843         int ret = -1;
1844         int depth = -1;
1845         condition_x *tmp1 = NULL;
1846
1847         depth = xmlTextReaderDepth(reader);
1848         while ((ret = __next_child_element(reader, depth))) {
1849                 node = xmlTextReaderConstName(reader);
1850                 if (!node) {
1851                         DBG("xmlTextReaderConstName value is NULL\n");
1852                         return -1;
1853                 }
1854
1855                 if (strcmp(ASCII(node), "condition") == 0) {
1856                         condition_x *condition = malloc(sizeof(condition_x));
1857                         if (condition == NULL) {
1858                                 DBG("Malloc Failed\n");
1859                                 return -1;
1860                         }
1861                         memset(condition, '\0', sizeof(condition_x));
1862                         LISTADD(launchconditions->condition, condition);
1863                         ret = __ps_process_condition(reader, condition);
1864                 } else
1865                         return -1;
1866                 if (ret < 0) {
1867                         DBG("Processing launchconditions failed\n");
1868                         return ret;
1869                 }
1870         }
1871         if (launchconditions->condition) {
1872                 LISTHEAD(launchconditions->condition, tmp1);
1873                 launchconditions->condition = tmp1;
1874         }
1875
1876         xmlTextReaderRead(reader);
1877         if (xmlTextReaderValue(reader))
1878                 launchconditions->text = ASCII(xmlTextReaderValue(reader));
1879
1880         return ret;
1881 }
1882
1883 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
1884 {
1885         const xmlChar *node;
1886         int ret = -1;
1887         int depth = -1;
1888         define_x *tmp1 = NULL;
1889         request_x *tmp2 = NULL;
1890         depth = xmlTextReaderDepth(reader);
1891         while ((ret = __next_child_element(reader, depth))) {
1892                 node = xmlTextReaderConstName(reader);
1893                 if (!node) {
1894                         DBG("xmlTextReaderConstName value is NULL\n");
1895                         return -1;
1896                 }
1897
1898                 if (!strcmp(ASCII(node), "define")) {
1899                         define_x *define= malloc(sizeof(define_x));
1900                         if (define == NULL) {
1901                                 DBG("Malloc Failed\n");
1902                                 return -1;
1903                         }
1904                         memset(define, '\0', sizeof(define_x));
1905                         LISTADD(datashare->define, define);
1906                         ret = __ps_process_define(reader, define);
1907                 } else if (!strcmp(ASCII(node), "request")) {
1908                         request_x *request= malloc(sizeof(request_x));
1909                         if (request == NULL) {
1910                                 DBG("Malloc Failed\n");
1911                                 return -1;
1912                         }
1913                         memset(request, '\0', sizeof(request_x));
1914                         LISTADD(datashare->request, request);
1915                         ret = __ps_process_request(reader, request);
1916                 } else
1917                         return -1;
1918                 if (ret < 0) {
1919                         DBG("Processing data-share failed\n");
1920                         return ret;
1921                 }
1922         }
1923         if (datashare->define) {
1924                 LISTHEAD(datashare->define, tmp1);
1925                 datashare->define = tmp1;
1926         }
1927         if (datashare->request) {
1928                 LISTHEAD(datashare->request, tmp2);
1929                 datashare->request = tmp2;
1930         }
1931         return ret;
1932 }
1933
1934 static int __ps_process_layout(xmlTextReaderPtr reader, layout_x *layout)
1935 {
1936         /*TODO: once policy is set*/
1937         return 0;
1938 }
1939
1940 static char*
1941 __get_icon_with_path(char* icon)
1942 {
1943         if (!icon)
1944                 return NULL;
1945
1946         if (index(icon, '/') == NULL) {
1947                 char* theme = NULL;
1948                 char* icon_with_path = NULL;
1949                 int len;
1950
1951                 if (!package)
1952                         return NULL;
1953
1954                 theme = vconf_get_str("db/setting/theme");
1955                 if (!theme) {
1956                         theme = strdup("default");
1957                         if(!theme) {
1958                                 return NULL;
1959                         }
1960                 }
1961
1962                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
1963                 icon_with_path = malloc(len);
1964                 if(icon_with_path == NULL) {
1965                         DBG("(icon_with_path == NULL) return\n");
1966                         free(theme);
1967                         return NULL;
1968                 }
1969
1970                 memset(icon_with_path, 0, len);
1971
1972                 snprintf(icon_with_path, len, "/opt/share/icons/%s/small/%s", theme, icon);
1973                 do {
1974                         if (access(icon_with_path, R_OK) == 0) break;
1975                         snprintf(icon_with_path, len, "/usr/share/icons/%s/small/%s", theme, icon);
1976                         if (access(icon_with_path, R_OK) == 0) break;
1977                         DBG("cannot find icon %s", icon_with_path);
1978                         snprintf(icon_with_path, len,"/opt/share/icons/default/small/%s", icon);
1979                         if (access(icon_with_path, R_OK) == 0) break;
1980                         snprintf(icon_with_path, len, "/usr/share/icons/default/small/%s", icon);
1981                         if (access(icon_with_path, R_OK) == 0) break;
1982
1983                         /* icon path is going to be moved intto the app directory */
1984                         DBGE("icon file must be moved to %s", icon_with_path);
1985                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/%s/small/%s", package, theme, icon);
1986                         if (access(icon_with_path, R_OK) == 0) break;
1987                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/%s/small/%s", package, theme, icon);
1988                         if (access(icon_with_path, R_OK) == 0) break;
1989                         DBG("cannot find icon %s", icon_with_path);
1990                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/default/small/%s", package, icon);
1991                         if (access(icon_with_path, R_OK) == 0) break;
1992                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/default/small/%s", package, icon);
1993                         if (access(icon_with_path, R_OK) == 0) break;
1994                 } while (0);
1995
1996                 free(theme);
1997
1998                 DBG("Icon path : %s ---> %s", icon, icon_with_path);
1999
2000                 return icon_with_path;
2001         } else {
2002                 char* confirmed_icon = NULL;
2003
2004                 confirmed_icon = strdup(icon);
2005                 if (!confirmed_icon)
2006                         return NULL;
2007                 return confirmed_icon;
2008         }
2009 }
2010
2011
2012 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon)
2013 {
2014         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2015                 icon->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2016         if (xmlTextReaderConstXmlLang(reader)) {
2017                 icon->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2018                 if (icon->lang == NULL)
2019                         icon->lang = strdup(DEFAULT_LOCALE);
2020         } else {
2021                 icon->lang = strdup(DEFAULT_LOCALE);
2022         }
2023         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
2024                 icon->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
2025         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
2026                 icon->size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
2027         if (xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")))
2028                 icon->resolution = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")));
2029         xmlTextReaderRead(reader);
2030         if (xmlTextReaderValue(reader)) {
2031                 char *text  = ASCII(xmlTextReaderValue(reader));
2032                 if(text) {
2033                         icon->text = __get_icon_with_path(text);
2034                         free(text);
2035                 }
2036         }
2037
2038         return 0;
2039 }
2040
2041 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
2042 {
2043         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2044                 label->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2045         if (xmlTextReaderConstXmlLang(reader)) {
2046                 label->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2047                 if (label->lang == NULL)
2048                         label->lang = strdup(DEFAULT_LOCALE);
2049         } else {
2050                 label->lang = strdup(DEFAULT_LOCALE);
2051         }
2052         xmlTextReaderRead(reader);
2053         if (xmlTextReaderValue(reader))
2054                 label->text = ASCII(xmlTextReaderValue(reader));
2055
2056 /*      DBG("lable name %s\n", label->name);
2057         DBG("lable lang %s\n", label->lang);
2058         DBG("lable text %s\n", label->text);
2059 */
2060         return 0;
2061
2062 }
2063
2064 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
2065 {
2066         if (xmlTextReaderGetAttribute(reader, XMLCHAR("email")))
2067                 author->email = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("email")));
2068         if (xmlTextReaderGetAttribute(reader, XMLCHAR("href")))
2069                 author->href = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("href")));
2070         if (xmlTextReaderConstXmlLang(reader)) {
2071                 author->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2072                 if (author->lang == NULL)
2073                         author->lang = strdup(DEFAULT_LOCALE);
2074         } else {
2075                 author->lang = strdup(DEFAULT_LOCALE);
2076         }
2077         xmlTextReaderRead(reader);
2078         if (xmlTextReaderValue(reader))
2079                 author->text = ASCII(xmlTextReaderValue(reader));
2080         return 0;
2081 }
2082
2083 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
2084 {
2085         if (xmlTextReaderConstXmlLang(reader)) {
2086                 description->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2087                 if (description->lang == NULL)
2088                         description->lang = strdup(DEFAULT_LOCALE);
2089         } else {
2090                 description->lang = strdup(DEFAULT_LOCALE);
2091         }
2092         xmlTextReaderRead(reader);
2093         if (xmlTextReaderValue(reader))
2094                 description->text = ASCII(xmlTextReaderValue(reader));
2095         return 0;
2096 }
2097
2098 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
2099 {
2100         if (xmlTextReaderConstXmlLang(reader)) {
2101                 license->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2102                 if (license->lang == NULL)
2103                         license->lang = strdup(DEFAULT_LOCALE);
2104         } else {
2105                 license->lang = strdup(DEFAULT_LOCALE);
2106         }
2107         xmlTextReaderRead(reader);
2108         if (xmlTextReaderValue(reader))
2109                 license->text = ASCII(xmlTextReaderValue(reader));
2110         return 0;
2111 }
2112
2113 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability)
2114 {
2115         const xmlChar *node;
2116         int ret = -1;
2117         int depth = -1;
2118         resolution_x *tmp1 = NULL;
2119
2120         if (xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")))
2121                 capability->operationid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")));
2122
2123         depth = xmlTextReaderDepth(reader);
2124         while ((ret = __next_child_element(reader, depth))) {
2125                 node = xmlTextReaderConstName(reader);
2126                 if (!node) {
2127                         DBG("xmlTextReaderConstName value is NULL\n");
2128                         return -1;
2129                 }
2130
2131                 if (!strcmp(ASCII(node), "resolution")) {
2132                         resolution_x *resolution = malloc(sizeof(resolution_x));
2133                         if (resolution == NULL) {
2134                                 DBG("Malloc Failed\n");
2135                                 return -1;
2136                         }
2137                         memset(resolution, '\0', sizeof(resolution_x));
2138                         LISTADD(capability->resolution, resolution);
2139                         ret = __ps_process_resolution(reader, resolution);
2140                 } else
2141                         return -1;
2142                 if (ret < 0) {
2143                         DBG("Processing capability failed\n");
2144                         return ret;
2145                 }
2146         }
2147
2148         if (capability->resolution) {
2149                 LISTHEAD(capability->resolution, tmp1);
2150                 capability->resolution = tmp1;
2151         }
2152
2153         return ret;
2154 }
2155
2156 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
2157 {
2158         const xmlChar *node;
2159         int ret = -1;
2160         int depth = -1;
2161         capability_x *tmp1 = NULL;
2162
2163         if (xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")))
2164                 datacontrol->providerid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")));
2165
2166         depth = xmlTextReaderDepth(reader);
2167         while ((ret = __next_child_element(reader, depth))) {
2168                 node = xmlTextReaderConstName(reader);
2169                 if (!node) {
2170                         DBG("xmlTextReaderConstName value is NULL\n");
2171                         return -1;
2172                 }
2173
2174                 if (!strcmp(ASCII(node), "capability")) {
2175                         capability_x *capability = malloc(sizeof(capability_x));
2176                         if (capability == NULL) {
2177                                 DBG("Malloc Failed\n");
2178                                 return -1;
2179                         }
2180                         memset(capability, '\0', sizeof(capability_x));
2181                         LISTADD(datacontrol->capability, capability);
2182                         ret = __ps_process_capability(reader, capability);
2183                 } else
2184                         return -1;
2185                 if (ret < 0) {
2186                         DBG("Processing datacontrol failed\n");
2187                         return ret;
2188                 }
2189         }
2190
2191         if (datacontrol->capability) {
2192                 LISTHEAD(datacontrol->capability, tmp1);
2193                 datacontrol->capability = tmp1;
2194         }
2195
2196         return ret;
2197 }
2198
2199 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication)
2200 {
2201         const xmlChar *node;
2202         int ret = -1;
2203         int depth = -1;
2204         char *newappid = NULL;
2205         label_x *tmp1 = NULL;
2206         icon_x *tmp2 = NULL;
2207         appsvc_x *tmp3 = NULL;
2208         appcontrol_x *tmp4 = NULL;
2209         launchconditions_x *tmp5 = NULL;
2210         notification_x *tmp6 = NULL;
2211         datashare_x *tmp7 = NULL;
2212         category_x *tmp8 = NULL;
2213
2214         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
2215                 uiapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
2216                 if (uiapplication->appid == NULL) {
2217                         DBG("appid cant be NULL\n");
2218                         return -1;
2219                 }
2220         } else {
2221                 DBG("appid is mandatory\n");
2222                 return -1;
2223         }
2224         /*check appid*/
2225         ret = __validate_appid(package, uiapplication->appid, &newappid);
2226         if (ret == -1) {
2227                 DBG("appid is not proper\n");
2228                 return -1;
2229         } else {
2230                 if (newappid) {
2231                         if (uiapplication->appid)
2232                                 free((void *)uiapplication->appid);
2233                         uiapplication->appid = newappid;
2234                 }
2235         }
2236         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
2237                 uiapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
2238         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay"))) {
2239                 uiapplication->nodisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay")));
2240                 if (uiapplication->nodisplay == NULL)
2241                         uiapplication->nodisplay = strdup("false");
2242         } else {
2243                 uiapplication->nodisplay = strdup("false");
2244         }
2245         if (xmlTextReaderGetAttribute(reader, XMLCHAR("multiple"))) {
2246                 uiapplication->multiple = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("multiple")));
2247                 if (uiapplication->multiple == NULL)
2248                         uiapplication->multiple = strdup("false");
2249         } else {
2250                 uiapplication->multiple = strdup("false");
2251         }
2252         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2253                 uiapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2254         if (xmlTextReaderGetAttribute(reader, XMLCHAR("categories")))
2255                 uiapplication->categories = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("categories")));
2256         if (xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")))
2257                 uiapplication->extraid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")));
2258         if (xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage"))) {
2259                 uiapplication->taskmanage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage")));
2260                 if (uiapplication->taskmanage == NULL)
2261                         uiapplication->taskmanage = strdup("true");
2262         } else {
2263                 uiapplication->taskmanage = strdup("true");
2264         }
2265         if (xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration"))) {
2266                 uiapplication->hwacceleration = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration")));
2267                 if (uiapplication->hwacceleration == NULL)
2268                         uiapplication->hwacceleration = strdup("use-system-setting");
2269         } else {
2270                 uiapplication->hwacceleration = strdup("use-system-setting");
2271         }
2272         if (xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")))
2273                 uiapplication->recentimage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")));
2274         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp"))) {
2275                 uiapplication->mainapp = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp")));
2276                 if (uiapplication->mainapp == NULL)
2277                         uiapplication->mainapp = strdup("false");
2278         } else {
2279                 uiapplication->mainapp = strdup("false");
2280         }
2281
2282         depth = xmlTextReaderDepth(reader);
2283         while ((ret = __next_child_element(reader, depth))) {
2284                 node = xmlTextReaderConstName(reader);
2285                 if (!node) {
2286                         DBG("xmlTextReaderConstName value is NULL\n");
2287                         return -1;
2288                 }
2289                 if (!strcmp(ASCII(node), "label")) {
2290                         label_x *label = malloc(sizeof(label_x));
2291                         if (label == NULL) {
2292                                 DBG("Malloc Failed\n");
2293                                 return -1;
2294                         }
2295                         memset(label, '\0', sizeof(label_x));
2296                         LISTADD(uiapplication->label, label);
2297                         ret = __ps_process_label(reader, label);
2298                 } else if (!strcmp(ASCII(node), "icon")) {
2299                         icon_x *icon = malloc(sizeof(icon_x));
2300                         if (icon == NULL) {
2301                                 DBG("Malloc Failed\n");
2302                                 return -1;
2303                         }
2304                         memset(icon, '\0', sizeof(icon_x));
2305                         LISTADD(uiapplication->icon, icon);
2306                         ret = __ps_process_icon(reader, icon);
2307                 } else if (!strcmp(ASCII(node), "category")) {
2308                         category_x *category = malloc(sizeof(category_x));
2309                         if (category == NULL) {
2310                                 DBG("Malloc Failed\n");
2311                                 return -1;
2312                         }
2313                         memset(category, '\0', sizeof(category_x));
2314                         LISTADD(uiapplication->category, category);
2315                         ret = __ps_process_category(reader, category);
2316                 } else if (!strcmp(ASCII(node), "app-control")) {
2317                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
2318                         if (appcontrol == NULL) {
2319                                 DBG("Malloc Failed\n");
2320                                 return -1;
2321                         }
2322                         memset(appcontrol, '\0', sizeof(appcontrol_x));
2323                         LISTADD(uiapplication->appcontrol, appcontrol);
2324                         ret = __ps_process_appcontrol(reader, appcontrol);
2325                 } else if (!strcmp(ASCII(node), "application-service")) {
2326                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
2327                         if (appsvc == NULL) {
2328                                 DBG("Malloc Failed\n");
2329                                 return -1;
2330                         }
2331                         memset(appsvc, '\0', sizeof(appsvc_x));
2332                         LISTADD(uiapplication->appsvc, appsvc);
2333                         ret = __ps_process_appsvc(reader, appsvc);
2334                 } else if (!strcmp(ASCII(node), "data-share")) {
2335                         datashare_x *datashare = malloc(sizeof(datashare_x));
2336                         if (datashare == NULL) {
2337                                 DBG("Malloc Failed\n");
2338                                 return -1;
2339                         }
2340                         memset(datashare, '\0', sizeof(datashare_x));
2341                         LISTADD(uiapplication->datashare, datashare);
2342                         ret = __ps_process_datashare(reader, datashare);
2343                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
2344                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
2345                         if (launchconditions == NULL) {
2346                                 DBG("Malloc Failed\n");
2347                                 return -1;
2348                         }
2349                         memset(launchconditions, '\0', sizeof(launchconditions_x));
2350                         LISTADD(uiapplication->launchconditions, launchconditions);
2351                         ret = __ps_process_launchconditions(reader, launchconditions);
2352                 } else if (!strcmp(ASCII(node), "notification")) {
2353                         notification_x *notification = malloc(sizeof(notification_x));
2354                         if (notification == NULL) {
2355                                 DBG("Malloc Failed\n");
2356                                 return -1;
2357                         }
2358                         memset(notification, '\0', sizeof(notification_x));
2359                         LISTADD(uiapplication->notification, notification);
2360                         ret = __ps_process_notification(reader, notification);
2361                 } else
2362                         return -1;
2363                 if (ret < 0) {
2364                         DBG("Processing uiapplication failed\n");
2365                         return ret;
2366                 }
2367         }
2368
2369         if (uiapplication->label) {
2370                 LISTHEAD(uiapplication->label, tmp1);
2371                 uiapplication->label = tmp1;
2372         }
2373         if (uiapplication->icon) {
2374                 LISTHEAD(uiapplication->icon, tmp2);
2375                 uiapplication->icon = tmp2;
2376         }
2377         if (uiapplication->appsvc) {
2378                 LISTHEAD(uiapplication->appsvc, tmp3);
2379                 uiapplication->appsvc = tmp3;
2380         }
2381         if (uiapplication->appcontrol) {
2382                 LISTHEAD(uiapplication->appcontrol, tmp4);
2383                 uiapplication->appcontrol = tmp4;
2384         }
2385         if (uiapplication->launchconditions) {
2386                 LISTHEAD(uiapplication->launchconditions, tmp5);
2387                 uiapplication->launchconditions = tmp5;
2388         }
2389         if (uiapplication->notification) {
2390                 LISTHEAD(uiapplication->notification, tmp6);
2391                 uiapplication->notification = tmp6;
2392         }
2393         if (uiapplication->datashare) {
2394                 LISTHEAD(uiapplication->datashare, tmp7);
2395                 uiapplication->datashare = tmp7;
2396         }
2397         if (uiapplication->category) {
2398                 LISTHEAD(uiapplication->category, tmp8);
2399                 uiapplication->category = tmp8;
2400         }
2401
2402         return ret;
2403 }
2404
2405 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication)
2406 {
2407         const xmlChar *node;
2408         int ret = -1;
2409         int depth = -1;
2410         char *newappid = NULL;
2411         label_x *tmp1 = NULL;
2412         icon_x *tmp2 = NULL;
2413         appsvc_x *tmp3 = NULL;
2414         appcontrol_x *tmp4 = NULL;
2415         datacontrol_x *tmp5 = NULL;
2416         launchconditions_x *tmp6 = NULL;
2417         notification_x *tmp7 = NULL;
2418         datashare_x *tmp8 = NULL;
2419         category_x *tmp9 = NULL;
2420
2421         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
2422                 serviceapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
2423                 if (serviceapplication->appid == NULL) {
2424                         DBG("appid cant be NULL\n");
2425                         return -1;
2426                 }
2427         } else {
2428                 DBG("appid is mandatory\n");
2429                 return -1;
2430         }
2431         /*check appid*/
2432         ret = __validate_appid(package, serviceapplication->appid, &newappid);
2433         if (ret == -1) {
2434                 DBG("appid is not proper\n");
2435                 return -1;
2436         } else {
2437                 if (newappid) {
2438                         if (serviceapplication->appid)
2439                                 free((void *)serviceapplication->appid);
2440                         serviceapplication->appid = newappid;
2441                 }
2442         }
2443         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
2444                 serviceapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
2445         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2446                 serviceapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2447         if (xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot"))) {
2448                 serviceapplication->onboot = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot")));
2449                 if (serviceapplication->onboot == NULL)
2450                         serviceapplication->onboot = strdup("false");
2451         } else {
2452                 serviceapplication->onboot = strdup("false");
2453         }
2454         if (xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart"))) {
2455                 serviceapplication->autorestart = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart")));
2456                 if (serviceapplication->autorestart == NULL)
2457                         serviceapplication->autorestart = strdup("false");
2458         } else {
2459                 serviceapplication->autorestart = strdup("false");
2460         }
2461
2462         depth = xmlTextReaderDepth(reader);
2463         while ((ret = __next_child_element(reader, depth))) {
2464                 node = xmlTextReaderConstName(reader);
2465                 if (!node) {
2466                         DBG("xmlTextReaderConstName value is NULL\n");
2467                         return -1;
2468                 }
2469
2470                 if (!strcmp(ASCII(node), "label")) {
2471                         label_x *label = malloc(sizeof(label_x));
2472                         if (label == NULL) {
2473                                 DBG("Malloc Failed\n");
2474                                 return -1;
2475                         }
2476                         memset(label, '\0', sizeof(label_x));
2477                         LISTADD(serviceapplication->label, label);
2478                         ret = __ps_process_label(reader, label);
2479                 } else if (!strcmp(ASCII(node), "icon")) {
2480                         icon_x *icon = malloc(sizeof(icon_x));
2481                         if (icon == NULL) {
2482                                 DBG("Malloc Failed\n");
2483                                 return -1;
2484                         }
2485                         memset(icon, '\0', sizeof(icon_x));
2486                         LISTADD(serviceapplication->icon, icon);
2487                         ret = __ps_process_icon(reader, icon);
2488                 } else if (!strcmp(ASCII(node), "category")) {
2489                         category_x *category = malloc(sizeof(category_x));
2490                         if (category == NULL) {
2491                                 DBG("Malloc Failed\n");
2492                                 return -1;
2493                         }
2494                         memset(category, '\0', sizeof(category_x));
2495                         LISTADD(serviceapplication->category, category);
2496                         ret = __ps_process_category(reader, category);
2497                 } else if (!strcmp(ASCII(node), "app-control")) {
2498                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
2499                         if (appcontrol == NULL) {
2500                                 DBG("Malloc Failed\n");
2501                                 return -1;
2502                         }
2503                         memset(appcontrol, '\0', sizeof(appcontrol_x));
2504                         LISTADD(serviceapplication->appcontrol, appcontrol);
2505                         ret = __ps_process_appcontrol(reader, appcontrol);
2506                 } else if (!strcmp(ASCII(node), "application-service")) {
2507                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
2508                         if (appsvc == NULL) {
2509                                 DBG("Malloc Failed\n");
2510                                 return -1;
2511                         }
2512                         memset(appsvc, '\0', sizeof(appsvc_x));
2513                         LISTADD(serviceapplication->appsvc, appsvc);
2514                         ret = __ps_process_appsvc(reader, appsvc);
2515                 } else if (!strcmp(ASCII(node), "data-share")) {
2516                         datashare_x *datashare = malloc(sizeof(datashare_x));
2517                         if (datashare == NULL) {
2518                                 DBG("Malloc Failed\n");
2519                                 return -1;
2520                         }
2521                         memset(datashare, '\0', sizeof(datashare_x));
2522                         LISTADD(serviceapplication->datashare, datashare);
2523                         ret = __ps_process_datashare(reader, datashare);
2524                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
2525                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
2526                         if (launchconditions == NULL) {
2527                                 DBG("Malloc Failed\n");
2528                                 return -1;
2529                         }
2530                         memset(launchconditions, '\0', sizeof(launchconditions_x));
2531                         LISTADD(serviceapplication->launchconditions, launchconditions);
2532                         ret = __ps_process_launchconditions(reader, launchconditions);
2533                 } else if (!strcmp(ASCII(node), "notification")) {
2534                         notification_x *notification = malloc(sizeof(notification_x));
2535                         if (notification == NULL) {
2536                                 DBG("Malloc Failed\n");
2537                                 return -1;
2538                         }
2539                         memset(notification, '\0', sizeof(notification_x));
2540                         LISTADD(serviceapplication->notification, notification);
2541                         ret = __ps_process_notification(reader, notification);
2542                 } else if (!strcmp(ASCII(node), "data-control")) {
2543                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
2544                         if (datacontrol == NULL) {
2545                                 DBG("Malloc Failed\n");
2546                                 return -1;
2547                         }
2548                         memset(datacontrol, '\0', sizeof(datacontrol_x));
2549                         LISTADD(serviceapplication->datacontrol, datacontrol);
2550                         ret = __ps_process_datacontrol(reader, datacontrol);
2551                 } else
2552                         return -1;
2553                 if (ret < 0) {
2554                         DBG("Processing serviceapplication failed\n");
2555                         return ret;
2556                 }
2557         }
2558
2559         if (serviceapplication->label) {
2560                 LISTHEAD(serviceapplication->label, tmp1);
2561                 serviceapplication->label = tmp1;
2562         }
2563         if (serviceapplication->icon) {
2564                 LISTHEAD(serviceapplication->icon, tmp2);
2565                 serviceapplication->icon = tmp2;
2566         }
2567         if (serviceapplication->appsvc) {
2568                 LISTHEAD(serviceapplication->appsvc, tmp3);
2569                 serviceapplication->appsvc = tmp3;
2570         }
2571         if (serviceapplication->appcontrol) {
2572                 LISTHEAD(serviceapplication->appcontrol, tmp4);
2573                 serviceapplication->appcontrol = tmp4;
2574         }
2575         if (serviceapplication->datacontrol) {
2576                 LISTHEAD(serviceapplication->datacontrol, tmp5);
2577                 serviceapplication->datacontrol = tmp5;
2578         }
2579         if (serviceapplication->launchconditions) {
2580                 LISTHEAD(serviceapplication->launchconditions, tmp6);
2581                 serviceapplication->launchconditions = tmp6;
2582         }
2583         if (serviceapplication->notification) {
2584                 LISTHEAD(serviceapplication->notification, tmp7);
2585                 serviceapplication->notification = tmp7;
2586         }
2587         if (serviceapplication->datashare) {
2588                 LISTHEAD(serviceapplication->datashare, tmp8);
2589                 serviceapplication->datashare = tmp8;
2590         }
2591         if (serviceapplication->category) {
2592                 LISTHEAD(serviceapplication->category, tmp9);
2593                 serviceapplication->category = tmp9;
2594         }
2595
2596         return ret;
2597 }
2598
2599 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
2600 {
2601         /*TODO: once policy is set*/
2602         return 0;
2603 }
2604
2605 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
2606 {
2607         /*TODO: once policy is set*/
2608         return 0;
2609 }
2610
2611 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
2612 {
2613         /*TODO: once policy is set*/
2614         return 0;
2615 }
2616
2617 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
2618 {
2619         /*TODO: once policy is set*/
2620         return 0;
2621 }
2622
2623 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
2624 {
2625         /*TODO: once policy is set*/
2626         return 0;
2627 }
2628
2629 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx)
2630 {
2631         DBG("__start_process\n");
2632         const xmlChar *node;
2633         int ret = -1;
2634         int depth = -1;
2635         label_x *tmp1 = NULL;
2636         author_x *tmp2 = NULL;
2637         description_x *tmp3 = NULL;
2638         license_x *tmp4 = NULL;
2639         uiapplication_x *tmp5 = NULL;
2640         serviceapplication_x *tmp6 = NULL;
2641         daemon_x *tmp7 = NULL;
2642         theme_x *tmp8 = NULL;
2643         font_x *tmp9 = NULL;
2644         ime_x *tmp10 = NULL;
2645         icon_x *tmp11 = NULL;
2646         compatibility_x *tmp12 = NULL;
2647         deviceprofile_x *tmp13 = NULL;
2648
2649         depth = xmlTextReaderDepth(reader);
2650         while ((ret = __next_child_element(reader, depth))) {
2651                 node = xmlTextReaderConstName(reader);
2652                 if (!node) {
2653                         DBG("xmlTextReaderConstName value is NULL\n");
2654                         return -1;
2655                 }
2656
2657                 if (!strcmp(ASCII(node), "label")) {
2658                         label_x *label = malloc(sizeof(label_x));
2659                         if (label == NULL) {
2660                                 DBG("Malloc Failed\n");
2661                                 return -1;
2662                         }
2663                         memset(label, '\0', sizeof(label_x));
2664                         LISTADD(mfx->label, label);
2665                         ret = __ps_process_label(reader, label);
2666                 } else if (!strcmp(ASCII(node), "author")) {
2667                         author_x *author = malloc(sizeof(author_x));
2668                         if (author == NULL) {
2669                                 DBG("Malloc Failed\n");
2670                                 return -1;
2671                         }
2672                         memset(author, '\0', sizeof(author_x));
2673                         LISTADD(mfx->author, author);
2674                         ret = __ps_process_author(reader, author);
2675                 } else if (!strcmp(ASCII(node), "description")) {
2676                         description_x *description = malloc(sizeof(description_x));
2677                         if (description == NULL) {
2678                                 DBG("Malloc Failed\n");
2679                                 return -1;
2680                         }
2681                         memset(description, '\0', sizeof(description_x));
2682                         LISTADD(mfx->description, description);
2683                         ret = __ps_process_description(reader, description);
2684                 } else if (!strcmp(ASCII(node), "license")) {
2685                         license_x *license = malloc(sizeof(license_x));
2686                         if (license == NULL) {
2687                                 DBG("Malloc Failed\n");
2688                                 return -1;
2689                         }
2690                         memset(license, '\0', sizeof(license_x));
2691                         LISTADD(mfx->license, license);
2692                         ret = __ps_process_license(reader, license);
2693                 } else if (!strcmp(ASCII(node), "ui-application")) {
2694                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
2695                         if (uiapplication == NULL) {
2696                                 DBG("Malloc Failed\n");
2697                                 return -1;
2698                         }
2699                         memset(uiapplication, '\0', sizeof(uiapplication_x));
2700                         LISTADD(mfx->uiapplication, uiapplication);
2701                         ret = __ps_process_uiapplication(reader, uiapplication);
2702                 } else if (!strcmp(ASCII(node), "service-application")) {
2703                         serviceapplication_x *serviceapplication = malloc(sizeof(serviceapplication_x));
2704                         if (serviceapplication == NULL) {
2705                                 DBG("Malloc Failed\n");
2706                                 return -1;
2707                         }
2708                         memset(serviceapplication, '\0', sizeof(serviceapplication_x));
2709                         LISTADD(mfx->serviceapplication, serviceapplication);
2710                         ret = __ps_process_serviceapplication(reader, serviceapplication);
2711                 } else if (!strcmp(ASCII(node), "daemon")) {
2712                         daemon_x *daemon = malloc(sizeof(daemon_x));
2713                         if (daemon == NULL) {
2714                                 DBG("Malloc Failed\n");
2715                                 return -1;
2716                         }
2717                         memset(daemon, '\0', sizeof(daemon_x));
2718                         LISTADD(mfx->daemon, daemon);
2719                         ret = __ps_process_daemon(reader, daemon);
2720                 } else if (!strcmp(ASCII(node), "theme")) {
2721                         theme_x *theme = malloc(sizeof(theme_x));
2722                         if (theme == NULL) {
2723                                 DBG("Malloc Failed\n");
2724                                 return -1;
2725                         }
2726                         memset(theme, '\0', sizeof(theme_x));
2727                         LISTADD(mfx->theme, theme);
2728                         ret = __ps_process_theme(reader, theme);
2729                 } else if (!strcmp(ASCII(node), "font")) {
2730                         font_x *font = malloc(sizeof(font_x));
2731                         if (font == NULL) {
2732                                 DBG("Malloc Failed\n");
2733                                 return -1;
2734                         }
2735                         memset(font, '\0', sizeof(font_x));
2736                         LISTADD(mfx->font, font);
2737                         ret = __ps_process_font(reader, font);
2738                 } else if (!strcmp(ASCII(node), "ime")) {
2739                         ime_x *ime = malloc(sizeof(ime_x));
2740                         if (ime == NULL) {
2741                                 DBG("Malloc Failed\n");
2742                                 return -1;
2743                         }
2744                         memset(ime, '\0', sizeof(ime_x));
2745                         LISTADD(mfx->ime, ime);
2746                         ret = __ps_process_ime(reader, ime);
2747                 } else if (!strcmp(ASCII(node), "icon")) {
2748                         icon_x *icon = malloc(sizeof(icon_x));
2749                         if (icon == NULL) {
2750                                 DBG("Malloc Failed\n");
2751                                 return -1;
2752                         }
2753                         memset(icon, '\0', sizeof(icon_x));
2754                         LISTADD(mfx->icon, icon);
2755                         ret = __ps_process_icon(reader, icon);
2756                 } else if (!strcmp(ASCII(node), "device-profile")) {
2757                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
2758                         if (deviceprofile == NULL) {
2759                                 DBG("Malloc Failed\n");
2760                                 return -1;
2761                         }
2762                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
2763                         LISTADD(mfx->deviceprofile, deviceprofile);
2764                         ret = __ps_process_deviceprofile(reader, deviceprofile);
2765                 } else if (!strcmp(ASCII(node), "compatibility")) {
2766                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
2767                         if (compatibility == NULL) {
2768                                 DBG("Malloc Failed\n");
2769                                 return -1;
2770                         }
2771                         memset(compatibility, '\0', sizeof(compatibility_x));
2772                         LISTADD(mfx->compatibility, compatibility);
2773                         ret = __ps_process_compatibility(reader, compatibility);
2774                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
2775                         continue;
2776                 } else if (!strcmp(ASCII(node), "livebox")) {
2777                         continue;
2778                 } else if (!strcmp(ASCII(node), "Accounts")) {
2779                         continue;
2780                 } else if (!strcmp(ASCII(node), "account")) {
2781                         continue;
2782                 } else
2783                         return -1;
2784
2785                 if (ret < 0) {
2786                         DBG("Processing manifest failed\n");
2787                         return ret;
2788                 }
2789         }
2790         if (mfx->label) {
2791                 LISTHEAD(mfx->label, tmp1);
2792                 mfx->label = tmp1;
2793         }
2794         if (mfx->author) {
2795                 LISTHEAD(mfx->author, tmp2);
2796                 mfx->author = tmp2;
2797         }
2798         if (mfx->description) {
2799                 LISTHEAD(mfx->description, tmp3);
2800                 mfx->description= tmp3;
2801         }
2802         if (mfx->license) {
2803                 LISTHEAD(mfx->license, tmp4);
2804                 mfx->license= tmp4;
2805         }
2806         if (mfx->uiapplication) {
2807                 LISTHEAD(mfx->uiapplication, tmp5);
2808                 mfx->uiapplication = tmp5;
2809         }
2810         if (mfx->serviceapplication) {
2811                 LISTHEAD(mfx->serviceapplication, tmp6);
2812                 mfx->serviceapplication = tmp6;
2813         }
2814         if (mfx->daemon) {
2815                 LISTHEAD(mfx->daemon, tmp7);
2816                 mfx->daemon= tmp7;
2817         }
2818         if (mfx->theme) {
2819                 LISTHEAD(mfx->theme, tmp8);
2820                 mfx->theme= tmp8;
2821         }
2822         if (mfx->font) {
2823                 LISTHEAD(mfx->font, tmp9);
2824                 mfx->font= tmp9;
2825         }
2826         if (mfx->ime) {
2827                 LISTHEAD(mfx->ime, tmp10);
2828                 mfx->ime= tmp10;
2829         }
2830         if (mfx->icon) {
2831                 LISTHEAD(mfx->icon, tmp11);
2832                 mfx->icon= tmp11;
2833         }
2834         if (mfx->compatibility) {
2835                 LISTHEAD(mfx->compatibility, tmp12);
2836                 mfx->compatibility= tmp12;
2837         }
2838         if (mfx->deviceprofile) {
2839                 LISTHEAD(mfx->deviceprofile, tmp13);
2840                 mfx->deviceprofile= tmp13;
2841         }
2842
2843         return ret;
2844 }
2845
2846 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx)
2847 {
2848         const xmlChar *node;
2849         int ret = -1;
2850
2851         if ((ret = __next_child_element(reader, -1))) {
2852                 node = xmlTextReaderConstName(reader);
2853                 if (!node) {
2854                         DBG("xmlTextReaderConstName value is NULL\n");
2855                         return -1;
2856                 }
2857
2858                 if (!strcmp(ASCII(node), "manifest")) {
2859                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")))
2860                                 mfx->ns = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")));
2861                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("package"))) {
2862                                 mfx->package= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("package")));
2863                                 if (mfx->package == NULL) {
2864                                         DBG("package cant be NULL\n");
2865                                         return -1;
2866                                 }
2867                         } else {
2868                                 DBG("package field is mandatory\n");
2869                                 return -1;
2870                         }
2871                         package = mfx->package;
2872                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("version")))
2873                                 mfx->version= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("version")));
2874                         /*app2ext needs package size for external installation*/
2875                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
2876                                 mfx->package_size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
2877                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")))
2878                                 mfx->installlocation = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")));
2879                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2880                                 mfx->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2881                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
2882                         mfx->preload = strdup("False");
2883                         mfx->removable = strdup("True");
2884                         mfx->readonly = strdup("False");
2885                         char buf[PKG_STRING_LEN_MAX] = {'\0'};
2886                         char *val = NULL;
2887                         time_t current_time;
2888                         time(&current_time);
2889                         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", current_time);
2890                         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
2891                         mfx->installed_time = val;
2892
2893                         ret = __start_process(reader, mfx);
2894                 } else {
2895                         DBG("No Manifest element found\n");
2896                         return -1;
2897                 }
2898         }
2899         return ret;
2900 }
2901
2902 #define DESKTOP_RW_PATH "/opt/share/applications/"
2903 #define DESKTOP_RO_PATH "/usr/share/applications/"
2904
2905 static char* __convert_to_system_locale(const char *mlocale)
2906 {
2907         if (mlocale == NULL)
2908                 return NULL;
2909         char *locale = NULL;
2910         locale = (char *)calloc(1, 6);
2911         if (!locale) {
2912                 DBGE("Malloc Failed\n");
2913                 return NULL;
2914         }
2915
2916         strncpy(locale, mlocale, 2);
2917         strncat(locale, "_", 1);
2918         locale[3] = toupper(mlocale[3]);
2919         locale[4] = toupper(mlocale[4]);
2920         return locale;
2921 }
2922
2923
2924 /* desktop shoud be generated automatically based on manifest */
2925 /* Currently removable, taskmanage, etc fields are not considerd. it will be decided soon.*/
2926 #define BUFMAX 1024*128
2927 static int __ps_make_nativeapp_desktop(manifest_x * mfx)
2928 {
2929         FILE* file = NULL;
2930         int fd = 0;
2931         char filepath[PKG_STRING_LEN_MAX] = "";
2932         char *buf = NULL;
2933         char *buftemp = NULL;
2934         char *locale = NULL;
2935
2936         buf = (char *)calloc(1, BUFMAX);
2937         if (!buf) {
2938                 DBGE("Malloc Failed\n");
2939                 return -1;
2940         }
2941
2942         buftemp = (char *)calloc(1, BUFMAX);
2943         if (!buftemp) {
2944                 DBGE("Malloc Failed\n");
2945                 free(buf);
2946                 return -1;
2947         }
2948
2949         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
2950
2951                 if(mfx->readonly && !strcasecmp(mfx->readonly, "True"))
2952                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RO_PATH, mfx->uiapplication->appid);
2953                 else
2954                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, mfx->uiapplication->appid);
2955
2956                 /* skip if desktop exists
2957                 if (access(filepath, R_OK) == 0)
2958                         continue;
2959                 */
2960
2961                 file = fopen(filepath, "w");
2962                 if(file == NULL)
2963                 {
2964                     DBGE("Can't open %s", filepath);
2965                     free(buf);
2966                     free(buftemp);
2967                     return -1;
2968                 }
2969
2970                 snprintf(buf, BUFMAX, "[Desktop Entry]\n");
2971                 fwrite(buf, 1, strlen(buf), file);
2972
2973                 for( ; mfx->uiapplication->label ; mfx->uiapplication->label = mfx->uiapplication->label->next) {
2974                         if(!strcmp(mfx->uiapplication->label->lang, DEFAULT_LOCALE)) {
2975                                 snprintf(buf, BUFMAX, "Name=%s\n",      mfx->uiapplication->label->text);
2976                         } else {
2977                                 locale = __convert_to_system_locale(mfx->uiapplication->label->lang);
2978                                 snprintf(buf, BUFMAX, "Name[%s]=%s\n", locale,
2979                                         mfx->uiapplication->label->text);
2980                                 free(locale);
2981                         }
2982                         fwrite(buf, 1, strlen(buf), file);
2983                 }
2984
2985                 if(mfx->uiapplication->label && mfx->uiapplication->label->text) {
2986                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->uiapplication->label->text);
2987                         fwrite(buf, 1, strlen(buf), file);
2988                 }
2989 /*
2990                 else if(mfx->label && mfx->label->text) {
2991                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->label->text);
2992                         fwrite(buf, 1, strlen(buf), file);
2993                 } else {
2994                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->package);
2995                         fwrite(buf, 1, strlen(buf), file);
2996                 }
2997 */
2998
2999
3000                 snprintf(buf, BUFMAX, "Type=Application\n");
3001                 fwrite(buf, 1, strlen(buf), file);
3002
3003                 if(mfx->uiapplication->exec) {
3004                         snprintf(buf, BUFMAX, "Exec=%s\n", mfx->uiapplication->exec);
3005                         fwrite(buf, 1, strlen(buf), file);
3006                 }
3007
3008                 if(mfx->uiapplication->icon && mfx->uiapplication->icon->text) {
3009                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->uiapplication->icon->text);
3010                         fwrite(buf, 1, strlen(buf), file);
3011                 } else if(mfx->icon && mfx->icon->text) {
3012                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->icon->text);
3013                         fwrite(buf, 1, strlen(buf), file);
3014                 }
3015
3016                 if(mfx->version) {
3017                         snprintf(buf, BUFMAX, "Version=%s\n", mfx->version);
3018                         fwrite(buf, 1, strlen(buf), file);
3019                 }
3020
3021                 if(mfx->uiapplication->nodisplay) {
3022                         snprintf(buf, BUFMAX, "NoDisplay=%s\n", mfx->uiapplication->nodisplay);
3023                         fwrite(buf, 1, strlen(buf), file);
3024                 }
3025
3026                 if(mfx->uiapplication->categories) {
3027                         snprintf(buf, BUFMAX, "Categories=%s\n", mfx->uiapplication->categories);
3028                         fwrite(buf, 1, strlen(buf), file);
3029                 }
3030
3031                 if(mfx->uiapplication->taskmanage && !strcasecmp(mfx->uiapplication->taskmanage, "False")) {
3032                         snprintf(buf, BUFMAX, "X-TIZEN-TaskManage=False\n");
3033                         fwrite(buf, 1, strlen(buf), file);
3034                 }
3035
3036                 if(mfx->uiapplication->hwacceleration) {
3037                         snprintf(buf, BUFMAX, "Hw-Acceleration=%s\n", mfx->uiapplication->hwacceleration);
3038                         fwrite(buf, 1, strlen(buf), file);
3039                 }
3040
3041                 if(mfx->uiapplication->multiple && !strcasecmp(mfx->uiapplication->multiple, "True")) {
3042                         snprintf(buf, BUFMAX, "X-TIZEN-Multiple=True\n");
3043                         fwrite(buf, 1, strlen(buf), file);
3044                 }
3045
3046                 if(mfx->uiapplication->extraid) {
3047                         snprintf(buf, BUFMAX, "X-TIZEN-PackageID=%s\n", mfx->uiapplication->extraid);
3048                         fwrite(buf, 1, strlen(buf), file);
3049                 }
3050
3051                 if(mfx->removable && !strcasecmp(mfx->removable, "False")) {
3052                         snprintf(buf, BUFMAX, "X-TIZEN-Removable=False\n");
3053                         fwrite(buf, 1, strlen(buf), file);
3054                 }
3055
3056                 if(mfx->type) {
3057                         snprintf(buf, BUFMAX, "X-TIZEN-PackageType=%s\n", mfx->type);
3058                         fwrite(buf, 1, strlen(buf), file);
3059                 }
3060
3061                 snprintf(buf, BUFMAX, "X-TIZEN-PkgID=%s\n", mfx->package);
3062                 fwrite(buf, 1, strlen(buf), file);
3063
3064
3065 //              snprintf(buf, BUFMAX, "X-TIZEN-PackageType=rpm\n");
3066 //              fwrite(buf, 1, strlen(buf), file);
3067
3068
3069                 if(mfx->uiapplication->appsvc) {
3070                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3071                         DBG("buf[%s]\n", buf);
3072
3073
3074                         uiapplication_x *up = mfx->uiapplication;
3075                         appsvc_x *asvc = NULL;
3076                         operation_x *op = NULL;
3077                         mime_x *mi = NULL;
3078                         uri_x *ui = NULL;
3079                         subapp_x *sub = NULL;
3080                         int ret = -1;
3081                         char query[PKG_STRING_LEN_MAX] = {'\0'};
3082                         char *operation = NULL;
3083                         char *mime = NULL;
3084                         char *uri = NULL;
3085                         char *subapp = NULL;
3086                         int i = 0;
3087
3088
3089                         asvc = up->appsvc;
3090                         while(asvc != NULL)
3091                         {
3092                                 op = asvc->operation;
3093                                 while(op != NULL)
3094                                 {
3095                                         if (op)
3096                                                 operation = op->name;
3097                                         mi = asvc->mime;
3098
3099                                         do
3100                                         {
3101                                                 if (mi)
3102                                                         mime = mi->name;
3103                                                 sub = asvc->subapp;
3104                                                 do
3105                                                 {
3106                                                         if (sub)
3107                                                                 subapp = sub->name;
3108                                                         ui = asvc->uri;
3109                                                         do
3110                                                         {
3111                                                                 if (ui)
3112                                                                         uri = ui->name;
3113
3114                                                                 if(i++ > 0) {
3115                                                                         strncpy(buftemp, buf, BUFMAX);
3116                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3117                                                                 }
3118
3119
3120                                                                 strncpy(buftemp, buf, BUFMAX);
3121                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3122                                                                 DBG("buf[%s]\n", buf);
3123
3124                                                                 if (ui)
3125                                                                         ui = ui->next;
3126                                                                 uri = NULL;
3127                                                         } while(ui != NULL);
3128                                                 if (sub)
3129                                                                 sub = sub->next;
3130                                                         subapp = NULL;
3131                                                 }while(sub != NULL);
3132                                                 if (mi)
3133                                                         mi = mi->next;
3134                                                 mime = NULL;
3135                                         }while(mi != NULL);
3136                                         if (op)
3137                                                 op = op->next;
3138                                         operation = NULL;
3139                                 }
3140                                 asvc = asvc->next;
3141                         }
3142
3143
3144                         fwrite(buf, 1, strlen(buf), file);
3145
3146 //                      strncpy(buftemp, buf, BUFMAX);
3147 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
3148 //                      fwrite(buf, 1, strlen(buf), file);
3149                 }
3150
3151                 if(mfx->uiapplication->appcontrol) {
3152                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3153                         DBG("buf[%s]\n", buf);
3154
3155                         uiapplication_x *up = mfx->uiapplication;
3156                         appcontrol_x *acontrol = NULL;
3157                         operation_x *op = NULL;
3158                         mime_x *mi = NULL;
3159                         uri_x *ui = NULL;
3160                         subapp_x *sub = NULL;
3161                         int ret = -1;
3162                         char query[PKG_STRING_LEN_MAX] = {'\0'};
3163                         char *operation = NULL;
3164                         char *mime = NULL;
3165                         char *uri = NULL;
3166                         char *subapp = NULL;
3167                         int i = 0;
3168
3169                         acontrol = up->appcontrol;
3170                         while(acontrol != NULL)
3171                         {
3172                                 op = acontrol->operation;
3173                                 while(op != NULL)
3174                                 {
3175                                         if (op)
3176                                                 operation = op->name;
3177                                         mi = acontrol->mime;
3178
3179                                         do
3180                                         {
3181                                                 if (mi)
3182                                                         mime = mi->name;
3183                                                 sub = acontrol->subapp;
3184                                                 do
3185                                                 {
3186                                                         if (sub)
3187                                                                 subapp = sub->name;
3188                                                         ui = acontrol->uri;
3189                                                         do
3190                                                         {
3191                                                                 if (ui)
3192                                                                         uri = ui->name;
3193
3194                                                                 if(i++ > 0) {
3195                                                                         strncpy(buftemp, buf, BUFMAX);
3196                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3197                                                                 }
3198
3199                                                                 strncpy(buftemp, buf, BUFMAX);
3200                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3201                                                                 DBG("buf[%s]\n", buf);
3202
3203                                                                 if (ui)
3204                                                                         ui = ui->next;
3205                                                                 uri = NULL;
3206                                                         } while(ui != NULL);
3207                                                 if (sub)
3208                                                                 sub = sub->next;
3209                                                         subapp = NULL;
3210                                                 }while(sub != NULL);
3211                                                 if (mi)
3212                                                         mi = mi->next;
3213                                                 mime = NULL;
3214                                         }while(mi != NULL);
3215                                         if (op)
3216                                                 op = op->next;
3217                                         operation = NULL;
3218                                 }
3219                                 acontrol = acontrol->next;
3220                         }
3221
3222
3223                         fwrite(buf, 1, strlen(buf), file);
3224
3225 //                      strncpy(buftemp, buf, BUFMAX);
3226 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
3227 //                      fwrite(buf, 1, strlen(buf), file);
3228                 }
3229
3230                 fflush(file);
3231                 fd = fileno(file);
3232                 fsync(fd);
3233                 fclose(file);
3234         }
3235
3236         free(buf);
3237         free(buftemp);
3238
3239         return 0;
3240 }
3241
3242 static int __ps_remove_nativeapp_desktop(manifest_x *mfx)
3243 {
3244     char filepath[PKG_STRING_LEN_MAX] = "";
3245         int ret = 0;
3246
3247         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
3248                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, mfx->uiapplication->appid);
3249
3250                 ret = remove(filepath);
3251                 if (ret <0)
3252                         return -1;
3253         }
3254
3255         return 0;
3256 }
3257
3258 #define MANIFEST_RO_PREFIX "/usr/share/packages/"
3259 #define PRELOAD_PACKAGE_LIST "/usr/etc/package-manager/preload/preload_list.txt"
3260 static int __add_preload_info(manifest_x * mfx, const char *manifest)
3261 {
3262         FILE *fp = NULL;
3263         char buffer[1024] = { 0 };
3264         int state = 0;
3265
3266         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
3267                 free(mfx->readonly);
3268                 mfx->readonly = strdup("True");
3269
3270                 free(mfx->preload);
3271                 mfx->preload = strdup("True");
3272
3273                 free(mfx->removable);
3274                 mfx->removable = strdup("False");
3275
3276                 return 0;
3277         }
3278
3279         fp = fopen(PRELOAD_PACKAGE_LIST, "r");
3280         if (fp == NULL) {
3281                 DBGE("no preload list\n");
3282                 return -1;
3283         }
3284
3285         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
3286                 if (buffer[0] == '#') {
3287                         if(strcasestr(buffer, "RW_NORM"))
3288                                 state = 2;
3289                         else if(strcasestr(buffer, "RW_RM"))
3290                                 state = 3;
3291                         else
3292                                 continue;
3293                 }
3294
3295                 __str_trim(buffer);
3296
3297                 if(!strcmp(mfx->package, buffer)) {
3298                         free(mfx->preload);
3299                         mfx->preload = strdup("True");
3300                         if(state == 2){
3301                                 free(mfx->readonly);
3302                                 mfx->readonly = strdup("False");
3303                                 free(mfx->removable);
3304                                 mfx->removable = strdup("False");
3305                         } else if(state == 3){
3306                                 free(mfx->readonly);
3307                                 mfx->readonly = strdup("False");
3308                                 free(mfx->removable);
3309                                 mfx->removable = strdup("True");
3310                         }
3311                 }
3312
3313                 memset(buffer, 0x00, sizeof(buffer));
3314         }
3315
3316         if (fp != NULL)
3317                 fclose(fp);
3318
3319         return 0;
3320 }
3321
3322
3323 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
3324 {
3325         if (mfx == NULL)
3326                 return;
3327         if (mfx->ns) {
3328                 free((void *)mfx->ns);
3329                 mfx->ns = NULL;
3330         }
3331         if (mfx->package) {
3332                 free((void *)mfx->package);
3333                 mfx->package = NULL;
3334         }
3335         if (mfx->version) {
3336                 free((void *)mfx->version);
3337                 mfx->version = NULL;
3338         }
3339         if (mfx->installlocation) {
3340                 free((void *)mfx->installlocation);
3341                 mfx->installlocation = NULL;
3342         }
3343         if (mfx->preload) {
3344                 free((void *)mfx->preload);
3345                 mfx->preload = NULL;
3346         }
3347         if (mfx->readonly) {
3348                 free((void *)mfx->readonly);
3349                 mfx->readonly = NULL;
3350         }
3351         if (mfx->removable) {
3352                 free((void *)mfx->removable);
3353                 mfx->removable = NULL;
3354         }
3355         if (mfx->type) {
3356                 free((void *)mfx->type);
3357                 mfx->type = NULL;
3358         }
3359         if (mfx->installed_time) {
3360                 free((void *)mfx->installed_time);
3361                 mfx->installed_time = NULL;
3362         }
3363         if (mfx->storeclient_id) {
3364                 free((void *)mfx->storeclient_id);
3365                 mfx->storeclient_id = NULL;
3366         }
3367         if (mfx->mainapp_id) {
3368                 free((void *)mfx->mainapp_id);
3369                 mfx->mainapp_id = NULL;
3370         }
3371         if (mfx->package_url) {
3372                 free((void *)mfx->package_url);
3373                 mfx->package_url = NULL;
3374         }
3375
3376         /*Free Icon*/
3377         if (mfx->icon) {
3378                 icon_x *icon = mfx->icon;
3379                 icon_x *tmp = NULL;
3380                 while(icon != NULL)
3381                 {
3382                         tmp = icon->next;
3383                         __ps_free_icon(icon);
3384                         icon = tmp;
3385                 }
3386         }
3387         /*Free Label*/
3388         if (mfx->label) {
3389                 label_x *label = mfx->label;
3390                 label_x *tmp = NULL;
3391                 while(label != NULL)
3392                 {
3393                         tmp = label->next;
3394                         __ps_free_label(label);
3395                         label = tmp;
3396                 }
3397         }
3398         /*Free Author*/
3399         if (mfx->author) {
3400                 author_x *author = mfx->author;
3401                 author_x *tmp = NULL;
3402                 while(author != NULL)
3403                 {
3404                         tmp = author->next;
3405                         __ps_free_author(author);
3406                         author = tmp;
3407                 }
3408         }
3409         /*Free Description*/
3410         if (mfx->description) {
3411                 description_x *description = mfx->description;
3412                 description_x *tmp = NULL;
3413                 while(description != NULL)
3414                 {
3415                         tmp = description->next;
3416                         __ps_free_description(description);
3417                         description = tmp;
3418                 }
3419         }
3420         /*Free License*/
3421         if (mfx->license) {
3422                 license_x *license = mfx->license;
3423                 license_x *tmp = NULL;
3424                 while(license != NULL)
3425                 {
3426                         tmp = license->next;
3427                         __ps_free_license(license);
3428                         license = tmp;
3429                 }
3430         }
3431         /*Free UiApplication*/
3432         if (mfx->uiapplication) {
3433                 uiapplication_x *uiapplication = mfx->uiapplication;
3434                 uiapplication_x *tmp = NULL;
3435                 while(uiapplication != NULL)
3436                 {
3437                         tmp = uiapplication->next;
3438                         __ps_free_uiapplication(uiapplication);
3439                         uiapplication = tmp;
3440                 }
3441         }
3442         /*Free ServiceApplication*/
3443         if (mfx->serviceapplication) {
3444                 serviceapplication_x *serviceapplication = mfx->serviceapplication;
3445                 serviceapplication_x *tmp = NULL;
3446                 while(serviceapplication != NULL)
3447                 {
3448                         tmp = serviceapplication->next;
3449                         __ps_free_serviceapplication(serviceapplication);
3450                         serviceapplication = tmp;
3451                 }
3452         }
3453         /*Free Daemon*/
3454         if (mfx->daemon) {
3455                 daemon_x *daemon = mfx->daemon;
3456                 daemon_x *tmp = NULL;
3457                 while(daemon != NULL)
3458                 {
3459                         tmp = daemon->next;
3460                         __ps_free_daemon(daemon);
3461                         daemon = tmp;
3462                 }
3463         }
3464         /*Free Theme*/
3465         if (mfx->theme) {
3466                 theme_x *theme = mfx->theme;
3467                 theme_x *tmp = NULL;
3468                 while(theme != NULL)
3469                 {
3470                         tmp = theme->next;
3471                         __ps_free_theme(theme);
3472                         theme = tmp;
3473                 }
3474         }
3475         /*Free Font*/
3476         if (mfx->font) {
3477                 font_x *font = mfx->font;
3478                 font_x *tmp = NULL;
3479                 while(font != NULL)
3480                 {
3481                         tmp = font->next;
3482                         __ps_free_font(font);
3483                         font = tmp;
3484                 }
3485         }
3486         /*Free Ime*/
3487         if (mfx->ime) {
3488                 ime_x *ime = mfx->ime;
3489                 ime_x *tmp = NULL;
3490                 while(ime != NULL)
3491                 {
3492                         tmp = ime->next;
3493                         __ps_free_ime(ime);
3494                         ime = tmp;
3495                 }
3496         }
3497         /*Free Compatibility*/
3498         if (mfx->compatibility) {
3499                 compatibility_x *compatibility = mfx->compatibility;
3500                 compatibility_x *tmp = NULL;
3501                 while(compatibility != NULL)
3502                 {
3503                         tmp = compatibility->next;
3504                         __ps_free_compatibility(compatibility);
3505                         compatibility = tmp;
3506                 }
3507         }
3508         /*Free DeviceProfile*/
3509         if (mfx->deviceprofile) {
3510                 deviceprofile_x *deviceprofile = mfx->deviceprofile;
3511                 deviceprofile_x *tmp = NULL;
3512                 while(deviceprofile != NULL)
3513                 {
3514                         tmp = deviceprofile->next;
3515                         __ps_free_deviceprofile(deviceprofile);
3516                         deviceprofile = tmp;
3517                 }
3518         }
3519         free((void*)mfx);
3520         mfx = NULL;
3521         return;
3522 }
3523
3524 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
3525 {
3526         DBG("parsing start\n");
3527         xmlTextReaderPtr reader;
3528         manifest_x *mfx = NULL;
3529
3530         reader = xmlReaderForFile(manifest, NULL, 0);
3531         if (reader) {
3532                 mfx = malloc(sizeof(manifest_x));
3533                 if (mfx) {
3534                         memset(mfx, '\0', sizeof(manifest_x));
3535                         if (__process_manifest(reader, mfx) < 0) {
3536                                 DBG("Parsing Failed\n");
3537                                 pkgmgr_parser_free_manifest_xml(mfx);
3538                                 mfx = NULL;
3539                         } else
3540                                 DBG("Parsing Success\n");
3541                 } else {
3542                         DBG("Memory allocation error\n");
3543                 }
3544                 xmlFreeTextReader(reader);
3545         } else {
3546                 DBG("Unable to create xml reader\n");
3547         }
3548         return mfx;
3549 }
3550
3551 /* These APIs are intended to call parser directly */
3552
3553 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
3554 {
3555         char *temp[] = {"shortcut-list", "livebox", "Accounts", "account", NULL};
3556         if (manifest == NULL) {
3557                 DBG("argument supplied is NULL\n");
3558                 return PMINFO_R_EINVAL;
3559         }
3560         DBG("parsing manifest for installation: %s\n", manifest);
3561         manifest_x *mfx = NULL;
3562         int ret = -1;
3563         xmlInitParser();
3564         mfx = pkgmgr_parser_process_manifest_xml(manifest);
3565         DBG("Parsing Finished\n");
3566         if (mfx == NULL)
3567                 return PMINFO_R_ERROR;
3568         
3569         __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
3570         __add_preload_info(mfx, manifest);
3571         DBG("Added preload infomation\n");
3572         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
3573         if (ret == -1)
3574                 DBG("DB Insert failed\n");
3575         else
3576                 DBG("DB Insert Success\n");
3577
3578         ret = __ps_make_nativeapp_desktop(mfx);
3579         if (ret == -1)
3580                 DBG("Creating desktop file failed\n");
3581         else
3582                 DBG("Creating desktop file Success\n");
3583
3584         pkgmgr_parser_free_manifest_xml(mfx);
3585         DBG("Free Done\n");
3586         xmlCleanupParser();
3587
3588         return PMINFO_R_OK;
3589 }
3590
3591 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
3592 {
3593         char *temp[] = {"shortcut-list", "livebox", "Accounts", "account", NULL};
3594         if (manifest == NULL) {
3595                 DBG("argument supplied is NULL\n");
3596                 return PMINFO_R_EINVAL;
3597         }
3598         DBG("parsing manifest for upgradation: %s\n", manifest);
3599         manifest_x *mfx = NULL;
3600         int ret = -1;
3601         xmlInitParser();
3602         mfx = pkgmgr_parser_process_manifest_xml(manifest);
3603         DBG("Parsing Finished\n");
3604         if (mfx == NULL)
3605                 return PMINFO_R_ERROR;
3606         
3607         __streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
3608         __add_preload_info(mfx, manifest);
3609         DBG("Added preload infomation\n");
3610         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
3611         if (ret == -1)
3612                 DBG("DB Update failed\n");
3613         else
3614                 DBG("DB Update Success\n");
3615
3616         ret = __ps_make_nativeapp_desktop(mfx);
3617         if (ret == -1)
3618                 DBG("Creating desktop file failed\n");
3619         else
3620                 DBG("Creating desktop file Success\n");
3621
3622         pkgmgr_parser_free_manifest_xml(mfx);
3623         DBG("Free Done\n");
3624         xmlCleanupParser();
3625
3626         return PMINFO_R_OK;
3627 }
3628
3629 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
3630 {
3631         char *temp[] = {"shortcut-list", "livebox", "Accounts", "account", NULL};
3632         if (manifest == NULL) {
3633                 DBG("argument supplied is NULL\n");
3634                 return PMINFO_R_EINVAL;
3635         }
3636         DBG("parsing manifest for uninstallation: %s\n", manifest);
3637         manifest_x *mfx = NULL;
3638         int ret = -1;
3639         xmlInitParser();
3640         mfx = pkgmgr_parser_process_manifest_xml(manifest);
3641         DBG("Parsing Finished\n");
3642         if (mfx == NULL)
3643                 return PMINFO_R_ERROR;
3644         
3645         __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
3646         __add_preload_info(mfx, manifest);
3647         DBG("Added preload infomation\n");
3648
3649         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
3650         if (ret == -1)
3651                 DBG("DB Delete failed\n");
3652         else
3653                 DBG("DB Delete Success\n");
3654
3655         ret = __ps_remove_nativeapp_desktop(mfx);
3656         if (ret == -1)
3657                 DBG("Removing desktop file failed\n");
3658         else
3659                 DBG("Removing desktop file Success\n");
3660
3661         pkgmgr_parser_free_manifest_xml(mfx);
3662         DBG("Free Done\n");
3663         xmlCleanupParser();
3664
3665         return PMINFO_R_OK;
3666 }
3667
3668 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
3669 {
3670         return __pkgid_to_manifest(pkgid);
3671 }
3672
3673 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
3674 {
3675         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
3676 }
3677
3678 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
3679 {
3680         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
3681 }
3682
3683 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
3684 {
3685         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
3686 }
3687
3688 #define SCHEMA_FILE "/usr/etc/package-manager/preload/manifest.xsd"
3689 #if 1
3690 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
3691 {
3692         if (manifest == NULL) {
3693                 DBGE("manifest file is NULL\n");
3694                 return PMINFO_R_EINVAL;
3695         }
3696         int ret = -1;
3697         xmlSchemaParserCtxtPtr ctx;
3698         xmlSchemaValidCtxtPtr vctx;
3699         xmlSchemaPtr xschema;
3700         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
3701         if (ctx == NULL) {
3702                 DBGE("xmlSchemaNewParserCtxt() Failed\n");
3703                 return PMINFO_R_ERROR;
3704         }
3705         xschema = xmlSchemaParse(ctx);
3706         if (xschema == NULL) {
3707                 DBGE("xmlSchemaParse() Failed\n");
3708                 return PMINFO_R_ERROR;
3709         }
3710         vctx = xmlSchemaNewValidCtxt(xschema);
3711         if (vctx == NULL) {
3712                 DBGE("xmlSchemaNewValidCtxt() Failed\n");
3713                 return PMINFO_R_ERROR;
3714         }
3715         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
3716         ret = xmlSchemaValidateFile(vctx, manifest, 0);
3717         if (ret == -1) {
3718                 DBGE("xmlSchemaValidateFile() failed\n");
3719                 return PMINFO_R_ERROR;
3720         } else if (ret == 0) {
3721                 DBGE("Manifest is Valid\n");
3722                 return PMINFO_R_OK;
3723         } else {
3724                 DBGE("Manifest Validation Failed with error code %d\n", ret);
3725                 return PMINFO_R_ERROR;
3726         }
3727         return PMINFO_R_OK;
3728 }
3729
3730 #else
3731 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
3732 {
3733         int err = 0;
3734         int status = 0;
3735         pid_t pid;
3736
3737         pid = fork();
3738
3739         switch (pid) {
3740         case -1:
3741                 DBGE("fork failed\n");
3742                 return -1;
3743         case 0:
3744                 /* child */
3745                 {
3746                         int dev_null_fd = open ("/dev/null", O_RDWR);
3747                         if (dev_null_fd >= 0)
3748                         {
3749                                 dup2 (dev_null_fd, 0);/*stdin*/
3750                                 dup2 (dev_null_fd, 1);/*stdout*/
3751                                 dup2 (dev_null_fd, 2);/*stderr*/
3752                         }
3753
3754                         if (execl("/usr/bin/xmllint", "xmllint", manifest, "--schema",
3755                                 SCHEMA_FILE, NULL) < 0) {
3756                                 DBGE("execl error\n");
3757                         }
3758
3759                         _exit(100);
3760                 }
3761         default:
3762                 /* parent */
3763                 break;
3764         }
3765
3766         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
3767                 if (err < 0) {
3768                         if (errno == EINTR)
3769                                 continue;
3770                         DBGE("waitpid failed\n");
3771                         return -1;
3772                 }
3773         }
3774
3775
3776         if(WIFEXITED(status) && !WEXITSTATUS(status))
3777                 return 0;
3778         else
3779                 return -1;
3780 }
3781 #endif