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