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