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