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