merge master branch
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgr-info.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
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <db-util.h>
28 #include <sqlite3.h>
29 #include <vconf.h>
30 #include <glib.h>
31 #include <ctype.h>
32 #include <assert.h>
33
34 #include <libxml/parser.h>
35 #include <libxml/xmlreader.h>
36 #include <libxml/xmlschemas.h>
37
38 #include "pkgmgr_parser.h"
39 #include "pkgmgr-info-internal.h"
40 #include "pkgmgr-info-debug.h"
41 #include "pkgmgr-info.h"
42 #include "pkgmgr_parser_db.h"
43 #include <dirent.h>
44 #include <sys/stat.h>
45
46 #define ASC_CHAR(s) (const char *)s
47 #define XML_CHAR(s) (const xmlChar *)s
48
49 #define MANIFEST_DB     "/opt/dbspace/.pkgmgr_parser.db"
50 #define MAX_QUERY_LEN   4096
51 #define MAX_CERT_TYPE   9
52 #define CERT_DB         "/opt/dbspace/.pkgmgr_cert.db"
53 #define DATACONTROL_DB  "/opt/usr/dbspace/.app-package.db"
54 #define PKG_TYPE_STRING_LEN_MAX         128
55 #define PKG_VERSION_STRING_LEN_MAX      128
56 #define PKG_VALUE_STRING_LEN_MAX                512
57 #define PKG_LOCALE_STRING_LEN_MAX               8
58 #define PKG_RW_PATH "/opt/usr/apps/"
59 #define PKG_RO_PATH "/usr/apps/"
60 #define BLOCK_SIZE      4096 /*in bytes*/
61
62 #define MMC_PATH "/opt/storage/sdcard"
63 #define PKG_SD_PATH MMC_PATH"/app2sd/"
64 #define PKG_INSTALLATION_PATH "/opt/usr/apps/"
65
66 #define FILTER_QUERY_COUNT_PACKAGE      "select count(DISTINCT package_info.package) " \
67                                 "from package_info LEFT OUTER JOIN package_localized_info " \
68                                 "ON package_info.package=package_localized_info.package " \
69                                 "and package_localized_info.package_locale='%s' where "
70
71 #define FILTER_QUERY_LIST_PACKAGE       "select DISTINCT package_info.package " \
72                                 "from package_info LEFT OUTER JOIN package_localized_info " \
73                                 "ON package_info.package=package_localized_info.package " \
74                                 "and package_localized_info.package_locale='%s' where "
75
76 #define FILTER_QUERY_COUNT_APP  "select count(DISTINCT package_app_info.app_id) " \
77                                 "from package_app_info LEFT OUTER JOIN package_app_localized_info " \
78                                 "ON package_app_info.app_id=package_app_localized_info.app_id " \
79                                 "and package_app_localized_info.app_locale='%s' " \
80                                 "LEFT OUTER JOIN package_app_app_svc " \
81                                 "ON package_app_info.app_id=package_app_app_svc.app_id " \
82                                 "LEFT OUTER JOIN package_app_app_category " \
83                                 "ON package_app_info.app_id=package_app_app_category.app_id where "
84
85 #define FILTER_QUERY_LIST_APP   "select DISTINCT package_app_info.app_id, package_app_info.app_component " \
86                                 "from package_app_info LEFT OUTER JOIN package_app_localized_info " \
87                                 "ON package_app_info.app_id=package_app_localized_info.app_id " \
88                                 "and package_app_localized_info.app_locale='%s' " \
89                                 "LEFT OUTER JOIN package_app_app_svc " \
90                                 "ON package_app_info.app_id=package_app_app_svc.app_id " \
91                                 "LEFT OUTER JOIN package_app_app_category " \
92                                 "ON package_app_info.app_id=package_app_app_category.app_id where "
93
94 #define METADATA_FILTER_QUERY_SELECT_CLAUSE     "select DISTINCT package_app_info.app_id, package_app_info.app_component " \
95                                 "from package_app_info LEFT OUTER JOIN package_app_app_metadata " \
96                                 "ON package_app_info.app_id=package_app_app_metadata.app_id where "
97
98 #define METADATA_FILTER_QUERY_UNION_CLAUSE      " UNION "METADATA_FILTER_QUERY_SELECT_CLAUSE
99
100 #define LANGUAGE_LENGTH 2
101
102 typedef struct _pkgmgr_instcertinfo_x {
103         char *pkgid;
104         char *cert_info[MAX_CERT_TYPE]; /*certificate data*/
105         int is_new[MAX_CERT_TYPE];              /*whether already exist in table or not*/
106         int ref_count[MAX_CERT_TYPE];           /*reference count of certificate data*/
107         int cert_id[MAX_CERT_TYPE];             /*certificate ID in index table*/
108 } pkgmgr_instcertinfo_x;
109
110 typedef struct _pkgmgr_certindexinfo_x {
111         int cert_id;
112         int cert_ref_count;
113 } pkgmgr_certindexinfo_x;
114
115 typedef struct _pkgmgr_pkginfo_x {
116         manifest_x *manifest_info;
117         char *locale;
118
119         struct _pkgmgr_pkginfo_x *prev;
120         struct _pkgmgr_pkginfo_x *next;
121 } pkgmgr_pkginfo_x;
122
123 typedef struct _pkgmgr_cert_x {
124         char *pkgid;
125         int cert_id;
126 } pkgmgr_cert_x;
127
128 typedef struct _pkgmgr_datacontrol_x {
129         char *appid;
130         char *access;
131 } pkgmgr_datacontrol_x;
132
133 typedef struct _pkgmgr_iconpath_x {
134         char *appid;
135         char *iconpath;
136 } pkgmgr_iconpath_x;
137
138 typedef struct _pkgmgr_image_x {
139         char *imagepath;
140 } pkgmgr_image_x;
141
142 typedef struct _pkgmgr_locale_x {
143         char *locale;
144 } pkgmgr_locale_x;
145
146 typedef struct _pkgmgr_appinfo_x {
147         const char *package;
148         char *locale;
149         pkgmgrinfo_app_component app_component;
150         union {
151                 uiapplication_x *uiapp_info;
152                 serviceapplication_x *svcapp_info;
153         };
154 } pkgmgr_appinfo_x;
155
156 typedef struct _pkgmgr_certinfo_x {
157         char *pkgid;
158         char *cert_value;
159         char *cert_info[MAX_CERT_TYPE]; /*certificate info*/
160         int cert_id[MAX_CERT_TYPE];             /*certificate ID in index table*/
161 } pkgmgr_certinfo_x;
162
163 /*For filter APIs*/
164 typedef struct _pkgmgrinfo_filter_x {
165         GSList *list;
166 } pkgmgrinfo_filter_x;
167
168 typedef struct _pkgmgrinfo_node_x {
169         int prop;
170         char *key;
171         char *value;
172 } pkgmgrinfo_node_x;
173
174 typedef struct _pkgmgrinfo_appcontrol_x {
175         int operation_count;
176         int uri_count;
177         int mime_count;
178         int subapp_count;
179         char **operation;
180         char **uri;
181         char **mime;
182         char **subapp;
183 } pkgmgrinfo_appcontrol_x;
184
185 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
186
187 char *pkgtype = "rpm";
188 __thread sqlite3 *manifest_db = NULL;
189 __thread sqlite3 *datacontrol_db = NULL;
190 __thread sqlite3 *cert_db = NULL;
191
192 static int __open_manifest_db();
193 static int __exec_pkginfo_query(char *query, void *data);
194 static int __exec_certinfo_query(char *query, void *data);
195 static int __exec_certindexinfo_query(char *query, void *data);
196 static int __pkginfo_cb(void *data, int ncols, char **coltxt, char **colname);
197 static int __appinfo_cb(void *data, int ncols, char **coltxt, char **colname);
198 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname);
199 static int __certindexinfo_cb(void *data, int ncols, char **coltxt, char **colname);
200 static int __validate_cb(void *data, int ncols, char **coltxt, char **colname);
201 static int __maxid_cb(void *data, int ncols, char **coltxt, char **colname);
202 static int __count_cb(void *data, int ncols, char **coltxt, char **colname);
203 static int __uiapp_list_cb(void *data, int ncols, char **coltxt, char **colname);
204 static int __svcapp_list_cb(void *data, int ncols, char **coltxt, char **colname);
205 static int __pkg_list_cb(void *data, int ncols, char **coltxt, char **colname);
206 static int __app_list_cb(void *data, int ncols, char **coltxt, char **colname);
207 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data);
208 static void __cleanup_appinfo(pkgmgr_appinfo_x *data);
209 static char* __convert_system_locale_to_manifest_locale(char *syslocale);
210 static void __destroy_each_node(gpointer data, gpointer user_data);
211 static void __get_filter_condition(gpointer data, char **condition);
212 static void __get_metadata_filter_condition(gpointer data, char **condition);
213 static gint __compare_func(gconstpointer data1, gconstpointer data2);
214 static int __delete_certinfo(const char *pkgid);
215
216 static gint __compare_func(gconstpointer data1, gconstpointer data2)
217 {
218         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
219         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
220         if (node1->prop == node2->prop)
221                 return 0;
222         else if (node1->prop > node2->prop)
223                 return 1;
224         else
225                 return -1;
226 }
227
228 static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
229 {
230         int *p = (int*)data;
231         *p = atoi(coltxt[0]);
232         _LOGE("count value is %d\n", *p);
233         return 0;
234 }
235
236 static void __destroy_each_node(gpointer data, gpointer user_data)
237 {
238         ret_if(data == NULL);
239         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
240         if (node->value) {
241                 free(node->value);
242                 node->value = NULL;
243         }
244         if (node->key) {
245                 free(node->key);
246                 node->key = NULL;
247         }
248         free(node);
249         node = NULL;
250 }
251
252 static void __get_metadata_filter_condition(gpointer data, char **condition)
253 {
254         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
255         char key[MAX_QUERY_LEN] = {'\0'};
256         char value[MAX_QUERY_LEN] = {'\0'};
257         if (node->key) {
258                 snprintf(key, MAX_QUERY_LEN, "(package_app_app_metadata.md_key='%s'", node->key);
259         }
260         if (node->value) {
261                 snprintf(value, MAX_QUERY_LEN, " AND package_app_app_metadata.md_value='%s')", node->value);
262                 strcat(key, value);
263         } else {
264                 strcat(key, ")");
265         }
266         *condition = strdup(key);
267         return;
268 }
269
270 static void __get_filter_condition(gpointer data, char **condition)
271 {
272         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
273         char buf[MAX_QUERY_LEN + 1] = {'\0'};
274         char temp[PKG_STRING_LEN_MAX] = {'\0'};
275         switch (node->prop) {
276         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
277                 snprintf(buf, MAX_QUERY_LEN, "package_info.package='%s'", node->value);
278                 break;
279         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
280                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_type='%s'", node->value);
281                 break;
282         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
283                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_version='%s'", node->value);
284                 break;
285         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
286                 snprintf(buf, MAX_QUERY_LEN, "package_info.install_location='%s'", node->value);
287                 break;
288         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE:
289                 snprintf(buf, MAX_QUERY_LEN, "package_info.installed_storage='%s'", node->value);
290                 break;
291         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
292                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_name='%s'", node->value);
293                 break;
294         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
295                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_href='%s'", node->value);
296                 break;
297         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
298                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_email='%s'", node->value);
299                 break;
300         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
301                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_size='%s'", node->value);
302                 break;
303         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
304                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_removable IN %s", node->value);
305                 break;
306         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
307                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_preload IN %s", node->value);
308                 break;
309         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
310                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_readonly IN %s", node->value);
311                 break;
312         case E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE:
313                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_update IN %s", node->value);
314                 break;
315         case E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING:
316                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_appsetting IN %s", node->value);
317                 break;
318         case E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING:
319                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_nodisplay IN %s", node->value);
320                 break;
321
322         case E_PMINFO_APPINFO_PROP_APP_ID:
323                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_id='%s'", node->value);
324                 break;
325         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
326                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_component='%s'", node->value);
327                 break;
328         case E_PMINFO_APPINFO_PROP_APP_EXEC:
329                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_exec='%s'", node->value);
330                 break;
331         case E_PMINFO_APPINFO_PROP_APP_ICON:
332                 snprintf(buf, MAX_QUERY_LEN, "package_app_localized_info.app_icon='%s'", node->value);
333                 break;
334         case E_PMINFO_APPINFO_PROP_APP_TYPE:
335                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_type='%s'", node->value);
336                 break;
337         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
338                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
339                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.operation IN %s", temp);
340                 break;
341         case E_PMINFO_APPINFO_PROP_APP_URI:
342                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
343                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.uri_scheme IN %s", temp);
344                 break;
345         case E_PMINFO_APPINFO_PROP_APP_MIME:
346                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
347                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.mime_type IN %s", temp);
348                 break;
349         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
350                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
351                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_category.category IN %s", temp);
352                 break;
353         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
354                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_nodisplay IN %s", node->value);
355                 break;
356         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
357                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_multiple IN %s", node->value);
358                 break;
359         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
360                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_onboot IN %s", node->value);
361                 break;
362         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
363                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_autorestart IN %s", node->value);
364                 break;
365         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
366                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_taskmanage IN %s", node->value);
367                 break;
368         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
369                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_hwacceleration='%s'", node->value);
370                 break;
371         case E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION:
372                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_launchcondition IN %s", node->value);
373                 break;
374         default:
375                 _LOGE("Invalid Property Type\n");
376                 *condition = NULL;
377                 return;
378         }
379         *condition = strdup(buf);
380         return;
381 }
382
383 static char* __convert_system_locale_to_manifest_locale(char *syslocale)
384 {
385         if (syslocale == NULL)
386                 return strdup(DEFAULT_LOCALE);
387         char *locale = NULL;
388         locale = (char *)calloc(1, 6);
389         retvm_if(!locale, NULL, "Malloc Failed\n");
390
391         strncpy(locale, syslocale, 2);
392         strncat(locale, "-", 1);
393         locale[3] = syslocale[3] + 32;
394         locale[4] = syslocale[4] + 32;
395         return locale;
396 }
397
398 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
399 {
400         ret_if(data == NULL);
401         if (data->locale){
402                 free((void *)data->locale);
403                 data->locale = NULL;
404         }
405
406         pkgmgr_parser_free_manifest_xml(data->manifest_info);
407         free((void *)data);
408         data = NULL;
409         return;
410 }
411
412 static void __cleanup_appinfo(pkgmgr_appinfo_x *data)
413 {
414         ret_if(data == NULL);
415         if (data->package){
416                 free((void *)data->package);
417                 data->package = NULL;
418         }
419         if (data->locale){
420                 free((void *)data->locale);
421                 data->locale = NULL;
422         }
423
424         manifest_x *mfx = calloc(1, sizeof(manifest_x));
425         if (data->app_component == PMINFO_UI_APP)
426                 mfx->uiapplication = data->uiapp_info;
427         else if (data->app_component == PMINFO_SVC_APP)
428                 mfx->serviceapplication = data->svcapp_info;
429         pkgmgr_parser_free_manifest_xml(mfx);
430         free((void *)data);
431         data = NULL;
432         return;
433 }
434
435 static int __open_manifest_db()
436 {
437         int ret = -1;
438         if (access(MANIFEST_DB, F_OK) == 0) {
439                 ret =
440                     db_util_open_with_options(MANIFEST_DB, &manifest_db,
441                                  SQLITE_OPEN_READONLY, NULL);
442                 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", MANIFEST_DB);
443                 return 0;
444         }
445         _LOGE("Manifest DB does not exists !!\n");
446         return -1;
447 }
448
449 static int __open_datacontrol_db()
450 {
451         int ret = -1;
452         if (access(DATACONTROL_DB, F_OK) == 0) {
453                 ret =
454                     db_util_open_with_options(DATACONTROL_DB, &datacontrol_db,
455                                  SQLITE_OPEN_READONLY, NULL);
456                 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", DATACONTROL_DB);
457                 return 0;
458         }
459         _LOGE("Datacontrol DB does not exists !!\n");
460         return -1;
461 }
462
463 static int __pkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
464 {
465         pkgmgr_pkginfo_x *udata = (pkgmgr_pkginfo_x *)data;
466         int i = 0;
467         pkgmgr_pkginfo_x *info = NULL;
468         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
469         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
470
471         LISTADD(udata, info);
472         for(i = 0; i < ncols; i++)
473         {
474                 if (strcmp(colname[i], "package") == 0) {
475                         if (coltxt[i])
476                                 info->manifest_info->package = strdup(coltxt[i]);
477                         else
478                                 info->manifest_info->package = NULL;
479                 } else
480                         continue;
481         }
482
483         return 0;
484 }
485
486 static int __app_list_cb(void *data, int ncols, char **coltxt, char **colname)
487 {
488         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
489         int i = 0;
490         int j = 0;
491         uiapplication_x *uiapp = NULL;
492         serviceapplication_x *svcapp = NULL;
493         for(i = 0; i < ncols; i++)
494         {
495                 if ((strcmp(colname[i], "app_component") == 0) ||
496                         (strcmp(colname[i], "package_app_info.app_component") == 0)) {
497                         if (coltxt[i]) {
498                                 if (strcmp(coltxt[i], "uiapp") == 0) {
499                                         uiapp = calloc(1, sizeof(uiapplication_x));
500                                         if (uiapp == NULL) {
501                                                 _LOGE("Out of Memory!!!\n");
502                                                 return -1;
503                                         }
504                                         LISTADD(info->manifest_info->uiapplication, uiapp);
505                                         for(j = 0; j < ncols; j++)
506                                         {
507                                                 if ((strcmp(colname[j], "app_id") == 0) ||
508                                                         (strcmp(colname[j], "package_app_info.app_id") == 0)) {
509                                                         if (coltxt[j])
510                                                                 info->manifest_info->uiapplication->appid = strdup(coltxt[j]);
511                                                 } else if (strcmp(colname[j], "package") == 0) {
512                                                         if (coltxt[j])
513                                                                 info->manifest_info->uiapplication->package = strdup(coltxt[j]);
514                                                 } else
515                                                         continue;
516                                         }
517                                 } else {
518                                         svcapp = calloc(1, sizeof(serviceapplication_x));
519                                         if (svcapp == NULL) {
520                                                 _LOGE("Out of Memory!!!\n");
521                                                 return -1;
522                                         }
523                                         LISTADD(info->manifest_info->serviceapplication, svcapp);
524                                         for(j = 0; j < ncols; j++)
525                                         {
526                                                 if ((strcmp(colname[j], "app_id") == 0) ||
527                                                         (strcmp(colname[j], "package_app_info.app_id") == 0)) {
528                                                         if (coltxt[j])
529                                                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[j]);
530                                                 } else if (strcmp(colname[j], "package") == 0) {
531                                                         if (coltxt[j])
532                                                                 info->manifest_info->serviceapplication->package = strdup(coltxt[j]);
533                                                 } else
534                                                         continue;
535                                         }
536                                 }
537                         }
538                 } else
539                         continue;
540         }
541
542         return 0;
543 }
544
545
546 static int __uiapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
547 {
548         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
549         int i = 0;
550         uiapplication_x *uiapp = NULL;
551         icon_x *icon = NULL;
552         label_x *label = NULL;
553
554         uiapp = calloc(1, sizeof(uiapplication_x));
555         LISTADD(info->manifest_info->uiapplication, uiapp);
556         icon = calloc(1, sizeof(icon_x));
557         LISTADD(info->manifest_info->uiapplication->icon, icon);
558         label = calloc(1, sizeof(label_x));
559         LISTADD(info->manifest_info->uiapplication->label, label);
560
561         for(i = 0; i < ncols; i++)
562         {
563                 if (strcmp(colname[i], "app_id") == 0) {
564                         if (coltxt[i])
565                                 info->manifest_info->uiapplication->appid = strdup(coltxt[i]);
566                         else
567                                 info->manifest_info->uiapplication->appid = NULL;
568                 } else if (strcmp(colname[i], "app_exec") == 0) {
569                         if (coltxt[i])
570                                 info->manifest_info->uiapplication->exec = strdup(coltxt[i]);
571                         else
572                                 info->manifest_info->uiapplication->exec = NULL;
573                 } else if (strcmp(colname[i], "app_type") == 0 ){
574                         if (coltxt[i])
575                                 info->manifest_info->uiapplication->type = strdup(coltxt[i]);
576                         else
577                                 info->manifest_info->uiapplication->type = NULL;
578                 } else if (strcmp(colname[i], "app_nodisplay") == 0 ){
579                         if (coltxt[i])
580                                 info->manifest_info->uiapplication->nodisplay = strdup(coltxt[i]);
581                         else
582                                 info->manifest_info->uiapplication->nodisplay = NULL;
583                 } else if (strcmp(colname[i], "app_multiple") == 0 ){
584                         if (coltxt[i])
585                                 info->manifest_info->uiapplication->multiple = strdup(coltxt[i]);
586                         else
587                                 info->manifest_info->uiapplication->multiple = NULL;
588                 } else if (strcmp(colname[i], "app_taskmanage") == 0 ){
589                         if (coltxt[i])
590                                 info->manifest_info->uiapplication->taskmanage = strdup(coltxt[i]);
591                         else
592                                 info->manifest_info->uiapplication->taskmanage = NULL;
593                 } else if (strcmp(colname[i], "app_hwacceleration") == 0 ){
594                         if (coltxt[i])
595                                 info->manifest_info->uiapplication->hwacceleration = strdup(coltxt[i]);
596                         else
597                                 info->manifest_info->uiapplication->hwacceleration = NULL;
598                 } else if (strcmp(colname[i], "app_indicatordisplay") == 0 ){
599                         if (coltxt[i])
600                                 info->manifest_info->uiapplication->indicatordisplay = strdup(coltxt[i]);
601                         else
602                                 info->manifest_info->uiapplication->indicatordisplay = NULL;
603                 } else if (strcmp(colname[i], "app_portraitimg") == 0 ){
604                         if (coltxt[i])
605                                 info->manifest_info->uiapplication->portraitimg = strdup(coltxt[i]);
606                         else
607                                 info->manifest_info->uiapplication->portraitimg = NULL;
608                 } else if (strcmp(colname[i], "app_landscapeimg") == 0 ){
609                         if (coltxt[i])
610                                 info->manifest_info->uiapplication->landscapeimg = strdup(coltxt[i]);
611                         else
612                                 info->manifest_info->uiapplication->landscapeimg = NULL;
613                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0 ){
614                         if (coltxt[i])
615                                 info->manifest_info->uiapplication->guestmode_visibility = strdup(coltxt[i]);
616                         else
617                                 info->manifest_info->uiapplication->guestmode_visibility = NULL;
618                 } else if (strcmp(colname[i], "package") == 0 ){
619                         if (coltxt[i])
620                                 info->manifest_info->uiapplication->package = strdup(coltxt[i]);
621                         else
622                                 info->manifest_info->uiapplication->package = NULL;
623                 } else if (strcmp(colname[i], "app_icon") == 0) {
624                         if (coltxt[i])
625                                 info->manifest_info->uiapplication->icon->text = strdup(coltxt[i]);
626                         else
627                                 info->manifest_info->uiapplication->icon->text = NULL;
628                 } else if (strcmp(colname[i], "app_enabled") == 0 ) {
629                         if (coltxt[i])
630                                 info->manifest_info->uiapplication->enabled= strdup(coltxt[i]);
631                         else
632                                 info->manifest_info->uiapplication->enabled = NULL;
633                 } else if (strcmp(colname[i], "app_label") == 0 ) {
634                         if (coltxt[i])
635                                 info->manifest_info->uiapplication->label->text = strdup(coltxt[i]);
636                         else
637                                 info->manifest_info->uiapplication->label->text = NULL;
638                 } else if (strcmp(colname[i], "app_recentimage") == 0 ) {
639                         if (coltxt[i])
640                                 info->manifest_info->uiapplication->recentimage = strdup(coltxt[i]);
641                         else
642                                 info->manifest_info->uiapplication->recentimage = NULL;
643                 } else if (strcmp(colname[i], "app_mainapp") == 0 ) {
644                         if (coltxt[i])
645                                 info->manifest_info->uiapplication->mainapp = strdup(coltxt[i]);
646                         else
647                                 info->manifest_info->uiapplication->mainapp = NULL;
648                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
649                         if (coltxt[i]) {
650                                 info->manifest_info->uiapplication->icon->lang = strdup(coltxt[i]);
651                                 info->manifest_info->uiapplication->label->lang = strdup(coltxt[i]);
652                         }
653                         else {
654                                 info->manifest_info->uiapplication->icon->lang = NULL;
655                                 info->manifest_info->uiapplication->label->lang = NULL;
656                         }
657                 } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
658                         if (coltxt[i])
659                                 info->manifest_info->uiapplication->permission_type = strdup(coltxt[i]);
660                         else
661                                 info->manifest_info->uiapplication->permission_type = NULL;
662                 } else if (strcmp(colname[i], "component_type") == 0 ) {
663                         if (coltxt[i])
664                                 info->manifest_info->uiapplication->component_type = strdup(coltxt[i]);
665                         else
666                                 info->manifest_info->uiapplication->component_type = NULL;
667                 } else if (strcmp(colname[i], "app_preload") == 0 ) {
668                         if (coltxt[i])
669                                 info->manifest_info->uiapplication->preload = strdup(coltxt[i]);
670                         else
671                                 info->manifest_info->uiapplication->preload = NULL;
672                 } else if (strcmp(colname[i], "app_submode") == 0 ) {
673                         if (coltxt[i])
674                                 info->manifest_info->uiapplication->submode = strdup(coltxt[i]);
675                         else
676                                 info->manifest_info->uiapplication->submode = NULL;
677                 } else if (strcmp(colname[i], "app_submode_mainid") == 0 ) {
678                         if (coltxt[i])
679                                 info->manifest_info->uiapplication->submode_mainid = strdup(coltxt[i]);
680                         else
681                                 info->manifest_info->uiapplication->submode_mainid = NULL;
682                 } else
683                         continue;
684         }
685         return 0;
686 }
687
688 static int __svcapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
689 {
690         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
691         int i = 0;
692         serviceapplication_x *svcapp = NULL;
693         icon_x *icon = NULL;
694         label_x *label = NULL;
695
696         svcapp = calloc(1, sizeof(serviceapplication_x));
697         LISTADD(info->manifest_info->serviceapplication, svcapp);
698         icon = calloc(1, sizeof(icon_x));
699         LISTADD(info->manifest_info->serviceapplication->icon, icon);
700         label = calloc(1, sizeof(label_x));
701         LISTADD(info->manifest_info->serviceapplication->label, label);
702         for(i = 0; i < ncols; i++)
703         {
704                 if (strcmp(colname[i], "app_id") == 0) {
705                         if (coltxt[i])
706                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[i]);
707                         else
708                                 info->manifest_info->serviceapplication->appid = NULL;
709                 } else if (strcmp(colname[i], "app_exec") == 0) {
710                         if (coltxt[i])
711                                 info->manifest_info->serviceapplication->exec = strdup(coltxt[i]);
712                         else
713                                 info->manifest_info->serviceapplication->exec = NULL;
714                 } else if (strcmp(colname[i], "app_type") == 0 ){
715                         if (coltxt[i])
716                                 info->manifest_info->serviceapplication->type = strdup(coltxt[i]);
717                         else
718                                 info->manifest_info->serviceapplication->type = NULL;
719                 } else if (strcmp(colname[i], "app_onboot") == 0 ){
720                         if (coltxt[i])
721                                 info->manifest_info->serviceapplication->onboot = strdup(coltxt[i]);
722                         else
723                                 info->manifest_info->serviceapplication->onboot = NULL;
724                 } else if (strcmp(colname[i], "app_autorestart") == 0 ){
725                         if (coltxt[i])
726                                 info->manifest_info->serviceapplication->autorestart = strdup(coltxt[i]);
727                         else
728                                 info->manifest_info->serviceapplication->autorestart = NULL;
729                 } else if (strcmp(colname[i], "package") == 0 ){
730                         if (coltxt[i])
731                                 info->manifest_info->serviceapplication->package = strdup(coltxt[i]);
732                         else
733                                 info->manifest_info->serviceapplication->package = NULL;
734                 } else if (strcmp(colname[i], "app_icon") == 0) {
735                         if (coltxt[i])
736                                 info->manifest_info->serviceapplication->icon->text = strdup(coltxt[i]);
737                         else
738                                 info->manifest_info->serviceapplication->icon->text = NULL;
739                 } else if (strcmp(colname[i], "app_label") == 0 ) {
740                         if (coltxt[i])
741                                 info->manifest_info->serviceapplication->label->text = strdup(coltxt[i]);
742                         else
743                                 info->manifest_info->serviceapplication->label->text = NULL;
744                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
745                         if (coltxt[i]) {
746                                 info->manifest_info->serviceapplication->icon->lang = strdup(coltxt[i]);
747                                 info->manifest_info->serviceapplication->label->lang = strdup(coltxt[i]);
748                         }
749                         else {
750                                 info->manifest_info->serviceapplication->icon->lang = NULL;
751                                 info->manifest_info->serviceapplication->label->lang = NULL;
752                         }
753                 } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
754                         if (coltxt[i])
755                                 info->manifest_info->serviceapplication->permission_type = strdup(coltxt[i]);
756                         else
757                                 info->manifest_info->serviceapplication->permission_type = NULL;
758                 } else
759                         continue;
760         }
761         return 0;
762 }
763
764 static int __allapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
765 {
766         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
767         int i = 0;
768         int j = 0;
769         uiapplication_x *uiapp = NULL;
770         serviceapplication_x *svcapp = NULL;
771         for(j = 0; j < ncols; j++)
772         {
773                 if (strcmp(colname[j], "app_component") == 0) {
774                         if (coltxt[j]) {
775                                 if (strcmp(coltxt[j], "uiapp") == 0) {
776                                         uiapp = calloc(1, sizeof(uiapplication_x));
777                                         if (uiapp == NULL) {
778                                                 _LOGE("Out of Memory!!!\n");
779                                                 return -1;
780                                         }
781                                         LISTADD(info->manifest_info->uiapplication, uiapp);
782                                         for(i = 0; i < ncols; i++)
783                                         {
784                                                 if (strcmp(colname[i], "app_id") == 0) {
785                                                         if (coltxt[i])
786                                                                 info->manifest_info->uiapplication->appid = strdup(coltxt[i]);
787                                                         else
788                                                                 info->manifest_info->uiapplication->appid = NULL;
789                                                 } else if (strcmp(colname[i], "app_exec") == 0) {
790                                                         if (coltxt[i])
791                                                                 info->manifest_info->uiapplication->exec = strdup(coltxt[i]);
792                                                         else
793                                                                 info->manifest_info->uiapplication->exec = NULL;
794                                                 } else if (strcmp(colname[i], "app_type") == 0 ){
795                                                         if (coltxt[i])
796                                                                 info->manifest_info->uiapplication->type = strdup(coltxt[i]);
797                                                         else
798                                                                 info->manifest_info->uiapplication->type = NULL;
799                                                 } else if (strcmp(colname[i], "app_nodisplay") == 0 ){
800                                                         if (coltxt[i])
801                                                                 info->manifest_info->uiapplication->nodisplay = strdup(coltxt[i]);
802                                                         else
803                                                                 info->manifest_info->uiapplication->nodisplay = NULL;
804                                                 } else if (strcmp(colname[i], "app_multiple") == 0 ){
805                                                         if (coltxt[i])
806                                                                 info->manifest_info->uiapplication->multiple = strdup(coltxt[i]);
807                                                         else
808                                                                 info->manifest_info->uiapplication->multiple = NULL;
809                                                 } else if (strcmp(colname[i], "app_taskmanage") == 0 ){
810                                                         if (coltxt[i])
811                                                                 info->manifest_info->uiapplication->taskmanage = strdup(coltxt[i]);
812                                                         else
813                                                                 info->manifest_info->uiapplication->taskmanage = NULL;
814                                                 } else if (strcmp(colname[i], "app_hwacceleration") == 0 ){
815                                                         if (coltxt[i])
816                                                                 info->manifest_info->uiapplication->hwacceleration = strdup(coltxt[i]);
817                                                         else
818                                                                 info->manifest_info->uiapplication->hwacceleration = NULL;
819                                                 } else if (strcmp(colname[i], "app_indicatordisplay") == 0 ){
820                                                         if (coltxt[i])
821                                                                 info->manifest_info->uiapplication->indicatordisplay = strdup(coltxt[i]);
822                                                         else
823                                                                 info->manifest_info->uiapplication->indicatordisplay = NULL;
824                                                 } else if (strcmp(colname[i], "app_portraitimg") == 0 ){
825                                                         if (coltxt[i])
826                                                                 info->manifest_info->uiapplication->portraitimg = strdup(coltxt[i]);
827                                                         else
828                                                                 info->manifest_info->uiapplication->portraitimg = NULL;
829                                                 } else if (strcmp(colname[i], "app_landscapeimg") == 0 ){
830                                                         if (coltxt[i])
831                                                                 info->manifest_info->uiapplication->landscapeimg = strdup(coltxt[i]);
832                                                         else
833                                                                 info->manifest_info->uiapplication->landscapeimg = NULL;
834                                                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0 ){
835                                                         if (coltxt[i])
836                                                                 info->manifest_info->uiapplication->guestmode_visibility = strdup(coltxt[i]);
837                                                         else
838                                                                 info->manifest_info->uiapplication->guestmode_visibility = NULL;
839                                                 } else if (strcmp(colname[i], "package") == 0 ){
840                                                         if (coltxt[i])
841                                                                 info->manifest_info->uiapplication->package = strdup(coltxt[i]);
842                                                         else
843                                                                 info->manifest_info->uiapplication->package = NULL;
844                                                 } else if (strcmp(colname[i], "app_icon") == 0) {
845                                                         if (coltxt[i])
846                                                                 info->manifest_info->uiapplication->icon->text = strdup(coltxt[i]);
847                                                         else
848                                                                 info->manifest_info->uiapplication->icon->text = NULL;
849                                                 } else if (strcmp(colname[i], "app_label") == 0 ) {
850                                                         if (coltxt[i])
851                                                                 info->manifest_info->uiapplication->label->text = strdup(coltxt[i]);
852                                                         else
853                                                                 info->manifest_info->uiapplication->label->text = NULL;
854                                                 } else if (strcmp(colname[i], "app_recentimage") == 0 ) {
855                                                         if (coltxt[i])
856                                                                 info->manifest_info->uiapplication->recentimage = strdup(coltxt[i]);
857                                                         else
858                                                                 info->manifest_info->uiapplication->recentimage = NULL;
859                                                 } else if (strcmp(colname[i], "app_mainapp") == 0 ) {
860                                                         if (coltxt[i])
861                                                                 info->manifest_info->uiapplication->mainapp= strdup(coltxt[i]);
862                                                         else
863                                                                 info->manifest_info->uiapplication->mainapp = NULL;
864                                                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
865                                                         if (coltxt[i]) {
866                                                                 info->manifest_info->uiapplication->icon->lang = strdup(coltxt[i]);
867                                                                 info->manifest_info->uiapplication->label->lang = strdup(coltxt[i]);
868                                                         }
869                                                         else {
870                                                                 info->manifest_info->uiapplication->icon->lang = NULL;
871                                                                 info->manifest_info->uiapplication->label->lang = NULL;
872                                                         }
873                                                 } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
874                                                         if (coltxt[i])
875                                                                 info->manifest_info->uiapplication->permission_type = strdup(coltxt[i]);
876                                                         else
877                                                                 info->manifest_info->uiapplication->permission_type = NULL;
878                                                 } else
879                                                         continue;
880                                         }
881                                 } else {
882                                         svcapp = calloc(1, sizeof(serviceapplication_x));
883                                         if (svcapp == NULL) {
884                                                 _LOGE("Out of Memory!!!\n");
885                                                 return -1;
886                                         }
887                                         LISTADD(info->manifest_info->serviceapplication, svcapp);
888                                         for(i = 0; i < ncols; i++)
889                                         {
890                                                 if (strcmp(colname[i], "app_id") == 0) {
891                                                         if (coltxt[i])
892                                                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[i]);
893                                                         else
894                                                                 info->manifest_info->serviceapplication->appid = NULL;
895                                                 } else if (strcmp(colname[i], "app_exec") == 0) {
896                                                         if (coltxt[i])
897                                                                 info->manifest_info->serviceapplication->exec = strdup(coltxt[i]);
898                                                         else
899                                                                 info->manifest_info->serviceapplication->exec = NULL;
900                                                 } else if (strcmp(colname[i], "app_type") == 0 ){
901                                                         if (coltxt[i])
902                                                                 info->manifest_info->serviceapplication->type = strdup(coltxt[i]);
903                                                         else
904                                                                 info->manifest_info->serviceapplication->type = NULL;
905                                                 } else if (strcmp(colname[i], "app_onboot") == 0 ){
906                                                         if (coltxt[i])
907                                                                 info->manifest_info->serviceapplication->onboot = strdup(coltxt[i]);
908                                                         else
909                                                                 info->manifest_info->serviceapplication->onboot = NULL;
910                                                 } else if (strcmp(colname[i], "app_autorestart") == 0 ){
911                                                         if (coltxt[i])
912                                                                 info->manifest_info->serviceapplication->autorestart = strdup(coltxt[i]);
913                                                         else
914                                                                 info->manifest_info->serviceapplication->autorestart = NULL;
915                                                 } else if (strcmp(colname[i], "package") == 0 ){
916                                                         if (coltxt[i])
917                                                                 info->manifest_info->serviceapplication->package = strdup(coltxt[i]);
918                                                         else
919                                                                 info->manifest_info->serviceapplication->package = NULL;
920                                                 } else if (strcmp(colname[i], "app_icon") == 0) {
921                                                         if (coltxt[i])
922                                                                 info->manifest_info->serviceapplication->icon->text = strdup(coltxt[i]);
923                                                         else
924                                                                 info->manifest_info->serviceapplication->icon->text = NULL;
925                                                 } else if (strcmp(colname[i], "app_label") == 0 ) {
926                                                         if (coltxt[i])
927                                                                 info->manifest_info->serviceapplication->label->text = strdup(coltxt[i]);
928                                                         else
929                                                                 info->manifest_info->serviceapplication->label->text = NULL;
930                                                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
931                                                         if (coltxt[i]) {
932                                                                 info->manifest_info->serviceapplication->icon->lang = strdup(coltxt[i]);
933                                                                 info->manifest_info->serviceapplication->label->lang = strdup(coltxt[i]);
934                                                         }
935                                                         else {
936                                                                 info->manifest_info->serviceapplication->icon->lang = NULL;
937                                                                 info->manifest_info->serviceapplication->label->lang = NULL;
938                                                         }
939                                                 } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
940                                                         if (coltxt[i])
941                                                                 info->manifest_info->serviceapplication->permission_type = strdup(coltxt[i]);
942                                                         else
943                                                                 info->manifest_info->serviceapplication->permission_type = NULL;
944                                                 } else
945                                                         continue;
946                                         }
947                                 }
948                         }
949                 } else
950                         continue;
951         }
952
953         return 0;
954 }
955
956
957
958 static int __validate_cb(void *data, int ncols, char **coltxt, char **colname)
959 {
960         int *p = (int*)data;
961         *p = atoi(coltxt[0]);
962         return 0;
963 }
964
965 static int __maxid_cb(void *data, int ncols, char **coltxt, char **colname)
966 {
967         int *p = (int*)data;
968         if (coltxt[0])
969                 *p = atoi(coltxt[0]);
970         return 0;
971 }
972
973 static int __pkginfo_cb(void *data, int ncols, char **coltxt, char **colname)
974 {
975         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
976         int i = 0;
977         author_x *author = NULL;
978         icon_x *icon = NULL;
979         label_x *label = NULL;
980         description_x *description = NULL;
981         privilege_x *privilege = NULL;
982
983         author = calloc(1, sizeof(author_x));
984         LISTADD(info->manifest_info->author, author);
985         icon = calloc(1, sizeof(icon_x));
986         LISTADD(info->manifest_info->icon, icon);
987         label = calloc(1, sizeof(label_x));
988         LISTADD(info->manifest_info->label, label);
989         description = calloc(1, sizeof(description_x));
990         LISTADD(info->manifest_info->description, description);
991         privilege = calloc(1, sizeof(privilege_x));
992         LISTADD(info->manifest_info->privileges->privilege, privilege);
993         for(i = 0; i < ncols; i++)
994         {
995                 if (strcmp(colname[i], "package_version") == 0) {
996                         if (coltxt[i])
997                                 info->manifest_info->version = strdup(coltxt[i]);
998                         else
999                                 info->manifest_info->version = NULL;
1000                 } else if (strcmp(colname[i], "package_type") == 0) {
1001                         if (coltxt[i])
1002                                 info->manifest_info->type = strdup(coltxt[i]);
1003                         else
1004                                 info->manifest_info->type = NULL;
1005                 } else if (strcmp(colname[i], "install_location") == 0) {
1006                         if (coltxt[i])
1007                                 info->manifest_info->installlocation = strdup(coltxt[i]);
1008                         else
1009                                 info->manifest_info->installlocation = NULL;
1010                 } else if (strcmp(colname[i], "package_size") == 0) {
1011                         if (coltxt[i])
1012                                 info->manifest_info->package_size = strdup(coltxt[i]);
1013                         else
1014                                 info->manifest_info->package_size = NULL;
1015                 } else if (strcmp(colname[i], "author_email") == 0 ){
1016                         if (coltxt[i])
1017                                 info->manifest_info->author->email = strdup(coltxt[i]);
1018                         else
1019                                 info->manifest_info->author->email = NULL;
1020                 } else if (strcmp(colname[i], "author_href") == 0 ){
1021                         if (coltxt[i])
1022                                 info->manifest_info->author->href = strdup(coltxt[i]);
1023                         else
1024                                 info->manifest_info->author->href = NULL;
1025                 } else if (strcmp(colname[i], "package_label") == 0 ){
1026                         if (coltxt[i])
1027                                 info->manifest_info->label->text = strdup(coltxt[i]);
1028                         else
1029                                 info->manifest_info->label->text = NULL;
1030                 } else if (strcmp(colname[i], "package_icon") == 0 ){
1031                         if (coltxt[i])
1032                                 info->manifest_info->icon->text = strdup(coltxt[i]);
1033                         else
1034                                 info->manifest_info->icon->text = NULL;
1035                 } else if (strcmp(colname[i], "package_description") == 0 ){
1036                         if (coltxt[i])
1037                                 info->manifest_info->description->text = strdup(coltxt[i]);
1038                         else
1039                                 info->manifest_info->description->text = NULL;
1040                 } else if (strcmp(colname[i], "package_author") == 0 ){
1041                         if (coltxt[i])
1042                                 info->manifest_info->author->text = strdup(coltxt[i]);
1043                         else
1044                                 info->manifest_info->author->text = NULL;
1045                 } else if (strcmp(colname[i], "package_removable") == 0 ){
1046                         if (coltxt[i])
1047                                 info->manifest_info->removable = strdup(coltxt[i]);
1048                         else
1049                                 info->manifest_info->removable = NULL;
1050                 } else if (strcmp(colname[i], "package_preload") == 0 ){
1051                         if (coltxt[i])
1052                                 info->manifest_info->preload = strdup(coltxt[i]);
1053                         else
1054                                 info->manifest_info->preload = NULL;
1055                 } else if (strcmp(colname[i], "package_readonly") == 0 ){
1056                         if (coltxt[i])
1057                                 info->manifest_info->readonly = strdup(coltxt[i]);
1058                         else
1059                                 info->manifest_info->readonly = NULL;
1060                 } else if (strcmp(colname[i], "package_update") == 0 ){
1061                         if (coltxt[i])
1062                                 info->manifest_info->update= strdup(coltxt[i]);
1063                         else
1064                                 info->manifest_info->update = NULL;
1065                 } else if (strcmp(colname[i], "package_appsetting") == 0 ){
1066                         if (coltxt[i])
1067                                 info->manifest_info->appsetting = strdup(coltxt[i]);
1068                         else
1069                                 info->manifest_info->appsetting = NULL;
1070                 } else if (strcmp(colname[i], "installed_time") == 0 ){
1071                         if (coltxt[i])
1072                                 info->manifest_info->installed_time = strdup(coltxt[i]);
1073                         else
1074                                 info->manifest_info->installed_time = NULL;
1075                 } else if (strcmp(colname[i], "mainapp_id") == 0 ){
1076                         if (coltxt[i])
1077                                 info->manifest_info->mainapp_id = strdup(coltxt[i]);
1078                         else
1079                                 info->manifest_info->mainapp_id = NULL;
1080                 } else if (strcmp(colname[i], "root_path") == 0 ){
1081                         if (coltxt[i])
1082                                 info->manifest_info->root_path = strdup(coltxt[i]);
1083                         else
1084                                 info->manifest_info->root_path = NULL;
1085                 } else if (strcmp(colname[i], "csc_path") == 0 ){
1086                         if (coltxt[i])
1087                                 info->manifest_info->csc_path = strdup(coltxt[i]);
1088                         else
1089                                 info->manifest_info->csc_path = NULL;
1090                 } else if (strcmp(colname[i], "privilege") == 0 ){
1091                         if (coltxt[i])
1092                                 info->manifest_info->privileges->privilege->text = strdup(coltxt[i]);
1093                         else
1094                                 info->manifest_info->privileges->privilege->text = NULL;
1095                 } else if (strcmp(colname[i], "package_locale") == 0 ){
1096                         if (coltxt[i]) {
1097                                 info->manifest_info->author->lang = strdup(coltxt[i]);
1098                                 info->manifest_info->icon->lang = strdup(coltxt[i]);
1099                                 info->manifest_info->label->lang = strdup(coltxt[i]);
1100                                 info->manifest_info->description->lang = strdup(coltxt[i]);
1101                         }
1102                         else {
1103                                 info->manifest_info->author->lang = NULL;
1104                                 info->manifest_info->icon->lang = NULL;
1105                                 info->manifest_info->label->lang = NULL;
1106                                 info->manifest_info->description->lang = NULL;
1107                         }
1108                 } else if (strcmp(colname[i], "package_url") == 0 ){
1109                         if (coltxt[i])
1110                                 info->manifest_info->package_url = strdup(coltxt[i]);
1111                         else
1112                                 info->manifest_info->package_url = NULL;
1113                 } else
1114                         continue;
1115         }
1116         return 0;
1117 }
1118
1119
1120 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1121 {
1122         if ( strcasecmp(comp, "uiapp") == 0)
1123                 return PMINFO_UI_APP;
1124         else if ( strcasecmp(comp, "svcapp") == 0)
1125                 return PMINFO_SVC_APP;
1126         else
1127                 return -1;
1128 }
1129
1130 static int __certindexinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1131 {
1132         pkgmgr_certindexinfo_x *info = (pkgmgr_certindexinfo_x *)data;
1133         int i = 0;
1134         for(i = 0; i < ncols; i++) {
1135                 if (strcmp(colname[i], "cert_id") == 0) {
1136                         if (coltxt[i])
1137                                 info->cert_id = atoi(coltxt[i]);
1138                         else
1139                                 info->cert_id = 0;
1140                 } else if (strcmp(colname[i], "cert_ref_count") == 0) {
1141                         if (coltxt[i])
1142                                 info->cert_ref_count = atoi(coltxt[i]);
1143                         else
1144                                 info->cert_ref_count = 0;
1145                 } else
1146                         continue;
1147         }
1148         return 0;
1149 }
1150 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1151 {
1152         pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)data;
1153         int i = 0;
1154         for(i = 0; i < ncols; i++)
1155         {
1156                 if (strcmp(colname[i], "package") == 0) {
1157                         if (coltxt[i])
1158                                 info->pkgid = strdup(coltxt[i]);
1159                         else
1160                                 info->pkgid = NULL;
1161                 } else if (strcmp(colname[i], "author_signer_cert") == 0) {
1162                         if (coltxt[i])
1163                                 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = atoi(coltxt[i]);
1164                         else
1165                                 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = 0;
1166                 } else if (strcmp(colname[i], "author_im_cert") == 0) {
1167                         if (coltxt[i])
1168                                 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
1169                         else
1170                                 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = 0;
1171                 } else if (strcmp(colname[i], "author_root_cert") == 0) {
1172                         if (coltxt[i])
1173                                 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = atoi(coltxt[i]);
1174                         else
1175                                 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = 0;
1176                 } else if (strcmp(colname[i], "dist_signer_cert") == 0 ){
1177                         if (coltxt[i])
1178                                 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = atoi(coltxt[i]);
1179                         else
1180                                 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = 0;
1181                 } else if (strcmp(colname[i], "dist_im_cert") == 0 ){
1182                         if (coltxt[i])
1183                                 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
1184                         else
1185                                 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = 0;
1186                 } else if (strcmp(colname[i], "dist_root_cert") == 0 ){
1187                         if (coltxt[i])
1188                                 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = atoi(coltxt[i]);
1189                         else
1190                                 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = 0;
1191                 } else if (strcmp(colname[i], "dist2_signer_cert") == 0 ){
1192                         if (coltxt[i])
1193                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = atoi(coltxt[i]);
1194                         else
1195                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = 0;
1196                 } else if (strcmp(colname[i], "dist2_im_cert") == 0 ){
1197                         if (coltxt[i])
1198                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = atoi(coltxt[i]);
1199                         else
1200                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = 0;
1201                 } else if (strcmp(colname[i], "dist2_root_cert") == 0 ){
1202                         if (coltxt[i])
1203                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = atoi(coltxt[i]);
1204                         else
1205                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = 0;
1206                 } else if (strcmp(colname[i], "cert_info") == 0 ){
1207                         if (coltxt[i])
1208                                 info->cert_value = strdup(coltxt[i]);
1209                         else
1210                                 info->cert_value = NULL;
1211                 } else
1212                         continue;
1213         }
1214         return 0;
1215 }
1216
1217 static int __mini_appinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1218 {
1219         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
1220         int i = 0;
1221         uiapplication_x *uiapp = NULL;
1222         uiapp = calloc(1, sizeof(uiapplication_x));
1223         if (uiapp == NULL) {
1224                 _LOGE("Out of Memory!!!\n");
1225                 return -1;
1226         }
1227
1228         LISTADD(info->uiapp_info, uiapp);
1229
1230         for(i = 0; i < ncols; i++)
1231         {
1232                 if (strcmp(colname[i], "app_id") == 0) {
1233                         /*appid being foreign key, is column in every table
1234                         Hence appid gets strduped every time leading to memory leak.
1235                         If appid is already set, just continue.*/
1236                         if (info->uiapp_info->appid)
1237                                 continue;
1238                         if (coltxt[i])
1239                                 info->uiapp_info->appid = strdup(coltxt[i]);
1240                         else
1241                                 info->uiapp_info->appid = NULL;
1242                 } else if (strcmp(colname[i], "app_exec") == 0) {
1243                         if (coltxt[i])
1244                                 info->uiapp_info->exec = strdup(coltxt[i]);
1245                         else
1246                                 info->uiapp_info->exec = NULL;
1247                 } else if (strcmp(colname[i], "app_nodisplay") == 0) {
1248                         if (coltxt[i])
1249                                 info->uiapp_info->nodisplay = strdup(coltxt[i]);
1250                         else
1251                                 info->uiapp_info->nodisplay = NULL;
1252                 } else if (strcmp(colname[i], "app_type") == 0 ) {
1253                         if (coltxt[i])
1254                                 info->uiapp_info->type = strdup(coltxt[i]);
1255                         else
1256                                 info->uiapp_info->type = NULL;
1257                 } else if (strcmp(colname[i], "app_multiple") == 0 ) {
1258                         if (coltxt[i])
1259                                 info->uiapp_info->multiple = strdup(coltxt[i]);
1260                         else
1261                                 info->uiapp_info->multiple = NULL;
1262                 } else if (strcmp(colname[i], "app_taskmanage") == 0 ) {
1263                         if (coltxt[i])
1264                                 info->uiapp_info->taskmanage = strdup(coltxt[i]);
1265                         else
1266                                 info->uiapp_info->taskmanage = NULL;
1267                 } else if (strcmp(colname[i], "app_hwacceleration") == 0 ) {
1268                         if (coltxt[i])
1269                                 info->uiapp_info->hwacceleration = strdup(coltxt[i]);
1270                         else
1271                                 info->uiapp_info->hwacceleration = NULL;
1272                 } else if (strcmp(colname[i], "app_enabled") == 0 ) {
1273                         if (coltxt[i])
1274                                 info->uiapp_info->enabled= strdup(coltxt[i]);
1275                         else
1276                                 info->uiapp_info->enabled = NULL;
1277                 } else if (strcmp(colname[i], "app_indicatordisplay") == 0){
1278                         if (coltxt[i])
1279                                 info->uiapp_info->indicatordisplay = strdup(coltxt[i]);
1280                         else
1281                                 info->uiapp_info->indicatordisplay = NULL;
1282                 } else if (strcmp(colname[i], "app_portraitimg") == 0){
1283                         if (coltxt[i])
1284                                 info->uiapp_info->portraitimg = strdup(coltxt[i]);
1285                         else
1286                                 info->uiapp_info->portraitimg = NULL;
1287                 } else if (strcmp(colname[i], "app_landscapeimg") == 0){
1288                         if (coltxt[i])
1289                                 info->uiapp_info->landscapeimg = strdup(coltxt[i]);
1290                         else
1291                                 info->uiapp_info->landscapeimg = NULL;
1292                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0){
1293                         if (coltxt[i])
1294                                 info->uiapp_info->guestmode_visibility = strdup(coltxt[i]);
1295                         else
1296                                 info->uiapp_info->guestmode_visibility = NULL;
1297                 } else if (strcmp(colname[i], "app_recentimage") == 0 ) {
1298                         if (coltxt[i])
1299                                 info->uiapp_info->recentimage = strdup(coltxt[i]);
1300                         else
1301                                 info->uiapp_info->recentimage = NULL;
1302                 } else if (strcmp(colname[i], "app_mainapp") == 0 ) {
1303                         if (coltxt[i])
1304                                 info->uiapp_info->mainapp = strdup(coltxt[i]);
1305                         else
1306                                 info->uiapp_info->mainapp = NULL;
1307                 } else if (strcmp(colname[i], "package") == 0 ) {
1308                         if (coltxt[i])
1309                                 info->uiapp_info->package = strdup(coltxt[i]);
1310                         else
1311                                 info->uiapp_info->package = NULL;
1312                 } else if (strcmp(colname[i], "app_component") == 0) {
1313                         if (coltxt[i])
1314                                 info->uiapp_info->app_component = strdup(coltxt[i]);
1315                         else
1316                                 info->uiapp_info->app_component = NULL;
1317                 } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
1318                         if (coltxt[i])
1319                                 info->uiapp_info->permission_type = strdup(coltxt[i]);
1320                         else
1321                                 info->uiapp_info->permission_type = NULL;
1322                 } else if (strcmp(colname[i], "component_type") == 0 ) {
1323                         if (coltxt[i])
1324                                 info->uiapp_info->component_type = strdup(coltxt[i]);
1325                         else
1326                                 info->uiapp_info->component_type = NULL;
1327                 } else if (strcmp(colname[i], "app_preload") == 0 ) {
1328                         if (coltxt[i])
1329                                 info->uiapp_info->preload = strdup(coltxt[i]);
1330                         else
1331                                 info->uiapp_info->preload = NULL;
1332                 } else if (strcmp(colname[i], "app_submode") == 0 ) {
1333                         if (coltxt[i])
1334                                 info->uiapp_info->submode = strdup(coltxt[i]);
1335                         else
1336                                 info->uiapp_info->submode = NULL;
1337                 } else if (strcmp(colname[i], "app_submode_mainid") == 0 ) {
1338                         if (coltxt[i])
1339                                 info->uiapp_info->submode_mainid = strdup(coltxt[i]);
1340                         else
1341                                 info->uiapp_info->submode_mainid = NULL;
1342                 } else
1343                         continue;
1344         }
1345
1346         return 0;
1347 }
1348
1349 static int __appinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1350 {
1351         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
1352         int i = 0;
1353         icon_x *icon = NULL;
1354         label_x *label = NULL;
1355         category_x *category = NULL;
1356         metadata_x *metadata = NULL;
1357         permission_x *permission = NULL;
1358         image_x *image = NULL;
1359
1360         switch (info->app_component) {
1361         case PMINFO_UI_APP:
1362                 icon = calloc(1, sizeof(icon_x));
1363                 LISTADD(info->uiapp_info->icon, icon);
1364                 label = calloc(1, sizeof(label_x));
1365                 LISTADD(info->uiapp_info->label, label);
1366                 category = calloc(1, sizeof(category_x));
1367                 LISTADD(info->uiapp_info->category, category);
1368                 metadata = calloc(1, sizeof(metadata_x));
1369                 LISTADD(info->uiapp_info->metadata, metadata);
1370                 permission = calloc(1, sizeof(permission_x));
1371                 LISTADD(info->uiapp_info->permission, permission);
1372                 image = calloc(1, sizeof(image_x));
1373                 LISTADD(info->uiapp_info->image, image);
1374
1375                 for(i = 0; i < ncols; i++)
1376                 {
1377                         if (strcmp(colname[i], "app_id") == 0) {
1378                                 /*appid being foreign key, is column in every table
1379                                 Hence appid gets strduped every time leading to memory leak.
1380                                 If appid is already set, just continue.*/
1381                                 if (info->uiapp_info->appid)
1382                                         continue;
1383                                 if (coltxt[i])
1384                                         info->uiapp_info->appid = strdup(coltxt[i]);
1385                                 else
1386                                         info->uiapp_info->appid = NULL;
1387                         } else if (strcmp(colname[i], "app_exec") == 0) {
1388                                 if (coltxt[i])
1389                                         info->uiapp_info->exec = strdup(coltxt[i]);
1390                                 else
1391                                         info->uiapp_info->exec = NULL;
1392                         } else if (strcmp(colname[i], "app_nodisplay") == 0) {
1393                                 if (coltxt[i])
1394                                         info->uiapp_info->nodisplay = strdup(coltxt[i]);
1395                                 else
1396                                         info->uiapp_info->nodisplay = NULL;
1397                         } else if (strcmp(colname[i], "app_type") == 0 ) {
1398                                 if (coltxt[i])
1399                                         info->uiapp_info->type = strdup(coltxt[i]);
1400                                 else
1401                                         info->uiapp_info->type = NULL;
1402                         } else if (strcmp(colname[i], "app_icon_section") == 0 ) {
1403                                 if (coltxt[i])
1404                                         info->uiapp_info->icon->section= strdup(coltxt[i]);
1405                                 else
1406                                         info->uiapp_info->icon->section = NULL;
1407                         } else if (strcmp(colname[i], "app_icon") == 0) {
1408                                 if (coltxt[i])
1409                                         info->uiapp_info->icon->text = strdup(coltxt[i]);
1410                                 else
1411                                         info->uiapp_info->icon->text = NULL;
1412                         } else if (strcmp(colname[i], "app_label") == 0 ) {
1413                                 if (coltxt[i])
1414                                         info->uiapp_info->label->text = strdup(coltxt[i]);
1415                                 else
1416                                         info->uiapp_info->label->text = NULL;
1417                         } else if (strcmp(colname[i], "app_multiple") == 0 ) {
1418                                 if (coltxt[i])
1419                                         info->uiapp_info->multiple = strdup(coltxt[i]);
1420                                 else
1421                                         info->uiapp_info->multiple = NULL;
1422                         } else if (strcmp(colname[i], "app_taskmanage") == 0 ) {
1423                                 if (coltxt[i])
1424                                         info->uiapp_info->taskmanage = strdup(coltxt[i]);
1425                                 else
1426                                         info->uiapp_info->taskmanage = NULL;
1427                         } else if (strcmp(colname[i], "app_hwacceleration") == 0 ) {
1428                                 if (coltxt[i])
1429                                         info->uiapp_info->hwacceleration = strdup(coltxt[i]);
1430                                 else
1431                                         info->uiapp_info->hwacceleration = NULL;
1432                         } else if (strcmp(colname[i], "app_enabled") == 0 ) {
1433                                 if (coltxt[i])
1434                                         info->uiapp_info->enabled= strdup(coltxt[i]);
1435                                 else
1436                                         info->uiapp_info->enabled = NULL;
1437                         } else if (strcmp(colname[i], "app_indicatordisplay") == 0){
1438                                 if (coltxt[i])
1439                                         info->uiapp_info->indicatordisplay = strdup(coltxt[i]);
1440                                 else
1441                                         info->uiapp_info->indicatordisplay = NULL;
1442                         } else if (strcmp(colname[i], "app_portraitimg") == 0){
1443                                 if (coltxt[i])
1444                                         info->uiapp_info->portraitimg = strdup(coltxt[i]);
1445                                 else
1446                                         info->uiapp_info->portraitimg = NULL;
1447                         } else if (strcmp(colname[i], "app_landscapeimg") == 0){
1448                                 if (coltxt[i])
1449                                         info->uiapp_info->landscapeimg = strdup(coltxt[i]);
1450                                 else
1451                                         info->uiapp_info->landscapeimg = NULL;
1452                         } else if (strcmp(colname[i], "app_guestmodevisibility") == 0){
1453                                 if (coltxt[i])
1454                                         info->uiapp_info->guestmode_visibility = strdup(coltxt[i]);
1455                                 else
1456                                         info->uiapp_info->guestmode_visibility = NULL;
1457                         } else if (strcmp(colname[i], "category") == 0 ) {
1458                                 if (coltxt[i])
1459                                         info->uiapp_info->category->name = strdup(coltxt[i]);
1460                                 else
1461                                         info->uiapp_info->category->name = NULL;
1462                         } else if (strcmp(colname[i], "md_key") == 0 ) {
1463                                 if (coltxt[i])
1464                                         info->uiapp_info->metadata->key = strdup(coltxt[i]);
1465                                 else
1466                                         info->uiapp_info->metadata->key = NULL;
1467                         } else if (strcmp(colname[i], "md_value") == 0 ) {
1468                                 if (coltxt[i])
1469                                         info->uiapp_info->metadata->value = strdup(coltxt[i]);
1470                                 else
1471                                         info->uiapp_info->metadata->value = NULL;
1472                         } else if (strcmp(colname[i], "pm_type") == 0 ) {
1473                                 if (coltxt[i])
1474                                         info->uiapp_info->permission->type= strdup(coltxt[i]);
1475                                 else
1476                                         info->uiapp_info->permission->type = NULL;
1477                         } else if (strcmp(colname[i], "pm_value") == 0 ) {
1478                                 if (coltxt[i])
1479                                         info->uiapp_info->permission->value = strdup(coltxt[i]);
1480                                 else
1481                                         info->uiapp_info->permission->value = NULL;
1482                         } else if (strcmp(colname[i], "app_recentimage") == 0 ) {
1483                                 if (coltxt[i])
1484                                         info->uiapp_info->recentimage = strdup(coltxt[i]);
1485                                 else
1486                                         info->uiapp_info->recentimage = NULL;
1487                         } else if (strcmp(colname[i], "app_mainapp") == 0 ) {
1488                                 if (coltxt[i])
1489                                         info->uiapp_info->mainapp = strdup(coltxt[i]);
1490                                 else
1491                                         info->uiapp_info->mainapp = NULL;
1492                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
1493                                 if (coltxt[i]) {
1494                                         info->uiapp_info->icon->lang = strdup(coltxt[i]);
1495                                         info->uiapp_info->label->lang = strdup(coltxt[i]);
1496                                 }
1497                                 else {
1498                                         info->uiapp_info->icon->lang = NULL;
1499                                         info->uiapp_info->label->lang = NULL;
1500                                 }
1501                         } else if (strcmp(colname[i], "app_image") == 0) {
1502                                         if (coltxt[i])
1503                                                 info->uiapp_info->image->text= strdup(coltxt[i]);
1504                                         else
1505                                                 info->uiapp_info->image->text = NULL;
1506                         } else if (strcmp(colname[i], "app_image_section") == 0) {
1507                                         if (coltxt[i])
1508                                                 info->uiapp_info->image->section= strdup(coltxt[i]);
1509                                         else
1510                                                 info->uiapp_info->image->section = NULL;
1511                         } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
1512                                 if (coltxt[i])
1513                                         info->uiapp_info->permission_type = strdup(coltxt[i]);
1514                                 else
1515                                         info->uiapp_info->permission_type = NULL;
1516                         } else if (strcmp(colname[i], "component_type") == 0 ) {
1517                                 if (coltxt[i])
1518                                         info->uiapp_info->component_type = strdup(coltxt[i]);
1519                                 else
1520                                         info->uiapp_info->component_type = NULL;
1521                         } else if (strcmp(colname[i], "app_preload") == 0 ) {
1522                                 if (coltxt[i])
1523                                         info->uiapp_info->preload = strdup(coltxt[i]);
1524                                 else
1525                                         info->uiapp_info->preload = NULL;
1526                         } else if (strcmp(colname[i], "app_submode") == 0 ) {
1527                                 if (coltxt[i])
1528                                         info->uiapp_info->submode = strdup(coltxt[i]);
1529                                 else
1530                                         info->uiapp_info->submode = NULL;
1531                         } else if (strcmp(colname[i], "app_submode_mainid") == 0 ) {
1532                                 if (coltxt[i])
1533                                         info->uiapp_info->submode_mainid = strdup(coltxt[i]);
1534                                 else
1535                                         info->uiapp_info->submode_mainid = NULL;
1536                         } else
1537                                 continue;
1538                 }
1539                 break;
1540         case PMINFO_SVC_APP:
1541                 icon = calloc(1, sizeof(icon_x));
1542                 LISTADD(info->svcapp_info->icon, icon);
1543                 label = calloc(1, sizeof(label_x));
1544                 LISTADD(info->svcapp_info->label, label);
1545                 category = calloc(1, sizeof(category_x));
1546                 LISTADD(info->svcapp_info->category, category);
1547                 metadata = calloc(1, sizeof(metadata_x));
1548                 LISTADD(info->svcapp_info->metadata, metadata);
1549                 permission = calloc(1, sizeof(permission_x));
1550                 LISTADD(info->svcapp_info->permission, permission);
1551                 for(i = 0; i < ncols; i++)
1552                 {
1553                         if (strcmp(colname[i], "app_id") == 0) {
1554                                 /*appid being foreign key, is column in every table
1555                                 Hence appid gets strduped every time leading to memory leak.
1556                                 If appid is already set, just continue.*/
1557                                 if (info->svcapp_info->appid)
1558                                         continue;
1559                                 if (coltxt[i])
1560                                         info->svcapp_info->appid = strdup(coltxt[i]);
1561                                 else
1562                                         info->svcapp_info->appid = NULL;
1563                         } else if (strcmp(colname[i], "app_exec") == 0) {
1564                                 if (coltxt[i])
1565                                         info->svcapp_info->exec = strdup(coltxt[i]);
1566                                 else
1567                                         info->svcapp_info->exec = NULL;
1568                         } else if (strcmp(colname[i], "app_icon") == 0) {
1569                                 if (coltxt[i])
1570                                         info->svcapp_info->icon->text = strdup(coltxt[i]);
1571                                 else
1572                                         info->svcapp_info->icon->text = NULL;
1573                         } else if (strcmp(colname[i], "app_label") == 0 ) {
1574                                 if (coltxt[i])
1575                                         info->svcapp_info->label->text = strdup(coltxt[i]);
1576                                 else
1577                                         info->svcapp_info->label->text = NULL;
1578                         } else if (strcmp(colname[i], "app_type") == 0 ) {
1579                                 if (coltxt[i])
1580                                         info->svcapp_info->type = strdup(coltxt[i]);
1581                                 else
1582                                         info->svcapp_info->type = NULL;
1583                         } else if (strcmp(colname[i], "app_onboot") == 0 ) {
1584                                 if (coltxt[i])
1585                                         info->svcapp_info->onboot = strdup(coltxt[i]);
1586                                 else
1587                                         info->svcapp_info->onboot = NULL;
1588                         } else if (strcmp(colname[i], "app_autorestart") == 0 ) {
1589                                 if (coltxt[i])
1590                                         info->svcapp_info->autorestart = strdup(coltxt[i]);
1591                                 else
1592                                         info->svcapp_info->autorestart = NULL;
1593                         } else if (strcmp(colname[i], "app_enabled") == 0 ) {
1594                                 if (coltxt[i])
1595                                         info->svcapp_info->enabled= strdup(coltxt[i]);
1596                                 else
1597                                         info->svcapp_info->enabled = NULL;
1598                         } else if (strcmp(colname[i], "category") == 0 ) {
1599                                 if (coltxt[i])
1600                                         info->svcapp_info->category->name = strdup(coltxt[i]);
1601                                 else
1602                                         info->svcapp_info->category->name = NULL;
1603                         } else if (strcmp(colname[i], "md_key") == 0 ) {
1604                                 if (coltxt[i])
1605                                         info->svcapp_info->metadata->key = strdup(coltxt[i]);
1606                                 else
1607                                         info->svcapp_info->metadata->key = NULL;
1608                         } else if (strcmp(colname[i], "md_value") == 0 ) {
1609                                 if (coltxt[i])
1610                                         info->svcapp_info->metadata->value = strdup(coltxt[i]);
1611                                 else
1612                                         info->svcapp_info->metadata->value = NULL;
1613                         } else if (strcmp(colname[i], "pm_type") == 0 ) {
1614                                 if (coltxt[i])
1615                                         info->svcapp_info->permission->type= strdup(coltxt[i]);
1616                                 else
1617                                         info->svcapp_info->permission->type = NULL;
1618                         } else if (strcmp(colname[i], "pm_value") == 0 ) {
1619                                 if (coltxt[i])
1620                                         info->svcapp_info->permission->value = strdup(coltxt[i]);
1621                                 else
1622                                         info->svcapp_info->permission->value = NULL;
1623                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
1624                                 if (coltxt[i]) {
1625                                         info->svcapp_info->icon->lang = strdup(coltxt[i]);
1626                                         info->svcapp_info->label->lang = strdup(coltxt[i]);
1627                                 }
1628                                 else {
1629                                         info->svcapp_info->icon->lang = NULL;
1630                                         info->svcapp_info->label->lang = NULL;
1631                                 }
1632                         } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
1633                                 if (coltxt[i])
1634                                         info->svcapp_info->permission_type = strdup(coltxt[i]);
1635                                 else
1636                                         info->svcapp_info->permission_type = NULL;
1637                         } else
1638                                 continue;
1639                 }
1640                 break;
1641         default:
1642                 break;
1643         }
1644
1645         return 0;
1646 }
1647
1648
1649 static int __appcomponent_cb(void *data, int ncols, char **coltxt, char **colname)
1650 {
1651         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
1652         int i = 0;
1653         for(i = 0; i < ncols; i++)
1654         {
1655                 if (strcmp(colname[i], "app_component") == 0) {
1656                         info->app_component = __appcomponent_convert(coltxt[i]);
1657                 } else if (strcmp(colname[i], "package") == 0) {
1658                         info->package = strdup(coltxt[i]);
1659                 }
1660         }
1661
1662         return 0;
1663 }
1664
1665 static int __datacontrol_cb(void *data, int ncols, char **coltxt, char **colname)
1666 {
1667         pkgmgr_datacontrol_x *info = (pkgmgr_datacontrol_x *)data;
1668         int i = 0;
1669         for(i = 0; i < ncols; i++)
1670         {
1671                 if (strcmp(colname[i], "PACKAGE_NAME") == 0) {
1672                         if (coltxt[i])
1673                                 info->appid = strdup(coltxt[i]);
1674                         else
1675                                 info->appid = NULL;
1676                 } else if (strcmp(colname[i], "ACCESS") == 0 ){
1677                         if (coltxt[i])
1678                                 info->access = strdup(coltxt[i]);
1679                         else
1680                                 info->access = NULL;
1681                 } else
1682                         continue;
1683         }
1684         return 0;
1685 }
1686
1687 static int __cert_cb(void *data, int ncols, char **coltxt, char **colname)
1688 {
1689         pkgmgr_cert_x *info = (pkgmgr_cert_x *)data;
1690         int i = 0;
1691
1692         for(i = 0; i < ncols; i++)
1693         {
1694                 if (strcmp(colname[i], "author_signer_cert") == 0) {
1695                         if (coltxt[i])
1696                                 info->cert_id = atoi(coltxt[i]);
1697                         else
1698                                 info->cert_id = 0;
1699                 } else if (strcmp(colname[i], "package") == 0) {
1700                         if (coltxt[i])
1701                                 info->pkgid= strdup(coltxt[i]);
1702                         else
1703                                 info->pkgid = NULL;
1704                 } else
1705                         continue;
1706         }
1707         return 0;
1708 }
1709
1710 /* get the first locale value*/
1711 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
1712 {
1713         pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
1714
1715         if (ncols >= 1)
1716                 info->locale = strdup(coltxt[0]);
1717         else
1718                 info->locale = NULL;
1719
1720         return 0;
1721 }
1722
1723 static int __exec_pkginfo_query(char *query, void *data)
1724 {
1725         char *error_message = NULL;
1726         if (SQLITE_OK !=
1727             sqlite3_exec(manifest_db, query, __pkginfo_cb, data, &error_message)) {
1728                 _LOGE("Don't execute query = %s error message = %s\n", query,
1729                        error_message);
1730                 sqlite3_free(error_message);
1731                 return -1;
1732         }
1733         sqlite3_free(error_message);
1734         return 0;
1735 }
1736
1737 static int __exec_certinfo_query(char *query, void *data)
1738 {
1739         char *error_message = NULL;
1740         if (SQLITE_OK !=
1741             sqlite3_exec(cert_db, query, __certinfo_cb, data, &error_message)) {
1742                 _LOGE("Don't execute query = %s error message = %s\n", query,
1743                        error_message);
1744                 sqlite3_free(error_message);
1745                 return -1;
1746         }
1747         sqlite3_free(error_message);
1748         return 0;
1749 }
1750
1751 static int __exec_certindexinfo_query(char *query, void *data)
1752 {
1753         char *error_message = NULL;
1754         if (SQLITE_OK !=
1755             sqlite3_exec(cert_db, query, __certindexinfo_cb, data, &error_message)) {
1756                 _LOGE("Don't execute query = %s error message = %s\n", query,
1757                        error_message);
1758                 sqlite3_free(error_message);
1759                 return -1;
1760         }
1761         sqlite3_free(error_message);
1762         return 0;
1763 }
1764
1765 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
1766 {
1767         char *error_message = NULL;
1768         if (SQLITE_OK !=
1769             sqlite3_exec(db, query, callback, data, &error_message)) {
1770                 _LOGE("Don't execute query = %s error message = %s\n", query,
1771                        error_message);
1772                 sqlite3_free(error_message);
1773                 return -1;
1774         }
1775         sqlite3_free(error_message);
1776         return 0;
1777 }
1778
1779
1780 static int __child_element(xmlTextReaderPtr reader, int depth)
1781 {
1782         int ret = xmlTextReaderRead(reader);
1783         int cur = xmlTextReaderDepth(reader);
1784         while (ret == 1) {
1785
1786                 switch (xmlTextReaderNodeType(reader)) {
1787                 case XML_READER_TYPE_ELEMENT:
1788                         if (cur == depth + 1)
1789                                 return 1;
1790                         break;
1791                 case XML_READER_TYPE_TEXT:
1792                         /*text is handled by each function separately*/
1793                         if (cur == depth + 1)
1794                                 return 0;
1795                         break;
1796                 case XML_READER_TYPE_END_ELEMENT:
1797                         if (cur == depth)
1798                                 return 0;
1799                         break;
1800                 default:
1801                         if (cur <= depth)
1802                                 return 0;
1803                         break;
1804                 }
1805                 ret = xmlTextReaderRead(reader);
1806                 cur = xmlTextReaderDepth(reader);
1807         }
1808         return ret;
1809 }
1810
1811 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
1812 {
1813         int *p = (int*)data;
1814         *p = atoi(coltxt[0]);
1815         return 0;
1816 }
1817
1818 static int __check_app_locale_from_app_localized_info_by_exact(sqlite3 *db, const char *appid, const char *locale)
1819 {
1820         int result_query = -1;
1821         int ret = 0;
1822         char query[MAX_QUERY_LEN];
1823
1824         snprintf(query, MAX_QUERY_LEN, "select exists(select app_locale from package_app_localized_info where app_id='%s' and app_locale='%s')", appid, locale);
1825         ret = __exec_db_query(db, query, __check_validation_of_qurey_cb, (void *)&result_query);
1826         retvm_if(ret == -1, PMINFO_R_ERROR, "Exec DB query failed");
1827         return result_query;
1828 }
1829
1830 static int __check_app_locale_from_app_localized_info_by_fallback(sqlite3 *db, const char *appid, const char *locale)
1831 {
1832         int result_query = -1;
1833         int ret = 0;
1834         char wildcard[2] = {'%','\0'};
1835         char query[MAX_QUERY_LEN];
1836         char lang[3] = {'\0'};
1837         strncpy(lang, locale, LANGUAGE_LENGTH);
1838
1839         snprintf(query, MAX_QUERY_LEN, "select exists(select app_locale from package_app_localized_info where app_id='%s' and app_locale like '%s%s')", appid, lang, wildcard);
1840         ret = __exec_db_query(db, query, __check_validation_of_qurey_cb, (void *)&result_query);
1841         retvm_if(ret == -1, PMINFO_R_ERROR, "Exec DB query failed");
1842         return result_query;
1843 }
1844
1845 static char* __get_app_locale_from_app_localized_info_by_fallback(sqlite3 *db, const char *appid, const char *locale)
1846 {
1847         int ret = 0;
1848         char wildcard[2] = {'%','\0'};
1849         char lang[3] = {'\0'};
1850         char query[MAX_QUERY_LEN];
1851         char *locale_new = NULL;
1852         pkgmgr_locale_x *info = NULL;
1853
1854         info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
1855         if (info == NULL) {
1856                 _LOGE("Out of Memory!!!\n");
1857                 return NULL;
1858         }
1859         memset(info, '\0', sizeof(*info));
1860
1861         strncpy(lang, locale, 2);
1862         snprintf(query, MAX_QUERY_LEN, "select app_locale from package_app_localized_info where app_id='%s' and app_locale like '%s%s'", appid, lang, wildcard);
1863         ret = __exec_db_query(db, query, __fallback_locale_cb, (void *)info);
1864         tryvm_if(ret == -1, PMINFO_R_ERROR, "Exec DB query failed");
1865         locale_new = info->locale;
1866         free(info);
1867         return locale_new;
1868 catch:
1869         if (info) {
1870                 free(info);
1871                 info = NULL;
1872         }
1873         return NULL;
1874 }
1875
1876 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
1877 {
1878         char *locale = malloc(6);
1879         if (!locale) {
1880                 _LOGE("Malloc Failed\n");
1881                 return NULL;
1882         }
1883
1884         sprintf(locale, "%c%c-%c%c", syslocale[0], syslocale[1], tolower(syslocale[3]), tolower(syslocale[4]));
1885         return locale;
1886 }
1887
1888 static char* __get_app_locale_by_fallback(sqlite3 *db, const char *appid, const char *syslocale)
1889 {
1890         assert(appid);
1891         assert(syslocale);
1892
1893         char *locale = NULL;
1894         char *locale_new = NULL;
1895         int check_result = 0;
1896
1897         locale = __convert_syslocale_to_manifest_locale((char *)syslocale);
1898
1899         /*check exact matching */
1900         check_result = __check_app_locale_from_app_localized_info_by_exact(db, appid, locale);
1901
1902         /* Exact found */
1903         if (check_result == 1) {
1904                 _LOGD("%s find exact locale(%s)\n", appid, locale);
1905                 return locale;
1906         }
1907
1908         /* fallback matching */
1909         check_result = __check_app_locale_from_app_localized_info_by_fallback(db, appid, locale);
1910         if(check_result == 1) {
1911                    locale_new = __get_app_locale_from_app_localized_info_by_fallback(db, appid, locale);
1912                    free(locale);
1913                    if (locale_new == NULL)
1914                            locale_new =  strdup(DEFAULT_LOCALE);
1915                    return locale_new;
1916         }
1917
1918         /* default locale */
1919         free(locale);
1920         return  strdup(DEFAULT_LOCALE);
1921 }
1922
1923 long long _pkgmgr_calculate_dir_size(char *dirname)
1924 {
1925         long long total = 0;
1926         long long ret = 0;
1927         int q = 0; /*quotient*/
1928         int r = 0; /*remainder*/
1929         DIR *dp = NULL;
1930         struct dirent *ep = NULL;
1931         struct stat fileinfo;
1932         char abs_filename[FILENAME_MAX] = { 0, };
1933         retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL");
1934
1935         dp = opendir(dirname);
1936         if (dp != NULL) {
1937                 while ((ep = readdir(dp)) != NULL) {
1938                         if (!strcmp(ep->d_name, ".") ||
1939                                 !strcmp(ep->d_name, "..")) {
1940                                 continue;
1941                         }
1942                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
1943                                  ep->d_name);
1944                         if (lstat(abs_filename, &fileinfo) < 0)
1945                                 perror(abs_filename);
1946                         else {
1947                                 if (S_ISDIR(fileinfo.st_mode)) {
1948                                         total += fileinfo.st_size;
1949                                         if (strcmp(ep->d_name, ".")
1950                                             && strcmp(ep->d_name, "..")) {
1951                                                 ret = _pkgmgr_calculate_dir_size
1952                                                     (abs_filename);
1953                                                 total = total + ret;
1954                                         }
1955                                 } else if (S_ISLNK(fileinfo.st_mode)) {
1956                                         continue;
1957                                 } else {
1958                                         /*It is a file. Calculate the actual
1959                                         size occupied (in terms of 4096 blocks)*/
1960                                 q = (fileinfo.st_size / BLOCK_SIZE);
1961                                 r = (fileinfo.st_size % BLOCK_SIZE);
1962                                 if (r) {
1963                                         q = q + 1;
1964                                 }
1965                                 total += q * BLOCK_SIZE;
1966                                 }
1967                         }
1968                 }
1969                 (void)closedir(dp);
1970         } else {
1971                 _LOGE("Couldn't open the directory\n");
1972                 return -1;
1973         }
1974         return total;
1975
1976 }
1977
1978 static int __delete_certinfo(const char *pkgid)
1979 {
1980         int ret = -1;
1981         int i = 0;
1982         int j = 0;
1983         int c = 0;
1984         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
1985         char *error_message = NULL;
1986         char query[MAX_QUERY_LEN] = {'\0'};
1987         pkgmgr_certinfo_x *certinfo = NULL;
1988         pkgmgr_certindexinfo_x *indexinfo = NULL;
1989         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
1990         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
1991         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
1992         if (indexinfo == NULL) {
1993                 _LOGE("Out of Memory!!!");
1994                 ret = PMINFO_R_ERROR;
1995                 goto err;
1996         }
1997         /*populate certinfo from DB*/
1998         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
1999         ret = __exec_certinfo_query(query, (void *)certinfo);
2000         if (ret == -1) {
2001                 _LOGE("Package Cert Info DB Information retrieval failed\n");
2002                 ret = PMINFO_R_ERROR;
2003                 goto err;
2004         }
2005         /*Update cert index table*/
2006         for (i = 0; i < MAX_CERT_TYPE; i++) {
2007                 if ((certinfo->cert_id)[i]) {
2008                         for (j = 0; j < MAX_CERT_TYPE; j++) {
2009                                 if ((certinfo->cert_id)[i] == unique_id[j]) {
2010                                         /*Ref count has already been updated. Just continue*/
2011                                         break;
2012                                 }
2013                         }
2014                         if (j == MAX_CERT_TYPE)
2015                                 unique_id[c++] = (certinfo->cert_id)[i];
2016                         else
2017                                 continue;
2018                         memset(query, '\0', MAX_QUERY_LEN);
2019                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
2020                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
2021                         if (ret == -1) {
2022                                 _LOGE("Cert Info DB Information retrieval failed\n");
2023                                 ret = PMINFO_R_ERROR;
2024                                 goto err;
2025                         }
2026                         memset(query, '\0', MAX_QUERY_LEN);
2027                         if (indexinfo->cert_ref_count > 1) {
2028                                 /*decrease ref count*/
2029                                 snprintf(query, MAX_QUERY_LEN, "update package_cert_index_info set cert_ref_count=%d where cert_id=%d ",
2030                                 indexinfo->cert_ref_count - 1, (certinfo->cert_id)[i]);
2031                         } else {
2032                                 /*delete this certificate as ref count is 1 and it will become 0*/
2033                                 snprintf(query, MAX_QUERY_LEN, "delete from  package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
2034                         }
2035                         if (SQLITE_OK !=
2036                             sqlite3_exec(cert_db, query, NULL, NULL, &error_message)) {
2037                                 _LOGE("Don't execute query = %s error message = %s\n", query,
2038                                        error_message);
2039                                 sqlite3_free(error_message);
2040                                 ret = PMINFO_R_ERROR;
2041                                 goto err;
2042                         }
2043                 }
2044         }
2045         /*Now delete the entry from db*/
2046         snprintf(query, MAX_QUERY_LEN, "delete from package_cert_info where package='%s'", pkgid);
2047         if (SQLITE_OK !=
2048             sqlite3_exec(cert_db, query, NULL, NULL, &error_message)) {
2049                 _LOGE("Don't execute query = %s error message = %s\n", query,
2050                        error_message);
2051                 sqlite3_free(error_message);
2052                 ret = PMINFO_R_ERROR;
2053                 goto err;
2054         }
2055         ret = PMINFO_R_OK;
2056 err:
2057         if (indexinfo) {
2058                 free(indexinfo);
2059                 indexinfo = NULL;
2060         }
2061         if (certinfo->pkgid) {
2062                 free(certinfo->pkgid);
2063                 certinfo->pkgid = NULL;
2064         }
2065         for (i = 0; i < MAX_CERT_TYPE; i++) {
2066                 if ((certinfo->cert_info)[i]) {
2067                         free((certinfo->cert_info)[i]);
2068                         (certinfo->cert_info)[i] = NULL;
2069                 }
2070         }
2071         free(certinfo);
2072         certinfo = NULL;
2073         return ret;
2074 }
2075
2076 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
2077 {
2078         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
2079         char *error_message = NULL;
2080         int ret = PMINFO_R_OK;
2081         char query[MAX_QUERY_LEN] = {'\0'};
2082         char *syslocale = NULL;
2083         char *locale = NULL;
2084         pkgmgr_pkginfo_x *pkginfo = NULL;
2085         label_x *tmp1 = NULL;
2086         icon_x *tmp2 = NULL;
2087         description_x *tmp3 = NULL;
2088         author_x *tmp4 = NULL;
2089         privilege_x *tmp5 = NULL;
2090
2091         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2092         if (syslocale == NULL) {
2093                 _LOGE("current locale is NULL\n");
2094                 ret = PMINFO_R_ERROR;
2095                 goto err;
2096         }
2097         locale = __convert_system_locale_to_manifest_locale(syslocale);
2098         if (locale == NULL) {
2099                 _LOGE("manifest locale is NULL\n");
2100                 ret = PMINFO_R_EINVAL;
2101                 goto err;
2102         }
2103
2104         ret = __open_manifest_db();
2105         if (ret == -1) {
2106                 _LOGE("Fail to open manifest DB\n");
2107                 ret = PMINFO_R_ERROR;
2108                 goto err;
2109         }
2110         pkgmgr_pkginfo_x *tmphead = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2111         pkgmgr_pkginfo_x *node = NULL;
2112         pkgmgr_pkginfo_x *temp_node = NULL;
2113
2114         snprintf(query, MAX_QUERY_LEN, "select * from package_info");
2115         if (SQLITE_OK !=
2116             sqlite3_exec(manifest_db, query, __pkg_list_cb, (void *)tmphead, &error_message)) {
2117                 _LOGE("Don't execute query = %s error message = %s\n", query,
2118                        error_message);
2119                 sqlite3_free(error_message);
2120                 sqlite3_close(manifest_db);
2121                 ret = PMINFO_R_ERROR;
2122                 goto err;
2123         }
2124
2125         LISTHEAD(tmphead, node);
2126
2127         for(node = node->next; node ; node = node->next) {
2128                 pkginfo = node;
2129                 pkginfo->locale = strdup(locale);
2130                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
2131                 if (pkginfo->manifest_info->privileges == NULL) {
2132                         _LOGE("Failed to allocate memory for privileges info\n");
2133                         ret = PMINFO_R_ERROR;
2134                         goto err;
2135                 }
2136                 /*populate manifest_info from DB*/
2137                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
2138                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2139                 if (ret == -1) {
2140                         _LOGE("Package Info DB Information retrieval failed\n");
2141                         ret = PMINFO_R_ERROR;
2142                         goto err;
2143                 }
2144                 memset(query, '\0', MAX_QUERY_LEN);
2145                 /*populate privilege_info from DB*/
2146                 snprintf(query, MAX_QUERY_LEN, "select * from package_privilege_info where package='%s' ", pkginfo->manifest_info->package);
2147                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2148                 if (ret == -1) {
2149                         _LOGE("Package Privilege Info DB Information retrieval failed\n");
2150                         ret = PMINFO_R_ERROR;
2151                         goto err;
2152                 }
2153                 memset(query, '\0', MAX_QUERY_LEN);
2154                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2155                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
2156                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2157                 if (ret == -1) {
2158                         _LOGE("Package Info DB Information retrieval failed\n");
2159                         ret = PMINFO_R_ERROR;
2160                         goto err;
2161                 }
2162                 /*Also store the values corresponding to default locales*/
2163                 memset(query, '\0', MAX_QUERY_LEN);
2164                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2165                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
2166                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2167                 if (ret == -1) {
2168                         _LOGE("Package Info DB Information retrieval failed\n");
2169                         ret = PMINFO_R_ERROR;
2170                         goto err;
2171                 }
2172                 if (pkginfo->manifest_info->label) {
2173                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
2174                         pkginfo->manifest_info->label = tmp1;
2175                 }
2176                 if (pkginfo->manifest_info->icon) {
2177                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2178                         pkginfo->manifest_info->icon = tmp2;
2179                 }
2180                 if (pkginfo->manifest_info->description) {
2181                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
2182                         pkginfo->manifest_info->description = tmp3;
2183                 }
2184                 if (pkginfo->manifest_info->author) {
2185                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
2186                         pkginfo->manifest_info->author = tmp4;
2187                 }
2188                 if (pkginfo->manifest_info->privileges->privilege) {
2189                         LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
2190                         pkginfo->manifest_info->privileges->privilege = tmp5;
2191                 }
2192         }
2193
2194         LISTHEAD(tmphead, node);
2195
2196         for(node = node->next; node ; node = node->next) {
2197                 pkginfo = node;
2198                 ret = pkg_list_cb( (void *)pkginfo, user_data);
2199                 if(ret < 0)
2200                         break;
2201         }
2202
2203         ret = PMINFO_R_OK;
2204
2205 err:
2206         sqlite3_close(manifest_db);
2207         if (syslocale) {
2208                 free(syslocale);
2209                 syslocale = NULL;
2210         }
2211         if (locale) {
2212                 free(locale);
2213                 locale = NULL;
2214         }
2215         LISTHEAD(tmphead, node);
2216         temp_node = node->next;
2217         node = temp_node;
2218         while (node) {
2219                 temp_node = node->next;
2220                 __cleanup_pkginfo(node);
2221                 node = temp_node;
2222         }
2223         __cleanup_pkginfo(tmphead);
2224         return ret;
2225 }
2226
2227
2228 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
2229 {
2230         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2231         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2232         pkgmgr_pkginfo_x *pkginfo = NULL;
2233         int ret = PMINFO_R_OK;
2234         char query[MAX_QUERY_LEN] = {'\0'};
2235         char *syslocale = NULL;
2236         char *locale = NULL;
2237         int exist = 0;
2238         label_x *tmp1 = NULL;
2239         icon_x *tmp2 = NULL;
2240         description_x *tmp3 = NULL;
2241         author_x *tmp4 = NULL;
2242         privilege_x *tmp5 = NULL;
2243         sqlite3 *pkginfo_db = NULL;
2244
2245         /*validate pkgid*/
2246         ret = db_util_open_with_options(MANIFEST_DB, &pkginfo_db, SQLITE_OPEN_READONLY, NULL);
2247         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
2248
2249         /*check pkgid exist on db*/
2250         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_info where package='%s')", pkgid);
2251         ret = __exec_db_query(pkginfo_db, query, __validate_cb, (void *)&exist);
2252         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec[%s] fail", pkgid);
2253         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "pkgid[%s] not found in DB", pkgid);
2254
2255         /*get system locale*/
2256         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2257         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
2258
2259         /*get locale on db*/
2260         locale = __convert_system_locale_to_manifest_locale(syslocale);
2261         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
2262
2263         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2264         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
2265
2266         pkginfo->locale = strdup(locale);
2267
2268         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
2269         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
2270
2271         pkginfo->manifest_info->package = strdup(pkgid);
2272         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
2273         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
2274
2275         /*populate manifest_info from DB*/
2276         snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkgid);
2277         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2278         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2279
2280         memset(query, '\0', MAX_QUERY_LEN);
2281         /*populate privilege_info from DB*/
2282         snprintf(query, MAX_QUERY_LEN, "select * from package_privilege_info where package='%s' ", pkgid);
2283         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2284         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Privilege Info DB Information retrieval failed");
2285
2286         memset(query, '\0', MAX_QUERY_LEN);
2287         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2288                 " package='%s' and package_locale='%s'", pkgid, locale);
2289         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2290         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2291
2292         /*Also store the values corresponding to default locales*/
2293         memset(query, '\0', MAX_QUERY_LEN);
2294         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2295                 " package='%s' and package_locale='%s'", pkgid, DEFAULT_LOCALE);
2296         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2297         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2298
2299         if (pkginfo->manifest_info->label) {
2300                 LISTHEAD(pkginfo->manifest_info->label, tmp1);
2301                 pkginfo->manifest_info->label = tmp1;
2302         }
2303         if (pkginfo->manifest_info->icon) {
2304                 LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2305                 pkginfo->manifest_info->icon = tmp2;
2306         }
2307         if (pkginfo->manifest_info->description) {
2308                 LISTHEAD(pkginfo->manifest_info->description, tmp3);
2309                 pkginfo->manifest_info->description = tmp3;
2310         }
2311         if (pkginfo->manifest_info->author) {
2312                 LISTHEAD(pkginfo->manifest_info->author, tmp4);
2313                 pkginfo->manifest_info->author = tmp4;
2314         }
2315         if (pkginfo->manifest_info->privileges->privilege) {
2316                 LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
2317                 pkginfo->manifest_info->privileges->privilege = tmp5;
2318         }
2319
2320 catch:
2321         if (ret == PMINFO_R_OK)
2322                 *handle = (void*)pkginfo;
2323         else {
2324                 *handle = NULL;
2325                 __cleanup_pkginfo(pkginfo);
2326         }
2327         sqlite3_close(pkginfo_db);
2328
2329         if (syslocale) {
2330                 free(syslocale);
2331                 syslocale = NULL;
2332         }
2333         if (locale) {
2334                 free(locale);
2335                 locale = NULL;
2336         }
2337         return ret;
2338 }
2339
2340
2341 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
2342 {
2343         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2344         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2345         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2346         if (info->manifest_info->package)
2347                 *pkg_name = (char *)info->manifest_info->package;
2348         else
2349                 return PMINFO_R_ERROR;
2350
2351         return PMINFO_R_OK;
2352 }
2353
2354 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
2355 {
2356         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2357         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2358         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2359         if (info->manifest_info->package)
2360                 *pkgid = (char *)info->manifest_info->package;
2361         else
2362                 return PMINFO_R_ERROR;
2363
2364         return PMINFO_R_OK;
2365 }
2366
2367 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
2368 {
2369         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2370         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2371         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2372         if (info->manifest_info->type)
2373                 *type = (char *)info->manifest_info->type;
2374         else
2375                 *type = pkgtype;
2376         return PMINFO_R_OK;
2377 }
2378
2379 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
2380 {
2381         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2382         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2383         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2384         *version = (char *)info->manifest_info->version;
2385         return PMINFO_R_OK;
2386 }
2387
2388 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
2389 {
2390         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2391         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2392         char *val = NULL;
2393         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2394         val = (char *)info->manifest_info->installlocation;
2395         if (val) {
2396                 if (strcmp(val, "internal-only") == 0)
2397                         *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
2398                 else if (strcmp(val, "prefer-external") == 0)
2399                         *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
2400                 else
2401                         *location = PMINFO_INSTALL_LOCATION_AUTO;
2402         }
2403         return PMINFO_R_OK;
2404 }
2405
2406 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
2407 {
2408         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2409         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2410         char *val = NULL;
2411         char *location = NULL;
2412         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2413         location = (char *)info->manifest_info->installlocation;
2414         val = (char *)info->manifest_info->package_size;
2415         if (val) {
2416                 *size = atoi(val);
2417         } else {
2418                 *size = 0;
2419                 _LOGE("package size is not specified\n");
2420                 return PMINFO_R_ERROR;
2421         }
2422         return PMINFO_R_OK;
2423 }
2424
2425 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
2426 {
2427         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2428         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2429
2430         char *pkgid = NULL;
2431         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
2432         long long rw_size = 0;
2433         long long ro_size= 0;
2434         long long tmp_size= 0;
2435         long long total_size= 0;
2436         struct stat fileinfo;
2437         int ret = -1;
2438
2439         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
2440         if(ret < 0)
2441                 return PMINFO_R_ERROR;
2442
2443         /* RW area */
2444         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
2445         if (lstat(device_path, &fileinfo) == 0) {
2446                 if (!S_ISLNK(fileinfo.st_mode)) {
2447                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2448                         if (tmp_size > 0)
2449                                 rw_size += tmp_size;
2450                 }
2451         }
2452
2453         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
2454         if (lstat(device_path, &fileinfo) == 0) {
2455                 if (!S_ISLNK(fileinfo.st_mode)) {
2456                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2457                         if (tmp_size > 0)
2458                         rw_size += tmp_size;
2459                 }
2460         }
2461
2462         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
2463         if (lstat(device_path, &fileinfo) == 0) {
2464                 if (!S_ISLNK(fileinfo.st_mode)) {
2465                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2466                         if (tmp_size > 0)
2467                         rw_size += tmp_size;
2468                 }
2469         }
2470
2471         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
2472         if (lstat(device_path, &fileinfo) == 0) {
2473                 if (!S_ISLNK(fileinfo.st_mode)) {
2474                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2475                         if (tmp_size > 0)
2476                                 rw_size += tmp_size;
2477                 }
2478         }
2479
2480         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
2481         if (lstat(device_path, &fileinfo) == 0) {
2482                 if (!S_ISLNK(fileinfo.st_mode)) {
2483                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2484                         if (tmp_size > 0)
2485                                 rw_size += tmp_size;
2486         }
2487         }
2488
2489         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
2490         if (lstat(device_path, &fileinfo) == 0) {
2491                 if (!S_ISLNK(fileinfo.st_mode)) {
2492                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2493                         if (tmp_size > 0)
2494                                 rw_size += tmp_size;
2495                 }
2496         }
2497
2498         /* RO area */
2499         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
2500         if (lstat(device_path, &fileinfo) == 0) {
2501                 if (!S_ISLNK(fileinfo.st_mode)) {
2502                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2503                         if (tmp_size > 0)
2504                                 ro_size += tmp_size;
2505                 }
2506         }
2507
2508         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
2509         if (lstat(device_path, &fileinfo) == 0) {
2510                 if (!S_ISLNK(fileinfo.st_mode)) {
2511                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2512                         if (tmp_size > 0)
2513                                 ro_size += tmp_size;
2514                 }
2515         }
2516
2517         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
2518         if (lstat(device_path, &fileinfo) == 0) {
2519                 if (!S_ISLNK(fileinfo.st_mode)) {
2520                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2521                         if (tmp_size > 0)
2522                                 ro_size += tmp_size;
2523                 }
2524         }
2525
2526         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
2527         if (lstat(device_path, &fileinfo) == 0) {
2528                 if (!S_ISLNK(fileinfo.st_mode)) {
2529                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2530                         if (tmp_size > 0)
2531                                 ro_size += tmp_size;
2532                 }
2533         }
2534
2535         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
2536         if (lstat(device_path, &fileinfo) == 0) {
2537                 if (!S_ISLNK(fileinfo.st_mode)) {
2538                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2539                         if (tmp_size > 0)
2540                                 ro_size += tmp_size;
2541         }
2542         }
2543
2544         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
2545         if (lstat(device_path, &fileinfo) == 0) {
2546                 if (!S_ISLNK(fileinfo.st_mode)) {
2547                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2548                         if (tmp_size > 0)
2549                                 ro_size += tmp_size;
2550                 }
2551         }
2552
2553         /* Total size */
2554         total_size = rw_size + ro_size;
2555         *size = (int)total_size;
2556
2557         return PMINFO_R_OK;
2558 }
2559
2560 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
2561 {
2562         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2563         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2564
2565         char *pkgid = NULL;
2566         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
2567         long long total_size= 0;
2568         int ret = -1;
2569
2570         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
2571         if(ret < 0)
2572                 return PMINFO_R_ERROR;
2573
2574         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
2575         if (access(device_path, R_OK) == 0)
2576                 total_size = _pkgmgr_calculate_dir_size(device_path);
2577         if (total_size < 0)
2578                 return PMINFO_R_ERROR;
2579
2580         *size = (int)total_size;
2581
2582         return PMINFO_R_OK;
2583 }
2584
2585 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
2586 {
2587         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
2588         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2589         int ret = PMINFO_R_OK;
2590         char *locale = NULL;
2591         icon_x *ptr = NULL;
2592         *icon = NULL;
2593
2594         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2595
2596         locale = info->locale;
2597         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
2598
2599         for(ptr = info->manifest_info->icon; ptr != NULL; ptr = ptr->next)
2600         {
2601                 if (ptr->lang) {
2602                         if (strcmp(ptr->lang, locale) == 0) {
2603                                 *icon = (char *)ptr->text;
2604                                 if (strcasecmp(*icon, "(null)") == 0) {
2605                                         locale = DEFAULT_LOCALE;
2606                                         continue;
2607                                 } else
2608                                         break;
2609                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2610                                 *icon = (char *)ptr->text;
2611                                 break;
2612                         }
2613                 }
2614         }
2615
2616         return ret;
2617 }
2618
2619 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
2620 {
2621         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
2622         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2623         int ret = PMINFO_R_OK;
2624         char *locale = NULL;
2625         label_x *ptr = NULL;
2626         *label = NULL;
2627
2628         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2629         locale = info->locale;
2630         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
2631
2632         for(ptr = info->manifest_info->label; ptr != NULL; ptr = ptr->next)
2633         {
2634                 if (ptr->lang) {
2635                         if (strcmp(ptr->lang, locale) == 0) {
2636                                 *label = (char *)ptr->text;
2637                                 if (strcasecmp(*label, "(null)") == 0) {
2638                                         locale = DEFAULT_LOCALE;
2639                                         continue;
2640                                 } else
2641                                         break;
2642                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2643                                 *label = (char *)ptr->text;
2644                                 break;
2645                         }
2646                 }
2647         }
2648
2649         return ret;
2650 }
2651
2652 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
2653 {
2654         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2655         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2656         char *locale = NULL;
2657         description_x *ptr = NULL;
2658         *description = NULL;
2659
2660         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2661         locale = info->locale;
2662         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
2663
2664         for(ptr = info->manifest_info->description; ptr != NULL; ptr = ptr->next)
2665         {
2666                 if (ptr->lang) {
2667                         if (strcmp(ptr->lang, locale) == 0) {
2668                                 *description = (char *)ptr->text;
2669                                 if (strcasecmp(*description, "(null)") == 0) {
2670                                         locale = DEFAULT_LOCALE;
2671                                         continue;
2672                                 } else
2673                                         break;
2674                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2675                                 *description = (char *)ptr->text;
2676                                 break;
2677                         }
2678                 }
2679         }
2680         return PMINFO_R_OK;
2681 }
2682
2683 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
2684 {
2685         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2686         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2687         char *locale = NULL;
2688         author_x *ptr = NULL;
2689         *author_name = NULL;
2690
2691         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2692         locale = info->locale;
2693         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
2694
2695         for(ptr = info->manifest_info->author; ptr != NULL; ptr = ptr->next)
2696         {
2697                 if (ptr->lang) {
2698                         if (strcmp(ptr->lang, locale) == 0) {
2699                                 *author_name = (char *)ptr->text;
2700                                 if (strcasecmp(*author_name, "(null)") == 0) {
2701                                         locale = DEFAULT_LOCALE;
2702                                         continue;
2703                                 } else
2704                                         break;
2705                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2706                                 *author_name = (char *)ptr->text;
2707                                 break;
2708                         }
2709                 }
2710         }
2711         return PMINFO_R_OK;
2712 }
2713
2714 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
2715 {
2716         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2717         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2718         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2719         *author_email = (char *)info->manifest_info->author->email;
2720         return PMINFO_R_OK;
2721 }
2722
2723 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
2724 {
2725         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2726         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2727         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2728         *author_href = (char *)info->manifest_info->author->href;
2729         return PMINFO_R_OK;
2730 }
2731
2732 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
2733 {
2734         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2735         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2736
2737         char *pkgid = NULL;
2738         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2739         if (pkgid == NULL){
2740                  _LOGE("invalid func parameters\n");
2741                  return PMINFO_R_ERROR;
2742         }
2743
2744         FILE *fp = NULL;
2745         char app_mmc_path[FILENAME_MAX] = { 0, };
2746         char app_dir_path[FILENAME_MAX] = { 0, };
2747         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
2748         snprintf(app_dir_path, FILENAME_MAX,
2749         "%s%s", PKG_INSTALLATION_PATH, pkgid);
2750         snprintf(app_mmc_path, FILENAME_MAX,
2751         "%s%s", PKG_SD_PATH, pkgid);
2752         snprintf(app_mmc_internal_path, FILENAME_MAX,
2753         "%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
2754
2755         /*check whether application is in external memory or not */
2756         fp = fopen(app_mmc_path, "r");
2757         if (fp != NULL) {
2758                 fclose(fp);
2759                 fp = NULL;
2760                 *storage = PMINFO_EXTERNAL_STORAGE;
2761                 return PMINFO_R_OK;
2762         }
2763
2764         /*check whether application is in internal or not */
2765         fp = fopen(app_dir_path, "r");
2766         if (fp == NULL) {
2767                 *storage = -1;
2768                 return PMINFO_R_ERROR;
2769         } else {
2770                 fclose(fp);
2771                 /*check whether the application is installed in SD card
2772                         but SD card is not present*/
2773                 fp = fopen(app_mmc_internal_path, "r");
2774                 if (fp == NULL) {
2775                         *storage = PMINFO_INTERNAL_STORAGE;
2776                         return PMINFO_R_OK;
2777                 } else {
2778                         fclose(fp);
2779                         *storage = PMINFO_EXTERNAL_STORAGE;
2780                         return PMINFO_R_OK;
2781                 }
2782         }
2783 }
2784
2785 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
2786 {
2787         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2788         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2789         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2790         if (info->manifest_info->installed_time)
2791                 *installed_time = atoi(info->manifest_info->installed_time);
2792         else
2793                 return PMINFO_R_ERROR;
2794
2795         return PMINFO_R_OK;
2796 }
2797
2798 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
2799 {
2800         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2801         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2802         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2803         *storeclientid = (char *)info->manifest_info->storeclient_id;
2804         return PMINFO_R_OK;
2805 }
2806
2807 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
2808 {
2809         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2810         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2811         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2812         *mainappid = (char *)info->manifest_info->mainapp_id;
2813         return PMINFO_R_OK;
2814 }
2815
2816 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
2817 {
2818         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2819         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2820         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2821         *url = (char *)info->manifest_info->package_url;
2822         return PMINFO_R_OK;
2823 }
2824
2825 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
2826 {
2827         const char *val = NULL;
2828         const xmlChar *node;
2829         xmlTextReaderPtr reader;
2830         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
2831         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2832
2833         xmlInitParser();
2834         reader = xmlReaderForFile(manifest, NULL, 0);
2835
2836         if (reader){
2837                 if (__child_element(reader, -1)) {
2838                         node = xmlTextReaderConstName(reader);
2839                         if (!node) {
2840                                 _LOGE("xmlTextReaderConstName value is NULL\n");
2841                                 xmlFreeTextReader(reader);
2842                                 xmlCleanupParser();
2843                                 return PMINFO_R_ERROR;
2844                         }
2845
2846                         if (!strcmp(ASC_CHAR(node), "manifest")) {
2847                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
2848                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
2849
2850                                 if (val) {
2851                                         *size = atoi(val);
2852                                 } else {
2853                                         *size = 0;
2854                                         _LOGE("package size is not specified\n");
2855                                         xmlFreeTextReader(reader);
2856                                         xmlCleanupParser();
2857                                         return PMINFO_R_ERROR;
2858                                 }
2859                         } else {
2860                                 _LOGE("Unable to create xml reader\n");
2861                                 xmlFreeTextReader(reader);
2862                                 xmlCleanupParser();
2863                                 return PMINFO_R_ERROR;
2864                         }
2865                 }
2866         } else {
2867                 _LOGE("xmlReaderForFile value is NULL\n");
2868                 xmlCleanupParser();
2869                 return PMINFO_R_ERROR;
2870         }
2871
2872         xmlFreeTextReader(reader);
2873         xmlCleanupParser();
2874
2875         return PMINFO_R_OK;
2876 }
2877
2878 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
2879 {
2880         const char *val = NULL;
2881         const xmlChar *node;
2882         xmlTextReaderPtr reader;
2883         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
2884         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2885
2886         xmlInitParser();
2887         reader = xmlReaderForFile(manifest, NULL, 0);
2888
2889         if (reader){
2890                 if ( __child_element(reader, -1)) {
2891                         node = xmlTextReaderConstName(reader);
2892                         if (!node) {
2893                                 _LOGE("xmlTextReaderConstName value is NULL\n");
2894                                 xmlFreeTextReader(reader);
2895                                 xmlCleanupParser();
2896                                 return PMINFO_R_ERROR;
2897                         }
2898
2899                         if (!strcmp(ASC_CHAR(node), "manifest")) {
2900                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
2901                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
2902
2903                                 if (val) {
2904                                         if (strcmp(val, "internal-only") == 0)
2905                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
2906                                         else if (strcmp(val, "prefer-external") == 0)
2907                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
2908                                         else
2909                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
2910                                 }
2911                         } else {
2912                                 _LOGE("Unable to create xml reader\n");
2913                                 xmlFreeTextReader(reader);
2914                                 xmlCleanupParser();
2915                                 return PMINFO_R_ERROR;
2916                         }
2917                 }
2918         } else {
2919                 _LOGE("xmlReaderForFile value is NULL\n");
2920                 xmlCleanupParser();
2921                 return PMINFO_R_ERROR;
2922         }
2923
2924         xmlFreeTextReader(reader);
2925         xmlCleanupParser();
2926
2927         return PMINFO_R_OK;
2928 }
2929
2930
2931 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
2932 {
2933         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2934         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2935
2936         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2937         if (info->manifest_info->root_path)
2938                 *path = (char *)info->manifest_info->root_path;
2939         else
2940                 return PMINFO_R_ERROR;
2941
2942         return PMINFO_R_OK;
2943 }
2944
2945 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
2946 {
2947         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2948         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2949
2950         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2951         if (info->manifest_info->csc_path)
2952                 *path = (char *)info->manifest_info->csc_path;
2953         else
2954                 *path = (char *)info->manifest_info->csc_path;
2955
2956         return PMINFO_R_OK;
2957 }
2958
2959 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(const char *lhs_package_id, const char *rhs_package_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
2960 {
2961         retvm_if(lhs_package_id == NULL, PMINFO_R_EINVAL, "lhs package ID is NULL");
2962         retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
2963         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2964
2965         int ret = PMINFO_R_OK;
2966         char query[MAX_QUERY_LEN] = {'\0'};
2967         char *error_message = NULL;
2968         pkgmgr_cert_x *info= NULL;
2969         int lcert = 0;
2970         int rcert = 0;
2971         int exist = -1;
2972         *compare_result = PMINFO_CERT_COMPARE_ERROR;
2973         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
2974         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
2975
2976         ret = db_util_open_with_options(CERT_DB, &cert_db,
2977                                         SQLITE_OPEN_READONLY, NULL);
2978         if (ret != SQLITE_OK) {
2979                 _LOGE("connect db [%s] failed!\n", CERT_DB);
2980                 ret = PMINFO_R_ERROR;
2981                 goto err;
2982         }
2983
2984         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", lhs_package_id);
2985         if (SQLITE_OK !=
2986             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
2987                 _LOGE("Don't execute query = %s error message = %s\n", query,
2988                        error_message);
2989                 ret = PMINFO_R_ERROR;
2990                 goto err;
2991         }
2992
2993         if (exist == 0) {
2994                 lcert = 0;
2995         } else {
2996                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", lhs_package_id);
2997                 if (SQLITE_OK !=
2998                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
2999                         _LOGE("Don't execute query = %s error message = %s\n", query,
3000                                    error_message);
3001                         ret = PMINFO_R_ERROR;
3002                         goto err;
3003                 }
3004                 lcert = info->cert_id;
3005         }
3006
3007         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", rhs_package_id);
3008         if (SQLITE_OK !=
3009                 sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3010                 _LOGE("Don't execute query = %s error message = %s\n", query,
3011                            error_message);
3012                 ret = PMINFO_R_ERROR;
3013                 goto err;
3014         }
3015
3016         if (exist == 0) {
3017                 rcert = 0;
3018         } else {
3019                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", rhs_package_id);
3020                 if (SQLITE_OK !=
3021                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3022                         _LOGE("Don't execute query = %s error message = %s\n", query,
3023                                    error_message);
3024                         ret = PMINFO_R_ERROR;
3025                         goto err;
3026                 }
3027                 rcert = info->cert_id;
3028         }
3029
3030         if ((lcert == 0) || (rcert == 0))
3031         {
3032                 if ((lcert == 0) && (rcert == 0))
3033                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
3034                 else if (lcert == 0)
3035                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
3036                 else if (rcert == 0)
3037                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
3038         } else {
3039                 if (lcert == rcert)
3040                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
3041                 else
3042                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
3043         }
3044
3045 err:
3046         sqlite3_free(error_message);
3047         sqlite3_close(cert_db);
3048         if (info) {
3049                 if (info->pkgid) {
3050                         free(info->pkgid);
3051                         info->pkgid = NULL;
3052                 }
3053                 free(info);
3054                 info = NULL;
3055         }
3056         return ret;
3057 }
3058
3059
3060 API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id, const char *rhs_app_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
3061 {
3062         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
3063         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
3064         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3065
3066         int ret = PMINFO_R_OK;
3067         char query[MAX_QUERY_LEN] = {'\0'};
3068         char *error_message = NULL;
3069         pkgmgr_cert_x *info= NULL;
3070         int exist = -1;
3071         char *lpkgid = NULL;
3072         char *rpkgid = NULL;
3073
3074         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3075         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3076
3077         ret = db_util_open_with_options(MANIFEST_DB, &manifest_db,
3078                                         SQLITE_OPEN_READONLY, NULL);
3079         if (ret != SQLITE_OK) {
3080                 _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
3081                 ret = PMINFO_R_ERROR;
3082                 goto err;
3083         }
3084
3085         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3086         if (SQLITE_OK !=
3087             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3088                 _LOGE("Don't execute query = %s error message = %s\n", query,
3089                        error_message);
3090                 ret = PMINFO_R_ERROR;
3091                 goto err;
3092         }
3093
3094         if (exist == 0) {
3095                 lpkgid = NULL;
3096         } else {
3097                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3098                 if (SQLITE_OK !=
3099                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3100                         _LOGE("Don't execute query = %s error message = %s\n", query,
3101                                    error_message);
3102                         ret = PMINFO_R_ERROR;
3103                         goto err;
3104                 }
3105                 lpkgid = strdup(info->pkgid);
3106                 if (lpkgid == NULL) {
3107                         _LOGE("Out of Memory\n");
3108                         ret = PMINFO_R_ERROR;
3109                         goto err;
3110                 }
3111                 free(info->pkgid);
3112                 info->pkgid = NULL;
3113         }
3114
3115         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3116         if (SQLITE_OK !=
3117             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3118                 _LOGE("Don't execute query = %s error message = %s\n", query,
3119                        error_message);
3120                 ret = PMINFO_R_ERROR;
3121                 goto err;
3122         }
3123
3124         if (exist == 0) {
3125                 rpkgid = NULL;
3126         } else {
3127                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3128                 if (SQLITE_OK !=
3129                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3130                         _LOGE("Don't execute query = %s error message = %s\n", query,
3131                                    error_message);
3132                         ret = PMINFO_R_ERROR;
3133                         goto err;
3134                 }
3135                 rpkgid = strdup(info->pkgid);
3136                 if (rpkgid == NULL) {
3137                         _LOGE("Out of Memory\n");
3138                         ret = PMINFO_R_ERROR;
3139                         goto err;
3140                 }
3141                 free(info->pkgid);
3142                 info->pkgid = NULL;
3143         }
3144         ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lpkgid, rpkgid, compare_result);
3145  err:
3146         sqlite3_free(error_message);
3147         sqlite3_close(manifest_db);
3148         if (info) {
3149                 if (info->pkgid) {
3150                         free(info->pkgid);
3151                         info->pkgid = NULL;
3152                 }
3153                 free(info);
3154                 info = NULL;
3155         }
3156         if (lpkgid) {
3157                 free(lpkgid);
3158                 lpkgid = NULL;
3159         }
3160         if (rpkgid) {
3161                 free(rpkgid);
3162                 rpkgid = NULL;
3163         }
3164         return ret;
3165 }
3166
3167 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
3168 {
3169         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3170         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3171         char *pkgid = NULL;
3172         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
3173         if (pkgid == NULL){
3174                  _LOGD("invalid func parameters\n");
3175                  return PMINFO_R_ERROR;
3176         }
3177          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
3178
3179         FILE *fp = NULL;
3180         char app_mmc_path[FILENAME_MAX] = { 0, };
3181         char app_dir_path[FILENAME_MAX] = { 0, };
3182         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
3183         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
3184         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
3185         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
3186
3187         /*check whether application is in external memory or not */
3188         fp = fopen(app_mmc_path, "r");
3189         if (fp == NULL){
3190                 _LOGD(" app path in external memory not accesible\n");
3191         } else {
3192                 fclose(fp);
3193                 fp = NULL;
3194                 *accessible = 1;
3195                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
3196                 return PMINFO_R_OK;
3197         }
3198
3199         /*check whether application is in internal or not */
3200         fp = fopen(app_dir_path, "r");
3201         if (fp == NULL) {
3202                 _LOGD(" app path in internal memory not accesible\n");
3203                 *accessible = 0;
3204                 return PMINFO_R_ERROR;
3205         } else {
3206                 fclose(fp);
3207                 /*check whether the application is installed in SD card
3208                 but SD card is not present*/
3209                 fp = fopen(app_mmc_internal_path, "r");
3210                 if (fp == NULL){
3211                         *accessible = 1;
3212                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
3213                         return PMINFO_R_OK;
3214                 }
3215                 else{
3216                         *accessible = 0;
3217                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
3218                 }
3219                 fclose(fp);
3220         }
3221
3222         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
3223         return PMINFO_R_OK;
3224 }
3225
3226 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
3227 {
3228         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3229         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3230         char *val = NULL;
3231         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3232         val = (char *)info->manifest_info->removable;
3233         if (val) {
3234                 if (strcasecmp(val, "true") == 0)
3235                         *removable = 1;
3236                 else if (strcasecmp(val, "false") == 0)
3237                         *removable = 0;
3238                 else
3239                         *removable = 1;
3240         }
3241         return PMINFO_R_OK;
3242 }
3243
3244 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
3245 {
3246         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3247         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3248
3249         char *val = NULL;
3250         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3251
3252         val = (char *)info->manifest_info->installlocation;
3253         if (val) {
3254                 if (strcmp(val, "internal-only") == 0)
3255                         *movable = 0;
3256                 else if (strcmp(val, "prefer-external") == 0)
3257                         *movable = 1;
3258                 else
3259                         *movable = 1;
3260         }
3261
3262         return PMINFO_R_OK;
3263 }
3264
3265 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
3266 {
3267         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3268         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3269         char *val = NULL;
3270         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3271         val = (char *)info->manifest_info->preload;
3272         if (val) {
3273                 if (strcasecmp(val, "true") == 0)
3274                         *preload = 1;
3275                 else if (strcasecmp(val, "false") == 0)
3276                         *preload = 0;
3277                 else
3278                         *preload = 0;
3279         }
3280         return PMINFO_R_OK;
3281 }
3282
3283 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
3284 {
3285         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3286         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3287         char *val = NULL;
3288         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3289         val = (char *)info->manifest_info->readonly;
3290         if (val) {
3291                 if (strcasecmp(val, "true") == 0)
3292                         *readonly = 1;
3293                 else if (strcasecmp(val, "false") == 0)
3294                         *readonly = 0;
3295                 else
3296                         *readonly = 0;
3297         }
3298         return PMINFO_R_OK;
3299 }
3300
3301 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
3302 {
3303         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3304         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3305
3306         char *val = NULL;
3307         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3308         val = (char *)info->manifest_info->update;
3309         if (val) {
3310                 if (strcasecmp(val, "true") == 0)
3311                         *update = 1;
3312                 else if (strcasecmp(val, "false") == 0)
3313                         *update = 0;
3314                 else
3315                         *update = 1;
3316         }
3317         return PMINFO_R_OK;
3318 }
3319
3320 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
3321 {
3322         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3323         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3324         __cleanup_pkginfo(info);
3325         return PMINFO_R_OK;
3326 }
3327
3328 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
3329 {
3330         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
3331         *handle = NULL;
3332         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
3333         if (filter == NULL) {
3334                 _LOGE("Out of Memory!!!");
3335                 return PMINFO_R_ERROR;
3336         }
3337         *handle = filter;
3338         return PMINFO_R_OK;
3339 }
3340
3341 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
3342 {
3343         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3344         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3345         if (filter->list){
3346                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
3347                 g_slist_free(filter->list);
3348         }
3349         free(filter);
3350         filter = NULL;
3351         return PMINFO_R_OK;
3352 }
3353
3354 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
3355                                 const char *property, const int value)
3356 {
3357         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3358         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3359         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
3360         char *val = NULL;
3361         GSList *link = NULL;
3362         int prop = -1;
3363         prop = _pminfo_pkginfo_convert_to_prop_int(property);
3364         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
3365                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
3366                 _LOGE("Invalid Integer Property\n");
3367                 return PMINFO_R_EINVAL;
3368         }
3369         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3370         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3371         if (node == NULL) {
3372                 _LOGE("Out of Memory!!!\n");
3373                 return PMINFO_R_ERROR;
3374         }
3375         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
3376         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
3377         if (val == NULL) {
3378                 _LOGE("Out of Memory\n");
3379                 free(node);
3380                 node = NULL;
3381                 return PMINFO_R_ERROR;
3382         }
3383         node->prop = prop;
3384         node->value = val;
3385         /*If API is called multiple times for same property, we should override the previous values.
3386         Last value set will be used for filtering.*/
3387         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3388         if (link)
3389                 filter->list = g_slist_delete_link(filter->list, link);
3390         filter->list = g_slist_append(filter->list, (gpointer)node);
3391         return PMINFO_R_OK;
3392
3393 }
3394
3395 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
3396                                 const char *property, const bool value)
3397 {
3398         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3399         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3400         char *val = NULL;
3401         GSList *link = NULL;
3402         int prop = -1;
3403         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
3404         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
3405                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
3406                 _LOGE("Invalid Boolean Property\n");
3407                 return PMINFO_R_EINVAL;
3408         }
3409         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3410         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3411         if (node == NULL) {
3412                 _LOGE("Out of Memory!!!\n");
3413                 return PMINFO_R_ERROR;
3414         }
3415         if (value)
3416                 val = strndup("('true','True')", 15);
3417         else
3418                 val = strndup("('false','False')", 17);
3419         if (val == NULL) {
3420                 _LOGE("Out of Memory\n");
3421                 free(node);
3422                 node = NULL;
3423                 return PMINFO_R_ERROR;
3424         }
3425         node->prop = prop;
3426         node->value = val;
3427         /*If API is called multiple times for same property, we should override the previous values.
3428         Last value set will be used for filtering.*/
3429         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3430         if (link)
3431                 filter->list = g_slist_delete_link(filter->list, link);
3432         filter->list = g_slist_append(filter->list, (gpointer)node);
3433         return PMINFO_R_OK;
3434
3435 }
3436
3437 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
3438                                 const char *property, const char *value)
3439 {
3440         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3441         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3442         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3443         char *val = NULL;
3444         GSList *link = NULL;
3445         int prop = -1;
3446         prop = _pminfo_pkginfo_convert_to_prop_str(property);
3447         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
3448                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
3449                 _LOGE("Invalid String Property\n");
3450                 return PMINFO_R_EINVAL;
3451         }
3452         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3453         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3454         if (node == NULL) {
3455                 _LOGE("Out of Memory!!!\n");
3456                 return PMINFO_R_ERROR;
3457         }
3458         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
3459                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
3460         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
3461                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
3462         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
3463                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
3464         else if (strcmp(value, "installed_internal") == 0)
3465                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
3466         else if (strcmp(value, "installed_external") == 0)
3467                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
3468         else
3469                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
3470         if (val == NULL) {
3471                 _LOGE("Out of Memory\n");
3472                 free(node);
3473                 node = NULL;
3474                 return PMINFO_R_ERROR;
3475         }
3476         node->prop = prop;
3477         node->value = val;
3478         /*If API is called multiple times for same property, we should override the previous values.
3479         Last value set will be used for filtering.*/
3480         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3481         if (link)
3482                 filter->list = g_slist_delete_link(filter->list, link);
3483         filter->list = g_slist_append(filter->list, (gpointer)node);
3484         return PMINFO_R_OK;
3485
3486 }
3487
3488 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
3489 {
3490         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3491         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3492         char *syslocale = NULL;
3493         char *locale = NULL;
3494         char *condition = NULL;
3495         char *error_message = NULL;
3496         char query[MAX_QUERY_LEN] = {'\0'};
3497         char where[MAX_QUERY_LEN] = {'\0'};
3498         GSList *list;
3499         int ret = 0;
3500
3501         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3502         /*Get current locale*/
3503         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3504         if (syslocale == NULL) {
3505                 _LOGE("current locale is NULL\n");
3506                 return PMINFO_R_ERROR;
3507         }
3508         locale = __convert_system_locale_to_manifest_locale(syslocale);
3509         if (locale == NULL) {
3510                 _LOGE("manifest locale is NULL\n");
3511                 free(syslocale);
3512                 return PMINFO_R_ERROR;
3513         }
3514
3515         ret = __open_manifest_db();
3516         if (ret == -1) {
3517                 _LOGE("Fail to open manifest DB\n");
3518                 ret = PMINFO_R_ERROR;
3519                 goto err;
3520         }
3521
3522         /*Start constructing query*/
3523         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
3524
3525         /*Get where clause*/
3526         for (list = filter->list; list; list = g_slist_next(list)) {
3527                 __get_filter_condition(list->data, &condition);
3528                 if (condition) {
3529                         strncat(where, condition, sizeof(where) - strlen(where) -1);
3530                         where[sizeof(where) - 1] = '\0';
3531                         free(condition);
3532                         condition = NULL;
3533                 }
3534                 if (g_slist_next(list)) {
3535                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
3536                         where[sizeof(where) - 1] = '\0';
3537                 }
3538         }
3539         _LOGE("where = %s\n", where);
3540         if (strlen(where) > 0) {
3541                 strncat(query, where, sizeof(query) - strlen(query) - 1);
3542                 query[sizeof(query) - 1] = '\0';
3543         }
3544         _LOGE("query = %s\n", query);
3545
3546         /*Execute Query*/
3547         if (SQLITE_OK !=
3548             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
3549                 _LOGE("Don't execute query = %s error message = %s\n", query,
3550                        error_message);
3551                 sqlite3_free(error_message);
3552                 sqlite3_close(manifest_db);
3553                 ret = PMINFO_R_ERROR;
3554                 *count = 0;
3555                 goto err;
3556         }
3557         ret = PMINFO_R_OK;
3558 err:
3559         if (locale) {
3560                 free(locale);
3561                 locale = NULL;
3562         }
3563         if (syslocale) {
3564                 free(syslocale);
3565                 syslocale = NULL;
3566         }
3567         sqlite3_close(manifest_db);
3568         return ret;
3569 }
3570
3571 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
3572                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
3573 {
3574         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3575         retvm_if(pkg_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3576         char *syslocale = NULL;
3577         char *locale = NULL;
3578         char *condition = NULL;
3579         char *error_message = NULL;
3580         char query[MAX_QUERY_LEN] = {'\0'};
3581         char where[MAX_QUERY_LEN] = {'\0'};
3582         GSList *list;
3583         int ret = 0;
3584         label_x *tmp1 = NULL;
3585         icon_x *tmp2 = NULL;
3586         description_x *tmp3 = NULL;
3587         author_x *tmp4 = NULL;
3588         privilege_x *tmp5 = NULL;
3589         pkgmgr_pkginfo_x *node = NULL;
3590         pkgmgr_pkginfo_x *tmphead = NULL;
3591         pkgmgr_pkginfo_x *pkginfo = NULL;
3592
3593         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3594         /*Get current locale*/
3595         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3596         if (syslocale == NULL) {
3597                 _LOGE("current locale is NULL\n");
3598                 return PMINFO_R_ERROR;
3599         }
3600         locale = __convert_system_locale_to_manifest_locale(syslocale);
3601         if (locale == NULL) {
3602                 _LOGE("manifest locale is NULL\n");
3603                 free(syslocale);
3604                 return PMINFO_R_ERROR;
3605         }
3606
3607         ret = __open_manifest_db();
3608         if (ret == -1) {
3609                 _LOGE("Fail to open manifest DB\n");
3610                 ret = PMINFO_R_ERROR;
3611                 goto err;
3612         }
3613         /*Start constructing query*/
3614         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, locale);
3615
3616         /*Get where clause*/
3617         for (list = filter->list; list; list = g_slist_next(list)) {
3618                 __get_filter_condition(list->data, &condition);
3619                 if (condition) {
3620                         strncat(where, condition, sizeof(where) - strlen(where) -1);
3621                         where[sizeof(where) - 1] = '\0';
3622                         free(condition);
3623                         condition = NULL;
3624                 }
3625                 if (g_slist_next(list)) {
3626                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
3627                         where[sizeof(where) - 1] = '\0';
3628                 }
3629         }
3630         _LOGE("where = %s\n", where);
3631         if (strlen(where) > 0) {
3632                 strncat(query, where, sizeof(query) - strlen(query) - 1);
3633                 query[sizeof(query) - 1] = '\0';
3634         }
3635         _LOGE("query = %s\n", query);
3636         tmphead = calloc(1, sizeof(pkgmgr_pkginfo_x));
3637         if (tmphead == NULL) {
3638                 _LOGE("Out of Memory!!!\n");
3639                 ret = PMINFO_R_ERROR;
3640                 goto err;
3641         }
3642
3643         if (SQLITE_OK !=
3644             sqlite3_exec(manifest_db, query, __pkg_list_cb, (void *)tmphead, &error_message)) {
3645                 _LOGE("Don't execute query = %s error message = %s\n", query,
3646                        error_message);
3647                 sqlite3_free(error_message);
3648                 sqlite3_close(manifest_db);
3649                 ret = PMINFO_R_ERROR;
3650                 goto err;
3651         }
3652
3653         LISTHEAD(tmphead, node);
3654         for(node = node->next ; node ; node = node->next) {
3655                 pkginfo = node;
3656                 pkginfo->locale = strdup(locale);
3657                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
3658                 if (pkginfo->manifest_info->privileges == NULL) {
3659                         _LOGE("Failed to allocate memory for privileges info\n");
3660                         ret = PMINFO_R_ERROR;
3661                         goto err;
3662                 }
3663
3664                 /*populate manifest_info from DB*/
3665                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
3666                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3667                 if (ret == -1) {
3668                         _LOGE("Package Info DB Information retrieval failed\n");
3669                         ret = PMINFO_R_ERROR;
3670                         goto err;
3671                 }
3672                 memset(query, '\0', MAX_QUERY_LEN);
3673                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
3674                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
3675                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3676                 if (ret == -1) {
3677                         _LOGE("Package Info DB Information retrieval failed\n");
3678                         ret = PMINFO_R_ERROR;
3679                         goto err;
3680                 }
3681                 /*Also store the values corresponding to default locales*/
3682                 memset(query, '\0', MAX_QUERY_LEN);
3683                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
3684                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
3685                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3686                 if (ret == -1) {
3687                         _LOGE("Package Info DB Information retrieval failed\n");
3688                         ret = PMINFO_R_ERROR;
3689                         goto err;
3690                 }
3691                 if (pkginfo->manifest_info->label) {
3692                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
3693                         pkginfo->manifest_info->label = tmp1;
3694                 }
3695                 if (pkginfo->manifest_info->icon) {
3696                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
3697                         pkginfo->manifest_info->icon = tmp2;
3698                 }
3699                 if (pkginfo->manifest_info->description) {
3700                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
3701                         pkginfo->manifest_info->description = tmp3;
3702                 }
3703                 if (pkginfo->manifest_info->author) {
3704                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
3705                         pkginfo->manifest_info->author = tmp4;
3706                 }
3707                 if (pkginfo->manifest_info->privileges->privilege) {
3708                         LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
3709                         pkginfo->manifest_info->privileges->privilege = tmp5;
3710                 }
3711         }
3712
3713         LISTHEAD(tmphead, node);
3714
3715         for(node = node->next ; node ; node = node->next) {
3716                 pkginfo = node;
3717                 ret = pkg_cb( (void *)pkginfo, user_data);
3718                 if(ret < 0)
3719                         break;
3720         }
3721         ret = PMINFO_R_OK;
3722
3723 err:
3724         if (locale) {
3725                 free(locale);
3726                 locale = NULL;
3727         }
3728         if (syslocale) {
3729                 free(syslocale);
3730                 syslocale = NULL;
3731         }
3732         sqlite3_close(manifest_db);
3733         __cleanup_pkginfo(tmphead);
3734         return ret;
3735 }
3736
3737 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
3738                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
3739 {
3740         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3741         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
3742         int ret = -1;
3743         privilege_x *ptr = NULL;
3744         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3745         ptr = info->manifest_info->privileges->privilege;
3746         for (; ptr; ptr = ptr->next) {
3747                 if (ptr->text){
3748                         ret = privilege_func(ptr->text, user_data);
3749                         if (ret < 0)
3750                                 break;
3751                 }
3752         }
3753         return PMINFO_R_OK;
3754 }
3755
3756 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
3757                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
3758 {
3759         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3760         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback pointer is NULL");
3761         retvm_if((component != PMINFO_UI_APP) && (component != PMINFO_SVC_APP) && (component != PMINFO_ALL_APP), PMINFO_R_EINVAL, "Invalid App Component Type");
3762
3763         char *syslocale = NULL;
3764         char *locale = NULL;
3765         int ret = -1;
3766         char query[MAX_QUERY_LEN] = {'\0'};
3767         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3768         pkgmgr_pkginfo_x *allinfo = NULL;
3769         pkgmgr_appinfo_x *appinfo = NULL;
3770         icon_x *ptr1 = NULL;
3771         label_x *ptr2 = NULL;
3772         category_x *ptr3 = NULL;
3773         metadata_x *ptr4 = NULL;
3774         permission_x *ptr5 = NULL;
3775         image_x *ptr6 = NULL;
3776         sqlite3 *appinfo_db = NULL;
3777
3778         /*get system locale*/
3779         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3780         tryvm_if(syslocale == NULL, ret = PMINFO_R_EINVAL, "current locale is NULL");
3781
3782         /*get locale on db*/
3783         locale = __convert_system_locale_to_manifest_locale(syslocale);
3784         tryvm_if(locale == NULL, ret = PMINFO_R_EINVAL, "manifest locale is NULL");
3785
3786         /*calloc allinfo*/
3787         allinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
3788         tryvm_if(allinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
3789
3790         /*calloc manifest_info*/
3791         allinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
3792         tryvm_if(allinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
3793
3794         /*calloc appinfo*/
3795         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
3796         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
3797
3798         /*set component type*/
3799         if (component == PMINFO_UI_APP)
3800                 appinfo->app_component = PMINFO_UI_APP;
3801         if (component == PMINFO_SVC_APP)
3802                 appinfo->app_component = PMINFO_SVC_APP;
3803         if (component == PMINFO_ALL_APP)
3804                 appinfo->app_component = PMINFO_ALL_APP;
3805
3806         /*open db */
3807         ret = db_util_open_with_options(MANIFEST_DB, &appinfo_db, SQLITE_OPEN_READONLY, NULL);
3808         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
3809
3810         appinfo->package = strdup(info->manifest_info->package);
3811         snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
3812                         "from package_app_info where " \
3813                         "package='%s' and app_component='%s'",
3814                         info->manifest_info->package,
3815                         (appinfo->app_component==PMINFO_UI_APP ? "uiapp" : "svcapp"));
3816
3817         switch(component) {
3818         case PMINFO_UI_APP:
3819                 /*Populate ui app info */
3820                 ret = __exec_db_query(appinfo_db, query, __uiapp_list_cb, (void *)info);
3821                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
3822
3823                 uiapplication_x *tmp = NULL;
3824                 if (info->manifest_info->uiapplication) {
3825                         LISTHEAD(info->manifest_info->uiapplication, tmp);
3826                         info->manifest_info->uiapplication = tmp;
3827                 }
3828                 /*Populate localized info for default locales and call callback*/
3829                 /*If the callback func return < 0 we break and no more call back is called*/
3830                 while(tmp != NULL)
3831                 {
3832                         appinfo->locale = strdup(locale);
3833                         appinfo->uiapp_info = tmp;
3834                         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
3835                                 if (locale) {
3836                                         free(locale);
3837                                 }
3838                                 locale = __get_app_locale_by_fallback(appinfo_db, appinfo->uiapp_info->appid, syslocale);
3839                         }
3840
3841                         memset(query, '\0', MAX_QUERY_LEN);
3842                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
3843                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3844                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3845
3846                         memset(query, '\0', MAX_QUERY_LEN);
3847                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
3848                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3849                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3850
3851                         /*store setting notification icon section*/
3852                         memset(query, '\0', MAX_QUERY_LEN);
3853                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
3854                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3855                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
3856                         
3857                         /*store app preview image info*/
3858                         memset(query, '\0', MAX_QUERY_LEN);
3859                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
3860                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3861                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
3862
3863                         if (appinfo->uiapp_info->label) {
3864                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
3865                                 appinfo->uiapp_info->label = ptr2;
3866                         }
3867                         if (appinfo->uiapp_info->icon) {
3868                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
3869                                 appinfo->uiapp_info->icon = ptr1;
3870                         }
3871                         if (appinfo->uiapp_info->category) {
3872                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
3873                                 appinfo->uiapp_info->category = ptr3;
3874                         }
3875                         if (appinfo->uiapp_info->metadata) {
3876                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
3877                                 appinfo->uiapp_info->metadata = ptr4;
3878                         }
3879                         if (appinfo->uiapp_info->permission) {
3880                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
3881                                 appinfo->uiapp_info->permission = ptr5;
3882                         }
3883                         if (appinfo->uiapp_info->image) {
3884                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
3885                                 appinfo->uiapp_info->image = ptr6;
3886                         }
3887                         ret = app_func((void *)appinfo, user_data);
3888                         if (ret < 0)
3889                                 break;
3890                         tmp = tmp->next;
3891                 }
3892                 break;
3893         case PMINFO_SVC_APP:
3894                 /*Populate svc app info */
3895                 ret = __exec_db_query(appinfo_db, query, __svcapp_list_cb, (void *)info);
3896                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
3897
3898                 serviceapplication_x *tmp1 = NULL;
3899                 if (info->manifest_info->serviceapplication) {
3900                         LISTHEAD(info->manifest_info->serviceapplication, tmp1);
3901                         info->manifest_info->serviceapplication = tmp1;
3902                 }
3903                 /*Populate localized info for default locales and call callback*/
3904                 /*If the callback func return < 0 we break and no more call back is called*/
3905                 while(tmp1 != NULL)
3906                 {
3907                         appinfo->locale = strdup(locale);
3908                         appinfo->svcapp_info = tmp1;
3909                         memset(query, '\0', MAX_QUERY_LEN);
3910                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
3911                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3912                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3913
3914                         memset(query, '\0', MAX_QUERY_LEN);
3915                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, DEFAULT_LOCALE);
3916                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3917                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3918
3919                         if (appinfo->svcapp_info->label) {
3920                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
3921                                 appinfo->svcapp_info->label = ptr2;
3922                         }
3923                         if (appinfo->svcapp_info->icon) {
3924                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
3925                                 appinfo->svcapp_info->icon = ptr1;
3926                         }
3927                         if (appinfo->svcapp_info->category) {
3928                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
3929                                 appinfo->svcapp_info->category = ptr3;
3930                         }
3931                         if (appinfo->svcapp_info->metadata) {
3932                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
3933                                 appinfo->svcapp_info->metadata = ptr4;
3934                         }
3935                         if (appinfo->svcapp_info->permission) {
3936                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
3937                                 appinfo->svcapp_info->permission = ptr5;
3938                         }
3939                         ret = app_func((void *)appinfo, user_data);
3940                         if (ret < 0)
3941                                 break;
3942                         tmp1 = tmp1->next;
3943                 }
3944                 break;
3945         case PMINFO_ALL_APP:
3946                 memset(query, '\0', MAX_QUERY_LEN);
3947                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where package='%s'", info->manifest_info->package);
3948
3949                 /*Populate all app info */
3950                 ret = __exec_db_query(appinfo_db, query, __allapp_list_cb, (void *)allinfo);
3951                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
3952
3953                 /*UI Apps*/
3954                 appinfo->app_component = PMINFO_UI_APP;
3955                 uiapplication_x *tmp2 = NULL;
3956                 if (allinfo->manifest_info->uiapplication) {
3957                         LISTHEAD(allinfo->manifest_info->uiapplication, tmp2);
3958                         allinfo->manifest_info->uiapplication = tmp2;
3959                 }
3960                 /*Populate localized info for default locales and call callback*/
3961                 /*If the callback func return < 0 we break and no more call back is called*/
3962                 while(tmp2 != NULL)
3963                 {
3964                         appinfo->locale = strdup(locale);
3965                         appinfo->uiapp_info = tmp2;
3966                         memset(query, '\0', MAX_QUERY_LEN);
3967                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
3968                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3969                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3970
3971                         memset(query, '\0', MAX_QUERY_LEN);
3972                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
3973                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3974                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
3975
3976                         /*store setting notification icon section*/
3977                         memset(query, '\0', MAX_QUERY_LEN);
3978                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
3979                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3980                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
3981                         
3982                         /*store app preview image info*/
3983                         memset(query, '\0', MAX_QUERY_LEN);
3984                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
3985                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
3986                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
3987
3988                         if (appinfo->uiapp_info->label) {
3989                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
3990                                 appinfo->uiapp_info->label = ptr2;
3991                         }
3992                         if (appinfo->uiapp_info->icon) {
3993                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
3994                                 appinfo->uiapp_info->icon = ptr1;
3995                         }
3996                         if (appinfo->uiapp_info->category) {
3997                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
3998                                 appinfo->uiapp_info->category = ptr3;
3999                         }
4000                         if (appinfo->uiapp_info->metadata) {
4001                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
4002                                 appinfo->uiapp_info->metadata = ptr4;
4003                         }
4004                         if (appinfo->uiapp_info->permission) {
4005                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
4006                                 appinfo->uiapp_info->permission = ptr5;
4007                         }
4008                         if (appinfo->uiapp_info->image) {
4009                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
4010                                 appinfo->uiapp_info->image = ptr6;
4011                         }
4012                         ret = app_func((void *)appinfo, user_data);
4013                         if (ret < 0)
4014                                 break;
4015                         tmp2 = tmp2->next;
4016                 }
4017
4018                 /*SVC Apps*/
4019                 appinfo->app_component = PMINFO_SVC_APP;
4020                 serviceapplication_x *tmp3 = NULL;
4021                 if (allinfo->manifest_info->serviceapplication) {
4022                         LISTHEAD(allinfo->manifest_info->serviceapplication, tmp3);
4023                         allinfo->manifest_info->serviceapplication = tmp3;
4024                 }
4025                 /*Populate localized info for default locales and call callback*/
4026                 /*If the callback func return < 0 we break and no more call back is called*/
4027                 while(tmp3 != NULL)
4028                 {
4029                         appinfo->locale = strdup(locale);
4030                         appinfo->svcapp_info = tmp3;
4031                         memset(query, '\0', MAX_QUERY_LEN);
4032                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4033                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4034                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4035
4036                         memset(query, '\0', MAX_QUERY_LEN);
4037                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, DEFAULT_LOCALE);
4038                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4039                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4040
4041                         if (appinfo->svcapp_info->label) {
4042                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4043                                 appinfo->svcapp_info->label = ptr2;
4044                         }
4045                         if (appinfo->svcapp_info->icon) {
4046                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4047                                 appinfo->svcapp_info->icon = ptr1;
4048                         }
4049                         if (appinfo->svcapp_info->category) {
4050                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
4051                                 appinfo->svcapp_info->category = ptr3;
4052                         }
4053                         if (appinfo->svcapp_info->metadata) {
4054                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
4055                                 appinfo->svcapp_info->metadata = ptr4;
4056                         }
4057                         if (appinfo->svcapp_info->permission) {
4058                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
4059                                 appinfo->svcapp_info->permission = ptr5;
4060                         }
4061                         ret = app_func((void *)appinfo, user_data);
4062                         if (ret < 0)
4063                                 break;
4064                         tmp3 = tmp3->next;
4065                 }
4066                 appinfo->app_component = PMINFO_ALL_APP;
4067                 break;
4068
4069         }
4070
4071         ret = PMINFO_R_OK;
4072 catch:
4073         if (locale) {
4074                 free(locale);
4075                 locale = NULL;
4076         }
4077         if (syslocale) {
4078                 free(syslocale);
4079                 syslocale = NULL;
4080         }
4081         if (appinfo) {
4082                 if (appinfo->package) {
4083                         free((void *)appinfo->package);
4084                         appinfo->package = NULL;
4085                 }
4086                 free(appinfo);
4087                 appinfo = NULL;
4088         }
4089         __cleanup_pkginfo(allinfo);
4090
4091         sqlite3_close(appinfo_db);
4092         return ret;
4093 }
4094
4095 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
4096 {
4097         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
4098
4099         int ret = PMINFO_R_OK;
4100         char query[MAX_QUERY_LEN] = {'\0'};
4101         pkgmgr_appinfo_x *info = NULL;
4102         pkgmgr_appinfo_x *appinfo = NULL;
4103         uiapplication_x *ptr1 = NULL;
4104         sqlite3 *appinfo_db = NULL;
4105
4106         /*open db*/
4107         ret = db_util_open_with_options(MANIFEST_DB, &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4108         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
4109
4110         /*calloc appinfo*/
4111         info = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4112         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4113
4114         /*calloc uiapplication_x*/
4115         info->uiapp_info= (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
4116         tryvm_if(info->uiapp_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4117
4118         /*calloc appinfo*/
4119         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4120         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4121
4122         /*query package_app_info*/
4123         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
4124         ret = __exec_db_query(appinfo_db, query, __mini_appinfo_cb, (void *)info);
4125         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4126
4127         LISTHEAD(info->uiapp_info, ptr1);
4128
4129         /*call back*/
4130         for(ptr1 = ptr1->next ; ptr1 ; ptr1 = ptr1->next)
4131         {
4132                 appinfo->uiapp_info= ptr1;
4133                 appinfo->package = strdup(ptr1->package);
4134                 appinfo->app_component = PMINFO_UI_APP;
4135
4136                 ret = app_func((void *)appinfo, user_data);
4137                 if (ret < 0)
4138                         break;
4139                 free((void *)appinfo->package);
4140                 appinfo->package = NULL;
4141         }
4142
4143 catch:
4144         sqlite3_close(appinfo_db);
4145
4146         if (appinfo) {
4147                 free(appinfo);
4148                 appinfo = NULL;
4149         }
4150         __cleanup_appinfo(info);
4151         return ret;
4152 }
4153
4154 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
4155 {
4156         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
4157
4158         int ret = PMINFO_R_OK;
4159         char query[MAX_QUERY_LEN] = {'\0'};
4160         char *syslocale = NULL;
4161         char *locale = NULL;
4162         pkgmgr_appinfo_x *appinfo = NULL;
4163         uiapplication_x *ptr1 = NULL;
4164         serviceapplication_x *ptr2 = NULL;
4165         label_x *tmp1 = NULL;
4166         icon_x *tmp2 = NULL;
4167         category_x *tmp3 = NULL;
4168         metadata_x *tmp4 = NULL;
4169         permission_x *tmp5 = NULL;
4170         image_x *tmp6 = NULL;
4171         sqlite3 *appinfo_db = NULL;
4172
4173         /*get system locale*/
4174         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4175         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
4176
4177         /*get locale on db*/
4178         locale = __convert_system_locale_to_manifest_locale(syslocale);
4179         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
4180
4181         /*open db*/
4182         ret = db_util_open_with_options(MANIFEST_DB, &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4183         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
4184
4185         /*calloc pkginfo*/
4186         pkgmgr_pkginfo_x *info = NULL;
4187         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
4188         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4189
4190         /*calloc manifest_info*/
4191         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
4192         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4193
4194         /*calloc appinfo*/
4195         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4196         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4197
4198         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
4199         ret = __exec_db_query(appinfo_db, query, __app_list_cb, (void *)info);
4200         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4201
4202         if (info->manifest_info->uiapplication) {
4203                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
4204                 info->manifest_info->uiapplication = ptr1;
4205         }
4206         if (info->manifest_info->serviceapplication) {
4207                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
4208                 info->manifest_info->serviceapplication = ptr2;
4209         }
4210
4211         /*UI Apps*/
4212         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
4213         {
4214                 appinfo->locale = strdup(locale);
4215                 appinfo->app_component = PMINFO_UI_APP;
4216                 appinfo->package = strdup(ptr1->package);
4217                 appinfo->uiapp_info = ptr1;
4218                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4219                                 "from package_app_info where " \
4220                                 "app_id='%s'", ptr1->appid);
4221                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4222                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4223
4224                 if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
4225                         if (locale) {
4226                                 free(locale);
4227                         }
4228                         locale = __get_app_locale_by_fallback(appinfo_db, ptr1->appid, syslocale);
4229                 }
4230
4231                 memset(query, '\0', MAX_QUERY_LEN);
4232                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4233                                 "from package_app_localized_info where " \
4234                                 "app_id='%s' and app_locale='%s'",
4235                                 ptr1->appid, locale);
4236                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4237                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4238
4239                 memset(query, '\0', MAX_QUERY_LEN);
4240                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4241                                 "from package_app_localized_info where " \
4242                                 "app_id='%s' and app_locale='%s'",
4243                                 ptr1->appid, DEFAULT_LOCALE);
4244
4245                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4246                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4247
4248                 /*store setting notification icon section*/
4249                 memset(query, '\0', MAX_QUERY_LEN);
4250                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", ptr1->appid);
4251                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4252                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4253                 
4254                 /*store app preview image info*/
4255                 memset(query, '\0', MAX_QUERY_LEN);
4256                 snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", ptr1->appid);
4257                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4258                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4259
4260                 if (appinfo->uiapp_info->label) {
4261                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
4262                         appinfo->uiapp_info->label = tmp1;
4263                 }
4264                 if (appinfo->uiapp_info->icon) {
4265                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
4266                         appinfo->uiapp_info->icon= tmp2;
4267                 }
4268                 if (appinfo->uiapp_info->category) {
4269                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
4270                         appinfo->uiapp_info->category = tmp3;
4271                 }
4272                 if (appinfo->uiapp_info->metadata) {
4273                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
4274                         appinfo->uiapp_info->metadata = tmp4;
4275                 }
4276                 if (appinfo->uiapp_info->permission) {
4277                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
4278                         appinfo->uiapp_info->permission = tmp5;
4279                 }
4280                 if (appinfo->uiapp_info->image) {
4281                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
4282                         appinfo->uiapp_info->image = tmp6;
4283                 }
4284                 ret = app_func((void *)appinfo, user_data);
4285                 if (ret < 0)
4286                         break;
4287                 free((void *)appinfo->package);
4288                 appinfo->package = NULL;
4289         }
4290         /*Service Apps*/
4291         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
4292         {
4293                 appinfo->locale = strdup(locale);
4294                 appinfo->app_component = PMINFO_SVC_APP;
4295                 appinfo->package = strdup(ptr2->package);
4296                 appinfo->svcapp_info = ptr2;
4297                 memset(query, '\0', MAX_QUERY_LEN);
4298                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4299                                 "from package_app_info where " \
4300                                 "app_id='%s'", ptr2->appid);
4301                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4302                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4303
4304                 memset(query, '\0', MAX_QUERY_LEN);
4305                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4306                                 "from package_app_localized_info where " \
4307                                 "app_id='%s' and app_locale='%s'",
4308                                 ptr2->appid, locale);
4309                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4310                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4311
4312                 memset(query, '\0', MAX_QUERY_LEN);
4313                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4314                                 "from package_app_localized_info where " \
4315                                 "app_id='%s' and app_locale='%s'",
4316                                 ptr2->appid, DEFAULT_LOCALE);
4317                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4318                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4319
4320                 if (appinfo->svcapp_info->label) {
4321                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
4322                         appinfo->svcapp_info->label = tmp1;
4323                 }
4324                 if (appinfo->svcapp_info->icon) {
4325                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
4326                         appinfo->svcapp_info->icon= tmp2;
4327                 }
4328                 if (appinfo->svcapp_info->category) {
4329                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
4330                         appinfo->svcapp_info->category = tmp3;
4331                 }
4332                 if (appinfo->svcapp_info->metadata) {
4333                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
4334                         appinfo->svcapp_info->metadata = tmp4;
4335                 }
4336                 if (appinfo->svcapp_info->permission) {
4337                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
4338                         appinfo->svcapp_info->permission = tmp5;
4339                 }
4340                 ret = app_func((void *)appinfo, user_data);
4341                 if (ret < 0)
4342                         break;
4343                 free((void *)appinfo->package);
4344                 appinfo->package = NULL;
4345         }
4346         ret = PMINFO_R_OK;
4347
4348 catch:
4349         if (locale) {
4350                 free(locale);
4351                 locale = NULL;
4352         }
4353         if (syslocale) {
4354                 free(syslocale);
4355                 syslocale = NULL;
4356         }
4357         sqlite3_close(appinfo_db);
4358         if (appinfo) {
4359                 free(appinfo);
4360                 appinfo = NULL;
4361         }
4362         __cleanup_pkginfo(info);
4363         return ret;
4364 }
4365
4366 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
4367 {
4368         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL");
4369         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4370
4371         pkgmgr_appinfo_x *appinfo = NULL;
4372         char *syslocale = NULL;
4373         char *locale = NULL;
4374         int ret = -1;
4375         int exist = 0;
4376         label_x *tmp1 = NULL;
4377         icon_x *tmp2 = NULL;
4378         category_x *tmp3 = NULL;
4379         metadata_x *tmp4 = NULL;
4380         permission_x *tmp5 = NULL;
4381         image_x *tmp6 = NULL;
4382         char query[MAX_QUERY_LEN] = {'\0'};
4383         sqlite3 *appinfo_db = NULL;
4384
4385         /*open db*/
4386         ret = db_util_open_with_options(MANIFEST_DB, &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4387         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
4388
4389         /*check appid exist on db*/
4390         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", appid);
4391         ret = __exec_db_query(appinfo_db, query, __validate_cb, (void *)&exist);
4392         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec fail");
4393         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "Appid[%s] not found in DB", appid);
4394
4395         /*get system locale*/
4396         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4397         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
4398
4399         /*get locale on db*/
4400         locale = __convert_system_locale_to_manifest_locale(syslocale);
4401         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
4402
4403         /*calloc appinfo*/
4404         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4405         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
4406
4407         /*check app_component from DB*/
4408         memset(query, '\0', MAX_QUERY_LEN);
4409         snprintf(query, MAX_QUERY_LEN, "select app_component, package from package_app_info where app_id='%s' ", appid);
4410         ret = __exec_db_query(appinfo_db, query, __appcomponent_cb, (void *)appinfo);
4411         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4412
4413         /*calloc app_component*/
4414         if (appinfo->app_component == PMINFO_UI_APP) {
4415                 appinfo->uiapp_info = (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
4416                 tryvm_if(appinfo->uiapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for uiapp info");
4417         } else {
4418                 appinfo->svcapp_info = (serviceapplication_x *)calloc(1, sizeof(serviceapplication_x));
4419                 tryvm_if(appinfo->svcapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for svcapp info");
4420         }
4421         appinfo->locale = strdup(locale);
4422
4423         /*populate app_info from DB*/
4424         memset(query, '\0', MAX_QUERY_LEN);
4425         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' ", appid);
4426         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4427         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4428
4429         memset(query, '\0', MAX_QUERY_LEN);
4430         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, locale);
4431         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4432         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4433
4434         /*Also store the values corresponding to default locales*/
4435         memset(query, '\0', MAX_QUERY_LEN);
4436         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, DEFAULT_LOCALE);
4437         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4438         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4439
4440         /*Populate app category*/
4441         memset(query, '\0', MAX_QUERY_LEN);
4442         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_category where app_id='%s'", appid);
4443         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4444         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Category Info DB Information retrieval failed");
4445
4446         /*Populate app metadata*/
4447         memset(query, '\0', MAX_QUERY_LEN);
4448         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_metadata where app_id='%s'", appid);
4449         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4450         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Metadata Info DB Information retrieval failed");
4451
4452         /*Populate app permission*/
4453         memset(query, '\0', MAX_QUERY_LEN);
4454         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_permission where app_id='%s'", appid);
4455         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4456         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App permission Info DB Information retrieval failed");
4457
4458         /*store setting notification icon section*/
4459         memset(query, '\0', MAX_QUERY_LEN);
4460         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appid);
4461         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4462         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4463
4464         /*store app preview image info*/
4465         memset(query, '\0', MAX_QUERY_LEN);
4466         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appid);
4467         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4468         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4469
4470         switch (appinfo->app_component) {
4471         case PMINFO_UI_APP:
4472                 if (appinfo->uiapp_info->label) {
4473                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
4474                         appinfo->uiapp_info->label = tmp1;
4475                 }
4476                 if (appinfo->uiapp_info->icon) {
4477                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
4478                         appinfo->uiapp_info->icon = tmp2;
4479                 }
4480                 if (appinfo->uiapp_info->category) {
4481                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
4482                         appinfo->uiapp_info->category = tmp3;
4483                 }
4484                 if (appinfo->uiapp_info->metadata) {
4485                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
4486                         appinfo->uiapp_info->metadata = tmp4;
4487                 }
4488                 if (appinfo->uiapp_info->permission) {
4489                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
4490                         appinfo->uiapp_info->permission = tmp5;
4491                 }
4492                 if (appinfo->uiapp_info->image) {
4493                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
4494                         appinfo->uiapp_info->image = tmp6;
4495                 }
4496                 break;
4497         case PMINFO_SVC_APP:
4498                 if (appinfo->svcapp_info->label) {
4499                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
4500                         appinfo->svcapp_info->label = tmp1;
4501                 }
4502                 if (appinfo->svcapp_info->icon) {
4503                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
4504                         appinfo->svcapp_info->icon = tmp2;
4505                 }
4506                 if (appinfo->svcapp_info->category) {
4507                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
4508                         appinfo->svcapp_info->category = tmp3;
4509                 }
4510                 if (appinfo->svcapp_info->metadata) {
4511                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
4512                         appinfo->svcapp_info->metadata = tmp4;
4513                 }
4514                 if (appinfo->svcapp_info->permission) {
4515                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
4516                         appinfo->svcapp_info->permission = tmp5;
4517                 }
4518                 break;
4519         default:
4520                 break;
4521         }
4522
4523         ret = PMINFO_R_OK;
4524
4525 catch:
4526         if (ret == PMINFO_R_OK)
4527                 *handle = (void*)appinfo;
4528         else {
4529                 *handle = NULL;
4530                 __cleanup_appinfo(appinfo);
4531         }
4532
4533         sqlite3_close(appinfo_db);
4534         if (syslocale) {
4535                 free(syslocale);
4536                 syslocale = NULL;
4537         }
4538         if (locale) {
4539                 free(locale);
4540                 locale = NULL;
4541         }
4542         return ret;
4543 }
4544
4545
4546 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h  handle, char **appid)
4547 {
4548         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4549         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4550         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4551
4552         if (info->app_component == PMINFO_UI_APP)
4553                 *appid = (char *)info->uiapp_info->appid;
4554         else if (info->app_component == PMINFO_SVC_APP)
4555                 *appid = (char *)info->svcapp_info->appid;
4556
4557         return PMINFO_R_OK;
4558 }
4559
4560 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h  handle, char **pkg_name)
4561 {
4562         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4563         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4564         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4565
4566         *pkg_name = (char *)info->package;
4567
4568         return PMINFO_R_OK;
4569 }
4570
4571 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h  handle, char **pkgid)
4572 {
4573         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4574         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4575         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4576
4577         *pkgid = (char *)info->package;
4578
4579         return PMINFO_R_OK;
4580 }
4581
4582 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h  handle, char **exec)
4583 {
4584         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4585         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4586         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4587
4588         if (info->app_component == PMINFO_UI_APP)
4589                 *exec = (char *)info->uiapp_info->exec;
4590         if (info->app_component == PMINFO_SVC_APP)
4591                 *exec = (char *)info->svcapp_info->exec;
4592
4593         return PMINFO_R_OK;
4594 }
4595
4596
4597 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4598 {
4599         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4600         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4601         char *locale = NULL;
4602         icon_x *ptr = NULL;
4603         icon_x *start = NULL;
4604         *icon = NULL;
4605
4606         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4607                 locale = info->locale;
4608                 retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
4609
4610         if (info->app_component == PMINFO_UI_APP)
4611                 start = info->uiapp_info->icon;
4612         if (info->app_component == PMINFO_SVC_APP)
4613                 start = info->svcapp_info->icon;
4614         for(ptr = start; ptr != NULL; ptr = ptr->next)
4615         {
4616                 if (ptr->lang) {
4617                         if (strcmp(ptr->lang, locale) == 0) {
4618                                 *icon = (char *)ptr->text;
4619                                 if (strcasecmp(*icon, "(null)") == 0) {
4620                                         locale = DEFAULT_LOCALE;
4621                                         continue;
4622                                 } else
4623                                         break;
4624                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
4625                                 *icon = (char *)ptr->text;
4626                                 break;
4627                         }
4628                 }
4629         }
4630         return PMINFO_R_OK;
4631 }
4632
4633
4634 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h  handle, char **label)
4635 {
4636         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4637         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4638         char *locale = NULL;
4639         label_x *ptr = NULL;
4640         label_x *start = NULL;
4641         *label = NULL;
4642
4643         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4644         locale = info->locale;
4645         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
4646
4647         if (info->app_component == PMINFO_UI_APP)
4648                 start = info->uiapp_info->label;
4649         if (info->app_component == PMINFO_SVC_APP)
4650                 start = info->svcapp_info->label;
4651         for(ptr = start; ptr != NULL; ptr = ptr->next)
4652         {
4653                 if (ptr->lang) {
4654                         if (strcmp(ptr->lang, locale) == 0) {
4655                                 *label = (char *)ptr->text;
4656                                 if (strcasecmp(*label, "(null)") == 0) {
4657                                         locale = DEFAULT_LOCALE;
4658                                         continue;
4659                                 } else
4660                                         break;
4661                         } else if (strncasecmp(ptr->lang, locale, 2) == 0) {
4662                                 *label = (char *)ptr->text;
4663                                 if (strcasecmp(*label, "(null)") == 0) {
4664                                                 locale = DEFAULT_LOCALE;
4665                                                 continue;
4666                                 } else
4667                                                 break;
4668                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
4669                                 *label = (char *)ptr->text;
4670                                 break;
4671                         }
4672                 }
4673         }
4674         return PMINFO_R_OK;
4675 }
4676
4677
4678 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_component *component)
4679 {
4680         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4681         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4682         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4683
4684         if (info->app_component == PMINFO_UI_APP)
4685                 *component = PMINFO_UI_APP;
4686         else if (info->app_component == PMINFO_SVC_APP)
4687                 *component = PMINFO_SVC_APP;
4688         else
4689                 return PMINFO_R_ERROR;
4690
4691         return PMINFO_R_OK;
4692 }
4693
4694 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h  handle, char **app_type)
4695 {
4696         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4697         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4698         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4699
4700         if (info->app_component == PMINFO_UI_APP)
4701                 *app_type = (char *)info->uiapp_info->type;
4702         if (info->app_component == PMINFO_SVC_APP)
4703                 *app_type = (char *)info->svcapp_info->type;
4704
4705         return PMINFO_R_OK;
4706 }
4707
4708 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
4709                                         int *operation_count, char ***operation)
4710 {
4711         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4712         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4713         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4714         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4715         *operation_count = data->operation_count;
4716         *operation = data->operation;
4717         return PMINFO_R_OK;
4718 }
4719
4720 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
4721                                         int *uri_count, char ***uri)
4722 {
4723         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4724         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4725         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4726         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4727         *uri_count = data->uri_count;
4728         *uri = data->uri;
4729         return PMINFO_R_OK;
4730 }
4731
4732 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
4733                                         int *mime_count, char ***mime)
4734 {
4735         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4736         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4737         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4738         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4739         *mime_count = data->mime_count;
4740         *mime = data->mime;
4741         return PMINFO_R_OK;
4742 }
4743
4744 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
4745                                         int *subapp_count, char ***subapp)
4746 {
4747         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4748         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4749         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4750         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4751         *subapp_count = data->subapp_count;
4752         *subapp = data->subapp;
4753         return PMINFO_R_OK;
4754 }
4755
4756 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4757 {
4758         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
4759         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4760
4761         char *val = NULL;
4762         icon_x *ptr = NULL;
4763         icon_x *start = NULL;
4764         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4765
4766         start = info->uiapp_info->icon;
4767
4768         for(ptr = start; ptr != NULL; ptr = ptr->next)
4769         {
4770                 if (ptr->section) {
4771                         val = (char *)ptr->section;
4772                         if (strcmp(val, "setting") == 0){
4773                                 *icon = (char *)ptr->text;
4774                                 break;
4775                         }
4776                 }
4777         }
4778         return PMINFO_R_OK;
4779 }
4780
4781
4782 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4783 {
4784         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
4785         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4786
4787         char *val = NULL;
4788         icon_x *ptr = NULL;
4789         icon_x *start = NULL;
4790         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4791
4792         start = info->uiapp_info->icon;
4793
4794         for(ptr = start; ptr != NULL; ptr = ptr->next)
4795         {
4796                 if (ptr->section) {
4797                         val = (char *)ptr->section;
4798
4799                         if (strcmp(val, "notification") == 0){
4800                                 *icon = (char *)ptr->text;
4801                                 break;
4802                         }
4803                 }
4804         }
4805
4806         return PMINFO_R_OK;
4807 }
4808
4809 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_recentimage *type)
4810 {
4811         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4812         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4813         char *val = NULL;
4814         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4815         val = (char *)info->uiapp_info->recentimage;
4816         if (val) {
4817                 if (strcasecmp(val, "capture") == 0)
4818                         *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
4819                 else if (strcasecmp(val, "icon") == 0)
4820                         *type = PMINFO_RECENTIMAGE_USE_ICON;
4821                 else
4822                         *type = PMINFO_RECENTIMAGE_USE_NOTHING;
4823         }
4824
4825         return PMINFO_R_OK;
4826 }
4827
4828 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h  handle, char **preview_img)
4829 {
4830         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
4831         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4832
4833         char *val = NULL;
4834         image_x *ptr = NULL;
4835         image_x *start = NULL;
4836         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4837
4838         start = info->uiapp_info->image;
4839
4840         for(ptr = start; ptr != NULL; ptr = ptr->next)
4841         {
4842                 if (ptr->section) {
4843                         val = (char *)ptr->section;
4844
4845                         if (strcmp(val, "preview") == 0)
4846                                 *preview_img = (char *)ptr->text;
4847
4848                         break;
4849                 }
4850         }
4851         return PMINFO_R_OK;
4852 }
4853
4854 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_permission_type *permission)
4855 {
4856         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
4857         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4858
4859         char *val = NULL;
4860         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4861
4862         val = info->uiapp_info->permission_type;
4863
4864         if (strcmp(val, "signature") == 0)
4865                 *permission = PMINFO_PERMISSION_SIGNATURE;
4866         else if (strcmp(val, "privilege") == 0)
4867                 *permission = PMINFO_PERMISSION_PRIVILEGE;
4868         else
4869                 *permission = PMINFO_PERMISSION_NORMAL;
4870
4871         return PMINFO_R_OK;
4872 }
4873
4874 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h  handle, char **component_type)
4875 {
4876         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4877         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4878         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4879
4880         *component_type = (char *)info->uiapp_info->component_type;
4881
4882         return PMINFO_R_OK;
4883 }
4884
4885 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
4886 {
4887         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4888         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4889         char *val = NULL;
4890         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4891         val = (char *)info->uiapp_info->hwacceleration;
4892         if (val) {
4893                 if (strcasecmp(val, "not-use-GL") == 0)
4894                         *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
4895                 else if (strcasecmp(val, "use-GL") == 0)
4896                         *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
4897                 else
4898                         *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
4899         }
4900         return PMINFO_R_OK;
4901 }
4902
4903 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h  handle, char **portrait_img, char **landscape_img)
4904 {
4905         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4906         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4907         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4908         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4909
4910         if (info->app_component == PMINFO_UI_APP){
4911                 *portrait_img = (char *)info->uiapp_info->portraitimg;
4912                 *landscape_img = (char *)info->uiapp_info->landscapeimg;
4913         }
4914
4915         return PMINFO_R_OK;
4916 }
4917
4918 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
4919 {
4920         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4921         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
4922         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4923
4924         *submode_mainid = (char *)info->uiapp_info->submode_mainid;
4925
4926         return PMINFO_R_OK;
4927 }
4928
4929 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
4930                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
4931 {
4932         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4933         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
4934         int ret = -1;
4935         permission_x *ptr = NULL;
4936         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4937         if (info->app_component == PMINFO_UI_APP)
4938                 ptr = info->uiapp_info->permission;
4939         else if (info->app_component == PMINFO_SVC_APP)
4940                 ptr = info->svcapp_info->permission;
4941         else
4942                 return PMINFO_R_EINVAL;
4943         for (; ptr; ptr = ptr->next) {
4944                 ret = permission_func(ptr->value, user_data);
4945                 if (ret < 0)
4946                         break;
4947         }
4948         return PMINFO_R_OK;
4949 }
4950
4951 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
4952                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
4953 {
4954         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4955         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
4956         int ret = -1;
4957         category_x *ptr = NULL;
4958         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4959         if (info->app_component == PMINFO_UI_APP)
4960                 ptr = info->uiapp_info->category;
4961         else if (info->app_component == PMINFO_SVC_APP)
4962                 ptr = info->svcapp_info->category;
4963         else
4964                 return PMINFO_R_EINVAL;
4965         for (; ptr; ptr = ptr->next) {
4966                 ret = category_func(ptr->name, user_data);
4967                 if (ret < 0)
4968                         break;
4969         }
4970         return PMINFO_R_OK;
4971 }
4972
4973 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
4974                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
4975 {
4976         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4977         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
4978         int ret = -1;
4979         metadata_x *ptr = NULL;
4980         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4981         if (info->app_component == PMINFO_UI_APP)
4982                 ptr = info->uiapp_info->metadata;
4983         else if (info->app_component == PMINFO_SVC_APP)
4984                 ptr = info->svcapp_info->metadata;
4985         else
4986                 return PMINFO_R_EINVAL;
4987         for (; ptr; ptr = ptr->next) {
4988                 ret = metadata_func(ptr->key, ptr->value, user_data);
4989                 if (ret < 0)
4990                         break;
4991         }
4992         return PMINFO_R_OK;
4993 }
4994
4995 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
4996                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
4997 {
4998         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
4999         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5000         int i = 0;
5001         int ret = -1;
5002         int oc = 0;
5003         int mc = 0;
5004         int uc = 0;
5005         int sc = 0;
5006         char *pkgid = NULL;
5007         char *manifest = NULL;
5008         char **operation = NULL;
5009         char **uri = NULL;
5010         char **mime = NULL;
5011         char **subapp = NULL;
5012         appcontrol_x *appcontrol = NULL;
5013         manifest_x *mfx = NULL;
5014         operation_x *op = NULL;
5015         uri_x *ui = NULL;
5016         mime_x *mi = NULL;
5017         subapp_x *sa = NULL;
5018         pkgmgrinfo_app_component component;
5019         pkgmgrinfo_appcontrol_x *ptr = NULL;
5020         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
5021         if (ret < 0) {
5022                 _LOGE("Failed to get package name\n");
5023                 return PMINFO_R_ERROR;
5024         }
5025         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
5026         if (ret < 0) {
5027                 _LOGE("Failed to get app component name\n");
5028                 return PMINFO_R_ERROR;
5029         }
5030         manifest = pkgmgr_parser_get_manifest_file(pkgid);
5031         if (manifest == NULL) {
5032                 _LOGE("Failed to fetch package manifest file\n");
5033                 return PMINFO_R_ERROR;
5034         }
5035         mfx = pkgmgr_parser_process_manifest_xml(manifest);
5036         if (mfx == NULL) {
5037                 _LOGE("Failed to parse package manifest file\n");
5038                 free(manifest);
5039                 manifest = NULL;
5040                 return PMINFO_R_ERROR;
5041         }
5042         free(manifest);
5043         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
5044         if (ptr == NULL) {
5045                 _LOGE("Out of Memory!!!\n");
5046                 pkgmgr_parser_free_manifest_xml(mfx);
5047                 return PMINFO_R_ERROR;
5048         }
5049         /*Get Operation, Uri, Mime*/
5050         switch (component) {
5051         case PMINFO_UI_APP:
5052                 if (mfx->uiapplication) {
5053                         if (mfx->uiapplication->appsvc) {
5054                                 appcontrol = mfx->uiapplication->appsvc;
5055                         }
5056                 }
5057                 break;
5058         case PMINFO_SVC_APP:
5059                 if (mfx->serviceapplication) {
5060                         if (mfx->serviceapplication->appsvc) {
5061                                 appcontrol = mfx->serviceapplication->appsvc;
5062                         }
5063                 }
5064                 break;
5065         default:
5066                 break;
5067         }
5068         for (; appcontrol; appcontrol = appcontrol->next) {
5069                 op = appcontrol->operation;
5070                 for (; op; op = op->next)
5071                         oc = oc + 1;
5072                 op = appcontrol->operation;
5073
5074                 ui = appcontrol->uri;
5075                 for (; ui; ui = ui->next)
5076                         uc = uc + 1;
5077                 ui = appcontrol->uri;
5078
5079                 mi = appcontrol->mime;
5080                 for (; mi; mi = mi->next)
5081                         mc = mc + 1;
5082                 mi = appcontrol->mime;
5083
5084                 sa = appcontrol->subapp;
5085                 for (; sa; sa = sa->next)
5086                         sc = sc + 1;
5087                 sa = appcontrol->subapp;
5088
5089                 operation = (char **)calloc(oc, sizeof(char *));
5090                 for (i = 0; i < oc; i++) {
5091                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
5092                         op = op->next;
5093                 }
5094
5095                 uri = (char **)calloc(uc, sizeof(char *));
5096                 for (i = 0; i < uc; i++) {
5097                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
5098                         ui = ui->next;
5099                 }
5100
5101                 mime = (char **)calloc(mc, sizeof(char *));
5102                 for (i = 0; i < mc; i++) {
5103                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
5104                         mi = mi->next;
5105                 }
5106
5107                 subapp = (char **)calloc(sc, sizeof(char *));
5108                 for (i = 0; i < sc; i++) {
5109                         subapp[i] = strndup(sa->name, PKG_STRING_LEN_MAX - 1);
5110                         sa = sa->next;
5111                 }
5112
5113                 /*populate appcontrol handle*/
5114                 ptr->operation_count = oc;
5115                 ptr->uri_count = uc;
5116                 ptr->mime_count = mc;
5117                 ptr->subapp_count = sc;
5118                 ptr->operation = operation;
5119                 ptr->uri = uri;
5120                 ptr->mime = mime;
5121                 ptr->subapp = subapp;
5122
5123                 ret = appcontrol_func((void *)ptr, user_data);
5124                 for (i = 0; i < oc; i++) {
5125                         if (operation[i]) {
5126                                 free(operation[i]);
5127                                 operation[i] = NULL;
5128                         }
5129                 }
5130                 if (operation) {
5131                         free(operation);
5132                         operation = NULL;
5133                 }
5134                 for (i = 0; i < uc; i++) {
5135                         if (uri[i]) {
5136                                 free(uri[i]);
5137                                 uri[i] = NULL;
5138                         }
5139                 }
5140                 if (uri) {
5141                         free(uri);
5142                         uri = NULL;
5143                 }
5144                 for (i = 0; i < mc; i++) {
5145                         if (mime[i]) {
5146                                 free(mime[i]);
5147                                 mime[i] = NULL;
5148                         }
5149                 }
5150                 if (mime) {
5151                         free(mime);
5152                         mime = NULL;
5153                 }
5154                 for (i = 0; i < sc; i++) {
5155                         if (subapp[i]) {
5156                                 free(subapp[i]);
5157                                 subapp[i] = NULL;
5158                         }
5159                 }
5160                 if (subapp) {
5161                         free(subapp);
5162                         subapp = NULL;
5163                 }
5164                 if (ret < 0)
5165                         break;
5166                 uc = 0;
5167                 mc = 0;
5168                 oc = 0;
5169                 sc = 0;
5170         }
5171         pkgmgr_parser_free_manifest_xml(mfx);
5172         if (ptr) {
5173                 free(ptr);
5174                 ptr = NULL;
5175         }
5176         return PMINFO_R_OK;
5177 }
5178
5179 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h  handle, bool *nodisplay)
5180 {
5181         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5182         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5183         char *val = NULL;
5184         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5185         val = (char *)info->uiapp_info->nodisplay;
5186         if (val) {
5187                 if (strcasecmp(val, "true") == 0)
5188                         *nodisplay = 1;
5189                 else if (strcasecmp(val, "false") == 0)
5190                         *nodisplay = 0;
5191                 else
5192                         *nodisplay = 0;
5193         }
5194         return PMINFO_R_OK;
5195 }
5196
5197 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h  handle, bool *multiple)
5198 {
5199         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5200         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5201         char *val = NULL;
5202         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5203         val = (char *)info->uiapp_info->multiple;
5204         if (val) {
5205                 if (strcasecmp(val, "true") == 0)
5206                         *multiple = 1;
5207                 else if (strcasecmp(val, "false") == 0)
5208                         *multiple = 0;
5209                 else
5210                         *multiple = 0;
5211         }
5212         return PMINFO_R_OK;
5213 }
5214
5215 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
5216 {
5217         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5218         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5219         char *val = NULL;
5220         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5221         val = (char *)info->uiapp_info->indicatordisplay;
5222         if (val) {
5223                 if (strcasecmp(val, "true") == 0){
5224                         *indicator_disp = 1;
5225                 }else if (strcasecmp(val, "false") == 0){
5226                         *indicator_disp = 0;
5227                 }else{
5228                         *indicator_disp = 0;
5229                 }
5230         }
5231         return PMINFO_R_OK;
5232 }
5233
5234 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
5235 {
5236         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5237         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5238         char *val = NULL;
5239         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5240         val = (char *)info->uiapp_info->taskmanage;
5241         if (val) {
5242                 if (strcasecmp(val, "true") == 0)
5243                         *taskmanage = 1;
5244                 else if (strcasecmp(val, "false") == 0)
5245                         *taskmanage = 0;
5246                 else
5247                         *taskmanage = 0;
5248         }
5249         return PMINFO_R_OK;
5250 }
5251
5252 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
5253 {
5254         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5255         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5256         char *val = NULL;
5257         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5258         if (info->app_component == PMINFO_UI_APP)
5259                 val = (char *)info->uiapp_info->enabled;
5260         else if (info->app_component == PMINFO_SVC_APP)
5261                 val = (char *)info->uiapp_info->enabled;
5262         else {
5263                 _LOGE("invalid component type\n");
5264                 return PMINFO_R_EINVAL;
5265         }
5266
5267         if (val) {
5268                 if (strcasecmp(val, "true") == 0)
5269                         *enabled = 1;
5270                 else if (strcasecmp(val, "false") == 0)
5271                         *enabled = 0;
5272                 else
5273                         *enabled = 1;
5274         }
5275         return PMINFO_R_OK;
5276
5277 }
5278
5279 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
5280 {
5281         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5282         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5283         char *val = NULL;
5284         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5285         val = (char *)info->svcapp_info->onboot;
5286         if (val) {
5287                 if (strcasecmp(val, "true") == 0)
5288                         *onboot = 1;
5289                 else if (strcasecmp(val, "false") == 0)
5290                         *onboot = 0;
5291                 else
5292                         *onboot = 0;
5293         }
5294         return PMINFO_R_OK;
5295 }
5296
5297 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
5298 {
5299         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5300         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5301         char *val = NULL;
5302         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5303         val = (char *)info->svcapp_info->autorestart;
5304         if (val) {
5305                 if (strcasecmp(val, "true") == 0)
5306                         *autorestart = 1;
5307                 else if (strcasecmp(val, "false") == 0)
5308                         *autorestart = 0;
5309                 else
5310                         *autorestart = 0;
5311         }
5312         return PMINFO_R_OK;
5313 }
5314
5315 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
5316 {
5317         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5318         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5319         char *val = NULL;
5320         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5321         val = (char *)info->uiapp_info->mainapp;
5322         if (val) {
5323                 if (strcasecmp(val, "true") == 0)
5324                         *mainapp = 1;
5325                 else if (strcasecmp(val, "false") == 0)
5326                         *mainapp = 0;
5327                 else
5328                         *mainapp = 0;
5329         }
5330         return PMINFO_R_OK;
5331 }
5332
5333 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
5334 {
5335         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5336         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5337         char *val = NULL;
5338         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5339         val = (char *)info->uiapp_info->preload;
5340         if (val) {
5341                 if (strcasecmp(val, "true") == 0)
5342                         *preload = 1;
5343                 else if (strcasecmp(val, "false") == 0)
5344                         *preload = 0;
5345                 else
5346                         *preload = 0;
5347         }
5348         return PMINFO_R_OK;
5349 }
5350
5351 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
5352 {
5353         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5354         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5355         char *val = NULL;
5356         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5357         val = (char *)info->uiapp_info->submode;
5358         if (val) {
5359                 if (strcasecmp(val, "true") == 0)
5360                         *submode = 1;
5361                 else if (strcasecmp(val, "false") == 0)
5362                         *submode = 0;
5363                 else
5364                         *submode = 0;
5365         }
5366         return PMINFO_R_OK;
5367 }
5368
5369 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h  handle)
5370 {
5371         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5372         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5373         __cleanup_appinfo(info);
5374         return PMINFO_R_OK;
5375 }
5376
5377 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
5378 {
5379         return (pkgmgrinfo_pkginfo_filter_create(handle));
5380 }
5381
5382 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
5383 {
5384         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
5385 }
5386
5387 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
5388                                 const char *property, const int value)
5389 {
5390         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5391         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5392         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
5393         char *val = NULL;
5394         GSList *link = NULL;
5395         int prop = -1;
5396         prop = _pminfo_appinfo_convert_to_prop_int(property);
5397         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
5398                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
5399                 _LOGE("Invalid Integer Property\n");
5400                 return PMINFO_R_EINVAL;
5401         }
5402         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5403         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5404         if (node == NULL) {
5405                 _LOGE("Out of Memory!!!\n");
5406                 return PMINFO_R_ERROR;
5407         }
5408         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
5409         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
5410         if (val == NULL) {
5411                 _LOGE("Out of Memory\n");
5412                 free(node);
5413                 node = NULL;
5414                 return PMINFO_R_ERROR;
5415         }
5416         node->prop = prop;
5417         node->value = val;
5418         /*If API is called multiple times for same property, we should override the previous values.
5419         Last value set will be used for filtering.*/
5420         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5421         if (link)
5422                 filter->list = g_slist_delete_link(filter->list, link);
5423         filter->list = g_slist_append(filter->list, (gpointer)node);
5424         return PMINFO_R_OK;
5425
5426 }
5427
5428 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
5429                                 const char *property, const bool value)
5430 {
5431         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5432         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5433         char *val = NULL;
5434         GSList *link = NULL;
5435         int prop = -1;
5436         prop = _pminfo_appinfo_convert_to_prop_bool(property);
5437         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
5438                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
5439                 _LOGE("Invalid Boolean Property\n");
5440                 return PMINFO_R_EINVAL;
5441         }
5442         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5443         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5444         if (node == NULL) {
5445                 _LOGE("Out of Memory!!!\n");
5446                 return PMINFO_R_ERROR;
5447         }
5448         if (value)
5449                 val = strndup("('true','True')", 15);
5450         else
5451                 val = strndup("('false','False')", 17);
5452         if (val == NULL) {
5453                 _LOGE("Out of Memory\n");
5454                 free(node);
5455                 node = NULL;
5456                 return PMINFO_R_ERROR;
5457         }
5458         node->prop = prop;
5459         node->value = val;
5460         /*If API is called multiple times for same property, we should override the previous values.
5461         Last value set will be used for filtering.*/
5462         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5463         if (link)
5464                 filter->list = g_slist_delete_link(filter->list, link);
5465         filter->list = g_slist_append(filter->list, (gpointer)node);
5466         return PMINFO_R_OK;
5467
5468 }
5469
5470 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
5471                                 const char *property, const char *value)
5472 {
5473         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5474         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5475         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5476         char *val = NULL;
5477         pkgmgrinfo_node_x *ptr = NULL;
5478         char prev[PKG_STRING_LEN_MAX] = {'\0'};
5479         char temp[PKG_STRING_LEN_MAX] = {'\0'};
5480         GSList *link = NULL;
5481         int prop = -1;
5482         prop = _pminfo_appinfo_convert_to_prop_str(property);
5483         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
5484                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
5485                 _LOGE("Invalid String Property\n");
5486                 return PMINFO_R_EINVAL;
5487         }
5488         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5489         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5490         if (node == NULL) {
5491                 _LOGE("Out of Memory!!!\n");
5492                 return PMINFO_R_ERROR;
5493         }
5494         node->prop = prop;
5495         switch (prop) {
5496         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
5497                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
5498                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
5499                 else
5500                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
5501                 node->value = val;
5502                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5503                 if (link)
5504                         filter->list = g_slist_delete_link(filter->list, link);
5505                 filter->list = g_slist_append(filter->list, (gpointer)node);
5506                 break;
5507         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
5508         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
5509         case E_PMINFO_APPINFO_PROP_APP_URI:
5510         case E_PMINFO_APPINFO_PROP_APP_MIME:
5511                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
5512                 if (val == NULL) {
5513                         _LOGE("Out of Memory\n");
5514                         free(node);
5515                         node = NULL;
5516                         return PMINFO_R_ERROR;
5517                 }
5518                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5519                 if (link) {
5520                         ptr = (pkgmgrinfo_node_x *)link->data;
5521                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
5522                         _LOGE("Previous value is %s\n", prev);
5523                         filter->list = g_slist_delete_link(filter->list, link);
5524                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
5525                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
5526                         _LOGE("New value is %s\n", val);
5527                         node->value = val;
5528                         filter->list = g_slist_append(filter->list, (gpointer)node);
5529                         memset(temp, '\0', PKG_STRING_LEN_MAX);
5530                 } else {
5531                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
5532                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
5533                         _LOGE("First value is %s\n", val);
5534                         node->value = val;
5535                         filter->list = g_slist_append(filter->list, (gpointer)node);
5536                         memset(temp, '\0', PKG_STRING_LEN_MAX);
5537                 }
5538                 break;
5539         default:
5540                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
5541                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5542                 if (link)
5543                         filter->list = g_slist_delete_link(filter->list, link);
5544                 filter->list = g_slist_append(filter->list, (gpointer)node);
5545                 break;
5546         }
5547         return PMINFO_R_OK;
5548 }
5549
5550 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
5551 {
5552         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5553         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5554         char *syslocale = NULL;
5555         char *locale = NULL;
5556         char *condition = NULL;
5557         char *error_message = NULL;
5558         char query[MAX_QUERY_LEN] = {'\0'};
5559         char where[MAX_QUERY_LEN] = {'\0'};
5560         GSList *list;
5561         int ret = 0;
5562
5563         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5564         /*Get current locale*/
5565         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5566         if (syslocale == NULL) {
5567                 _LOGE("current locale is NULL\n");
5568                 return PMINFO_R_ERROR;
5569         }
5570         locale = __convert_system_locale_to_manifest_locale(syslocale);
5571         if (locale == NULL) {
5572                 _LOGE("manifest locale is NULL\n");
5573                 free(syslocale);
5574                 return PMINFO_R_ERROR;
5575         }
5576
5577         ret = __open_manifest_db();
5578         if (ret == -1) {
5579                 _LOGE("Fail to open manifest DB\n");
5580                 ret = PMINFO_R_ERROR;
5581                 goto err;
5582         }
5583
5584         /*Start constructing query*/
5585         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
5586
5587         /*Get where clause*/
5588         for (list = filter->list; list; list = g_slist_next(list)) {
5589                 __get_filter_condition(list->data, &condition);
5590                 if (condition) {
5591                         strncat(where, condition, sizeof(where) - strlen(where) -1);
5592                         where[sizeof(where) - 1] = '\0';
5593                         free(condition);
5594                         condition = NULL;
5595                 }
5596                 if (g_slist_next(list)) {
5597                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
5598                         where[sizeof(where) - 1] = '\0';
5599                 }
5600         }
5601         _LOGE("where = %s\n", where);
5602         if (strlen(where) > 0) {
5603                 strncat(query, where, sizeof(query) - strlen(query) - 1);
5604                 query[sizeof(query) - 1] = '\0';
5605         }
5606         _LOGE("query = %s\n", query);
5607
5608         /*Execute Query*/
5609         if (SQLITE_OK !=
5610             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
5611                 _LOGE("Don't execute query = %s error message = %s\n", query,
5612                        error_message);
5613                 sqlite3_free(error_message);
5614                 sqlite3_close(manifest_db);
5615                 ret = PMINFO_R_ERROR;
5616                 *count = 0;
5617                 goto err;
5618         }
5619         ret = PMINFO_R_OK;
5620 err:
5621         if (locale) {
5622                 free(locale);
5623                 locale = NULL;
5624         }
5625         if (syslocale) {
5626                 free(syslocale);
5627                 syslocale = NULL;
5628         }
5629         sqlite3_close(manifest_db);
5630         return ret;
5631 }
5632
5633 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
5634                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
5635 {
5636         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5637         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
5638         char *syslocale = NULL;
5639         char *locale = NULL;
5640         char *condition = NULL;
5641         char *error_message = NULL;
5642         char query[MAX_QUERY_LEN] = {'\0'};
5643         char where[MAX_QUERY_LEN] = {'\0'};
5644         GSList *list;
5645         int ret = 0;
5646         uiapplication_x *ptr1 = NULL;
5647         serviceapplication_x *ptr2 = NULL;
5648         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5649         /*Get current locale*/
5650         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5651         if (syslocale == NULL) {
5652                 _LOGE("current locale is NULL\n");
5653                 return PMINFO_R_ERROR;
5654         }
5655         locale = __convert_system_locale_to_manifest_locale(syslocale);
5656         if (locale == NULL) {
5657                 _LOGE("manifest locale is NULL\n");
5658                 free(syslocale);
5659                 return PMINFO_R_ERROR;
5660         }
5661
5662         ret = __open_manifest_db();
5663         if (ret == -1) {
5664                 _LOGE("Fail to open manifest DB\n");
5665                 ret = PMINFO_R_ERROR;
5666                 goto err;
5667         }
5668         /*Start constructing query*/
5669         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_APP, locale);
5670         /*Get where clause*/
5671         for (list = filter->list; list; list = g_slist_next(list)) {
5672                 __get_filter_condition(list->data, &condition);
5673                 if (condition) {
5674                         strncat(where, condition, sizeof(where) - strlen(where) -1);
5675                         where[sizeof(where) - 1] = '\0';
5676                         free(condition);
5677                         condition = NULL;
5678                 }
5679                 if (g_slist_next(list)) {
5680                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
5681                         where[sizeof(where) - 1] = '\0';
5682                 }
5683         }
5684         _LOGE("where = %s\n", where);
5685         if (strlen(where) > 0) {
5686                 strncat(query, where, sizeof(query) - strlen(query) - 1);
5687                 query[sizeof(query) - 1] = '\0';
5688         }
5689         _LOGE("query = %s\n", query);
5690         /*To get filtered list*/
5691         pkgmgr_pkginfo_x *info = NULL;
5692         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5693         if (info == NULL) {
5694                 _LOGE("Out of Memory!!!\n");
5695                 ret = PMINFO_R_ERROR;
5696                 goto err;
5697         }
5698         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5699         if (info->manifest_info == NULL) {
5700                 _LOGE("Out of Memory!!!\n");
5701                 ret = PMINFO_R_ERROR;
5702                 goto err;
5703         }
5704         /*To get detail app info for each member of filtered list*/
5705         pkgmgr_pkginfo_x *filtinfo = NULL;
5706         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5707         if (filtinfo == NULL) {
5708                 _LOGE("Out of Memory!!!\n");
5709                 ret = PMINFO_R_ERROR;
5710                 goto err;
5711         }
5712         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5713         if (filtinfo->manifest_info == NULL) {
5714                 _LOGE("Out of Memory!!!\n");
5715                 ret = PMINFO_R_ERROR;
5716                 goto err;
5717         }
5718         pkgmgr_appinfo_x *appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5719         if (appinfo == NULL) {
5720                 _LOGE("Out of Memory!!!\n");
5721                 ret = PMINFO_R_ERROR;
5722                 goto err;
5723         }
5724         if (SQLITE_OK !=
5725             sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message)) {
5726                 _LOGE("Don't execute query = %s error message = %s\n", query,
5727                        error_message);
5728                 sqlite3_free(error_message);
5729                 sqlite3_close(manifest_db);
5730                 ret = PMINFO_R_ERROR;
5731                 goto err;
5732         }
5733         memset(query, '\0', MAX_QUERY_LEN);
5734         if (info->manifest_info->uiapplication) {
5735                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5736                 info->manifest_info->uiapplication = ptr1;
5737         }
5738         if (info->manifest_info->serviceapplication) {
5739                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5740                 info->manifest_info->serviceapplication = ptr2;
5741         }
5742         /*Filtered UI Apps*/
5743         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5744         {
5745                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5746                                                         ptr1->appid, "uiapp");
5747                 if (SQLITE_OK !=
5748                 sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)filtinfo, &error_message)) {
5749                         _LOGE("Don't execute query = %s error message = %s\n", query,
5750                                error_message);
5751                         sqlite3_free(error_message);
5752                         sqlite3_close(manifest_db);
5753                         ret = PMINFO_R_ERROR;
5754                         goto err;
5755                 }
5756         }
5757         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5758         {
5759                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5760                                                         ptr2->appid, "svcapp");
5761                 if (SQLITE_OK !=
5762                 sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)filtinfo, &error_message)) {
5763                         _LOGE("Don't execute query = %s error message = %s\n", query,
5764                                error_message);
5765                         sqlite3_free(error_message);
5766                         sqlite3_close(manifest_db);
5767                         ret = PMINFO_R_ERROR;
5768                         goto err;
5769                 }
5770         }
5771         if (filtinfo->manifest_info->uiapplication) {
5772                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
5773                 filtinfo->manifest_info->uiapplication = ptr1;
5774         }
5775         /*If the callback func return < 0 we break and no more call back is called*/
5776         while(ptr1 != NULL)
5777         {
5778                 appinfo->locale = strdup(locale);
5779                 appinfo->uiapp_info = ptr1;
5780                 appinfo->app_component = PMINFO_UI_APP;
5781                 ret = app_cb((void *)appinfo, user_data);
5782                 if (ret < 0)
5783                         break;
5784                 ptr1 = ptr1->next;
5785         }
5786         /*Filtered Service Apps*/
5787         if (filtinfo->manifest_info->serviceapplication) {
5788                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
5789                 filtinfo->manifest_info->serviceapplication = ptr2;
5790         }
5791         /*If the callback func return < 0 we break and no more call back is called*/
5792         while(ptr2 != NULL)
5793         {
5794                 appinfo->locale = strdup(locale);
5795                 appinfo->svcapp_info = ptr2;
5796                 appinfo->app_component = PMINFO_SVC_APP;
5797                 ret = app_cb((void *)appinfo, user_data);
5798                 if (ret < 0)
5799                         break;
5800                 ptr2 = ptr2->next;
5801         }
5802         ret = PMINFO_R_OK;
5803 err:
5804         if (locale) {
5805                 free(locale);
5806                 locale = NULL;
5807         }
5808         if (syslocale) {
5809                 free(syslocale);
5810                 syslocale = NULL;
5811         }
5812         sqlite3_close(manifest_db);
5813         if (appinfo) {
5814                 free(appinfo);
5815                 appinfo = NULL;
5816         }
5817         __cleanup_pkginfo(info);
5818         __cleanup_pkginfo(filtinfo);
5819         return ret;
5820 }
5821
5822 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
5823 {
5824         return (pkgmgrinfo_pkginfo_filter_create(handle));
5825 }
5826
5827 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
5828 {
5829         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
5830 }
5831
5832 API int pkgmgrinfo_appinfo_metadata_filter_add(pkgmgrinfo_appinfo_metadata_filter_h handle,
5833                 const char *key, const char *value)
5834 {
5835         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
5836         retvm_if(key == NULL, PMINFO_R_EINVAL, "metadata key supplied is NULL\n");
5837         /*value can be NULL. In that case all apps with specified key should be displayed*/
5838         int ret = 0;
5839         char *k = NULL;
5840         char *v = NULL;
5841         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5842         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5843         retvm_if(node == NULL, PMINFO_R_ERROR, "Out of Memory!!!\n");
5844         k = strdup(key);
5845         tryvm_if(k == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5846         node->key = k;
5847         if (value) {
5848                 v = strdup(value);
5849                 tryvm_if(v == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5850         }
5851         node->value = v;
5852         /*If API is called multiple times, we should OR all conditions.*/
5853         filter->list = g_slist_append(filter->list, (gpointer)node);
5854         /*All memory will be freed in destroy API*/
5855         return PMINFO_R_OK;
5856 catch:
5857         if (node) {
5858                 if (node->key) {
5859                         free(node->key);
5860                         node->key = NULL;
5861                 }
5862                 if (node->value) {
5863                         free(node->value);
5864                         node->value = NULL;
5865                 }
5866                 free(node);
5867                 node = NULL;
5868         }
5869         return ret;
5870 }
5871
5872 API int pkgmgrinfo_appinfo_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
5873                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
5874 {
5875         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
5876         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Callback function supplied is NULL\n");
5877         char *syslocale = NULL;
5878         char *locale = NULL;
5879         char *condition = NULL;
5880         char *error_message = NULL;
5881         char query[MAX_QUERY_LEN] = {'\0'};
5882         char where[MAX_QUERY_LEN] = {'\0'};
5883         GSList *list;
5884         int ret = 0;
5885         pkgmgr_pkginfo_x *info = NULL;
5886         pkgmgr_pkginfo_x *filtinfo = NULL;
5887         pkgmgr_appinfo_x *appinfo = NULL;
5888         uiapplication_x *ptr1 = NULL;
5889         serviceapplication_x *ptr2 = NULL;
5890         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5891
5892         /*Get current locale*/
5893         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5894         retvm_if(syslocale == NULL, PMINFO_R_ERROR, "current locale is NULL\n");
5895         locale = __convert_system_locale_to_manifest_locale(syslocale);
5896         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL\n");
5897
5898         ret = __open_manifest_db();
5899         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Fail to open manifest DB\n");
5900
5901         /*Start constructing query*/
5902         memset(where, '\0', MAX_QUERY_LEN);
5903         memset(query, '\0', MAX_QUERY_LEN);
5904         snprintf(query, MAX_QUERY_LEN - 1, METADATA_FILTER_QUERY_SELECT_CLAUSE);
5905         /*Get where clause*/
5906         for (list = filter->list; list; list = g_slist_next(list)) {
5907                 __get_metadata_filter_condition(list->data, &condition);
5908                 if (condition) {
5909                         strncat(where, condition, sizeof(where) - strlen(where) -1);
5910                         free(condition);
5911                         condition = NULL;
5912                 }
5913                 if (g_slist_next(list)) {
5914                         strncat(where, METADATA_FILTER_QUERY_UNION_CLAUSE, sizeof(where) - strlen(where) - 1);
5915                 }
5916         }
5917         _LOGE("where = %s (%d)\n", where, strlen(where));
5918         if (strlen(where) > 0) {
5919                 strncat(query, where, sizeof(query) - strlen(query) - 1);
5920         }
5921         _LOGE("query = %s (%d)\n", query, strlen(query));
5922         /*To get filtered list*/
5923         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5924         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5925
5926         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5927         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5928
5929         /*To get detail app info for each member of filtered list*/
5930         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5931         tryvm_if(filtinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5932
5933         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5934         tryvm_if(filtinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5935
5936         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5937         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
5938
5939         ret = sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message);
5940         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
5941         memset(query, '\0', MAX_QUERY_LEN);
5942
5943         if (info->manifest_info->uiapplication) {
5944                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5945                 info->manifest_info->uiapplication = ptr1;
5946         }
5947         if (info->manifest_info->serviceapplication) {
5948                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5949                 info->manifest_info->serviceapplication = ptr2;
5950         }
5951
5952         /*UI Apps*/
5953         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5954         {
5955                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5956                                                         ptr1->appid, "uiapp");
5957                 ret = sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)filtinfo, &error_message);
5958                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
5959                 memset(query, '\0', MAX_QUERY_LEN);
5960         }
5961         /*Service Apps*/
5962         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5963         {
5964                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5965                                                         ptr2->appid, "svcapp");
5966                 ret = sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)filtinfo, &error_message);
5967                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
5968                 memset(query, '\0', MAX_QUERY_LEN);
5969         }
5970         /*Filtered UI Apps*/
5971         if (filtinfo->manifest_info->uiapplication) {
5972                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
5973                 filtinfo->manifest_info->uiapplication = ptr1;
5974         }
5975         /*If the callback func return < 0 we break and no more call back is called*/
5976         while(ptr1 != NULL)
5977         {
5978                 appinfo->locale = strdup(locale);
5979                 appinfo->uiapp_info = ptr1;
5980                 appinfo->app_component = PMINFO_UI_APP;
5981                 ret = app_cb((void *)appinfo, user_data);
5982                 if (ret < 0)
5983                         break;
5984                 ptr1 = ptr1->next;
5985         }
5986         /*Filtered Service Apps*/
5987         if (filtinfo->manifest_info->serviceapplication) {
5988                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
5989                 filtinfo->manifest_info->serviceapplication = ptr2;
5990         }
5991         /*If the callback func return < 0 we break and no more call back is called*/
5992         while(ptr2 != NULL)
5993         {
5994                 appinfo->locale = strdup(locale);
5995                 appinfo->svcapp_info = ptr2;
5996                 appinfo->app_component = PMINFO_SVC_APP;
5997                 ret = app_cb((void *)appinfo, user_data);
5998                 if (ret < 0)
5999                         break;
6000                 ptr2 = ptr2->next;
6001         }
6002         ret = PMINFO_R_OK;
6003 catch:
6004         if (locale) {
6005                 free(locale);
6006                 locale = NULL;
6007         }
6008         if (syslocale) {
6009                 free(syslocale);
6010                 syslocale = NULL;
6011         }
6012         sqlite3_free(error_message);
6013         sqlite3_close(manifest_db);
6014         if (appinfo) {
6015                 free(appinfo);
6016                 appinfo = NULL;
6017         }
6018         __cleanup_pkginfo(info);
6019         __cleanup_pkginfo(filtinfo);
6020         return ret;
6021 }
6022
6023 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
6024 {
6025         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6026         pkgmgr_certinfo_x *certinfo = NULL;
6027         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
6028         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
6029         *handle = (void *)certinfo;
6030         return PMINFO_R_OK;
6031 }
6032
6033 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle)
6034 {
6035         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
6036         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
6037         pkgmgr_certinfo_x *certinfo = NULL;
6038         char *error_message = NULL;
6039         int ret = PMINFO_R_OK;
6040         char query[MAX_QUERY_LEN] = {'\0'};
6041         int exist = 0;
6042         int i = 0;
6043
6044         /*Open db.*/
6045         ret = db_util_open_with_options(CERT_DB, &cert_db,
6046                                         SQLITE_OPEN_READONLY, NULL);
6047         if (ret != SQLITE_OK) {
6048                 _LOGE("connect db [%s] failed!\n", CERT_DB);
6049                 return PMINFO_R_ERROR;
6050         }
6051         /*validate pkgid*/
6052         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
6053         if (SQLITE_OK !=
6054             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6055                 _LOGE("Don't execute query = %s error message = %s\n", query,
6056                        error_message);
6057                 sqlite3_free(error_message);
6058                 ret = PMINFO_R_ERROR;
6059                 goto err;
6060         }
6061         if (exist == 0) {
6062                 _LOGE("Package not found in DB\n");
6063                 ret = PMINFO_R_ERROR;
6064                 goto err;
6065         }
6066         certinfo = (pkgmgr_certinfo_x *)handle;
6067         /*populate certinfo from DB*/
6068         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
6069         ret = __exec_certinfo_query(query, (void *)certinfo);
6070         if (ret == -1) {
6071                 _LOGE("Package Cert Info DB Information retrieval failed\n");
6072                 ret = PMINFO_R_ERROR;
6073                 goto err;
6074         }
6075         for (i = 0; i < MAX_CERT_TYPE; i++) {
6076                 memset(query, '\0', MAX_QUERY_LEN);
6077                 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
6078                 ret = __exec_certinfo_query(query, (void *)certinfo);
6079                 if (ret == -1) {
6080                         _LOGE("Cert Info DB Information retrieval failed\n");
6081                         ret = PMINFO_R_ERROR;
6082                         goto err;
6083                 }
6084                 if (certinfo->cert_value) {
6085                         (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
6086                         free(certinfo->cert_value);
6087                         certinfo->cert_value = NULL;
6088                 }
6089         }
6090 err:
6091         sqlite3_close(cert_db);
6092         return ret;
6093 }
6094
6095 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
6096 {
6097         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6098         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6099         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
6100         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
6101         pkgmgr_certinfo_x *certinfo = NULL;
6102         certinfo = (pkgmgr_certinfo_x *)handle;
6103         if ((certinfo->cert_info)[cert_type])
6104                 *cert_value = (certinfo->cert_info)[cert_type];
6105         else
6106                 *cert_value = NULL;
6107         return PMINFO_R_OK;
6108 }
6109
6110 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
6111 {
6112         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6113         int i = 0;
6114         pkgmgr_certinfo_x *certinfo = NULL;
6115         certinfo = (pkgmgr_certinfo_x *)handle;
6116         if (certinfo->pkgid) {
6117                 free(certinfo->pkgid);
6118                 certinfo->pkgid = NULL;
6119         }
6120         for (i = 0; i < MAX_CERT_TYPE; i++) {
6121                 if ((certinfo->cert_info)[i]) {
6122                         free((certinfo->cert_info)[i]);
6123                         (certinfo->cert_info)[i] = NULL;
6124                 }
6125         }
6126         free(certinfo);
6127         certinfo = NULL;
6128         return PMINFO_R_OK;
6129 }
6130
6131 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
6132 {
6133         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6134         pkgmgr_instcertinfo_x *certinfo = NULL;
6135         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
6136         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
6137         *handle = (void *)certinfo;
6138         return PMINFO_R_OK;
6139 }
6140
6141 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
6142 {
6143         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6144         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6145         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
6146         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
6147         pkgmgr_instcertinfo_x *certinfo = NULL;
6148         certinfo = (pkgmgr_instcertinfo_x *)handle;
6149         (certinfo->cert_info)[cert_type] = strdup(cert_value);
6150         return PMINFO_R_OK;
6151 }
6152
6153 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle)
6154 {
6155         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
6156         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
6157         char *error_message = NULL;
6158         char query[MAX_QUERY_LEN] = {'\0'};
6159         char *vquery = NULL;
6160         int len = 0;
6161         int i = 0;
6162         int j = 0;
6163         int c = 0;
6164         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
6165         int newid = 0;
6166         int is_new = 0;
6167         int exist = -1;
6168         int ret = -1;
6169         int maxid = 0;
6170         int flag = 0;
6171         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
6172         pkgmgr_certindexinfo_x *indexinfo = NULL;
6173         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
6174         if (indexinfo == NULL) {
6175                 _LOGE("Out of Memory!!!");
6176                 return PMINFO_R_ERROR;
6177         }
6178         info->pkgid = strdup(pkgid);
6179
6180         /*Open db.*/
6181         ret = db_util_open_with_options(CERT_DB, &cert_db,
6182                                         SQLITE_OPEN_READWRITE, NULL);
6183         if (ret != SQLITE_OK) {
6184                 _LOGE("connect db [%s] failed!\n", CERT_DB);
6185                 ret = PMINFO_R_ERROR;
6186                 goto err;
6187         }
6188         /*Begin Transaction*/
6189         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
6190         if (ret != SQLITE_OK) {
6191                 _LOGE("Failed to begin transaction\n");
6192                 ret = PMINFO_R_ERROR;
6193                 goto err;
6194         }
6195         _LOGE("Transaction Begin\n");
6196         /*Check if request is to insert/update*/
6197         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
6198         if (SQLITE_OK !=
6199             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6200                 _LOGE("Don't execute query = %s error message = %s\n", query,
6201                        error_message);
6202                 sqlite3_free(error_message);
6203                 ret = PMINFO_R_ERROR;
6204                 goto err;
6205         }
6206         if (exist) {
6207                 /*Update request.
6208                 We cant just issue update query directly. We need to manage index table also.
6209                 Hence it is better to delete and insert again in case of update*/
6210                 ret = __delete_certinfo(pkgid);
6211                 if (ret < 0)
6212                         _LOGE("Certificate Deletion Failed\n");
6213         }
6214         for (i = 0; i < MAX_CERT_TYPE; i++) {
6215                 if ((info->cert_info)[i]) {
6216                         for (j = 0; j < i; j++) {
6217                                 if ( (info->cert_info)[j]) {
6218                                         if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
6219                                                 (info->cert_id)[i] = (info->cert_id)[j];
6220                                                 (info->is_new)[i] = 0;
6221                                                 (info->ref_count)[i] = (info->ref_count)[j];
6222                                                 break;
6223                                         }
6224                                 }
6225                         }
6226                         if (j < i)
6227                                 continue;
6228                         memset(query, '\0', MAX_QUERY_LEN);
6229                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info " \
6230                                 "where cert_info='%s'",(info->cert_info)[i]);
6231                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
6232                         if (ret == -1) {
6233                                 _LOGE("Cert Info DB Information retrieval failed\n");
6234                                 ret = PMINFO_R_ERROR;
6235                                 goto err;
6236                         }
6237                         if (indexinfo->cert_id == 0) {
6238                                 /*New certificate. Get newid*/
6239                                 memset(query, '\0', MAX_QUERY_LEN);
6240                                 snprintf(query, MAX_QUERY_LEN, "select MAX(cert_id) from package_cert_index_info ");
6241                                 if (SQLITE_OK !=
6242                                     sqlite3_exec(cert_db, query, __maxid_cb, (void *)&newid, &error_message)) {
6243                                         _LOGE("Don't execute query = %s error message = %s\n", query,
6244                                                error_message);
6245                                         sqlite3_free(error_message);
6246                                         ret = PMINFO_R_ERROR;
6247                                         goto err;
6248                                 }
6249                                 newid = newid + 1;
6250                                 if (flag == 0) {
6251                                         maxid = newid;
6252                                         flag = 1;
6253                                 }
6254                                 indexinfo->cert_id = maxid;
6255                                 indexinfo->cert_ref_count = 1;
6256                                 is_new = 1;
6257                                 maxid = maxid + 1;
6258                         }
6259                         (info->cert_id)[i] = indexinfo->cert_id;
6260                         (info->is_new)[i] = is_new;
6261                         (info->ref_count)[i] = indexinfo->cert_ref_count;
6262                         _LOGE("Id:Count = %d %d\n", indexinfo->cert_id, indexinfo->cert_ref_count);
6263                         indexinfo->cert_id = 0;
6264                         indexinfo->cert_ref_count = 0;
6265                         is_new = 0;
6266                 }
6267         }
6268         len = MAX_QUERY_LEN;
6269         for (i = 0; i < MAX_CERT_TYPE; i++) {
6270                 if ((info->cert_info)[i])
6271                         len+= strlen((info->cert_info)[i]);
6272         }
6273         vquery = (char *)calloc(1, len);
6274         /*insert*/
6275         snprintf(vquery, len,
6276                  "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
6277                 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
6278                 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
6279                  info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
6280                 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
6281                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
6282                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
6283                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
6284         if (SQLITE_OK !=
6285             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
6286                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
6287                        error_message);
6288                 sqlite3_free(error_message);
6289                 ret = PMINFO_R_ERROR;
6290                 goto err;
6291         }
6292         /*Update index table info*/
6293         /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
6294         for (i = 0; i < MAX_CERT_TYPE; i++) {
6295                 if ((info->cert_info)[i]) {
6296                         memset(vquery, '\0', len);
6297                         if ((info->is_new)[i]) {
6298                                 snprintf(vquery, len, "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
6299                                 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
6300                                 unique_id[c++] = (info->cert_id)[i];
6301                         } else {
6302                                 /*Update*/
6303                                 for (j = 0; j < MAX_CERT_TYPE; j++) {
6304                                         if ((info->cert_id)[i] == unique_id[j]) {
6305                                                 /*Ref count has already been increased. Just continue*/
6306                                                 break;
6307                                         }
6308                                 }
6309                                 if (j == MAX_CERT_TYPE)
6310                                         unique_id[c++] = (info->cert_id)[i];
6311                                 else
6312                                         continue;
6313                                 snprintf(vquery, len, "update package_cert_index_info set cert_ref_count=%d " \
6314                                 "where cert_id=%d",  (info->ref_count)[i] + 1, (info->cert_id)[i]);
6315                         }
6316                         if (SQLITE_OK !=
6317                             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
6318                                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
6319                                        error_message);
6320                                 sqlite3_free(error_message);
6321                                 ret = PMINFO_R_ERROR;
6322                                 goto err;
6323                         }
6324                 }
6325         }
6326         /*Commit transaction*/
6327         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
6328         if (ret != SQLITE_OK) {
6329                 _LOGE("Failed to commit transaction, Rollback now\n");
6330                 sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
6331                 ret = PMINFO_R_ERROR;
6332                 goto err;
6333         }
6334         _LOGE("Transaction Commit and End\n");
6335         ret =  PMINFO_R_OK;
6336 err:
6337         sqlite3_close(cert_db);
6338         if (vquery) {
6339                 free(vquery);
6340                 vquery = NULL;
6341         }
6342         if (indexinfo) {
6343                 free(indexinfo);
6344                 indexinfo = NULL;
6345         }
6346         return ret;
6347 }
6348
6349 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
6350 {
6351         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6352         int i = 0;
6353         pkgmgr_instcertinfo_x *certinfo = NULL;
6354         certinfo = (pkgmgr_instcertinfo_x *)handle;
6355         if (certinfo->pkgid) {
6356                 free(certinfo->pkgid);
6357                 certinfo->pkgid = NULL;
6358         }
6359         for (i = 0; i < MAX_CERT_TYPE; i++) {
6360                 if ((certinfo->cert_info)[i]) {
6361                         free((certinfo->cert_info)[i]);
6362                         (certinfo->cert_info)[i] = NULL;
6363                 }
6364         }
6365         free(certinfo);
6366         certinfo = NULL;
6367         return PMINFO_R_OK;
6368 }
6369
6370 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
6371 {
6372         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6373         int ret = -1;
6374         /*Open db.*/
6375         ret = db_util_open_with_options(CERT_DB, &cert_db,
6376                                         SQLITE_OPEN_READWRITE, NULL);
6377         if (ret != SQLITE_OK) {
6378                 _LOGE("connect db [%s] failed!\n", CERT_DB);
6379                 ret = PMINFO_R_ERROR;
6380                 goto err;
6381         }
6382         /*Begin Transaction*/
6383         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
6384         if (ret != SQLITE_OK) {
6385                 _LOGE("Failed to begin transaction\n");
6386                 ret = PMINFO_R_ERROR;
6387                 goto err;
6388         }
6389         _LOGE("Transaction Begin\n");
6390         ret = __delete_certinfo(pkgid);
6391         if (ret < 0) {
6392                 _LOGE("Certificate Deletion Failed\n");
6393         } else {
6394                 _LOGE("Certificate Deletion Success\n");
6395         }
6396         /*Commit transaction*/
6397         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
6398         if (ret != SQLITE_OK) {
6399                 _LOGE("Failed to commit transaction, Rollback now\n");
6400                 sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
6401                 ret = PMINFO_R_ERROR;
6402                 goto err;
6403         }
6404         _LOGE("Transaction Commit and End\n");
6405         ret = PMINFO_R_OK;
6406 err:
6407         sqlite3_close(cert_db);
6408         return ret;
6409 }
6410
6411 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
6412 {
6413         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
6414         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6415
6416         char *manifest = NULL;
6417         manifest_x *mfx = NULL;
6418
6419         manifest = pkgmgr_parser_get_manifest_file(pkgid);
6420         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
6421
6422         mfx = pkgmgr_parser_process_manifest_xml(manifest);
6423         if (manifest) {
6424                 free(manifest);
6425                 manifest = NULL;
6426         }
6427         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
6428
6429         *handle = (void *)mfx;
6430
6431         return PMINFO_R_OK;
6432 }
6433
6434 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
6435 {
6436         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
6437         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6438
6439         int len = strlen(type);
6440         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
6441
6442         manifest_x *mfx = (manifest_x *)handle;
6443
6444         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
6445         return PMINFO_R_OK;
6446 }
6447
6448 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
6449 {
6450         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
6451         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6452
6453         int len = strlen(version);
6454         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
6455
6456         manifest_x *mfx = (manifest_x *)handle;
6457
6458         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
6459         return PMINFO_R_OK;
6460 }
6461
6462 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
6463 {
6464         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6465         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
6466
6467         manifest_x *mfx = (manifest_x *)handle;
6468
6469         if (location == INSTALL_INTERNAL)
6470                 strcpy(mfx->installlocation, "internal-only");
6471         else if (location == INSTALL_EXTERNAL)
6472                 strcpy(mfx->installlocation, "prefer-external");
6473
6474         return PMINFO_R_OK;
6475 }
6476
6477 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
6478 {
6479         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6480         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
6481
6482         manifest_x *mfx = (manifest_x *)handle;
6483
6484         mfx->package_size = strdup(size);
6485
6486         return PMINFO_R_OK;
6487 }
6488
6489 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
6490 {
6491         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6492         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
6493
6494         int len = strlen(label_txt);
6495         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
6496
6497         manifest_x *mfx = (manifest_x *)handle;
6498
6499         label_x *label = calloc(1, sizeof(label_x));
6500         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
6501
6502         LISTADD(mfx->label, label);
6503         if (locale)
6504                 mfx->label->lang = strdup(locale);
6505         else
6506                 mfx->label->lang = strdup(DEFAULT_LOCALE);
6507         mfx->label->text = strdup(label_txt);
6508
6509         return PMINFO_R_OK;
6510 }
6511
6512 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
6513 {
6514         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6515         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
6516
6517         int len = strlen(icon_txt);
6518         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
6519
6520         manifest_x *mfx = (manifest_x *)handle;
6521
6522         icon_x *icon = calloc(1, sizeof(icon_x));
6523         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
6524
6525         LISTADD(mfx->icon, icon);
6526         if (locale)
6527                 mfx->icon->lang = strdup(locale);
6528         else
6529                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
6530         mfx->icon->text = strdup(icon_txt);
6531
6532         return PMINFO_R_OK;
6533 }
6534
6535 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
6536 {
6537         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6538         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
6539
6540         int len = strlen(desc_txt);
6541         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
6542
6543         manifest_x *mfx = (manifest_x *)handle;
6544
6545         description_x *description = calloc(1, sizeof(description_x));
6546         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
6547
6548         LISTADD(mfx->description, description);
6549         if (locale)
6550                 mfx->description->lang = strdup(locale);
6551         else
6552                 mfx->description->lang = strdup(DEFAULT_LOCALE);
6553         mfx->description->text = strdup(desc_txt);
6554
6555         return PMINFO_R_OK;
6556 }
6557
6558 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
6559                 const char *author_email, const char *author_href, const char *locale)
6560 {
6561         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6562         manifest_x *mfx = (manifest_x *)handle;
6563         author_x *author = calloc(1, sizeof(author_x));
6564         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
6565
6566         LISTADD(mfx->author, author);
6567         if (author_name)
6568                 mfx->author->text = strdup(author_name);
6569         if (author_email)
6570                 mfx->author->email = strdup(author_email);
6571         if (author_href)
6572                 mfx->author->href = strdup(author_href);
6573         if (locale)
6574                 mfx->author->lang = strdup(locale);
6575         else
6576                 mfx->author->lang = strdup(DEFAULT_LOCALE);
6577         return PMINFO_R_OK;
6578 }
6579
6580 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
6581 {
6582         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6583         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
6584
6585         manifest_x *mfx = (manifest_x *)handle;
6586
6587         if (removable == 0)
6588                 strcpy(mfx->removable, "false");
6589         else if (removable == 1)
6590                 strcpy(mfx->removable, "true");
6591
6592         return PMINFO_R_OK;
6593 }
6594
6595 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
6596 {
6597         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6598         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
6599
6600         manifest_x *mfx = (manifest_x *)handle;
6601
6602         if (preload == 0)
6603                 strcpy(mfx->preload, "false");
6604         else if (preload == 1)
6605                 strcpy(mfx->preload, "true");
6606
6607         return PMINFO_R_OK;
6608 }
6609
6610 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
6611 {
6612         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6613         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
6614
6615         manifest_x *mfx = (manifest_x *)handle;
6616
6617         if (location == INSTALL_INTERNAL)
6618                 strcpy(mfx->installed_storage, "installed_internal");
6619         else if (location == INSTALL_EXTERNAL)
6620                 strcpy(mfx->installed_storage, "installed_external");
6621
6622         return PMINFO_R_OK;
6623 }
6624
6625 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
6626 {
6627         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6628
6629         int ret = 0;
6630         manifest_x *mfx = NULL;
6631         mfx = (manifest_x *)handle;
6632
6633         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
6634         if (ret == 0) {
6635                 _LOGE("Successfully stored info in DB\n");
6636                 return PMINFO_R_OK;
6637         } else {
6638                 _LOGE("Failed to store info in DB\n");
6639                 return PMINFO_R_ERROR;
6640         }
6641 }
6642
6643 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
6644 {
6645         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
6646
6647         manifest_x *mfx = NULL;
6648         mfx = (manifest_x *)handle;
6649         pkgmgr_parser_free_manifest_xml(mfx);
6650         return PMINFO_R_OK;
6651 }
6652
6653 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
6654 {
6655         /* Should be implemented later */
6656         return 0;
6657 }
6658
6659 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
6660 {
6661         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
6662         int ret = -1;
6663         char query[MAX_QUERY_LEN] = {'\0'};
6664         ret = __open_manifest_db();
6665
6666         if (access(MANIFEST_DB, F_OK) == 0) {
6667                 ret = db_util_open(MANIFEST_DB, &manifest_db,
6668                          DB_UTIL_REGISTER_HOOK_METHOD);
6669                 if (ret != SQLITE_OK) {
6670                         _LOGE("connect db [%s] failed! Manifest DB does not exists!!\n", MANIFEST_DB);
6671                         return PMINFO_R_ERROR;
6672                 }
6673         }
6674
6675         /*Begin transaction*/
6676         ret = sqlite3_exec(manifest_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
6677         if (ret != SQLITE_OK) {
6678                 _LOGE("Failed to begin transaction\n");
6679                 sqlite3_close(manifest_db);
6680                 return PMINFO_R_ERROR;
6681         }
6682         _LOGD("Transaction Begin\n");
6683
6684         memset(query, '\0', MAX_QUERY_LEN);
6685         snprintf(query, MAX_QUERY_LEN,
6686                 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
6687
6688         char *error_message = NULL;
6689         if (SQLITE_OK !=
6690             sqlite3_exec(manifest_db, query, NULL, NULL, &error_message)) {
6691                 _LOGE("Don't execute query = %s error message = %s\n", query,
6692                        error_message);
6693                 sqlite3_free(error_message);
6694                 return PMINFO_R_ERROR;
6695         }
6696         sqlite3_free(error_message);
6697
6698         /*Commit transaction*/
6699         ret = sqlite3_exec(manifest_db, "COMMIT", NULL, NULL, NULL);
6700         if (ret != SQLITE_OK) {
6701                 _LOGE("Failed to commit transaction. Rollback now\n");
6702                 sqlite3_exec(manifest_db, "ROLLBACK", NULL, NULL, NULL);
6703                 sqlite3_close(manifest_db);
6704                 return PMINFO_R_ERROR;
6705         }
6706         _LOGD("Transaction Commit and End\n");
6707         sqlite3_close(manifest_db);
6708
6709         return PMINFO_R_OK;
6710 }
6711
6712
6713 API int pkgmgrinfo_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
6714 {
6715         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6716         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6717         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6718         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6719         int ret = PMINFO_R_OK;
6720         char query[MAX_QUERY_LEN] = {'\0'};
6721         char *error_message = NULL;
6722         pkgmgr_datacontrol_x *data = NULL;
6723
6724         ret = __open_datacontrol_db();
6725         if (ret == -1) {
6726                 _LOGE("Fail to open datacontrol DB\n");
6727                 return PMINFO_R_ERROR;
6728         }
6729
6730         data = (pkgmgr_datacontrol_x *)calloc(1, sizeof(pkgmgr_datacontrol_x));
6731         if (data == NULL) {
6732                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
6733                 sqlite3_close(datacontrol_db);
6734                 return PMINFO_R_ERROR;
6735         }
6736
6737         snprintf(query, MAX_QUERY_LEN, 
6738                 "select appinfo.package_name, datacontrol.access from appinfo, datacontrol where datacontrol.id=appinfo.unique_id and datacontrol.provider_id = '%s' and datacontrol.type='%s' COLLATE NOCASE",
6739                 providerid, type);
6740
6741         if (SQLITE_OK !=
6742                 sqlite3_exec(datacontrol_db, query, __datacontrol_cb, (void *)data, &error_message)) {
6743                 _LOGE("Don't execute query = %s error message = %s\n", query,
6744                            error_message);
6745                 sqlite3_free(error_message);
6746                 sqlite3_close(datacontrol_db);
6747                 return PMINFO_R_ERROR;
6748         }
6749
6750         *appid = (char *)data->appid;
6751         *access = (char *)data->access;
6752         free(data);
6753         sqlite3_close(datacontrol_db);
6754
6755         return PMINFO_R_OK;
6756 }
6757
6758 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
6759 {
6760         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
6761         int ret = -1;
6762         char query[MAX_QUERY_LEN] = {'\0'};
6763         char *error_message = NULL;
6764         ret = __open_manifest_db();
6765
6766         if (access(MANIFEST_DB, F_OK) == 0) {
6767                 ret = db_util_open(MANIFEST_DB, &manifest_db,
6768                          DB_UTIL_REGISTER_HOOK_METHOD);
6769                 if (ret != SQLITE_OK) {
6770                         _LOGE("connect db [%s] failed! Manifest DB does not exists!!\n", MANIFEST_DB);
6771                         return PMINFO_R_ERROR;
6772                 }
6773         }
6774
6775         /*Begin transaction*/
6776         ret = sqlite3_exec(manifest_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
6777         if (ret != SQLITE_OK) {
6778                 _LOGE("Failed to begin transaction\n");
6779                 sqlite3_close(manifest_db);
6780                 return PMINFO_R_ERROR;
6781         }
6782         _LOGD("Transaction Begin\n");
6783
6784         memset(query, '\0', MAX_QUERY_LEN);
6785         snprintf(query, MAX_QUERY_LEN,
6786                 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
6787
6788         if (SQLITE_OK !=
6789             sqlite3_exec(manifest_db, query, NULL, NULL, &error_message)) {
6790                 _LOGE("Don't execute query = %s error message = %s\n", query,
6791                        error_message);
6792                 sqlite3_free(error_message);
6793                 return PMINFO_R_ERROR;
6794         }
6795
6796         /*Commit transaction*/
6797         ret = sqlite3_exec(manifest_db, "COMMIT", NULL, NULL, NULL);
6798         if (ret != SQLITE_OK) {
6799                 _LOGE("Failed to commit transaction. Rollback now\n");
6800                 sqlite3_exec(manifest_db, "ROLLBACK", NULL, NULL, NULL);
6801                 sqlite3_close(manifest_db);
6802                 return PMINFO_R_ERROR;
6803         }
6804         _LOGD("Transaction Commit and End\n");
6805         sqlite3_close(manifest_db);
6806
6807         return PMINFO_R_OK;
6808 }
6809
6810 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
6811 {
6812         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6813         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6814         char *val = NULL;
6815         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6816         val = (char *)info->uiapp_info->guestmode_visibility;
6817         if (val) {
6818                 if (strcasecmp(val, "true") == 0){
6819                         *status = 1;
6820                 }else if (strcasecmp(val, "false") == 0){
6821                         *status = 0;
6822                 }else{
6823                         *status = 1;
6824                 }
6825         }
6826         return PMINFO_R_OK;
6827 }
6828
6829 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
6830 {
6831         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6832         char *val = NULL;
6833         int ret = 0;
6834         char *noti_string = NULL;
6835         int len = 0;
6836         char query[MAX_QUERY_LEN] = {'\0'};
6837         char *errmsg = NULL;
6838         sqlite3 *pkgmgr_parser_db;
6839
6840         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6841         val = (char *)info->uiapp_info->guestmode_visibility;
6842         if (val ) {
6843                 ret =
6844                     db_util_open_with_options(MANIFEST_DB, &pkgmgr_parser_db,
6845                                  SQLITE_OPEN_READWRITE, NULL);
6846
6847                 if (ret != SQLITE_OK) {
6848                         _LOGE("DB Open Failed\n");
6849                         return PMINFO_R_ERROR;
6850                 }
6851
6852                 /*TODO: Write to DB here*/
6853                 if (status == true)
6854                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->uiapp_info->appid);
6855                 else
6856                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->uiapp_info->appid);
6857
6858                 if (SQLITE_OK != sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg)) {
6859                         _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
6860                         free(errmsg);
6861                         sqlite3_close(pkgmgr_parser_db);
6862                         return PMINFO_R_ERROR;
6863                 }else{
6864                         sqlite3_close(pkgmgr_parser_db);
6865                         len = strlen((char *)info->uiapp_info->appid) + 8;
6866                         noti_string = calloc(1, len);
6867                         if (noti_string == NULL){
6868                                 return PMINFO_R_ERROR;
6869                         }
6870                         snprintf(noti_string, len, "update:%s", (char *)info->uiapp_info->appid);
6871                 vconf_set_str(VCONFKEY_AIL_INFO_STATE, noti_string);
6872                         vconf_set_str(VCONFKEY_MENUSCREEN_DESKTOP, noti_string); // duplicate, will be removed
6873                         free(noti_string);
6874                 }
6875         }
6876         return PMINFO_R_OK;
6877 }