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