1ee5242a5cfd903e0148136c494f992ee25a7356
[framework/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         free((void*)uiapplication);
1361         uiapplication = NULL;
1362 }
1363
1364 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication)
1365 {
1366         if (serviceapplication == NULL)
1367                 return;
1368         if (serviceapplication->exec) {
1369                 free((void *)serviceapplication->exec);
1370                 serviceapplication->exec = NULL;
1371         }
1372         if (serviceapplication->appid) {
1373                 free((void *)serviceapplication->appid);
1374                 serviceapplication->appid = NULL;
1375         }
1376         if (serviceapplication->onboot) {
1377                 free((void *)serviceapplication->onboot);
1378                 serviceapplication->onboot = NULL;
1379         }
1380         if (serviceapplication->autorestart) {
1381                 free((void *)serviceapplication->autorestart);
1382                 serviceapplication->autorestart = NULL;
1383         }
1384         if (serviceapplication->type) {
1385                 free((void *)serviceapplication->type);
1386                 serviceapplication->type = NULL;
1387         }
1388         if (serviceapplication->enabled) {
1389                 free((void *)serviceapplication->enabled);
1390                 serviceapplication->enabled = NULL;
1391         }
1392         if (serviceapplication->package) {
1393                 free((void *)serviceapplication->package);
1394                 serviceapplication->package = NULL;
1395         }
1396         /*Free Label*/
1397         if (serviceapplication->label) {
1398                 label_x *label = serviceapplication->label;
1399                 label_x *tmp = NULL;
1400                 while(label != NULL) {
1401                         tmp = label->next;
1402                         __ps_free_label(label);
1403                         label = tmp;
1404                 }
1405         }
1406         /*Free Icon*/
1407         if (serviceapplication->icon) {
1408                 icon_x *icon = serviceapplication->icon;
1409                 icon_x *tmp = NULL;
1410                 while(icon != NULL) {
1411                         tmp = icon->next;
1412                         __ps_free_icon(icon);
1413                         icon = tmp;
1414                 }
1415         }
1416         /*Free AppControl*/
1417         if (serviceapplication->appcontrol) {
1418                 appcontrol_x *appcontrol = serviceapplication->appcontrol;
1419                 appcontrol_x *tmp = NULL;
1420                 while(appcontrol != NULL) {
1421                         tmp = appcontrol->next;
1422                         __ps_free_appcontrol(appcontrol);
1423                         appcontrol = tmp;
1424                 }
1425         }
1426         /*Free DataControl*/
1427         if (serviceapplication->datacontrol) {
1428                 datacontrol_x *datacontrol = serviceapplication->datacontrol;
1429                 datacontrol_x *tmp = NULL;
1430                 while(datacontrol != NULL) {
1431                         tmp = datacontrol->next;
1432                         __ps_free_datacontrol(datacontrol);
1433                         datacontrol = tmp;
1434                 }
1435         }
1436         /*Free LaunchConditions*/
1437         if (serviceapplication->launchconditions) {
1438                 launchconditions_x *launchconditions = serviceapplication->launchconditions;
1439                 launchconditions_x *tmp = NULL;
1440                 while(launchconditions != NULL) {
1441                         tmp = launchconditions->next;
1442                         __ps_free_launchconditions(launchconditions);
1443                         launchconditions = tmp;
1444                 }
1445         }
1446         /*Free Notification*/
1447         if (serviceapplication->notification) {
1448                 notification_x *notification = serviceapplication->notification;
1449                 notification_x *tmp = NULL;
1450                 while(notification != NULL) {
1451                         tmp = notification->next;
1452                         __ps_free_notification(notification);
1453                         notification = tmp;
1454                 }
1455         }
1456         /*Free DataShare*/
1457         if (serviceapplication->datashare) {
1458                 datashare_x *datashare = serviceapplication->datashare;
1459                 datashare_x *tmp = NULL;
1460                 while(datashare != NULL) {
1461                         tmp = datashare->next;
1462                         __ps_free_datashare(datashare);
1463                         datashare = tmp;
1464                 }
1465         }
1466         /*Free AppSvc*/
1467         if (serviceapplication->appsvc) {
1468                 appsvc_x *appsvc = serviceapplication->appsvc;
1469                 appsvc_x *tmp = NULL;
1470                 while(appsvc != NULL) {
1471                         tmp = appsvc->next;
1472                         __ps_free_appsvc(appsvc);
1473                         appsvc = tmp;
1474                 }
1475         }
1476         /*Free Category*/
1477         if (serviceapplication->category) {
1478                 category_x *category = serviceapplication->category;
1479                 category_x *tmp = NULL;
1480                 while(category != NULL) {
1481                         tmp = category->next;
1482                         __ps_free_category(category);
1483                         category = tmp;
1484                 }
1485         }
1486         /*Free Metadata*/
1487         if (serviceapplication->metadata) {
1488                 metadata_x *metadata = serviceapplication->metadata;
1489                 metadata_x *tmp = NULL;
1490                 while(metadata != NULL) {
1491                         tmp = metadata->next;
1492                         __ps_free_metadata(metadata);
1493                         metadata = tmp;
1494                 }
1495         }
1496         /*Free permission*/
1497         if (serviceapplication->permission) {
1498                 permission_x *permission = serviceapplication->permission;
1499                 permission_x *tmp = NULL;
1500                 while(permission != NULL) {
1501                         tmp = permission->next;
1502                         __ps_free_permission(permission);
1503                         permission = tmp;
1504                 }
1505         }
1506         free((void*)serviceapplication);
1507         serviceapplication = NULL;
1508 }
1509
1510 static void __ps_free_font(font_x *font)
1511 {
1512         if (font == NULL)
1513                 return;
1514         if (font->name) {
1515                 free((void *)font->name);
1516                 font->name = NULL;
1517         }
1518         if (font->text) {
1519                 free((void *)font->text);
1520                 font->text = NULL;
1521         }
1522         free((void*)font);
1523         font = NULL;
1524 }
1525
1526 static void __ps_free_theme(theme_x *theme)
1527 {
1528         if (theme == NULL)
1529                 return;
1530         if (theme->name) {
1531                 free((void *)theme->name);
1532                 theme->name = NULL;
1533         }
1534         if (theme->text) {
1535                 free((void *)theme->text);
1536                 theme->text = NULL;
1537         }
1538         free((void*)theme);
1539         theme = NULL;
1540 }
1541
1542 static void __ps_free_daemon(daemon_x *daemon)
1543 {
1544         if (daemon == NULL)
1545                 return;
1546         if (daemon->name) {
1547                 free((void *)daemon->name);
1548                 daemon->name = NULL;
1549         }
1550         if (daemon->text) {
1551                 free((void *)daemon->text);
1552                 daemon->text = NULL;
1553         }
1554         free((void*)daemon);
1555         daemon = NULL;
1556 }
1557
1558 static void __ps_free_ime(ime_x *ime)
1559 {
1560         if (ime == NULL)
1561                 return;
1562         if (ime->name) {
1563                 free((void *)ime->name);
1564                 ime->name = NULL;
1565         }
1566         if (ime->text) {
1567                 free((void *)ime->text);
1568                 ime->text = NULL;
1569         }
1570         free((void*)ime);
1571         ime = NULL;
1572 }
1573
1574
1575 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
1576 {
1577         xmlTextReaderRead(reader);
1578         if (xmlTextReaderValue(reader))
1579                 allowed->text = ASCII(xmlTextReaderValue(reader));
1580         return 0;
1581 }
1582
1583 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
1584 {
1585         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1586                 operation->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1587 /* Text does not exist. Only attribute exists
1588         xmlTextReaderRead(reader);
1589         if (xmlTextReaderValue(reader))
1590                 operation->text = ASCII(xmlTextReaderValue(reader));
1591 */
1592         return 0;
1593 }
1594
1595 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
1596 {
1597         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1598                 uri->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1599 /* Text does not exist. Only attribute exists
1600         xmlTextReaderRead(reader);
1601         if (xmlTextReaderValue(reader))
1602                 uri->text = ASCII(xmlTextReaderValue(reader));
1603 */
1604         return 0;
1605 }
1606
1607 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
1608 {
1609         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1610                 mime->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1611 /* Text does not exist. Only attribute exists
1612         xmlTextReaderRead(reader);
1613         if (xmlTextReaderValue(reader))
1614                 mime->text = ASCII(xmlTextReaderValue(reader));
1615 */
1616         return 0;
1617 }
1618
1619 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
1620 {
1621         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1622                 subapp->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1623 /* Text does not exist. Only attribute exists
1624         xmlTextReaderRead(reader);
1625         if (xmlTextReaderValue(reader))
1626                 mime->text = ASCII(xmlTextReaderValue(reader));
1627 */
1628         return 0;
1629 }
1630
1631 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
1632 {
1633         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1634                 condition->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1635         xmlTextReaderRead(reader);
1636         if (xmlTextReaderValue(reader))
1637                 condition->text = ASCII(xmlTextReaderValue(reader));
1638         return 0;
1639 }
1640
1641 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
1642 {
1643         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1644                 notification->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1645         xmlTextReaderRead(reader);
1646         if (xmlTextReaderValue(reader))
1647                 notification->text = ASCII(xmlTextReaderValue(reader));
1648         return 0;
1649 }
1650
1651 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
1652 {
1653         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1654                 category->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1655         return 0;
1656 }
1657
1658 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
1659 {
1660         xmlTextReaderRead(reader);
1661         if (xmlTextReaderValue(reader)) {
1662                 privilege->text = ASCII(xmlTextReaderValue(reader));
1663         }
1664         return 0;
1665 }
1666
1667 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
1668 {
1669         if (xmlTextReaderGetAttribute(reader, XMLCHAR("key")))
1670                 metadata->key = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("key")));
1671         if (xmlTextReaderGetAttribute(reader, XMLCHAR("value")))
1672                 metadata->value = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("value")));
1673         return 0;
1674 }
1675
1676 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
1677 {
1678         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
1679                 permission->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
1680
1681         xmlTextReaderRead(reader);
1682         if (xmlTextReaderValue(reader))
1683                 permission->value = ASCII(xmlTextReaderValue(reader));
1684         return 0;
1685 }
1686
1687 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
1688 {
1689         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1690                 compatibility->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1691         xmlTextReaderRead(reader);
1692         if (xmlTextReaderValue(reader))
1693                 compatibility->text = ASCII(xmlTextReaderValue(reader));
1694         return 0;
1695 }
1696
1697 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution)
1698 {
1699         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")))
1700                 resolution->mimetype = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")));
1701         if (xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")))
1702                 resolution->urischeme = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")));
1703         return 0;
1704 }
1705
1706 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
1707 {
1708         xmlTextReaderRead(reader);
1709         if (xmlTextReaderValue(reader))
1710                 request->text = ASCII(xmlTextReaderValue(reader));
1711         return 0;
1712 }
1713
1714 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
1715 {
1716         const xmlChar *node;
1717         int ret = -1;
1718         int depth = -1;
1719         allowed_x *tmp1 = NULL;
1720         request_x *tmp2 = NULL;
1721
1722         if (xmlTextReaderGetAttribute(reader, XMLCHAR("path")))
1723                 define->path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("path")));
1724
1725         depth = xmlTextReaderDepth(reader);
1726         while ((ret = __next_child_element(reader, depth))) {
1727                 node = xmlTextReaderConstName(reader);
1728                 if (!node) {
1729                         DBG("xmlTextReaderConstName value is NULL\n");
1730                         return -1;
1731                 }
1732
1733                 if (!strcmp(ASCII(node), "allowed")) {
1734                         allowed_x *allowed= malloc(sizeof(allowed_x));
1735                         if (allowed == NULL) {
1736                                 DBG("Malloc Failed\n");
1737                                 return -1;
1738                         }
1739                         memset(allowed, '\0', sizeof(allowed_x));
1740                         LISTADD(define->allowed, allowed);
1741                         ret = __ps_process_allowed(reader, allowed);
1742                 } else if (!strcmp(ASCII(node), "request")) {
1743                         request_x *request = malloc(sizeof(request_x));
1744                         if (request == NULL) {
1745                                 DBG("Malloc Failed\n");
1746                                 return -1;
1747                         }
1748                         memset(request, '\0', sizeof(request_x));
1749                         LISTADD(define->request, request);
1750                         ret = __ps_process_request(reader, request);
1751                 } else
1752                         return -1;
1753                 if (ret < 0) {
1754                         DBG("Processing define failed\n");
1755                         return ret;
1756                 }
1757         }
1758         if (define->allowed) {
1759                 LISTHEAD(define->allowed, tmp1);
1760                 define->allowed = tmp1;
1761         }
1762         if (define->request) {
1763                 LISTHEAD(define->request, tmp2);
1764                 define->request = tmp2;
1765         }
1766         return ret;
1767 }
1768
1769 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol)
1770 {
1771         const xmlChar *node;
1772         int ret = -1;
1773         int depth = -1;
1774         operation_x *tmp1 = NULL;
1775         uri_x *tmp2 = NULL;
1776         mime_x *tmp3 = NULL;
1777         subapp_x *tmp4 = NULL;
1778
1779         depth = xmlTextReaderDepth(reader);
1780         while ((ret = __next_child_element(reader, depth))) {
1781                 node = xmlTextReaderConstName(reader);
1782                 if (!node) {
1783                         DBG("xmlTextReaderConstName value is NULL\n");
1784                         return -1;
1785                 }
1786
1787                 if (!strcmp(ASCII(node), "operation")) {
1788                         operation_x *operation = malloc(sizeof(operation_x));
1789                         if (operation == NULL) {
1790                                 DBG("Malloc Failed\n");
1791                                 return -1;
1792                         }
1793                         memset(operation, '\0', sizeof(operation_x));
1794                         LISTADD(appcontrol->operation, operation);
1795                         ret = __ps_process_operation(reader, operation);
1796                         DBG("operation processing\n");
1797                 } else if (!strcmp(ASCII(node), "uri")) {
1798                         uri_x *uri= malloc(sizeof(uri_x));
1799                         if (uri == NULL) {
1800                                 DBG("Malloc Failed\n");
1801                                 return -1;
1802                         }
1803                         memset(uri, '\0', sizeof(uri_x));
1804                         LISTADD(appcontrol->uri, uri);
1805                         ret = __ps_process_uri(reader, uri);
1806                         DBG("uri processing\n");
1807                 } else if (!strcmp(ASCII(node), "mime")) {
1808                         mime_x *mime = malloc(sizeof(mime_x));
1809                         if (mime == NULL) {
1810                                 DBG("Malloc Failed\n");
1811                                 return -1;
1812                         }
1813                         memset(mime, '\0', sizeof(mime_x));
1814                         LISTADD(appcontrol->mime, mime);
1815                         ret = __ps_process_mime(reader, mime);
1816                         DBG("mime processing\n");
1817                 } else if (!strcmp(ASCII(node), "subapp")) {
1818                         subapp_x *subapp = malloc(sizeof(subapp_x));
1819                         if (subapp == NULL) {
1820                                 DBG("Malloc Failed\n");
1821                                 return -1;
1822                         }
1823                         memset(subapp, '\0', sizeof(subapp_x));
1824                         LISTADD(appcontrol->subapp, subapp);
1825                         ret = __ps_process_subapp(reader, subapp);
1826                         DBG("subapp processing\n");
1827                 } else
1828                         return -1;
1829                 if (ret < 0) {
1830                         DBG("Processing appcontrol failed\n");
1831                         return ret;
1832                 }
1833         }
1834         if (appcontrol->operation) {
1835                 LISTHEAD(appcontrol->operation, tmp1);
1836                 appcontrol->operation = tmp1;
1837         }
1838         if (appcontrol->uri) {
1839                 LISTHEAD(appcontrol->uri, tmp2);
1840                 appcontrol->uri = tmp2;
1841         }
1842         if (appcontrol->mime) {
1843                 LISTHEAD(appcontrol->mime, tmp3);
1844                 appcontrol->mime = tmp3;
1845         }
1846         if (appcontrol->subapp) {
1847                 LISTHEAD(appcontrol->subapp, tmp4);
1848                 appcontrol->subapp = tmp4;
1849         }
1850
1851         xmlTextReaderRead(reader);
1852         if (xmlTextReaderValue(reader))
1853                 appcontrol->text = ASCII(xmlTextReaderValue(reader));
1854
1855         return ret;
1856 }
1857
1858 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
1859 {
1860         const xmlChar *node;
1861         int ret = -1;
1862         int depth = -1;
1863         operation_x *tmp1 = NULL;
1864         uri_x *tmp2 = NULL;
1865         mime_x *tmp3 = NULL;
1866         subapp_x *tmp4 = NULL;
1867
1868         depth = xmlTextReaderDepth(reader);
1869         while ((ret = __next_child_element(reader, depth))) {
1870                 node = xmlTextReaderConstName(reader);
1871                 if (!node) {
1872                         DBG("xmlTextReaderConstName value is NULL\n");
1873                         return -1;
1874                 }
1875
1876                 if (!strcmp(ASCII(node), "operation")) {
1877                         operation_x *operation = malloc(sizeof(operation_x));
1878                         if (operation == NULL) {
1879                                 DBG("Malloc Failed\n");
1880                                 return -1;
1881                         }
1882                         memset(operation, '\0', sizeof(operation_x));
1883                         LISTADD(appsvc->operation, operation);
1884                         ret = __ps_process_operation(reader, operation);
1885                         DBG("operation processing\n");
1886                 } else if (!strcmp(ASCII(node), "uri")) {
1887                         uri_x *uri= malloc(sizeof(uri_x));
1888                         if (uri == NULL) {
1889                                 DBG("Malloc Failed\n");
1890                                 return -1;
1891                         }
1892                         memset(uri, '\0', sizeof(uri_x));
1893                         LISTADD(appsvc->uri, uri);
1894                         ret = __ps_process_uri(reader, uri);
1895                         DBG("uri processing\n");
1896                 } else if (!strcmp(ASCII(node), "mime")) {
1897                         mime_x *mime = malloc(sizeof(mime_x));
1898                         if (mime == NULL) {
1899                                 DBG("Malloc Failed\n");
1900                                 return -1;
1901                         }
1902                         memset(mime, '\0', sizeof(mime_x));
1903                         LISTADD(appsvc->mime, mime);
1904                         ret = __ps_process_mime(reader, mime);
1905                         DBG("mime processing\n");
1906                 } else if (!strcmp(ASCII(node), "subapp")) {
1907                         subapp_x *subapp = malloc(sizeof(subapp_x));
1908                         if (subapp == NULL) {
1909                                 DBG("Malloc Failed\n");
1910                                 return -1;
1911                         }
1912                         memset(subapp, '\0', sizeof(subapp_x));
1913                         LISTADD(appsvc->subapp, subapp);
1914                         ret = __ps_process_subapp(reader, subapp);
1915                         DBG("subapp processing\n");
1916                 } else
1917                         return -1;
1918                 if (ret < 0) {
1919                         DBG("Processing appsvc failed\n");
1920                         return ret;
1921                 }
1922         }
1923         if (appsvc->operation) {
1924                 LISTHEAD(appsvc->operation, tmp1);
1925                 appsvc->operation = tmp1;
1926         }
1927         if (appsvc->uri) {
1928                 LISTHEAD(appsvc->uri, tmp2);
1929                 appsvc->uri = tmp2;
1930         }
1931         if (appsvc->mime) {
1932                 LISTHEAD(appsvc->mime, tmp3);
1933                 appsvc->mime = tmp3;
1934         }
1935         if (appsvc->subapp) {
1936                 LISTHEAD(appsvc->subapp, tmp4);
1937                 appsvc->subapp = tmp4;
1938         }
1939
1940         xmlTextReaderRead(reader);
1941         if (xmlTextReaderValue(reader))
1942                 appsvc->text = ASCII(xmlTextReaderValue(reader));
1943
1944         return ret;
1945 }
1946
1947
1948 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges)
1949 {
1950         const xmlChar *node;
1951         int ret = -1;
1952         int depth = -1;
1953         privilege_x *tmp1 = NULL;
1954
1955         depth = xmlTextReaderDepth(reader);
1956         while ((ret = __next_child_element(reader, depth))) {
1957                 node = xmlTextReaderConstName(reader);
1958                 if (!node) {
1959                         DBG("xmlTextReaderConstName value is NULL\n");
1960                         return -1;
1961                 }
1962
1963                 if (strcmp(ASCII(node), "privilege") == 0) {
1964                         privilege_x *privilege = malloc(sizeof(privilege_x));
1965                         if (privilege == NULL) {
1966                                 DBG("Malloc Failed\n");
1967                                 return -1;
1968                         }
1969                         memset(privilege, '\0', sizeof(privilege_x));
1970                         LISTADD(privileges->privilege, privilege);
1971                         ret = __ps_process_privilege(reader, privilege);
1972                 } else
1973                         return -1;
1974                 if (ret < 0) {
1975                         DBG("Processing privileges failed\n");
1976                         return ret;
1977                 }
1978         }
1979         if (privileges->privilege) {
1980                 LISTHEAD(privileges->privilege, tmp1);
1981                 privileges->privilege = tmp1;
1982         }
1983         return ret;
1984 }
1985
1986 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
1987 {
1988         const xmlChar *node;
1989         int ret = -1;
1990         int depth = -1;
1991         condition_x *tmp1 = NULL;
1992
1993         depth = xmlTextReaderDepth(reader);
1994         while ((ret = __next_child_element(reader, depth))) {
1995                 node = xmlTextReaderConstName(reader);
1996                 if (!node) {
1997                         DBG("xmlTextReaderConstName value is NULL\n");
1998                         return -1;
1999                 }
2000
2001                 if (strcmp(ASCII(node), "condition") == 0) {
2002                         condition_x *condition = malloc(sizeof(condition_x));
2003                         if (condition == NULL) {
2004                                 DBG("Malloc Failed\n");
2005                                 return -1;
2006                         }
2007                         memset(condition, '\0', sizeof(condition_x));
2008                         LISTADD(launchconditions->condition, condition);
2009                         ret = __ps_process_condition(reader, condition);
2010                 } else
2011                         return -1;
2012                 if (ret < 0) {
2013                         DBG("Processing launchconditions failed\n");
2014                         return ret;
2015                 }
2016         }
2017         if (launchconditions->condition) {
2018                 LISTHEAD(launchconditions->condition, tmp1);
2019                 launchconditions->condition = tmp1;
2020         }
2021
2022         xmlTextReaderRead(reader);
2023         if (xmlTextReaderValue(reader))
2024                 launchconditions->text = ASCII(xmlTextReaderValue(reader));
2025
2026         return ret;
2027 }
2028
2029 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
2030 {
2031         const xmlChar *node;
2032         int ret = -1;
2033         int depth = -1;
2034         define_x *tmp1 = NULL;
2035         request_x *tmp2 = NULL;
2036         depth = xmlTextReaderDepth(reader);
2037         while ((ret = __next_child_element(reader, depth))) {
2038                 node = xmlTextReaderConstName(reader);
2039                 if (!node) {
2040                         DBG("xmlTextReaderConstName value is NULL\n");
2041                         return -1;
2042                 }
2043
2044                 if (!strcmp(ASCII(node), "define")) {
2045                         define_x *define= malloc(sizeof(define_x));
2046                         if (define == NULL) {
2047                                 DBG("Malloc Failed\n");
2048                                 return -1;
2049                         }
2050                         memset(define, '\0', sizeof(define_x));
2051                         LISTADD(datashare->define, define);
2052                         ret = __ps_process_define(reader, define);
2053                 } else if (!strcmp(ASCII(node), "request")) {
2054                         request_x *request= malloc(sizeof(request_x));
2055                         if (request == NULL) {
2056                                 DBG("Malloc Failed\n");
2057                                 return -1;
2058                         }
2059                         memset(request, '\0', sizeof(request_x));
2060                         LISTADD(datashare->request, request);
2061                         ret = __ps_process_request(reader, request);
2062                 } else
2063                         return -1;
2064                 if (ret < 0) {
2065                         DBG("Processing data-share failed\n");
2066                         return ret;
2067                 }
2068         }
2069         if (datashare->define) {
2070                 LISTHEAD(datashare->define, tmp1);
2071                 datashare->define = tmp1;
2072         }
2073         if (datashare->request) {
2074                 LISTHEAD(datashare->request, tmp2);
2075                 datashare->request = tmp2;
2076         }
2077         return ret;
2078 }
2079
2080 static char*
2081 __get_icon_with_path(const char* icon)
2082 {
2083         if (!icon)
2084                 return NULL;
2085
2086         if (index(icon, '/') == NULL) {
2087                 char* theme = NULL;
2088                 char* icon_with_path = NULL;
2089                 int len;
2090
2091                 if (!package)
2092                         return NULL;
2093
2094                 theme = vconf_get_str("db/setting/theme");
2095                 if (!theme) {
2096                         theme = strdup("default");
2097                         if(!theme) {
2098                                 return NULL;
2099                         }
2100                 }
2101
2102                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
2103                 icon_with_path = malloc(len);
2104                 if(icon_with_path == NULL) {
2105                         DBG("(icon_with_path == NULL) return\n");
2106                         free(theme);
2107                         return NULL;
2108                 }
2109
2110                 memset(icon_with_path, 0, len);
2111
2112                 snprintf(icon_with_path, len, "/opt/share/icons/%s/small/%s", theme, icon);
2113                 do {
2114                         if (access(icon_with_path, R_OK) == 0) break;
2115                         snprintf(icon_with_path, len, "/usr/share/icons/%s/small/%s", theme, icon);
2116                         if (access(icon_with_path, R_OK) == 0) break;
2117                         DBG("cannot find icon %s", icon_with_path);
2118                         snprintf(icon_with_path, len,"/opt/share/icons/default/small/%s", icon);
2119                         if (access(icon_with_path, R_OK) == 0) break;
2120                         snprintf(icon_with_path, len, "/usr/share/icons/default/small/%s", icon);
2121                         if (access(icon_with_path, R_OK) == 0) break;
2122
2123                         /* icon path is going to be moved intto the app directory */
2124                         DBGE("icon file must be moved to %s", icon_with_path);
2125                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/%s/small/%s", package, theme, icon);
2126                         if (access(icon_with_path, R_OK) == 0) break;
2127                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/%s/small/%s", package, theme, icon);
2128                         if (access(icon_with_path, R_OK) == 0) break;
2129                         DBG("cannot find icon %s", icon_with_path);
2130                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/default/small/%s", package, icon);
2131                         if (access(icon_with_path, R_OK) == 0) break;
2132                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/default/small/%s", package, icon);
2133                         if (access(icon_with_path, R_OK) == 0) break;
2134                         snprintf(icon_with_path, len, "/usr/ug/res/images/%s/%s", package, icon);
2135                         if (access(icon_with_path, R_OK) == 0) break;
2136                 } while (0);
2137
2138                 free(theme);
2139
2140                 DBG("Icon path : %s ---> %s", icon, icon_with_path);
2141
2142                 return icon_with_path;
2143         } else {
2144                 char* confirmed_icon = NULL;
2145
2146                 confirmed_icon = strdup(icon);
2147                 if (!confirmed_icon)
2148                         return NULL;
2149                 return confirmed_icon;
2150         }
2151 }
2152
2153 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
2154 {
2155         int i = 0;
2156         char delims[] = "=";
2157         char *ret_result = NULL;
2158         char *tag = NULL;
2159
2160         if (tagv == NULL)
2161                 return;
2162
2163         for (tag = strdup(tagv[0]); tag != NULL; ) {
2164                 ret_result = strtok(tag, delims);
2165
2166                 /*check tag :  preload */
2167                 if (strcmp(ret_result, "preload") == 0) {
2168                         ret_result = strtok(NULL, delims);
2169                         if (strcmp(ret_result, "true") == 0) {
2170                                 free((void *)mfx->preload);
2171                                 mfx->preload = strdup("true");
2172                         } else if (strcmp(ret_result, "false") == 0) {
2173                                 free((void *)mfx->preload);
2174                                 mfx->preload = strdup("false");
2175                         }
2176                 /*check tag :  removable*/
2177                 } else if (strcmp(ret_result, "removable") == 0) {
2178                         ret_result = strtok(NULL, delims);
2179                         if (strcmp(ret_result, "true") == 0){
2180                                 free((void *)mfx->removable);
2181                                 mfx->removable = strdup("true");
2182                         } else if (strcmp(ret_result, "false") == 0) {
2183                                 free((void *)mfx->removable);
2184                                 mfx->removable = strdup("false");
2185                         }
2186                 /*check tag :  not matched*/
2187                 } else
2188                         DBG("tag process [%s]is not defined\n", ret_result);
2189
2190                 free(tag);
2191
2192                 /*check next value*/
2193                 if (tagv[++i] != NULL)
2194                         tag = strdup(tagv[i]);
2195                 else {
2196                         DBG("tag process success...\n");
2197                         return;
2198                 }
2199         }
2200 }
2201
2202 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon)
2203 {
2204         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2205                 icon->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2206         if (xmlTextReaderConstXmlLang(reader)) {
2207                 icon->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2208                 if (icon->lang == NULL)
2209                         icon->lang = strdup(DEFAULT_LOCALE);
2210         } else {
2211                 icon->lang = strdup(DEFAULT_LOCALE);
2212         }
2213         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
2214                 icon->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
2215         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
2216                 icon->size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
2217         if (xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")))
2218                 icon->resolution = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")));
2219         xmlTextReaderRead(reader);
2220         if (xmlTextReaderValue(reader)) {
2221                 const char *text  = ASCII(xmlTextReaderValue(reader));
2222                 if(text) {
2223                         icon->text = (const char *)__get_icon_with_path(text);
2224                         free((void *)text);
2225                 }
2226         }
2227
2228         return 0;
2229 }
2230
2231 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
2232 {
2233         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2234                 image->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2235         if (xmlTextReaderConstXmlLang(reader)) {
2236                 image->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2237                 if (image->lang == NULL)
2238                         image->lang = strdup(DEFAULT_LOCALE);
2239         } else {
2240                 image->lang = strdup(DEFAULT_LOCALE);
2241         }
2242         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
2243                 image->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
2244         xmlTextReaderRead(reader);
2245         if (xmlTextReaderValue(reader))
2246                 image->text = ASCII(xmlTextReaderValue(reader));
2247
2248         return 0;
2249 }
2250
2251 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
2252 {
2253         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2254                 label->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2255         if (xmlTextReaderConstXmlLang(reader)) {
2256                 label->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2257                 if (label->lang == NULL)
2258                         label->lang = strdup(DEFAULT_LOCALE);
2259         } else {
2260                 label->lang = strdup(DEFAULT_LOCALE);
2261         }
2262         xmlTextReaderRead(reader);
2263         if (xmlTextReaderValue(reader))
2264                 label->text = ASCII(xmlTextReaderValue(reader));
2265
2266 /*      DBG("lable name %s\n", label->name);
2267         DBG("lable lang %s\n", label->lang);
2268         DBG("lable text %s\n", label->text);
2269 */
2270         return 0;
2271
2272 }
2273
2274 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
2275 {
2276         if (xmlTextReaderGetAttribute(reader, XMLCHAR("email")))
2277                 author->email = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("email")));
2278         if (xmlTextReaderGetAttribute(reader, XMLCHAR("href")))
2279                 author->href = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("href")));
2280         if (xmlTextReaderConstXmlLang(reader)) {
2281                 author->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2282                 if (author->lang == NULL)
2283                         author->lang = strdup(DEFAULT_LOCALE);
2284         } else {
2285                 author->lang = strdup(DEFAULT_LOCALE);
2286         }
2287         xmlTextReaderRead(reader);
2288         if (xmlTextReaderValue(reader))
2289                 author->text = ASCII(xmlTextReaderValue(reader));
2290         return 0;
2291 }
2292
2293 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
2294 {
2295         if (xmlTextReaderConstXmlLang(reader)) {
2296                 description->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2297                 if (description->lang == NULL)
2298                         description->lang = strdup(DEFAULT_LOCALE);
2299         } else {
2300                 description->lang = strdup(DEFAULT_LOCALE);
2301         }
2302         xmlTextReaderRead(reader);
2303         if (xmlTextReaderValue(reader))
2304                 description->text = ASCII(xmlTextReaderValue(reader));
2305         return 0;
2306 }
2307
2308 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
2309 {
2310         if (xmlTextReaderConstXmlLang(reader)) {
2311                 license->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2312                 if (license->lang == NULL)
2313                         license->lang = strdup(DEFAULT_LOCALE);
2314         } else {
2315                 license->lang = strdup(DEFAULT_LOCALE);
2316         }
2317         xmlTextReaderRead(reader);
2318         if (xmlTextReaderValue(reader))
2319                 license->text = ASCII(xmlTextReaderValue(reader));
2320         return 0;
2321 }
2322
2323 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability)
2324 {
2325         const xmlChar *node;
2326         int ret = -1;
2327         int depth = -1;
2328         resolution_x *tmp1 = NULL;
2329
2330         if (xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")))
2331                 capability->operationid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")));
2332
2333         depth = xmlTextReaderDepth(reader);
2334         while ((ret = __next_child_element(reader, depth))) {
2335                 node = xmlTextReaderConstName(reader);
2336                 if (!node) {
2337                         DBG("xmlTextReaderConstName value is NULL\n");
2338                         return -1;
2339                 }
2340
2341                 if (!strcmp(ASCII(node), "resolution")) {
2342                         resolution_x *resolution = malloc(sizeof(resolution_x));
2343                         if (resolution == NULL) {
2344                                 DBG("Malloc Failed\n");
2345                                 return -1;
2346                         }
2347                         memset(resolution, '\0', sizeof(resolution_x));
2348                         LISTADD(capability->resolution, resolution);
2349                         ret = __ps_process_resolution(reader, resolution);
2350                 } else
2351                         return -1;
2352                 if (ret < 0) {
2353                         DBG("Processing capability failed\n");
2354                         return ret;
2355                 }
2356         }
2357
2358         if (capability->resolution) {
2359                 LISTHEAD(capability->resolution, tmp1);
2360                 capability->resolution = tmp1;
2361         }
2362
2363         return ret;
2364 }
2365
2366 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
2367 {
2368         const xmlChar *node;
2369         int ret = -1;
2370         int depth = -1;
2371         capability_x *tmp1 = NULL;
2372
2373         if (xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")))
2374                 datacontrol->providerid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")));
2375
2376         depth = xmlTextReaderDepth(reader);
2377         while ((ret = __next_child_element(reader, depth))) {
2378                 node = xmlTextReaderConstName(reader);
2379                 if (!node) {
2380                         DBG("xmlTextReaderConstName value is NULL\n");
2381                         return -1;
2382                 }
2383
2384                 if (!strcmp(ASCII(node), "capability")) {
2385                         capability_x *capability = malloc(sizeof(capability_x));
2386                         if (capability == NULL) {
2387                                 DBG("Malloc Failed\n");
2388                                 return -1;
2389                         }
2390                         memset(capability, '\0', sizeof(capability_x));
2391                         LISTADD(datacontrol->capability, capability);
2392                         ret = __ps_process_capability(reader, capability);
2393                 } else
2394                         return -1;
2395                 if (ret < 0) {
2396                         DBG("Processing datacontrol failed\n");
2397                         return ret;
2398                 }
2399         }
2400
2401         if (datacontrol->capability) {
2402                 LISTHEAD(datacontrol->capability, tmp1);
2403                 datacontrol->capability = tmp1;
2404         }
2405
2406         return ret;
2407 }
2408
2409 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication)
2410 {
2411         const xmlChar *node;
2412         int ret = -1;
2413         int depth = -1;
2414         char *newappid = NULL;
2415         label_x *tmp1 = NULL;
2416         icon_x *tmp2 = NULL;
2417         appsvc_x *tmp3 = NULL;
2418         appcontrol_x *tmp4 = NULL;
2419         launchconditions_x *tmp5 = NULL;
2420         notification_x *tmp6 = NULL;
2421         datashare_x *tmp7 = NULL;
2422         category_x *tmp8 = NULL;
2423         metadata_x *tmp9 = NULL;
2424         image_x *tmp10 = NULL;
2425         permission_x *tmp11 = NULL;
2426
2427         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
2428                 uiapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
2429                 if (uiapplication->appid == NULL) {
2430                         DBG("appid cant be NULL\n");
2431                         return -1;
2432                 }
2433         } else {
2434                 DBG("appid is mandatory\n");
2435                 return -1;
2436         }
2437         /*check appid*/
2438         ret = __validate_appid(package, uiapplication->appid, &newappid);
2439         if (ret == -1) {
2440                 DBG("appid is not proper\n");
2441                 return -1;
2442         } else {
2443                 if (newappid) {
2444                         if (uiapplication->appid)
2445                                 free((void *)uiapplication->appid);
2446                         uiapplication->appid = newappid;
2447                 }
2448         }
2449         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
2450                 uiapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
2451         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay"))) {
2452                 uiapplication->nodisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay")));
2453                 if (uiapplication->nodisplay == NULL)
2454                         uiapplication->nodisplay = strdup("false");
2455         } else {
2456                 uiapplication->nodisplay = strdup("false");
2457         }
2458         if (xmlTextReaderGetAttribute(reader, XMLCHAR("multiple"))) {
2459                 uiapplication->multiple = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("multiple")));
2460                 if (uiapplication->multiple == NULL)
2461                         uiapplication->multiple = strdup("false");
2462         } else {
2463                 uiapplication->multiple = strdup("false");
2464         }
2465         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2466                 uiapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2467         if (xmlTextReaderGetAttribute(reader, XMLCHAR("categories")))
2468                 uiapplication->categories = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("categories")));
2469         if (xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")))
2470                 uiapplication->extraid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")));
2471         if (xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage"))) {
2472                 uiapplication->taskmanage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage")));
2473                 if (uiapplication->taskmanage == NULL)
2474                         uiapplication->taskmanage = strdup("true");
2475         } else {
2476                 uiapplication->taskmanage = strdup("true");
2477         }
2478         if (xmlTextReaderGetAttribute(reader, XMLCHAR("enabled"))) {
2479                 uiapplication->enabled = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("enabled")));
2480                 if (uiapplication->enabled == NULL)
2481                         uiapplication->enabled = strdup("true");
2482         } else {
2483                 uiapplication->enabled = strdup("true");
2484         }
2485         if (xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration"))) {
2486                 uiapplication->hwacceleration = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration")));
2487                 if (uiapplication->hwacceleration == NULL)
2488                         uiapplication->hwacceleration = strdup("use-system-setting");
2489         } else {
2490                 uiapplication->hwacceleration = strdup("use-system-setting");
2491         }
2492         if (xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")))
2493                 uiapplication->recentimage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")));
2494         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp"))) {
2495                 uiapplication->mainapp = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp")));
2496                 if (uiapplication->mainapp == NULL)
2497                         uiapplication->mainapp = strdup("false");
2498         } else {
2499                 uiapplication->mainapp = strdup("false");
2500         }
2501         if (xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition"))) {
2502                 uiapplication->launchcondition = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition")));
2503                 if (uiapplication->launchcondition == NULL)
2504                         uiapplication->launchcondition = strdup("false");
2505         } else {
2506                 uiapplication->launchcondition = strdup("false");
2507         }
2508
2509         if (xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay"))) {
2510                 uiapplication->indicatordisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay")));
2511                 if (uiapplication->indicatordisplay == NULL)
2512                         uiapplication->indicatordisplay = strdup("true");
2513         } else {
2514                 uiapplication->indicatordisplay = strdup("true");
2515         }
2516         if (xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")))
2517                 uiapplication->portraitimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")));
2518         else
2519                 uiapplication->portraitimg = NULL;
2520         if (xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")))
2521                 uiapplication->landscapeimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")));
2522         else
2523                 uiapplication->landscapeimg = NULL;
2524         if (xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility"))) {
2525                 uiapplication->guestmode_visibility = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility")));
2526                 if (uiapplication->guestmode_visibility == NULL)
2527                         uiapplication->guestmode_visibility = strdup("true");
2528         } else {
2529                 uiapplication->guestmode_visibility = strdup("true");
2530         }
2531
2532         depth = xmlTextReaderDepth(reader);
2533         while ((ret = __next_child_element(reader, depth))) {
2534                 node = xmlTextReaderConstName(reader);
2535                 if (!node) {
2536                         DBG("xmlTextReaderConstName value is NULL\n");
2537                         return -1;
2538                 }
2539                 if (!strcmp(ASCII(node), "label")) {
2540                         label_x *label = malloc(sizeof(label_x));
2541                         if (label == NULL) {
2542                                 DBG("Malloc Failed\n");
2543                                 return -1;
2544                         }
2545                         memset(label, '\0', sizeof(label_x));
2546                         LISTADD(uiapplication->label, label);
2547                         ret = __ps_process_label(reader, label);
2548                 } else if (!strcmp(ASCII(node), "icon")) {
2549                         icon_x *icon = malloc(sizeof(icon_x));
2550                         if (icon == NULL) {
2551                                 DBG("Malloc Failed\n");
2552                                 return -1;
2553                         }
2554                         memset(icon, '\0', sizeof(icon_x));
2555                         LISTADD(uiapplication->icon, icon);
2556                         ret = __ps_process_icon(reader, icon);
2557                 } else if (!strcmp(ASCII(node), "image")) {
2558                         image_x *image = malloc(sizeof(image_x));
2559                         if (image == NULL) {
2560                                 DBG("Malloc Failed\n");
2561                                 return -1;
2562                         }
2563                         memset(image, '\0', sizeof(image_x));
2564                         LISTADD(uiapplication->image, image);
2565                         ret = __ps_process_image(reader, image);
2566                 } else if (!strcmp(ASCII(node), "category")) {
2567                         category_x *category = malloc(sizeof(category_x));
2568                         if (category == NULL) {
2569                                 DBG("Malloc Failed\n");
2570                                 return -1;
2571                         }
2572                         memset(category, '\0', sizeof(category_x));
2573                         LISTADD(uiapplication->category, category);
2574                         ret = __ps_process_category(reader, category);
2575                 } else if (!strcmp(ASCII(node), "metadata")) {
2576                         metadata_x *metadata = malloc(sizeof(metadata_x));
2577                         if (metadata == NULL) {
2578                                 DBG("Malloc Failed\n");
2579                                 return -1;
2580                         }
2581                         memset(metadata, '\0', sizeof(metadata_x));
2582                         LISTADD(uiapplication->metadata, metadata);
2583                         ret = __ps_process_metadata(reader, metadata);
2584                 } else if (!strcmp(ASCII(node), "permission")) {
2585                         permission_x *permission = malloc(sizeof(permission_x));
2586                         if (permission == NULL) {
2587                                 DBG("Malloc Failed\n");
2588                                 return -1;
2589                         }
2590                         memset(permission, '\0', sizeof(permission_x));
2591                         LISTADD(uiapplication->permission, permission);
2592                         ret = __ps_process_permission(reader, permission);
2593                 } else if (!strcmp(ASCII(node), "app-control")) {
2594                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
2595                         if (appcontrol == NULL) {
2596                                 DBG("Malloc Failed\n");
2597                                 return -1;
2598                         }
2599                         memset(appcontrol, '\0', sizeof(appcontrol_x));
2600                         LISTADD(uiapplication->appcontrol, appcontrol);
2601                         ret = __ps_process_appcontrol(reader, appcontrol);
2602                 } else if (!strcmp(ASCII(node), "application-service")) {
2603                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
2604                         if (appsvc == NULL) {
2605                                 DBG("Malloc Failed\n");
2606                                 return -1;
2607                         }
2608                         memset(appsvc, '\0', sizeof(appsvc_x));
2609                         LISTADD(uiapplication->appsvc, appsvc);
2610                         ret = __ps_process_appsvc(reader, appsvc);
2611                 } else if (!strcmp(ASCII(node), "data-share")) {
2612                         datashare_x *datashare = malloc(sizeof(datashare_x));
2613                         if (datashare == NULL) {
2614                                 DBG("Malloc Failed\n");
2615                                 return -1;
2616                         }
2617                         memset(datashare, '\0', sizeof(datashare_x));
2618                         LISTADD(uiapplication->datashare, datashare);
2619                         ret = __ps_process_datashare(reader, datashare);
2620                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
2621                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
2622                         if (launchconditions == NULL) {
2623                                 DBG("Malloc Failed\n");
2624                                 return -1;
2625                         }
2626                         memset(launchconditions, '\0', sizeof(launchconditions_x));
2627                         LISTADD(uiapplication->launchconditions, launchconditions);
2628                         ret = __ps_process_launchconditions(reader, launchconditions);
2629                 } else if (!strcmp(ASCII(node), "notification")) {
2630                         notification_x *notification = malloc(sizeof(notification_x));
2631                         if (notification == NULL) {
2632                                 DBG("Malloc Failed\n");
2633                                 return -1;
2634                         }
2635                         memset(notification, '\0', sizeof(notification_x));
2636                         LISTADD(uiapplication->notification, notification);
2637                         ret = __ps_process_notification(reader, notification);
2638                 } else
2639                         return -1;
2640                 if (ret < 0) {
2641                         DBG("Processing uiapplication failed\n");
2642                         return ret;
2643                 }
2644         }
2645
2646         if (uiapplication->label) {
2647                 LISTHEAD(uiapplication->label, tmp1);
2648                 uiapplication->label = tmp1;
2649         }
2650         if (uiapplication->icon) {
2651                 LISTHEAD(uiapplication->icon, tmp2);
2652                 uiapplication->icon = tmp2;
2653         }
2654         if (uiapplication->appsvc) {
2655                 LISTHEAD(uiapplication->appsvc, tmp3);
2656                 uiapplication->appsvc = tmp3;
2657         }
2658         if (uiapplication->appcontrol) {
2659                 LISTHEAD(uiapplication->appcontrol, tmp4);
2660                 uiapplication->appcontrol = tmp4;
2661         }
2662         if (uiapplication->launchconditions) {
2663                 LISTHEAD(uiapplication->launchconditions, tmp5);
2664                 uiapplication->launchconditions = tmp5;
2665         }
2666         if (uiapplication->notification) {
2667                 LISTHEAD(uiapplication->notification, tmp6);
2668                 uiapplication->notification = tmp6;
2669         }
2670         if (uiapplication->datashare) {
2671                 LISTHEAD(uiapplication->datashare, tmp7);
2672                 uiapplication->datashare = tmp7;
2673         }
2674         if (uiapplication->category) {
2675                 LISTHEAD(uiapplication->category, tmp8);
2676                 uiapplication->category = tmp8;
2677         }
2678         if (uiapplication->metadata) {
2679                 LISTHEAD(uiapplication->metadata, tmp9);
2680                 uiapplication->metadata = tmp9;
2681         }
2682         if (uiapplication->image) {
2683                 LISTHEAD(uiapplication->image, tmp10);
2684                 uiapplication->image = tmp10;
2685         }
2686         if (uiapplication->permission) {
2687                 LISTHEAD(uiapplication->permission, tmp11);
2688                 uiapplication->permission = tmp11;
2689         }
2690
2691         return ret;
2692 }
2693
2694 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication)
2695 {
2696         const xmlChar *node;
2697         int ret = -1;
2698         int depth = -1;
2699         char *newappid = NULL;
2700         label_x *tmp1 = NULL;
2701         icon_x *tmp2 = NULL;
2702         appsvc_x *tmp3 = NULL;
2703         appcontrol_x *tmp4 = NULL;
2704         datacontrol_x *tmp5 = NULL;
2705         launchconditions_x *tmp6 = NULL;
2706         notification_x *tmp7 = NULL;
2707         datashare_x *tmp8 = NULL;
2708         category_x *tmp9 = NULL;
2709         metadata_x *tmp10 = NULL;
2710         permission_x *tmp11 = NULL;
2711
2712         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
2713                 serviceapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
2714                 if (serviceapplication->appid == NULL) {
2715                         DBG("appid cant be NULL\n");
2716                         return -1;
2717                 }
2718         } else {
2719                 DBG("appid is mandatory\n");
2720                 return -1;
2721         }
2722         /*check appid*/
2723         ret = __validate_appid(package, serviceapplication->appid, &newappid);
2724         if (ret == -1) {
2725                 DBG("appid is not proper\n");
2726                 return -1;
2727         } else {
2728                 if (newappid) {
2729                         if (serviceapplication->appid)
2730                                 free((void *)serviceapplication->appid);
2731                         serviceapplication->appid = newappid;
2732                 }
2733         }
2734         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
2735                 serviceapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
2736         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2737                 serviceapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2738         if (xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot"))) {
2739                 serviceapplication->onboot = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot")));
2740                 if (serviceapplication->onboot == NULL)
2741                         serviceapplication->onboot = strdup("false");
2742         } else {
2743                 serviceapplication->onboot = strdup("false");
2744         }
2745         if (xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart"))) {
2746                 serviceapplication->autorestart = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart")));
2747                 if (serviceapplication->autorestart == NULL)
2748                         serviceapplication->autorestart = strdup("false");
2749         } else {
2750                 serviceapplication->autorestart = strdup("false");
2751         }
2752
2753         depth = xmlTextReaderDepth(reader);
2754         while ((ret = __next_child_element(reader, depth))) {
2755                 node = xmlTextReaderConstName(reader);
2756                 if (!node) {
2757                         DBG("xmlTextReaderConstName value is NULL\n");
2758                         return -1;
2759                 }
2760
2761                 if (!strcmp(ASCII(node), "label")) {
2762                         label_x *label = malloc(sizeof(label_x));
2763                         if (label == NULL) {
2764                                 DBG("Malloc Failed\n");
2765                                 return -1;
2766                         }
2767                         memset(label, '\0', sizeof(label_x));
2768                         LISTADD(serviceapplication->label, label);
2769                         ret = __ps_process_label(reader, label);
2770                 } else if (!strcmp(ASCII(node), "icon")) {
2771                         icon_x *icon = malloc(sizeof(icon_x));
2772                         if (icon == NULL) {
2773                                 DBG("Malloc Failed\n");
2774                                 return -1;
2775                         }
2776                         memset(icon, '\0', sizeof(icon_x));
2777                         LISTADD(serviceapplication->icon, icon);
2778                         ret = __ps_process_icon(reader, icon);
2779                 } else if (!strcmp(ASCII(node), "category")) {
2780                         category_x *category = malloc(sizeof(category_x));
2781                         if (category == NULL) {
2782                                 DBG("Malloc Failed\n");
2783                                 return -1;
2784                         }
2785                         memset(category, '\0', sizeof(category_x));
2786                         LISTADD(serviceapplication->category, category);
2787                         ret = __ps_process_category(reader, category);
2788                 } else if (!strcmp(ASCII(node), "metadata")) {
2789                         metadata_x *metadata = malloc(sizeof(metadata_x));
2790                         if (metadata == NULL) {
2791                                 DBG("Malloc Failed\n");
2792                                 return -1;
2793                         }
2794                         memset(metadata, '\0', sizeof(metadata_x));
2795                         LISTADD(serviceapplication->metadata, metadata);
2796                         ret = __ps_process_metadata(reader, metadata);
2797                 } else if (!strcmp(ASCII(node), "permission")) {
2798                         permission_x *permission = malloc(sizeof(permission_x));
2799                         if (permission == NULL) {
2800                                 DBG("Malloc Failed\n");
2801                                 return -1;
2802                         }
2803                         memset(permission, '\0', sizeof(permission_x));
2804                         LISTADD(serviceapplication->permission, permission);
2805                         ret = __ps_process_permission(reader, permission);
2806                 } else if (!strcmp(ASCII(node), "app-control")) {
2807                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
2808                         if (appcontrol == NULL) {
2809                                 DBG("Malloc Failed\n");
2810                                 return -1;
2811                         }
2812                         memset(appcontrol, '\0', sizeof(appcontrol_x));
2813                         LISTADD(serviceapplication->appcontrol, appcontrol);
2814                         ret = __ps_process_appcontrol(reader, appcontrol);
2815                 } else if (!strcmp(ASCII(node), "application-service")) {
2816                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
2817                         if (appsvc == NULL) {
2818                                 DBG("Malloc Failed\n");
2819                                 return -1;
2820                         }
2821                         memset(appsvc, '\0', sizeof(appsvc_x));
2822                         LISTADD(serviceapplication->appsvc, appsvc);
2823                         ret = __ps_process_appsvc(reader, appsvc);
2824                 } else if (!strcmp(ASCII(node), "data-share")) {
2825                         datashare_x *datashare = malloc(sizeof(datashare_x));
2826                         if (datashare == NULL) {
2827                                 DBG("Malloc Failed\n");
2828                                 return -1;
2829                         }
2830                         memset(datashare, '\0', sizeof(datashare_x));
2831                         LISTADD(serviceapplication->datashare, datashare);
2832                         ret = __ps_process_datashare(reader, datashare);
2833                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
2834                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
2835                         if (launchconditions == NULL) {
2836                                 DBG("Malloc Failed\n");
2837                                 return -1;
2838                         }
2839                         memset(launchconditions, '\0', sizeof(launchconditions_x));
2840                         LISTADD(serviceapplication->launchconditions, launchconditions);
2841                         ret = __ps_process_launchconditions(reader, launchconditions);
2842                 } else if (!strcmp(ASCII(node), "notification")) {
2843                         notification_x *notification = malloc(sizeof(notification_x));
2844                         if (notification == NULL) {
2845                                 DBG("Malloc Failed\n");
2846                                 return -1;
2847                         }
2848                         memset(notification, '\0', sizeof(notification_x));
2849                         LISTADD(serviceapplication->notification, notification);
2850                         ret = __ps_process_notification(reader, notification);
2851                 } else if (!strcmp(ASCII(node), "data-control")) {
2852                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
2853                         if (datacontrol == NULL) {
2854                                 DBG("Malloc Failed\n");
2855                                 return -1;
2856                         }
2857                         memset(datacontrol, '\0', sizeof(datacontrol_x));
2858                         LISTADD(serviceapplication->datacontrol, datacontrol);
2859                         ret = __ps_process_datacontrol(reader, datacontrol);
2860                 } else
2861                         return -1;
2862                 if (ret < 0) {
2863                         DBG("Processing serviceapplication failed\n");
2864                         return ret;
2865                 }
2866         }
2867
2868         if (serviceapplication->label) {
2869                 LISTHEAD(serviceapplication->label, tmp1);
2870                 serviceapplication->label = tmp1;
2871         }
2872         if (serviceapplication->icon) {
2873                 LISTHEAD(serviceapplication->icon, tmp2);
2874                 serviceapplication->icon = tmp2;
2875         }
2876         if (serviceapplication->appsvc) {
2877                 LISTHEAD(serviceapplication->appsvc, tmp3);
2878                 serviceapplication->appsvc = tmp3;
2879         }
2880         if (serviceapplication->appcontrol) {
2881                 LISTHEAD(serviceapplication->appcontrol, tmp4);
2882                 serviceapplication->appcontrol = tmp4;
2883         }
2884         if (serviceapplication->datacontrol) {
2885                 LISTHEAD(serviceapplication->datacontrol, tmp5);
2886                 serviceapplication->datacontrol = tmp5;
2887         }
2888         if (serviceapplication->launchconditions) {
2889                 LISTHEAD(serviceapplication->launchconditions, tmp6);
2890                 serviceapplication->launchconditions = tmp6;
2891         }
2892         if (serviceapplication->notification) {
2893                 LISTHEAD(serviceapplication->notification, tmp7);
2894                 serviceapplication->notification = tmp7;
2895         }
2896         if (serviceapplication->datashare) {
2897                 LISTHEAD(serviceapplication->datashare, tmp8);
2898                 serviceapplication->datashare = tmp8;
2899         }
2900         if (serviceapplication->category) {
2901                 LISTHEAD(serviceapplication->category, tmp9);
2902                 serviceapplication->category = tmp9;
2903         }
2904         if (serviceapplication->metadata) {
2905                 LISTHEAD(serviceapplication->metadata, tmp10);
2906                 serviceapplication->metadata = tmp10;
2907         }
2908         if (serviceapplication->permission) {
2909                 LISTHEAD(serviceapplication->permission, tmp11);
2910                 serviceapplication->permission = tmp11;
2911         }
2912
2913         return ret;
2914 }
2915
2916 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
2917 {
2918         /*TODO: once policy is set*/
2919         return 0;
2920 }
2921
2922 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
2923 {
2924         /*TODO: once policy is set*/
2925         return 0;
2926 }
2927
2928 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
2929 {
2930         /*TODO: once policy is set*/
2931         return 0;
2932 }
2933
2934 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
2935 {
2936         /*TODO: once policy is set*/
2937         return 0;
2938 }
2939
2940 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
2941 {
2942         /*TODO: once policy is set*/
2943         return 0;
2944 }
2945
2946 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx)
2947 {
2948         DBG("__start_process\n");
2949         const xmlChar *node;
2950         int ret = -1;
2951         int depth = -1;
2952         label_x *tmp1 = NULL;
2953         author_x *tmp2 = NULL;
2954         description_x *tmp3 = NULL;
2955         license_x *tmp4 = NULL;
2956         uiapplication_x *tmp5 = NULL;
2957         serviceapplication_x *tmp6 = NULL;
2958         daemon_x *tmp7 = NULL;
2959         theme_x *tmp8 = NULL;
2960         font_x *tmp9 = NULL;
2961         ime_x *tmp10 = NULL;
2962         icon_x *tmp11 = NULL;
2963         compatibility_x *tmp12 = NULL;
2964         deviceprofile_x *tmp13 = NULL;
2965         privileges_x *tmp14 = NULL;
2966
2967         depth = xmlTextReaderDepth(reader);
2968         while ((ret = __next_child_element(reader, depth))) {
2969                 node = xmlTextReaderConstName(reader);
2970                 if (!node) {
2971                         DBG("xmlTextReaderConstName value is NULL\n");
2972                         return -1;
2973                 }
2974
2975                 if (!strcmp(ASCII(node), "label")) {
2976                         label_x *label = malloc(sizeof(label_x));
2977                         if (label == NULL) {
2978                                 DBG("Malloc Failed\n");
2979                                 return -1;
2980                         }
2981                         memset(label, '\0', sizeof(label_x));
2982                         LISTADD(mfx->label, label);
2983                         ret = __ps_process_label(reader, label);
2984                 } else if (!strcmp(ASCII(node), "author")) {
2985                         author_x *author = malloc(sizeof(author_x));
2986                         if (author == NULL) {
2987                                 DBG("Malloc Failed\n");
2988                                 return -1;
2989                         }
2990                         memset(author, '\0', sizeof(author_x));
2991                         LISTADD(mfx->author, author);
2992                         ret = __ps_process_author(reader, author);
2993                 } else if (!strcmp(ASCII(node), "description")) {
2994                         description_x *description = malloc(sizeof(description_x));
2995                         if (description == NULL) {
2996                                 DBG("Malloc Failed\n");
2997                                 return -1;
2998                         }
2999                         memset(description, '\0', sizeof(description_x));
3000                         LISTADD(mfx->description, description);
3001                         ret = __ps_process_description(reader, description);
3002                 } else if (!strcmp(ASCII(node), "license")) {
3003                         license_x *license = malloc(sizeof(license_x));
3004                         if (license == NULL) {
3005                                 DBG("Malloc Failed\n");
3006                                 return -1;
3007                         }
3008                         memset(license, '\0', sizeof(license_x));
3009                         LISTADD(mfx->license, license);
3010                         ret = __ps_process_license(reader, license);
3011                 } else if (!strcmp(ASCII(node), "privileges")) {
3012                         privileges_x *privileges = malloc(sizeof(privileges_x));
3013                         if (privileges == NULL) {
3014                                 DBG("Malloc Failed\n");
3015                                 return -1;
3016                         }
3017                         memset(privileges, '\0', sizeof(privileges_x));
3018                         LISTADD(mfx->privileges, privileges);
3019                         ret = __ps_process_privileges(reader, privileges);
3020                 } else if (!strcmp(ASCII(node), "ui-application")) {
3021                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
3022                         if (uiapplication == NULL) {
3023                                 DBG("Malloc Failed\n");
3024                                 return -1;
3025                         }
3026                         memset(uiapplication, '\0', sizeof(uiapplication_x));
3027                         LISTADD(mfx->uiapplication, uiapplication);
3028                         ret = __ps_process_uiapplication(reader, uiapplication);
3029                 } else if (!strcmp(ASCII(node), "service-application")) {
3030                         serviceapplication_x *serviceapplication = malloc(sizeof(serviceapplication_x));
3031                         if (serviceapplication == NULL) {
3032                                 DBG("Malloc Failed\n");
3033                                 return -1;
3034                         }
3035                         memset(serviceapplication, '\0', sizeof(serviceapplication_x));
3036                         LISTADD(mfx->serviceapplication, serviceapplication);
3037                         ret = __ps_process_serviceapplication(reader, serviceapplication);
3038                 } else if (!strcmp(ASCII(node), "daemon")) {
3039                         daemon_x *daemon = malloc(sizeof(daemon_x));
3040                         if (daemon == NULL) {
3041                                 DBG("Malloc Failed\n");
3042                                 return -1;
3043                         }
3044                         memset(daemon, '\0', sizeof(daemon_x));
3045                         LISTADD(mfx->daemon, daemon);
3046                         ret = __ps_process_daemon(reader, daemon);
3047                 } else if (!strcmp(ASCII(node), "theme")) {
3048                         theme_x *theme = malloc(sizeof(theme_x));
3049                         if (theme == NULL) {
3050                                 DBG("Malloc Failed\n");
3051                                 return -1;
3052                         }
3053                         memset(theme, '\0', sizeof(theme_x));
3054                         LISTADD(mfx->theme, theme);
3055                         ret = __ps_process_theme(reader, theme);
3056                 } else if (!strcmp(ASCII(node), "font")) {
3057                         font_x *font = malloc(sizeof(font_x));
3058                         if (font == NULL) {
3059                                 DBG("Malloc Failed\n");
3060                                 return -1;
3061                         }
3062                         memset(font, '\0', sizeof(font_x));
3063                         LISTADD(mfx->font, font);
3064                         ret = __ps_process_font(reader, font);
3065                 } else if (!strcmp(ASCII(node), "ime")) {
3066                         ime_x *ime = malloc(sizeof(ime_x));
3067                         if (ime == NULL) {
3068                                 DBG("Malloc Failed\n");
3069                                 return -1;
3070                         }
3071                         memset(ime, '\0', sizeof(ime_x));
3072                         LISTADD(mfx->ime, ime);
3073                         ret = __ps_process_ime(reader, ime);
3074                 } else if (!strcmp(ASCII(node), "icon")) {
3075                         icon_x *icon = malloc(sizeof(icon_x));
3076                         if (icon == NULL) {
3077                                 DBG("Malloc Failed\n");
3078                                 return -1;
3079                         }
3080                         memset(icon, '\0', sizeof(icon_x));
3081                         LISTADD(mfx->icon, icon);
3082                         ret = __ps_process_icon(reader, icon);
3083                 } else if (!strcmp(ASCII(node), "device-profile")) {
3084                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
3085                         if (deviceprofile == NULL) {
3086                                 DBG("Malloc Failed\n");
3087                                 return -1;
3088                         }
3089                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
3090                         LISTADD(mfx->deviceprofile, deviceprofile);
3091                         ret = __ps_process_deviceprofile(reader, deviceprofile);
3092                 } else if (!strcmp(ASCII(node), "compatibility")) {
3093                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
3094                         if (compatibility == NULL) {
3095                                 DBG("Malloc Failed\n");
3096                                 return -1;
3097                         }
3098                         memset(compatibility, '\0', sizeof(compatibility_x));
3099                         LISTADD(mfx->compatibility, compatibility);
3100                         ret = __ps_process_compatibility(reader, compatibility);
3101                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
3102                         continue;
3103                 } else if (!strcmp(ASCII(node), "livebox")) {
3104                         continue;
3105                 } else if (!strcmp(ASCII(node), "account")) {
3106                         continue;
3107                 } else if (!strcmp(ASCII(node), "notifications")) {
3108                         continue;
3109                 } else if (!strcmp(ASCII(node), "ime")) {
3110                         continue;
3111                 } else
3112                         return -1;
3113
3114                 if (ret < 0) {
3115                         DBG("Processing manifest failed\n");
3116                         return ret;
3117                 }
3118         }
3119         if (mfx->label) {
3120                 LISTHEAD(mfx->label, tmp1);
3121                 mfx->label = tmp1;
3122         }
3123         if (mfx->author) {
3124                 LISTHEAD(mfx->author, tmp2);
3125                 mfx->author = tmp2;
3126         }
3127         if (mfx->description) {
3128                 LISTHEAD(mfx->description, tmp3);
3129                 mfx->description= tmp3;
3130         }
3131         if (mfx->license) {
3132                 LISTHEAD(mfx->license, tmp4);
3133                 mfx->license= tmp4;
3134         }
3135         if (mfx->uiapplication) {
3136                 LISTHEAD(mfx->uiapplication, tmp5);
3137                 mfx->uiapplication = tmp5;
3138         }
3139         if (mfx->serviceapplication) {
3140                 LISTHEAD(mfx->serviceapplication, tmp6);
3141                 mfx->serviceapplication = tmp6;
3142         }
3143         if (mfx->daemon) {
3144                 LISTHEAD(mfx->daemon, tmp7);
3145                 mfx->daemon= tmp7;
3146         }
3147         if (mfx->theme) {
3148                 LISTHEAD(mfx->theme, tmp8);
3149                 mfx->theme= tmp8;
3150         }
3151         if (mfx->font) {
3152                 LISTHEAD(mfx->font, tmp9);
3153                 mfx->font= tmp9;
3154         }
3155         if (mfx->ime) {
3156                 LISTHEAD(mfx->ime, tmp10);
3157                 mfx->ime= tmp10;
3158         }
3159         if (mfx->icon) {
3160                 LISTHEAD(mfx->icon, tmp11);
3161                 mfx->icon= tmp11;
3162         }
3163         if (mfx->compatibility) {
3164                 LISTHEAD(mfx->compatibility, tmp12);
3165                 mfx->compatibility= tmp12;
3166         }
3167         if (mfx->deviceprofile) {
3168                 LISTHEAD(mfx->deviceprofile, tmp13);
3169                 mfx->deviceprofile= tmp13;
3170         }
3171         if (mfx->privileges) {
3172                 LISTHEAD(mfx->privileges, tmp14);
3173                 mfx->privileges = tmp14;
3174         }
3175         return ret;
3176 }
3177
3178 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx)
3179 {
3180         const xmlChar *node;
3181         int ret = -1;
3182
3183         if ((ret = __next_child_element(reader, -1))) {
3184                 node = xmlTextReaderConstName(reader);
3185                 if (!node) {
3186                         DBG("xmlTextReaderConstName value is NULL\n");
3187                         return -1;
3188                 }
3189
3190                 if (!strcmp(ASCII(node), "manifest")) {
3191                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")))
3192                                 mfx->ns = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")));
3193                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("package"))) {
3194                                 mfx->package= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("package")));
3195                                 if (mfx->package == NULL) {
3196                                         DBG("package cant be NULL\n");
3197                                         return -1;
3198                                 }
3199                         } else {
3200                                 DBG("package field is mandatory\n");
3201                                 return -1;
3202                         }
3203                         package = mfx->package;
3204                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("version")))
3205                                 mfx->version= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("version")));
3206                         /*app2ext needs package size for external installation*/
3207                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
3208                                 mfx->package_size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
3209                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")))
3210                                 mfx->installlocation = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")));
3211                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
3212                                 mfx->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
3213                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")))
3214                                 mfx->root_path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")));
3215                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting"))) {
3216                                 mfx->appsetting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting")));
3217                                 if (mfx->appsetting == NULL)
3218                                         mfx->appsetting = strdup("false");
3219                         } else {
3220                                 mfx->appsetting = strdup("false");
3221                         }
3222                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")))
3223                                 mfx->storeclient_id= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")));
3224                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting"))) {
3225                                 mfx->nodisplay_setting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting")));
3226                                 if (mfx->nodisplay_setting == NULL)
3227                                         mfx->nodisplay_setting = strdup("false");
3228                         } else {
3229                                 mfx->nodisplay_setting = strdup("false");
3230                         }
3231
3232                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
3233                         mfx->preload = strdup("False");
3234                         mfx->removable = strdup("True");
3235                         mfx->readonly = strdup("False");
3236                         char buf[PKG_STRING_LEN_MAX] = {'\0'};
3237                         char *val = NULL;
3238                         time_t current_time;
3239                         time(&current_time);
3240                         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", current_time);
3241                         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
3242                         mfx->installed_time = val;
3243
3244                         mfx->installed_storage= strdup("installed_internal");
3245
3246                         ret = __start_process(reader, mfx);
3247                 } else {
3248                         DBG("No Manifest element found\n");
3249                         return -1;
3250                 }
3251         }
3252         return ret;
3253 }
3254
3255 #define DESKTOP_RW_PATH "/opt/share/applications/"
3256 #define DESKTOP_RO_PATH "/usr/share/applications/"
3257
3258 static char* __convert_to_system_locale(const char *mlocale)
3259 {
3260         if (mlocale == NULL)
3261                 return NULL;
3262         char *locale = NULL;
3263         locale = (char *)calloc(1, 6);
3264         if (!locale) {
3265                 DBGE("Malloc Failed\n");
3266                 return NULL;
3267         }
3268
3269         strncpy(locale, mlocale, 2);
3270         strncat(locale, "_", 1);
3271         locale[3] = toupper(mlocale[3]);
3272         locale[4] = toupper(mlocale[4]);
3273         return locale;
3274 }
3275
3276 #define LIBAIL_PATH "/usr/lib/libail.so.0"
3277
3278 /* operation_type */
3279 typedef enum {
3280         AIL_INSTALL = 0,
3281         AIL_UPDATE,
3282         AIL_REMOVE,
3283         AIL_MAX
3284 } AIL_TYPE;
3285
3286 static int __ail_change_info(int op, const char *appid)
3287 {
3288         void *lib_handle = NULL;
3289         int (*ail_desktop_operation) (const char *);
3290         char *aop = NULL;
3291         int ret = 0;
3292
3293         if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
3294                 DBGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
3295                 goto END;
3296         }
3297
3298
3299         switch (op) {
3300                 case 0:
3301                         aop  = "ail_desktop_add";
3302                         break;
3303                 case 1:
3304                         aop  = "ail_desktop_update";
3305                         break;
3306                 case 2:
3307                         aop  = "ail_desktop_remove";
3308                         break;
3309                 default:
3310                         goto END;
3311                         break;
3312         }
3313
3314         if ((ail_desktop_operation =
3315              dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
3316                 DBGE("can not find symbol \n");
3317                 goto END;
3318         }
3319
3320         ret = ail_desktop_operation(appid);
3321
3322 END:
3323         if (lib_handle)
3324                 dlclose(lib_handle);
3325
3326         return ret;
3327 }
3328
3329
3330 /* desktop shoud be generated automatically based on manifest */
3331 /* Currently removable, taskmanage, etc fields are not considerd. it will be decided soon.*/
3332 #define BUFMAX 1024*128
3333 static int __ps_make_nativeapp_desktop(manifest_x * mfx, bool is_update)
3334 {
3335         FILE* file = NULL;
3336         int fd = 0;
3337         char filepath[PKG_STRING_LEN_MAX] = "";
3338         char *buf = NULL;
3339         char *buftemp = NULL;
3340         char *locale = NULL;
3341
3342         buf = (char *)calloc(1, BUFMAX);
3343         if (!buf) {
3344                 DBGE("Malloc Failed\n");
3345                 return -1;
3346         }
3347
3348         buftemp = (char *)calloc(1, BUFMAX);
3349         if (!buftemp) {
3350                 DBGE("Malloc Failed\n");
3351                 free(buf);
3352                 return -1;
3353         }
3354
3355         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
3356
3357                 if(mfx->readonly && !strcasecmp(mfx->readonly, "True"))
3358                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RO_PATH, mfx->uiapplication->appid);
3359                 else
3360                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, mfx->uiapplication->appid);
3361
3362                 /* skip if desktop exists
3363                 if (access(filepath, R_OK) == 0)
3364                         continue;
3365                 */
3366
3367                 file = fopen(filepath, "w");
3368                 if(file == NULL)
3369                 {
3370                     DBGE("Can't open %s", filepath);
3371                     free(buf);
3372                     free(buftemp);
3373                     return -1;
3374                 }
3375
3376                 snprintf(buf, BUFMAX, "[Desktop Entry]\n");
3377                 fwrite(buf, 1, strlen(buf), file);
3378
3379                 for( ; mfx->uiapplication->label ; mfx->uiapplication->label = mfx->uiapplication->label->next) {
3380                         if(!strcmp(mfx->uiapplication->label->lang, DEFAULT_LOCALE)) {
3381                                 snprintf(buf, BUFMAX, "Name=%s\n",      mfx->uiapplication->label->text);
3382                         } else {
3383                                 locale = __convert_to_system_locale(mfx->uiapplication->label->lang);
3384                                 snprintf(buf, BUFMAX, "Name[%s]=%s\n", locale,
3385                                         mfx->uiapplication->label->text);
3386                                 free(locale);
3387                         }
3388                         fwrite(buf, 1, strlen(buf), file);
3389                 }
3390
3391                 if(mfx->uiapplication->label && mfx->uiapplication->label->text) {
3392                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->uiapplication->label->text);
3393                         fwrite(buf, 1, strlen(buf), file);
3394                 }
3395 /*
3396                 else if(mfx->label && mfx->label->text) {
3397                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->label->text);
3398                         fwrite(buf, 1, strlen(buf), file);
3399                 } else {
3400                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->package);
3401                         fwrite(buf, 1, strlen(buf), file);
3402                 }
3403 */
3404
3405
3406                 snprintf(buf, BUFMAX, "Type=Application\n");
3407                 fwrite(buf, 1, strlen(buf), file);
3408
3409                 if(mfx->uiapplication->exec) {
3410                         snprintf(buf, BUFMAX, "Exec=%s\n", mfx->uiapplication->exec);
3411                         fwrite(buf, 1, strlen(buf), file);
3412                 }
3413
3414                 if(mfx->uiapplication->icon && mfx->uiapplication->icon->text) {
3415                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->uiapplication->icon->text);
3416                         fwrite(buf, 1, strlen(buf), file);
3417                 } else if(mfx->icon && mfx->icon->text) {
3418                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->icon->text);
3419                         fwrite(buf, 1, strlen(buf), file);
3420                 }
3421
3422                 if(mfx->version) {
3423                         snprintf(buf, BUFMAX, "Version=%s\n", mfx->version);
3424                         fwrite(buf, 1, strlen(buf), file);
3425                 }
3426
3427                 if(mfx->uiapplication->nodisplay) {
3428                         snprintf(buf, BUFMAX, "NoDisplay=%s\n", mfx->uiapplication->nodisplay);
3429                         fwrite(buf, 1, strlen(buf), file);
3430                 }
3431
3432                 if(mfx->uiapplication->categories) {
3433                         snprintf(buf, BUFMAX, "Categories=%s\n", mfx->uiapplication->categories);
3434                         fwrite(buf, 1, strlen(buf), file);
3435                 }
3436
3437                 if(mfx->uiapplication->taskmanage && !strcasecmp(mfx->uiapplication->taskmanage, "False")) {
3438                         snprintf(buf, BUFMAX, "X-TIZEN-TaskManage=False\n");
3439                         fwrite(buf, 1, strlen(buf), file);
3440                 }
3441
3442                 if(mfx->uiapplication->enabled && !strcasecmp(mfx->uiapplication->enabled, "False")) {
3443                         snprintf(buf, BUFMAX, "X-TIZEN-Enabled=False\n");
3444                         fwrite(buf, 1, strlen(buf), file);
3445                 }
3446
3447                 if(mfx->uiapplication->hwacceleration) {
3448                         snprintf(buf, BUFMAX, "Hw-Acceleration=%s\n", mfx->uiapplication->hwacceleration);
3449                         fwrite(buf, 1, strlen(buf), file);
3450                 }
3451
3452                 if(mfx->uiapplication->multiple && !strcasecmp(mfx->uiapplication->multiple, "True")) {
3453                         snprintf(buf, BUFMAX, "X-TIZEN-Multiple=True\n");
3454                         fwrite(buf, 1, strlen(buf), file);
3455                 }
3456
3457                 if(mfx->uiapplication->extraid) {
3458                         snprintf(buf, BUFMAX, "X-TIZEN-PackageID=%s\n", mfx->uiapplication->extraid);
3459                         fwrite(buf, 1, strlen(buf), file);
3460                 }
3461
3462                 if(mfx->removable && !strcasecmp(mfx->removable, "False")) {
3463                         snprintf(buf, BUFMAX, "X-TIZEN-Removable=False\n");
3464                         fwrite(buf, 1, strlen(buf), file);
3465                 }
3466
3467                 if(mfx->type) {
3468                         snprintf(buf, BUFMAX, "X-TIZEN-PackageType=%s\n", mfx->type);
3469                         fwrite(buf, 1, strlen(buf), file);
3470                 }
3471
3472                 snprintf(buf, BUFMAX, "X-TIZEN-PkgID=%s\n", mfx->package);
3473                 fwrite(buf, 1, strlen(buf), file);
3474
3475
3476 //              snprintf(buf, BUFMAX, "X-TIZEN-PackageType=rpm\n");
3477 //              fwrite(buf, 1, strlen(buf), file);
3478
3479
3480                 if(mfx->uiapplication->appsvc) {
3481                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3482                         DBG("buf[%s]\n", buf);
3483
3484
3485                         uiapplication_x *up = mfx->uiapplication;
3486                         appsvc_x *asvc = NULL;
3487                         operation_x *op = NULL;
3488                         mime_x *mi = NULL;
3489                         uri_x *ui = NULL;
3490                         subapp_x *sub = NULL;
3491                         const char *operation = NULL;
3492                         const char *mime = NULL;
3493                         const char *uri = NULL;
3494                         const char *subapp = NULL;
3495                         int i = 0;
3496
3497
3498                         asvc = up->appsvc;
3499                         while(asvc != NULL) {
3500                                 op = asvc->operation;
3501                                 while(op != NULL) {
3502                                         if (op)
3503                                                 operation = op->name;
3504                                         mi = asvc->mime;
3505
3506                                         do
3507                                         {
3508                                                 if (mi)
3509                                                         mime = mi->name;
3510                                                 sub = asvc->subapp;
3511                                                 do
3512                                                 {
3513                                                         if (sub)
3514                                                                 subapp = sub->name;
3515                                                         ui = asvc->uri;
3516                                                         do
3517                                                         {
3518                                                                 if (ui)
3519                                                                         uri = ui->name;
3520
3521                                                                 if(i++ > 0) {
3522                                                                         strncpy(buftemp, buf, BUFMAX);
3523                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3524                                                                 }
3525
3526
3527                                                                 strncpy(buftemp, buf, BUFMAX);
3528                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3529                                                                 DBG("buf[%s]\n", buf);
3530
3531                                                                 if (ui)
3532                                                                         ui = ui->next;
3533                                                                 uri = NULL;
3534                                                         } while(ui != NULL);
3535                                                 if (sub)
3536                                                                 sub = sub->next;
3537                                                         subapp = NULL;
3538                                                 }while(sub != NULL);
3539                                                 if (mi)
3540                                                         mi = mi->next;
3541                                                 mime = NULL;
3542                                         }while(mi != NULL);
3543                                         if (op)
3544                                                 op = op->next;
3545                                         operation = NULL;
3546                                 }
3547                                 asvc = asvc->next;
3548                         }
3549
3550
3551                         fwrite(buf, 1, strlen(buf), file);
3552
3553 //                      strncpy(buftemp, buf, BUFMAX);
3554 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
3555 //                      fwrite(buf, 1, strlen(buf), file);
3556                 }
3557
3558                 if(mfx->uiapplication->appcontrol) {
3559                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3560                         DBG("buf[%s]\n", buf);
3561
3562                         uiapplication_x *up = mfx->uiapplication;
3563                         appcontrol_x *acontrol = NULL;
3564                         operation_x *op = NULL;
3565                         mime_x *mi = NULL;
3566                         uri_x *ui = NULL;
3567                         subapp_x *sub = NULL;
3568                         const char *operation = NULL;
3569                         const char *mime = NULL;
3570                         const char *uri = NULL;
3571                         const char *subapp = NULL;
3572                         int i = 0;
3573
3574                         acontrol = up->appcontrol;
3575                         while(acontrol != NULL) {
3576                                 op = acontrol->operation;
3577                                 while(op != NULL) {
3578                                         if (op)
3579                                                 operation = op->name;
3580                                         mi = acontrol->mime;
3581
3582                                         do
3583                                         {
3584                                                 if (mi)
3585                                                         mime = mi->name;
3586                                                 sub = acontrol->subapp;
3587                                                 do
3588                                                 {
3589                                                         if (sub)
3590                                                                 subapp = sub->name;
3591                                                         ui = acontrol->uri;
3592                                                         do
3593                                                         {
3594                                                                 if (ui)
3595                                                                         uri = ui->name;
3596
3597                                                                 if(i++ > 0) {
3598                                                                         strncpy(buftemp, buf, BUFMAX);
3599                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3600                                                                 }
3601
3602                                                                 strncpy(buftemp, buf, BUFMAX);
3603                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3604                                                                 DBG("buf[%s]\n", buf);
3605
3606                                                                 if (ui)
3607                                                                         ui = ui->next;
3608                                                                 uri = NULL;
3609                                                         } while(ui != NULL);
3610                                                 if (sub)
3611                                                                 sub = sub->next;
3612                                                         subapp = NULL;
3613                                                 }while(sub != NULL);
3614                                                 if (mi)
3615                                                         mi = mi->next;
3616                                                 mime = NULL;
3617                                         }while(mi != NULL);
3618                                         if (op)
3619                                                 op = op->next;
3620                                         operation = NULL;
3621                                 }
3622                                 acontrol = acontrol->next;
3623                         }
3624
3625
3626                         fwrite(buf, 1, strlen(buf), file);
3627
3628 //                      strncpy(buftemp, buf, BUFMAX);
3629 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
3630 //                      fwrite(buf, 1, strlen(buf), file);
3631                 }
3632
3633                 fflush(file);
3634                 fd = fileno(file);
3635                 fsync(fd);
3636                 fclose(file);
3637
3638                 if (!is_update)
3639                         __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid);
3640                 else
3641                         __ail_change_info(AIL_UPDATE, mfx->uiapplication->appid);
3642         }
3643
3644         free(buf);
3645         free(buftemp);
3646
3647         return 0;
3648 }
3649
3650 static int __ps_remove_nativeapp_desktop(manifest_x *mfx)
3651 {
3652         char filepath[PKG_STRING_LEN_MAX] = "";
3653         int ret = 0;
3654         uiapplication_x *uiapplication = mfx->uiapplication;
3655
3656         for(; uiapplication; uiapplication=uiapplication->next) {
3657                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, uiapplication->appid);
3658
3659                 __ail_change_info(AIL_REMOVE, uiapplication->appid);
3660
3661                 ret = remove(filepath);
3662                 if (ret <0)
3663                         return -1;
3664         }
3665
3666         return 0;
3667 }
3668
3669 #define LIBAPPSVC_PATH "/usr/lib/libappsvc.so.0"
3670
3671 static int __ps_remove_appsvc_db(manifest_x *mfx)
3672 {
3673         void *lib_handle = NULL;
3674         int (*appsvc_operation) (const char *);
3675         int ret = 0;
3676         uiapplication_x *uiapplication = mfx->uiapplication;
3677
3678         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
3679                 DBGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAPPSVC_PATH);
3680                 goto END;
3681         }
3682
3683         if ((appsvc_operation =
3684                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
3685                 DBGE("can not find symbol \n");
3686                 goto END;
3687         }
3688
3689         for(; uiapplication; uiapplication=uiapplication->next) {
3690                 ret = appsvc_operation(uiapplication->appid);
3691                 if (ret <0)
3692                         DBGE("can not operation  symbol \n");
3693         }
3694
3695 END:
3696         if (lib_handle)
3697                 dlclose(lib_handle);
3698
3699         return ret;
3700 }
3701
3702 #define MANIFEST_RO_PREFIX "/usr/share/packages/"
3703 #define PRELOAD_PACKAGE_LIST "/usr/etc/package-manager/preload/preload_list.txt"
3704 static int __add_preload_info(manifest_x * mfx, const char *manifest)
3705 {
3706         FILE *fp = NULL;
3707         char buffer[1024] = { 0 };
3708         int state = 0;
3709
3710         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
3711                 free((void *)mfx->readonly);
3712                 mfx->readonly = strdup("True");
3713
3714                 free((void *)mfx->preload);
3715                 mfx->preload = strdup("True");
3716
3717                 free((void *)mfx->removable);
3718                 mfx->removable = strdup("False");
3719
3720                 return 0;
3721         }
3722
3723         fp = fopen(PRELOAD_PACKAGE_LIST, "r");
3724         if (fp == NULL) {
3725                 DBGE("no preload list\n");
3726                 return -1;
3727         }
3728
3729         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
3730                 if (buffer[0] == '#') {
3731                         if(strcasestr(buffer, "RW_NORM"))
3732                                 state = 2;
3733                         else if(strcasestr(buffer, "RW_RM"))
3734                                 state = 3;
3735                         else
3736                                 continue;
3737                 }
3738
3739                 __str_trim(buffer);
3740
3741                 if(!strcmp(mfx->package, buffer)) {
3742                         free((void *)mfx->preload);
3743                         mfx->preload = strdup("True");
3744                         if(state == 2){
3745                                 free((void *)mfx->readonly);
3746                                 mfx->readonly = strdup("False");
3747                                 free((void *)mfx->removable);
3748                                 mfx->removable = strdup("False");
3749                         } else if(state == 3){
3750                                 free((void *)mfx->readonly);
3751                                 mfx->readonly = strdup("False");
3752                                 free((void *)mfx->removable);
3753                                 mfx->removable = strdup("True");
3754                         }
3755                 }
3756
3757                 memset(buffer, 0x00, sizeof(buffer));
3758         }
3759
3760         if (fp != NULL)
3761                 fclose(fp);
3762
3763         return 0;
3764 }
3765
3766
3767 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
3768 {
3769         if (mfx == NULL)
3770                 return;
3771         if (mfx->ns) {
3772                 free((void *)mfx->ns);
3773                 mfx->ns = NULL;
3774         }
3775         if (mfx->package) {
3776                 free((void *)mfx->package);
3777                 mfx->package = NULL;
3778         }
3779         if (mfx->version) {
3780                 free((void *)mfx->version);
3781                 mfx->version = NULL;
3782         }
3783         if (mfx->installlocation) {
3784                 free((void *)mfx->installlocation);
3785                 mfx->installlocation = NULL;
3786         }
3787         if (mfx->preload) {
3788                 free((void *)mfx->preload);
3789                 mfx->preload = NULL;
3790         }
3791         if (mfx->readonly) {
3792                 free((void *)mfx->readonly);
3793                 mfx->readonly = NULL;
3794         }
3795         if (mfx->removable) {
3796                 free((void *)mfx->removable);
3797                 mfx->removable = NULL;
3798         }
3799         if (mfx->update) {
3800                 free((void *)mfx->update);
3801                 mfx->update = NULL;
3802         }
3803         if (mfx->type) {
3804                 free((void *)mfx->type);
3805                 mfx->type = NULL;
3806         }
3807         if (mfx->package_size) {
3808                 free((void *)mfx->package_size);
3809                 mfx->package_size = NULL;
3810         }
3811         if (mfx->installed_time) {
3812                 free((void *)mfx->installed_time);
3813                 mfx->installed_time = NULL;
3814         }
3815         if (mfx->installed_storage) {
3816                 free((void *)mfx->installed_storage);
3817                 mfx->installed_storage = NULL;
3818         }
3819         if (mfx->storeclient_id) {
3820                 free((void *)mfx->storeclient_id);
3821                 mfx->storeclient_id = NULL;
3822         }
3823         if (mfx->mainapp_id) {
3824                 free((void *)mfx->mainapp_id);
3825                 mfx->mainapp_id = NULL;
3826         }
3827         if (mfx->package_url) {
3828                 free((void *)mfx->package_url);
3829                 mfx->package_url = NULL;
3830         }
3831         if (mfx->root_path) {
3832                 free((void *)mfx->root_path);
3833                 mfx->root_path = NULL;
3834         }
3835         if (mfx->appsetting) {
3836                 free((void *)mfx->appsetting);
3837                 mfx->appsetting = NULL;
3838         }
3839         if (mfx->nodisplay_setting) {
3840                 free((void *)mfx->nodisplay_setting);
3841                 mfx->nodisplay_setting = NULL;
3842         }
3843
3844         /*Free Icon*/
3845         if (mfx->icon) {
3846                 icon_x *icon = mfx->icon;
3847                 icon_x *tmp = NULL;
3848                 while(icon != NULL) {
3849                         tmp = icon->next;
3850                         __ps_free_icon(icon);
3851                         icon = tmp;
3852                 }
3853         }
3854         /*Free Label*/
3855         if (mfx->label) {
3856                 label_x *label = mfx->label;
3857                 label_x *tmp = NULL;
3858                 while(label != NULL) {
3859                         tmp = label->next;
3860                         __ps_free_label(label);
3861                         label = tmp;
3862                 }
3863         }
3864         /*Free Author*/
3865         if (mfx->author) {
3866                 author_x *author = mfx->author;
3867                 author_x *tmp = NULL;
3868                 while(author != NULL) {
3869                         tmp = author->next;
3870                         __ps_free_author(author);
3871                         author = tmp;
3872                 }
3873         }
3874         /*Free Description*/
3875         if (mfx->description) {
3876                 description_x *description = mfx->description;
3877                 description_x *tmp = NULL;
3878                 while(description != NULL) {
3879                         tmp = description->next;
3880                         __ps_free_description(description);
3881                         description = tmp;
3882                 }
3883         }
3884         /*Free License*/
3885         if (mfx->license) {
3886                 license_x *license = mfx->license;
3887                 license_x *tmp = NULL;
3888                 while(license != NULL) {
3889                         tmp = license->next;
3890                         __ps_free_license(license);
3891                         license = tmp;
3892                 }
3893         }
3894         /*Free Privileges*/
3895         if (mfx->privileges) {
3896                 privileges_x *privileges = mfx->privileges;
3897                 privileges_x *tmp = NULL;
3898                 while(privileges != NULL) {
3899                         tmp = privileges->next;
3900                         __ps_free_privileges(privileges);
3901                         privileges = tmp;
3902                 }
3903         }
3904         /*Free UiApplication*/
3905         if (mfx->uiapplication) {
3906                 uiapplication_x *uiapplication = mfx->uiapplication;
3907                 uiapplication_x *tmp = NULL;
3908                 while(uiapplication != NULL) {
3909                         tmp = uiapplication->next;
3910                         __ps_free_uiapplication(uiapplication);
3911                         uiapplication = tmp;
3912                 }
3913         }
3914         /*Free ServiceApplication*/
3915         if (mfx->serviceapplication) {
3916                 serviceapplication_x *serviceapplication = mfx->serviceapplication;
3917                 serviceapplication_x *tmp = NULL;
3918                 while(serviceapplication != NULL) {
3919                         tmp = serviceapplication->next;
3920                         __ps_free_serviceapplication(serviceapplication);
3921                         serviceapplication = tmp;
3922                 }
3923         }
3924         /*Free Daemon*/
3925         if (mfx->daemon) {
3926                 daemon_x *daemon = mfx->daemon;
3927                 daemon_x *tmp = NULL;
3928                 while(daemon != NULL) {
3929                         tmp = daemon->next;
3930                         __ps_free_daemon(daemon);
3931                         daemon = tmp;
3932                 }
3933         }
3934         /*Free Theme*/
3935         if (mfx->theme) {
3936                 theme_x *theme = mfx->theme;
3937                 theme_x *tmp = NULL;
3938                 while(theme != NULL) {
3939                         tmp = theme->next;
3940                         __ps_free_theme(theme);
3941                         theme = tmp;
3942                 }
3943         }
3944         /*Free Font*/
3945         if (mfx->font) {
3946                 font_x *font = mfx->font;
3947                 font_x *tmp = NULL;
3948                 while(font != NULL) {
3949                         tmp = font->next;
3950                         __ps_free_font(font);
3951                         font = tmp;
3952                 }
3953         }
3954         /*Free Ime*/
3955         if (mfx->ime) {
3956                 ime_x *ime = mfx->ime;
3957                 ime_x *tmp = NULL;
3958                 while(ime != NULL) {
3959                         tmp = ime->next;
3960                         __ps_free_ime(ime);
3961                         ime = tmp;
3962                 }
3963         }
3964         /*Free Compatibility*/
3965         if (mfx->compatibility) {
3966                 compatibility_x *compatibility = mfx->compatibility;
3967                 compatibility_x *tmp = NULL;
3968                 while(compatibility != NULL) {
3969                         tmp = compatibility->next;
3970                         __ps_free_compatibility(compatibility);
3971                         compatibility = tmp;
3972                 }
3973         }
3974         /*Free DeviceProfile*/
3975         if (mfx->deviceprofile) {
3976                 deviceprofile_x *deviceprofile = mfx->deviceprofile;
3977                 deviceprofile_x *tmp = NULL;
3978                 while(deviceprofile != NULL) {
3979                         tmp = deviceprofile->next;
3980                         __ps_free_deviceprofile(deviceprofile);
3981                         deviceprofile = tmp;
3982                 }
3983         }
3984         free((void*)mfx);
3985         mfx = NULL;
3986         return;
3987 }
3988
3989 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
3990 {
3991         DBG("parsing start\n");
3992         xmlTextReaderPtr reader;
3993         manifest_x *mfx = NULL;
3994
3995         reader = xmlReaderForFile(manifest, NULL, 0);
3996         if (reader) {
3997                 mfx = malloc(sizeof(manifest_x));
3998                 if (mfx) {
3999                         memset(mfx, '\0', sizeof(manifest_x));
4000                         if (__process_manifest(reader, mfx) < 0) {
4001                                 DBG("Parsing Failed\n");
4002                                 pkgmgr_parser_free_manifest_xml(mfx);
4003                                 mfx = NULL;
4004                         } else
4005                                 DBG("Parsing Success\n");
4006                 } else {
4007                         DBG("Memory allocation error\n");
4008                 }
4009                 xmlFreeTextReader(reader);
4010         } else {
4011                 DBG("Unable to create xml reader\n");
4012         }
4013         return mfx;
4014 }
4015
4016 /* These APIs are intended to call parser directly */
4017
4018 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
4019 {
4020         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", NULL};
4021         if (manifest == NULL) {
4022                 DBG("argument supplied is NULL\n");
4023                 return PMINFO_R_EINVAL;
4024         }
4025         DBG("parsing manifest for installation: %s\n", manifest);
4026         manifest_x *mfx = NULL;
4027         int ret = -1;
4028         char roxml_check[PKG_STRING_LEN_MAX] = {'\0'};
4029
4030         xmlInitParser();
4031         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4032         DBG("Parsing Finished\n");
4033         if (mfx == NULL)
4034                 return PMINFO_R_ERROR;
4035         
4036         __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
4037         __add_preload_info(mfx, manifest);
4038         DBG("Added preload infomation\n");
4039
4040         snprintf(roxml_check, PKG_STRING_LEN_MAX, MANIFEST_RO_DIRECTORY "/%s.xml", mfx->package);
4041         if (access(roxml_check, F_OK) == 0)
4042                 mfx->update = strdup("true");
4043         else
4044                 mfx->update = strdup("false");
4045
4046         __ps_process_tag(mfx, tagv);
4047
4048         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
4049         if (ret == -1)
4050                 DBG("DB Insert failed\n");
4051         else
4052                 DBG("DB Insert Success\n");
4053
4054         ret = __ps_make_nativeapp_desktop(mfx, 0);
4055         if (ret == -1)
4056                 DBG("Creating desktop file failed\n");
4057         else
4058                 DBG("Creating desktop file Success\n");
4059
4060         pkgmgr_parser_free_manifest_xml(mfx);
4061         DBG("Free Done\n");
4062         xmlCleanupParser();
4063
4064         return PMINFO_R_OK;
4065 }
4066
4067 API int pkgmgr_parser_create_desktop_file(manifest_x *mfx)
4068 {
4069         int ret = 0;
4070         if (mfx == NULL) {
4071                 DBG("Manifest pointer is NULL\n");
4072                 return -1;
4073         }
4074         ret = __ps_make_nativeapp_desktop(mfx, 0);
4075         if (ret == -1)
4076                 DBG("Creating desktop file failed\n");
4077         else
4078                 DBG("Creating desktop file Success\n");
4079         return ret;
4080 }
4081
4082
4083 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
4084 {
4085         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", NULL};
4086         if (manifest == NULL) {
4087                 DBG("argument supplied is NULL\n");
4088                 return PMINFO_R_EINVAL;
4089         }
4090         DBG("parsing manifest for upgradation: %s\n", manifest);
4091         manifest_x *mfx = NULL;
4092         int ret = -1;
4093         bool preload;
4094         pkgmgrinfo_pkginfo_h handle;
4095
4096         xmlInitParser();
4097         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4098         DBG("Parsing Finished\n");
4099         if (mfx == NULL)
4100                 return PMINFO_R_ERROR;
4101         
4102         __streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
4103         __add_preload_info(mfx, manifest);
4104         DBG("Added preload infomation\n");
4105
4106         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
4107         if (ret != PMINFO_R_OK)
4108                 DBG("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
4109
4110         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
4111         if (ret != PMINFO_R_OK)
4112                 DBG("pkgmgrinfo_pkginfo_is_preload failed\n");
4113
4114         if (preload){
4115                 free((void *)mfx->preload);
4116                 mfx->preload = strdup("true");
4117         }
4118
4119         mfx->update = strdup("true");
4120         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
4121         if (ret == -1)
4122                 DBG("DB Update failed\n");
4123         else
4124                 DBG("DB Update Success\n");
4125
4126         ret = __ps_make_nativeapp_desktop(mfx, 1);
4127         if (ret == -1)
4128                 DBG("Creating desktop file failed\n");
4129         else
4130                 DBG("Creating desktop file Success\n");
4131
4132         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4133         pkgmgr_parser_free_manifest_xml(mfx);
4134         DBG("Free Done\n");
4135         xmlCleanupParser();
4136
4137         return PMINFO_R_OK;
4138 }
4139
4140 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
4141 {
4142         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", NULL};
4143         if (manifest == NULL) {
4144                 DBG("argument supplied is NULL\n");
4145                 return PMINFO_R_EINVAL;
4146         }
4147         DBG("parsing manifest for uninstallation: %s\n", manifest);
4148         manifest_x *mfx = NULL;
4149         int ret = -1;
4150         xmlInitParser();
4151         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4152         DBG("Parsing Finished\n");
4153         if (mfx == NULL)
4154                 return PMINFO_R_ERROR;
4155         
4156         __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
4157         __add_preload_info(mfx, manifest);
4158         DBG("Added preload infomation\n");
4159
4160         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
4161         if (ret == -1)
4162                 DBG("DB Delete failed\n");
4163         else
4164                 DBG("DB Delete Success\n");
4165
4166         ret = __ps_remove_nativeapp_desktop(mfx);
4167         if (ret == -1)
4168                 DBG("Removing desktop file failed\n");
4169         else
4170                 DBG("Removing desktop file Success\n");
4171
4172         ret = __ps_remove_appsvc_db(mfx);
4173         if (ret == -1)
4174                 DBG("Removing appsvc_db failed\n");
4175         else
4176                 DBG("Removing appsvc_db Success\n");
4177
4178         pkgmgr_parser_free_manifest_xml(mfx);
4179         DBG("Free Done\n");
4180         xmlCleanupParser();
4181
4182         return PMINFO_R_OK;
4183 }
4184
4185 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
4186 {
4187         return __pkgid_to_manifest(pkgid);
4188 }
4189
4190 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4191 {
4192         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
4193 }
4194
4195 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4196 {
4197         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
4198 }
4199
4200 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4201 {
4202         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
4203 }
4204
4205 #define SCHEMA_FILE "/usr/etc/package-manager/preload/manifest.xsd"
4206 #if 1
4207 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4208 {
4209         if (manifest == NULL) {
4210                 DBGE("manifest file is NULL\n");
4211                 return PMINFO_R_EINVAL;
4212         }
4213         int ret = -1;
4214         xmlSchemaParserCtxtPtr ctx;
4215         xmlSchemaValidCtxtPtr vctx;
4216         xmlSchemaPtr xschema;
4217         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
4218         if (ctx == NULL) {
4219                 DBGE("xmlSchemaNewParserCtxt() Failed\n");
4220                 return PMINFO_R_ERROR;
4221         }
4222         xschema = xmlSchemaParse(ctx);
4223         if (xschema == NULL) {
4224                 DBGE("xmlSchemaParse() Failed\n");
4225                 return PMINFO_R_ERROR;
4226         }
4227         vctx = xmlSchemaNewValidCtxt(xschema);
4228         if (vctx == NULL) {
4229                 DBGE("xmlSchemaNewValidCtxt() Failed\n");
4230                 return PMINFO_R_ERROR;
4231         }
4232         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
4233         ret = xmlSchemaValidateFile(vctx, manifest, 0);
4234         if (ret == -1) {
4235                 DBGE("xmlSchemaValidateFile() failed\n");
4236                 return PMINFO_R_ERROR;
4237         } else if (ret == 0) {
4238                 DBGE("Manifest is Valid\n");
4239                 return PMINFO_R_OK;
4240         } else {
4241                 DBGE("Manifest Validation Failed with error code %d\n", ret);
4242                 return PMINFO_R_ERROR;
4243         }
4244         return PMINFO_R_OK;
4245 }
4246
4247 #else
4248 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4249 {
4250         int err = 0;
4251         int status = 0;
4252         pid_t pid;
4253
4254         pid = fork();
4255
4256         switch (pid) {
4257         case -1:
4258                 DBGE("fork failed\n");
4259                 return -1;
4260         case 0:
4261                 /* child */
4262                 {
4263                         int dev_null_fd = open ("/dev/null", O_RDWR);
4264                         if (dev_null_fd >= 0)
4265                         {
4266                                 dup2 (dev_null_fd, 0);/*stdin*/
4267                                 dup2 (dev_null_fd, 1);/*stdout*/
4268                                 dup2 (dev_null_fd, 2);/*stderr*/
4269                         }
4270
4271                         if (execl("/usr/bin/xmllint", "xmllint", manifest, "--schema",
4272                                 SCHEMA_FILE, NULL) < 0) {
4273                                 DBGE("execl error\n");
4274                         }
4275
4276                         _exit(100);
4277                 }
4278         default:
4279                 /* parent */
4280                 break;
4281         }
4282
4283         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
4284                 if (err < 0) {
4285                         if (errno == EINTR)
4286                                 continue;
4287                         DBGE("waitpid failed\n");
4288                         return -1;
4289                 }
4290         }
4291
4292
4293         if(WIFEXITED(status) && !WEXITSTATUS(status))
4294                 return 0;
4295         else
4296                 return -1;
4297 }
4298 #endif