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