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