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