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