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