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