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