implement app screen reader
[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->screenreader) {
1444                 free((void *)uiapplication->screenreader);
1445                 uiapplication->screenreader = NULL;
1446         }
1447         if (uiapplication->mainapp) {
1448                 free((void *)uiapplication->mainapp);
1449                 uiapplication->mainapp = NULL;
1450         }
1451         if (uiapplication->recentimage) {
1452                 free((void *)uiapplication->recentimage);
1453                 uiapplication->recentimage = NULL;
1454         }
1455         if (uiapplication->package) {
1456                 free((void *)uiapplication->package);
1457                 uiapplication->package = NULL;
1458         }
1459         if (uiapplication->launchcondition) {
1460                 free((void *)uiapplication->launchcondition);
1461                 uiapplication->launchcondition = NULL;
1462         }
1463         /*Free Label*/
1464         if (uiapplication->label) {
1465                 label_x *label = uiapplication->label;
1466                 label_x *tmp = NULL;
1467                 while(label != NULL) {
1468                         tmp = label->next;
1469                         __ps_free_label(label);
1470                         label = tmp;
1471                 }
1472         }
1473         /*Free Icon*/
1474         if (uiapplication->icon) {
1475                 icon_x *icon = uiapplication->icon;
1476                 icon_x *tmp = NULL;
1477                 while(icon != NULL) {
1478                         tmp = icon->next;
1479                         __ps_free_icon(icon);
1480                         icon = tmp;
1481                 }
1482         }
1483         /*Free image*/
1484         if (uiapplication->image) {
1485                 image_x *image = uiapplication->image;
1486                 image_x *tmp = NULL;
1487                 while(image != NULL) {
1488                         tmp = image->next;
1489                         __ps_free_image(image);
1490                         image = tmp;
1491                 }
1492         }
1493         /*Free AppControl*/
1494         if (uiapplication->appcontrol) {
1495                 appcontrol_x *appcontrol = uiapplication->appcontrol;
1496                 appcontrol_x *tmp = NULL;
1497                 while(appcontrol != NULL) {
1498                         tmp = appcontrol->next;
1499                         __ps_free_appcontrol(appcontrol);
1500                         appcontrol = tmp;
1501                 }
1502         }
1503         /*Free LaunchConditions*/
1504         if (uiapplication->launchconditions) {
1505                 launchconditions_x *launchconditions = uiapplication->launchconditions;
1506                 launchconditions_x *tmp = NULL;
1507                 while(launchconditions != NULL) {
1508                         tmp = launchconditions->next;
1509                         __ps_free_launchconditions(launchconditions);
1510                         launchconditions = tmp;
1511                 }
1512         }
1513         /*Free Notification*/
1514         if (uiapplication->notification) {
1515                 notification_x *notification = uiapplication->notification;
1516                 notification_x *tmp = NULL;
1517                 while(notification != NULL) {
1518                         tmp = notification->next;
1519                         __ps_free_notification(notification);
1520                         notification = tmp;
1521                 }
1522         }
1523         /*Free DataShare*/
1524         if (uiapplication->datashare) {
1525                 datashare_x *datashare = uiapplication->datashare;
1526                 datashare_x *tmp = NULL;
1527                 while(datashare != NULL) {
1528                         tmp = datashare->next;
1529                         __ps_free_datashare(datashare);
1530                         datashare = tmp;
1531                 }
1532         }
1533         /*Free AppSvc*/
1534         if (uiapplication->appsvc) {
1535                 appsvc_x *appsvc = uiapplication->appsvc;
1536                 appsvc_x *tmp = NULL;
1537                 while(appsvc != NULL) {
1538                         tmp = appsvc->next;
1539                         __ps_free_appsvc(appsvc);
1540                         appsvc = tmp;
1541                 }
1542         }
1543         /*Free Category*/
1544         if (uiapplication->category) {
1545                 category_x *category = uiapplication->category;
1546                 category_x *tmp = NULL;
1547                 while(category != NULL) {
1548                         tmp = category->next;
1549                         __ps_free_category(category);
1550                         category = tmp;
1551                 }
1552         }
1553         /*Free Metadata*/
1554         if (uiapplication->metadata) {
1555                 metadata_x *metadata = uiapplication->metadata;
1556                 metadata_x *tmp = NULL;
1557                 while(metadata != NULL) {
1558                         tmp = metadata->next;
1559                         __ps_free_metadata(metadata);
1560                         metadata = tmp;
1561                 }
1562         }
1563         /*Free permission*/
1564         if (uiapplication->permission) {
1565                 permission_x *permission = uiapplication->permission;
1566                 permission_x *tmp = NULL;
1567                 while(permission != NULL) {
1568                         tmp = permission->next;
1569                         __ps_free_permission(permission);
1570                         permission = tmp;
1571                 }
1572         }
1573         /* _PRODUCT_LAUNCHING_ENHANCED_ START */
1574         if (uiapplication->indicatordisplay) {
1575                 free((void *)uiapplication->indicatordisplay);
1576                 uiapplication->indicatordisplay = NULL;
1577         }
1578         if (uiapplication->portraitimg) {
1579                 free((void *)uiapplication->portraitimg);
1580                 uiapplication->portraitimg = NULL;
1581         }
1582         if (uiapplication->landscapeimg) {
1583                 free((void *)uiapplication->landscapeimg);
1584                 uiapplication->landscapeimg = NULL;
1585         }
1586         /* _PRODUCT_LAUNCHING_ENHANCED_ END */
1587         if (uiapplication->guestmode_visibility) {
1588                 free((void *)uiapplication->guestmode_visibility);
1589                 uiapplication->guestmode_visibility = NULL;
1590         }
1591         if (uiapplication->app_component) {
1592                 free((void *)uiapplication->app_component);
1593                 uiapplication->app_component = NULL;
1594         }
1595         if (uiapplication->permission_type) {
1596                 free((void *)uiapplication->permission_type);
1597                 uiapplication->permission_type = NULL;
1598         }
1599         if (uiapplication->component_type) {
1600                 free((void *)uiapplication->component_type);
1601                 uiapplication->component_type = NULL;
1602         }
1603         if (uiapplication->preload) {
1604                 free((void *)uiapplication->preload);
1605                 uiapplication->preload = NULL;
1606         }
1607         if (uiapplication->submode) {
1608                 free((void *)uiapplication->submode);
1609                 uiapplication->submode = NULL;
1610         }
1611         if (uiapplication->submode_mainid) {
1612                 free((void *)uiapplication->submode_mainid);
1613                 uiapplication->submode_mainid = NULL;
1614         }
1615
1616         free((void*)uiapplication);
1617         uiapplication = NULL;
1618 }
1619
1620 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication)
1621 {
1622         if (serviceapplication == NULL)
1623                 return;
1624         if (serviceapplication->exec) {
1625                 free((void *)serviceapplication->exec);
1626                 serviceapplication->exec = NULL;
1627         }
1628         if (serviceapplication->appid) {
1629                 free((void *)serviceapplication->appid);
1630                 serviceapplication->appid = NULL;
1631         }
1632         if (serviceapplication->onboot) {
1633                 free((void *)serviceapplication->onboot);
1634                 serviceapplication->onboot = NULL;
1635         }
1636         if (serviceapplication->autorestart) {
1637                 free((void *)serviceapplication->autorestart);
1638                 serviceapplication->autorestart = NULL;
1639         }
1640         if (serviceapplication->type) {
1641                 free((void *)serviceapplication->type);
1642                 serviceapplication->type = NULL;
1643         }
1644         if (serviceapplication->enabled) {
1645                 free((void *)serviceapplication->enabled);
1646                 serviceapplication->enabled = NULL;
1647         }
1648         if (serviceapplication->package) {
1649                 free((void *)serviceapplication->package);
1650                 serviceapplication->package = NULL;
1651         }
1652         if (serviceapplication->permission_type) {
1653                 free((void *)serviceapplication->permission_type);
1654                 serviceapplication->permission_type = NULL;
1655         }
1656         /*Free Label*/
1657         if (serviceapplication->label) {
1658                 label_x *label = serviceapplication->label;
1659                 label_x *tmp = NULL;
1660                 while(label != NULL) {
1661                         tmp = label->next;
1662                         __ps_free_label(label);
1663                         label = tmp;
1664                 }
1665         }
1666         /*Free Icon*/
1667         if (serviceapplication->icon) {
1668                 icon_x *icon = serviceapplication->icon;
1669                 icon_x *tmp = NULL;
1670                 while(icon != NULL) {
1671                         tmp = icon->next;
1672                         __ps_free_icon(icon);
1673                         icon = tmp;
1674                 }
1675         }
1676         /*Free AppControl*/
1677         if (serviceapplication->appcontrol) {
1678                 appcontrol_x *appcontrol = serviceapplication->appcontrol;
1679                 appcontrol_x *tmp = NULL;
1680                 while(appcontrol != NULL) {
1681                         tmp = appcontrol->next;
1682                         __ps_free_appcontrol(appcontrol);
1683                         appcontrol = tmp;
1684                 }
1685         }
1686         /*Free DataControl*/
1687         if (serviceapplication->datacontrol) {
1688                 datacontrol_x *datacontrol = serviceapplication->datacontrol;
1689                 datacontrol_x *tmp = NULL;
1690                 while(datacontrol != NULL) {
1691                         tmp = datacontrol->next;
1692                         __ps_free_datacontrol(datacontrol);
1693                         datacontrol = tmp;
1694                 }
1695         }
1696         /*Free LaunchConditions*/
1697         if (serviceapplication->launchconditions) {
1698                 launchconditions_x *launchconditions = serviceapplication->launchconditions;
1699                 launchconditions_x *tmp = NULL;
1700                 while(launchconditions != NULL) {
1701                         tmp = launchconditions->next;
1702                         __ps_free_launchconditions(launchconditions);
1703                         launchconditions = tmp;
1704                 }
1705         }
1706         /*Free Notification*/
1707         if (serviceapplication->notification) {
1708                 notification_x *notification = serviceapplication->notification;
1709                 notification_x *tmp = NULL;
1710                 while(notification != NULL) {
1711                         tmp = notification->next;
1712                         __ps_free_notification(notification);
1713                         notification = tmp;
1714                 }
1715         }
1716         /*Free DataShare*/
1717         if (serviceapplication->datashare) {
1718                 datashare_x *datashare = serviceapplication->datashare;
1719                 datashare_x *tmp = NULL;
1720                 while(datashare != NULL) {
1721                         tmp = datashare->next;
1722                         __ps_free_datashare(datashare);
1723                         datashare = tmp;
1724                 }
1725         }
1726         /*Free AppSvc*/
1727         if (serviceapplication->appsvc) {
1728                 appsvc_x *appsvc = serviceapplication->appsvc;
1729                 appsvc_x *tmp = NULL;
1730                 while(appsvc != NULL) {
1731                         tmp = appsvc->next;
1732                         __ps_free_appsvc(appsvc);
1733                         appsvc = tmp;
1734                 }
1735         }
1736         /*Free Category*/
1737         if (serviceapplication->category) {
1738                 category_x *category = serviceapplication->category;
1739                 category_x *tmp = NULL;
1740                 while(category != NULL) {
1741                         tmp = category->next;
1742                         __ps_free_category(category);
1743                         category = tmp;
1744                 }
1745         }
1746         /*Free Metadata*/
1747         if (serviceapplication->metadata) {
1748                 metadata_x *metadata = serviceapplication->metadata;
1749                 metadata_x *tmp = NULL;
1750                 while(metadata != NULL) {
1751                         tmp = metadata->next;
1752                         __ps_free_metadata(metadata);
1753                         metadata = tmp;
1754                 }
1755         }
1756         /*Free permission*/
1757         if (serviceapplication->permission) {
1758                 permission_x *permission = serviceapplication->permission;
1759                 permission_x *tmp = NULL;
1760                 while(permission != NULL) {
1761                         tmp = permission->next;
1762                         __ps_free_permission(permission);
1763                         permission = tmp;
1764                 }
1765         }
1766         free((void*)serviceapplication);
1767         serviceapplication = NULL;
1768 }
1769
1770 static void __ps_free_font(font_x *font)
1771 {
1772         if (font == NULL)
1773                 return;
1774         if (font->name) {
1775                 free((void *)font->name);
1776                 font->name = NULL;
1777         }
1778         if (font->text) {
1779                 free((void *)font->text);
1780                 font->text = NULL;
1781         }
1782         free((void*)font);
1783         font = NULL;
1784 }
1785
1786 static void __ps_free_theme(theme_x *theme)
1787 {
1788         if (theme == NULL)
1789                 return;
1790         if (theme->name) {
1791                 free((void *)theme->name);
1792                 theme->name = NULL;
1793         }
1794         if (theme->text) {
1795                 free((void *)theme->text);
1796                 theme->text = NULL;
1797         }
1798         free((void*)theme);
1799         theme = NULL;
1800 }
1801
1802 static void __ps_free_daemon(daemon_x *daemon)
1803 {
1804         if (daemon == NULL)
1805                 return;
1806         if (daemon->name) {
1807                 free((void *)daemon->name);
1808                 daemon->name = NULL;
1809         }
1810         if (daemon->text) {
1811                 free((void *)daemon->text);
1812                 daemon->text = NULL;
1813         }
1814         free((void*)daemon);
1815         daemon = NULL;
1816 }
1817
1818 static void __ps_free_ime(ime_x *ime)
1819 {
1820         if (ime == NULL)
1821                 return;
1822         if (ime->name) {
1823                 free((void *)ime->name);
1824                 ime->name = NULL;
1825         }
1826         if (ime->text) {
1827                 free((void *)ime->text);
1828                 ime->text = NULL;
1829         }
1830         free((void*)ime);
1831         ime = NULL;
1832 }
1833
1834 int __ps_process_mdparser(manifest_x *mfx, ACTION_TYPE action)
1835 {
1836         int ret = -1;
1837         FILE *fp = NULL;
1838         char md_key[PKG_STRING_LEN_MAX] = { 0 };
1839
1840         fp = fopen(MDPARSER_LIST, "r");
1841         if (fp == NULL) {
1842                 DBG("no preload list\n");
1843                 return -1;
1844         }
1845
1846         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
1847                 __str_trim(md_key);
1848                 DBG("md_key = %s\n", md_key);
1849
1850                 ret = __run_mdparser_prestep(mfx, md_key, action);
1851
1852                 memset(md_key, 0x00, sizeof(md_key));
1853         }
1854
1855         if (fp != NULL)
1856                 fclose(fp);
1857
1858         return 0;
1859 }
1860
1861 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
1862 {
1863         xmlTextReaderRead(reader);
1864         if (xmlTextReaderValue(reader))
1865                 allowed->text = ASCII(xmlTextReaderValue(reader));
1866         return 0;
1867 }
1868
1869 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
1870 {
1871         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1872                 operation->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1873 /* Text does not exist. Only attribute exists
1874         xmlTextReaderRead(reader);
1875         if (xmlTextReaderValue(reader))
1876                 operation->text = ASCII(xmlTextReaderValue(reader));
1877 */
1878         return 0;
1879 }
1880
1881 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
1882 {
1883         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1884                 uri->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1885 /* Text does not exist. Only attribute exists
1886         xmlTextReaderRead(reader);
1887         if (xmlTextReaderValue(reader))
1888                 uri->text = ASCII(xmlTextReaderValue(reader));
1889 */
1890         return 0;
1891 }
1892
1893 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
1894 {
1895         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1896                 mime->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1897 /* Text does not exist. Only attribute exists
1898         xmlTextReaderRead(reader);
1899         if (xmlTextReaderValue(reader))
1900                 mime->text = ASCII(xmlTextReaderValue(reader));
1901 */
1902         return 0;
1903 }
1904
1905 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
1906 {
1907         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1908                 subapp->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1909 /* Text does not exist. Only attribute exists
1910         xmlTextReaderRead(reader);
1911         if (xmlTextReaderValue(reader))
1912                 mime->text = ASCII(xmlTextReaderValue(reader));
1913 */
1914         return 0;
1915 }
1916
1917 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
1918 {
1919         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1920                 condition->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1921         xmlTextReaderRead(reader);
1922         if (xmlTextReaderValue(reader))
1923                 condition->text = ASCII(xmlTextReaderValue(reader));
1924         return 0;
1925 }
1926
1927 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
1928 {
1929         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1930                 notification->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1931         xmlTextReaderRead(reader);
1932         if (xmlTextReaderValue(reader))
1933                 notification->text = ASCII(xmlTextReaderValue(reader));
1934         return 0;
1935 }
1936
1937 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
1938 {
1939         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1940                 category->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1941         return 0;
1942 }
1943
1944 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
1945 {
1946         xmlTextReaderRead(reader);
1947         if (xmlTextReaderValue(reader)) {
1948                 privilege->text = ASCII(xmlTextReaderValue(reader));
1949         }
1950         return 0;
1951 }
1952
1953 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
1954 {
1955         if (xmlTextReaderGetAttribute(reader, XMLCHAR("key")))
1956                 metadata->key = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("key")));
1957         if (xmlTextReaderGetAttribute(reader, XMLCHAR("value")))
1958                 metadata->value = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("value")));
1959         return 0;
1960 }
1961
1962 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
1963 {
1964         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
1965                 permission->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
1966
1967         xmlTextReaderRead(reader);
1968         if (xmlTextReaderValue(reader))
1969                 permission->value = ASCII(xmlTextReaderValue(reader));
1970         return 0;
1971 }
1972
1973 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
1974 {
1975         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
1976                 compatibility->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
1977         xmlTextReaderRead(reader);
1978         if (xmlTextReaderValue(reader))
1979                 compatibility->text = ASCII(xmlTextReaderValue(reader));
1980         return 0;
1981 }
1982
1983 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution)
1984 {
1985         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")))
1986                 resolution->mimetype = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")));
1987         if (xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")))
1988                 resolution->urischeme = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")));
1989         return 0;
1990 }
1991
1992 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
1993 {
1994         xmlTextReaderRead(reader);
1995         if (xmlTextReaderValue(reader))
1996                 request->text = ASCII(xmlTextReaderValue(reader));
1997         return 0;
1998 }
1999
2000 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
2001 {
2002         const xmlChar *node;
2003         int ret = -1;
2004         int depth = -1;
2005         allowed_x *tmp1 = NULL;
2006         request_x *tmp2 = NULL;
2007
2008         if (xmlTextReaderGetAttribute(reader, XMLCHAR("path")))
2009                 define->path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("path")));
2010
2011         depth = xmlTextReaderDepth(reader);
2012         while ((ret = __next_child_element(reader, depth))) {
2013                 node = xmlTextReaderConstName(reader);
2014                 if (!node) {
2015                         DBG("xmlTextReaderConstName value is NULL\n");
2016                         return -1;
2017                 }
2018
2019                 if (!strcmp(ASCII(node), "allowed")) {
2020                         allowed_x *allowed= malloc(sizeof(allowed_x));
2021                         if (allowed == NULL) {
2022                                 DBG("Malloc Failed\n");
2023                                 return -1;
2024                         }
2025                         memset(allowed, '\0', sizeof(allowed_x));
2026                         LISTADD(define->allowed, allowed);
2027                         ret = __ps_process_allowed(reader, allowed);
2028                 } else if (!strcmp(ASCII(node), "request")) {
2029                         request_x *request = malloc(sizeof(request_x));
2030                         if (request == NULL) {
2031                                 DBG("Malloc Failed\n");
2032                                 return -1;
2033                         }
2034                         memset(request, '\0', sizeof(request_x));
2035                         LISTADD(define->request, request);
2036                         ret = __ps_process_request(reader, request);
2037                 } else
2038                         return -1;
2039                 if (ret < 0) {
2040                         DBG("Processing define failed\n");
2041                         return ret;
2042                 }
2043         }
2044         if (define->allowed) {
2045                 LISTHEAD(define->allowed, tmp1);
2046                 define->allowed = tmp1;
2047         }
2048         if (define->request) {
2049                 LISTHEAD(define->request, tmp2);
2050                 define->request = tmp2;
2051         }
2052         return ret;
2053 }
2054
2055 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol)
2056 {
2057         const xmlChar *node;
2058         int ret = -1;
2059         int depth = -1;
2060         operation_x *tmp1 = NULL;
2061         uri_x *tmp2 = NULL;
2062         mime_x *tmp3 = NULL;
2063         subapp_x *tmp4 = NULL;
2064
2065         depth = xmlTextReaderDepth(reader);
2066         while ((ret = __next_child_element(reader, depth))) {
2067                 node = xmlTextReaderConstName(reader);
2068                 if (!node) {
2069                         DBG("xmlTextReaderConstName value is NULL\n");
2070                         return -1;
2071                 }
2072
2073                 if (!strcmp(ASCII(node), "operation")) {
2074                         operation_x *operation = malloc(sizeof(operation_x));
2075                         if (operation == NULL) {
2076                                 DBG("Malloc Failed\n");
2077                                 return -1;
2078                         }
2079                         memset(operation, '\0', sizeof(operation_x));
2080                         LISTADD(appcontrol->operation, operation);
2081                         ret = __ps_process_operation(reader, operation);
2082                         DBG("operation processing\n");
2083                 } else if (!strcmp(ASCII(node), "uri")) {
2084                         uri_x *uri= malloc(sizeof(uri_x));
2085                         if (uri == NULL) {
2086                                 DBG("Malloc Failed\n");
2087                                 return -1;
2088                         }
2089                         memset(uri, '\0', sizeof(uri_x));
2090                         LISTADD(appcontrol->uri, uri);
2091                         ret = __ps_process_uri(reader, uri);
2092                         DBG("uri processing\n");
2093                 } else if (!strcmp(ASCII(node), "mime")) {
2094                         mime_x *mime = malloc(sizeof(mime_x));
2095                         if (mime == NULL) {
2096                                 DBG("Malloc Failed\n");
2097                                 return -1;
2098                         }
2099                         memset(mime, '\0', sizeof(mime_x));
2100                         LISTADD(appcontrol->mime, mime);
2101                         ret = __ps_process_mime(reader, mime);
2102                         DBG("mime processing\n");
2103                 } else if (!strcmp(ASCII(node), "subapp")) {
2104                         subapp_x *subapp = malloc(sizeof(subapp_x));
2105                         if (subapp == NULL) {
2106                                 DBG("Malloc Failed\n");
2107                                 return -1;
2108                         }
2109                         memset(subapp, '\0', sizeof(subapp_x));
2110                         LISTADD(appcontrol->subapp, subapp);
2111                         ret = __ps_process_subapp(reader, subapp);
2112                         DBG("subapp processing\n");
2113                 } else
2114                         return -1;
2115                 if (ret < 0) {
2116                         DBG("Processing appcontrol failed\n");
2117                         return ret;
2118                 }
2119         }
2120         if (appcontrol->operation) {
2121                 LISTHEAD(appcontrol->operation, tmp1);
2122                 appcontrol->operation = tmp1;
2123         }
2124         if (appcontrol->uri) {
2125                 LISTHEAD(appcontrol->uri, tmp2);
2126                 appcontrol->uri = tmp2;
2127         }
2128         if (appcontrol->mime) {
2129                 LISTHEAD(appcontrol->mime, tmp3);
2130                 appcontrol->mime = tmp3;
2131         }
2132         if (appcontrol->subapp) {
2133                 LISTHEAD(appcontrol->subapp, tmp4);
2134                 appcontrol->subapp = tmp4;
2135         }
2136
2137         xmlTextReaderRead(reader);
2138         if (xmlTextReaderValue(reader))
2139                 appcontrol->text = ASCII(xmlTextReaderValue(reader));
2140
2141         return ret;
2142 }
2143
2144 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
2145 {
2146         const xmlChar *node;
2147         int ret = -1;
2148         int depth = -1;
2149         operation_x *tmp1 = NULL;
2150         uri_x *tmp2 = NULL;
2151         mime_x *tmp3 = NULL;
2152         subapp_x *tmp4 = NULL;
2153
2154         depth = xmlTextReaderDepth(reader);
2155         while ((ret = __next_child_element(reader, depth))) {
2156                 node = xmlTextReaderConstName(reader);
2157                 if (!node) {
2158                         DBG("xmlTextReaderConstName value is NULL\n");
2159                         return -1;
2160                 }
2161
2162                 if (!strcmp(ASCII(node), "operation")) {
2163                         operation_x *operation = malloc(sizeof(operation_x));
2164                         if (operation == NULL) {
2165                                 DBG("Malloc Failed\n");
2166                                 return -1;
2167                         }
2168                         memset(operation, '\0', sizeof(operation_x));
2169                         LISTADD(appsvc->operation, operation);
2170                         ret = __ps_process_operation(reader, operation);
2171                         DBG("operation processing\n");
2172                 } else if (!strcmp(ASCII(node), "uri")) {
2173                         uri_x *uri= malloc(sizeof(uri_x));
2174                         if (uri == NULL) {
2175                                 DBG("Malloc Failed\n");
2176                                 return -1;
2177                         }
2178                         memset(uri, '\0', sizeof(uri_x));
2179                         LISTADD(appsvc->uri, uri);
2180                         ret = __ps_process_uri(reader, uri);
2181                         DBG("uri processing\n");
2182                 } else if (!strcmp(ASCII(node), "mime")) {
2183                         mime_x *mime = malloc(sizeof(mime_x));
2184                         if (mime == NULL) {
2185                                 DBG("Malloc Failed\n");
2186                                 return -1;
2187                         }
2188                         memset(mime, '\0', sizeof(mime_x));
2189                         LISTADD(appsvc->mime, mime);
2190                         ret = __ps_process_mime(reader, mime);
2191                         DBG("mime processing\n");
2192                 } else if (!strcmp(ASCII(node), "subapp")) {
2193                         subapp_x *subapp = malloc(sizeof(subapp_x));
2194                         if (subapp == NULL) {
2195                                 DBG("Malloc Failed\n");
2196                                 return -1;
2197                         }
2198                         memset(subapp, '\0', sizeof(subapp_x));
2199                         LISTADD(appsvc->subapp, subapp);
2200                         ret = __ps_process_subapp(reader, subapp);
2201                         DBG("subapp processing\n");
2202                 } else
2203                         return -1;
2204                 if (ret < 0) {
2205                         DBG("Processing appsvc failed\n");
2206                         return ret;
2207                 }
2208         }
2209         if (appsvc->operation) {
2210                 LISTHEAD(appsvc->operation, tmp1);
2211                 appsvc->operation = tmp1;
2212         }
2213         if (appsvc->uri) {
2214                 LISTHEAD(appsvc->uri, tmp2);
2215                 appsvc->uri = tmp2;
2216         }
2217         if (appsvc->mime) {
2218                 LISTHEAD(appsvc->mime, tmp3);
2219                 appsvc->mime = tmp3;
2220         }
2221         if (appsvc->subapp) {
2222                 LISTHEAD(appsvc->subapp, tmp4);
2223                 appsvc->subapp = tmp4;
2224         }
2225
2226         xmlTextReaderRead(reader);
2227         if (xmlTextReaderValue(reader))
2228                 appsvc->text = ASCII(xmlTextReaderValue(reader));
2229
2230         return ret;
2231 }
2232
2233
2234 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges)
2235 {
2236         const xmlChar *node;
2237         int ret = -1;
2238         int depth = -1;
2239         privilege_x *tmp1 = NULL;
2240
2241         depth = xmlTextReaderDepth(reader);
2242         while ((ret = __next_child_element(reader, depth))) {
2243                 node = xmlTextReaderConstName(reader);
2244                 if (!node) {
2245                         DBG("xmlTextReaderConstName value is NULL\n");
2246                         return -1;
2247                 }
2248
2249                 if (strcmp(ASCII(node), "privilege") == 0) {
2250                         privilege_x *privilege = malloc(sizeof(privilege_x));
2251                         if (privilege == NULL) {
2252                                 DBG("Malloc Failed\n");
2253                                 return -1;
2254                         }
2255                         memset(privilege, '\0', sizeof(privilege_x));
2256                         LISTADD(privileges->privilege, privilege);
2257                         ret = __ps_process_privilege(reader, privilege);
2258                 } else
2259                         return -1;
2260                 if (ret < 0) {
2261                         DBG("Processing privileges failed\n");
2262                         return ret;
2263                 }
2264         }
2265         if (privileges->privilege) {
2266                 LISTHEAD(privileges->privilege, tmp1);
2267                 privileges->privilege = tmp1;
2268         }
2269         return ret;
2270 }
2271
2272 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
2273 {
2274         const xmlChar *node;
2275         int ret = -1;
2276         int depth = -1;
2277         condition_x *tmp1 = NULL;
2278
2279         depth = xmlTextReaderDepth(reader);
2280         while ((ret = __next_child_element(reader, depth))) {
2281                 node = xmlTextReaderConstName(reader);
2282                 if (!node) {
2283                         DBG("xmlTextReaderConstName value is NULL\n");
2284                         return -1;
2285                 }
2286
2287                 if (strcmp(ASCII(node), "condition") == 0) {
2288                         condition_x *condition = malloc(sizeof(condition_x));
2289                         if (condition == NULL) {
2290                                 DBG("Malloc Failed\n");
2291                                 return -1;
2292                         }
2293                         memset(condition, '\0', sizeof(condition_x));
2294                         LISTADD(launchconditions->condition, condition);
2295                         ret = __ps_process_condition(reader, condition);
2296                 } else
2297                         return -1;
2298                 if (ret < 0) {
2299                         DBG("Processing launchconditions failed\n");
2300                         return ret;
2301                 }
2302         }
2303         if (launchconditions->condition) {
2304                 LISTHEAD(launchconditions->condition, tmp1);
2305                 launchconditions->condition = tmp1;
2306         }
2307
2308         xmlTextReaderRead(reader);
2309         if (xmlTextReaderValue(reader))
2310                 launchconditions->text = ASCII(xmlTextReaderValue(reader));
2311
2312         return ret;
2313 }
2314
2315 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
2316 {
2317         const xmlChar *node;
2318         int ret = -1;
2319         int depth = -1;
2320         define_x *tmp1 = NULL;
2321         request_x *tmp2 = NULL;
2322         depth = xmlTextReaderDepth(reader);
2323         while ((ret = __next_child_element(reader, depth))) {
2324                 node = xmlTextReaderConstName(reader);
2325                 if (!node) {
2326                         DBG("xmlTextReaderConstName value is NULL\n");
2327                         return -1;
2328                 }
2329
2330                 if (!strcmp(ASCII(node), "define")) {
2331                         define_x *define= malloc(sizeof(define_x));
2332                         if (define == NULL) {
2333                                 DBG("Malloc Failed\n");
2334                                 return -1;
2335                         }
2336                         memset(define, '\0', sizeof(define_x));
2337                         LISTADD(datashare->define, define);
2338                         ret = __ps_process_define(reader, define);
2339                 } else if (!strcmp(ASCII(node), "request")) {
2340                         request_x *request= malloc(sizeof(request_x));
2341                         if (request == NULL) {
2342                                 DBG("Malloc Failed\n");
2343                                 return -1;
2344                         }
2345                         memset(request, '\0', sizeof(request_x));
2346                         LISTADD(datashare->request, request);
2347                         ret = __ps_process_request(reader, request);
2348                 } else
2349                         return -1;
2350                 if (ret < 0) {
2351                         DBG("Processing data-share failed\n");
2352                         return ret;
2353                 }
2354         }
2355         if (datashare->define) {
2356                 LISTHEAD(datashare->define, tmp1);
2357                 datashare->define = tmp1;
2358         }
2359         if (datashare->request) {
2360                 LISTHEAD(datashare->request, tmp2);
2361                 datashare->request = tmp2;
2362         }
2363         return ret;
2364 }
2365
2366 static char*
2367 __get_icon_with_path(const char* icon)
2368 {
2369         if (!icon)
2370                 return NULL;
2371
2372         if (index(icon, '/') == NULL) {
2373                 char* theme = NULL;
2374                 char* icon_with_path = NULL;
2375                 int len;
2376
2377                 if (!package)
2378                         return NULL;
2379
2380                 theme = vconf_get_str("db/setting/theme");
2381                 if (!theme) {
2382                         theme = strdup("default");
2383                         if(!theme) {
2384                                 return NULL;
2385                         }
2386                 }
2387
2388                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
2389                 icon_with_path = malloc(len);
2390                 if(icon_with_path == NULL) {
2391                         DBG("(icon_with_path == NULL) return\n");
2392                         free(theme);
2393                         return NULL;
2394                 }
2395
2396                 memset(icon_with_path, 0, len);
2397
2398                 snprintf(icon_with_path, len, "/opt/share/icons/%s/small/%s", theme, icon);
2399                 do {
2400                         if (access(icon_with_path, R_OK) == 0) break;
2401                         snprintf(icon_with_path, len, "/usr/share/icons/%s/small/%s", theme, icon);
2402                         if (access(icon_with_path, R_OK) == 0) break;
2403                         DBG("cannot find icon %s", icon_with_path);
2404                         snprintf(icon_with_path, len,"/opt/share/icons/default/small/%s", icon);
2405                         if (access(icon_with_path, R_OK) == 0) break;
2406                         snprintf(icon_with_path, len, "/usr/share/icons/default/small/%s", icon);
2407                         if (access(icon_with_path, R_OK) == 0) break;
2408
2409                         /* icon path is going to be moved intto the app directory */
2410                         DBGE("icon file must be moved to %s", icon_with_path);
2411                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/%s/small/%s", package, theme, icon);
2412                         if (access(icon_with_path, R_OK) == 0) break;
2413                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/%s/small/%s", package, theme, icon);
2414                         if (access(icon_with_path, R_OK) == 0) break;
2415                         DBG("cannot find icon %s", icon_with_path);
2416                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/default/small/%s", package, icon);
2417                         if (access(icon_with_path, R_OK) == 0) break;
2418                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/default/small/%s", package, icon);
2419                         if (access(icon_with_path, R_OK) == 0) break;
2420                         snprintf(icon_with_path, len, "/usr/ug/res/images/%s/%s", package, icon);
2421                         if (access(icon_with_path, R_OK) == 0) break;
2422                 } while (0);
2423
2424                 free(theme);
2425
2426                 DBG("Icon path : %s ---> %s", icon, icon_with_path);
2427
2428                 return icon_with_path;
2429         } else {
2430                 char* confirmed_icon = NULL;
2431
2432                 confirmed_icon = strdup(icon);
2433                 if (!confirmed_icon)
2434                         return NULL;
2435                 return confirmed_icon;
2436         }
2437 }
2438
2439 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
2440 {
2441         int i = 0;
2442         char delims[] = "=";
2443         char *ret_result = NULL;
2444         char *tag = NULL;
2445
2446         if (tagv == NULL)
2447                 return;
2448
2449         for (tag = strdup(tagv[0]); tag != NULL; ) {
2450                 ret_result = strtok(tag, delims);
2451
2452                 /*check tag :  preload */
2453                 if (strcmp(ret_result, "preload") == 0) {
2454                         ret_result = strtok(NULL, delims);
2455                         if (strcmp(ret_result, "true") == 0) {
2456                                 free((void *)mfx->preload);
2457                                 mfx->preload = strdup("true");
2458                         } else if (strcmp(ret_result, "false") == 0) {
2459                                 free((void *)mfx->preload);
2460                                 mfx->preload = strdup("false");
2461                         }
2462                 /*check tag :  removable*/
2463                 } else if (strcmp(ret_result, "removable") == 0) {
2464                         ret_result = strtok(NULL, delims);
2465                         if (strcmp(ret_result, "true") == 0){
2466                                 free((void *)mfx->removable);
2467                                 mfx->removable = strdup("true");
2468                         } else if (strcmp(ret_result, "false") == 0) {
2469                                 free((void *)mfx->removable);
2470                                 mfx->removable = strdup("false");
2471                         }
2472                 /*check tag :  not matched*/
2473                 } else
2474                         DBG("tag process [%s]is not defined\n", ret_result);
2475
2476                 free(tag);
2477
2478                 /*check next value*/
2479                 if (tagv[++i] != NULL)
2480                         tag = strdup(tagv[i]);
2481                 else {
2482                         DBG("tag process success...\n");
2483                         return;
2484                 }
2485         }
2486 }
2487
2488 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon)
2489 {
2490         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2491                 icon->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2492         if (xmlTextReaderConstXmlLang(reader)) {
2493                 icon->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2494                 if (icon->lang == NULL)
2495                         icon->lang = strdup(DEFAULT_LOCALE);
2496         } else {
2497                 icon->lang = strdup(DEFAULT_LOCALE);
2498         }
2499         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
2500                 icon->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
2501         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
2502                 icon->size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
2503         if (xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")))
2504                 icon->resolution = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")));
2505         xmlTextReaderRead(reader);
2506         if (xmlTextReaderValue(reader)) {
2507                 const char *text  = ASCII(xmlTextReaderValue(reader));
2508                 if(text) {
2509                         icon->text = (const char *)__get_icon_with_path(text);
2510                         free((void *)text);
2511                 }
2512         }
2513
2514         return 0;
2515 }
2516
2517 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
2518 {
2519         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2520                 image->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2521         if (xmlTextReaderConstXmlLang(reader)) {
2522                 image->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2523                 if (image->lang == NULL)
2524                         image->lang = strdup(DEFAULT_LOCALE);
2525         } else {
2526                 image->lang = strdup(DEFAULT_LOCALE);
2527         }
2528         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
2529                 image->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
2530         xmlTextReaderRead(reader);
2531         if (xmlTextReaderValue(reader))
2532                 image->text = ASCII(xmlTextReaderValue(reader));
2533
2534         return 0;
2535 }
2536
2537 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
2538 {
2539         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2540                 label->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2541         if (xmlTextReaderConstXmlLang(reader)) {
2542                 label->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2543                 if (label->lang == NULL)
2544                         label->lang = strdup(DEFAULT_LOCALE);
2545         } else {
2546                 label->lang = strdup(DEFAULT_LOCALE);
2547         }
2548         xmlTextReaderRead(reader);
2549         if (xmlTextReaderValue(reader))
2550                 label->text = ASCII(xmlTextReaderValue(reader));
2551
2552 /*      DBG("lable name %s\n", label->name);
2553         DBG("lable lang %s\n", label->lang);
2554         DBG("lable text %s\n", label->text);
2555 */
2556         return 0;
2557
2558 }
2559
2560 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
2561 {
2562         if (xmlTextReaderGetAttribute(reader, XMLCHAR("email")))
2563                 author->email = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("email")));
2564         if (xmlTextReaderGetAttribute(reader, XMLCHAR("href")))
2565                 author->href = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("href")));
2566         if (xmlTextReaderConstXmlLang(reader)) {
2567                 author->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2568                 if (author->lang == NULL)
2569                         author->lang = strdup(DEFAULT_LOCALE);
2570         } else {
2571                 author->lang = strdup(DEFAULT_LOCALE);
2572         }
2573         xmlTextReaderRead(reader);
2574         if (xmlTextReaderValue(reader))
2575                 author->text = ASCII(xmlTextReaderValue(reader));
2576         return 0;
2577 }
2578
2579 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
2580 {
2581         if (xmlTextReaderConstXmlLang(reader)) {
2582                 description->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2583                 if (description->lang == NULL)
2584                         description->lang = strdup(DEFAULT_LOCALE);
2585         } else {
2586                 description->lang = strdup(DEFAULT_LOCALE);
2587         }
2588         xmlTextReaderRead(reader);
2589         if (xmlTextReaderValue(reader))
2590                 description->text = ASCII(xmlTextReaderValue(reader));
2591         return 0;
2592 }
2593
2594 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
2595 {
2596         if (xmlTextReaderConstXmlLang(reader)) {
2597                 license->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
2598                 if (license->lang == NULL)
2599                         license->lang = strdup(DEFAULT_LOCALE);
2600         } else {
2601                 license->lang = strdup(DEFAULT_LOCALE);
2602         }
2603         xmlTextReaderRead(reader);
2604         if (xmlTextReaderValue(reader))
2605                 license->text = ASCII(xmlTextReaderValue(reader));
2606         return 0;
2607 }
2608
2609 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability)
2610 {
2611         const xmlChar *node;
2612         int ret = -1;
2613         int depth = -1;
2614         resolution_x *tmp1 = NULL;
2615
2616         if (xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")))
2617                 capability->operationid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")));
2618
2619         depth = xmlTextReaderDepth(reader);
2620         while ((ret = __next_child_element(reader, depth))) {
2621                 node = xmlTextReaderConstName(reader);
2622                 if (!node) {
2623                         DBG("xmlTextReaderConstName value is NULL\n");
2624                         return -1;
2625                 }
2626
2627                 if (!strcmp(ASCII(node), "resolution")) {
2628                         resolution_x *resolution = malloc(sizeof(resolution_x));
2629                         if (resolution == NULL) {
2630                                 DBG("Malloc Failed\n");
2631                                 return -1;
2632                         }
2633                         memset(resolution, '\0', sizeof(resolution_x));
2634                         LISTADD(capability->resolution, resolution);
2635                         ret = __ps_process_resolution(reader, resolution);
2636                 } else
2637                         return -1;
2638                 if (ret < 0) {
2639                         DBG("Processing capability failed\n");
2640                         return ret;
2641                 }
2642         }
2643
2644         if (capability->resolution) {
2645                 LISTHEAD(capability->resolution, tmp1);
2646                 capability->resolution = tmp1;
2647         }
2648
2649         return ret;
2650 }
2651
2652 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
2653 {
2654         const xmlChar *node;
2655         int ret = -1;
2656         int depth = -1;
2657         capability_x *tmp1 = NULL;
2658
2659         if (xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")))
2660                 datacontrol->providerid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")));
2661
2662         depth = xmlTextReaderDepth(reader);
2663         while ((ret = __next_child_element(reader, depth))) {
2664                 node = xmlTextReaderConstName(reader);
2665                 if (!node) {
2666                         DBG("xmlTextReaderConstName value is NULL\n");
2667                         return -1;
2668                 }
2669
2670                 if (!strcmp(ASCII(node), "capability")) {
2671                         capability_x *capability = malloc(sizeof(capability_x));
2672                         if (capability == NULL) {
2673                                 DBG("Malloc Failed\n");
2674                                 return -1;
2675                         }
2676                         memset(capability, '\0', sizeof(capability_x));
2677                         LISTADD(datacontrol->capability, capability);
2678                         ret = __ps_process_capability(reader, capability);
2679                 } else
2680                         return -1;
2681                 if (ret < 0) {
2682                         DBG("Processing datacontrol failed\n");
2683                         return ret;
2684                 }
2685         }
2686
2687         if (datacontrol->capability) {
2688                 LISTHEAD(datacontrol->capability, tmp1);
2689                 datacontrol->capability = tmp1;
2690         }
2691
2692         return ret;
2693 }
2694
2695 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication)
2696 {
2697         const xmlChar *node;
2698         int ret = -1;
2699         int depth = -1;
2700         char *newappid = NULL;
2701         label_x *tmp1 = NULL;
2702         icon_x *tmp2 = NULL;
2703         appsvc_x *tmp3 = NULL;
2704         appcontrol_x *tmp4 = NULL;
2705         launchconditions_x *tmp5 = NULL;
2706         notification_x *tmp6 = NULL;
2707         datashare_x *tmp7 = NULL;
2708         category_x *tmp8 = NULL;
2709         metadata_x *tmp9 = NULL;
2710         image_x *tmp10 = NULL;
2711         permission_x *tmp11 = NULL;
2712
2713         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
2714                 uiapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
2715                 if (uiapplication->appid == NULL) {
2716                         DBG("appid cant be NULL\n");
2717                         return -1;
2718                 }
2719         } else {
2720                 DBG("appid is mandatory\n");
2721                 return -1;
2722         }
2723         /*check appid*/
2724         ret = __validate_appid(package, uiapplication->appid, &newappid);
2725         if (ret == -1) {
2726                 DBG("appid is not proper\n");
2727                 return -1;
2728         } else {
2729                 if (newappid) {
2730                         if (uiapplication->appid)
2731                                 free((void *)uiapplication->appid);
2732                         uiapplication->appid = newappid;
2733                 }
2734                 uiapplication->package= strdup(package);
2735         }
2736         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
2737                 uiapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
2738         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay"))) {
2739                 uiapplication->nodisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay")));
2740                 if (uiapplication->nodisplay == NULL)
2741                         uiapplication->nodisplay = strdup("false");
2742         } else {
2743                 uiapplication->nodisplay = strdup("false");
2744         }
2745         if (xmlTextReaderGetAttribute(reader, XMLCHAR("multiple"))) {
2746                 uiapplication->multiple = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("multiple")));
2747                 if (uiapplication->multiple == NULL)
2748                         uiapplication->multiple = strdup("false");
2749         } else {
2750                 uiapplication->multiple = strdup("false");
2751         }
2752         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2753                 uiapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2754         if (xmlTextReaderGetAttribute(reader, XMLCHAR("categories")))
2755                 uiapplication->categories = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("categories")));
2756         if (xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")))
2757                 uiapplication->extraid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")));
2758         if (xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage"))) {
2759                 uiapplication->taskmanage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage")));
2760                 if (uiapplication->taskmanage == NULL)
2761                         uiapplication->taskmanage = strdup("true");
2762         } else {
2763                 uiapplication->taskmanage = strdup("true");
2764         }
2765         if (xmlTextReaderGetAttribute(reader, XMLCHAR("enabled"))) {
2766                 uiapplication->enabled = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("enabled")));
2767                 if (uiapplication->enabled == NULL)
2768                         uiapplication->enabled = strdup("true");
2769         } else {
2770                 uiapplication->enabled = strdup("true");
2771         }
2772         if (xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration"))) {
2773                 uiapplication->hwacceleration = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration")));
2774                 if (uiapplication->hwacceleration == NULL)
2775                         uiapplication->hwacceleration = strdup("use-system-setting");
2776         } else {
2777                 uiapplication->hwacceleration = strdup("use-system-setting");
2778         }
2779         if (xmlTextReaderGetAttribute(reader, XMLCHAR("screen-reader"))) {
2780                 uiapplication->screenreader = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("screen-reader")));
2781                 if (uiapplication->screenreader == NULL)
2782                         uiapplication->screenreader = strdup("use-system-setting");
2783         } else {
2784                 uiapplication->screenreader = strdup("use-system-setting");
2785         }
2786         if (xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")))
2787                 uiapplication->recentimage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")));
2788         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp"))) {
2789                 uiapplication->mainapp = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp")));
2790                 if (uiapplication->mainapp == NULL)
2791                         uiapplication->mainapp = strdup("false");
2792         } else {
2793                 uiapplication->mainapp = strdup("false");
2794         }
2795         if (xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition"))) {
2796                 uiapplication->launchcondition = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition")));
2797                 if (uiapplication->launchcondition == NULL)
2798                         uiapplication->launchcondition = strdup("false");
2799         } else {
2800                 uiapplication->launchcondition = strdup("false");
2801         }
2802
2803         if (xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay"))) {
2804                 uiapplication->indicatordisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay")));
2805                 if (uiapplication->indicatordisplay == NULL)
2806                         uiapplication->indicatordisplay = strdup("true");
2807         } else {
2808                 uiapplication->indicatordisplay = strdup("true");
2809         }
2810         if (xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")))
2811                 uiapplication->portraitimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")));
2812         else
2813                 uiapplication->portraitimg = NULL;
2814         if (xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")))
2815                 uiapplication->landscapeimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")));
2816         else
2817                 uiapplication->landscapeimg = NULL;
2818         if (xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility"))) {
2819                 uiapplication->guestmode_visibility = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility")));
2820                 if (uiapplication->guestmode_visibility == NULL)
2821                         uiapplication->guestmode_visibility = strdup("true");
2822         } else {
2823                 uiapplication->guestmode_visibility = strdup("true");
2824         }
2825         if (xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type"))) {
2826                 uiapplication->permission_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type")));
2827                 if (uiapplication->permission_type == NULL)
2828                         uiapplication->permission_type = strdup("normal");
2829         } else {
2830                 uiapplication->permission_type = strdup("normal");
2831         }
2832         if (xmlTextReaderGetAttribute(reader, XMLCHAR("component-type"))) {
2833                 uiapplication->component_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("component-type")));
2834                 if (uiapplication->component_type == NULL)
2835                         uiapplication->component_type = strdup("uiapp");
2836         } else {
2837                 uiapplication->component_type = strdup("uiapp");
2838         }
2839         if (xmlTextReaderGetAttribute(reader, XMLCHAR("submode"))) {
2840                 uiapplication->submode = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("submode")));
2841                 if (uiapplication->submode == NULL)
2842                         uiapplication->submode = strdup("false");
2843         } else {
2844                 uiapplication->submode = strdup("false");
2845         }
2846         if (xmlTextReaderGetAttribute(reader, XMLCHAR("submode-mainid")))
2847                 uiapplication->submode_mainid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("submode-mainid")));
2848
2849         depth = xmlTextReaderDepth(reader);
2850         while ((ret = __next_child_element(reader, depth))) {
2851                 node = xmlTextReaderConstName(reader);
2852                 if (!node) {
2853                         DBG("xmlTextReaderConstName value is NULL\n");
2854                         return -1;
2855                 }
2856                 if (!strcmp(ASCII(node), "label")) {
2857                         label_x *label = malloc(sizeof(label_x));
2858                         if (label == NULL) {
2859                                 DBG("Malloc Failed\n");
2860                                 return -1;
2861                         }
2862                         memset(label, '\0', sizeof(label_x));
2863                         LISTADD(uiapplication->label, label);
2864                         ret = __ps_process_label(reader, label);
2865                 } else if (!strcmp(ASCII(node), "icon")) {
2866                         icon_x *icon = malloc(sizeof(icon_x));
2867                         if (icon == NULL) {
2868                                 DBG("Malloc Failed\n");
2869                                 return -1;
2870                         }
2871                         memset(icon, '\0', sizeof(icon_x));
2872                         LISTADD(uiapplication->icon, icon);
2873                         ret = __ps_process_icon(reader, icon);
2874                 } else if (!strcmp(ASCII(node), "image")) {
2875                         image_x *image = malloc(sizeof(image_x));
2876                         if (image == NULL) {
2877                                 DBG("Malloc Failed\n");
2878                                 return -1;
2879                         }
2880                         memset(image, '\0', sizeof(image_x));
2881                         LISTADD(uiapplication->image, image);
2882                         ret = __ps_process_image(reader, image);
2883                 } else if (!strcmp(ASCII(node), "category")) {
2884                         category_x *category = malloc(sizeof(category_x));
2885                         if (category == NULL) {
2886                                 DBG("Malloc Failed\n");
2887                                 return -1;
2888                         }
2889                         memset(category, '\0', sizeof(category_x));
2890                         LISTADD(uiapplication->category, category);
2891                         ret = __ps_process_category(reader, category);
2892                 } else if (!strcmp(ASCII(node), "metadata")) {
2893                         metadata_x *metadata = malloc(sizeof(metadata_x));
2894                         if (metadata == NULL) {
2895                                 DBG("Malloc Failed\n");
2896                                 return -1;
2897                         }
2898                         memset(metadata, '\0', sizeof(metadata_x));
2899                         LISTADD(uiapplication->metadata, metadata);
2900                         ret = __ps_process_metadata(reader, metadata);
2901                 } else if (!strcmp(ASCII(node), "permission")) {
2902                         permission_x *permission = malloc(sizeof(permission_x));
2903                         if (permission == NULL) {
2904                                 DBG("Malloc Failed\n");
2905                                 return -1;
2906                         }
2907                         memset(permission, '\0', sizeof(permission_x));
2908                         LISTADD(uiapplication->permission, permission);
2909                         ret = __ps_process_permission(reader, permission);
2910                 } else if (!strcmp(ASCII(node), "app-control")) {
2911                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
2912                         if (appcontrol == NULL) {
2913                                 DBG("Malloc Failed\n");
2914                                 return -1;
2915                         }
2916                         memset(appcontrol, '\0', sizeof(appcontrol_x));
2917                         LISTADD(uiapplication->appcontrol, appcontrol);
2918                         ret = __ps_process_appcontrol(reader, appcontrol);
2919                 } else if (!strcmp(ASCII(node), "application-service")) {
2920                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
2921                         if (appsvc == NULL) {
2922                                 DBG("Malloc Failed\n");
2923                                 return -1;
2924                         }
2925                         memset(appsvc, '\0', sizeof(appsvc_x));
2926                         LISTADD(uiapplication->appsvc, appsvc);
2927                         ret = __ps_process_appsvc(reader, appsvc);
2928                 } else if (!strcmp(ASCII(node), "data-share")) {
2929                         datashare_x *datashare = malloc(sizeof(datashare_x));
2930                         if (datashare == NULL) {
2931                                 DBG("Malloc Failed\n");
2932                                 return -1;
2933                         }
2934                         memset(datashare, '\0', sizeof(datashare_x));
2935                         LISTADD(uiapplication->datashare, datashare);
2936                         ret = __ps_process_datashare(reader, datashare);
2937                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
2938                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
2939                         if (launchconditions == NULL) {
2940                                 DBG("Malloc Failed\n");
2941                                 return -1;
2942                         }
2943                         memset(launchconditions, '\0', sizeof(launchconditions_x));
2944                         LISTADD(uiapplication->launchconditions, launchconditions);
2945                         ret = __ps_process_launchconditions(reader, launchconditions);
2946                 } else if (!strcmp(ASCII(node), "notification")) {
2947                         notification_x *notification = malloc(sizeof(notification_x));
2948                         if (notification == NULL) {
2949                                 DBG("Malloc Failed\n");
2950                                 return -1;
2951                         }
2952                         memset(notification, '\0', sizeof(notification_x));
2953                         LISTADD(uiapplication->notification, notification);
2954                         ret = __ps_process_notification(reader, notification);
2955                 } else
2956                         return -1;
2957                 if (ret < 0) {
2958                         DBG("Processing uiapplication failed\n");
2959                         return ret;
2960                 }
2961         }
2962
2963         if (uiapplication->label) {
2964                 LISTHEAD(uiapplication->label, tmp1);
2965                 uiapplication->label = tmp1;
2966         }
2967         if (uiapplication->icon) {
2968                 LISTHEAD(uiapplication->icon, tmp2);
2969                 uiapplication->icon = tmp2;
2970         }
2971         if (uiapplication->appsvc) {
2972                 LISTHEAD(uiapplication->appsvc, tmp3);
2973                 uiapplication->appsvc = tmp3;
2974         }
2975         if (uiapplication->appcontrol) {
2976                 LISTHEAD(uiapplication->appcontrol, tmp4);
2977                 uiapplication->appcontrol = tmp4;
2978         }
2979         if (uiapplication->launchconditions) {
2980                 LISTHEAD(uiapplication->launchconditions, tmp5);
2981                 uiapplication->launchconditions = tmp5;
2982         }
2983         if (uiapplication->notification) {
2984                 LISTHEAD(uiapplication->notification, tmp6);
2985                 uiapplication->notification = tmp6;
2986         }
2987         if (uiapplication->datashare) {
2988                 LISTHEAD(uiapplication->datashare, tmp7);
2989                 uiapplication->datashare = tmp7;
2990         }
2991         if (uiapplication->category) {
2992                 LISTHEAD(uiapplication->category, tmp8);
2993                 uiapplication->category = tmp8;
2994         }
2995         if (uiapplication->metadata) {
2996                 LISTHEAD(uiapplication->metadata, tmp9);
2997                 uiapplication->metadata = tmp9;
2998         }
2999         if (uiapplication->image) {
3000                 LISTHEAD(uiapplication->image, tmp10);
3001                 uiapplication->image = tmp10;
3002         }
3003         if (uiapplication->permission) {
3004                 LISTHEAD(uiapplication->permission, tmp11);
3005                 uiapplication->permission = tmp11;
3006         }
3007
3008         return ret;
3009 }
3010
3011 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication)
3012 {
3013         const xmlChar *node;
3014         int ret = -1;
3015         int depth = -1;
3016         char *newappid = NULL;
3017         label_x *tmp1 = NULL;
3018         icon_x *tmp2 = NULL;
3019         appsvc_x *tmp3 = NULL;
3020         appcontrol_x *tmp4 = NULL;
3021         datacontrol_x *tmp5 = NULL;
3022         launchconditions_x *tmp6 = NULL;
3023         notification_x *tmp7 = NULL;
3024         datashare_x *tmp8 = NULL;
3025         category_x *tmp9 = NULL;
3026         metadata_x *tmp10 = NULL;
3027         permission_x *tmp11 = NULL;
3028
3029         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
3030                 serviceapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
3031                 if (serviceapplication->appid == NULL) {
3032                         DBG("appid cant be NULL\n");
3033                         return -1;
3034                 }
3035         } else {
3036                 DBG("appid is mandatory\n");
3037                 return -1;
3038         }
3039         /*check appid*/
3040         ret = __validate_appid(package, serviceapplication->appid, &newappid);
3041         if (ret == -1) {
3042                 DBG("appid is not proper\n");
3043                 return -1;
3044         } else {
3045                 if (newappid) {
3046                         if (serviceapplication->appid)
3047                                 free((void *)serviceapplication->appid);
3048                         serviceapplication->appid = newappid;
3049                 }
3050         }
3051         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
3052                 serviceapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
3053         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
3054                 serviceapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
3055         if (xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot"))) {
3056                 serviceapplication->onboot = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot")));
3057                 if (serviceapplication->onboot == NULL)
3058                         serviceapplication->onboot = strdup("false");
3059         } else {
3060                 serviceapplication->onboot = strdup("false");
3061         }
3062         if (xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart"))) {
3063                 serviceapplication->autorestart = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart")));
3064                 if (serviceapplication->autorestart == NULL)
3065                         serviceapplication->autorestart = strdup("false");
3066         } else {
3067                 serviceapplication->autorestart = strdup("false");
3068         }
3069         if (xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type"))) {
3070                 serviceapplication->permission_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type")));
3071                 if (serviceapplication->permission_type == NULL)
3072                         serviceapplication->permission_type = strdup("normal");
3073         } else {
3074                 serviceapplication->permission_type = strdup("normal");
3075         }
3076
3077         depth = xmlTextReaderDepth(reader);
3078         while ((ret = __next_child_element(reader, depth))) {
3079                 node = xmlTextReaderConstName(reader);
3080                 if (!node) {
3081                         DBG("xmlTextReaderConstName value is NULL\n");
3082                         return -1;
3083                 }
3084
3085                 if (!strcmp(ASCII(node), "label")) {
3086                         label_x *label = malloc(sizeof(label_x));
3087                         if (label == NULL) {
3088                                 DBG("Malloc Failed\n");
3089                                 return -1;
3090                         }
3091                         memset(label, '\0', sizeof(label_x));
3092                         LISTADD(serviceapplication->label, label);
3093                         ret = __ps_process_label(reader, label);
3094                 } else if (!strcmp(ASCII(node), "icon")) {
3095                         icon_x *icon = malloc(sizeof(icon_x));
3096                         if (icon == NULL) {
3097                                 DBG("Malloc Failed\n");
3098                                 return -1;
3099                         }
3100                         memset(icon, '\0', sizeof(icon_x));
3101                         LISTADD(serviceapplication->icon, icon);
3102                         ret = __ps_process_icon(reader, icon);
3103                 } else if (!strcmp(ASCII(node), "category")) {
3104                         category_x *category = malloc(sizeof(category_x));
3105                         if (category == NULL) {
3106                                 DBG("Malloc Failed\n");
3107                                 return -1;
3108                         }
3109                         memset(category, '\0', sizeof(category_x));
3110                         LISTADD(serviceapplication->category, category);
3111                         ret = __ps_process_category(reader, category);
3112                 } else if (!strcmp(ASCII(node), "metadata")) {
3113                         metadata_x *metadata = malloc(sizeof(metadata_x));
3114                         if (metadata == NULL) {
3115                                 DBG("Malloc Failed\n");
3116                                 return -1;
3117                         }
3118                         memset(metadata, '\0', sizeof(metadata_x));
3119                         LISTADD(serviceapplication->metadata, metadata);
3120                         ret = __ps_process_metadata(reader, metadata);
3121                 } else if (!strcmp(ASCII(node), "permission")) {
3122                         permission_x *permission = malloc(sizeof(permission_x));
3123                         if (permission == NULL) {
3124                                 DBG("Malloc Failed\n");
3125                                 return -1;
3126                         }
3127                         memset(permission, '\0', sizeof(permission_x));
3128                         LISTADD(serviceapplication->permission, permission);
3129                         ret = __ps_process_permission(reader, permission);
3130                 } else if (!strcmp(ASCII(node), "app-control")) {
3131                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
3132                         if (appcontrol == NULL) {
3133                                 DBG("Malloc Failed\n");
3134                                 return -1;
3135                         }
3136                         memset(appcontrol, '\0', sizeof(appcontrol_x));
3137                         LISTADD(serviceapplication->appcontrol, appcontrol);
3138                         ret = __ps_process_appcontrol(reader, appcontrol);
3139                 } else if (!strcmp(ASCII(node), "application-service")) {
3140                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
3141                         if (appsvc == NULL) {
3142                                 DBG("Malloc Failed\n");
3143                                 return -1;
3144                         }
3145                         memset(appsvc, '\0', sizeof(appsvc_x));
3146                         LISTADD(serviceapplication->appsvc, appsvc);
3147                         ret = __ps_process_appsvc(reader, appsvc);
3148                 } else if (!strcmp(ASCII(node), "data-share")) {
3149                         datashare_x *datashare = malloc(sizeof(datashare_x));
3150                         if (datashare == NULL) {
3151                                 DBG("Malloc Failed\n");
3152                                 return -1;
3153                         }
3154                         memset(datashare, '\0', sizeof(datashare_x));
3155                         LISTADD(serviceapplication->datashare, datashare);
3156                         ret = __ps_process_datashare(reader, datashare);
3157                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
3158                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
3159                         if (launchconditions == NULL) {
3160                                 DBG("Malloc Failed\n");
3161                                 return -1;
3162                         }
3163                         memset(launchconditions, '\0', sizeof(launchconditions_x));
3164                         LISTADD(serviceapplication->launchconditions, launchconditions);
3165                         ret = __ps_process_launchconditions(reader, launchconditions);
3166                 } else if (!strcmp(ASCII(node), "notification")) {
3167                         notification_x *notification = malloc(sizeof(notification_x));
3168                         if (notification == NULL) {
3169                                 DBG("Malloc Failed\n");
3170                                 return -1;
3171                         }
3172                         memset(notification, '\0', sizeof(notification_x));
3173                         LISTADD(serviceapplication->notification, notification);
3174                         ret = __ps_process_notification(reader, notification);
3175                 } else if (!strcmp(ASCII(node), "data-control")) {
3176                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
3177                         if (datacontrol == NULL) {
3178                                 DBG("Malloc Failed\n");
3179                                 return -1;
3180                         }
3181                         memset(datacontrol, '\0', sizeof(datacontrol_x));
3182                         LISTADD(serviceapplication->datacontrol, datacontrol);
3183                         ret = __ps_process_datacontrol(reader, datacontrol);
3184                 } else
3185                         return -1;
3186                 if (ret < 0) {
3187                         DBG("Processing serviceapplication failed\n");
3188                         return ret;
3189                 }
3190         }
3191
3192         if (serviceapplication->label) {
3193                 LISTHEAD(serviceapplication->label, tmp1);
3194                 serviceapplication->label = tmp1;
3195         }
3196         if (serviceapplication->icon) {
3197                 LISTHEAD(serviceapplication->icon, tmp2);
3198                 serviceapplication->icon = tmp2;
3199         }
3200         if (serviceapplication->appsvc) {
3201                 LISTHEAD(serviceapplication->appsvc, tmp3);
3202                 serviceapplication->appsvc = tmp3;
3203         }
3204         if (serviceapplication->appcontrol) {
3205                 LISTHEAD(serviceapplication->appcontrol, tmp4);
3206                 serviceapplication->appcontrol = tmp4;
3207         }
3208         if (serviceapplication->datacontrol) {
3209                 LISTHEAD(serviceapplication->datacontrol, tmp5);
3210                 serviceapplication->datacontrol = tmp5;
3211         }
3212         if (serviceapplication->launchconditions) {
3213                 LISTHEAD(serviceapplication->launchconditions, tmp6);
3214                 serviceapplication->launchconditions = tmp6;
3215         }
3216         if (serviceapplication->notification) {
3217                 LISTHEAD(serviceapplication->notification, tmp7);
3218                 serviceapplication->notification = tmp7;
3219         }
3220         if (serviceapplication->datashare) {
3221                 LISTHEAD(serviceapplication->datashare, tmp8);
3222                 serviceapplication->datashare = tmp8;
3223         }
3224         if (serviceapplication->category) {
3225                 LISTHEAD(serviceapplication->category, tmp9);
3226                 serviceapplication->category = tmp9;
3227         }
3228         if (serviceapplication->metadata) {
3229                 LISTHEAD(serviceapplication->metadata, tmp10);
3230                 serviceapplication->metadata = tmp10;
3231         }
3232         if (serviceapplication->permission) {
3233                 LISTHEAD(serviceapplication->permission, tmp11);
3234                 serviceapplication->permission = tmp11;
3235         }
3236
3237         return ret;
3238 }
3239
3240 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
3241 {
3242         /*TODO: once policy is set*/
3243         return 0;
3244 }
3245
3246 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
3247 {
3248         /*TODO: once policy is set*/
3249         return 0;
3250 }
3251
3252 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
3253 {
3254         /*TODO: once policy is set*/
3255         return 0;
3256 }
3257
3258 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
3259 {
3260         /*TODO: once policy is set*/
3261         return 0;
3262 }
3263
3264 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
3265 {
3266         /*TODO: once policy is set*/
3267         return 0;
3268 }
3269
3270 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx)
3271 {
3272         DBG("__start_process\n");
3273         const xmlChar *node;
3274         int ret = -1;
3275         int depth = -1;
3276         label_x *tmp1 = NULL;
3277         author_x *tmp2 = NULL;
3278         description_x *tmp3 = NULL;
3279         license_x *tmp4 = NULL;
3280         uiapplication_x *tmp5 = NULL;
3281         serviceapplication_x *tmp6 = NULL;
3282         daemon_x *tmp7 = NULL;
3283         theme_x *tmp8 = NULL;
3284         font_x *tmp9 = NULL;
3285         ime_x *tmp10 = NULL;
3286         icon_x *tmp11 = NULL;
3287         compatibility_x *tmp12 = NULL;
3288         deviceprofile_x *tmp13 = NULL;
3289         privileges_x *tmp14 = NULL;
3290
3291         depth = xmlTextReaderDepth(reader);
3292         while ((ret = __next_child_element(reader, depth))) {
3293                 node = xmlTextReaderConstName(reader);
3294                 if (!node) {
3295                         DBG("xmlTextReaderConstName value is NULL\n");
3296                         return -1;
3297                 }
3298
3299                 if (!strcmp(ASCII(node), "label")) {
3300                         label_x *label = malloc(sizeof(label_x));
3301                         if (label == NULL) {
3302                                 DBG("Malloc Failed\n");
3303                                 return -1;
3304                         }
3305                         memset(label, '\0', sizeof(label_x));
3306                         LISTADD(mfx->label, label);
3307                         ret = __ps_process_label(reader, label);
3308                 } else if (!strcmp(ASCII(node), "author")) {
3309                         author_x *author = malloc(sizeof(author_x));
3310                         if (author == NULL) {
3311                                 DBG("Malloc Failed\n");
3312                                 return -1;
3313                         }
3314                         memset(author, '\0', sizeof(author_x));
3315                         LISTADD(mfx->author, author);
3316                         ret = __ps_process_author(reader, author);
3317                 } else if (!strcmp(ASCII(node), "description")) {
3318                         description_x *description = malloc(sizeof(description_x));
3319                         if (description == NULL) {
3320                                 DBG("Malloc Failed\n");
3321                                 return -1;
3322                         }
3323                         memset(description, '\0', sizeof(description_x));
3324                         LISTADD(mfx->description, description);
3325                         ret = __ps_process_description(reader, description);
3326                 } else if (!strcmp(ASCII(node), "license")) {
3327                         license_x *license = malloc(sizeof(license_x));
3328                         if (license == NULL) {
3329                                 DBG("Malloc Failed\n");
3330                                 return -1;
3331                         }
3332                         memset(license, '\0', sizeof(license_x));
3333                         LISTADD(mfx->license, license);
3334                         ret = __ps_process_license(reader, license);
3335                 } else if (!strcmp(ASCII(node), "privileges")) {
3336                         privileges_x *privileges = malloc(sizeof(privileges_x));
3337                         if (privileges == NULL) {
3338                                 DBG("Malloc Failed\n");
3339                                 return -1;
3340                         }
3341                         memset(privileges, '\0', sizeof(privileges_x));
3342                         LISTADD(mfx->privileges, privileges);
3343                         ret = __ps_process_privileges(reader, privileges);
3344                 } else if (!strcmp(ASCII(node), "ui-application")) {
3345                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
3346                         if (uiapplication == NULL) {
3347                                 DBG("Malloc Failed\n");
3348                                 return -1;
3349                         }
3350                         memset(uiapplication, '\0', sizeof(uiapplication_x));
3351                         LISTADD(mfx->uiapplication, uiapplication);
3352                         ret = __ps_process_uiapplication(reader, uiapplication);
3353                 } else if (!strcmp(ASCII(node), "service-application")) {
3354                         serviceapplication_x *serviceapplication = malloc(sizeof(serviceapplication_x));
3355                         if (serviceapplication == NULL) {
3356                                 DBG("Malloc Failed\n");
3357                                 return -1;
3358                         }
3359                         memset(serviceapplication, '\0', sizeof(serviceapplication_x));
3360                         LISTADD(mfx->serviceapplication, serviceapplication);
3361                         ret = __ps_process_serviceapplication(reader, serviceapplication);
3362                 } else if (!strcmp(ASCII(node), "daemon")) {
3363                         daemon_x *daemon = malloc(sizeof(daemon_x));
3364                         if (daemon == NULL) {
3365                                 DBG("Malloc Failed\n");
3366                                 return -1;
3367                         }
3368                         memset(daemon, '\0', sizeof(daemon_x));
3369                         LISTADD(mfx->daemon, daemon);
3370                         ret = __ps_process_daemon(reader, daemon);
3371                 } else if (!strcmp(ASCII(node), "theme")) {
3372                         theme_x *theme = malloc(sizeof(theme_x));
3373                         if (theme == NULL) {
3374                                 DBG("Malloc Failed\n");
3375                                 return -1;
3376                         }
3377                         memset(theme, '\0', sizeof(theme_x));
3378                         LISTADD(mfx->theme, theme);
3379                         ret = __ps_process_theme(reader, theme);
3380                 } else if (!strcmp(ASCII(node), "font")) {
3381                         font_x *font = malloc(sizeof(font_x));
3382                         if (font == NULL) {
3383                                 DBG("Malloc Failed\n");
3384                                 return -1;
3385                         }
3386                         memset(font, '\0', sizeof(font_x));
3387                         LISTADD(mfx->font, font);
3388                         ret = __ps_process_font(reader, font);
3389                 } else if (!strcmp(ASCII(node), "ime")) {
3390                         ime_x *ime = malloc(sizeof(ime_x));
3391                         if (ime == NULL) {
3392                                 DBG("Malloc Failed\n");
3393                                 return -1;
3394                         }
3395                         memset(ime, '\0', sizeof(ime_x));
3396                         LISTADD(mfx->ime, ime);
3397                         ret = __ps_process_ime(reader, ime);
3398                 } else if (!strcmp(ASCII(node), "icon")) {
3399                         icon_x *icon = malloc(sizeof(icon_x));
3400                         if (icon == NULL) {
3401                                 DBG("Malloc Failed\n");
3402                                 return -1;
3403                         }
3404                         memset(icon, '\0', sizeof(icon_x));
3405                         LISTADD(mfx->icon, icon);
3406                         ret = __ps_process_icon(reader, icon);
3407                 } else if (!strcmp(ASCII(node), "device-profile")) {
3408                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
3409                         if (deviceprofile == NULL) {
3410                                 DBG("Malloc Failed\n");
3411                                 return -1;
3412                         }
3413                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
3414                         LISTADD(mfx->deviceprofile, deviceprofile);
3415                         ret = __ps_process_deviceprofile(reader, deviceprofile);
3416                 } else if (!strcmp(ASCII(node), "compatibility")) {
3417                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
3418                         if (compatibility == NULL) {
3419                                 DBG("Malloc Failed\n");
3420                                 return -1;
3421                         }
3422                         memset(compatibility, '\0', sizeof(compatibility_x));
3423                         LISTADD(mfx->compatibility, compatibility);
3424                         ret = __ps_process_compatibility(reader, compatibility);
3425                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
3426                         continue;
3427                 } else if (!strcmp(ASCII(node), "livebox")) {
3428                         continue;
3429                 } else if (!strcmp(ASCII(node), "account")) {
3430                         continue;
3431                 } else if (!strcmp(ASCII(node), "notifications")) {
3432                         continue;
3433                 } else if (!strcmp(ASCII(node), "ime")) {
3434                         continue;
3435                 } else
3436                         return -1;
3437
3438                 if (ret < 0) {
3439                         DBG("Processing manifest failed\n");
3440                         return ret;
3441                 }
3442         }
3443         if (mfx->label) {
3444                 LISTHEAD(mfx->label, tmp1);
3445                 mfx->label = tmp1;
3446         }
3447         if (mfx->author) {
3448                 LISTHEAD(mfx->author, tmp2);
3449                 mfx->author = tmp2;
3450         }
3451         if (mfx->description) {
3452                 LISTHEAD(mfx->description, tmp3);
3453                 mfx->description= tmp3;
3454         }
3455         if (mfx->license) {
3456                 LISTHEAD(mfx->license, tmp4);
3457                 mfx->license= tmp4;
3458         }
3459         if (mfx->uiapplication) {
3460                 LISTHEAD(mfx->uiapplication, tmp5);
3461                 mfx->uiapplication = tmp5;
3462         }
3463         if (mfx->serviceapplication) {
3464                 LISTHEAD(mfx->serviceapplication, tmp6);
3465                 mfx->serviceapplication = tmp6;
3466         }
3467         if (mfx->daemon) {
3468                 LISTHEAD(mfx->daemon, tmp7);
3469                 mfx->daemon= tmp7;
3470         }
3471         if (mfx->theme) {
3472                 LISTHEAD(mfx->theme, tmp8);
3473                 mfx->theme= tmp8;
3474         }
3475         if (mfx->font) {
3476                 LISTHEAD(mfx->font, tmp9);
3477                 mfx->font= tmp9;
3478         }
3479         if (mfx->ime) {
3480                 LISTHEAD(mfx->ime, tmp10);
3481                 mfx->ime= tmp10;
3482         }
3483         if (mfx->icon) {
3484                 LISTHEAD(mfx->icon, tmp11);
3485                 mfx->icon= tmp11;
3486         }
3487         if (mfx->compatibility) {
3488                 LISTHEAD(mfx->compatibility, tmp12);
3489                 mfx->compatibility= tmp12;
3490         }
3491         if (mfx->deviceprofile) {
3492                 LISTHEAD(mfx->deviceprofile, tmp13);
3493                 mfx->deviceprofile= tmp13;
3494         }
3495         if (mfx->privileges) {
3496                 LISTHEAD(mfx->privileges, tmp14);
3497                 mfx->privileges = tmp14;
3498         }
3499         return ret;
3500 }
3501
3502 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx)
3503 {
3504         const xmlChar *node;
3505         int ret = -1;
3506
3507         if ((ret = __next_child_element(reader, -1))) {
3508                 node = xmlTextReaderConstName(reader);
3509                 if (!node) {
3510                         DBG("xmlTextReaderConstName value is NULL\n");
3511                         return -1;
3512                 }
3513
3514                 if (!strcmp(ASCII(node), "manifest")) {
3515                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")))
3516                                 mfx->ns = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")));
3517                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("package"))) {
3518                                 mfx->package= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("package")));
3519                                 if (mfx->package == NULL) {
3520                                         DBG("package cant be NULL\n");
3521                                         return -1;
3522                                 }
3523                         } else {
3524                                 DBG("package field is mandatory\n");
3525                                 return -1;
3526                         }
3527                         package = mfx->package;
3528                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("version")))
3529                                 mfx->version= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("version")));
3530                         /*app2ext needs package size for external installation*/
3531                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
3532                                 mfx->package_size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
3533                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")))
3534                                 mfx->installlocation = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")));
3535                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
3536                                 mfx->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
3537                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")))
3538                                 mfx->root_path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")));
3539                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("csc_path")))
3540                                 mfx->csc_path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("csc_path")));
3541                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting"))) {
3542                                 mfx->appsetting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting")));
3543                                 if (mfx->appsetting == NULL)
3544                                         mfx->appsetting = strdup("false");
3545                         } else {
3546                                 mfx->appsetting = strdup("false");
3547                         }
3548                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")))
3549                                 mfx->storeclient_id= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")));
3550                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting"))) {
3551                                 mfx->nodisplay_setting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting")));
3552                                 if (mfx->nodisplay_setting == NULL)
3553                                         mfx->nodisplay_setting = strdup("false");
3554                         } else {
3555                                 mfx->nodisplay_setting = strdup("false");
3556                         }
3557                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("url")))
3558                                 mfx->package_url= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("url")));
3559
3560                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
3561                         mfx->preload = strdup("False");
3562                         mfx->removable = strdup("True");
3563                         mfx->readonly = strdup("False");
3564                         mfx->update = strdup("False");
3565                         char buf[PKG_STRING_LEN_MAX] = {'\0'};
3566                         char *val = NULL;
3567                         time_t current_time;
3568                         time(&current_time);
3569                         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", current_time);
3570                         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
3571                         mfx->installed_time = val;
3572
3573                         mfx->installed_storage= strdup("installed_internal");
3574
3575                         ret = __start_process(reader, mfx);
3576                 } else {
3577                         DBG("No Manifest element found\n");
3578                         return -1;
3579                 }
3580         }
3581         return ret;
3582 }
3583
3584 #define DESKTOP_RW_PATH "/opt/share/applications/"
3585 #define DESKTOP_RO_PATH "/usr/share/applications/"
3586 #define MANIFEST_RO_PREFIX "/usr/share/packages/"
3587
3588 static char* __convert_to_system_locale(const char *mlocale)
3589 {
3590         if (mlocale == NULL)
3591                 return NULL;
3592         char *locale = NULL;
3593         locale = (char *)calloc(1, 6);
3594         if (!locale) {
3595                 DBGE("Malloc Failed\n");
3596                 return NULL;
3597         }
3598
3599         strncpy(locale, mlocale, 2);
3600         strncat(locale, "_", 1);
3601         locale[3] = toupper(mlocale[3]);
3602         locale[4] = toupper(mlocale[4]);
3603         return locale;
3604 }
3605
3606 #define LIBAIL_PATH "/usr/lib/libail.so.0"
3607
3608 /* operation_type */
3609 typedef enum {
3610         AIL_INSTALL = 0,
3611         AIL_UPDATE,
3612         AIL_REMOVE,
3613         AIL_CLEAN,
3614         AIL_MAX
3615 } AIL_TYPE;
3616
3617 static int __ail_change_info(int op, const char *appid)
3618 {
3619         void *lib_handle = NULL;
3620         int (*ail_desktop_operation) (const char *);
3621         char *aop = NULL;
3622         int ret = 0;
3623
3624         if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
3625                 DBGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
3626                 goto END;
3627         }
3628
3629
3630         switch (op) {
3631                 case 0:
3632                         aop  = "ail_desktop_add";
3633                         break;
3634                 case 1:
3635                         aop  = "ail_desktop_update";
3636                         break;
3637                 case 2:
3638                         aop  = "ail_desktop_remove";
3639                         break;
3640                 case 3:
3641                         aop  = "ail_desktop_clean";
3642                         break;
3643                 default:
3644                         goto END;
3645                         break;
3646         }
3647
3648         if ((ail_desktop_operation =
3649              dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
3650                 DBGE("can not find symbol \n");
3651                 goto END;
3652         }
3653
3654         ret = ail_desktop_operation(appid);
3655
3656 END:
3657         if (lib_handle)
3658                 dlclose(lib_handle);
3659
3660         return ret;
3661 }
3662
3663
3664 /* desktop shoud be generated automatically based on manifest */
3665 /* Currently removable, taskmanage, etc fields are not considerd. it will be decided soon.*/
3666 #define BUFMAX 1024*128
3667 static int __ps_make_nativeapp_desktop(manifest_x * mfx, const char *manifest, bool is_update)
3668 {
3669         FILE* file = NULL;
3670         int fd = 0;
3671         char filepath[PKG_STRING_LEN_MAX] = "";
3672         char *buf = NULL;
3673         char *buftemp = NULL;
3674         char *locale = NULL;
3675
3676         buf = (char *)calloc(1, BUFMAX);
3677         if (!buf) {
3678                 DBGE("Malloc Failed\n");
3679                 return -1;
3680         }
3681
3682         buftemp = (char *)calloc(1, BUFMAX);
3683         if (!buftemp) {
3684                 DBGE("Malloc Failed\n");
3685                 free(buf);
3686                 return -1;
3687         }
3688
3689         if (is_update)
3690                 __ail_change_info(AIL_CLEAN, mfx->package);
3691
3692         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
3693
3694                 if (manifest != NULL) {
3695                         /* skip making a deskfile and update ail, if preload app is updated */
3696                         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
3697                                 __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid);
3698                     DBGE("preload app is update : skip and update ail : %s", manifest);
3699                                 continue;
3700                         }
3701                 }
3702
3703                 if(mfx->readonly && !strcasecmp(mfx->readonly, "True"))
3704                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RO_PATH, mfx->uiapplication->appid);
3705                 else
3706                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, mfx->uiapplication->appid);
3707
3708                 /* skip if desktop exists
3709                 if (access(filepath, R_OK) == 0)
3710                         continue;
3711                 */
3712
3713                 file = fopen(filepath, "w");
3714                 if(file == NULL)
3715                 {
3716                     DBGE("Can't open %s", filepath);
3717                     free(buf);
3718                     free(buftemp);
3719                     return -1;
3720                 }
3721
3722                 snprintf(buf, BUFMAX, "[Desktop Entry]\n");
3723                 fwrite(buf, 1, strlen(buf), file);
3724
3725                 for( ; mfx->uiapplication->label ; mfx->uiapplication->label = mfx->uiapplication->label->next) {
3726                         if(!strcmp(mfx->uiapplication->label->lang, DEFAULT_LOCALE)) {
3727                                 snprintf(buf, BUFMAX, "Name=%s\n",      mfx->uiapplication->label->text);
3728                         } else {
3729                                 locale = __convert_to_system_locale(mfx->uiapplication->label->lang);
3730                                 snprintf(buf, BUFMAX, "Name[%s]=%s\n", locale,
3731                                         mfx->uiapplication->label->text);
3732                                 free(locale);
3733                         }
3734                         fwrite(buf, 1, strlen(buf), file);
3735                 }
3736
3737                 if(mfx->uiapplication->label && mfx->uiapplication->label->text) {
3738                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->uiapplication->label->text);
3739                         fwrite(buf, 1, strlen(buf), file);
3740                 }
3741 /*
3742                 else if(mfx->label && mfx->label->text) {
3743                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->label->text);
3744                         fwrite(buf, 1, strlen(buf), file);
3745                 } else {
3746                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->package);
3747                         fwrite(buf, 1, strlen(buf), file);
3748                 }
3749 */
3750
3751
3752                 snprintf(buf, BUFMAX, "Type=Application\n");
3753                 fwrite(buf, 1, strlen(buf), file);
3754
3755                 if(mfx->uiapplication->exec) {
3756                         snprintf(buf, BUFMAX, "Exec=%s\n", mfx->uiapplication->exec);
3757                         fwrite(buf, 1, strlen(buf), file);
3758                 }
3759
3760                 if(mfx->uiapplication->icon && mfx->uiapplication->icon->text) {
3761                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->uiapplication->icon->text);
3762                         fwrite(buf, 1, strlen(buf), file);
3763                 } else if(mfx->icon && mfx->icon->text) {
3764                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->icon->text);
3765                         fwrite(buf, 1, strlen(buf), file);
3766                 }
3767
3768                 // MIME types
3769                 if(mfx->uiapplication && mfx->uiapplication->appsvc) {
3770                         appsvc_x *asvc = mfx->uiapplication->appsvc;
3771                         mime_x *mi = NULL;
3772                         const char *mime = NULL;
3773                         const char *mime_delim = "; ";
3774                         int mime_count = 0;
3775
3776                         strncpy(buf, "MimeType=", BUFMAX-1);
3777                         while (asvc) {
3778                                 mi = asvc->mime;
3779                                 while (mi) {
3780                                         mime_count++;
3781                                         mime = mi->name;
3782                                         DBG("MIME type: %s\n", mime);
3783                                         strncat(buf, mime, BUFMAX-strlen(buf)-1);
3784                                         if(mi->next) {
3785                                                 strncat(buf, mime_delim, BUFMAX-strlen(buf)-1);
3786                                         }
3787
3788                                         mi = mi->next;
3789                                         mime = NULL;
3790                                 }
3791                                 asvc = asvc->next;
3792                         }
3793                         DBG("MIME types: buf[%s]\n", buf);
3794                         DBG("MIME count: %d\n", mime_count);
3795                         if(mime_count)
3796                                 fwrite(buf, 1, strlen(buf), file);
3797                 }
3798
3799                 if(mfx->version) {
3800                         snprintf(buf, BUFMAX, "Version=%s\n", mfx->version);
3801                         fwrite(buf, 1, strlen(buf), file);
3802                 }
3803
3804                 if(mfx->uiapplication->nodisplay) {
3805                         snprintf(buf, BUFMAX, "NoDisplay=%s\n", mfx->uiapplication->nodisplay);
3806                         fwrite(buf, 1, strlen(buf), file);
3807                 }
3808
3809                 if(mfx->uiapplication->categories) {
3810                         snprintf(buf, BUFMAX, "Categories=%s\n", mfx->uiapplication->categories);
3811                         fwrite(buf, 1, strlen(buf), file);
3812                 }
3813
3814                 if(mfx->uiapplication->taskmanage && !strcasecmp(mfx->uiapplication->taskmanage, "False")) {
3815                         snprintf(buf, BUFMAX, "X-TIZEN-TaskManage=False\n");
3816                         fwrite(buf, 1, strlen(buf), file);
3817                 }
3818
3819                 if(mfx->uiapplication->enabled && !strcasecmp(mfx->uiapplication->enabled, "False")) {
3820                         snprintf(buf, BUFMAX, "X-TIZEN-Enabled=False\n");
3821                         fwrite(buf, 1, strlen(buf), file);
3822                 }
3823
3824                 if(mfx->uiapplication->hwacceleration) {
3825                         snprintf(buf, BUFMAX, "Hw-Acceleration=%s\n", mfx->uiapplication->hwacceleration);
3826                         fwrite(buf, 1, strlen(buf), file);
3827                 }
3828
3829                 if(mfx->uiapplication->multiple && !strcasecmp(mfx->uiapplication->multiple, "True")) {
3830                         snprintf(buf, BUFMAX, "X-TIZEN-Multiple=True\n");
3831                         fwrite(buf, 1, strlen(buf), file);
3832                 }
3833
3834                 if(mfx->uiapplication->extraid) {
3835                         snprintf(buf, BUFMAX, "X-TIZEN-PackageID=%s\n", mfx->uiapplication->extraid);
3836                         fwrite(buf, 1, strlen(buf), file);
3837                 }
3838
3839                 if(mfx->removable && !strcasecmp(mfx->removable, "False")) {
3840                         snprintf(buf, BUFMAX, "X-TIZEN-Removable=False\n");
3841                         fwrite(buf, 1, strlen(buf), file);
3842                 }
3843
3844                 if(mfx->type) {
3845                         snprintf(buf, BUFMAX, "X-TIZEN-PackageType=%s\n", mfx->type);
3846                         fwrite(buf, 1, strlen(buf), file);
3847                 }
3848
3849                 snprintf(buf, BUFMAX, "X-TIZEN-PkgID=%s\n", mfx->package);
3850                 fwrite(buf, 1, strlen(buf), file);
3851
3852
3853 //              snprintf(buf, BUFMAX, "X-TIZEN-PackageType=rpm\n");
3854 //              fwrite(buf, 1, strlen(buf), file);
3855
3856
3857                 if(mfx->uiapplication->appsvc) {
3858                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3859                         DBG("buf[%s]\n", buf);
3860
3861
3862                         uiapplication_x *up = mfx->uiapplication;
3863                         appsvc_x *asvc = NULL;
3864                         operation_x *op = NULL;
3865                         mime_x *mi = NULL;
3866                         uri_x *ui = NULL;
3867                         subapp_x *sub = NULL;
3868                         const char *operation = NULL;
3869                         const char *mime = NULL;
3870                         const char *uri = NULL;
3871                         const char *subapp = NULL;
3872                         int i = 0;
3873
3874
3875                         asvc = up->appsvc;
3876                         while(asvc != NULL) {
3877                                 op = asvc->operation;
3878                                 while(op != NULL) {
3879                                         if (op)
3880                                                 operation = op->name;
3881                                         mi = asvc->mime;
3882
3883                                         do
3884                                         {
3885                                                 if (mi)
3886                                                         mime = mi->name;
3887                                                 sub = asvc->subapp;
3888                                                 do
3889                                                 {
3890                                                         if (sub)
3891                                                                 subapp = sub->name;
3892                                                         ui = asvc->uri;
3893                                                         do
3894                                                         {
3895                                                                 if (ui)
3896                                                                         uri = ui->name;
3897
3898                                                                 if(i++ > 0) {
3899                                                                         strncpy(buftemp, buf, BUFMAX);
3900                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3901                                                                 }
3902
3903
3904                                                                 strncpy(buftemp, buf, BUFMAX);
3905                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3906                                                                 DBG("buf[%s]\n", buf);
3907
3908                                                                 if (ui)
3909                                                                         ui = ui->next;
3910                                                                 uri = NULL;
3911                                                         } while(ui != NULL);
3912                                                 if (sub)
3913                                                                 sub = sub->next;
3914                                                         subapp = NULL;
3915                                                 }while(sub != NULL);
3916                                                 if (mi)
3917                                                         mi = mi->next;
3918                                                 mime = NULL;
3919                                         }while(mi != NULL);
3920                                         if (op)
3921                                                 op = op->next;
3922                                         operation = NULL;
3923                                 }
3924                                 asvc = asvc->next;
3925                         }
3926
3927
3928                         fwrite(buf, 1, strlen(buf), file);
3929
3930 //                      strncpy(buftemp, buf, BUFMAX);
3931 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
3932 //                      fwrite(buf, 1, strlen(buf), file);
3933                 }
3934
3935                 if(mfx->uiapplication->appcontrol) {
3936                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
3937                         DBG("buf[%s]\n", buf);
3938
3939                         uiapplication_x *up = mfx->uiapplication;
3940                         appcontrol_x *acontrol = NULL;
3941                         operation_x *op = NULL;
3942                         mime_x *mi = NULL;
3943                         uri_x *ui = NULL;
3944                         subapp_x *sub = NULL;
3945                         const char *operation = NULL;
3946                         const char *mime = NULL;
3947                         const char *uri = NULL;
3948                         const char *subapp = NULL;
3949                         int i = 0;
3950
3951                         acontrol = up->appcontrol;
3952                         while(acontrol != NULL) {
3953                                 op = acontrol->operation;
3954                                 while(op != NULL) {
3955                                         if (op)
3956                                                 operation = op->name;
3957                                         mi = acontrol->mime;
3958
3959                                         do
3960                                         {
3961                                                 if (mi)
3962                                                         mime = mi->name;
3963                                                 sub = acontrol->subapp;
3964                                                 do
3965                                                 {
3966                                                         if (sub)
3967                                                                 subapp = sub->name;
3968                                                         ui = acontrol->uri;
3969                                                         do
3970                                                         {
3971                                                                 if (ui)
3972                                                                         uri = ui->name;
3973
3974                                                                 if(i++ > 0) {
3975                                                                         strncpy(buftemp, buf, BUFMAX);
3976                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
3977                                                                 }
3978
3979                                                                 strncpy(buftemp, buf, BUFMAX);
3980                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
3981                                                                 DBG("buf[%s]\n", buf);
3982
3983                                                                 if (ui)
3984                                                                         ui = ui->next;
3985                                                                 uri = NULL;
3986                                                         } while(ui != NULL);
3987                                                 if (sub)
3988                                                                 sub = sub->next;
3989                                                         subapp = NULL;
3990                                                 }while(sub != NULL);
3991                                                 if (mi)
3992                                                         mi = mi->next;
3993                                                 mime = NULL;
3994                                         }while(mi != NULL);
3995                                         if (op)
3996                                                 op = op->next;
3997                                         operation = NULL;
3998                                 }
3999                                 acontrol = acontrol->next;
4000                         }
4001
4002
4003                         fwrite(buf, 1, strlen(buf), file);
4004
4005 //                      strncpy(buftemp, buf, BUFMAX);
4006 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
4007 //                      fwrite(buf, 1, strlen(buf), file);
4008                 }
4009
4010                 fflush(file);
4011                 fd = fileno(file);
4012                 fsync(fd);
4013                 fclose(file);
4014
4015                 __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid);
4016         }
4017
4018         free(buf);
4019         free(buftemp);
4020
4021         return 0;
4022 }
4023
4024 static int __ps_remove_nativeapp_desktop(manifest_x *mfx)
4025 {
4026         char filepath[PKG_STRING_LEN_MAX] = "";
4027         int ret = 0;
4028         uiapplication_x *uiapplication = mfx->uiapplication;
4029
4030         for(; uiapplication; uiapplication=uiapplication->next) {
4031                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, uiapplication->appid);
4032
4033                 __ail_change_info(AIL_REMOVE, uiapplication->appid);
4034
4035                 ret = remove(filepath);
4036                 if (ret <0)
4037                         return -1;
4038         }
4039
4040         return 0;
4041 }
4042
4043 #define LIBAPPSVC_PATH "/usr/lib/libappsvc.so.0"
4044
4045 static int __ps_remove_appsvc_db(manifest_x *mfx)
4046 {
4047         void *lib_handle = NULL;
4048         int (*appsvc_operation) (const char *);
4049         int ret = 0;
4050         uiapplication_x *uiapplication = mfx->uiapplication;
4051
4052         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
4053                 DBGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAPPSVC_PATH);
4054                 goto END;
4055         }
4056
4057         if ((appsvc_operation =
4058                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
4059                 DBGE("can not find symbol \n");
4060                 goto END;
4061         }
4062
4063         for(; uiapplication; uiapplication=uiapplication->next) {
4064                 ret = appsvc_operation(uiapplication->appid);
4065                 if (ret <0)
4066                         DBGE("can not operation  symbol \n");
4067         }
4068
4069 END:
4070         if (lib_handle)
4071                 dlclose(lib_handle);
4072
4073         return ret;
4074 }
4075
4076 #define PRELOAD_PACKAGE_LIST "/usr/etc/package-manager/preload/preload_list.txt"
4077 static int __add_preload_info(manifest_x * mfx, const char *manifest)
4078 {
4079         FILE *fp = NULL;
4080         char buffer[1024] = { 0 };
4081         int state = 0;
4082
4083         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
4084                 free((void *)mfx->readonly);
4085                 mfx->readonly = strdup("True");
4086
4087                 free((void *)mfx->preload);
4088                 mfx->preload = strdup("True");
4089
4090                 free((void *)mfx->removable);
4091                 mfx->removable = strdup("False");
4092
4093                 return 0;
4094         }
4095
4096         fp = fopen(PRELOAD_PACKAGE_LIST, "r");
4097         if (fp == NULL) {
4098                 DBGE("no preload list\n");
4099                 return -1;
4100         }
4101
4102         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
4103                 if (buffer[0] == '#') {
4104                         if(strcasestr(buffer, "RW_NORM"))
4105                                 state = 2;
4106                         else if(strcasestr(buffer, "RW_RM"))
4107                                 state = 3;
4108                         else
4109                                 continue;
4110                 }
4111
4112                 __str_trim(buffer);
4113
4114                 if(!strcmp(mfx->package, buffer)) {
4115                         free((void *)mfx->preload);
4116                         mfx->preload = strdup("True");
4117                         if(state == 2){
4118                                 free((void *)mfx->readonly);
4119                                 mfx->readonly = strdup("False");
4120                                 free((void *)mfx->removable);
4121                                 mfx->removable = strdup("False");
4122                         } else if(state == 3){
4123                                 free((void *)mfx->readonly);
4124                                 mfx->readonly = strdup("False");
4125                                 free((void *)mfx->removable);
4126                                 mfx->removable = strdup("True");
4127                         }
4128                 }
4129
4130                 memset(buffer, 0x00, sizeof(buffer));
4131         }
4132
4133         if (fp != NULL)
4134                 fclose(fp);
4135
4136         return 0;
4137 }
4138
4139 static int __check_preload_updated(manifest_x * mfx, const char *manifest)
4140 {
4141         char filepath[PKG_STRING_LEN_MAX] = "";
4142         int ret = 0;
4143         uiapplication_x *uiapplication = mfx->uiapplication;
4144
4145         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
4146                 /* if preload app is updated, then remove previous desktop file on RW*/
4147                 for(; uiapplication; uiapplication=uiapplication->next) {
4148                                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, uiapplication->appid);
4149                         ret = remove(filepath);
4150                         if (ret <0)
4151                                 return -1;
4152                 }
4153         } else {
4154                 /* if downloaded app is updated, then update tag set true*/
4155                 free((void *)mfx->update);
4156                 mfx->update = strdup("true");
4157         }
4158
4159         return 0;
4160 }
4161
4162
4163 API int pkgmgr_parser_create_desktop_file(manifest_x *mfx)
4164 {
4165         int ret = 0;
4166         if (mfx == NULL) {
4167                 DBG("Manifest pointer is NULL\n");
4168                 return -1;
4169         }
4170         ret = __ps_make_nativeapp_desktop(mfx, NULL, 0);
4171         if (ret == -1)
4172                 DBG("Creating desktop file failed\n");
4173         else
4174                 DBG("Creating desktop file Success\n");
4175         return ret;
4176 }
4177
4178 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
4179 {
4180         if (mfx == NULL)
4181                 return;
4182         if (mfx->ns) {
4183                 free((void *)mfx->ns);
4184                 mfx->ns = NULL;
4185         }
4186         if (mfx->package) {
4187                 free((void *)mfx->package);
4188                 mfx->package = NULL;
4189         }
4190         if (mfx->version) {
4191                 free((void *)mfx->version);
4192                 mfx->version = NULL;
4193         }
4194         if (mfx->installlocation) {
4195                 free((void *)mfx->installlocation);
4196                 mfx->installlocation = NULL;
4197         }
4198         if (mfx->preload) {
4199                 free((void *)mfx->preload);
4200                 mfx->preload = NULL;
4201         }
4202         if (mfx->readonly) {
4203                 free((void *)mfx->readonly);
4204                 mfx->readonly = NULL;
4205         }
4206         if (mfx->removable) {
4207                 free((void *)mfx->removable);
4208                 mfx->removable = NULL;
4209         }
4210         if (mfx->update) {
4211                 free((void *)mfx->update);
4212                 mfx->update = NULL;
4213         }
4214         if (mfx->type) {
4215                 free((void *)mfx->type);
4216                 mfx->type = NULL;
4217         }
4218         if (mfx->package_size) {
4219                 free((void *)mfx->package_size);
4220                 mfx->package_size = NULL;
4221         }
4222         if (mfx->installed_time) {
4223                 free((void *)mfx->installed_time);
4224                 mfx->installed_time = NULL;
4225         }
4226         if (mfx->installed_storage) {
4227                 free((void *)mfx->installed_storage);
4228                 mfx->installed_storage = NULL;
4229         }
4230         if (mfx->storeclient_id) {
4231                 free((void *)mfx->storeclient_id);
4232                 mfx->storeclient_id = NULL;
4233         }
4234         if (mfx->mainapp_id) {
4235                 free((void *)mfx->mainapp_id);
4236                 mfx->mainapp_id = NULL;
4237         }
4238         if (mfx->package_url) {
4239                 free((void *)mfx->package_url);
4240                 mfx->package_url = NULL;
4241         }
4242         if (mfx->root_path) {
4243                 free((void *)mfx->root_path);
4244                 mfx->root_path = NULL;
4245         }
4246         if (mfx->csc_path) {
4247                 free((void *)mfx->csc_path);
4248                 mfx->csc_path = NULL;
4249         }
4250         if (mfx->appsetting) {
4251                 free((void *)mfx->appsetting);
4252                 mfx->appsetting = NULL;
4253         }
4254         if (mfx->nodisplay_setting) {
4255                 free((void *)mfx->nodisplay_setting);
4256                 mfx->nodisplay_setting = NULL;
4257         }
4258
4259         /*Free Icon*/
4260         if (mfx->icon) {
4261                 icon_x *icon = mfx->icon;
4262                 icon_x *tmp = NULL;
4263                 while(icon != NULL) {
4264                         tmp = icon->next;
4265                         __ps_free_icon(icon);
4266                         icon = tmp;
4267                 }
4268         }
4269         /*Free Label*/
4270         if (mfx->label) {
4271                 label_x *label = mfx->label;
4272                 label_x *tmp = NULL;
4273                 while(label != NULL) {
4274                         tmp = label->next;
4275                         __ps_free_label(label);
4276                         label = tmp;
4277                 }
4278         }
4279         /*Free Author*/
4280         if (mfx->author) {
4281                 author_x *author = mfx->author;
4282                 author_x *tmp = NULL;
4283                 while(author != NULL) {
4284                         tmp = author->next;
4285                         __ps_free_author(author);
4286                         author = tmp;
4287                 }
4288         }
4289         /*Free Description*/
4290         if (mfx->description) {
4291                 description_x *description = mfx->description;
4292                 description_x *tmp = NULL;
4293                 while(description != NULL) {
4294                         tmp = description->next;
4295                         __ps_free_description(description);
4296                         description = tmp;
4297                 }
4298         }
4299         /*Free License*/
4300         if (mfx->license) {
4301                 license_x *license = mfx->license;
4302                 license_x *tmp = NULL;
4303                 while(license != NULL) {
4304                         tmp = license->next;
4305                         __ps_free_license(license);
4306                         license = tmp;
4307                 }
4308         }
4309         /*Free Privileges*/
4310         if (mfx->privileges) {
4311                 privileges_x *privileges = mfx->privileges;
4312                 privileges_x *tmp = NULL;
4313                 while(privileges != NULL) {
4314                         tmp = privileges->next;
4315                         __ps_free_privileges(privileges);
4316                         privileges = tmp;
4317                 }
4318         }
4319         /*Free UiApplication*/
4320         if (mfx->uiapplication) {
4321                 uiapplication_x *uiapplication = mfx->uiapplication;
4322                 uiapplication_x *tmp = NULL;
4323                 while(uiapplication != NULL) {
4324                         tmp = uiapplication->next;
4325                         __ps_free_uiapplication(uiapplication);
4326                         uiapplication = tmp;
4327                 }
4328         }
4329         /*Free ServiceApplication*/
4330         if (mfx->serviceapplication) {
4331                 serviceapplication_x *serviceapplication = mfx->serviceapplication;
4332                 serviceapplication_x *tmp = NULL;
4333                 while(serviceapplication != NULL) {
4334                         tmp = serviceapplication->next;
4335                         __ps_free_serviceapplication(serviceapplication);
4336                         serviceapplication = tmp;
4337                 }
4338         }
4339         /*Free Daemon*/
4340         if (mfx->daemon) {
4341                 daemon_x *daemon = mfx->daemon;
4342                 daemon_x *tmp = NULL;
4343                 while(daemon != NULL) {
4344                         tmp = daemon->next;
4345                         __ps_free_daemon(daemon);
4346                         daemon = tmp;
4347                 }
4348         }
4349         /*Free Theme*/
4350         if (mfx->theme) {
4351                 theme_x *theme = mfx->theme;
4352                 theme_x *tmp = NULL;
4353                 while(theme != NULL) {
4354                         tmp = theme->next;
4355                         __ps_free_theme(theme);
4356                         theme = tmp;
4357                 }
4358         }
4359         /*Free Font*/
4360         if (mfx->font) {
4361                 font_x *font = mfx->font;
4362                 font_x *tmp = NULL;
4363                 while(font != NULL) {
4364                         tmp = font->next;
4365                         __ps_free_font(font);
4366                         font = tmp;
4367                 }
4368         }
4369         /*Free Ime*/
4370         if (mfx->ime) {
4371                 ime_x *ime = mfx->ime;
4372                 ime_x *tmp = NULL;
4373                 while(ime != NULL) {
4374                         tmp = ime->next;
4375                         __ps_free_ime(ime);
4376                         ime = tmp;
4377                 }
4378         }
4379         /*Free Compatibility*/
4380         if (mfx->compatibility) {
4381                 compatibility_x *compatibility = mfx->compatibility;
4382                 compatibility_x *tmp = NULL;
4383                 while(compatibility != NULL) {
4384                         tmp = compatibility->next;
4385                         __ps_free_compatibility(compatibility);
4386                         compatibility = tmp;
4387                 }
4388         }
4389         /*Free DeviceProfile*/
4390         if (mfx->deviceprofile) {
4391                 deviceprofile_x *deviceprofile = mfx->deviceprofile;
4392                 deviceprofile_x *tmp = NULL;
4393                 while(deviceprofile != NULL) {
4394                         tmp = deviceprofile->next;
4395                         __ps_free_deviceprofile(deviceprofile);
4396                         deviceprofile = tmp;
4397                 }
4398         }
4399         free((void*)mfx);
4400         mfx = NULL;
4401         return;
4402 }
4403
4404 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
4405 {
4406         DBG("parsing start\n");
4407         xmlTextReaderPtr reader;
4408         manifest_x *mfx = NULL;
4409
4410         reader = xmlReaderForFile(manifest, NULL, 0);
4411         if (reader) {
4412                 mfx = malloc(sizeof(manifest_x));
4413                 if (mfx) {
4414                         memset(mfx, '\0', sizeof(manifest_x));
4415                         if (__process_manifest(reader, mfx) < 0) {
4416                                 DBG("Parsing Failed\n");
4417                                 pkgmgr_parser_free_manifest_xml(mfx);
4418                                 mfx = NULL;
4419                         } else
4420                                 DBG("Parsing Success\n");
4421                 } else {
4422                         DBG("Memory allocation error\n");
4423                 }
4424                 xmlFreeTextReader(reader);
4425         } else {
4426                 DBG("Unable to create xml reader\n");
4427         }
4428         return mfx;
4429 }
4430
4431 /* These APIs are intended to call parser directly */
4432
4433 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
4434 {
4435         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4436         if (manifest == NULL) {
4437                 DBG("argument supplied is NULL\n");
4438                 return PMINFO_R_EINVAL;
4439         }
4440         DBG("parsing manifest for installation: %s\n", manifest);
4441         manifest_x *mfx = NULL;
4442         int ret = -1;
4443         char roxml_check[PKG_STRING_LEN_MAX] = {'\0'};
4444
4445         xmlInitParser();
4446         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4447         DBG("Parsing Finished\n");
4448         if (mfx == NULL)
4449                 return PMINFO_R_ERROR;
4450
4451         __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
4452         __add_preload_info(mfx, manifest);
4453         DBG("Added preload infomation\n");
4454
4455         __ps_process_tag(mfx, tagv);
4456
4457         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
4458         if (ret == -1)
4459                 DBG("DB Insert failed\n");
4460         else
4461                 DBG("DB Insert Success\n");
4462
4463         ret = __ps_process_mdparser(mfx, ACTION_INSTALL);
4464         if (ret == -1)
4465                 DBG("Creating metadata parser failed\n");
4466
4467         ret = __ps_make_nativeapp_desktop(mfx, NULL, 0);
4468         if (ret == -1)
4469                 DBG("Creating desktop file failed\n");
4470         else
4471                 DBG("Creating desktop file Success\n");
4472
4473         pkgmgr_parser_free_manifest_xml(mfx);
4474         DBG("Free Done\n");
4475         xmlCleanupParser();
4476
4477         return PMINFO_R_OK;
4478 }
4479
4480 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
4481 {
4482         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4483         if (manifest == NULL) {
4484                 DBG("argument supplied is NULL\n");
4485                 return PMINFO_R_EINVAL;
4486         }
4487         DBG("parsing manifest for upgradation: %s\n", manifest);
4488         manifest_x *mfx = NULL;
4489         int ret = -1;
4490         bool preload = false;
4491         char *csc_path = NULL;
4492         pkgmgrinfo_pkginfo_h handle = NULL;
4493
4494         xmlInitParser();
4495         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4496         DBG("Parsing Finished\n");
4497         if (mfx == NULL)
4498                 return PMINFO_R_ERROR;
4499         
4500         __streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
4501         __add_preload_info(mfx, manifest);
4502         DBG("Added preload infomation\n");
4503         __check_preload_updated(mfx, manifest);
4504
4505         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
4506         if (ret != PMINFO_R_OK)
4507                 DBG("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
4508
4509         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
4510         if (ret != PMINFO_R_OK)
4511                 DBG("pkgmgrinfo_pkginfo_is_preload failed\n");
4512
4513         if (preload) {
4514                 free((void *)mfx->preload);
4515                 mfx->preload = strdup("true");
4516         }
4517
4518         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
4519         if (ret != PMINFO_R_OK)
4520                 DBG("pkgmgrinfo_pkginfo_get_csc_path failed\n");
4521
4522         if (csc_path != NULL) {
4523                 if (mfx->csc_path)
4524                         free((void *)mfx->csc_path);
4525                 mfx->csc_path = strdup(csc_path);
4526         }
4527
4528         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
4529         if (ret == -1)
4530                 DBG("DB Update failed\n");
4531         else
4532                 DBG("DB Update Success\n");
4533
4534         ret = __ps_process_mdparser(mfx, ACTION_UPGRADE);
4535         if (ret == -1)
4536                 DBG("Upgrade metadata parser failed\n");
4537
4538         ret = __ps_make_nativeapp_desktop(mfx, manifest, 1);
4539         if (ret == -1)
4540                 DBG("Creating desktop file failed\n");
4541         else
4542                 DBG("Creating desktop file Success\n");
4543
4544         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4545         pkgmgr_parser_free_manifest_xml(mfx);
4546         DBG("Free Done\n");
4547         xmlCleanupParser();
4548
4549         return PMINFO_R_OK;
4550 }
4551
4552 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
4553 {
4554         char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4555         if (manifest == NULL) {
4556                 DBG("argument supplied is NULL\n");
4557                 return PMINFO_R_EINVAL;
4558         }
4559         DBG("parsing manifest for uninstallation: %s\n", manifest);
4560         manifest_x *mfx = NULL;
4561         int ret = -1;
4562         xmlInitParser();
4563         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4564         DBG("Parsing Finished\n");
4565         if (mfx == NULL)
4566                 return PMINFO_R_ERROR;
4567         
4568         __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
4569         __add_preload_info(mfx, manifest);
4570         DBG("Added preload infomation\n");
4571
4572         ret = __ps_process_mdparser(mfx, ACTION_UNINSTALL);
4573         if (ret == -1)
4574                 DBG("Removing metadata parser failed\n");
4575
4576         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
4577         if (ret == -1)
4578                 DBG("DB Delete failed\n");
4579         else
4580                 DBG("DB Delete Success\n");
4581
4582         ret = __ps_remove_nativeapp_desktop(mfx);
4583         if (ret == -1)
4584                 DBG("Removing desktop file failed\n");
4585         else
4586                 DBG("Removing desktop file Success\n");
4587
4588         ret = __ps_remove_appsvc_db(mfx);
4589         if (ret == -1)
4590                 DBG("Removing appsvc_db failed\n");
4591         else
4592                 DBG("Removing appsvc_db Success\n");
4593
4594         pkgmgr_parser_free_manifest_xml(mfx);
4595         DBG("Free Done\n");
4596         xmlCleanupParser();
4597
4598         return PMINFO_R_OK;
4599 }
4600
4601 API int pkgmgr_parser_parse_manifest_for_preload()
4602 {
4603         return pkgmgr_parser_update_preload_info_in_db();
4604 }
4605
4606 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
4607 {
4608         return __pkgid_to_manifest(pkgid);
4609 }
4610
4611 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4612 {
4613         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
4614 }
4615
4616 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4617 {
4618         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
4619 }
4620
4621 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4622 {
4623         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
4624 }
4625
4626 #define SCHEMA_FILE "/usr/etc/package-manager/preload/manifest.xsd"
4627 #if 1
4628 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4629 {
4630         if (manifest == NULL) {
4631                 DBGE("manifest file is NULL\n");
4632                 return PMINFO_R_EINVAL;
4633         }
4634         int ret = -1;
4635         xmlSchemaParserCtxtPtr ctx;
4636         xmlSchemaValidCtxtPtr vctx;
4637         xmlSchemaPtr xschema;
4638         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
4639         if (ctx == NULL) {
4640                 DBGE("xmlSchemaNewParserCtxt() Failed\n");
4641                 return PMINFO_R_ERROR;
4642         }
4643         xschema = xmlSchemaParse(ctx);
4644         if (xschema == NULL) {
4645                 DBGE("xmlSchemaParse() Failed\n");
4646                 return PMINFO_R_ERROR;
4647         }
4648         vctx = xmlSchemaNewValidCtxt(xschema);
4649         if (vctx == NULL) {
4650                 DBGE("xmlSchemaNewValidCtxt() Failed\n");
4651                 return PMINFO_R_ERROR;
4652         }
4653         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
4654         ret = xmlSchemaValidateFile(vctx, manifest, 0);
4655         if (ret == -1) {
4656                 DBGE("xmlSchemaValidateFile() failed\n");
4657                 return PMINFO_R_ERROR;
4658         } else if (ret == 0) {
4659                 DBGE("Manifest is Valid\n");
4660                 return PMINFO_R_OK;
4661         } else {
4662                 DBGE("Manifest Validation Failed with error code %d\n", ret);
4663                 return PMINFO_R_ERROR;
4664         }
4665         return PMINFO_R_OK;
4666 }
4667
4668 #else
4669 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4670 {
4671         int err = 0;
4672         int status = 0;
4673         pid_t pid;
4674
4675         pid = fork();
4676
4677         switch (pid) {
4678         case -1:
4679                 DBGE("fork failed\n");
4680                 return -1;
4681         case 0:
4682                 /* child */
4683                 {
4684                         int dev_null_fd = open ("/dev/null", O_RDWR);
4685                         if (dev_null_fd >= 0)
4686                         {
4687                                 dup2 (dev_null_fd, 0);/*stdin*/
4688                                 dup2 (dev_null_fd, 1);/*stdout*/
4689                                 dup2 (dev_null_fd, 2);/*stderr*/
4690                         }
4691
4692                         if (execl("/usr/bin/xmllint", "xmllint", manifest, "--schema",
4693                                 SCHEMA_FILE, NULL) < 0) {
4694                                 DBGE("execl error\n");
4695                         }
4696
4697                         _exit(100);
4698                 }
4699         default:
4700                 /* parent */
4701                 break;
4702         }
4703
4704         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
4705                 if (err < 0) {
4706                         if (errno == EINTR)
4707                                 continue;
4708                         DBGE("waitpid failed\n");
4709                         return -1;
4710                 }
4711         }
4712
4713
4714         if(WIFEXITED(status) && !WEXITSTATUS(status))
4715                 return 0;
4716         else
4717                 return -1;
4718 }
4719 #endif