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