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