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