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