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