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