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