tizen 2.3 release
[framework/appfw/pkgmgr-info.git] / parser / pkgmgr_parser.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 #define _GNU_SOURCE
23 #include <dlfcn.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <string.h>
31 #include <libxml/parser.h>
32 #include <libxml/xmlreader.h>
33 #include <libxml/xmlschemas.h>
34 #include <vconf.h>
35 #include <glib.h>
36 #include <db-util.h>
37 #include <journal/appcore.h>
38
39 #include "pkgmgr_parser.h"
40 #include "pkgmgr_parser_internal.h"
41 #include "pkgmgr_parser_db.h"
42 #include "pkgmgr_parser_db_util.h"
43 #include "pkgmgr_parser_signature.h"
44 #include "pkgmgr_parser_plugin.h"
45
46 #include "pkgmgrinfo_debug.h"
47
48 #ifdef LOG_TAG
49 #undef LOG_TAG
50 #endif
51 #define LOG_TAG "PKGMGR_PARSER"
52
53 #define MANIFEST_RW_DIRECTORY "/opt/share/packages"
54 #define MANIFEST_RO_DIRECTORY "/usr/share/packages"
55 #define ASCII(s) (const char *)s
56 #define XMLCHAR(s) (const xmlChar *)s
57
58
59 const char *package;
60
61 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label);
62 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege);
63 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges);
64 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile);
65 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed);
66 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation);
67 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri);
68 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime);
69 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp);
70 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition);
71 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notifiation);
72 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category);
73 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata);
74 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission);
75 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility);
76 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution);
77 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request);
78 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define);
79 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc);
80 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions);
81 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare);
82 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon);
83 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author);
84 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description);
85 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability);
86 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license);
87 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol);
88 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication);
89 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font);
90 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme);
91 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon);
92 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime);
93 static char *__pkgid_to_manifest(const char *pkgid);
94 static int __next_child_element(xmlTextReaderPtr reader, int depth);
95 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx);
96 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx);
97
98
99
100 static void __save_xml_attribute(xmlTextReaderPtr reader, char *attribute, const char **xml_attribute, char *default_value)
101 {
102         xmlChar *attrib_val = xmlTextReaderGetAttribute(reader, XMLCHAR(attribute));
103         if (attrib_val) {
104                 *xml_attribute = ASCII(attrib_val);
105         } else {
106                 if (default_value != NULL) {
107                         *xml_attribute = strdup(default_value);
108                 }
109         }
110 }
111
112 static void __save_xml_lang(xmlTextReaderPtr reader, const char **xml_attribute)
113 {
114         const xmlChar *attrib_val = xmlTextReaderConstXmlLang(reader);
115         if (attrib_val != NULL) {
116                 *xml_attribute = strdup(ASCII(attrib_val));
117         } else {
118                 *xml_attribute = strdup(DEFAULT_LOCALE);
119         }
120 }
121
122 static void __save_xml_value(xmlTextReaderPtr reader, const char **xml_attribute)
123 {
124         xmlTextReaderRead(reader);
125         xmlChar *attrib_val = xmlTextReaderValue(reader);
126
127         if (attrib_val) {
128                 *xml_attribute = ASCII(attrib_val);
129         }
130 }
131
132 static void __save_xml_supportmode(xmlTextReaderPtr reader, manifest_x * mfx)
133 {
134         xmlChar *attrib_val = xmlTextReaderGetAttribute(reader, XMLCHAR("support-mode"));
135         if (attrib_val) {
136                 int temp_mode = 0;
137                 char buffer[10] = {'\0'};
138
139                 if (strstr(ASCII(attrib_val), "ultra-power-saving")) {
140                         temp_mode |= PMINFO_SUPPORT_MODE_ULTRA_POWER_SAVING;    // PMINFO_MODE_PROP_ULTRA_POWER_SAVING  0x00000001
141                 }
142                 if (strstr(ASCII(attrib_val), "cool-down")) {
143                         temp_mode |= PMINFO_SUPPORT_MODE_COOL_DOWN;     // PMINFO_MODE_PROP_COOL_DOWN                   0x00000002
144                 }
145                 if (strstr(ASCII(attrib_val), "screen-reader")) {
146                         temp_mode |= PMINFO_SUPPORT_MODE_SCREEN_READER; // PMINFO_MODE_PROP_SCREEN_READER               0x00000004
147                 }
148                 sprintf(buffer, "%d", temp_mode);
149                 mfx->support_mode = strdup(buffer);
150                 xmlFree(attrib_val);
151         } else {
152                 mfx->support_mode = strdup("0");
153         }
154 }
155
156 static void __save_xml_supportfeature(const char *metadata_key, uiapplication_x *uiapplication)
157 {
158         if (metadata_key) {
159                 int temp_mode = 0;
160                 char buffer[PKG_STRING_LEN_MAX] = {'\0'};
161
162                 if (uiapplication->support_feature) {
163                         temp_mode = atoi(uiapplication->support_feature);
164                         FREE_AND_NULL(uiapplication->support_feature);
165                 }
166
167                 if (strcmp(metadata_key, "http://developer.samsung.com/tizen/metadata/multiwindow") == 0) {
168                         temp_mode |= PMINFO_SUPPORT_FEATURE_MULTI_WINDOW;
169                 } else if (strcmp(metadata_key, "http://developer.samsung.com/tizen/metadata/oomtermination") == 0) {
170                         temp_mode |= PMINFO_SUPPORT_FEATURE_OOM_TERMINATION;
171                 } else if (strcmp(metadata_key, "http://developer.samsung.com/tizen/metadata/largememory") == 0) {
172                         temp_mode |= PMINFO_SUPPORT_FEATURE_LARGE_MEMORY;
173                 }
174
175                 sprintf(buffer, "%d", temp_mode);
176                 uiapplication->support_feature = strdup(buffer);
177         } else {
178                 uiapplication->support_feature = strdup("0");
179         }
180 }
181
182 static void __save_xml_installedtime(manifest_x * mfx)
183 {
184         char buf[PKG_STRING_LEN_MAX] = {'\0'};
185         char *val = NULL;
186         time_t current_time;
187         time(&current_time);
188         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", (int)current_time);
189         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
190         mfx->installed_time = val;
191 }
192
193 static void __save_xml_defaultvalue(manifest_x * mfx)
194 {
195         mfx->preload = strdup("False");
196         mfx->removable = strdup("True");
197         mfx->readonly = strdup("False");
198         mfx->update = strdup("False");
199         mfx->system = strdup("False");
200         mfx->installed_storage= strdup("installed_internal");
201         package = mfx->package;
202 }
203
204 static char *__pkgid_to_manifest(const char *pkgid)
205 {
206         char *manifest;
207         int size;
208
209         if (pkgid == NULL) {
210                 _LOGE("pkgid is NULL");
211                 return NULL;
212         }
213
214         size = strlen(MANIFEST_RW_DIRECTORY) + strlen(pkgid) + 10;
215         manifest = malloc(size);
216         if (manifest == NULL) {
217                 _LOGE("No memory");
218                 return NULL;
219         }
220         memset(manifest, '\0', size);
221         snprintf(manifest, size, MANIFEST_RW_DIRECTORY "/%s.xml", pkgid);
222
223         if (access(manifest, F_OK)) {
224                 snprintf(manifest, size, MANIFEST_RO_DIRECTORY "/%s.xml", pkgid);
225         }
226
227         return manifest;
228 }
229
230 static int __next_child_element(xmlTextReaderPtr reader, int depth)
231 {
232         int ret = xmlTextReaderRead(reader);
233         int cur = xmlTextReaderDepth(reader);
234         while (ret == 1) {
235
236                 switch (xmlTextReaderNodeType(reader)) {
237                 case XML_READER_TYPE_ELEMENT:
238                         if (cur == depth + 1)
239                                 return 1;
240                         break;
241                 case XML_READER_TYPE_TEXT:
242                         /*text is handled by each function separately*/
243                         if (cur == depth + 1)
244                                 return 0;
245                         break;
246                 case XML_READER_TYPE_END_ELEMENT:
247                         if (cur == depth)
248                                 return 0;
249                         break;
250                 default:
251                         if (cur <= depth)
252                                 return 0;
253                         break;
254                 }
255                 ret = xmlTextReaderRead(reader);
256                 cur = xmlTextReaderDepth(reader);
257         }
258         return ret;
259 }
260
261 static int __check_action_fota(char *const tagv[])
262 {
263         int i = 0;
264         char delims[] = "=";
265         char *ret_result = NULL;
266         char *tag = NULL;
267         int ret = PM_PARSER_R_ERROR;
268
269         if (tagv == NULL)
270                 return ret;
271
272         for (tag = strdup(tagv[0]); tag != NULL; ) {
273                 ret_result = strtok(tag, delims);
274
275                 /*check tag :  fota is true */
276                 if (strcmp(ret_result, "fota") == 0) {
277                         ret_result = strtok(NULL, delims);
278                         if (strcmp(ret_result, "true") == 0) {
279                                 ret = PM_PARSER_R_OK;
280                         }
281                 } else
282                         _LOGD("tag process [%s]is not defined\n", ret_result);
283
284                 FREE_AND_NULL(tag);
285
286                 /*check next value*/
287                 if (tagv[++i] != NULL)
288                         tag = strdup(tagv[i]);
289                 else {
290                         _LOGD("tag process success...%d\n" , ret);
291                         return ret;
292                 }
293         }
294
295         return ret;
296 }
297
298 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
299 {
300         __save_xml_value(reader, &allowed->text);
301         return 0;
302 }
303
304 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
305 {
306         __save_xml_attribute(reader, "name", &operation->name, NULL);
307 /* Text does not exist. Only attribute exists
308         xmlTextReaderRead(reader);
309         if (xmlTextReaderValue(reader))
310                 operation->text = ASCII(xmlTextReaderValue(reader));
311 */
312         return 0;
313 }
314
315 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
316 {
317         __save_xml_attribute(reader, "name", &uri->name, NULL);
318 /* Text does not exist. Only attribute exists
319         xmlTextReaderRead(reader);
320         if (xmlTextReaderValue(reader))
321                 uri->text = ASCII(xmlTextReaderValue(reader));
322 */
323         return 0;
324 }
325
326 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
327 {
328         __save_xml_attribute(reader, "name", &mime->name, NULL);
329 /* Text does not exist. Only attribute exists
330         xmlTextReaderRead(reader);
331         if (xmlTextReaderValue(reader))
332                 mime->text = ASCII(xmlTextReaderValue(reader));
333 */
334         return 0;
335 }
336
337 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
338 {
339         __save_xml_attribute(reader, "name", &subapp->name, NULL);
340 /* Text does not exist. Only attribute exists
341         xmlTextReaderRead(reader);
342         if (xmlTextReaderValue(reader))
343                 mime->text = ASCII(xmlTextReaderValue(reader));
344 */
345         return 0;
346 }
347
348 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
349 {
350         __save_xml_attribute(reader, "name", &condition->name, NULL);
351         __save_xml_value(reader, &condition->text);
352         return 0;
353 }
354
355 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
356 {
357         __save_xml_attribute(reader, "name", &notification->name, NULL);
358         __save_xml_value(reader, &notification->text);
359         return 0;
360 }
361
362 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
363 {
364         __save_xml_attribute(reader, "name", &category->name, NULL);
365         return 0;
366 }
367
368 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
369 {
370         __save_xml_value(reader, &privilege->text);
371         return 0;
372 }
373
374 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
375 {
376         __save_xml_attribute(reader, "key", &metadata->key, NULL);
377         __save_xml_attribute(reader, "value", &metadata->value, NULL);
378         return 0;
379 }
380
381 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
382 {
383         __save_xml_attribute(reader, "type", &permission->type, NULL);
384         __save_xml_value(reader, &permission->value);
385
386         return 0;
387 }
388
389 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
390 {
391         __save_xml_attribute(reader, "name", &compatibility->name, NULL);
392         __save_xml_value(reader, &compatibility->text);
393
394         return 0;
395 }
396
397 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution)
398 {
399         __save_xml_attribute(reader, "mime-type", &resolution->mimetype, NULL);
400         __save_xml_attribute(reader, "uri-scheme", &resolution->urischeme, NULL);
401         return 0;
402 }
403
404 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
405 {
406         __save_xml_value(reader, &request->text);
407
408         return 0;
409 }
410
411 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
412 {
413         const xmlChar *node;
414         int ret = -1;
415         int depth = -1;
416         allowed_x *tmp1 = NULL;
417         request_x *tmp2 = NULL;
418
419         __save_xml_attribute(reader, "path", &define->path, NULL);
420
421         depth = xmlTextReaderDepth(reader);
422         while ((ret = __next_child_element(reader, depth))) {
423                 node = xmlTextReaderConstName(reader);
424                 if (!node) {
425                         _LOGD("xmlTextReaderConstName value is NULL\n");
426                         return -1;
427                 }
428
429                 if (!strcmp(ASCII(node), "allowed")) {
430                         allowed_x *allowed= malloc(sizeof(allowed_x));
431                         if (allowed == NULL) {
432                                 _LOGD("Malloc Failed\n");
433                                 return -1;
434                         }
435                         memset(allowed, '\0', sizeof(allowed_x));
436                         LISTADD(define->allowed, allowed);
437                         ret = __ps_process_allowed(reader, allowed);
438                 } else if (!strcmp(ASCII(node), "request")) {
439                         request_x *request = malloc(sizeof(request_x));
440                         if (request == NULL) {
441                                 _LOGD("Malloc Failed\n");
442                                 return -1;
443                         }
444                         memset(request, '\0', sizeof(request_x));
445                         LISTADD(define->request, request);
446                         ret = __ps_process_request(reader, request);
447                 } else
448                         return -1;
449                 if (ret < 0) {
450                         _LOGD("Processing define failed\n");
451                         return ret;
452                 }
453         }
454
455         SAFE_LISTHEAD(define->allowed, tmp1);
456         SAFE_LISTHEAD(define->request, tmp2);
457
458         return ret;
459 }
460
461 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
462 {
463         const xmlChar *node;
464         int ret = -1;
465         int depth = -1;
466         operation_x *tmp1 = NULL;
467         uri_x *tmp2 = NULL;
468         mime_x *tmp3 = NULL;
469         subapp_x *tmp4 = NULL;
470
471         depth = xmlTextReaderDepth(reader);
472         while ((ret = __next_child_element(reader, depth))) {
473                 node = xmlTextReaderConstName(reader);
474                 if (!node) {
475                         _LOGD("xmlTextReaderConstName value is NULL\n");
476                         return -1;
477                 }
478
479                 if (!strcmp(ASCII(node), "operation")) {
480                         operation_x *operation = malloc(sizeof(operation_x));
481                         if (operation == NULL) {
482                                 _LOGD("Malloc Failed\n");
483                                 return -1;
484                         }
485                         memset(operation, '\0', sizeof(operation_x));
486                         LISTADD(appsvc->operation, operation);
487                         ret = __ps_process_operation(reader, operation);
488                         _LOGD("operation processing\n");
489                 } else if (!strcmp(ASCII(node), "uri")) {
490                         uri_x *uri= malloc(sizeof(uri_x));
491                         if (uri == NULL) {
492                                 _LOGD("Malloc Failed\n");
493                                 return -1;
494                         }
495                         memset(uri, '\0', sizeof(uri_x));
496                         LISTADD(appsvc->uri, uri);
497                         ret = __ps_process_uri(reader, uri);
498                         _LOGD("uri processing\n");
499                 } else if (!strcmp(ASCII(node), "mime")) {
500                         mime_x *mime = malloc(sizeof(mime_x));
501                         if (mime == NULL) {
502                                 _LOGD("Malloc Failed\n");
503                                 return -1;
504                         }
505                         memset(mime, '\0', sizeof(mime_x));
506                         LISTADD(appsvc->mime, mime);
507                         ret = __ps_process_mime(reader, mime);
508                         _LOGD("mime processing\n");
509                 } else if (!strcmp(ASCII(node), "subapp")) {
510                         subapp_x *subapp = malloc(sizeof(subapp_x));
511                         if (subapp == NULL) {
512                                 _LOGD("Malloc Failed\n");
513                                 return -1;
514                         }
515                         memset(subapp, '\0', sizeof(subapp_x));
516                         LISTADD(appsvc->subapp, subapp);
517                         ret = __ps_process_subapp(reader, subapp);
518                         _LOGD("subapp processing\n");
519                 } else
520                         return -1;
521                 if (ret < 0) {
522                         _LOGD("Processing appsvc failed\n");
523                         return ret;
524                 }
525         }
526
527         SAFE_LISTHEAD(appsvc->operation, tmp1);
528         SAFE_LISTHEAD(appsvc->uri, tmp2);
529         SAFE_LISTHEAD(appsvc->mime, tmp3);
530         SAFE_LISTHEAD(appsvc->subapp, tmp4);
531
532         __save_xml_value(reader, &appsvc->text);
533
534         return ret;
535 }
536
537
538 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges)
539 {
540         const xmlChar *node;
541         int ret = -1;
542         int depth = -1;
543         privilege_x *tmp1 = NULL;
544
545         depth = xmlTextReaderDepth(reader);
546         while ((ret = __next_child_element(reader, depth))) {
547                 node = xmlTextReaderConstName(reader);
548                 if (!node) {
549                         _LOGD("xmlTextReaderConstName value is NULL\n");
550                         return -1;
551                 }
552
553                 if (strcmp(ASCII(node), "privilege") == 0) {
554                         privilege_x *privilege = malloc(sizeof(privilege_x));
555                         if (privilege == NULL) {
556                                 _LOGD("Malloc Failed\n");
557                                 return -1;
558                         }
559                         memset(privilege, '\0', sizeof(privilege_x));
560                         LISTADD(privileges->privilege, privilege);
561                         ret = __ps_process_privilege(reader, privilege);
562                 } else
563                         return -1;
564                 if (ret < 0) {
565                         _LOGD("Processing privileges failed\n");
566                         return ret;
567                 }
568         }
569         SAFE_LISTHEAD(privileges->privilege, tmp1);
570         return ret;
571 }
572
573 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
574 {
575         const xmlChar *node;
576         int ret = -1;
577         int depth = -1;
578         condition_x *tmp1 = NULL;
579
580         depth = xmlTextReaderDepth(reader);
581         while ((ret = __next_child_element(reader, depth))) {
582                 node = xmlTextReaderConstName(reader);
583                 if (!node) {
584                         _LOGD("xmlTextReaderConstName value is NULL\n");
585                         return -1;
586                 }
587
588                 if (strcmp(ASCII(node), "condition") == 0) {
589                         condition_x *condition = malloc(sizeof(condition_x));
590                         if (condition == NULL) {
591                                 _LOGD("Malloc Failed\n");
592                                 return -1;
593                         }
594                         memset(condition, '\0', sizeof(condition_x));
595                         LISTADD(launchconditions->condition, condition);
596                         ret = __ps_process_condition(reader, condition);
597                 } else
598                         return -1;
599                 if (ret < 0) {
600                         _LOGD("Processing launchconditions failed\n");
601                         return ret;
602                 }
603         }
604
605         SAFE_LISTHEAD(launchconditions->condition, tmp1);
606
607         __save_xml_value(reader, &launchconditions->text);
608
609         return ret;
610 }
611
612 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
613 {
614         const xmlChar *node;
615         int ret = -1;
616         int depth = -1;
617         define_x *tmp1 = NULL;
618         request_x *tmp2 = NULL;
619         depth = xmlTextReaderDepth(reader);
620         while ((ret = __next_child_element(reader, depth))) {
621                 node = xmlTextReaderConstName(reader);
622                 if (!node) {
623                         _LOGD("xmlTextReaderConstName value is NULL\n");
624                         return -1;
625                 }
626
627                 if (!strcmp(ASCII(node), "define")) {
628                         define_x *define= malloc(sizeof(define_x));
629                         if (define == NULL) {
630                                 _LOGD("Malloc Failed\n");
631                                 return -1;
632                         }
633                         memset(define, '\0', sizeof(define_x));
634                         LISTADD(datashare->define, define);
635                         ret = __ps_process_define(reader, define);
636                 } else if (!strcmp(ASCII(node), "request")) {
637                         request_x *request= malloc(sizeof(request_x));
638                         if (request == NULL) {
639                                 _LOGD("Malloc Failed\n");
640                                 return -1;
641                         }
642                         memset(request, '\0', sizeof(request_x));
643                         LISTADD(datashare->request, request);
644                         ret = __ps_process_request(reader, request);
645                 } else
646                         return -1;
647                 if (ret < 0) {
648                         _LOGD("Processing data-share failed\n");
649                         return ret;
650                 }
651         }
652
653         SAFE_LISTHEAD(datashare->define, tmp1);
654         SAFE_LISTHEAD(datashare->request, tmp2);
655
656         return ret;
657 }
658
659 static char*
660 __get_icon_with_path(const char* icon)
661 {
662         if (!icon)
663                 return NULL;
664
665         if (index(icon, '/') == NULL) {
666                 char* theme = NULL;
667                 char* icon_with_path = NULL;
668                 int len;
669
670                 if (!package)
671                         return NULL;
672
673                 theme = strdup("default");
674                 if(theme == NULL){
675                         _LOGE("@malloc failed!!!");
676                         return NULL;
677                 }
678
679                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
680                 icon_with_path = malloc(len);
681                 if(icon_with_path == NULL) {
682                         _LOGD("(icon_with_path == NULL) return\n");
683                         FREE_AND_NULL(theme);
684                         return NULL;
685                 }
686
687                 memset(icon_with_path, 0, len);
688                 snprintf(icon_with_path, len, "/opt/usr/apps/%s/shared/res/icons/default/small/%s", package, icon);
689                 do {
690                         if (access(icon_with_path, R_OK) == 0) break;
691                         snprintf(icon_with_path, len, "/opt/share/icons/%s/small/%s", theme, icon);
692                         if (access(icon_with_path, R_OK) == 0) break;
693                         snprintf(icon_with_path, len, "/usr/share/icons/%s/small/%s", theme, icon);
694                         if (access(icon_with_path, R_OK) == 0) break;
695                         snprintf(icon_with_path, len,"/opt/share/icons/default/small/%s", icon);
696                         if (access(icon_with_path, R_OK) == 0) break;
697                         snprintf(icon_with_path, len, "/usr/share/icons/default/small/%s", icon);
698                         if (access(icon_with_path, R_OK) == 0) break;
699
700                         /* icon path is going to be moved intto the app directory */
701                         snprintf(icon_with_path, len, "/usr/apps/%s/shared/res/icons/default/small/%s", package, icon);
702                         if (access(icon_with_path, R_OK) == 0) break;
703                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/%s/small/%s", package, theme, icon);
704                         if (access(icon_with_path, R_OK) == 0) break;
705                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/%s/small/%s", package, theme, icon);
706                         if (access(icon_with_path, R_OK) == 0) break;
707                         snprintf(icon_with_path, len, "/opt/apps/%s/res/icons/default/small/%s", package, icon);
708                         if (access(icon_with_path, R_OK) == 0) break;
709                         snprintf(icon_with_path, len, "/usr/apps/%s/res/icons/default/small/%s", package, icon);
710                         if (access(icon_with_path, R_OK) == 0) break;
711                         snprintf(icon_with_path, len, "/usr/ug/res/images/%s/%s", package, icon);
712                         if (access(icon_with_path, R_OK) == 0) break;
713                 } while (0);
714
715                 FREE_AND_NULL(theme);
716
717                 /* If icon is not exist, return NULL */
718                 if (access(icon_with_path, R_OK) != 0) {
719                         _LOGE("Icon is not exist");
720                         FREE_AND_NULL(icon_with_path);
721                         return NULL;
722                 } else {
723                         _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
724                         return icon_with_path;
725                 }
726         } else {
727                 char* confirmed_icon = NULL;
728
729                 confirmed_icon = strdup(icon);
730                 if (!confirmed_icon)
731                         return NULL;
732                 return confirmed_icon;
733         }
734 }
735
736 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
737 {
738         int i = 0;
739         char delims[] = "=";
740         char *ret_result = NULL;
741         char *tag = NULL;
742
743         if (tagv == NULL)
744                 return;
745
746         for (tag = strdup(tagv[0]); tag != NULL; ) {
747                 ret_result = strtok(tag, delims);
748
749                 /*check tag :  preload */
750                 if (strcmp(ret_result, "preload") == 0) {
751                         ret_result = strtok(NULL, delims);
752                         if (strcmp(ret_result, "true") == 0) {
753                                 FREE_AND_NULL(mfx->preload);
754                                 mfx->preload = strdup("true");
755                         } else if (strcmp(ret_result, "false") == 0) {
756                                 FREE_AND_NULL(mfx->preload);
757                                 mfx->preload = strdup("false");
758                         }
759                 /*check tag :  removable*/
760                 } else if (strcmp(ret_result, "removable") == 0) {
761                         ret_result = strtok(NULL, delims);
762                         if (strcmp(ret_result, "true") == 0){
763                                 FREE_AND_NULL(mfx->removable);
764                                 mfx->removable = strdup("true");
765                         } else if (strcmp(ret_result, "false") == 0) {
766                                 FREE_AND_NULL(mfx->removable);
767                                 mfx->removable = strdup("false");
768                         }
769                 /*check tag :  not matched*/
770                 } else
771                         _LOGD("tag process [%s]is not defined\n", ret_result);
772
773                 free(tag);
774
775                 /*check next value*/
776                 if (tagv[++i] != NULL)
777                         tag = strdup(tagv[i]);
778                 else {
779                         /*update tag :  system*/
780                         if ((strcmp(mfx->preload,"true")==0) && (strcmp(mfx->removable,"false")==0)){
781                                 FREE_AND_NULL(mfx->system);
782                                 mfx->system = strdup("true");
783                         }
784                         _LOGD("tag process success...\n");
785                         return;
786                 }
787         }
788 }
789
790 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon)
791 {
792         __save_xml_attribute(reader, "name", &icon->name, NULL);
793         __save_xml_attribute(reader, "section", &icon->section, NULL);
794         __save_xml_attribute(reader, "size", &icon->size, NULL);
795         __save_xml_attribute(reader, "resolution", &icon->resolution, NULL);
796         __save_xml_lang(reader, &icon->lang);
797
798         xmlTextReaderRead(reader);
799         const char *text  = ASCII(xmlTextReaderValue(reader));
800         if(text) {
801                 icon->text = (const char *)__get_icon_with_path(text);
802                 FREE_AND_NULL(text);
803         }
804
805         return 0;
806 }
807
808 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
809 {
810         __save_xml_attribute(reader, "name", &image->name, NULL);
811         __save_xml_attribute(reader, "section", &image->section, NULL);
812         __save_xml_lang(reader, &image->lang);
813         __save_xml_value(reader, &image->text);
814
815         return 0;
816 }
817
818 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
819 {
820         __save_xml_attribute(reader, "name", &label->name, NULL);
821         __save_xml_lang(reader, &label->lang);
822         __save_xml_value(reader, &label->text);
823
824 /*      _LOGD("lable name %s\n", label->name);
825         _LOGD("lable lang %s\n", label->lang);
826         _LOGD("lable text %s\n", label->text);
827 */
828         return 0;
829
830 }
831
832 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
833 {
834         __save_xml_attribute(reader, "email", &author->email, NULL);
835         __save_xml_attribute(reader, "href", &author->href, NULL);
836         __save_xml_lang(reader, &author->lang);
837         __save_xml_value(reader, &author->text);
838
839         return 0;
840 }
841
842 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
843 {
844         __save_xml_lang(reader, &description->lang);
845         __save_xml_value(reader, &description->text);
846
847         return 0;
848 }
849
850 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
851 {
852         __save_xml_lang(reader, &license->lang);
853         __save_xml_value(reader, &license->text);
854
855         return 0;
856 }
857
858 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability)
859 {
860         const xmlChar *node;
861         int ret = -1;
862         int depth = -1;
863         resolution_x *tmp1 = NULL;
864
865         __save_xml_attribute(reader, "operation-id", &capability->operationid, NULL);
866
867         depth = xmlTextReaderDepth(reader);
868         while ((ret = __next_child_element(reader, depth))) {
869                 node = xmlTextReaderConstName(reader);
870                 if (!node) {
871                         _LOGD("xmlTextReaderConstName value is NULL\n");
872                         return -1;
873                 }
874
875                 if (!strcmp(ASCII(node), "resolution")) {
876                         resolution_x *resolution = malloc(sizeof(resolution_x));
877                         if (resolution == NULL) {
878                                 _LOGD("Malloc Failed\n");
879                                 return -1;
880                         }
881                         memset(resolution, '\0', sizeof(resolution_x));
882                         LISTADD(capability->resolution, resolution);
883                         ret = __ps_process_resolution(reader, resolution);
884                 } else
885                         return -1;
886                 if (ret < 0) {
887                         _LOGD("Processing capability failed\n");
888                         return ret;
889                 }
890         }
891
892         SAFE_LISTHEAD(capability->resolution, tmp1);
893
894         return ret;
895 }
896
897 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
898 {
899         __save_xml_attribute(reader, "providerid", &datacontrol->providerid, NULL);
900         __save_xml_attribute(reader, "access", &datacontrol->access, NULL);
901         __save_xml_attribute(reader, "type", &datacontrol->type, NULL);
902
903         return 0;
904 }
905
906 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication)
907 {
908         const xmlChar *node;
909         int ret = -1;
910         int depth = -1;
911         label_x *tmp1 = NULL;
912         icon_x *tmp2 = NULL;
913         appsvc_x *tmp3 = NULL;
914         launchconditions_x *tmp4 = NULL;
915         notification_x *tmp5 = NULL;
916         datashare_x *tmp6 = NULL;
917         category_x *tmp7 = NULL;
918         metadata_x *tmp8 = NULL;
919         image_x *tmp9 = NULL;
920         permission_x *tmp10 = NULL;
921         datacontrol_x *tmp11 = NULL;
922
923         __save_xml_attribute(reader, "appid", &uiapplication->appid, NULL);
924         retvm_if(uiapplication->appid == NULL, PM_PARSER_R_ERROR, "appid cant be NULL, appid field is mandatory\n");
925         __save_xml_attribute(reader, "exec", &uiapplication->exec, NULL);
926         __save_xml_attribute(reader, "nodisplay", &uiapplication->nodisplay, "false");
927         __save_xml_attribute(reader, "multiple", &uiapplication->multiple, "false");
928         __save_xml_attribute(reader, "type", &uiapplication->type, NULL);
929         __save_xml_attribute(reader, "categories", &uiapplication->categories, NULL);
930         __save_xml_attribute(reader, "extraid", &uiapplication->extraid, NULL);
931         __save_xml_attribute(reader, "taskmanage", &uiapplication->taskmanage, "true");
932         __save_xml_attribute(reader, "enabled", &uiapplication->enabled, "true");
933         __save_xml_attribute(reader, "hw-acceleration", &uiapplication->hwacceleration, "use-system-setting");
934         __save_xml_attribute(reader, "screen-reader", &uiapplication->screenreader, "use-system-setting");
935         __save_xml_attribute(reader, "mainapp", &uiapplication->mainapp, "false");
936         __save_xml_attribute(reader, "recentimage", &uiapplication->recentimage, "false");
937         __save_xml_attribute(reader, "launchcondition", &uiapplication->launchcondition, "false");
938         __save_xml_attribute(reader, "indicatordisplay", &uiapplication->indicatordisplay, "true");
939         __save_xml_attribute(reader, "portrait-effectimage", &uiapplication->portraitimg, NULL);
940         __save_xml_attribute(reader, "landscape-effectimage", &uiapplication->landscapeimg, NULL);
941         __save_xml_attribute(reader, "effectimage-type", &uiapplication->effectimage_type, "image");
942         __save_xml_attribute(reader, "guestmode-visibility", &uiapplication->guestmode_visibility, "true");
943         __save_xml_attribute(reader, "permission-type", &uiapplication->permission_type, "normal");
944         __save_xml_attribute(reader, "component-type", &uiapplication->component_type, "uiapp");
945         /*component_type has "svcapp" or "uiapp", if it is not, parsing manifest is fail*/
946         retvm_if(((strcmp(uiapplication->component_type, "svcapp") != 0) && (strcmp(uiapplication->component_type, "uiapp") != 0)), PM_PARSER_R_ERROR, "invalid component_type[%s]\n", uiapplication->component_type);
947         __save_xml_attribute(reader, "submode", &uiapplication->submode, "false");
948         __save_xml_attribute(reader, "submode-mainid", &uiapplication->submode_mainid, NULL);
949         __save_xml_attribute(reader, "process-pool", &uiapplication->process_pool, "false");
950         __save_xml_attribute(reader, "auto-restart", &uiapplication->autorestart, "false");
951         __save_xml_attribute(reader, "on-boot", &uiapplication->onboot, "false");
952         __save_xml_attribute(reader, "multi-instance", &uiapplication->multi_instance, "false");
953         __save_xml_attribute(reader, "multi-instance-mainid", &uiapplication->multi_instance_mainid, NULL);
954         __save_xml_attribute(reader, "ui-gadget", &uiapplication->ui_gadget, "false");
955         uiapplication->multi_window = strdup("false");
956         uiapplication->package= strdup(package);
957
958         depth = xmlTextReaderDepth(reader);
959         while ((ret = __next_child_element(reader, depth))) {
960                 node = xmlTextReaderConstName(reader);
961                 if (!node) {
962                         _LOGD("xmlTextReaderConstName value is NULL\n");
963                         return -1;
964                 }
965                 if (!strcmp(ASCII(node), "label")) {
966                         label_x *label = malloc(sizeof(label_x));
967                         if (label == NULL) {
968                                 _LOGD("Malloc Failed\n");
969                                 return -1;
970                         }
971                         memset(label, '\0', sizeof(label_x));
972                         LISTADD(uiapplication->label, label);
973                         ret = __ps_process_label(reader, label);
974                 } else if (!strcmp(ASCII(node), "icon")) {
975                         icon_x *icon = malloc(sizeof(icon_x));
976                         if (icon == NULL) {
977                                 _LOGD("Malloc Failed\n");
978                                 return -1;
979                         }
980                         memset(icon, '\0', sizeof(icon_x));
981                         LISTADD(uiapplication->icon, icon);
982                         ret = __ps_process_icon(reader, icon);
983                 } else if (!strcmp(ASCII(node), "image")) {
984                         image_x *image = malloc(sizeof(image_x));
985                         if (image == NULL) {
986                                 _LOGD("Malloc Failed\n");
987                                 return -1;
988                         }
989                         memset(image, '\0', sizeof(image_x));
990                         LISTADD(uiapplication->image, image);
991                         ret = __ps_process_image(reader, image);
992                 } else if (!strcmp(ASCII(node), "category")) {
993                         category_x *category = malloc(sizeof(category_x));
994                         if (category == NULL) {
995                                 _LOGD("Malloc Failed\n");
996                                 return -1;
997                         }
998                         memset(category, '\0', sizeof(category_x));
999                         LISTADD(uiapplication->category, category);
1000                         ret = __ps_process_category(reader, category);
1001                 } else if (!strcmp(ASCII(node), "metadata")) {
1002                         metadata_x *metadata = malloc(sizeof(metadata_x));
1003                         if (metadata == NULL) {
1004                                 _LOGD("Malloc Failed\n");
1005                                 return -1;
1006                         }
1007                         memset(metadata, '\0', sizeof(metadata_x));
1008                         LISTADD(uiapplication->metadata, metadata);
1009                         ret = __ps_process_metadata(reader, metadata);
1010                         // multiwindow check
1011                         if (strcmp(metadata->key, "http://developer.samsung.com/tizen/metadata/multiwindow") == 0)
1012                                 FREE_AND_STRDUP("true", uiapplication->multi_window);
1013
1014                         __save_xml_supportfeature(metadata->key, uiapplication);
1015                 } else if (!strcmp(ASCII(node), "permission")) {
1016                         permission_x *permission = malloc(sizeof(permission_x));
1017                         if (permission == NULL) {
1018                                 _LOGD("Malloc Failed\n");
1019                                 return -1;
1020                         }
1021                         memset(permission, '\0', sizeof(permission_x));
1022                         LISTADD(uiapplication->permission, permission);
1023                         ret = __ps_process_permission(reader, permission);
1024                 } else if (!strcmp(ASCII(node), "application-service")) {
1025                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
1026                         if (appsvc == NULL) {
1027                                 _LOGD("Malloc Failed\n");
1028                                 return -1;
1029                         }
1030                         memset(appsvc, '\0', sizeof(appsvc_x));
1031                         LISTADD(uiapplication->appsvc, appsvc);
1032                         ret = __ps_process_appsvc(reader, appsvc);
1033                 } else if (!strcmp(ASCII(node), "app-control")) {
1034                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
1035                         if (appsvc == NULL) {
1036                                 _LOGD("Malloc Failed\n");
1037                                 return -1;
1038                         }
1039                         memset(appsvc, '\0', sizeof(appsvc_x));
1040                         LISTADD(uiapplication->appsvc, appsvc);
1041                         ret = __ps_process_appsvc(reader, appsvc);
1042                 } else if (!strcmp(ASCII(node), "data-share")) {
1043                         datashare_x *datashare = malloc(sizeof(datashare_x));
1044                         if (datashare == NULL) {
1045                                 _LOGD("Malloc Failed\n");
1046                                 return -1;
1047                         }
1048                         memset(datashare, '\0', sizeof(datashare_x));
1049                         LISTADD(uiapplication->datashare, datashare);
1050                         ret = __ps_process_datashare(reader, datashare);
1051                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
1052                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
1053                         if (launchconditions == NULL) {
1054                                 _LOGD("Malloc Failed\n");
1055                                 return -1;
1056                         }
1057                         memset(launchconditions, '\0', sizeof(launchconditions_x));
1058                         LISTADD(uiapplication->launchconditions, launchconditions);
1059                         ret = __ps_process_launchconditions(reader, launchconditions);
1060                 } else if (!strcmp(ASCII(node), "notification")) {
1061                         notification_x *notification = malloc(sizeof(notification_x));
1062                         if (notification == NULL) {
1063                                 _LOGD("Malloc Failed\n");
1064                                 return -1;
1065                         }
1066                         memset(notification, '\0', sizeof(notification_x));
1067                         LISTADD(uiapplication->notification, notification);
1068                         ret = __ps_process_notification(reader, notification);
1069                 } else if (!strcmp(ASCII(node), "datacontrol")) {
1070                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
1071                         if (datacontrol == NULL) {
1072                                 _LOGD("Malloc Failed\n");
1073                                 return -1;
1074                         }
1075                         memset(datacontrol, '\0', sizeof(datacontrol_x));
1076                         LISTADD(uiapplication->datacontrol, datacontrol);
1077                         ret = __ps_process_datacontrol(reader, datacontrol);
1078                 } else
1079                         return -1;
1080                 if (ret < 0) {
1081                         _LOGD("Processing uiapplication failed\n");
1082                         return ret;
1083                 }
1084         }
1085
1086         SAFE_LISTHEAD(uiapplication->label, tmp1);
1087         SAFE_LISTHEAD(uiapplication->icon, tmp2);
1088         SAFE_LISTHEAD(uiapplication->appsvc, tmp3);
1089         SAFE_LISTHEAD(uiapplication->launchconditions, tmp4);
1090         SAFE_LISTHEAD(uiapplication->notification, tmp5);
1091         SAFE_LISTHEAD(uiapplication->datashare, tmp6);
1092         SAFE_LISTHEAD(uiapplication->category, tmp7);
1093         SAFE_LISTHEAD(uiapplication->metadata, tmp8);
1094         SAFE_LISTHEAD(uiapplication->image, tmp9);
1095         SAFE_LISTHEAD(uiapplication->permission, tmp10);
1096         SAFE_LISTHEAD(uiapplication->datacontrol, tmp11);
1097
1098         return ret;
1099 }
1100
1101 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
1102 {
1103         /*TODO: once policy is set*/
1104         __save_xml_attribute(reader, "name", &deviceprofile->name, NULL);
1105         return 0;
1106 }
1107
1108 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
1109 {
1110         /*TODO: once policy is set*/
1111         return 0;
1112 }
1113
1114 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
1115 {
1116         /*TODO: once policy is set*/
1117         return 0;
1118 }
1119
1120 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
1121 {
1122         /*TODO: once policy is set*/
1123         return 0;
1124 }
1125
1126 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
1127 {
1128         /*TODO: once policy is set*/
1129         return 0;
1130 }
1131
1132 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx)
1133 {
1134         _LOGD("__start_process\n");
1135         const xmlChar *node;
1136         int ret = -1;
1137         int depth = -1;
1138         label_x *tmp1 = NULL;
1139         author_x *tmp2 = NULL;
1140         description_x *tmp3 = NULL;
1141         license_x *tmp4 = NULL;
1142         uiapplication_x *tmp5 = NULL;
1143         daemon_x *tmp6 = NULL;
1144         theme_x *tmp7 = NULL;
1145         font_x *tmp8 = NULL;
1146         ime_x *tmp9 = NULL;
1147         icon_x *tmp10 = NULL;
1148         compatibility_x *tmp11 = NULL;
1149         deviceprofile_x *tmp12 = NULL;
1150         privileges_x *tmp13 = NULL;
1151
1152         depth = xmlTextReaderDepth(reader);
1153         while ((ret = __next_child_element(reader, depth))) {
1154                 node = xmlTextReaderConstName(reader);
1155                 if (!node) {
1156                         _LOGD("xmlTextReaderConstName value is NULL\n");
1157                         return -1;
1158                 }
1159
1160                 if (!strcmp(ASCII(node), "label")) {
1161                         label_x *label = malloc(sizeof(label_x));
1162                         if (label == NULL) {
1163                                 _LOGD("Malloc Failed\n");
1164                                 return -1;
1165                         }
1166                         memset(label, '\0', sizeof(label_x));
1167                         LISTADD(mfx->label, label);
1168                         ret = __ps_process_label(reader, label);
1169                 } else if (!strcmp(ASCII(node), "author")) {
1170                         author_x *author = malloc(sizeof(author_x));
1171                         if (author == NULL) {
1172                                 _LOGD("Malloc Failed\n");
1173                                 return -1;
1174                         }
1175                         memset(author, '\0', sizeof(author_x));
1176                         LISTADD(mfx->author, author);
1177                         ret = __ps_process_author(reader, author);
1178                 } else if (!strcmp(ASCII(node), "description")) {
1179                         description_x *description = malloc(sizeof(description_x));
1180                         if (description == NULL) {
1181                                 _LOGD("Malloc Failed\n");
1182                                 return -1;
1183                         }
1184                         memset(description, '\0', sizeof(description_x));
1185                         LISTADD(mfx->description, description);
1186                         ret = __ps_process_description(reader, description);
1187                 } else if (!strcmp(ASCII(node), "license")) {
1188                         license_x *license = malloc(sizeof(license_x));
1189                         if (license == NULL) {
1190                                 _LOGD("Malloc Failed\n");
1191                                 return -1;
1192                         }
1193                         memset(license, '\0', sizeof(license_x));
1194                         LISTADD(mfx->license, license);
1195                         ret = __ps_process_license(reader, license);
1196                 } else if (!strcmp(ASCII(node), "privileges")) {
1197                         privileges_x *privileges = malloc(sizeof(privileges_x));
1198                         if (privileges == NULL) {
1199                                 _LOGD("Malloc Failed\n");
1200                                 return -1;
1201                         }
1202                         memset(privileges, '\0', sizeof(privileges_x));
1203                         LISTADD(mfx->privileges, privileges);
1204                         ret = __ps_process_privileges(reader, privileges);
1205                 } else if (!strcmp(ASCII(node), "ui-application")) {
1206                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
1207                         if (uiapplication == NULL) {
1208                                 _LOGD("Malloc Failed\n");
1209                                 return -1;
1210                         }
1211                         memset(uiapplication, '\0', sizeof(uiapplication_x));
1212                         LISTADD(mfx->uiapplication, uiapplication);
1213                         ret = __ps_process_uiapplication(reader, uiapplication);
1214                 } else if (!strcmp(ASCII(node), "service-application")) {
1215                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
1216                         if (uiapplication == NULL) {
1217                                 _LOGD("Malloc Failed\n");
1218                                 return -1;
1219                         }
1220                         memset(uiapplication, '\0', sizeof(uiapplication_x));
1221                         LISTADD(mfx->uiapplication, uiapplication);
1222                         ret = __ps_process_uiapplication(reader, uiapplication);
1223                         FREE_AND_STRDUP("svcapp", uiapplication->component_type);
1224                 } else if (!strcmp(ASCII(node), "daemon")) {
1225                         daemon_x *daemon = malloc(sizeof(daemon_x));
1226                         if (daemon == NULL) {
1227                                 _LOGD("Malloc Failed\n");
1228                                 return -1;
1229                         }
1230                         memset(daemon, '\0', sizeof(daemon_x));
1231                         LISTADD(mfx->daemon, daemon);
1232                         ret = __ps_process_daemon(reader, daemon);
1233                 } else if (!strcmp(ASCII(node), "theme")) {
1234                         theme_x *theme = malloc(sizeof(theme_x));
1235                         if (theme == NULL) {
1236                                 _LOGD("Malloc Failed\n");
1237                                 return -1;
1238                         }
1239                         memset(theme, '\0', sizeof(theme_x));
1240                         LISTADD(mfx->theme, theme);
1241                         ret = __ps_process_theme(reader, theme);
1242                 } else if (!strcmp(ASCII(node), "font")) {
1243                         font_x *font = malloc(sizeof(font_x));
1244                         if (font == NULL) {
1245                                 _LOGD("Malloc Failed\n");
1246                                 return -1;
1247                         }
1248                         memset(font, '\0', sizeof(font_x));
1249                         LISTADD(mfx->font, font);
1250                         ret = __ps_process_font(reader, font);
1251                 } else if (!strcmp(ASCII(node), "ime")) {
1252                         ime_x *ime = malloc(sizeof(ime_x));
1253                         if (ime == NULL) {
1254                                 _LOGD("Malloc Failed\n");
1255                                 return -1;
1256                         }
1257                         memset(ime, '\0', sizeof(ime_x));
1258                         LISTADD(mfx->ime, ime);
1259                         ret = __ps_process_ime(reader, ime);
1260                 } else if (!strcmp(ASCII(node), "icon")) {
1261                         icon_x *icon = malloc(sizeof(icon_x));
1262                         if (icon == NULL) {
1263                                 _LOGD("Malloc Failed\n");
1264                                 return -1;
1265                         }
1266                         memset(icon, '\0', sizeof(icon_x));
1267                         LISTADD(mfx->icon, icon);
1268                         ret = __ps_process_icon(reader, icon);
1269                 } else if (!strcmp(ASCII(node), "profile")) {
1270                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
1271                         if (deviceprofile == NULL) {
1272                                 _LOGD("Malloc Failed\n");
1273                                 return -1;
1274                         }
1275                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
1276                         LISTADD(mfx->deviceprofile, deviceprofile);
1277                         ret = __ps_process_deviceprofile(reader, deviceprofile);
1278                 } else if (!strcmp(ASCII(node), "compatibility")) {
1279                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
1280                         if (compatibility == NULL) {
1281                                 _LOGD("Malloc Failed\n");
1282                                 return -1;
1283                         }
1284                         memset(compatibility, '\0', sizeof(compatibility_x));
1285                         LISTADD(mfx->compatibility, compatibility);
1286                         ret = __ps_process_compatibility(reader, compatibility);
1287                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
1288                         continue;
1289                 } else if (!strcmp(ASCII(node), "livebox")) {   /**< @todo will be deprecated soon */
1290                         continue;
1291                 } else if (!strcmp(ASCII(node), "dynamicbox")) {
1292                         continue;
1293                 } else if (!strcmp(ASCII(node), "account")) {
1294                         continue;
1295                 } else if (!strcmp(ASCII(node), "notifications")) {
1296                         continue;
1297                 } else if (!strcmp(ASCII(node), "ime")) {
1298                         continue;
1299                 } else
1300                         return -1;
1301
1302                 if (ret < 0) {
1303                         _LOGD("Processing manifest failed\n");
1304                         return ret;
1305                 }
1306         }
1307
1308         SAFE_LISTHEAD(mfx->label, tmp1);
1309         SAFE_LISTHEAD(mfx->author, tmp2);
1310         SAFE_LISTHEAD(mfx->description, tmp3);
1311         SAFE_LISTHEAD(mfx->license, tmp4);
1312         SAFE_LISTHEAD(mfx->uiapplication, tmp5);
1313         SAFE_LISTHEAD(mfx->daemon, tmp6);
1314         SAFE_LISTHEAD(mfx->theme, tmp7);
1315         SAFE_LISTHEAD(mfx->font, tmp8);
1316         SAFE_LISTHEAD(mfx->ime, tmp9);
1317         SAFE_LISTHEAD(mfx->icon, tmp10);
1318         SAFE_LISTHEAD(mfx->compatibility, tmp11);
1319         SAFE_LISTHEAD(mfx->deviceprofile, tmp12);
1320         SAFE_LISTHEAD(mfx->privileges, tmp13);
1321
1322         return ret;
1323 }
1324
1325 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx)
1326 {
1327         const xmlChar *node;
1328         int ret = -1;
1329
1330         if ((ret = __next_child_element(reader, -1))) {
1331                 node = xmlTextReaderConstName(reader);
1332                 retvm_if(!node, PM_PARSER_R_ERROR, "xmlTextReaderConstName value is NULL\n");
1333
1334                 if (!strcmp(ASCII(node), "manifest")) {
1335                         __save_xml_attribute(reader, "xmlns", &mfx->ns, NULL);
1336                         __save_xml_attribute(reader, "package", &mfx->package, NULL);
1337                         retvm_if(mfx->package == NULL, PM_PARSER_R_ERROR, "package cant be NULL, package field is mandatory\n");
1338                         __save_xml_attribute(reader, "version", &mfx->version, NULL);
1339                         __save_xml_attribute(reader, "size", &mfx->package_size, NULL);
1340                         __save_xml_attribute(reader, "install-location", &mfx->installlocation, "internal-only");
1341                         __save_xml_attribute(reader, "type", &mfx->type, NULL);
1342                         __save_xml_attribute(reader, "root_path", &mfx->root_path, NULL);
1343                         __save_xml_attribute(reader, "csc_path", &mfx->csc_path, NULL);
1344                         __save_xml_attribute(reader, "appsetting", &mfx->appsetting, "false");
1345                         __save_xml_attribute(reader, "storeclient-id", &mfx->storeclient_id, NULL);
1346                         __save_xml_attribute(reader, "nodisplay-setting", &mfx->nodisplay_setting, "false");
1347                         __save_xml_attribute(reader, "url", &mfx->package_url, NULL);
1348                         __save_xml_attribute(reader, "support-disable", &mfx->support_disable, "false");
1349                         __save_xml_attribute(reader, "mother-package", &mfx->mother_package, "false");
1350
1351                         /* need to check */
1352                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("support-reset"))) {
1353                                 mfx->support_reset = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("support-reset")));
1354                                 mfx->use_reset = strdup("false");
1355                         } else {
1356                                 mfx->use_reset = strdup("true");
1357                         }
1358
1359                         __save_xml_supportmode(reader, mfx);
1360                         __save_xml_installedtime(mfx);
1361                         __save_xml_defaultvalue(mfx);
1362
1363                         ret = __start_process(reader, mfx);
1364                 } else {
1365                         _LOGD("No Manifest element found\n");
1366                         return -1;
1367                 }
1368         }
1369         return ret;
1370 }
1371
1372
1373 static char* __convert_to_system_locale(const char *mlocale)
1374 {
1375         if (mlocale == NULL)
1376                 return NULL;
1377         char *locale = NULL;
1378         locale = (char *)calloc(1, 6);
1379         if (!locale) {
1380                 _LOGE("Malloc Failed\n");
1381                 return NULL;
1382         }
1383
1384         strncpy(locale, mlocale, 2);
1385         strncat(locale, "_", 1);
1386         locale[3] = toupper(mlocale[3]);
1387         locale[4] = toupper(mlocale[4]);
1388         return locale;
1389 }
1390
1391 static int __ail_change_info(int op, const char *appid)
1392 {
1393         void *lib_handle = NULL;
1394         int (*ail_desktop_operation) (const char *);
1395         char *aop = NULL;
1396         int ret = 0;
1397
1398         if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
1399                 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
1400                 goto END;
1401         }
1402
1403
1404         switch (op) {
1405                 case 0:
1406                         aop  = "ail_desktop_add";
1407                         break;
1408                 case 1:
1409                         aop  = "ail_desktop_update";
1410                         break;
1411                 case 2:
1412                         aop  = "ail_desktop_remove";
1413                         break;
1414                 case 3:
1415                         aop  = "ail_desktop_clean";
1416                         break;
1417                 case 4:
1418                         aop  = "ail_desktop_fota";
1419                         break;
1420                 default:
1421                         goto END;
1422                         break;
1423         }
1424
1425         if ((ail_desktop_operation =
1426              dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
1427                 _LOGE("can not find symbol \n");
1428                 goto END;
1429         }
1430
1431         ret = ail_desktop_operation(appid);
1432
1433 END:
1434         if (lib_handle)
1435                 dlclose(lib_handle);
1436
1437         return ret;
1438 }
1439
1440
1441 /* desktop shoud be generated automatically based on manifest */
1442 /* Currently removable, taskmanage, etc fields are not considerd. it will be decided soon.*/
1443 static int __ps_make_nativeapp_desktop(manifest_x * mfx, const char *manifest, ACTION_TYPE action)
1444 {
1445         FILE* file = NULL;
1446         int fd = 0;
1447         char filepath[PKG_STRING_LEN_MAX] = "";
1448         char *buf = NULL;
1449         char *buftemp = NULL;
1450         char *locale = NULL;
1451
1452         buf = (char *)calloc(1, BUFMAX);
1453         if (!buf) {
1454                 _LOGE("Malloc Failed\n");
1455                 return -1;
1456         }
1457
1458         buftemp = (char *)calloc(1, BUFMAX);
1459         if (!buftemp) {
1460                 _LOGE("Malloc Failed\n");
1461                 FREE_AND_NULL(buf);
1462                 return -1;
1463         }
1464
1465         if (action == ACTION_UPGRADE)
1466                 __ail_change_info(AIL_CLEAN, mfx->package);
1467
1468         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
1469
1470                 if (manifest != NULL) {
1471                         /* skip making a deskfile and update ail, if preload app is updated */
1472                         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
1473                                 __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid);
1474                     _LOGE("preload app is update : skip and update ail : %s", manifest);
1475                                 continue;
1476                         }
1477                 }
1478
1479                 if(mfx->readonly && !strcasecmp(mfx->readonly, "True"))
1480                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RO_PATH, mfx->uiapplication->appid);
1481                 else
1482                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, mfx->uiapplication->appid);
1483
1484                 /* skip if desktop exists
1485                 if (access(filepath, R_OK) == 0)
1486                         continue;
1487                 */
1488
1489                 file = fopen(filepath, "w");
1490                 if(file == NULL)
1491                 {
1492                     _LOGS("Can't open %s", filepath);
1493                     FREE_AND_NULL(buf);
1494                     FREE_AND_NULL(buftemp);
1495                     return -1;
1496                 }
1497
1498                 snprintf(buf, BUFMAX, "[Desktop Entry]\n");
1499                 fwrite(buf, 1, strlen(buf), file);
1500
1501                 for( ; mfx->uiapplication->label ; mfx->uiapplication->label = mfx->uiapplication->label->next) {
1502                         if(!strcmp(mfx->uiapplication->label->lang, DEFAULT_LOCALE)) {
1503                                 snprintf(buf, BUFMAX, "Name=%s\n",      mfx->uiapplication->label->text);
1504                         } else {
1505                                 locale = __convert_to_system_locale(mfx->uiapplication->label->lang);
1506                                 snprintf(buf, BUFMAX, "Name[%s]=%s\n", locale,
1507                                         mfx->uiapplication->label->text);
1508                                 FREE_AND_NULL(locale);
1509                         }
1510                         fwrite(buf, 1, strlen(buf), file);
1511                 }
1512
1513                 if(mfx->uiapplication->label && mfx->uiapplication->label->text) {
1514                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->uiapplication->label->text);
1515                         fwrite(buf, 1, strlen(buf), file);
1516                 }
1517 /*
1518                 else if(mfx->label && mfx->label->text) {
1519                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->label->text);
1520                         fwrite(buf, 1, strlen(buf), file);
1521                 } else {
1522                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->package);
1523                         fwrite(buf, 1, strlen(buf), file);
1524                 }
1525 */
1526
1527
1528                 snprintf(buf, BUFMAX, "Type=Application\n");
1529                 fwrite(buf, 1, strlen(buf), file);
1530
1531                 if(mfx->uiapplication->exec) {
1532                         snprintf(buf, BUFMAX, "Exec=%s\n", mfx->uiapplication->exec);
1533                         fwrite(buf, 1, strlen(buf), file);
1534                 }
1535
1536                 if(mfx->uiapplication->icon && mfx->uiapplication->icon->text) {
1537                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->uiapplication->icon->text);
1538                         fwrite(buf, 1, strlen(buf), file);
1539                 } else if(mfx->icon && mfx->icon->text) {
1540                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->icon->text);
1541                         fwrite(buf, 1, strlen(buf), file);
1542                 }
1543
1544                 // MIME types
1545                 if(mfx->uiapplication && mfx->uiapplication->appsvc) {
1546                         appsvc_x *asvc = mfx->uiapplication->appsvc;
1547                         mime_x *mi = NULL;
1548                         const char *mime = NULL;
1549                         const char *mime_delim = "; ";
1550                         int mime_count = 0;
1551
1552                         strncpy(buf, "MimeType=", BUFMAX-1);
1553                         while (asvc) {
1554                                 mi = asvc->mime;
1555                                 while (mi) {
1556                                         mime_count++;
1557                                         mime = mi->name;
1558                                         _LOGD("MIME type: %s\n", mime);
1559                                         strncat(buf, mime, BUFMAX-strlen(buf)-1);
1560                                         if(mi->next) {
1561                                                 strncat(buf, mime_delim, BUFMAX-strlen(buf)-1);
1562                                         }
1563
1564                                         mi = mi->next;
1565                                         mime = NULL;
1566                                 }
1567                                 asvc = asvc->next;
1568                         }
1569                         _LOGD("MIME types: buf[%s]\n", buf);
1570                         _LOGD("MIME count: %d\n", mime_count);
1571                         if(mime_count)
1572                                 fwrite(buf, 1, strlen(buf), file);
1573                 }
1574
1575                 if(mfx->version) {
1576                         snprintf(buf, BUFMAX, "Version=%s\n", mfx->version);
1577                         fwrite(buf, 1, strlen(buf), file);
1578                 }
1579
1580                 if(mfx->uiapplication->nodisplay) {
1581                         snprintf(buf, BUFMAX, "NoDisplay=%s\n", mfx->uiapplication->nodisplay);
1582                         fwrite(buf, 1, strlen(buf), file);
1583                 }
1584
1585                 if(mfx->uiapplication->categories) {
1586                         snprintf(buf, BUFMAX, "Categories=%s\n", mfx->uiapplication->categories);
1587                         fwrite(buf, 1, strlen(buf), file);
1588                 }
1589
1590                 if(mfx->uiapplication->taskmanage && !strcasecmp(mfx->uiapplication->taskmanage, "False")) {
1591                         snprintf(buf, BUFMAX, "X-TIZEN-TaskManage=False\n");
1592                         fwrite(buf, 1, strlen(buf), file);
1593                 }
1594
1595                 if(mfx->uiapplication->enabled && !strcasecmp(mfx->uiapplication->enabled, "False")) {
1596                         snprintf(buf, BUFMAX, "X-TIZEN-Enabled=False\n");
1597                         fwrite(buf, 1, strlen(buf), file);
1598                 }
1599
1600                 if(mfx->uiapplication->hwacceleration) {
1601                         snprintf(buf, BUFMAX, "Hw-Acceleration=%s\n", mfx->uiapplication->hwacceleration);
1602                         fwrite(buf, 1, strlen(buf), file);
1603                 }
1604
1605                 if(mfx->uiapplication->multiple && !strcasecmp(mfx->uiapplication->multiple, "True")) {
1606                         snprintf(buf, BUFMAX, "X-TIZEN-Multiple=True\n");
1607                         fwrite(buf, 1, strlen(buf), file);
1608                 }
1609
1610                 if(mfx->uiapplication->extraid) {
1611                         snprintf(buf, BUFMAX, "X-TIZEN-PackageID=%s\n", mfx->uiapplication->extraid);
1612                         fwrite(buf, 1, strlen(buf), file);
1613                 }
1614
1615                 if(mfx->removable && !strcasecmp(mfx->removable, "False")) {
1616                         snprintf(buf, BUFMAX, "X-TIZEN-Removable=False\n");
1617                         fwrite(buf, 1, strlen(buf), file);
1618                 }
1619
1620                 if(mfx->type) {
1621                         snprintf(buf, BUFMAX, "X-TIZEN-PackageType=%s\n", mfx->type);
1622                         fwrite(buf, 1, strlen(buf), file);
1623                 }
1624
1625                 if(mfx->uiapplication->submode && !strcasecmp(mfx->uiapplication->submode, "True")) {
1626                         snprintf(buf, BUFMAX, "X-TIZEN-Submode=%s\n", mfx->uiapplication->submode);
1627                         fwrite(buf, 1, strlen(buf), file);
1628                         snprintf(buf, BUFMAX, "X-TIZEN-SubmodeMainid=%s\n", mfx->uiapplication->submode_mainid);
1629                         fwrite(buf, 1, strlen(buf), file);
1630                 }
1631
1632                 snprintf(buf, BUFMAX, "X-TIZEN-PkgID=%s\n", mfx->package);
1633                 fwrite(buf, 1, strlen(buf), file);
1634
1635
1636                 snprintf(buf, BUFMAX, "X-TIZEN-InstalledStorage=%s\n", mfx->installed_storage);
1637                 fwrite(buf, 1, strlen(buf), file);
1638
1639 //              snprintf(buf, BUFMAX, "X-TIZEN-PackageType=rpm\n");
1640 //              fwrite(buf, 1, strlen(buf), file);
1641
1642
1643                 if(mfx->uiapplication->appsvc) {
1644                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
1645                         _LOGD("buf[%s]\n", buf);
1646
1647
1648                         uiapplication_x *up = mfx->uiapplication;
1649                         appsvc_x *asvc = NULL;
1650                         operation_x *op = NULL;
1651                         mime_x *mi = NULL;
1652                         uri_x *ui = NULL;
1653                         subapp_x *sub = NULL;
1654                         const char *operation = NULL;
1655                         const char *mime = NULL;
1656                         const char *uri = NULL;
1657                         const char *subapp = NULL;
1658                         int i = 0;
1659
1660
1661                         asvc = up->appsvc;
1662                         while(asvc != NULL) {
1663                                 op = asvc->operation;
1664                                 while(op != NULL) {
1665                                         if (op)
1666                                                 operation = op->name;
1667                                         mi = asvc->mime;
1668
1669                                         do
1670                                         {
1671                                                 if (mi)
1672                                                         mime = mi->name;
1673                                                 sub = asvc->subapp;
1674                                                 do
1675                                                 {
1676                                                         if (sub)
1677                                                                 subapp = sub->name;
1678                                                         ui = asvc->uri;
1679                                                         do
1680                                                         {
1681                                                                 if (ui)
1682                                                                         uri = ui->name;
1683
1684                                                                 if(i++ > 0) {
1685                                                                         strncpy(buftemp, buf, BUFMAX);
1686                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
1687                                                                 }
1688
1689
1690                                                                 strncpy(buftemp, buf, BUFMAX);
1691                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
1692                                                                 _LOGD("buf[%s]\n", buf);
1693
1694                                                                 if (ui)
1695                                                                         ui = ui->next;
1696                                                                 uri = NULL;
1697                                                         } while(ui != NULL);
1698                                                 if (sub)
1699                                                                 sub = sub->next;
1700                                                         subapp = NULL;
1701                                                 }while(sub != NULL);
1702                                                 if (mi)
1703                                                         mi = mi->next;
1704                                                 mime = NULL;
1705                                         }while(mi != NULL);
1706                                         if (op)
1707                                                 op = op->next;
1708                                         operation = NULL;
1709                                 }
1710                                 asvc = asvc->next;
1711                         }
1712
1713
1714                         fwrite(buf, 1, strlen(buf), file);
1715
1716 //                      strncpy(buftemp, buf, BUFMAX);
1717 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
1718 //                      fwrite(buf, 1, strlen(buf), file);
1719                 }
1720
1721                 fflush(file);
1722                 fd = fileno(file);
1723                 fsync(fd);
1724                 fclose(file);
1725                 if (action == ACTION_FOTA)
1726                         __ail_change_info(AIL_FOTA, mfx->uiapplication->appid);
1727                 else
1728                         __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid);
1729         }
1730
1731         FREE_AND_NULL(buf);
1732         FREE_AND_NULL(buftemp);
1733
1734         return 0;
1735 }
1736
1737 static int __ps_remove_nativeapp_desktop(manifest_x *mfx)
1738 {
1739         char filepath[PKG_STRING_LEN_MAX] = "";
1740         int ret = 0;
1741         uiapplication_x *uiapplication = mfx->uiapplication;
1742
1743         for(; uiapplication; uiapplication=uiapplication->next) {
1744                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, uiapplication->appid);
1745
1746                 __ail_change_info(AIL_REMOVE, uiapplication->appid);
1747
1748                 ret = remove(filepath);
1749                 if (ret <0)
1750                         return -1;
1751         }
1752
1753         return 0;
1754 }
1755
1756 void __add_preload_info(manifest_x * mfx, const char *manifest)
1757 {
1758         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
1759                 FREE_AND_NULL(mfx->readonly);
1760                 mfx->readonly = strdup("True");
1761
1762                 FREE_AND_NULL(mfx->preload);
1763                 mfx->preload = strdup("True");
1764
1765                 FREE_AND_NULL(mfx->removable);
1766                 mfx->removable = strdup("False");
1767
1768                 FREE_AND_NULL(mfx->system);
1769                 mfx->system = strdup("True");
1770         }
1771 }
1772
1773 static int __check_preload_updated(manifest_x * mfx, const char *manifest)
1774 {
1775         char filepath[PKG_STRING_LEN_MAX] = "";
1776         int ret = 0;
1777         uiapplication_x *uiapplication = mfx->uiapplication;
1778
1779         if(strstr(manifest, MANIFEST_RO_PREFIX)) {
1780                 /* if preload app is updated, then remove previous desktop file on RW*/
1781                 for(; uiapplication; uiapplication=uiapplication->next) {
1782                                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, uiapplication->appid);
1783                         ret = remove(filepath);
1784                         if (ret <0)
1785                                 return -1;
1786                 }
1787         } else {
1788                 /* if downloaded app is updated, then update tag set true*/
1789                 FREE_AND_NULL(mfx->update);
1790                 mfx->update = strdup("true");
1791         }
1792
1793         return 0;
1794 }
1795
1796 static int __get_appid_list(const char *pkgid, void *user_data)
1797 {
1798         int col = 0;
1799         int cols = 0;
1800         int ret = 0;
1801         char *appid = NULL;
1802
1803         sqlite3_stmt *stmt = NULL;
1804         sqlite3 *pkgmgr_parser_db;
1805
1806         char *query = sqlite3_mprintf("select app_id from package_app_info where package=%Q", pkgid);
1807
1808         ret = db_util_open(PKGMGR_PARSER_DB_FILE, &pkgmgr_parser_db, 0);
1809         if (ret != SQLITE_OK) {
1810                 _LOGE("open fail\n");
1811                 sqlite3_free(query);
1812                 return -1;
1813         }
1814         ret = sqlite3_prepare_v2(pkgmgr_parser_db, query, strlen(query), &stmt, NULL);
1815         if (ret != SQLITE_OK) {
1816                 _LOGE("prepare_v2 fail\n");
1817                 sqlite3_close(pkgmgr_parser_db);
1818                 sqlite3_free(query);
1819                 return -1;
1820         }
1821
1822         cols = sqlite3_column_count(stmt);
1823         while(1)
1824         {
1825                 ret = sqlite3_step(stmt);
1826                 if(ret == SQLITE_ROW) {
1827                         for(col = 0; col < cols; col++)
1828                         {
1829                                 appid = (char*)sqlite3_column_text(stmt, col);
1830                                 _LOGS("success find appid[%s]\n", appid);
1831                                 (*(GList**)user_data) = g_list_append((*(GList**)user_data), g_strdup(appid));
1832                         }
1833                         ret = 0;
1834                 } else {
1835                         break;
1836                 }
1837         }
1838         sqlite3_finalize(stmt);
1839         sqlite3_close(pkgmgr_parser_db);
1840         sqlite3_free(query);
1841         return ret;
1842 }
1843
1844 void __call_uninstall_plugin(const char *pkgid, GList *appid_list)
1845 {
1846         char *appid = NULL;
1847         GList *tmp_list;
1848
1849         if (pkgid == NULL) {
1850                 _LOGD("pkgid is null\n");
1851                 return;
1852         }
1853
1854         tmp_list = appid_list;
1855         _pkgmgr_parser_plugin_open_db();
1856
1857         _pkgmgr_parser_plugin_uninstall_plugin(PKGMGR_PARSER_PLUGIN_TAG, pkgid, pkgid);
1858
1859         while (tmp_list) {
1860                 appid = (char *)tmp_list->data;
1861                 if (appid) {
1862                         _pkgmgr_parser_plugin_uninstall_plugin(PKGMGR_PARSER_PLUGIN_METADATA, pkgid, appid);
1863                         _pkgmgr_parser_plugin_uninstall_plugin(PKGMGR_PARSER_PLUGIN_CATEGORY, pkgid, appid);
1864                 }
1865                 tmp_list = g_list_next(tmp_list);
1866         }
1867
1868         _pkgmgr_parser_plugin_close_db();
1869 }
1870
1871 static int __delete_pkgid_info_for_uninstallation(const char *pkgid)
1872 {
1873         retvm_if(pkgid == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
1874
1875         int ret = -1;
1876         char *appid = NULL;
1877         GList *appid_list = NULL;
1878         GList *tmp = NULL;
1879         char filepath[PKG_STRING_LEN_MAX] = "";
1880
1881         _LOGD("Start uninstall for pkgid : delete pkgid[%s]\n", pkgid);
1882
1883         /*get appid list*/
1884         ret = __get_appid_list(pkgid, &appid_list);
1885         retvm_if(ret < 0, PM_PARSER_R_ERROR, "Fail to get app_list");
1886
1887         _LOGD("call plugin uninstall\n");
1888
1889         /*call plugin uninstall*/
1890         __call_uninstall_plugin(pkgid, appid_list);
1891
1892         /* delete pkgmgr db */
1893         ret = pkgmgr_parser_delete_pkgid_info_from_db(pkgid);
1894         if (ret == -1)
1895                 _LOGD("DB pkgid info Delete failed\n");
1896         else
1897                 _LOGD("DB pkgid info Delete Success\n");
1898
1899         /* delete desktop file*/
1900         tmp = appid_list;
1901         while (tmp) {
1902                 appid = (char *)tmp->data;
1903                 if (appid) {
1904                         _LOGD("Delete appid[%s] info from db\n", appid);
1905
1906                         ret = pkgmgr_parser_delete_appid_info_from_db(appid);
1907                         if (ret == -1)
1908                                 _LOGD("DB appid info Delete failed\n");
1909                         else
1910                                 _LOGD("DB appid info Delete Success\n");
1911
1912                         ret = __ail_change_info(AIL_REMOVE, appid);
1913                         if (ret < 0)
1914                                 _LOGD("AIL_REMOVE failed\n");
1915
1916                         snprintf(filepath, sizeof(filepath),"%s%s.desktop", DESKTOP_RW_PATH, appid);
1917                         if (access(filepath, R_OK) == 0) {
1918                                 ret = remove(filepath);
1919                                 if (ret < 0)
1920                                         _LOGD("remove failed\n");
1921                         }
1922                         g_free(tmp->data);
1923                 }
1924                 tmp  = g_list_next(tmp);
1925         }
1926
1927         g_list_free(appid_list);
1928
1929         _LOGD("Finish : uninstall for pkgid\n");
1930
1931         return PM_PARSER_R_OK;
1932 }
1933
1934 static int __pkgmgr_db_check_update_condition(manifest_x *mfx)
1935 {
1936         int ret = -1;
1937         char *preload = NULL;
1938         char *system = NULL;
1939         char *csc_path = NULL;
1940         char *installed_storage = NULL;
1941         sqlite3_stmt *stmt = NULL;
1942         sqlite3 *pkgmgr_parser_db;
1943         char *query = NULL;
1944
1945         ret = _pkgmgr_db_open(PKGMGR_PARSER_DB_FILE, &pkgmgr_parser_db);
1946         retvm_if(ret != PM_PARSER_R_OK, PM_PARSER_R_ERROR, "_pkgmgr_db_open failed\n");
1947
1948         query = sqlite3_mprintf("select package_preload from package_info where package=%Q", mfx->package);
1949
1950         ret = _pkgmgr_db_prepare(pkgmgr_parser_db, query, &stmt);
1951         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_prepare failed[%s]\n", query);
1952
1953         ret = _pkgmgr_db_step(stmt);
1954         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_step failed\n");
1955
1956         ret = _pkgmgr_db_get_str(stmt, 0, &preload);
1957         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_get_str failed\n");
1958
1959         if (strcasecmp(preload, "true") == 0) {
1960                 FREE_AND_NULL(mfx->preload);
1961                 mfx->preload = strdup("true");
1962         }
1963
1964         _pkgmgr_db_finalize(stmt);
1965         sqlite3_free(query);
1966
1967         query = sqlite3_mprintf("select package_system from package_info where package=%Q", mfx->package);
1968
1969         ret = _pkgmgr_db_prepare(pkgmgr_parser_db, query, &stmt);
1970         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_prepare failed[%s]\n", query);
1971
1972         ret = _pkgmgr_db_step(stmt);
1973         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_step failed\n");
1974
1975         ret = _pkgmgr_db_get_str(stmt, 0, &system);
1976         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_get_str failed\n");
1977
1978         if (strcasecmp(system, "true") == 0) {
1979                 FREE_AND_NULL(mfx->system);
1980                 mfx->system = strdup("true");
1981         }
1982
1983         _pkgmgr_db_finalize(stmt);
1984         sqlite3_free(query);
1985
1986         query = sqlite3_mprintf("select csc_path from package_info where package=%Q", mfx->package);
1987
1988         ret = _pkgmgr_db_prepare(pkgmgr_parser_db, query, &stmt);
1989         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_prepare failed[%s]\n", query);
1990
1991         ret = _pkgmgr_db_step(stmt);
1992         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_step failed\n");
1993
1994         ret = _pkgmgr_db_get_str(stmt, 0, &csc_path);
1995         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_get_str failed\n");
1996
1997         if (csc_path != NULL) {
1998                 if (mfx->csc_path)
1999                         FREE_AND_NULL(mfx->csc_path);
2000                 mfx->csc_path = strdup(csc_path);
2001         }
2002
2003         _pkgmgr_db_finalize(stmt);
2004         sqlite3_free(query);
2005
2006         query = sqlite3_mprintf("select installed_storage from package_info where package=%Q", mfx->package);
2007
2008         ret = _pkgmgr_db_prepare(pkgmgr_parser_db, query, &stmt);
2009         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_prepare failed[%s]\n", query);
2010
2011         ret = _pkgmgr_db_step(stmt);
2012         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_step failed\n");
2013
2014         ret = _pkgmgr_db_get_str(stmt, 0, &installed_storage);
2015         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_get_str failed\n");
2016
2017         if (strcasecmp(installed_storage, "installed_external") == 0) {
2018                 FREE_AND_NULL(mfx->installed_storage);
2019                 mfx->installed_storage = strdup("installed_external");
2020         }
2021
2022         ret = PM_PARSER_R_OK;
2023
2024 catch:
2025
2026         sqlite3_free(query);
2027         _pkgmgr_db_finalize(stmt);
2028         _pkgmgr_db_close(pkgmgr_parser_db);
2029
2030         return ret;
2031 }
2032
2033 static int __pkgmgr_db_check_disable_condition(manifest_x *mfx)
2034 {
2035         int ret = -1;
2036         char *package_disable = NULL;
2037         sqlite3_stmt *stmt = NULL;
2038         sqlite3 *pkgmgr_parser_db;
2039         char *query = NULL;
2040
2041         ret = _pkgmgr_db_open(PKGMGR_PARSER_DB_FILE, &pkgmgr_parser_db);
2042         retvm_if(ret != PM_PARSER_R_OK, PM_PARSER_R_ERROR, "_pkgmgr_db_open failed\n");
2043
2044         query = sqlite3_mprintf("select package_disable from package_info where package=%Q", mfx->package);
2045
2046         ret = _pkgmgr_db_prepare(pkgmgr_parser_db, query, &stmt);
2047         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_prepare failed[%s]\n", query);
2048
2049         ret = _pkgmgr_db_step(stmt);
2050         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_step failed\n");
2051
2052         ret = _pkgmgr_db_get_str(stmt, 0, &package_disable);
2053         tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "_pkgmgr_db_get_str failed\n");
2054
2055         if (strcasecmp(package_disable, "true") == 0) {
2056                 ret = 1;
2057         } else {
2058                 ret = -1;
2059         }
2060
2061 catch:
2062
2063         sqlite3_free(query);
2064         _pkgmgr_db_finalize(stmt);
2065         _pkgmgr_db_close(pkgmgr_parser_db);
2066
2067         return ret;
2068 }
2069
2070 API int pkgmgr_parser_check_mdm_policy_for_uninstallation(const char *manifest)
2071 {
2072         int ret = PM_PARSER_R_OK;
2073         manifest_x *mfx = NULL;
2074
2075         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2076         retvm_if(mfx == NULL, PM_PARSER_R_OK, "argument supplied is NULL");
2077
2078         ret = __ps_check_mdm_policy(mfx, ACTION_UNINSTALL);
2079         if (ret < 0)
2080                 _LOGE("pkg[%s] violate mdm policy.", mfx->package);
2081
2082         pkgmgr_parser_free_manifest_xml(mfx);
2083
2084         return ret;
2085 }
2086
2087 API int pkgmgr_parser_create_desktop_file(manifest_x *mfx)
2088 {
2089         int ret = 0;
2090         if (mfx == NULL) {
2091                 _LOGD("Manifest pointer is NULL\n");
2092                 return -1;
2093         }
2094         ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL);
2095         if (ret == -1)
2096                 _LOGD("Creating desktop file failed\n");
2097         else
2098                 _LOGD("Creating desktop file Success\n");
2099         return ret;
2100 }
2101
2102 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
2103 {
2104         _pkgmgrinfo_basic_free_manifest_x(mfx);
2105 }
2106
2107 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
2108 {
2109         _LOGD("parsing start\n");
2110         xmlTextReaderPtr reader;
2111         manifest_x *mfx = NULL;
2112
2113         reader = xmlReaderForFile(manifest, NULL, 0);
2114         if (reader) {
2115                 mfx = malloc(sizeof(manifest_x));
2116                 if (mfx) {
2117                         memset(mfx, '\0', sizeof(manifest_x));
2118                         if (__process_manifest(reader, mfx) < 0) {
2119                                 _LOGD("Parsing Failed\n");
2120                                 pkgmgr_parser_free_manifest_xml(mfx);
2121                                 mfx = NULL;
2122                         } else
2123                                 _LOGD("Parsing Success\n");
2124                 } else {
2125                         _LOGD("Memory allocation error\n");
2126                 }
2127                 xmlFreeTextReader(reader);
2128         } else {
2129                 _LOGD("Unable to create xml reader\n");
2130         }
2131         return mfx;
2132 }
2133
2134 /* These APIs are intended to call parser directly */
2135
2136 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
2137 {
2138         retvm_if(manifest == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2139         _LOGD("parsing manifest for installation: %s\n", manifest);
2140
2141         manifest_x *mfx = NULL;
2142         int ret = -1;
2143         char *hash = NULL;
2144
2145         xmlInitParser();
2146         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2147         retvm_if(mfx == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2148
2149         _LOGD("Parsing Finished\n");
2150
2151         _LOGD("Generating the hash for manifest");
2152         hash = pkgmgrinfo_basic_generate_hash_for_file(manifest);
2153         if(hash != NULL){
2154                 mfx->hash = hash;
2155                 _LOGD("Hash for [%s] file is [%s]\n",manifest,hash);
2156         }
2157
2158         ret = __ps_check_mdm_policy(mfx, ACTION_INSTALL);
2159         tryvm_if(ret == PM_PARSER_R_ERROR, ret = PM_PARSER_R_ERROR, "pkg[%s] violate mdm policy.", mfx->package);
2160
2161         __add_preload_info(mfx, manifest);
2162
2163         _LOGD("Added preload infomation\n");
2164
2165         __ps_process_tag(mfx, tagv);
2166
2167         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
2168         if (ret < 0) {
2169                 ret = __delete_pkgid_info_for_uninstallation(mfx->package);
2170                 ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
2171                 retvm_if(ret == PM_PARSER_R_ERROR, PM_PARSER_R_ERROR, "DB Insert failed");
2172         }
2173
2174         _LOGD("DB Insert Success\n");
2175
2176         _pkgmgr_parser_plugin_process_plugin(mfx, manifest, ACTION_INSTALL);
2177
2178         ret = __check_action_fota(tagv);
2179         tryvm_if(ret == PM_PARSER_R_OK, ret = PM_PARSER_R_OK, "fota install called, dont need desktop generation\n");
2180
2181         ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL);
2182
2183         if (ret == -1)
2184                 _LOGD("Creating desktop file failed\n");
2185         else
2186                 _LOGD("Creating desktop file Success\n");
2187
2188
2189 catch:
2190         pkgmgr_parser_free_manifest_xml(mfx);
2191         _LOGD("Free Done\n");
2192         xmlCleanupParser();
2193
2194         return PM_PARSER_R_OK;
2195 }
2196
2197 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
2198 {
2199         retvm_if(manifest == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2200         _LOGD("parsing manifest for upgradation: %s\n", manifest);
2201
2202         manifest_x *mfx = NULL;
2203         int ret = -1;
2204         int package_disabled = 0;
2205         char *hash = NULL;
2206
2207         xmlInitParser();
2208         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2209         retvm_if(mfx == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2210
2211         _LOGD("Parsing Finished\n");
2212
2213         _LOGD("Generating the hash for manifest");
2214         hash = pkgmgrinfo_basic_generate_hash_for_file(manifest);
2215         if(hash != NULL){
2216                 mfx->hash = hash;
2217                 _LOGD("Hash for [%s] file is [%s]\n",manifest,hash);
2218         }
2219
2220         __add_preload_info(mfx, manifest);
2221         _LOGD("Added preload infomation\n");
2222         __check_preload_updated(mfx, manifest);
2223
2224         if (__pkgmgr_db_check_disable_condition(mfx) == 1) {
2225                 _LOGD("pkgid[%s] is disabed\n", mfx->package);
2226                 package_disabled = 1;
2227         }
2228
2229         ret = __pkgmgr_db_check_update_condition(mfx);
2230         if (ret < 0)
2231                 _LOGD("__pkgmgr_db_check_update_condition failed\n");
2232
2233         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2234         retvm_if(ret == PM_PARSER_R_ERROR, PM_PARSER_R_ERROR, "DB Insert failed");
2235         _LOGD("DB Update Success\n");
2236
2237         if (package_disabled == 1) {
2238                 ret = pkgmgr_parser_disable_pkg(mfx->package, NULL);
2239                 tryvm_if(ret != PM_PARSER_R_OK, ret = PM_PARSER_R_ERROR, "disable_pkg failed[%s]\n", mfx->package);
2240         }
2241
2242         _pkgmgr_parser_plugin_process_plugin(mfx, manifest, ACTION_UPGRADE);
2243
2244         ret = __check_action_fota(tagv);
2245         tryvm_if(ret == PM_PARSER_R_OK, ret = PM_PARSER_R_OK, "fota install called, dont need desktop generation\n");
2246
2247         ret = __ps_make_nativeapp_desktop(mfx, manifest, ACTION_UPGRADE);
2248         if (ret == -1)
2249                 _LOGD("Creating desktop file failed\n");
2250         else
2251                 _LOGD("Creating desktop file Success\n");
2252
2253 catch:
2254         journal_appcore_app_updated(mfx->package);
2255
2256         pkgmgr_parser_free_manifest_xml(mfx);
2257         _LOGD("Free Done\n");
2258         xmlCleanupParser();
2259
2260         return PM_PARSER_R_OK;
2261 }
2262
2263 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
2264 {
2265         retvm_if(manifest == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2266         int ret = -1;
2267         manifest_x *mfx = NULL;
2268
2269         if (!strstr(manifest, ".xml")) {
2270                 _LOGD("manifest is pkgid[%s]\n", manifest);
2271                 ret = __delete_pkgid_info_for_uninstallation(manifest);
2272                 if (ret <0)
2273                         _LOGD("__delete_manifest_for_fota failed\n");
2274
2275                 return PM_PARSER_R_OK;
2276         }
2277
2278         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2279
2280         xmlInitParser();
2281         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2282         retvm_if(mfx == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
2283
2284         _LOGD("Parsing Finished\n");
2285
2286         ret = __ps_check_mdm_policy(mfx, ACTION_UNINSTALL);
2287         retvm_if(ret == PM_PARSER_R_ERROR, PM_PARSER_R_ERROR, "pkg[%s] violate mdm policy.", mfx->package);
2288
2289         __add_preload_info(mfx, manifest);
2290         _LOGD("Added preload infomation\n");
2291
2292         _pkgmgr_parser_plugin_process_plugin(mfx, manifest, ACTION_UNINSTALL);
2293
2294         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
2295         if (ret == -1)
2296                 _LOGD("DB Delete failed\n");
2297         else
2298                 _LOGD("DB Delete Success\n");
2299
2300         ret = __ps_remove_nativeapp_desktop(mfx);
2301         if (ret == -1)
2302                 _LOGD("Removing desktop file failed\n");
2303         else
2304                 _LOGD("Removing desktop file Success\n");
2305
2306         pkgmgr_parser_free_manifest_xml(mfx);
2307         _LOGD("Free Done\n");
2308         xmlCleanupParser();
2309
2310         return PM_PARSER_R_OK;
2311 }
2312
2313 API int pkgmgr_parser_parse_manifest_for_preload()
2314 {
2315         return pkgmgr_parser_update_preload_info_in_db();
2316 }
2317
2318 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
2319 {
2320         return __pkgid_to_manifest(pkgid);
2321 }
2322
2323 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
2324 {
2325         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
2326 }
2327
2328 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
2329 {
2330         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
2331 }
2332
2333 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
2334 {
2335         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
2336 }
2337
2338 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
2339 {
2340         if (manifest == NULL) {
2341                 _LOGE("manifest file is NULL\n");
2342                 return PM_PARSER_R_EINVAL;
2343         }
2344         int ret = PM_PARSER_R_OK;
2345         xmlSchemaParserCtxtPtr ctx = NULL;
2346         xmlSchemaValidCtxtPtr vctx = NULL;
2347         xmlSchemaPtr xschema = NULL;
2348         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
2349         if (ctx == NULL) {
2350                 _LOGE("xmlSchemaNewParserCtxt() Failed\n");
2351                 return PM_PARSER_R_ERROR;
2352         }
2353         xschema = xmlSchemaParse(ctx);
2354         if (xschema == NULL) {
2355                 _LOGE("xmlSchemaParse() Failed\n");
2356                 ret = PM_PARSER_R_ERROR;
2357                 goto cleanup;
2358         }
2359         vctx = xmlSchemaNewValidCtxt(xschema);
2360         if (vctx == NULL) {
2361                 _LOGE("xmlSchemaNewValidCtxt() Failed\n");
2362                 return PM_PARSER_R_ERROR;
2363         }
2364         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
2365         ret = xmlSchemaValidateFile(vctx, manifest, 0);
2366         if (ret == -1) {
2367                 _LOGE("xmlSchemaValidateFile() failed\n");
2368                 ret = PM_PARSER_R_ERROR;
2369                 goto cleanup;
2370         } else if (ret == 0) {
2371                 _LOGE("Manifest is Valid\n");
2372                 ret = PM_PARSER_R_OK;
2373                 goto cleanup;
2374         } else {
2375                 _LOGE("Manifest Validation Failed with error code %d\n", ret);
2376                 ret = PM_PARSER_R_ERROR;
2377                 goto cleanup;
2378         }
2379
2380 cleanup:
2381         if(vctx != NULL)
2382                 xmlSchemaFreeValidCtxt(vctx);
2383
2384         if(ctx != NULL)
2385                 xmlSchemaFreeParserCtxt(ctx);
2386
2387         if(xschema != NULL)
2388                 xmlSchemaFree(xschema);
2389
2390         return ret;
2391 }
2392
2393 API int pkgmgr_parser_enable_pkg(const char *pkgid, char *const tagv[])
2394 {
2395         retvm_if(pkgid == NULL, PM_PARSER_R_ERROR, "pkgid is NULL.");
2396
2397         int ret = -1;
2398         char *manifest = NULL;
2399         manifest_x *mfx = NULL;
2400
2401         ret = pkgmgr_parser_update_enabled_pkg_info_in_db(pkgid);
2402         if (ret == -1)
2403                 _LOGD("pkgmgr_parser_update_enabled_pkg_info_in_db(%s) failed.\n", pkgid);
2404         else
2405                 _LOGD("pkgmgr_parser_update_enabled_pkg_info_in_db(%s) succeed.\n", pkgid);
2406
2407         manifest = __pkgid_to_manifest(pkgid);
2408         retvm_if(manifest == NULL, PM_PARSER_R_ERROR, "manifest of pkgid(%s) is NULL.", pkgid);
2409
2410         xmlInitParser();
2411         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2412         tryvm_if(mfx == NULL, ret = PM_PARSER_R_ERROR, "process_manifest_xml(%s) failed\n", pkgid);
2413
2414         _pkgmgr_parser_plugin_process_plugin(mfx, manifest, ACTION_INSTALL);
2415
2416         ret = PM_PARSER_R_OK;
2417
2418 catch:
2419         if (mfx)
2420                 pkgmgr_parser_free_manifest_xml(mfx);
2421
2422         if (manifest)
2423                 free(manifest);
2424
2425         xmlCleanupParser();
2426
2427         return ret;
2428 }
2429
2430 API int pkgmgr_parser_disable_pkg(const char *pkgid, char *const tagv[])
2431 {
2432         retvm_if(pkgid == NULL, PM_PARSER_R_ERROR, "pkgid is NULL.");
2433
2434         int ret = -1;
2435         GList *appid_list = NULL;
2436
2437         /*get appid list*/
2438         ret = __get_appid_list(pkgid, &appid_list);
2439         retvm_if(ret < 0, PM_PARSER_R_ERROR, "Fail to get app_list");
2440
2441         _LOGD("call plugin uninstall\n");
2442
2443         /*call plugin uninstall*/
2444         __call_uninstall_plugin(pkgid, appid_list);
2445
2446         ret = pkgmgr_parser_update_disabled_pkg_info_in_db(pkgid);
2447         if (ret == -1)
2448                 _LOGD("pkgmgr_parser_update_disabled_pkg_info_in_db(%s) failed.\n", pkgid);
2449         else
2450                 _LOGD("pkgmgr_parser_update_disabled_pkg_info_in_db(%s) succeed.\n", pkgid);
2451
2452         g_list_free(appid_list);
2453
2454         return PM_PARSER_R_OK;
2455 }
2456
2457 /* update aliasid entry in package-app-aliasid */
2458 API int pkgmgr_parser_insert_app_aliasid(void )
2459 {
2460         return pkgmgr_parser_insert_app_aliasid_info_in_db();
2461 }
2462
2463 API int pkgmgr_parser_update_app_aliasid(void )
2464 {
2465         return pkgmgr_parser_update_app_aliasid_info_in_db();
2466 }
2467