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