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