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