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