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