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