Remove warnings
[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 const 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 const char *getUserPkgParserDBPath(void)
442 {
443         return getUserPkgParserDBPathUID(GLOBAL_USER);
444 }
445
446 API const 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 const char *getUserPkgCertDBPath(void)
471 {
472          return getUserPkgCertDBPathUID(GLOBAL_USER);
473 }
474
475 API const 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(uid)) {
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[i], "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 = NULL;
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
1925         switch (info->app_component) {
1926         case PMINFO_UI_APP:
1927                 icon = calloc(1, sizeof(icon_x));
1928                 LISTADD(info->uiapp_info->icon, icon);
1929                 label = calloc(1, sizeof(label_x));
1930                 LISTADD(info->uiapp_info->label, label);
1931                 category = calloc(1, sizeof(category_x));
1932                 LISTADD(info->uiapp_info->category, category);
1933                 metadata = calloc(1, sizeof(metadata_x));
1934                 LISTADD(info->uiapp_info->metadata, metadata);
1935                 permission = calloc(1, sizeof(permission_x));
1936                 LISTADD(info->uiapp_info->permission, permission);
1937                 image = calloc(1, sizeof(image_x));
1938                 LISTADD(info->uiapp_info->image, image);
1939
1940                 for(i = 0; i < ncols; i++)
1941                 {
1942                         if (strcmp(colname[i], "app_id") == 0) {
1943                                 /*appid being foreign key, is column in every table
1944                                 Hence appid gets strduped every time leading to memory leak.
1945                                 If appid is already set, just continue.*/
1946                                 if (info->uiapp_info->appid)
1947                                         continue;
1948                                 if (coltxt[i])
1949                                         info->uiapp_info->appid = strdup(coltxt[i]);
1950                                 else
1951                                         info->uiapp_info->appid = NULL;
1952                         } else if (strcmp(colname[i], "app_exec") == 0) {
1953                                 if (coltxt[i])
1954                                         info->uiapp_info->exec = strdup(coltxt[i]);
1955                                 else
1956                                         info->uiapp_info->exec = NULL;
1957                         } else if (strcmp(colname[i], "app_nodisplay") == 0) {
1958                                 if (coltxt[i])
1959                                         info->uiapp_info->nodisplay = strdup(coltxt[i]);
1960                                 else
1961                                         info->uiapp_info->nodisplay = NULL;
1962                         } else if (strcmp(colname[i], "app_type") == 0 ) {
1963                                 if (coltxt[i])
1964                                         info->uiapp_info->type = strdup(coltxt[i]);
1965                                 else
1966                                         info->uiapp_info->type = NULL;
1967                         } else if (strcmp(colname[i], "app_icon_section") == 0 ) {
1968                                 if (coltxt[i])
1969                                         info->uiapp_info->icon->section= strdup(coltxt[i]);
1970                                 else
1971                                         info->uiapp_info->icon->section = NULL;
1972                         } else if (strcmp(colname[i], "app_icon") == 0) {
1973                                 if (coltxt[i])
1974                                         info->uiapp_info->icon->text = strdup(coltxt[i]);
1975                                 else
1976                                         info->uiapp_info->icon->text = NULL;
1977                         } else if (strcmp(colname[i], "app_label") == 0 ) {
1978                                 if (coltxt[i])
1979                                         info->uiapp_info->label->text = strdup(coltxt[i]);
1980                                 else
1981                                         info->uiapp_info->label->text = NULL;
1982                         } else if (strcmp(colname[i], "app_multiple") == 0 ) {
1983                                 if (coltxt[i])
1984                                         info->uiapp_info->multiple = strdup(coltxt[i]);
1985                                 else
1986                                         info->uiapp_info->multiple = NULL;
1987                         } else if (strcmp(colname[i], "app_taskmanage") == 0 ) {
1988                                 if (coltxt[i])
1989                                         info->uiapp_info->taskmanage = strdup(coltxt[i]);
1990                                 else
1991                                         info->uiapp_info->taskmanage = NULL;
1992                         } else if (strcmp(colname[i], "app_hwacceleration") == 0 ) {
1993                                 if (coltxt[i])
1994                                         info->uiapp_info->hwacceleration = strdup(coltxt[i]);
1995                                 else
1996                                         info->uiapp_info->hwacceleration = NULL;
1997                         } else if (strcmp(colname[i], "app_screenreader") == 0 ) {
1998                                 if (coltxt[i])
1999                                         info->uiapp_info->screenreader = strdup(coltxt[i]);
2000                                 else
2001                                         info->uiapp_info->screenreader = NULL;
2002                         } else if (strcmp(colname[i], "app_enabled") == 0 ) {
2003                                 if (coltxt[i])
2004                                         info->uiapp_info->enabled= strdup(coltxt[i]);
2005                                 else
2006                                         info->uiapp_info->enabled = NULL;
2007                         } else if (strcmp(colname[i], "app_indicatordisplay") == 0){
2008                                 if (coltxt[i])
2009                                         info->uiapp_info->indicatordisplay = strdup(coltxt[i]);
2010                                 else
2011                                         info->uiapp_info->indicatordisplay = NULL;
2012                         } else if (strcmp(colname[i], "app_portraitimg") == 0){
2013                                 if (coltxt[i])
2014                                         info->uiapp_info->portraitimg = strdup(coltxt[i]);
2015                                 else
2016                                         info->uiapp_info->portraitimg = NULL;
2017                         } else if (strcmp(colname[i], "app_landscapeimg") == 0){
2018                                 if (coltxt[i])
2019                                         info->uiapp_info->landscapeimg = strdup(coltxt[i]);
2020                                 else
2021                                         info->uiapp_info->landscapeimg = NULL;
2022                         } else if (strcmp(colname[i], "app_guestmodevisibility") == 0){
2023                                 if (coltxt[i])
2024                                         info->uiapp_info->guestmode_visibility = strdup(coltxt[i]);
2025                                 else
2026                                         info->uiapp_info->guestmode_visibility = NULL;
2027                         } else if (strcmp(colname[i], "category") == 0 ) {
2028                                 if (coltxt[i])
2029                                         info->uiapp_info->category->name = strdup(coltxt[i]);
2030                                 else
2031                                         info->uiapp_info->category->name = NULL;
2032                         } else if (strcmp(colname[i], "md_key") == 0 ) {
2033                                 if (coltxt[i])
2034                                         info->uiapp_info->metadata->key = strdup(coltxt[i]);
2035                                 else
2036                                         info->uiapp_info->metadata->key = NULL;
2037                         } else if (strcmp(colname[i], "md_value") == 0 ) {
2038                                 if (coltxt[i])
2039                                         info->uiapp_info->metadata->value = strdup(coltxt[i]);
2040                                 else
2041                                         info->uiapp_info->metadata->value = NULL;
2042                         } else if (strcmp(colname[i], "pm_type") == 0 ) {
2043                                 if (coltxt[i])
2044                                         info->uiapp_info->permission->type= strdup(coltxt[i]);
2045                                 else
2046                                         info->uiapp_info->permission->type = NULL;
2047                         } else if (strcmp(colname[i], "pm_value") == 0 ) {
2048                                 if (coltxt[i])
2049                                         info->uiapp_info->permission->value = strdup(coltxt[i]);
2050                                 else
2051                                         info->uiapp_info->permission->value = NULL;
2052                         } else if (strcmp(colname[i], "app_recentimage") == 0 ) {
2053                                 if (coltxt[i])
2054                                         info->uiapp_info->recentimage = strdup(coltxt[i]);
2055                                 else
2056                                         info->uiapp_info->recentimage = NULL;
2057                         } else if (strcmp(colname[i], "app_mainapp") == 0 ) {
2058                                 if (coltxt[i])
2059                                         info->uiapp_info->mainapp = strdup(coltxt[i]);
2060                                 else
2061                                         info->uiapp_info->mainapp = NULL;
2062                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
2063                                 if (coltxt[i]) {
2064                                         info->uiapp_info->icon->lang = strdup(coltxt[i]);
2065                                         info->uiapp_info->label->lang = strdup(coltxt[i]);
2066                                 }
2067                                 else {
2068                                         info->uiapp_info->icon->lang = NULL;
2069                                         info->uiapp_info->label->lang = NULL;
2070                                 }
2071                         } else if (strcmp(colname[i], "app_image") == 0) {
2072                                         if (coltxt[i])
2073                                                 info->uiapp_info->image->text= strdup(coltxt[i]);
2074                                         else
2075                                                 info->uiapp_info->image->text = NULL;
2076                         } else if (strcmp(colname[i], "app_image_section") == 0) {
2077                                         if (coltxt[i])
2078                                                 info->uiapp_info->image->section= strdup(coltxt[i]);
2079                                         else
2080                                                 info->uiapp_info->image->section = NULL;
2081                         } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
2082                                 if (coltxt[i])
2083                                         info->uiapp_info->permission_type = strdup(coltxt[i]);
2084                                 else
2085                                         info->uiapp_info->permission_type = NULL;
2086                         } else if (strcmp(colname[i], "component_type") == 0 ) {
2087                                 if (coltxt[i])
2088                                         info->uiapp_info->component_type = strdup(coltxt[i]);
2089                                 else
2090                                         info->uiapp_info->component_type = NULL;
2091                         } else if (strcmp(colname[i], "app_preload") == 0 ) {
2092                                 if (coltxt[i])
2093                                         info->uiapp_info->preload = strdup(coltxt[i]);
2094                                 else
2095                                         info->uiapp_info->preload = NULL;
2096                         } else if (strcmp(colname[i], "app_submode") == 0 ) {
2097                                 if (coltxt[i])
2098                                         info->uiapp_info->submode = strdup(coltxt[i]);
2099                                 else
2100                                         info->uiapp_info->submode = NULL;
2101                         } else if (strcmp(colname[i], "app_submode_mainid") == 0 ) {
2102                                 if (coltxt[i])
2103                                         info->uiapp_info->submode_mainid = strdup(coltxt[i]);
2104                                 else
2105                                         info->uiapp_info->submode_mainid = NULL;
2106                         } else if (strcmp(colname[i], "app_launch_mode") == 0 ) {
2107                                 if (coltxt[i])
2108                                         info->uiapp_info->launch_mode = strdup(coltxt[i]);
2109                                 else
2110                                         info->uiapp_info->launch_mode = NULL;
2111                         } else if (strcmp(colname[i], "app_control") == 0 ) {
2112                                 __parse_appcontrol(&info->uiapp_info->appcontrol, coltxt[i]);
2113                         } else
2114                                 continue;
2115                 }
2116                 break;
2117         case PMINFO_SVC_APP:
2118                 icon = calloc(1, sizeof(icon_x));
2119                 LISTADD(info->svcapp_info->icon, icon);
2120                 label = calloc(1, sizeof(label_x));
2121                 LISTADD(info->svcapp_info->label, label);
2122                 category = calloc(1, sizeof(category_x));
2123                 LISTADD(info->svcapp_info->category, category);
2124                 metadata = calloc(1, sizeof(metadata_x));
2125                 LISTADD(info->svcapp_info->metadata, metadata);
2126                 permission = calloc(1, sizeof(permission_x));
2127                 LISTADD(info->svcapp_info->permission, permission);
2128                 for(i = 0; i < ncols; i++)
2129                 {
2130                         if (strcmp(colname[i], "app_id") == 0) {
2131                                 /*appid being foreign key, is column in every table
2132                                 Hence appid gets strduped every time leading to memory leak.
2133                                 If appid is already set, just continue.*/
2134                                 if (info->svcapp_info->appid)
2135                                         continue;
2136                                 if (coltxt[i])
2137                                         info->svcapp_info->appid = strdup(coltxt[i]);
2138                                 else
2139                                         info->svcapp_info->appid = NULL;
2140                         } else if (strcmp(colname[i], "app_exec") == 0) {
2141                                 if (coltxt[i])
2142                                         info->svcapp_info->exec = strdup(coltxt[i]);
2143                                 else
2144                                         info->svcapp_info->exec = NULL;
2145                         } else if (strcmp(colname[i], "app_icon") == 0) {
2146                                 if (coltxt[i])
2147                                         info->svcapp_info->icon->text = strdup(coltxt[i]);
2148                                 else
2149                                         info->svcapp_info->icon->text = NULL;
2150                         } else if (strcmp(colname[i], "app_label") == 0 ) {
2151                                 if (coltxt[i])
2152                                         info->svcapp_info->label->text = strdup(coltxt[i]);
2153                                 else
2154                                         info->svcapp_info->label->text = NULL;
2155                         } else if (strcmp(colname[i], "app_type") == 0 ) {
2156                                 if (coltxt[i])
2157                                         info->svcapp_info->type = strdup(coltxt[i]);
2158                                 else
2159                                         info->svcapp_info->type = NULL;
2160                         } else if (strcmp(colname[i], "app_onboot") == 0 ) {
2161                                 if (coltxt[i])
2162                                         info->svcapp_info->onboot = strdup(coltxt[i]);
2163                                 else
2164                                         info->svcapp_info->onboot = NULL;
2165                         } else if (strcmp(colname[i], "app_autorestart") == 0 ) {
2166                                 if (coltxt[i])
2167                                         info->svcapp_info->autorestart = strdup(coltxt[i]);
2168                                 else
2169                                         info->svcapp_info->autorestart = NULL;
2170                         } else if (strcmp(colname[i], "app_enabled") == 0 ) {
2171                                 if (coltxt[i])
2172                                         info->svcapp_info->enabled= strdup(coltxt[i]);
2173                                 else
2174                                         info->svcapp_info->enabled = NULL;
2175                         } else if (strcmp(colname[i], "category") == 0 ) {
2176                                 if (coltxt[i])
2177                                         info->svcapp_info->category->name = strdup(coltxt[i]);
2178                                 else
2179                                         info->svcapp_info->category->name = NULL;
2180                         } else if (strcmp(colname[i], "md_key") == 0 ) {
2181                                 if (coltxt[i])
2182                                         info->svcapp_info->metadata->key = strdup(coltxt[i]);
2183                                 else
2184                                         info->svcapp_info->metadata->key = NULL;
2185                         } else if (strcmp(colname[i], "md_value") == 0 ) {
2186                                 if (coltxt[i])
2187                                         info->svcapp_info->metadata->value = strdup(coltxt[i]);
2188                                 else
2189                                         info->svcapp_info->metadata->value = NULL;
2190                         } else if (strcmp(colname[i], "pm_type") == 0 ) {
2191                                 if (coltxt[i])
2192                                         info->svcapp_info->permission->type= strdup(coltxt[i]);
2193                                 else
2194                                         info->svcapp_info->permission->type = NULL;
2195                         } else if (strcmp(colname[i], "pm_value") == 0 ) {
2196                                 if (coltxt[i])
2197                                         info->svcapp_info->permission->value = strdup(coltxt[i]);
2198                                 else
2199                                         info->svcapp_info->permission->value = NULL;
2200                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
2201                                 if (coltxt[i]) {
2202                                         info->svcapp_info->icon->lang = strdup(coltxt[i]);
2203                                         info->svcapp_info->label->lang = strdup(coltxt[i]);
2204                                 }
2205                                 else {
2206                                         info->svcapp_info->icon->lang = NULL;
2207                                         info->svcapp_info->label->lang = NULL;
2208                                 }
2209                         } else if (strcmp(colname[i], "app_permissiontype") == 0 ) {
2210                                 if (coltxt[i])
2211                                         info->svcapp_info->permission_type = strdup(coltxt[i]);
2212                                 else
2213                                         info->svcapp_info->permission_type = NULL;
2214                         } else if (strcmp(colname[i], "app_control") == 0 ) {
2215                                 __parse_appcontrol(&info->svcapp_info->appcontrol, coltxt[i]);
2216                         } else
2217                                 continue;
2218                 }
2219                 break;
2220         default:
2221                 break;
2222         }
2223
2224         return 0;
2225 }
2226
2227
2228 static int __appcomponent_cb(void *data, int ncols, char **coltxt, char **colname)
2229 {
2230         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
2231         int i = 0;
2232         for(i = 0; i < ncols; i++)
2233         {
2234                 if (strcmp(colname[i], "app_component") == 0) {
2235                         info->app_component = __appcomponent_convert(coltxt[i]);
2236                 } else if (strcmp(colname[i], "package") == 0) {
2237                         info->package = strdup(coltxt[i]);
2238                 }
2239         }
2240
2241         return 0;
2242 }
2243
2244 static int __datacontrol_cb(void *data, int ncols, char **coltxt, char **colname)
2245 {
2246         pkgmgr_datacontrol_x *info = (pkgmgr_datacontrol_x *)data;
2247         int i = 0;
2248         for(i = 0; i < ncols; i++)
2249         {
2250                 if (strcmp(colname[i], "PACKAGE_NAME") == 0) {
2251                         if (coltxt[i])
2252                                 info->appid = strdup(coltxt[i]);
2253                         else
2254                                 info->appid = NULL;
2255                 } else if (strcmp(colname[i], "ACCESS") == 0 ){
2256                         if (coltxt[i])
2257                                 info->access = strdup(coltxt[i]);
2258                         else
2259                                 info->access = NULL;
2260                 } else
2261                         continue;
2262         }
2263         return 0;
2264 }
2265
2266 static int __cert_cb(void *data, int ncols, char **coltxt, char **colname)
2267 {
2268         pkgmgr_cert_x *info = (pkgmgr_cert_x *)data;
2269         int i = 0;
2270
2271         for(i = 0; i < ncols; i++)
2272         {
2273                 if (strcmp(colname[i], "author_signer_cert") == 0) {
2274                         if (coltxt[i])
2275                                 info->cert_id = atoi(coltxt[i]);
2276                         else
2277                                 info->cert_id = 0;
2278                 } else if (strcmp(colname[i], "package") == 0) {
2279                         if (coltxt[i])
2280                                 info->pkgid= strdup(coltxt[i]);
2281                         else
2282                                 info->pkgid = NULL;
2283                 } else
2284                         continue;
2285         }
2286         return 0;
2287 }
2288
2289 /* get the first locale value*/
2290 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
2291 {
2292         pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
2293
2294         if (ncols >= 1)
2295                 info->locale = strdup(coltxt[0]);
2296         else
2297                 info->locale = NULL;
2298
2299         return 0;
2300 }
2301
2302 static int __exec_pkginfo_query(char *query, void *data)
2303 {
2304         char *error_message = NULL;
2305         if (SQLITE_OK !=
2306             sqlite3_exec(GET_DB(manifest_db), query, __pkginfo_cb, data, &error_message)) {
2307                 _LOGE("Don't execute query = %s error message = %s\n", query,
2308                        error_message);
2309                 sqlite3_free(error_message);
2310                 return -1;
2311         }
2312         sqlite3_free(error_message);
2313         return 0;
2314 }
2315
2316 static int __exec_certinfo_query(char *query, void *data)
2317 {
2318         char *error_message = NULL;
2319         if (SQLITE_OK !=
2320             sqlite3_exec(GET_DB(cert_db), query, __certinfo_cb, data, &error_message)) {
2321                 _LOGE("Don't execute query = %s error message = %s\n", query,
2322                        error_message);
2323                 sqlite3_free(error_message);
2324                 return -1;
2325         }
2326         sqlite3_free(error_message);
2327         return 0;
2328 }
2329
2330 static int __exec_certindexinfo_query(char *query, void *data)
2331 {
2332         char *error_message = NULL;
2333         if (SQLITE_OK !=
2334             sqlite3_exec(GET_DB(cert_db), query, __certindexinfo_cb, data, &error_message)) {
2335                 _LOGE("Don't execute query = %s error message = %s\n", query,
2336                        error_message);
2337                 sqlite3_free(error_message);
2338                 return -1;
2339         }
2340         sqlite3_free(error_message);
2341         return 0;
2342 }
2343
2344 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
2345 {
2346         char *error_message = NULL;
2347         int ret = sqlite3_exec(db, query, callback, data, &error_message);
2348         if (SQLITE_OK != ret) {
2349                 _LOGE("Don't execute query = %s error message = %s   ret = %d\n", query,
2350                        error_message, ret);
2351                 sqlite3_free(error_message);
2352                 return -1;
2353         }
2354         sqlite3_free(error_message);
2355         return 0;
2356 }
2357
2358
2359 static int __child_element(xmlTextReaderPtr reader, int depth)
2360 {
2361         int ret = xmlTextReaderRead(reader);
2362         int cur = xmlTextReaderDepth(reader);
2363         while (ret == 1) {
2364
2365                 switch (xmlTextReaderNodeType(reader)) {
2366                 case XML_READER_TYPE_ELEMENT:
2367                         if (cur == depth + 1)
2368                                 return 1;
2369                         break;
2370                 case XML_READER_TYPE_TEXT:
2371                         /*text is handled by each function separately*/
2372                         if (cur == depth + 1)
2373                                 return 0;
2374                         break;
2375                 case XML_READER_TYPE_END_ELEMENT:
2376                         if (cur == depth)
2377                                 return 0;
2378                         break;
2379                 default:
2380                         if (cur <= depth)
2381                                 return 0;
2382                         break;
2383                 }
2384                 ret = xmlTextReaderRead(reader);
2385                 cur = xmlTextReaderDepth(reader);
2386         }
2387         return ret;
2388 }
2389
2390 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
2391 {
2392         int *p = (int*)data;
2393         *p = atoi(coltxt[0]);
2394         return 0;
2395 }
2396
2397 static int __check_app_locale_from_app_localized_info_by_exact(sqlite3 *db, const char *appid, const char *locale)
2398 {
2399         int result_query = -1;
2400         int ret = 0;
2401         char query[MAX_QUERY_LEN];
2402
2403         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);
2404         ret = __exec_db_query(db, query, __check_validation_of_qurey_cb, (void *)&result_query);
2405         retvm_if(ret == -1, PMINFO_R_ERROR, "Exec DB query failed");
2406         return result_query;
2407 }
2408
2409 static int __check_app_locale_from_app_localized_info_by_fallback(sqlite3 *db, const char *appid, const char *locale)
2410 {
2411         int result_query = -1;
2412         int ret = 0;
2413         char wildcard[2] = {'%','\0'};
2414         char query[MAX_QUERY_LEN];
2415         char lang[3] = {'\0'};
2416         strncpy(lang, locale, LANGUAGE_LENGTH);
2417
2418         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);
2419         ret = __exec_db_query(db, query, __check_validation_of_qurey_cb, (void *)&result_query);
2420         retvm_if(ret == -1, PMINFO_R_ERROR, "Exec DB query failed");
2421         return result_query;
2422 }
2423
2424 static char* __get_app_locale_from_app_localized_info_by_fallback(sqlite3 *db, const char *appid, const char *locale)
2425 {
2426         int ret = 0;
2427         char wildcard[2] = {'%','\0'};
2428         char lang[3] = {'\0'};
2429         char query[MAX_QUERY_LEN];
2430         char *locale_new = NULL;
2431         pkgmgr_locale_x *info = NULL;
2432
2433         info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
2434         if (info == NULL) {
2435                 _LOGE("Out of Memory!!!\n");
2436                 return NULL;
2437         }
2438         memset(info, '\0', sizeof(*info));
2439
2440         strncpy(lang, locale, 2);
2441         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);
2442         ret = __exec_db_query(db, query, __fallback_locale_cb, (void *)info);
2443         trym_if(ret == -1, "Exec DB query failed");
2444         locale_new = info->locale;
2445         free(info);
2446         return locale_new;
2447 catch:
2448         if (info) {
2449                 free(info);
2450                 info = NULL;
2451         }
2452         return NULL;
2453 }
2454
2455 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
2456 {
2457         char *locale = malloc(6);
2458         if (!locale) {
2459                 _LOGE("Malloc Failed\n");
2460                 return NULL;
2461         }
2462
2463         sprintf(locale, "%c%c-%c%c", syslocale[0], syslocale[1], tolower(syslocale[3]), tolower(syslocale[4]));
2464         return locale;
2465 }
2466
2467 static char* __get_app_locale_by_fallback(sqlite3 *db, const char *appid, const char *syslocale)
2468 {
2469         assert(appid);
2470         assert(syslocale);
2471
2472         char *locale = NULL;
2473         char *locale_new = NULL;
2474         int check_result = 0;
2475
2476         locale = __convert_syslocale_to_manifest_locale((char *)syslocale);
2477
2478         /*check exact matching */
2479         check_result = __check_app_locale_from_app_localized_info_by_exact(db, appid, locale);
2480
2481         /* Exact found */
2482         if (check_result == 1) {
2483                 _LOGD("%s find exact locale(%s)\n", appid, locale);
2484                 return locale;
2485         }
2486
2487         /* fallback matching */
2488         check_result = __check_app_locale_from_app_localized_info_by_fallback(db, appid, locale);
2489         if(check_result == 1) {
2490                    locale_new = __get_app_locale_from_app_localized_info_by_fallback(db, appid, locale);
2491                    free(locale);
2492                    if (locale_new == NULL)
2493                            locale_new =  strdup(DEFAULT_LOCALE);
2494                    return locale_new;
2495         }
2496
2497         /* default locale */
2498         free(locale);
2499         return  strdup(DEFAULT_LOCALE);
2500 }
2501
2502 long long _pkgmgr_calculate_dir_size(char *dirname)
2503 {
2504         long long total = 0;
2505         long long ret = 0;
2506         int q = 0; /*quotient*/
2507         int r = 0; /*remainder*/
2508         DIR *dp = NULL;
2509         struct dirent *ep = NULL;
2510         struct stat fileinfo;
2511         char abs_filename[FILENAME_MAX] = { 0, };
2512         retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL");
2513
2514         dp = opendir(dirname);
2515         if (dp != NULL) {
2516                 while ((ep = readdir(dp)) != NULL) {
2517                         if (!strcmp(ep->d_name, ".") ||
2518                                 !strcmp(ep->d_name, "..")) {
2519                                 continue;
2520                         }
2521                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
2522                                  ep->d_name);
2523                         if (lstat(abs_filename, &fileinfo) < 0)
2524                                 perror(abs_filename);
2525                         else {
2526                                 if (S_ISDIR(fileinfo.st_mode)) {
2527                                         total += fileinfo.st_size;
2528                                         if (strcmp(ep->d_name, ".")
2529                                             && strcmp(ep->d_name, "..")) {
2530                                                 ret = _pkgmgr_calculate_dir_size
2531                                                     (abs_filename);
2532                                                 total = total + ret;
2533                                         }
2534                                 } else if (S_ISLNK(fileinfo.st_mode)) {
2535                                         continue;
2536                                 } else {
2537                                         /*It is a file. Calculate the actual
2538                                         size occupied (in terms of 4096 blocks)*/
2539                                 q = (fileinfo.st_size / BLOCK_SIZE);
2540                                 r = (fileinfo.st_size % BLOCK_SIZE);
2541                                 if (r) {
2542                                         q = q + 1;
2543                                 }
2544                                 total += q * BLOCK_SIZE;
2545                                 }
2546                         }
2547                 }
2548                 (void)closedir(dp);
2549         } else {
2550                 _LOGE("Couldn't open the directory\n");
2551                 return -1;
2552         }
2553         return total;
2554
2555 }
2556
2557 static int __delete_certinfo(const char *pkgid, uid_t uid)
2558 {
2559         int ret = -1;
2560         int i = 0;
2561         int j = 0;
2562         int c = 0;
2563         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
2564         char *error_message = NULL;
2565         char query[MAX_QUERY_LEN] = {'\0'};
2566         pkgmgr_certinfo_x *certinfo = NULL;
2567         pkgmgr_certindexinfo_x *indexinfo = NULL;
2568         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
2569         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
2570         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
2571         if (indexinfo == NULL) {
2572                 _LOGE("Out of Memory!!!");
2573                 ret = PMINFO_R_ERROR;
2574                 goto err;
2575         }
2576
2577         __open_cert_db(uid, "w");
2578         /*populate certinfo from DB*/
2579         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
2580         ret = __exec_certinfo_query(query, (void *)certinfo);
2581         if (ret == -1) {
2582                 _LOGE("Package Cert Info DB Information retrieval failed\n");
2583                 ret = PMINFO_R_ERROR;
2584                 goto err;
2585         }
2586         /*Update cert index table*/
2587         for (i = 0; i < MAX_CERT_TYPE; i++) {
2588                 if ((certinfo->cert_id)[i]) {
2589                         for (j = 0; j < MAX_CERT_TYPE; j++) {
2590                                 if ((certinfo->cert_id)[i] == unique_id[j]) {
2591                                         /*Ref count has already been updated. Just continue*/
2592                                         break;
2593                                 }
2594                         }
2595                         if (j == MAX_CERT_TYPE)
2596                                 unique_id[c++] = (certinfo->cert_id)[i];
2597                         else
2598                                 continue;
2599                         memset(query, '\0', MAX_QUERY_LEN);
2600                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
2601                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
2602                         if (ret == -1) {
2603                                 _LOGE("Cert Info DB Information retrieval failed\n");
2604                                 ret = PMINFO_R_ERROR;
2605                                 goto err;
2606                         }
2607                         memset(query, '\0', MAX_QUERY_LEN);
2608                         if (indexinfo->cert_ref_count > 1) {
2609                                 /*decrease ref count*/
2610                                 snprintf(query, MAX_QUERY_LEN, "update package_cert_index_info set cert_ref_count=%d where cert_id=%d ",
2611                                 indexinfo->cert_ref_count - 1, (certinfo->cert_id)[i]);
2612                         } else {
2613                                 /*delete this certificate as ref count is 1 and it will become 0*/
2614                                 snprintf(query, MAX_QUERY_LEN, "delete from  package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
2615                         }
2616                         if (SQLITE_OK !=
2617                             sqlite3_exec(GET_DB(cert_db), query, NULL, NULL, &error_message)) {
2618                                 _LOGE("Don't execute query = %s error message = %s\n", query,
2619                                        error_message);
2620                                 sqlite3_free(error_message);
2621                                 ret = PMINFO_R_ERROR;
2622                                 goto err;
2623                         }
2624                 }
2625         }
2626         /*Now delete the entry from db*/
2627         snprintf(query, MAX_QUERY_LEN, "delete from package_cert_info where package='%s'", pkgid);
2628         if (SQLITE_OK !=
2629             sqlite3_exec(GET_DB(cert_db), query, NULL, NULL, &error_message)) {
2630                 _LOGE("Don't execute query = %s error message = %s\n", query,
2631                        error_message);
2632                 sqlite3_free(error_message);
2633                 ret = PMINFO_R_ERROR;
2634                 goto err;
2635         }
2636         ret = PMINFO_R_OK;
2637 err:
2638         if (indexinfo) {
2639                 free(indexinfo);
2640                 indexinfo = NULL;
2641         }
2642         if (certinfo->pkgid) {
2643                 free(certinfo->pkgid);
2644                 certinfo->pkgid = NULL;
2645         }
2646         for (i = 0; i < MAX_CERT_TYPE; i++) {
2647                 if ((certinfo->cert_info)[i]) {
2648                         free((certinfo->cert_info)[i]);
2649                         (certinfo->cert_info)[i] = NULL;
2650                 }
2651         }
2652         __close_cert_db();
2653         free(certinfo);
2654         certinfo = NULL;
2655         return ret;
2656 }
2657
2658 static int __get_pkg_location(const char *pkgid)
2659 {
2660         retvm_if(pkgid == NULL, PMINFO_R_OK, "pkginfo handle is NULL");
2661
2662         FILE *fp = NULL;
2663         char pkg_mmc_path[FILENAME_MAX] = { 0, };
2664         snprintf(pkg_mmc_path, FILENAME_MAX, "%s%s", PKG_SD_PATH, pkgid);
2665
2666         /*check whether application is in external memory or not */
2667         fp = fopen(pkg_mmc_path, "r");
2668         if (fp != NULL) {
2669                 fclose(fp);
2670                 fp = NULL;
2671                 return PMINFO_EXTERNAL_STORAGE;
2672         }
2673
2674         return PMINFO_INTERNAL_STORAGE;
2675 }
2676
2677 API int pkgmgrinfo_pkginfo_get_usr_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data, uid_t uid)
2678 {
2679         retvm_if(pkg_list_cb == NULL, PMINFO_R_EINVAL, "callback function is NULL\n");
2680         char *error_message = NULL;
2681         int ret = PMINFO_R_OK;
2682         int ret_db = 0;
2683
2684         char query[MAX_QUERY_LEN] = {'\0'};
2685         char *syslocale = NULL;
2686         char *locale = NULL;
2687         pkgmgr_pkginfo_x *pkginfo = NULL;
2688         label_x *tmp1 = NULL;
2689         icon_x *tmp2 = NULL;
2690         description_x *tmp3 = NULL;
2691         author_x *tmp4 = NULL;
2692         privilege_x *tmp5 = NULL;
2693
2694         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2695         if (syslocale == NULL) {
2696                 _LOGE("current locale is NULL\n");
2697                 return PMINFO_R_ERROR;
2698         }
2699         locale = __convert_system_locale_to_manifest_locale(syslocale);
2700         if (locale == NULL) {
2701                 _LOGE("manifest locale is NULL\n");
2702                 free(syslocale);
2703                 return PMINFO_R_ERROR;
2704         }
2705
2706         ret_db = __open_manifest_db(uid);
2707         if (ret_db == -1) {
2708                 _LOGE("Fail to open manifest DB\n");
2709                 free(syslocale);
2710                 free(locale);
2711                 return PMINFO_R_ERROR;
2712         }
2713         pkgmgr_pkginfo_x *tmphead = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2714         pkgmgr_pkginfo_x *node = NULL;
2715         pkgmgr_pkginfo_x *temp_node = NULL;
2716
2717         snprintf(query, MAX_QUERY_LEN, "select * from package_info");
2718         if (SQLITE_OK !=
2719             sqlite3_exec(GET_DB(manifest_db), query, __pkg_list_cb, (void *)tmphead, &error_message)) {
2720                 _LOGE("Don't execute query = %s error message = %s\n", query,
2721                        error_message);
2722                 sqlite3_free(error_message);
2723                 ret = PMINFO_R_ERROR;
2724                 goto err;
2725         }
2726
2727         LISTHEAD(tmphead, node);
2728
2729         for(node = node->next; node ; node = node->next) {
2730                 pkginfo = node;
2731                 pkginfo->locale = strdup(locale);
2732                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
2733                 if (pkginfo->manifest_info->privileges == NULL) {
2734                         _LOGE("Failed to allocate memory for privileges info\n");
2735                         ret = PMINFO_R_ERROR;
2736                         goto err;
2737                 }
2738                 /*populate manifest_info from DB*/
2739                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
2740                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2741                 if (ret == -1) {
2742                         _LOGE("Package Info DB Information retrieval failed\n");
2743                         ret = PMINFO_R_ERROR;
2744                         goto err;
2745                 }
2746                 memset(query, '\0', MAX_QUERY_LEN);
2747                 /*populate privilege_info from DB*/
2748                 snprintf(query, MAX_QUERY_LEN, "select * from package_privilege_info where package='%s' ", pkginfo->manifest_info->package);
2749                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2750                 if (ret == -1) {
2751                         _LOGE("Package Privilege Info DB Information retrieval failed\n");
2752                         ret = PMINFO_R_ERROR;
2753                         goto err;
2754                 }
2755                 memset(query, '\0', MAX_QUERY_LEN);
2756                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2757                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
2758                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2759                 if (ret == -1) {
2760                         _LOGE("Package Info DB Information retrieval failed\n");
2761                         ret = PMINFO_R_ERROR;
2762                         goto err;
2763                 }
2764                 /*Also store the values corresponding to default locales*/
2765                 memset(query, '\0', MAX_QUERY_LEN);
2766                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2767                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
2768                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
2769                 if (ret == -1) {
2770                         _LOGE("Package Info DB Information retrieval failed\n");
2771                         ret = PMINFO_R_ERROR;
2772                         goto err;
2773                 }
2774                 if (pkginfo->manifest_info->label) {
2775                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
2776                         pkginfo->manifest_info->label = tmp1;
2777                 }
2778                 if (pkginfo->manifest_info->icon) {
2779                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2780                         pkginfo->manifest_info->icon = tmp2;
2781                 }
2782                 if (pkginfo->manifest_info->description) {
2783                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
2784                         pkginfo->manifest_info->description = tmp3;
2785                 }
2786                 if (pkginfo->manifest_info->author) {
2787                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
2788                         pkginfo->manifest_info->author = tmp4;
2789                 }
2790                 if (pkginfo->manifest_info->privileges->privilege) {
2791                         LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
2792                         pkginfo->manifest_info->privileges->privilege = tmp5;
2793                 }
2794         }
2795
2796         LISTHEAD(tmphead, node);
2797
2798         for(node = node->next; node ; node = node->next) {
2799                 pkginfo = node;
2800                 pkginfo->uid = uid;
2801                 ret = pkg_list_cb( (void *)pkginfo, user_data);
2802                 if(ret < 0)
2803                         break;
2804         }
2805
2806         ret = PMINFO_R_OK;
2807
2808 err:
2809         __close_manifest_db();
2810         if (syslocale) {
2811                 free(syslocale);
2812                 syslocale = NULL;
2813         }
2814         if (locale) {
2815                 free(locale);
2816                 locale = NULL;
2817         }
2818         LISTHEAD(tmphead, node);
2819         temp_node = node->next;
2820         node = temp_node;
2821         while (node) {
2822                 temp_node = node->next;
2823                 __cleanup_pkginfo(node);
2824                 node = temp_node;
2825         }
2826         __cleanup_pkginfo(tmphead);
2827         return ret;
2828 }
2829
2830 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
2831 {
2832         return pkgmgrinfo_pkginfo_get_usr_list(pkg_list_cb, user_data, GLOBAL_USER);
2833 }
2834
2835 API int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkginfo_h *handle)
2836 {
2837         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2838         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2839         pkgmgr_pkginfo_x *pkginfo = NULL;
2840         int ret = PMINFO_R_OK;
2841         char query[MAX_QUERY_LEN] = {'\0'};
2842         char *syslocale = NULL;
2843         char *locale = NULL;
2844         int exist = 0;
2845         label_x *tmp1 = NULL;
2846         icon_x *tmp2 = NULL;
2847         description_x *tmp3 = NULL;
2848         author_x *tmp4 = NULL;
2849         privilege_x *tmp5 = NULL;
2850         const char* user_pkg_parser = NULL;
2851
2852         *handle = NULL;
2853
2854         /*validate pkgid*/
2855         user_pkg_parser = getUserPkgParserDBPathUID(uid);
2856         ret = __open_manifest_db(uid);
2857           retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", user_pkg_parser);
2858
2859         /*check pkgid exist on db*/
2860         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_info where package='%s')", pkgid);
2861         ret = __exec_db_query(GET_DB(manifest_db), query, __validate_cb, (void *)&exist);
2862         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec[%s] fail", pkgid);
2863         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "pkgid[%s] for user[%d] is not found in DB", pkgid, uid);
2864
2865         /*get system locale*/
2866         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2867         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
2868
2869         /*get locale on db*/
2870         locale = __convert_system_locale_to_manifest_locale(syslocale);
2871         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
2872
2873         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2874         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
2875
2876         pkginfo->locale = strdup(locale);
2877
2878         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
2879         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
2880
2881         pkginfo->manifest_info->package = strdup(pkgid);
2882         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
2883         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
2884
2885         /*populate manifest_info from DB*/
2886         snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkgid);
2887         ret = __exec_db_query(GET_DB(manifest_db), query, __pkginfo_cb, (void *)pkginfo);
2888         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2889
2890         memset(query, '\0', MAX_QUERY_LEN);
2891         /*populate privilege_info from DB*/
2892         snprintf(query, MAX_QUERY_LEN, "select * from package_privilege_info where package='%s' ", pkgid);
2893         ret = __exec_db_query(GET_DB(manifest_db), query, __pkginfo_cb, (void *)pkginfo);
2894         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Privilege Info DB Information retrieval failed");
2895
2896         memset(query, '\0', MAX_QUERY_LEN);
2897         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2898                 " package='%s' and package_locale='%s'", pkgid, locale);
2899         ret = __exec_db_query(GET_DB(manifest_db), query, __pkginfo_cb, (void *)pkginfo);
2900         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2901
2902         /*Also store the values corresponding to default locales*/
2903         memset(query, '\0', MAX_QUERY_LEN);
2904         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2905                 " package='%s' and package_locale='%s'", pkgid, DEFAULT_LOCALE);
2906         ret = __exec_db_query(GET_DB(manifest_db), query, __pkginfo_cb, (void *)pkginfo);
2907         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2908
2909         if (pkginfo->manifest_info->label) {
2910                 LISTHEAD(pkginfo->manifest_info->label, tmp1);
2911                 pkginfo->manifest_info->label = tmp1;
2912         }
2913         if (pkginfo->manifest_info->icon) {
2914                 LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2915                 pkginfo->manifest_info->icon = tmp2;
2916         }
2917         if (pkginfo->manifest_info->description) {
2918                 LISTHEAD(pkginfo->manifest_info->description, tmp3);
2919                 pkginfo->manifest_info->description = tmp3;
2920         }
2921         if (pkginfo->manifest_info->author) {
2922                 LISTHEAD(pkginfo->manifest_info->author, tmp4);
2923                 pkginfo->manifest_info->author = tmp4;
2924         }
2925         if (pkginfo->manifest_info->privileges->privilege) {
2926                 LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
2927                 pkginfo->manifest_info->privileges->privilege = tmp5;
2928         }
2929
2930 catch:
2931         if (ret == PMINFO_R_OK)
2932                 *handle = (void*)pkginfo;
2933         else {
2934                 *handle = NULL;
2935                 __cleanup_pkginfo(pkginfo);
2936         }
2937         __close_manifest_db();
2938         if (syslocale) {
2939                 free(syslocale);
2940                 syslocale = NULL;
2941         }
2942         if (locale) {
2943                 free(locale);
2944                 locale = NULL;
2945         }
2946         return ret;
2947 }
2948
2949 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
2950 {
2951         return pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, GLOBAL_USER, handle);
2952 }
2953
2954 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
2955 {
2956         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2957
2958         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2959         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2960
2961         if (info->manifest_info == NULL || info->manifest_info->package == NULL)
2962                 return PMINFO_R_ERROR;
2963
2964         *pkg_name = (char *)info->manifest_info->package;
2965
2966         return PMINFO_R_OK;
2967 }
2968
2969 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
2970 {
2971         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2972
2973         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2974         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2975
2976         if (info->manifest_info == NULL || info->manifest_info->package == NULL)
2977                 return PMINFO_R_ERROR;
2978
2979         *pkgid = (char *)info->manifest_info->package;
2980
2981         return PMINFO_R_OK;
2982 }
2983
2984 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
2985 {
2986         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2987
2988         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
2989         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2990
2991         if (info->manifest_info == NULL || info->manifest_info->type == NULL)
2992                 return PMINFO_R_ERROR;
2993
2994         *type = (char *)info->manifest_info->type;
2995
2996         return PMINFO_R_OK;
2997 }
2998
2999 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
3000 {
3001         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3002
3003         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3004         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3005
3006         if (info->manifest_info == NULL || info->manifest_info->version == NULL)
3007                 return PMINFO_R_ERROR;
3008
3009         *version = (char *)info->manifest_info->version;
3010
3011         return PMINFO_R_OK;
3012 }
3013
3014 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
3015 {
3016         char *val;
3017         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3018
3019         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3020         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3021
3022         if (info->manifest_info == NULL || info->manifest_info->installlocation == NULL)
3023                 return PMINFO_R_ERROR;
3024
3025         val = (char *)info->manifest_info->installlocation;
3026         if (strcmp(val, "internal-only") == 0)
3027                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
3028         else if (strcmp(val, "prefer-external") == 0)
3029                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
3030         else
3031                 *location = PMINFO_INSTALL_LOCATION_AUTO;
3032
3033         return PMINFO_R_OK;
3034 }
3035
3036 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
3037 {
3038         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3039
3040         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3041         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3042
3043         if (info->manifest_info == NULL || info->manifest_info->package_size == NULL)
3044                 return PMINFO_R_ERROR;
3045
3046         *size = atoi((char *)info->manifest_info->package_size);
3047
3048         return PMINFO_R_OK;
3049 }
3050
3051 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
3052 {
3053         char *pkgid;
3054         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
3055         long long rw_size = 0;
3056         long long ro_size = 0;
3057         long long tmp_size = 0;
3058         long long total_size = 0;
3059         struct stat fileinfo;
3060         int ret;
3061
3062         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3063         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3064
3065         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
3066         if (ret < 0)
3067                 return PMINFO_R_ERROR;
3068
3069         /* RW area */
3070         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
3071         if (lstat(device_path, &fileinfo) == 0) {
3072                 if (!S_ISLNK(fileinfo.st_mode)) {
3073                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3074                         if (tmp_size > 0)
3075                                 rw_size += tmp_size;
3076                 }
3077         }
3078
3079         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
3080         if (lstat(device_path, &fileinfo) == 0) {
3081                 if (!S_ISLNK(fileinfo.st_mode)) {
3082                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3083                         if (tmp_size > 0)
3084                         rw_size += tmp_size;
3085                 }
3086         }
3087
3088         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
3089         if (lstat(device_path, &fileinfo) == 0) {
3090                 if (!S_ISLNK(fileinfo.st_mode)) {
3091                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3092                         if (tmp_size > 0)
3093                         rw_size += tmp_size;
3094                 }
3095         }
3096
3097         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
3098         if (lstat(device_path, &fileinfo) == 0) {
3099                 if (!S_ISLNK(fileinfo.st_mode)) {
3100                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3101                         if (tmp_size > 0)
3102                                 rw_size += tmp_size;
3103                 }
3104         }
3105
3106         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
3107         if (lstat(device_path, &fileinfo) == 0) {
3108                 if (!S_ISLNK(fileinfo.st_mode)) {
3109                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3110                         if (tmp_size > 0)
3111                                 rw_size += tmp_size;
3112         }
3113         }
3114
3115         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
3116         if (lstat(device_path, &fileinfo) == 0) {
3117                 if (!S_ISLNK(fileinfo.st_mode)) {
3118                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3119                         if (tmp_size > 0)
3120                                 rw_size += tmp_size;
3121                 }
3122         }
3123
3124         /* RO area */
3125         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
3126         if (lstat(device_path, &fileinfo) == 0) {
3127                 if (!S_ISLNK(fileinfo.st_mode)) {
3128                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3129                         if (tmp_size > 0)
3130                                 ro_size += tmp_size;
3131                 }
3132         }
3133
3134         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
3135         if (lstat(device_path, &fileinfo) == 0) {
3136                 if (!S_ISLNK(fileinfo.st_mode)) {
3137                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3138                         if (tmp_size > 0)
3139                                 ro_size += tmp_size;
3140                 }
3141         }
3142
3143         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
3144         if (lstat(device_path, &fileinfo) == 0) {
3145                 if (!S_ISLNK(fileinfo.st_mode)) {
3146                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3147                         if (tmp_size > 0)
3148                                 ro_size += tmp_size;
3149                 }
3150         }
3151
3152         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
3153         if (lstat(device_path, &fileinfo) == 0) {
3154                 if (!S_ISLNK(fileinfo.st_mode)) {
3155                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3156                         if (tmp_size > 0)
3157                                 ro_size += tmp_size;
3158                 }
3159         }
3160
3161         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
3162         if (lstat(device_path, &fileinfo) == 0) {
3163                 if (!S_ISLNK(fileinfo.st_mode)) {
3164                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3165                         if (tmp_size > 0)
3166                                 ro_size += tmp_size;
3167                 }
3168         }
3169
3170         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
3171         if (lstat(device_path, &fileinfo) == 0) {
3172                 if (!S_ISLNK(fileinfo.st_mode)) {
3173                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3174                         if (tmp_size > 0)
3175                                 ro_size += tmp_size;
3176                 }
3177         }
3178
3179         /* Total size */
3180         total_size = rw_size + ro_size;
3181         *size = (int)total_size;
3182
3183         return PMINFO_R_OK;
3184 }
3185
3186 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
3187 {
3188         char *pkgid;
3189         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
3190         long long total_size = 0;
3191
3192         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3193         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3194
3195         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) < 0)
3196                 return PMINFO_R_ERROR;
3197
3198         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
3199         if (access(device_path, R_OK) == 0)
3200                 total_size = _pkgmgr_calculate_dir_size(device_path);
3201         if (total_size < 0)
3202                 return PMINFO_R_ERROR;
3203
3204         *size = (int)total_size;
3205
3206         return PMINFO_R_OK;
3207 }
3208
3209 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
3210 {
3211         char *locale;
3212         icon_x *ptr;
3213         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3214
3215         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3216         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3217
3218         locale = info->locale;
3219         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3220
3221         for (ptr = info->manifest_info->icon; ptr != NULL; ptr = ptr->next) {
3222                 if (ptr->lang == NULL)
3223                         continue;
3224
3225                 if (strcmp(ptr->lang, locale) == 0) {
3226                         *icon = (char *)ptr->text;
3227                         if (strcasecmp(*icon, "(null)") == 0) {
3228                                 locale = DEFAULT_LOCALE;
3229                                 continue;
3230                         } else {
3231                                 return PMINFO_R_OK;
3232                         }
3233                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3234                         *icon = (char *)ptr->text;
3235                         return PMINFO_R_OK;
3236                 }
3237         }
3238
3239         return PMINFO_R_ERROR;
3240 }
3241
3242 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
3243 {
3244         char *locale;
3245         label_x *ptr;
3246         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3247
3248         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3249         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3250
3251         locale = info->locale;
3252         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3253
3254         for (ptr = info->manifest_info->label; ptr != NULL; ptr = ptr->next) {
3255                 if (ptr->lang == NULL)
3256                         continue;
3257
3258                 if (strcmp(ptr->lang, locale) == 0) {
3259                         *label = (char *)ptr->text;
3260                         if (strcasecmp(*label, "(null)") == 0) {
3261                                 locale = DEFAULT_LOCALE;
3262                                 continue;
3263                         } else {
3264                                 return PMINFO_R_OK;
3265                         }
3266                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3267                         *label = (char *)ptr->text;
3268                         return PMINFO_R_OK;
3269                 }
3270         }
3271
3272         return PMINFO_R_ERROR;
3273 }
3274
3275 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
3276 {
3277         char *locale;
3278         description_x *ptr;
3279         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3280
3281         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3282         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3283
3284         locale = info->locale;
3285         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3286
3287         for (ptr = info->manifest_info->description; ptr != NULL; ptr = ptr->next) {
3288                 if (ptr->lang == NULL)
3289                         continue;
3290
3291                 if (strcmp(ptr->lang, locale) == 0) {
3292                         *description = (char *)ptr->text;
3293                         if (strcasecmp(*description, PKGMGR_PARSER_EMPTY_STR) == 0) {
3294                                 locale = DEFAULT_LOCALE;
3295                                 continue;
3296                         } else {
3297                                 return PMINFO_R_OK;
3298                         }
3299                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3300                         *description = (char *)ptr->text;
3301                         return PMINFO_R_OK;
3302                 }
3303         }
3304
3305         return PMINFO_R_ERROR;
3306 }
3307
3308 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
3309 {
3310         char *locale;
3311         author_x *ptr;
3312         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3313
3314         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3315         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3316
3317         locale = info->locale;
3318         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3319
3320         for (ptr = info->manifest_info->author; ptr != NULL; ptr = ptr->next) {
3321                 if (ptr->lang == NULL)
3322                         continue;
3323
3324                 if (strcmp(ptr->lang, locale) == 0) {
3325                         *author_name = (char *)ptr->text;
3326                         if (strcasecmp(*author_name, PKGMGR_PARSER_EMPTY_STR) == 0) {
3327                                 locale = DEFAULT_LOCALE;
3328                                 continue;
3329                         } else {
3330                                 return PMINFO_R_OK;
3331                         }
3332                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3333                         *author_name = (char *)ptr->text;
3334                         return PMINFO_R_OK;
3335                 }
3336         }
3337
3338         return PMINFO_R_ERROR;
3339 }
3340
3341 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
3342 {
3343         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3344
3345         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3346         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3347
3348         if (info->manifest_info == NULL || info->manifest_info->author == NULL ||
3349                         info->manifest_info->author->email == NULL)
3350                 return PMINFO_R_ERROR;
3351
3352         *author_email = (char *)info->manifest_info->author->email;
3353
3354         return PMINFO_R_OK;
3355 }
3356
3357 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
3358 {
3359         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3360
3361         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3362         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3363
3364         if (info->manifest_info == NULL || info->manifest_info->author == NULL ||
3365                         info->manifest_info->author->href == NULL)
3366                 return PMINFO_R_ERROR;
3367
3368         *author_href = (char *)info->manifest_info->author->href;
3369
3370         return PMINFO_R_OK;
3371 }
3372
3373 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
3374 {
3375         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3376
3377         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3378         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3379
3380         if (info->manifest_info == NULL || info->manifest_info->installed_storage == NULL)
3381                 return PMINFO_R_ERROR;
3382
3383         if (strcmp(info->manifest_info->installed_storage,"installed_internal") == 0)
3384                 *storage = PMINFO_INTERNAL_STORAGE;
3385         else if (strcmp(info->manifest_info->installed_storage,"installed_external") == 0)
3386                 *storage = PMINFO_EXTERNAL_STORAGE;
3387         else
3388                 return PMINFO_R_ERROR;
3389
3390         return PMINFO_R_OK;
3391 }
3392
3393 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
3394 {
3395         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3396
3397         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3398         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3399
3400         if (info->manifest_info == NULL || info->manifest_info->installed_time == NULL)
3401                 return PMINFO_R_ERROR;
3402
3403         *installed_time = atoi(info->manifest_info->installed_time);
3404
3405         return PMINFO_R_OK;
3406 }
3407
3408 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
3409 {
3410         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3411
3412         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3413         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3414
3415         if (info->manifest_info == NULL || info->manifest_info->storeclient_id == NULL)
3416                 return PMINFO_R_ERROR;
3417
3418         *storeclientid = (char *)info->manifest_info->storeclient_id;
3419
3420         return PMINFO_R_OK;
3421 }
3422
3423 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
3424 {
3425         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3426
3427         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3428         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3429
3430         if (info->manifest_info == NULL || info->manifest_info->mainapp_id == NULL)
3431                 return PMINFO_R_ERROR;
3432
3433         *mainappid = (char *)info->manifest_info->mainapp_id;
3434
3435         return PMINFO_R_OK;
3436 }
3437
3438 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
3439 {
3440         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3441
3442         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3443         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3444
3445         if (info->manifest_info == NULL || info->manifest_info->package_url == NULL)
3446                 return PMINFO_R_ERROR;
3447
3448         *url = (char *)info->manifest_info->package_url;
3449
3450         return PMINFO_R_OK;
3451 }
3452
3453 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
3454 {
3455         const char *val = NULL;
3456         const xmlChar *node;
3457         xmlTextReaderPtr reader;
3458         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
3459         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3460
3461         xmlInitParser();
3462         reader = xmlReaderForFile(manifest, NULL, 0);
3463
3464         if (reader){
3465                 if (__child_element(reader, -1)) {
3466                         node = xmlTextReaderConstName(reader);
3467                         if (!node) {
3468                                 _LOGE("xmlTextReaderConstName value is NULL\n");
3469                                 xmlFreeTextReader(reader);
3470                                 xmlCleanupParser();
3471                                 return PMINFO_R_ERROR;
3472                         }
3473
3474                         if (!strcmp(ASC_CHAR(node), "manifest")) {
3475                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
3476                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
3477
3478                                 if (val) {
3479                                         *size = atoi(val);
3480                                 } else {
3481                                         *size = 0;
3482                                         _LOGE("package size is not specified\n");
3483                                         xmlFreeTextReader(reader);
3484                                         xmlCleanupParser();
3485                                         return PMINFO_R_ERROR;
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 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
3507 {
3508         const char *val = NULL;
3509         const xmlChar *node;
3510         xmlTextReaderPtr reader;
3511         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
3512         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3513
3514         xmlInitParser();
3515         reader = xmlReaderForFile(manifest, NULL, 0);
3516
3517         if (reader) {
3518                 if ( __child_element(reader, -1)) {
3519                         node = xmlTextReaderConstName(reader);
3520                         if (!node) {
3521                                 _LOGE("xmlTextReaderConstName value is NULL\n");
3522                                 xmlFreeTextReader(reader);
3523                                 xmlCleanupParser();
3524                                 return PMINFO_R_ERROR;
3525                         }
3526
3527                         if (!strcmp(ASC_CHAR(node), "manifest")) {
3528                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
3529                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
3530
3531                                 if (val) {
3532                                         if (strcmp(val, "internal-only") == 0)
3533                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
3534                                         else if (strcmp(val, "prefer-external") == 0)
3535                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
3536                                         else
3537                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
3538                                 }
3539                         } else {
3540                                 _LOGE("Unable to create xml reader\n");
3541                                 xmlFreeTextReader(reader);
3542                                 xmlCleanupParser();
3543                                 return PMINFO_R_ERROR;
3544                         }
3545                 }
3546         } else {
3547                 _LOGE("xmlReaderForFile value is NULL\n");
3548                 xmlCleanupParser();
3549                 return PMINFO_R_ERROR;
3550         }
3551
3552         xmlFreeTextReader(reader);
3553         xmlCleanupParser();
3554
3555         return PMINFO_R_OK;
3556 }
3557
3558
3559 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
3560 {
3561         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3562
3563         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3564         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3565
3566         if (info->manifest_info == NULL || info->manifest_info->root_path == NULL)
3567                 return PMINFO_R_ERROR;
3568
3569         *path = (char *)info->manifest_info->root_path;
3570
3571         return PMINFO_R_OK;
3572 }
3573
3574 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
3575 {
3576         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3577
3578         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3579         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3580
3581         if (info->manifest_info == NULL || info->manifest_info->csc_path == NULL)
3582                 return PMINFO_R_ERROR;
3583
3584         *path = (char *)info->manifest_info->csc_path;
3585
3586         return PMINFO_R_OK;
3587 }
3588
3589 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)
3590 {
3591         int ret = PMINFO_R_OK;
3592         char query[MAX_QUERY_LEN] = {'\0'};
3593         char *error_message;
3594         sqlite3_stmt *stmt = NULL;
3595         char *lhs_certinfo = NULL;
3596         char *rhs_certinfo = NULL;
3597         int lcert;
3598         int rcert;
3599         int exist;
3600         int i;
3601         int is_global = 0;
3602         *compare_result = PMINFO_CERT_COMPARE_ERROR;
3603
3604         retvm_if(lhs_package_id == NULL, PMINFO_R_EINVAL, "lhs package ID is NULL");
3605         retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
3606         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3607
3608         ret = __open_cert_db(uid, "r");
3609         if (ret != 0) {
3610                 ret = PMINFO_R_ERROR;
3611                 goto err;
3612         }
3613         _check_create_Cert_db(GET_DB(cert_db));
3614         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", lhs_package_id);
3615         if (SQLITE_OK !=
3616             sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
3617                 _LOGE("Don't execute query = %s error message = %s\n", query,
3618                        error_message);
3619                 ret = PMINFO_R_ERROR;
3620                 goto err;
3621         }
3622         lcert = exist;
3623
3624         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", rhs_package_id);
3625         if (SQLITE_OK !=
3626                 sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
3627                 _LOGE("Don't execute query = %s error message = %s\n", query,
3628                            error_message);
3629                 ret = PMINFO_R_ERROR;
3630                 goto err;
3631         }
3632         rcert = exist;
3633
3634         if (uid == GLOBAL_USER || uid == ROOT_UID) {
3635                 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=?)");
3636                 is_global = 1;
3637         } else
3638                 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=?)");
3639         if (SQLITE_OK != sqlite3_prepare_v2(GET_DB(cert_db), query, strlen(query), &stmt, NULL)) {
3640                 _LOGE("sqlite3_prepare_v2 error: %s", sqlite3_errmsg(GET_DB(cert_db)));
3641                 ret = PMINFO_R_ERROR;
3642                 goto err;
3643         }
3644
3645         for (i = 1; i <= 2 - is_global; i++) {
3646                 if (SQLITE_OK != sqlite3_bind_text(stmt, i, lhs_package_id, -1, SQLITE_STATIC)) {
3647                         _LOGE("sqlite3_bind_text error: %s", sqlite3_errmsg(GET_DB(cert_db)));
3648                         ret = PMINFO_R_ERROR;
3649                         goto err;
3650                 }
3651         }
3652         if (SQLITE_ROW != sqlite3_step(stmt) || sqlite3_column_text(stmt, 0) == NULL) {
3653                 _LOGE("sqlite3_step error: %s", sqlite3_errmsg(GET_DB(cert_db)));
3654                 ret = PMINFO_R_ERROR;
3655                 goto err;
3656         }
3657
3658         lhs_certinfo = strdup((const char *)sqlite3_column_text(stmt, 0));
3659         sqlite3_reset(stmt);
3660         sqlite3_clear_bindings(stmt);
3661
3662         for (i = 1; i <= 2 - is_global; i++) {
3663                 if (SQLITE_OK != sqlite3_bind_text(stmt, i, rhs_package_id, -1, SQLITE_STATIC)) {
3664                         _LOGE("sqlite3_bind_text error: %s", sqlite3_errmsg(GET_DB(cert_db)));
3665                         ret = PMINFO_R_ERROR;
3666                         goto err;
3667                 }
3668         }
3669         if (SQLITE_ROW != sqlite3_step(stmt) || sqlite3_column_text(stmt, 0) == NULL) {
3670                 _LOGE("sqlite3_step error: %s", sqlite3_errmsg(GET_DB(cert_db)));
3671                 ret = PMINFO_R_ERROR;
3672                 goto err;
3673         }
3674
3675         rhs_certinfo = strdup((const char *)sqlite3_column_text(stmt, 0));
3676
3677         if ((lcert == 0) || (rcert == 0)) {
3678                 if ((lcert == 0) && (rcert == 0))
3679                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
3680                 else if (lcert == 0)
3681                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
3682                 else if (rcert == 0)
3683                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
3684         } else {
3685                 if (!strcmp(lhs_certinfo, rhs_certinfo))
3686                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
3687                 else
3688                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
3689         }
3690
3691 err:
3692         if (stmt)
3693                 sqlite3_finalize(stmt);
3694         if (lhs_certinfo)
3695                 free(lhs_certinfo);
3696         if (rhs_certinfo)
3697                 free(rhs_certinfo);
3698         sqlite3_free(error_message);
3699         __close_cert_db();
3700
3701         return ret;
3702 }
3703
3704 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)
3705 {
3706         return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_package_id, rhs_package_id, GLOBAL_USER, compare_result);
3707 }
3708
3709 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)
3710 {
3711         int ret = PMINFO_R_OK;
3712         char query[MAX_QUERY_LEN] = {'\0'};
3713         char *error_message;
3714         pkgmgr_cert_x *info;
3715         int exist;
3716         char *lpkgid = NULL;
3717         char *rpkgid = NULL;
3718         const char* user_pkg_parser = getUserPkgParserDBPath();
3719
3720         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
3721         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
3722         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3723
3724         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3725         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3726
3727         ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
3728                                         SQLITE_OPEN_READONLY, NULL);
3729         if (ret != SQLITE_OK) {
3730                 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
3731                 ret = PMINFO_R_ERROR;
3732                 goto err;
3733         }
3734
3735         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3736         if (SQLITE_OK !=
3737             sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
3738                 _LOGE("Don't execute query = %s error message = %s\n", query,
3739                        error_message);
3740                 ret = PMINFO_R_ERROR;
3741                 goto err;
3742         }
3743
3744         if (exist == 0) {
3745                 lpkgid = NULL;
3746         } else {
3747                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3748                 if (SQLITE_OK !=
3749                         sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
3750                         _LOGE("Don't execute query = %s error message = %s\n", query,
3751                                    error_message);
3752                         ret = PMINFO_R_ERROR;
3753                         goto err;
3754                 }
3755                 lpkgid = strdup(info->pkgid);
3756                 if (lpkgid == NULL) {
3757                         _LOGE("Out of Memory\n");
3758                         ret = PMINFO_R_ERROR;
3759                         goto err;
3760                 }
3761                 free(info->pkgid);
3762                 info->pkgid = NULL;
3763         }
3764
3765         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3766         if (SQLITE_OK !=
3767             sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
3768                 _LOGE("Don't execute query = %s error message = %s\n", query,
3769                        error_message);
3770                 ret = PMINFO_R_ERROR;
3771                 goto err;
3772         }
3773
3774         if (exist == 0) {
3775                 rpkgid = NULL;
3776         } else {
3777                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3778                 if (SQLITE_OK !=
3779                         sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
3780                         _LOGE("Don't execute query = %s error message = %s\n", query,
3781                                    error_message);
3782                         ret = PMINFO_R_ERROR;
3783                         goto err;
3784                 }
3785                 rpkgid = strdup(info->pkgid);
3786                 if (rpkgid == NULL) {
3787                         _LOGE("Out of Memory\n");
3788                         ret = PMINFO_R_ERROR;
3789                         goto err;
3790                 }
3791                 free(info->pkgid);
3792                 info->pkgid = NULL;
3793         }
3794         ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lpkgid, rpkgid, compare_result);
3795  err:
3796         sqlite3_free(error_message);
3797         __close_manifest_db();
3798         if (info) {
3799                 if (info->pkgid) {
3800                         free(info->pkgid);
3801                         info->pkgid = NULL;
3802                 }
3803                 free(info);
3804                 info = NULL;
3805         }
3806         if (lpkgid) {
3807                 free(lpkgid);
3808                 lpkgid = NULL;
3809         }
3810         if (rpkgid) {
3811                 free(rpkgid);
3812                 rpkgid = NULL;
3813         }
3814         return ret;
3815 }
3816
3817 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)
3818 {
3819         int ret = PMINFO_R_OK;
3820         char query[MAX_QUERY_LEN] = {'\0'};
3821         char *error_message;
3822         pkgmgr_cert_x *info;
3823         int exist;
3824         char *lpkgid = NULL;
3825         char *rpkgid = NULL;
3826
3827         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
3828         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
3829         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3830
3831         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3832         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3833
3834         ret = __open_manifest_db(uid);
3835         if (ret != SQLITE_OK) {
3836                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
3837                 ret = PMINFO_R_ERROR;
3838                 goto err;
3839         }
3840
3841         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3842         if (SQLITE_OK !=
3843             sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
3844                 _LOGE("Don't execute query = %s error message = %s\n", query,
3845                        error_message);
3846                 ret = PMINFO_R_ERROR;
3847                 goto err;
3848         }
3849
3850         if (exist == 0) {
3851                 lpkgid = NULL;
3852         } else {
3853                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3854                 if (SQLITE_OK !=
3855                         sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
3856                         _LOGE("Don't execute query = %s error message = %s\n", query,
3857                                    error_message);
3858                         ret = PMINFO_R_ERROR;
3859                         goto err;
3860                 }
3861                 lpkgid = strdup(info->pkgid);
3862                 if (lpkgid == NULL) {
3863                         _LOGE("Out of Memory\n");
3864                         ret = PMINFO_R_ERROR;
3865                         goto err;
3866                 }
3867                 free(info->pkgid);
3868                 info->pkgid = NULL;
3869         }
3870
3871         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3872         if (SQLITE_OK !=
3873             sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
3874                 _LOGE("Don't execute query = %s error message = %s\n", query,
3875                        error_message);
3876                 ret = PMINFO_R_ERROR;
3877                 goto err;
3878         }
3879
3880         if (exist == 0) {
3881                 rpkgid = NULL;
3882         } else {
3883                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3884                 if (SQLITE_OK !=
3885                         sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
3886                         _LOGE("Don't execute query = %s error message = %s\n", query,
3887                                    error_message);
3888                         ret = PMINFO_R_ERROR;
3889                         goto err;
3890                 }
3891                 rpkgid = strdup(info->pkgid);
3892                 if (rpkgid == NULL) {
3893                         _LOGE("Out of Memory\n");
3894                         ret = PMINFO_R_ERROR;
3895                         goto err;
3896                 }
3897                 free(info->pkgid);
3898                 info->pkgid = NULL;
3899         }
3900         ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lpkgid, rpkgid, uid, compare_result);
3901  err:
3902         sqlite3_free(error_message);
3903         __close_manifest_db();
3904         if (info) {
3905                 if (info->pkgid) {
3906                         free(info->pkgid);
3907                         info->pkgid = NULL;
3908                 }
3909                 free(info);
3910                 info = NULL;
3911         }
3912         if (lpkgid) {
3913                 free(lpkgid);
3914                 lpkgid = NULL;
3915         }
3916         if (rpkgid) {
3917                 free(rpkgid);
3918                 rpkgid = NULL;
3919         }
3920         return ret;
3921 }
3922
3923 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
3924 {
3925         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3926         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3927
3928 #if 0 //smack issue occured, check later
3929         char *pkgid = NULL;
3930         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
3931         if (pkgid == NULL){
3932                  _LOGD("invalid func parameters\n");
3933                  return PMINFO_R_ERROR;
3934         }
3935          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
3936
3937         FILE *fp = NULL;
3938         char app_mmc_path[FILENAME_MAX] = { 0, };
3939         char app_dir_path[FILENAME_MAX] = { 0, };
3940         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
3941         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
3942         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
3943         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
3944
3945         /*check whether application is in external memory or not */
3946         fp = fopen(app_mmc_path, "r");
3947         if (fp == NULL){
3948                 _LOGD(" app path in external memory not accesible\n");
3949         } else {
3950                 fclose(fp);
3951                 fp = NULL;
3952                 *accessible = 1;
3953                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
3954                 return PMINFO_R_OK;
3955         }
3956
3957         /*check whether application is in internal or not */
3958         fp = fopen(app_dir_path, "r");
3959         if (fp == NULL) {
3960                 _LOGD(" app path in internal memory not accesible\n");
3961                 *accessible = 0;
3962                 return PMINFO_R_ERROR;
3963         } else {
3964                 fclose(fp);
3965                 /*check whether the application is installed in SD card
3966                 but SD card is not present*/
3967                 fp = fopen(app_mmc_internal_path, "r");
3968                 if (fp == NULL){
3969                         *accessible = 1;
3970                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
3971                         return PMINFO_R_OK;
3972                 }
3973                 else{
3974                         *accessible = 0;
3975                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
3976                 }
3977                 fclose(fp);
3978         }
3979
3980         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
3981 #endif
3982
3983         *accessible = 1;
3984         return PMINFO_R_OK;
3985 }
3986
3987 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
3988 {
3989         char *val;
3990         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3991
3992         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3993         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3994
3995         if (info->manifest_info == NULL || info->manifest_info->removable == NULL)
3996                 return PMINFO_R_ERROR;
3997
3998         val = (char *)info->manifest_info->removable;
3999         if (strcasecmp(val, "true") == 0)
4000                 *removable = 1;
4001         else if (strcasecmp(val, "false") == 0)
4002                 *removable = 0;
4003         else
4004                 *removable = 1;
4005
4006         return PMINFO_R_OK;
4007 }
4008
4009 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
4010 {
4011         char *val;
4012         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4013
4014         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4015         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4016
4017         if (info->manifest_info == NULL || info->manifest_info->installlocation == NULL)
4018                 return PMINFO_R_ERROR;
4019
4020         val = (char *)info->manifest_info->installlocation;
4021         if (strcmp(val, "internal-only") == 0)
4022                 *movable = 0;
4023         else if (strcmp(val, "prefer-external") == 0)
4024                 *movable = 1;
4025         else
4026                 *movable = 1;
4027
4028         return PMINFO_R_OK;
4029 }
4030
4031 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
4032 {
4033         char *val;
4034         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4035
4036         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4037         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4038
4039         if (info->manifest_info == NULL || info->manifest_info->preload == NULL)
4040                 return PMINFO_R_ERROR;
4041
4042         val = (char *)info->manifest_info->preload;
4043         if (strcasecmp(val, "true") == 0)
4044                 *preload = 1;
4045         else if (strcasecmp(val, "false") == 0)
4046                 *preload = 0;
4047         else
4048                 *preload = 0;
4049
4050         return PMINFO_R_OK;
4051 }
4052
4053 API int pkgmgrinfo_pkginfo_is_system(pkgmgrinfo_pkginfo_h handle, bool *system)
4054 {
4055         char *val;
4056         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4057
4058         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4059         retvm_if(system == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4060
4061         if (info->manifest_info == NULL || info->manifest_info->system == NULL)
4062                 return PMINFO_R_ERROR;
4063
4064         val = (char *)info->manifest_info->system;
4065         if (strcasecmp(val, "true") == 0)
4066                 *system = 1;
4067         else if (strcasecmp(val, "false") == 0)
4068                 *system = 0;
4069         else
4070                 *system = 0;
4071
4072         return PMINFO_R_OK;
4073 }
4074
4075 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
4076 {
4077         char *val;
4078         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4079
4080         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4081         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4082
4083         if (info->manifest_info == NULL || info->manifest_info->readonly == NULL)
4084                 return PMINFO_R_ERROR;
4085
4086         val = (char *)info->manifest_info->readonly;
4087         if (strcasecmp(val, "true") == 0)
4088                 *readonly = 1;
4089         else if (strcasecmp(val, "false") == 0)
4090                 *readonly = 0;
4091         else
4092                 *readonly = 0;
4093
4094         return PMINFO_R_OK;
4095 }
4096
4097 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
4098 {
4099         char *val;
4100         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4101
4102         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4103         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4104
4105         if (info->manifest_info == NULL || info->manifest_info->update == NULL)
4106                 return PMINFO_R_ERROR;
4107
4108         val = (char *)info->manifest_info->update;
4109         if (strcasecmp(val, "true") == 0)
4110                 *update = 1;
4111         else if (strcasecmp(val, "false") == 0)
4112                 *update = 0;
4113         else
4114                 *update = 1;
4115
4116         return PMINFO_R_OK;
4117 }
4118
4119 API int pkgmgrinfo_pkginfo_is_for_all_users(pkgmgrinfo_pkginfo_h handle, bool *for_all_users)
4120 {
4121         char *val;
4122         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4123
4124         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4125         retvm_if(for_all_users == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4126
4127         if (info->manifest_info == NULL || info->manifest_info->for_all_users == NULL)
4128                 return PMINFO_R_ERROR;
4129
4130         val = (char *)info->manifest_info->for_all_users;
4131         if (strcasecmp(val, "1") == 0)
4132                 *for_all_users = 1;
4133         else if (strcasecmp(val, "0") == 0)
4134                 *for_all_users = 0;
4135         else
4136                 *for_all_users = 1;
4137
4138         return PMINFO_R_OK;
4139 }
4140
4141
4142 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
4143 {
4144         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4145
4146         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4147
4148         __cleanup_pkginfo(info);
4149
4150         return PMINFO_R_OK;
4151 }
4152
4153 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
4154 {
4155         pkgmgrinfo_filter_x *filter;
4156
4157         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
4158
4159         filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
4160         if (filter == NULL) {
4161                 _LOGE("Out of Memory!!!");
4162                 return PMINFO_R_ERROR;
4163         }
4164
4165         *handle = filter;
4166
4167         return PMINFO_R_OK;
4168 }
4169
4170 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
4171 {
4172         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
4173
4174         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4175
4176         if (filter->list) {
4177                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
4178                 g_slist_free(filter->list);
4179         }
4180
4181         free(filter);
4182
4183         return PMINFO_R_OK;
4184 }
4185
4186 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
4187                                 const char *property, const int value)
4188 {
4189         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
4190         char *val;
4191         GSList *link;
4192         int prop;
4193         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
4194         pkgmgrinfo_node_x *node;
4195
4196         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4197         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4198
4199         prop = _pminfo_pkginfo_convert_to_prop_int(property);
4200         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
4201                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
4202                 _LOGE("Invalid Integer Property\n");
4203                 return PMINFO_R_EINVAL;
4204         }
4205         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
4206         if (node == NULL) {
4207                 _LOGE("Out of Memory!!!\n");
4208                 return PMINFO_R_ERROR;
4209         }
4210         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
4211         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
4212         if (val == NULL) {
4213                 _LOGE("Out of Memory\n");
4214                 free(node);
4215                 return PMINFO_R_ERROR;
4216         }
4217         node->prop = prop;
4218         node->value = val;
4219         /*If API is called multiple times for same property, we should override the previous values.
4220         Last value set will be used for filtering.*/
4221         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4222         if (link)
4223                 filter->list = g_slist_delete_link(filter->list, link);
4224         filter->list = g_slist_append(filter->list, (gpointer)node);
4225         return PMINFO_R_OK;
4226
4227 }
4228
4229 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
4230                                 const char *property, const bool value)
4231 {
4232         char *val;
4233         GSList *link;
4234         int prop;
4235         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
4236         pkgmgrinfo_node_x *node;
4237
4238         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4239         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4240
4241         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
4242         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
4243                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
4244                 _LOGE("Invalid Boolean Property\n");
4245                 return PMINFO_R_EINVAL;
4246         }
4247         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
4248         if (node == NULL) {
4249                 _LOGE("Out of Memory!!!\n");
4250                 return PMINFO_R_ERROR;
4251         }
4252         if (value)
4253                 val = strndup("('true','True')", 15);
4254         else
4255                 val = strndup("('false','False')", 17);
4256         if (val == NULL) {
4257                 _LOGE("Out of Memory\n");
4258                 free(node);
4259                 return PMINFO_R_ERROR;
4260         }
4261         node->prop = prop;
4262         node->value = val;
4263         /*If API is called multiple times for same property, we should override the previous values.
4264         Last value set will be used for filtering.*/
4265         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4266         if (link)
4267                 filter->list = g_slist_delete_link(filter->list, link);
4268         filter->list = g_slist_append(filter->list, (gpointer)node);
4269         return PMINFO_R_OK;
4270
4271 }
4272
4273 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
4274                                 const char *property, const char *value)
4275 {
4276         char *val;
4277         GSList *link;
4278         int prop;
4279         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
4280         pkgmgrinfo_node_x *node;
4281
4282         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4283         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4284         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4285
4286         prop = _pminfo_pkginfo_convert_to_prop_str(property);
4287         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
4288                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
4289                 _LOGE("Invalid String Property\n");
4290                 return PMINFO_R_EINVAL;
4291         }
4292         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
4293         if (node == NULL) {
4294                 _LOGE("Out of Memory!!!\n");
4295                 return PMINFO_R_ERROR;
4296         }
4297         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
4298                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
4299         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
4300                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
4301         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
4302                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
4303         else if (strcmp(value, "installed_internal") == 0)
4304                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
4305         else if (strcmp(value, "installed_external") == 0)
4306                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
4307         else
4308                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
4309         if (val == NULL) {
4310                 _LOGE("Out of Memory\n");
4311                 free(node);
4312                 return PMINFO_R_ERROR;
4313         }
4314         node->prop = prop;
4315         node->value = val;
4316         /*If API is called multiple times for same property, we should override the previous values.
4317         Last value set will be used for filtering.*/
4318         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4319         if (link)
4320                 filter->list = g_slist_delete_link(filter->list, link);
4321         filter->list = g_slist_append(filter->list, (gpointer)node);
4322         return PMINFO_R_OK;
4323
4324 }
4325
4326 API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count, uid_t uid)
4327 {
4328         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4329         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4330         char *syslocale = NULL;
4331         char *locale = NULL;
4332         char *condition = NULL;
4333         char *error_message = NULL;
4334         char query[MAX_QUERY_LEN] = {'\0'};
4335         char where[MAX_QUERY_LEN] = {'\0'};
4336         GSList *list;
4337         int ret = 0;
4338
4339         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4340         filter->uid = uid;
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
4362         /*Start constructing query*/
4363         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
4364
4365         /*Get where clause*/
4366         for (list = filter->list; list; list = g_slist_next(list)) {
4367                 __get_filter_condition(list->data, &condition);
4368                 if (condition) {
4369                         strncat(where, condition, sizeof(where) - strlen(where) -1);
4370                         where[sizeof(where) - 1] = '\0';
4371                         free(condition);
4372                         condition = NULL;
4373                 }
4374                 if (g_slist_next(list)) {
4375                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
4376                         where[sizeof(where) - 1] = '\0';
4377                 }
4378         }
4379         if (strlen(where) > 0) {
4380                 strncat(query, where, sizeof(query) - strlen(query) - 1);
4381                 query[sizeof(query) - 1] = '\0';
4382         }
4383
4384         /*Execute Query*/
4385         if (SQLITE_OK !=
4386             sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
4387                 _LOGE("Don't execute query = %s error message = %s\n", query,
4388                        error_message);
4389                 sqlite3_free(error_message);
4390                 ret = PMINFO_R_ERROR;
4391                 *count = 0;
4392                 goto err;
4393         }
4394         ret = PMINFO_R_OK;
4395 err:
4396         if (locale) {
4397                 free(locale);
4398                 locale = NULL;
4399         }
4400         if (syslocale) {
4401                 free(syslocale);
4402                 syslocale = NULL;
4403         }
4404         __close_manifest_db();
4405         return ret;
4406 }
4407
4408 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
4409 {
4410         return pkgmgrinfo_pkginfo_usr_filter_count(handle, count, GLOBAL_USER);
4411 }
4412
4413 API int pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
4414                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data, uid_t uid)
4415 {
4416         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4417         retvm_if(pkg_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4418         char *syslocale = NULL;
4419         char *locale = NULL;
4420         char *condition = NULL;
4421         char *error_message = NULL;
4422         char query[MAX_QUERY_LEN] = {'\0'};
4423         char where[MAX_QUERY_LEN] = {'\0'};
4424         GSList *list;
4425         int ret = 0;
4426         label_x *tmp1 = NULL;
4427         icon_x *tmp2 = NULL;
4428         description_x *tmp3 = NULL;
4429         author_x *tmp4 = NULL;
4430         privilege_x *tmp5 = NULL;
4431         pkgmgr_pkginfo_x *node = NULL;
4432         pkgmgr_pkginfo_x *tmphead = NULL;
4433         pkgmgr_pkginfo_x *pkginfo = NULL;
4434
4435         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4436         /*Get current locale*/
4437         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4438         if (syslocale == NULL) {
4439                 _LOGE("current locale is NULL\n");
4440                 return PMINFO_R_ERROR;
4441         }
4442         locale = __convert_system_locale_to_manifest_locale(syslocale);
4443         if (locale == NULL) {
4444                 _LOGE("manifest locale is NULL\n");
4445                 free(syslocale);
4446                 return PMINFO_R_ERROR;
4447         }
4448
4449         ret = __open_manifest_db(uid);
4450         if (ret == -1) {
4451                 _LOGE("Fail to open manifest DB\n");
4452                 free(syslocale);
4453                 free(locale);
4454                 return PMINFO_R_ERROR;
4455         }
4456         /*Start constructing query*/
4457         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, locale);
4458
4459         /*Get where clause*/
4460         for (list = filter->list; list; list = g_slist_next(list)) {
4461                 __get_filter_condition(list->data, &condition);
4462                 if (condition) {
4463                         strncat(where, condition, sizeof(where) - strlen(where) -1);
4464                         where[sizeof(where) - 1] = '\0';
4465                         free(condition);
4466                         condition = NULL;
4467                 }
4468                 if (g_slist_next(list)) {
4469                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
4470                         where[sizeof(where) - 1] = '\0';
4471                 }
4472         }
4473         if (strlen(where) > 0) {
4474                 strncat(query, where, sizeof(query) - strlen(query) - 1);
4475                 query[sizeof(query) - 1] = '\0';
4476         }
4477         tmphead = calloc(1, sizeof(pkgmgr_pkginfo_x));
4478         if (tmphead == NULL) {
4479                 _LOGE("Out of Memory!!!\n");
4480                 ret = PMINFO_R_ERROR;
4481                 goto err;
4482         }
4483
4484         tmphead->uid = uid;
4485         if (SQLITE_OK !=
4486             sqlite3_exec(GET_DB(manifest_db), query, __pkg_list_cb, (void *)tmphead, &error_message)) {
4487                 _LOGE("Don't execute query = %s error message = %s\n", query,
4488                        error_message);
4489                 sqlite3_free(error_message);
4490                 ret = PMINFO_R_ERROR;
4491                 goto err;
4492         }
4493
4494         LISTHEAD(tmphead, node);
4495         for(node = node->next ; node ; node = node->next) {
4496                 pkginfo = node;
4497                 pkginfo->locale = strdup(locale);
4498                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
4499                 if (pkginfo->manifest_info->privileges == NULL) {
4500                         _LOGE("Failed to allocate memory for privileges info\n");
4501                         ret = PMINFO_R_ERROR;
4502                         goto err;
4503                 }
4504
4505                 /*populate manifest_info from DB*/
4506                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
4507                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4508                 if (ret == -1) {
4509                         _LOGE("Package Info DB Information retrieval failed\n");
4510                         ret = PMINFO_R_ERROR;
4511                         goto err;
4512                 }
4513                 memset(query, '\0', MAX_QUERY_LEN);
4514                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
4515                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
4516                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4517                 if (ret == -1) {
4518                         _LOGE("Package Info DB Information retrieval failed\n");
4519                         ret = PMINFO_R_ERROR;
4520                         goto err;
4521                 }
4522                 /*Also store the values corresponding to default locales*/
4523                 memset(query, '\0', MAX_QUERY_LEN);
4524                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
4525                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
4526                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4527                 if (ret == -1) {
4528                         _LOGE("Package Info DB Information retrieval failed\n");
4529                         ret = PMINFO_R_ERROR;
4530                         goto err;
4531                 }
4532                 if (pkginfo->manifest_info->label) {
4533                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
4534                         pkginfo->manifest_info->label = tmp1;
4535                 }
4536                 if (pkginfo->manifest_info->icon) {
4537                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
4538                         pkginfo->manifest_info->icon = tmp2;
4539                 }
4540                 if (pkginfo->manifest_info->description) {
4541                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
4542                         pkginfo->manifest_info->description = tmp3;
4543                 }
4544                 if (pkginfo->manifest_info->author) {
4545                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
4546                         pkginfo->manifest_info->author = tmp4;
4547                 }
4548                 if (pkginfo->manifest_info->privileges->privilege) {
4549                         LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
4550                         pkginfo->manifest_info->privileges->privilege = tmp5;
4551                 }
4552         }
4553
4554         LISTHEAD(tmphead, node);
4555
4556         for(node = node->next ; node ; node = node->next) {
4557                 pkginfo = node;
4558                 pkginfo->uid = uid;
4559                 ret = pkg_cb( (void *)pkginfo, user_data);
4560                 if(ret < 0)
4561                         break;
4562         }
4563         ret = PMINFO_R_OK;
4564
4565 err:
4566         if (locale) {
4567                 free(locale);
4568                 locale = NULL;
4569         }
4570         if (syslocale) {
4571                 free(syslocale);
4572                 syslocale = NULL;
4573         }
4574         __close_manifest_db();
4575         __cleanup_pkginfo(tmphead);
4576         return ret;
4577 }
4578
4579 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
4580                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
4581 {
4582         return pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, pkg_cb, user_data, GLOBAL_USER);
4583 }
4584
4585 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
4586                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
4587 {
4588         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
4589         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
4590         int ret = -1;
4591         privilege_x *ptr = NULL;
4592         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4593         ptr = info->manifest_info->privileges->privilege;
4594         for (; ptr; ptr = ptr->next) {
4595                 if (ptr->text){
4596                         ret = privilege_func(ptr->text, user_data);
4597                         if (ret < 0)
4598                                 break;
4599                 }
4600         }
4601         return PMINFO_R_OK;
4602 }
4603
4604 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
4605                                                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
4606 {
4607         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
4608         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback pointer is NULL");
4609         retvm_if((component != PMINFO_UI_APP) && (component != PMINFO_SVC_APP) && (component != PMINFO_ALL_APP), PMINFO_R_EINVAL, "Invalid App Component Type");
4610
4611         char *syslocale = NULL;
4612         char *locale = NULL;
4613         int ret = -1;
4614         char query[MAX_QUERY_LEN] = {'\0'};
4615         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4616         pkgmgr_pkginfo_x *allinfo = NULL;
4617         pkgmgr_appinfo_x *appinfo = NULL;
4618         icon_x *ptr1 = NULL;
4619         label_x *ptr2 = NULL;
4620         category_x *ptr3 = NULL;
4621         metadata_x *ptr4 = NULL;
4622         permission_x *ptr5 = NULL;
4623         image_x *ptr6 = NULL;
4624         appcontrol_x *ptr7 = NULL;
4625         const char* user_pkg_parser = NULL;
4626
4627         /*get system locale*/
4628         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4629         retvm_if(syslocale == NULL, PMINFO_R_EINVAL, "current locale is NULL");
4630
4631         /*get locale on db*/
4632         locale = __convert_system_locale_to_manifest_locale(syslocale);
4633         tryvm_if(locale == NULL, ret = PMINFO_R_EINVAL, "manifest locale is NULL");
4634
4635         /*calloc allinfo*/
4636         allinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
4637         tryvm_if(allinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
4638
4639         /*calloc manifest_info*/
4640         allinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
4641         tryvm_if(allinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4642
4643         /*calloc appinfo*/
4644         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4645         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
4646
4647         /*set component type*/
4648         if (component == PMINFO_UI_APP)
4649                 appinfo->app_component = PMINFO_UI_APP;
4650         if (component == PMINFO_SVC_APP)
4651                 appinfo->app_component = PMINFO_SVC_APP;
4652         if (component == PMINFO_ALL_APP)
4653                 appinfo->app_component = PMINFO_ALL_APP;
4654
4655         /*open db */
4656         user_pkg_parser = getUserPkgParserDBPathUID(uid);
4657         ret = __open_manifest_db(uid);
4658         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", user_pkg_parser);
4659
4660         appinfo->package = strdup(info->manifest_info->package);
4661         snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4662                         "from package_app_info where " \
4663                         "package='%s' and app_component='%s'",
4664                         info->manifest_info->package,
4665                         (appinfo->app_component==PMINFO_UI_APP ? "uiapp" : "svcapp"));
4666
4667         switch(component) {
4668         case PMINFO_UI_APP:
4669                 /*Populate ui app info */
4670                 ret = __exec_db_query(GET_DB(manifest_db), query, __uiapp_list_cb, (void *)info);
4671                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4672
4673                 uiapplication_x *tmp = NULL;
4674                 if (info->manifest_info->uiapplication) {
4675                         LISTHEAD(info->manifest_info->uiapplication, tmp);
4676                         info->manifest_info->uiapplication = tmp;
4677                 }
4678                 /*Populate localized info for default locales and call callback*/
4679                 /*If the callback func return < 0 we break and no more call back is called*/
4680                 while(tmp != NULL)
4681                 {
4682                         appinfo->locale = strdup(locale);
4683                         appinfo->uiapp_info = tmp;
4684                         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
4685                                 if (locale) {
4686                                         free(locale);
4687                                 }
4688                                 locale = __get_app_locale_by_fallback(GET_DB(manifest_db), appinfo->uiapp_info->appid, syslocale);
4689                         }
4690
4691                         memset(query, '\0', MAX_QUERY_LEN);
4692                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
4693                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4694                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4695
4696                         memset(query, '\0', MAX_QUERY_LEN);
4697                         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);
4698                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4699                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4700
4701                         /*store setting notification icon section*/
4702                         memset(query, '\0', MAX_QUERY_LEN);
4703                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
4704                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4705                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4706                         
4707                         /*store app preview image info*/
4708                         memset(query, '\0', MAX_QUERY_LEN);
4709                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
4710                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4711                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4712
4713                         /*store app control info*/
4714                         snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", appinfo->uiapp_info->appid);
4715                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4716                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
4717
4718                         memset(query, '\0', MAX_QUERY_LEN);
4719                         if (appinfo->uiapp_info->label) {
4720                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
4721                                 appinfo->uiapp_info->label = ptr2;
4722                         }
4723                         if (appinfo->uiapp_info->icon) {
4724                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
4725                                 appinfo->uiapp_info->icon = ptr1;
4726                         }
4727                         if (appinfo->uiapp_info->category) {
4728                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
4729                                 appinfo->uiapp_info->category = ptr3;
4730                         }
4731                         if (appinfo->uiapp_info->metadata) {
4732                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
4733                                 appinfo->uiapp_info->metadata = ptr4;
4734                         }
4735                         if (appinfo->uiapp_info->permission) {
4736                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
4737                                 appinfo->uiapp_info->permission = ptr5;
4738                         }
4739                         if (appinfo->uiapp_info->image) {
4740                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
4741                                 appinfo->uiapp_info->image = ptr6;
4742                         }
4743                         if (appinfo->uiapp_info->appcontrol) {
4744                                 LISTHEAD(appinfo->uiapp_info->appcontrol, ptr7);
4745                                 appinfo->uiapp_info->appcontrol = ptr7;
4746                         }
4747                         ret = app_func((void *)appinfo, user_data);
4748                         if (ret < 0)
4749                                 break;
4750                         tmp = tmp->next;
4751                 }
4752                 break;
4753         case PMINFO_SVC_APP:
4754                 /*Populate svc app info */
4755                 ret = __exec_db_query(GET_DB(manifest_db), query, __svcapp_list_cb, (void *)info);
4756                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4757
4758                 serviceapplication_x *tmp1 = NULL;
4759                 if (info->manifest_info->serviceapplication) {
4760                         LISTHEAD(info->manifest_info->serviceapplication, tmp1);
4761                         info->manifest_info->serviceapplication = tmp1;
4762                 }
4763                 /*Populate localized info for default locales and call callback*/
4764                 /*If the callback func return < 0 we break and no more call back is called*/
4765                 while(tmp1 != NULL)
4766                 {
4767                         appinfo->locale = strdup(locale);
4768                         appinfo->svcapp_info = tmp1;
4769                         memset(query, '\0', MAX_QUERY_LEN);
4770                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4771                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4772                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4773
4774                         memset(query, '\0', MAX_QUERY_LEN);
4775                         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);
4776                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4777                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4778
4779                         snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", appinfo->svcapp_info->appid);
4780                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4781                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
4782
4783                         if (appinfo->svcapp_info->label) {
4784                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4785                                 appinfo->svcapp_info->label = ptr2;
4786                         }
4787                         if (appinfo->svcapp_info->icon) {
4788                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4789                                 appinfo->svcapp_info->icon = ptr1;
4790                         }
4791                         if (appinfo->svcapp_info->category) {
4792                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
4793                                 appinfo->svcapp_info->category = ptr3;
4794                         }
4795                         if (appinfo->svcapp_info->metadata) {
4796                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
4797                                 appinfo->svcapp_info->metadata = ptr4;
4798                         }
4799                         if (appinfo->svcapp_info->permission) {
4800                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
4801                                 appinfo->svcapp_info->permission = ptr5;
4802                         }
4803                         if (appinfo->svcapp_info->appcontrol) {
4804                                 LISTHEAD(appinfo->svcapp_info->appcontrol, ptr7);
4805                                 appinfo->svcapp_info->appcontrol = ptr7;
4806                         }
4807                         ret = app_func((void *)appinfo, user_data);
4808                         if (ret < 0)
4809                                 break;
4810                         tmp1 = tmp1->next;
4811                 }
4812                 break;
4813         case PMINFO_ALL_APP:
4814                 memset(query, '\0', MAX_QUERY_LEN);
4815                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where package='%s'", info->manifest_info->package);
4816
4817                 /*Populate all app info */
4818                 ret = __exec_db_query(GET_DB(manifest_db), query, __allapp_list_cb, (void *)allinfo);
4819                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4820
4821                 /*UI Apps*/
4822                 appinfo->app_component = PMINFO_UI_APP;
4823                 uiapplication_x *tmp2 = NULL;
4824                 if (allinfo->manifest_info->uiapplication) {
4825                         LISTHEAD(allinfo->manifest_info->uiapplication, tmp2);
4826                         allinfo->manifest_info->uiapplication = tmp2;
4827                 }
4828                 /*Populate localized info for default locales and call callback*/
4829                 /*If the callback func return < 0 we break and no more call back is called*/
4830                 while(tmp2 != NULL)
4831                 {
4832                         appinfo->locale = strdup(locale);
4833                         appinfo->uiapp_info = tmp2;
4834                         memset(query, '\0', MAX_QUERY_LEN);
4835                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
4836                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4837                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4838
4839                         memset(query, '\0', MAX_QUERY_LEN);
4840                         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);
4841                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4842                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4843
4844                         /*store setting notification icon section*/
4845                         memset(query, '\0', MAX_QUERY_LEN);
4846                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
4847                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4848                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4849                         
4850                         /*store app preview image info*/
4851                         memset(query, '\0', MAX_QUERY_LEN);
4852                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
4853                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4854                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4855
4856                         /*store app control info*/
4857                         snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", appinfo->uiapp_info->appid);
4858                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4859                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
4860
4861                         if (appinfo->uiapp_info->label) {
4862                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
4863                                 appinfo->uiapp_info->label = ptr2;
4864                         }
4865                         if (appinfo->uiapp_info->icon) {
4866                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
4867                                 appinfo->uiapp_info->icon = ptr1;
4868                         }
4869                         if (appinfo->uiapp_info->category) {
4870                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
4871                                 appinfo->uiapp_info->category = ptr3;
4872                         }
4873                         if (appinfo->uiapp_info->metadata) {
4874                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
4875                                 appinfo->uiapp_info->metadata = ptr4;
4876                         }
4877                         if (appinfo->uiapp_info->permission) {
4878                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
4879                                 appinfo->uiapp_info->permission = ptr5;
4880                         }
4881                         if (appinfo->uiapp_info->image) {
4882                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
4883                                 appinfo->uiapp_info->image = ptr6;
4884                         }
4885                         if (appinfo->uiapp_info->appcontrol) {
4886                                 LISTHEAD(appinfo->uiapp_info->appcontrol, ptr7);
4887                                 appinfo->uiapp_info->appcontrol = ptr7;
4888                         }
4889                         ret = app_func((void *)appinfo, user_data);
4890                         if (ret < 0)
4891                                 break;
4892                         tmp2 = tmp2->next;
4893                 }
4894
4895                 /*SVC Apps*/
4896                 appinfo->app_component = PMINFO_SVC_APP;
4897                 serviceapplication_x *tmp3 = NULL;
4898                 if (allinfo->manifest_info->serviceapplication) {
4899                         LISTHEAD(allinfo->manifest_info->serviceapplication, tmp3);
4900                         allinfo->manifest_info->serviceapplication = tmp3;
4901                 }
4902                 /*Populate localized info for default locales and call callback*/
4903                 /*If the callback func return < 0 we break and no more call back is called*/
4904                 while(tmp3 != NULL)
4905                 {
4906                         appinfo->locale = strdup(locale);
4907                         appinfo->svcapp_info = tmp3;
4908                         memset(query, '\0', MAX_QUERY_LEN);
4909                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4910                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4911                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4912
4913                         memset(query, '\0', MAX_QUERY_LEN);
4914                         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);
4915                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4916                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4917
4918                         snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", appinfo->svcapp_info->appid);
4919                         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
4920                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
4921
4922                         if (appinfo->svcapp_info->label) {
4923                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4924                                 appinfo->svcapp_info->label = ptr2;
4925                         }
4926                         if (appinfo->svcapp_info->icon) {
4927                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4928                                 appinfo->svcapp_info->icon = ptr1;
4929                         }
4930                         if (appinfo->svcapp_info->category) {
4931                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
4932                                 appinfo->svcapp_info->category = ptr3;
4933                         }
4934                         if (appinfo->svcapp_info->metadata) {
4935                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
4936                                 appinfo->svcapp_info->metadata = ptr4;
4937                         }
4938                         if (appinfo->svcapp_info->permission) {
4939                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
4940                                 appinfo->svcapp_info->permission = ptr5;
4941                         }
4942                         if (appinfo->svcapp_info->appcontrol) {
4943                                 LISTHEAD(appinfo->svcapp_info->appcontrol, ptr7);
4944                                 appinfo->svcapp_info->appcontrol = ptr7;
4945                         }
4946                         ret = app_func((void *)appinfo, user_data);
4947                         if (ret < 0)
4948                                 break;
4949                         tmp3 = tmp3->next;
4950                 }
4951                 appinfo->app_component = PMINFO_ALL_APP;
4952                 break;
4953
4954         }
4955
4956         ret = PMINFO_R_OK;
4957 catch:
4958         if (locale) {
4959                 free(locale);
4960                 locale = NULL;
4961         }
4962         if (syslocale) {
4963                 free(syslocale);
4964                 syslocale = NULL;
4965         }
4966         if (appinfo) {
4967                 if (appinfo->package) {
4968                         free((void *)appinfo->package);
4969                         appinfo->package = NULL;
4970                 }
4971                 free(appinfo);
4972                 appinfo = NULL;
4973         }
4974         __cleanup_pkginfo(allinfo);
4975
4976         __close_manifest_db();
4977         return ret;
4978 }
4979
4980 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
4981                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
4982 {
4983         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
4984 }
4985
4986 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
4987 {
4988         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
4989
4990         int ret = PMINFO_R_OK;
4991         char query[MAX_QUERY_LEN] = {'\0'};
4992         pkgmgr_appinfo_x *appinfo = NULL;
4993         uiapplication_x *ptr1 = NULL;
4994         serviceapplication_x *ptr2 = NULL;
4995         const char* user_pkg_parser = NULL;
4996
4997         /*open db*/
4998         user_pkg_parser = getUserPkgParserDBPathUID(uid);
4999         ret = __open_manifest_db(uid);
5000         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", user_pkg_parser);
5001
5002         /*calloc pkginfo*/
5003         pkgmgr_pkginfo_x *info = NULL;
5004         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5005         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5006
5007         /*calloc manifest_info*/
5008         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5009         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5010
5011         /*calloc appinfo*/
5012         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5013         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5014
5015         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
5016         ret = __exec_db_query(GET_DB(manifest_db), query, __mini_appinfo_cb, (void *)info);
5017         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5018
5019         if (info->manifest_info->uiapplication) {
5020                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5021                 info->manifest_info->uiapplication = ptr1;
5022         }
5023         if (info->manifest_info->serviceapplication) {
5024                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5025                 info->manifest_info->serviceapplication = ptr2;
5026         }
5027
5028         /*UI Apps*/
5029         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5030         {
5031                 appinfo->app_component = PMINFO_UI_APP;
5032                 appinfo->package = strdup(ptr1->package);
5033                 appinfo->uiapp_info = ptr1;
5034
5035                 ret = app_func((void *)appinfo, user_data);
5036                 if (ret < 0)
5037                         break;
5038                 free((void *)appinfo->package);
5039                 appinfo->package = NULL;
5040         }
5041         /*Service Apps*/
5042         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5043         {
5044                 appinfo->app_component = PMINFO_SVC_APP;
5045                 appinfo->package = strdup(ptr2->package);
5046                 appinfo->svcapp_info = ptr2;
5047
5048                 ret = app_func((void *)appinfo, user_data);
5049                 if (ret < 0)
5050                         break;
5051                 free((void *)appinfo->package);
5052                 appinfo->package = NULL;
5053         }
5054         ret = PMINFO_R_OK;
5055
5056 catch:
5057         __close_manifest_db();
5058         if (appinfo) {
5059                 free(appinfo);
5060                 appinfo = NULL;
5061         }
5062         __cleanup_pkginfo(info);
5063         return ret;
5064 }
5065
5066 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
5067 {
5068         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
5069 }
5070
5071 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
5072 {
5073         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
5074
5075         int ret = PMINFO_R_OK;
5076         char query[MAX_QUERY_LEN] = {'\0'};
5077         char *syslocale = NULL;
5078         char *locale = NULL;
5079         pkgmgr_appinfo_x *appinfo = NULL;
5080         uiapplication_x *ptr1 = NULL;
5081         serviceapplication_x *ptr2 = NULL;
5082         label_x *tmp1 = NULL;
5083         icon_x *tmp2 = NULL;
5084         category_x *tmp3 = NULL;
5085         metadata_x *tmp4 = NULL;
5086         permission_x *tmp5 = NULL;
5087         image_x *tmp6 = NULL;
5088         appcontrol_x *tmp7 = NULL;
5089         const char *user_pkg_parser = NULL;
5090         pkgmgr_pkginfo_x *info = NULL;
5091
5092         /*get system locale*/
5093         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5094         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
5095
5096         /*get locale on db*/
5097         locale = __convert_system_locale_to_manifest_locale(syslocale);
5098         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
5099
5100         /*open db*/
5101         user_pkg_parser = getUserPkgParserDBPathUID(uid);
5102         ret = __open_manifest_db(uid);
5103         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", user_pkg_parser);
5104
5105         /*calloc pkginfo*/
5106         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5107         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5108
5109         /*calloc manifest_info*/
5110         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5111         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5112
5113         /*calloc appinfo*/
5114         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5115         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5116
5117         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
5118         ret = __exec_db_query(GET_DB(manifest_db), query, __app_list_cb, (void *)info);
5119         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5120
5121         if (info->manifest_info->uiapplication) {
5122                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5123                 info->manifest_info->uiapplication = ptr1;
5124         }
5125         if (info->manifest_info->serviceapplication) {
5126                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5127                 info->manifest_info->serviceapplication = ptr2;
5128         }
5129
5130         /*UI Apps*/
5131         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5132         {
5133                 appinfo->locale = strdup(locale);
5134                 appinfo->app_component = PMINFO_UI_APP;
5135                 appinfo->package = strdup(ptr1->package);
5136                 appinfo->uiapp_info = ptr1;
5137                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5138                                 "from package_app_info where " \
5139                                 "app_id='%s'", ptr1->appid);
5140                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5141                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5142
5143                 if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
5144                         if (locale) {
5145                                 free(locale);
5146                         }
5147                         locale = __get_app_locale_by_fallback(GET_DB(manifest_db), ptr1->appid, syslocale);
5148                 }
5149
5150                 memset(query, '\0', MAX_QUERY_LEN);
5151                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5152                                 "from package_app_localized_info where " \
5153                                 "app_id='%s' and app_locale='%s'",
5154                                 ptr1->appid, locale);
5155                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5156                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5157
5158                 memset(query, '\0', MAX_QUERY_LEN);
5159                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5160                                 "from package_app_localized_info where " \
5161                                 "app_id='%s' and app_locale='%s'",
5162                                 ptr1->appid, DEFAULT_LOCALE);
5163
5164                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5165                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5166
5167                 /*store setting notification icon section*/
5168                 memset(query, '\0', MAX_QUERY_LEN);
5169                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", ptr1->appid);
5170                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5171                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
5172
5173                 /*store app preview image info*/
5174                 memset(query, '\0', MAX_QUERY_LEN);
5175                 snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", ptr1->appid);
5176                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5177                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
5178
5179                 /*store app control info*/
5180                 snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", ptr1->appid);
5181                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5182                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
5183
5184                 if (appinfo->uiapp_info->label) {
5185                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
5186                         appinfo->uiapp_info->label = tmp1;
5187                 }
5188                 if (appinfo->uiapp_info->icon) {
5189                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
5190                         appinfo->uiapp_info->icon= tmp2;
5191                 }
5192                 if (appinfo->uiapp_info->category) {
5193                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
5194                         appinfo->uiapp_info->category = tmp3;
5195                 }
5196                 if (appinfo->uiapp_info->metadata) {
5197                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
5198                         appinfo->uiapp_info->metadata = tmp4;
5199                 }
5200                 if (appinfo->uiapp_info->permission) {
5201                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
5202                         appinfo->uiapp_info->permission = tmp5;
5203                 }
5204                 if (appinfo->uiapp_info->image) {
5205                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
5206                         appinfo->uiapp_info->image = tmp6;
5207                 }
5208                 if (appinfo->uiapp_info->appcontrol) {
5209                         LISTHEAD(appinfo->uiapp_info->appcontrol, tmp7);
5210                         appinfo->uiapp_info->appcontrol = tmp7;
5211                 }
5212                 ret = app_func((void *)appinfo, user_data);
5213                 if (ret < 0)
5214                         break;
5215                 free((void *)appinfo->package);
5216                 appinfo->package = NULL;
5217         }
5218         /*Service Apps*/
5219         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5220         {
5221                 appinfo->locale = strdup(locale);
5222                 appinfo->app_component = PMINFO_SVC_APP;
5223                 appinfo->package = strdup(ptr2->package);
5224                 appinfo->svcapp_info = ptr2;
5225                 memset(query, '\0', MAX_QUERY_LEN);
5226                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5227                                 "from package_app_info where " \
5228                                 "app_id='%s'", ptr2->appid);
5229                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5230                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5231
5232                 memset(query, '\0', MAX_QUERY_LEN);
5233                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5234                                 "from package_app_localized_info where " \
5235                                 "app_id='%s' and app_locale='%s'",
5236                                 ptr2->appid, locale);
5237                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5238                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5239
5240                 memset(query, '\0', MAX_QUERY_LEN);
5241                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5242                                 "from package_app_localized_info where " \
5243                                 "app_id='%s' and app_locale='%s'",
5244                                 ptr2->appid, DEFAULT_LOCALE);
5245                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5246                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5247
5248                 snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", ptr2->appid);
5249                 ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5250                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
5251
5252                 if (appinfo->svcapp_info->label) {
5253                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
5254                         appinfo->svcapp_info->label = tmp1;
5255                 }
5256                 if (appinfo->svcapp_info->icon) {
5257                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
5258                         appinfo->svcapp_info->icon= tmp2;
5259                 }
5260                 if (appinfo->svcapp_info->category) {
5261                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
5262                         appinfo->svcapp_info->category = tmp3;
5263                 }
5264                 if (appinfo->svcapp_info->metadata) {
5265                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
5266                         appinfo->svcapp_info->metadata = tmp4;
5267                 }
5268                 if (appinfo->svcapp_info->permission) {
5269                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
5270                         appinfo->svcapp_info->permission = tmp5;
5271                 }
5272                 if (appinfo->svcapp_info->appcontrol) {
5273                         LISTHEAD(appinfo->svcapp_info->appcontrol, tmp7);
5274                         appinfo->svcapp_info->appcontrol = tmp7;
5275                 }
5276                 ret = app_func((void *)appinfo, user_data);
5277                 if (ret < 0)
5278                         break;
5279                 free((void *)appinfo->package);
5280                 appinfo->package = NULL;
5281         }
5282         ret = PMINFO_R_OK;
5283
5284 catch:
5285         if (locale) {
5286                 free(locale);
5287                 locale = NULL;
5288         }
5289         if (syslocale) {
5290                 free(syslocale);
5291                 syslocale = NULL;
5292         }
5293         __close_manifest_db();
5294         if (appinfo) {
5295                 free(appinfo);
5296                 appinfo = NULL;
5297         }
5298         __cleanup_pkginfo(info);
5299         return ret;
5300 }
5301
5302 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
5303 {
5304         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
5305 }
5306
5307 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid, pkgmgrinfo_appinfo_h *handle)
5308 {
5309         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL");
5310         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5311
5312         pkgmgr_appinfo_x *appinfo = NULL;
5313         char *syslocale = NULL;
5314         char *locale = NULL;
5315         int ret = -1;
5316         int exist = 0;
5317         label_x *tmp1 = NULL;
5318         icon_x *tmp2 = NULL;
5319         category_x *tmp3 = NULL;
5320         metadata_x *tmp4 = NULL;
5321         permission_x *tmp5 = NULL;
5322         image_x *tmp6 = NULL;
5323         appcontrol_x *tmp7 = NULL;
5324         char query[MAX_QUERY_LEN] = {'\0'};
5325         const char* user_pkg_parser = NULL;
5326
5327         *handle = NULL;
5328
5329         /*open db*/
5330         user_pkg_parser = getUserPkgParserDBPathUID(uid);
5331         ret = __open_manifest_db(uid);
5332         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", user_pkg_parser);
5333   
5334         /*check appid exist on db*/
5335         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", appid);
5336         ret = __exec_db_query(GET_DB(manifest_db), query, __validate_cb, (void *)&exist);
5337         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec fail");
5338         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "Appid[%s] for user[%d] is not found in DB", appid, uid);
5339
5340         /*get system locale*/
5341         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5342         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
5343
5344         /*get locale on db*/
5345         locale = __convert_system_locale_to_manifest_locale(syslocale);
5346         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
5347
5348         /*calloc appinfo*/
5349         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5350         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
5351
5352         /*check app_component from DB*/
5353         memset(query, '\0', MAX_QUERY_LEN);
5354         snprintf(query, MAX_QUERY_LEN, "select app_component, package from package_app_info where app_id='%s' ", appid);
5355         ret = __exec_db_query(GET_DB(manifest_db), query, __appcomponent_cb, (void *)appinfo);
5356         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5357
5358         /*calloc app_component*/
5359         if (appinfo->app_component == PMINFO_UI_APP) {
5360                 appinfo->uiapp_info = (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
5361                 tryvm_if(appinfo->uiapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for uiapp info");
5362         } else {
5363                 appinfo->svcapp_info = (serviceapplication_x *)calloc(1, sizeof(serviceapplication_x));
5364                 tryvm_if(appinfo->svcapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for svcapp info");
5365         }
5366         appinfo->locale = strdup(locale);
5367
5368         /*populate app_info from DB*/
5369         memset(query, '\0', MAX_QUERY_LEN);
5370         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' ", appid);
5371         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5372         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5373
5374         memset(query, '\0', MAX_QUERY_LEN);
5375         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, locale);
5376         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5377         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5378
5379         /*Also store the values corresponding to default locales*/
5380         memset(query, '\0', MAX_QUERY_LEN);
5381         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, DEFAULT_LOCALE);
5382         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5383         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5384
5385         /*Populate app category*/
5386         memset(query, '\0', MAX_QUERY_LEN);
5387         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_category where app_id='%s'", appid);
5388         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5389         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Category Info DB Information retrieval failed");
5390
5391         /*Populate app metadata*/
5392         memset(query, '\0', MAX_QUERY_LEN);
5393         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_metadata where app_id='%s'", appid);
5394         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5395         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Metadata Info DB Information retrieval failed");
5396
5397         /*Populate app permission*/
5398         memset(query, '\0', MAX_QUERY_LEN);
5399         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_permission where app_id='%s'", appid);
5400         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5401         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App permission Info DB Information retrieval failed");
5402
5403         /*store setting notification icon section*/
5404         memset(query, '\0', MAX_QUERY_LEN);
5405         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appid);
5406         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5407         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
5408
5409         /*store app preview image info*/
5410         memset(query, '\0', MAX_QUERY_LEN);
5411         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appid);
5412         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5413         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
5414
5415         /*store app control info*/
5416         snprintf(query, MAX_QUERY_LEN, "select app_control from package_app_app_control where app_id='%s'", appinfo->uiapp_info->appid);
5417         ret = __exec_db_query(GET_DB(manifest_db), query, __appinfo_cb, (void *)appinfo);
5418         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App control Info DB Information retrieval failed");
5419
5420         switch (appinfo->app_component) {
5421         case PMINFO_UI_APP:
5422                 if (appinfo->uiapp_info->label) {
5423                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
5424                         appinfo->uiapp_info->label = tmp1;
5425                 }
5426                 if (appinfo->uiapp_info->icon) {
5427                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
5428                         appinfo->uiapp_info->icon = tmp2;
5429                 }
5430                 if (appinfo->uiapp_info->category) {
5431                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
5432                         appinfo->uiapp_info->category = tmp3;
5433                 }
5434                 if (appinfo->uiapp_info->metadata) {
5435                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
5436                         appinfo->uiapp_info->metadata = tmp4;
5437                 }
5438                 if (appinfo->uiapp_info->permission) {
5439                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
5440                         appinfo->uiapp_info->permission = tmp5;
5441                 }
5442                 if (appinfo->uiapp_info->image) {
5443                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
5444                         appinfo->uiapp_info->image = tmp6;
5445                 }
5446                 if (appinfo->uiapp_info->appcontrol) {
5447                         LISTHEAD(appinfo->uiapp_info->appcontrol, tmp7);
5448                         appinfo->uiapp_info->appcontrol = tmp7;
5449                 }
5450                 break;
5451         case PMINFO_SVC_APP:
5452                 if (appinfo->svcapp_info->label) {
5453                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
5454                         appinfo->svcapp_info->label = tmp1;
5455                 }
5456                 if (appinfo->svcapp_info->icon) {
5457                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
5458                         appinfo->svcapp_info->icon = tmp2;
5459                 }
5460                 if (appinfo->svcapp_info->category) {
5461                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
5462                         appinfo->svcapp_info->category = tmp3;
5463                 }
5464                 if (appinfo->svcapp_info->metadata) {
5465                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
5466                         appinfo->svcapp_info->metadata = tmp4;
5467                 }
5468                 if (appinfo->svcapp_info->permission) {
5469                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
5470                         appinfo->svcapp_info->permission = tmp5;
5471                 }
5472                 if (appinfo->svcapp_info->appcontrol) {
5473                         LISTHEAD(appinfo->svcapp_info->appcontrol, tmp7);
5474                         appinfo->svcapp_info->appcontrol = tmp7;
5475                 }
5476                 break;
5477         default:
5478                 break;
5479         }
5480
5481         ret = PMINFO_R_OK;
5482
5483 catch:
5484         if (ret == PMINFO_R_OK)
5485                 *handle = (void*)appinfo;
5486         else {
5487                 *handle = NULL;
5488                 __cleanup_appinfo(appinfo);
5489         }
5490
5491         __close_manifest_db();
5492         if (syslocale) {
5493                 free(syslocale);
5494                 syslocale = NULL;
5495         }
5496         if (locale) {
5497                 free(locale);
5498                 locale = NULL;
5499         }
5500         return ret;
5501 }
5502
5503 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
5504 {
5505         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
5506 }
5507
5508 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
5509 {
5510         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5511
5512         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5513         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5514
5515         switch (info->app_component) {
5516         case PMINFO_UI_APP:
5517                 if (info->uiapp_info == NULL || info->uiapp_info->appid == NULL)
5518                         return PMINFO_R_ERROR;
5519                 *appid = (char *)info->uiapp_info->appid;
5520                 break;
5521         case PMINFO_SVC_APP:
5522                 if (info->svcapp_info == NULL || info->svcapp_info->appid == NULL)
5523                         return PMINFO_R_ERROR;
5524                 *appid = (char *)info->svcapp_info->appid;
5525                 break;
5526         default:
5527                 return PMINFO_R_ERROR;
5528         }
5529
5530         return PMINFO_R_OK;
5531 }
5532
5533 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
5534 {
5535         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5536
5537         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5538         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5539
5540         if (info->package == NULL)
5541                 return PMINFO_R_ERROR;
5542
5543         *pkg_name = (char *)info->package;
5544
5545         return PMINFO_R_OK;
5546 }
5547
5548 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
5549 {
5550         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5551
5552         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5553         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5554
5555         if (info->package == NULL)
5556                 return PMINFO_R_ERROR;
5557
5558         *pkgid = (char *)info->package;
5559
5560         return PMINFO_R_OK;
5561 }
5562
5563 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
5564 {
5565         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5566
5567         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5568         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5569
5570         switch (info->app_component) {
5571         case PMINFO_UI_APP:
5572                 if (info->uiapp_info == NULL || info->uiapp_info->exec == NULL)
5573                         return PMINFO_R_ERROR;
5574                 *exec = (char *)info->uiapp_info->exec;
5575                 break;
5576         case PMINFO_SVC_APP:
5577                 if (info->svcapp_info == NULL || info->svcapp_info->exec == NULL)
5578                         return PMINFO_R_ERROR;
5579                 *exec = (char *)info->svcapp_info->exec;
5580                 break;
5581         default:
5582                 return PMINFO_R_ERROR;
5583         }
5584
5585         return PMINFO_R_OK;
5586 }
5587
5588
5589 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
5590 {
5591         char *locale;
5592         icon_x *ptr;
5593         icon_x *start;
5594         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5595
5596         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5597         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5598
5599         locale = info->locale;
5600         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
5601
5602         if (info->app_component == PMINFO_UI_APP)
5603                 start = info->uiapp_info->icon;
5604         else if (info->app_component == PMINFO_SVC_APP)
5605                 start = info->svcapp_info->icon;
5606         else
5607                 return PMINFO_R_EINVAL;
5608
5609         for (ptr = start; ptr != NULL; ptr = ptr->next) {
5610                 if (ptr->lang == NULL)
5611                         continue;
5612
5613                 if (strcmp(ptr->lang, locale) == 0) {
5614                         *icon = (char *)ptr->text;
5615                         if (strcasecmp(*icon, "(null)") == 0) {
5616                                 locale = DEFAULT_LOCALE;
5617                                 continue;
5618                         } else {
5619                                 return PMINFO_R_OK;
5620                         }
5621                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
5622                         *icon = (char *)ptr->text;
5623                         return PMINFO_R_OK;
5624                 }
5625         }
5626
5627         return PMINFO_R_ERROR;
5628 }
5629
5630
5631 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
5632 {
5633         char *locale;
5634         label_x *ptr;
5635         label_x *start;
5636         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5637
5638         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5639         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5640
5641         locale = info->locale;
5642         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
5643
5644         if (info->app_component == PMINFO_UI_APP)
5645                 start = info->uiapp_info->label;
5646         else if (info->app_component == PMINFO_SVC_APP)
5647                 start = info->svcapp_info->label;
5648         else
5649                 return PMINFO_R_EINVAL;
5650
5651         for (ptr = start; ptr != NULL; ptr = ptr->next) {
5652                 if (ptr->lang == NULL)
5653                         continue;
5654
5655                 if (strcmp(ptr->lang, locale) == 0) {
5656                         *label = (char *)ptr->text;
5657                         if (strcasecmp(*label, "(null)") == 0) {
5658                                 locale = DEFAULT_LOCALE;
5659                                 continue;
5660                         } else {
5661                                 return PMINFO_R_OK;
5662                         }
5663                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
5664                         *label = (char *)ptr->text;
5665                         return PMINFO_R_OK;
5666                 }
5667         }
5668
5669         return PMINFO_R_ERROR;
5670 }
5671
5672 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
5673 {
5674         char *result = NULL;
5675         char *query = NULL;
5676         sqlite3_stmt *stmt = NULL;
5677         sqlite3 *db = NULL;
5678         char *val;
5679         const char *manifest_db;
5680
5681         manifest_db = getUserPkgParserDBPathUID(uid);
5682         if (manifest_db == NULL) {
5683                 _LOGE("Failed to get manifest db path");
5684                 goto err;
5685         }
5686
5687         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
5688                 _LOGE("DB open fail\n");
5689                 goto err;
5690         }
5691
5692         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
5693         if (query == NULL) {
5694                 _LOGE("Out of memory");
5695                 goto err;
5696         }
5697
5698         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
5699                 _LOGE("prepare_v2 fail\n");
5700                 goto err;
5701         }
5702
5703         if (sqlite3_step(stmt) == SQLITE_ROW) {
5704                 val = (char *)sqlite3_column_text(stmt, 0);
5705                 if (val != NULL)
5706                         result = strdup(val);
5707         }
5708
5709 err:
5710         sqlite3_finalize(stmt);
5711         sqlite3_free(query);
5712         sqlite3_close(db);
5713
5714         return result;
5715 }
5716
5717 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
5718 {
5719         char *val;
5720
5721         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
5722
5723         val = _get_localed_label(appid, locale, uid);
5724         if (val == NULL)
5725                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
5726
5727         if (val == NULL)
5728                 return PMINFO_R_ERROR;
5729
5730         *label = val;
5731
5732         return PMINFO_R_OK;
5733 }
5734
5735 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
5736 {
5737         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
5738 }
5739
5740 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
5741 {
5742         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5743
5744         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5745         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5746
5747         if (info->app_component == PMINFO_UI_APP)
5748                 *component = PMINFO_UI_APP;
5749         else if (info->app_component == PMINFO_SVC_APP)
5750                 *component = PMINFO_SVC_APP;
5751         else
5752                 return PMINFO_R_ERROR;
5753
5754         return PMINFO_R_OK;
5755 }
5756
5757 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
5758 {
5759         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5760
5761         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5762         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5763
5764         switch (info->app_component) {
5765         case PMINFO_UI_APP:
5766                 if (info->uiapp_info == NULL || info->uiapp_info->type == NULL)
5767                         return PMINFO_R_ERROR;
5768                 *app_type = (char *)info->uiapp_info->type;
5769                 break;
5770         case PMINFO_SVC_APP:
5771                 if (info->svcapp_info == NULL || info->svcapp_info->type == NULL)
5772                         return PMINFO_R_ERROR;
5773                 *app_type = (char *)info->svcapp_info->type;
5774                 break;
5775         default:
5776                 return PMINFO_R_ERROR;
5777         }
5778
5779         return PMINFO_R_OK;
5780 }
5781
5782 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
5783                                         int *operation_count, char ***operation)
5784 {
5785         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5786         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5787         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5788         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5789         *operation_count = data->operation_count;
5790         *operation = data->operation;
5791         return PMINFO_R_OK;
5792 }
5793
5794 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
5795                                         int *uri_count, char ***uri)
5796 {
5797         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5798         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5799         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5800         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5801         *uri_count = data->uri_count;
5802         *uri = data->uri;
5803         return PMINFO_R_OK;
5804 }
5805
5806 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
5807                                         int *mime_count, char ***mime)
5808 {
5809         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5810         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5811         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5812         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5813         *mime_count = data->mime_count;
5814         *mime = data->mime;
5815         return PMINFO_R_OK;
5816 }
5817
5818 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
5819                                         int *subapp_count, char ***subapp)
5820 {
5821         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5822         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5823         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5824         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5825         *subapp_count = data->subapp_count;
5826         *subapp = data->subapp;
5827         return PMINFO_R_OK;
5828 }
5829
5830 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
5831 {
5832         char *val;
5833         icon_x *ptr;
5834         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5835
5836         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5837         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5838
5839         for (ptr = info->uiapp_info->icon; ptr != NULL; ptr = ptr->next) {
5840                 if (ptr->section == NULL)
5841                         continue;
5842
5843                 val = (char *)ptr->section;
5844                 if (val && strcmp(val, "setting") == 0) {
5845                         *icon = (char *)ptr->text;
5846                         return PMINFO_R_OK;
5847                 }
5848         }
5849
5850         return PMINFO_R_ERROR;
5851 }
5852
5853
5854 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
5855 {
5856         char *val;
5857         icon_x *ptr;
5858         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5859
5860         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5861         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5862
5863         for (ptr = info->uiapp_info->icon; ptr != NULL; ptr = ptr->next) {
5864                 if (ptr->section == NULL)
5865                         continue;
5866
5867                 val = (char *)ptr->section;
5868                 if (val && strcmp(val, "notification") == 0){
5869                         *icon = (char *)ptr->text;
5870                         return PMINFO_R_OK;
5871                 }
5872         }
5873
5874         return PMINFO_R_ERROR;
5875 }
5876
5877 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
5878 {
5879         char *val;
5880         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5881
5882         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5883         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5884
5885         if (info->uiapp_info == NULL || info->uiapp_info->recentimage == NULL)
5886                 return PMINFO_R_ERROR;
5887
5888         val = (char *)info->uiapp_info->recentimage;
5889         if (strcasecmp(val, "capture") == 0)
5890                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
5891         else if (strcasecmp(val, "icon") == 0)
5892                 *type = PMINFO_RECENTIMAGE_USE_ICON;
5893         else
5894                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
5895
5896         return PMINFO_R_OK;
5897 }
5898
5899 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
5900 {
5901         char *val;
5902         image_x *ptr;
5903         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5904
5905         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5906         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5907
5908         for (ptr = info->uiapp_info->image; ptr != NULL; ptr = ptr->next) {
5909                 if (ptr->section == NULL)
5910                         continue;
5911
5912                 val = (char *)ptr->section;
5913                 if (val && strcmp(val, "preview") == 0) {
5914                         *preview_img = (char *)ptr->text;
5915                         return PMINFO_R_OK;
5916                 }
5917         }
5918
5919         return PMINFO_R_ERROR;
5920 }
5921
5922 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
5923 {
5924         const char *val;
5925         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5926
5927         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5928         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5929
5930         if (info->app_component == PMINFO_UI_APP)
5931                 val = info->uiapp_info->permission_type;
5932         else if (info->app_component == PMINFO_SVC_APP)
5933                 val = info->svcapp_info->permission_type;
5934         else
5935                 return PMINFO_R_ERROR;
5936
5937         if (val == NULL)
5938                 return PMINFO_R_ERROR;
5939
5940         if (strcmp(val, "signature") == 0)
5941                 *permission = PMINFO_PERMISSION_SIGNATURE;
5942         else if (strcmp(val, "privilege") == 0)
5943                 *permission = PMINFO_PERMISSION_PRIVILEGE;
5944         else
5945                 *permission = PMINFO_PERMISSION_NORMAL;
5946
5947         return PMINFO_R_OK;
5948 }
5949
5950 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
5951 {
5952         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5953
5954         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5955         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5956
5957         if (info->uiapp_info == NULL || info->uiapp_info->component_type == NULL)
5958                 return PMINFO_R_ERROR;
5959
5960         *component_type = (char *)info->uiapp_info->component_type;
5961
5962         return PMINFO_R_OK;
5963 }
5964
5965 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
5966 {
5967         char *val;
5968         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5969
5970         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5971         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5972
5973         if (info->uiapp_info == NULL || info->uiapp_info->hwacceleration == NULL)
5974                 return PMINFO_R_ERROR;
5975
5976         val = (char *)info->uiapp_info->hwacceleration;
5977         if (strcasecmp(val, "not-use-GL") == 0)
5978                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
5979         else if (strcasecmp(val, "use-GL") == 0)
5980                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
5981         else
5982                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
5983
5984         return PMINFO_R_OK;
5985 }
5986
5987 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
5988 {
5989         char *val;
5990         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5991
5992         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5993         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5994
5995         if (info->uiapp_info == NULL || info->uiapp_info->screenreader == NULL)
5996                 return PMINFO_R_ERROR;
5997
5998         val = (char *)info->uiapp_info->screenreader;
5999         if (strcasecmp(val, "screenreader-off") == 0)
6000                 *screenreader = PMINFO_SCREENREADER_OFF;
6001         else if (strcasecmp(val, "screenreader-on") == 0)
6002                 *screenreader = PMINFO_SCREENREADER_ON;
6003         else
6004                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
6005
6006         return PMINFO_R_OK;
6007 }
6008
6009 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
6010 {
6011         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6012
6013         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6014         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6015         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6016
6017         if (info->app_component != PMINFO_UI_APP || info->uiapp_info == NULL ||
6018                         info->uiapp_info->portraitimg || info->uiapp_info->landscapeimg == NULL)
6019                 return PMINFO_R_ERROR;
6020
6021         *portrait_img = (char *)info->uiapp_info->portraitimg;
6022         *landscape_img = (char *)info->uiapp_info->landscapeimg;
6023
6024         return PMINFO_R_OK;
6025 }
6026
6027 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
6028 {
6029         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6030
6031         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6032         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6033
6034         if (info->uiapp_info == NULL || info->uiapp_info->submode_mainid == NULL)
6035                 return PMINFO_R_ERROR;
6036
6037         *submode_mainid = (char *)info->uiapp_info->submode_mainid;
6038
6039         return PMINFO_R_OK;
6040 }
6041
6042 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
6043 {
6044         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6045
6046         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6047         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6048
6049         if (info->app_component != PMINFO_UI_APP)
6050                 return PMINFO_R_EINVAL;
6051
6052         if (info->uiapp_info->launch_mode == NULL)
6053                 return PMINFO_R_ERROR;
6054
6055         *mode = (char *)(info->uiapp_info->launch_mode);
6056
6057         return PMINFO_R_OK;
6058 }
6059
6060 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
6061 {
6062         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6063         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6064         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6065         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6066
6067         int ret = PMINFO_R_OK;
6068         char *query = NULL;
6069         sqlite3_stmt *stmt = NULL;
6070
6071         /*open db*/
6072         ret = __open_manifest_db(uid);
6073         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
6074
6075         /*Start constructing query*/
6076         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
6077
6078         /*prepare query*/
6079         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
6080         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
6081
6082         /*step query*/
6083         ret = sqlite3_step(stmt);
6084         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
6085
6086         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
6087         *access = strdup((char *)sqlite3_column_text(stmt, 2));
6088
6089         ret = PMINFO_R_OK;
6090
6091 catch:
6092         sqlite3_free(query);
6093         sqlite3_finalize(stmt);
6094         __close_manifest_db();
6095         return ret;
6096 }
6097
6098 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
6099 {
6100         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
6101 }
6102
6103 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
6104 {
6105         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
6106         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6107
6108         int ret = PMINFO_R_OK;
6109         char *query = NULL;
6110         sqlite3_stmt *stmt = NULL;
6111
6112         /*open db*/
6113         ret = __open_manifest_db(uid);
6114         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
6115
6116         /*Start constructing query*/
6117         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
6118
6119         /*prepare query*/
6120         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
6121         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
6122
6123         /*step query*/
6124         ret = sqlite3_step(stmt);
6125         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
6126
6127         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
6128
6129         ret = PMINFO_R_OK;
6130
6131 catch:
6132         sqlite3_free(query);
6133         sqlite3_finalize(stmt);
6134         __close_manifest_db();
6135         return ret;
6136 }
6137
6138 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
6139 {
6140         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
6141 }
6142
6143 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
6144                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
6145 {
6146         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6147         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6148         int ret = -1;
6149         permission_x *ptr = NULL;
6150         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6151         if (info->app_component == PMINFO_UI_APP)
6152                 ptr = info->uiapp_info->permission;
6153         else if (info->app_component == PMINFO_SVC_APP)
6154                 ptr = info->svcapp_info->permission;
6155         else
6156                 return PMINFO_R_EINVAL;
6157         for (; ptr; ptr = ptr->next) {
6158                 if (ptr->value) {
6159                         ret = permission_func(ptr->value, user_data);
6160                         if (ret < 0)
6161                                 break;
6162                 }
6163         }
6164         return PMINFO_R_OK;
6165 }
6166
6167 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
6168                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
6169 {
6170         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6171         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6172         int ret = -1;
6173         category_x *ptr = NULL;
6174         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6175         if (info->app_component == PMINFO_UI_APP)
6176                 ptr = info->uiapp_info->category;
6177         else if (info->app_component == PMINFO_SVC_APP)
6178                 ptr = info->svcapp_info->category;
6179         else
6180                 return PMINFO_R_EINVAL;
6181         for (; ptr; ptr = ptr->next) {
6182                 if (ptr->name) {
6183                         ret = category_func(ptr->name, user_data);
6184                         if (ret < 0)
6185                                 break;
6186                 }
6187         }
6188         return PMINFO_R_OK;
6189 }
6190
6191 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
6192                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
6193 {
6194         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6195         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6196         int ret = -1;
6197         metadata_x *ptr = NULL;
6198         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6199         if (info->app_component == PMINFO_UI_APP)
6200                 ptr = info->uiapp_info->metadata;
6201         else if (info->app_component == PMINFO_SVC_APP)
6202                 ptr = info->svcapp_info->metadata;
6203         else
6204                 return PMINFO_R_EINVAL;
6205         for (; ptr; ptr = ptr->next) {
6206                 if (ptr->key) {
6207                         ret = metadata_func(ptr->key, ptr->value, user_data);
6208                         if (ret < 0)
6209                                 break;
6210                 }
6211         }
6212         return PMINFO_R_OK;
6213 }
6214
6215 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
6216                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
6217 {
6218         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6219         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6220         int ret;
6221         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6222         appcontrol_x *appcontrol;
6223         pkgmgrinfo_app_component component;
6224         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
6225         if (ret < 0) {
6226                 _LOGE("Failed to get app component name\n");
6227                 return PMINFO_R_ERROR;
6228         }
6229         switch (component) {
6230         case PMINFO_UI_APP:
6231                 if (info->uiapp_info == NULL)
6232                         return PMINFO_R_EINVAL;
6233                 appcontrol = info->uiapp_info->appcontrol;
6234                 break;
6235         case PMINFO_SVC_APP:
6236                 if (info->svcapp_info == NULL)
6237                         return PMINFO_R_EINVAL;
6238                 appcontrol = info->svcapp_info->appcontrol;
6239                 break;
6240         default:
6241                 return PMINFO_R_EINVAL;
6242         }
6243         for (; appcontrol; appcontrol = appcontrol->next) {
6244                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
6245                 if (ret < 0)
6246                         break;
6247         }
6248
6249         return PMINFO_R_OK;
6250 }
6251
6252 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h  handle, bool *nodisplay)
6253 {
6254         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6255         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6256         char *val = NULL;
6257         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6258         val = (char *)info->uiapp_info->nodisplay;
6259         if (val) {
6260                 if (strcasecmp(val, "true") == 0)
6261                         *nodisplay = 1;
6262                 else if (strcasecmp(val, "false") == 0)
6263                         *nodisplay = 0;
6264                 else
6265                         *nodisplay = 0;
6266         }
6267         return PMINFO_R_OK;
6268 }
6269
6270 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h  handle, bool *multiple)
6271 {
6272         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6273         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6274         char *val = NULL;
6275         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6276         val = (char *)info->uiapp_info->multiple;
6277         if (val) {
6278                 if (strcasecmp(val, "true") == 0)
6279                         *multiple = 1;
6280                 else if (strcasecmp(val, "false") == 0)
6281                         *multiple = 0;
6282                 else
6283                         *multiple = 0;
6284         }
6285         return PMINFO_R_OK;
6286 }
6287
6288 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
6289 {
6290         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6291         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6292         char *val = NULL;
6293         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6294         val = (char *)info->uiapp_info->indicatordisplay;
6295         if (val) {
6296                 if (strcasecmp(val, "true") == 0){
6297                         *indicator_disp = 1;
6298                 }else if (strcasecmp(val, "false") == 0){
6299                         *indicator_disp = 0;
6300                 }else{
6301                         *indicator_disp = 0;
6302                 }
6303         }
6304         return PMINFO_R_OK;
6305 }
6306
6307 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
6308 {
6309         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6310         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6311         char *val = NULL;
6312         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6313         val = (char *)info->uiapp_info->taskmanage;
6314         if (val) {
6315                 if (strcasecmp(val, "true") == 0)
6316                         *taskmanage = 1;
6317                 else if (strcasecmp(val, "false") == 0)
6318                         *taskmanage = 0;
6319                 else
6320                         *taskmanage = 0;
6321         }
6322         return PMINFO_R_OK;
6323 }
6324
6325 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
6326 {
6327         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6328         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6329         char *val = NULL;
6330         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6331         if (info->app_component == PMINFO_UI_APP)
6332                 val = (char *)info->uiapp_info->enabled;
6333         else if (info->app_component == PMINFO_SVC_APP)
6334                 val = (char *)info->uiapp_info->enabled;
6335         else {
6336                 _LOGE("invalid component type\n");
6337                 return PMINFO_R_EINVAL;
6338         }
6339
6340         if (val) {
6341                 if (strcasecmp(val, "true") == 0)
6342                         *enabled = 1;
6343                 else if (strcasecmp(val, "false") == 0)
6344                         *enabled = 0;
6345                 else
6346                         *enabled = 1;
6347         }
6348         return PMINFO_R_OK;
6349
6350 }
6351
6352 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
6353 {
6354         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6355         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6356         char *val = NULL;
6357         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6358         val = (char *)info->svcapp_info->onboot;
6359         if (val) {
6360                 if (strcasecmp(val, "true") == 0)
6361                         *onboot = 1;
6362                 else if (strcasecmp(val, "false") == 0)
6363                         *onboot = 0;
6364                 else
6365                         *onboot = 0;
6366         }
6367         return PMINFO_R_OK;
6368 }
6369
6370 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
6371 {
6372         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6373         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6374         char *val = NULL;
6375         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6376         val = (char *)info->svcapp_info->autorestart;
6377         if (val) {
6378                 if (strcasecmp(val, "true") == 0)
6379                         *autorestart = 1;
6380                 else if (strcasecmp(val, "false") == 0)
6381                         *autorestart = 0;
6382                 else
6383                         *autorestart = 0;
6384         }
6385         return PMINFO_R_OK;
6386 }
6387
6388 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
6389 {
6390         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6391         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6392         char *val = NULL;
6393         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6394         val = (char *)info->uiapp_info->mainapp;
6395         if (val) {
6396                 if (strcasecmp(val, "true") == 0)
6397                         *mainapp = 1;
6398                 else if (strcasecmp(val, "false") == 0)
6399                         *mainapp = 0;
6400                 else
6401                         *mainapp = 0;
6402         }
6403         return PMINFO_R_OK;
6404 }
6405
6406 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
6407 {
6408         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6409         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6410         char *val = NULL;
6411         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6412         val = (char *)info->uiapp_info->preload;
6413         if (val) {
6414                 if (strcasecmp(val, "true") == 0)
6415                         *preload = 1;
6416                 else if (strcasecmp(val, "false") == 0)
6417                         *preload = 0;
6418                 else
6419                         *preload = 0;
6420         }
6421         return PMINFO_R_OK;
6422 }
6423
6424 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
6425 {
6426         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6427         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6428         char *val = NULL;
6429         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6430         val = (char *)info->uiapp_info->submode;
6431         if (val) {
6432                 if (strcasecmp(val, "true") == 0)
6433                         *submode = 1;
6434                 else if (strcasecmp(val, "false") == 0)
6435                         *submode = 0;
6436                 else
6437                         *submode = 0;
6438         }
6439         return PMINFO_R_OK;
6440 }
6441
6442 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
6443 {
6444         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6445         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
6446         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
6447
6448         category_x *ptr = NULL;
6449         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6450
6451         *exist = 0;
6452
6453         ptr = info->uiapp_info->category;
6454
6455         for (; ptr; ptr = ptr->next) {
6456                 if (ptr->name) {
6457                         if (strcasecmp(ptr->name, category) == 0)
6458                         {
6459                                 *exist = 1;
6460                                 break;
6461                         }
6462                 }
6463         }
6464
6465         return PMINFO_R_OK;
6466 }
6467
6468 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h  handle)
6469 {
6470         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6471         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6472         __cleanup_appinfo(info);
6473         return PMINFO_R_OK;
6474 }
6475
6476 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
6477 {
6478         return (pkgmgrinfo_pkginfo_filter_create(handle));
6479 }
6480
6481 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
6482 {
6483         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
6484 }
6485
6486 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
6487                                 const char *property, const int value)
6488 {
6489         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6490         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6491         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
6492         char *val = NULL;
6493         GSList *link = NULL;
6494         int prop = -1;
6495         prop = _pminfo_appinfo_convert_to_prop_int(property);
6496         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
6497                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
6498                 _LOGE("Invalid Integer Property\n");
6499                 return PMINFO_R_EINVAL;
6500         }
6501         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6502         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6503         if (node == NULL) {
6504                 _LOGE("Out of Memory!!!\n");
6505                 return PMINFO_R_ERROR;
6506         }
6507         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
6508         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
6509         if (val == NULL) {
6510                 _LOGE("Out of Memory\n");
6511                 free(node);
6512                 node = NULL;
6513                 return PMINFO_R_ERROR;
6514         }
6515         node->prop = prop;
6516         node->value = val;
6517         /*If API is called multiple times for same property, we should override the previous values.
6518         Last value set will be used for filtering.*/
6519         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6520         if (link)
6521                 filter->list = g_slist_delete_link(filter->list, link);
6522         filter->list = g_slist_append(filter->list, (gpointer)node);
6523         return PMINFO_R_OK;
6524
6525 }
6526
6527 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
6528                                 const char *property, const bool value)
6529 {
6530         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6531         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6532         char *val = NULL;
6533         GSList *link = NULL;
6534         int prop = -1;
6535         prop = _pminfo_appinfo_convert_to_prop_bool(property);
6536         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
6537                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
6538                 _LOGE("Invalid Boolean Property\n");
6539                 return PMINFO_R_EINVAL;
6540         }
6541         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6542         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6543         if (node == NULL) {
6544                 _LOGE("Out of Memory!!!\n");
6545                 return PMINFO_R_ERROR;
6546         }
6547         if (value)
6548                 val = strndup("('true','True')", 15);
6549         else
6550                 val = strndup("('false','False')", 17);
6551         if (val == NULL) {
6552                 _LOGE("Out of Memory\n");
6553                 free(node);
6554                 node = NULL;
6555                 return PMINFO_R_ERROR;
6556         }
6557         node->prop = prop;
6558         node->value = val;
6559         /*If API is called multiple times for same property, we should override the previous values.
6560         Last value set will be used for filtering.*/
6561         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6562         if (link)
6563                 filter->list = g_slist_delete_link(filter->list, link);
6564         filter->list = g_slist_append(filter->list, (gpointer)node);
6565         return PMINFO_R_OK;
6566
6567 }
6568
6569 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
6570                                 const char *property, const char *value)
6571 {
6572         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6573         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6574         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6575         char *val = NULL;
6576         pkgmgrinfo_node_x *ptr = NULL;
6577         char prev[PKG_STRING_LEN_MAX] = {'\0'};
6578         char temp[PKG_STRING_LEN_MAX] = {'\0'};
6579         GSList *link = NULL;
6580         int prop = -1;
6581         prop = _pminfo_appinfo_convert_to_prop_str(property);
6582         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
6583                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
6584                 _LOGE("Invalid String Property\n");
6585                 return PMINFO_R_EINVAL;
6586         }
6587         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6588         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6589         if (node == NULL) {
6590                 _LOGE("Out of Memory!!!\n");
6591                 return PMINFO_R_ERROR;
6592         }
6593         node->prop = prop;
6594         switch (prop) {
6595         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
6596                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
6597                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
6598                 else
6599                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
6600                 node->value = val;
6601                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6602                 if (link)
6603                         filter->list = g_slist_delete_link(filter->list, link);
6604                 filter->list = g_slist_append(filter->list, (gpointer)node);
6605                 break;
6606         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
6607         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
6608         case E_PMINFO_APPINFO_PROP_APP_URI:
6609         case E_PMINFO_APPINFO_PROP_APP_MIME:
6610                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
6611                 if (val == NULL) {
6612                         _LOGE("Out of Memory\n");
6613                         free(node);
6614                         node = NULL;
6615                         return PMINFO_R_ERROR;
6616                 }
6617                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6618                 if (link) {
6619                         ptr = (pkgmgrinfo_node_x *)link->data;
6620                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
6621                         _LOGE("Previous value is %s\n", prev);
6622                         filter->list = g_slist_delete_link(filter->list, link);
6623                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
6624                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6625                         _LOGE("New value is %s\n", val);
6626                         node->value = val;
6627                         filter->list = g_slist_append(filter->list, (gpointer)node);
6628                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6629                 } else {
6630                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
6631                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6632                         _LOGE("First value is %s\n", val);
6633                         node->value = val;
6634                         filter->list = g_slist_append(filter->list, (gpointer)node);
6635                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6636                 }
6637                 break;
6638         default:
6639                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
6640                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6641                 if (link)
6642                         filter->list = g_slist_delete_link(filter->list, link);
6643                 filter->list = g_slist_append(filter->list, (gpointer)node);
6644                 break;
6645         }
6646         return PMINFO_R_OK;
6647 }
6648
6649 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
6650 {
6651         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6652         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6653         char *syslocale = NULL;
6654         char *locale = NULL;
6655         char *condition = NULL;
6656         char *error_message = NULL;
6657         char query[MAX_QUERY_LEN] = {'\0'};
6658         char where[MAX_QUERY_LEN] = {'\0'};
6659         GSList *list;
6660         int ret = 0;
6661
6662         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6663         /*Get current locale*/
6664         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6665         if (syslocale == NULL) {
6666                 _LOGE("current locale is NULL\n");
6667                 return PMINFO_R_ERROR;
6668         }
6669         locale = __convert_system_locale_to_manifest_locale(syslocale);
6670         if (locale == NULL) {
6671                 _LOGE("manifest locale is NULL\n");
6672                 free(syslocale);
6673                 return PMINFO_R_ERROR;
6674         }
6675
6676         ret = __open_manifest_db(uid);
6677         if (ret == -1) {
6678                 _LOGE("Fail to open manifest DB\n");
6679                 free(syslocale);
6680                 free(locale);
6681                 return PMINFO_R_ERROR;
6682         }
6683
6684         /*Start constructing query*/
6685         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
6686
6687         /*Get where clause*/
6688         for (list = filter->list; list; list = g_slist_next(list)) {
6689                 __get_filter_condition(list->data, &condition);
6690                 if (condition) {
6691                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6692                         where[sizeof(where) - 1] = '\0';
6693                         free(condition);
6694                         condition = NULL;
6695                 }
6696                 if (g_slist_next(list)) {
6697                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6698                         where[sizeof(where) - 1] = '\0';
6699                 }
6700         }
6701         if (strlen(where) > 0) {
6702                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6703                 query[sizeof(query) - 1] = '\0';
6704         }
6705
6706         /*Execute Query*/
6707         if (SQLITE_OK !=
6708             sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
6709                 _LOGE("Don't execute query = %s error message = %s\n", query,
6710                        error_message);
6711                 sqlite3_free(error_message);
6712                 ret = PMINFO_R_ERROR;
6713                 *count = 0;
6714                 goto err;
6715         }
6716         ret = PMINFO_R_OK;
6717 err:
6718         if (locale) {
6719                 free(locale);
6720                 locale = NULL;
6721         }
6722         if (syslocale) {
6723                 free(syslocale);
6724                 syslocale = NULL;
6725         }
6726         __close_manifest_db();
6727         return ret;
6728 }
6729
6730 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
6731 {
6732         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
6733 }
6734
6735 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
6736                                 pkgmgrinfo_app_list_cb app_cb, void * user_data, uid_t uid)
6737 {
6738         char *syslocale;
6739         char *locale;
6740         char *condition;
6741         char *error_message;
6742         char query[MAX_QUERY_LEN] = {'\0'};
6743         char where[MAX_QUERY_LEN] = {'\0'};
6744         GSList *list;
6745         int ret;
6746         uiapplication_x *ptr1;
6747         serviceapplication_x *ptr2;
6748         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6749         pkgmgr_pkginfo_x *info;
6750         pkgmgr_pkginfo_x *filtinfo = NULL;
6751         pkgmgr_appinfo_x *appinfo = NULL;
6752
6753         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6754         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6755
6756         /*Get current locale*/
6757         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6758         if (syslocale == NULL) {
6759                 _LOGE("current locale is NULL\n");
6760                 return PMINFO_R_ERROR;
6761         }
6762         locale = __convert_system_locale_to_manifest_locale(syslocale);
6763         if (locale == NULL) {
6764                 _LOGE("manifest locale is NULL\n");
6765                 free(syslocale);
6766                 return PMINFO_R_ERROR;
6767         }
6768
6769         ret = __open_manifest_db(uid);
6770         if (ret == -1) {
6771                 _LOGE("Fail to open manifest DB\n");
6772                 free(syslocale);
6773                 free(locale);
6774                 return PMINFO_R_ERROR;
6775         }
6776         /*Start constructing query*/
6777         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_APP, locale);
6778         /*Get where clause*/
6779         for (list = filter->list; list; list = g_slist_next(list)) {
6780                 __get_filter_condition(list->data, &condition);
6781                 if (condition) {
6782                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6783                         where[sizeof(where) - 1] = '\0';
6784                         free(condition);
6785                         condition = NULL;
6786                 }
6787                 if (g_slist_next(list)) {
6788                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6789                         where[sizeof(where) - 1] = '\0';
6790                 }
6791         }
6792         if (strlen(where) > 0) {
6793                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6794                 query[sizeof(query) - 1] = '\0';
6795         }
6796         /*To get filtered list*/
6797         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6798         if (info == NULL) {
6799                 _LOGE("Out of Memory!!!\n");
6800                 ret = PMINFO_R_ERROR;
6801                 goto err;
6802         }
6803         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6804         if (info->manifest_info == NULL) {
6805                 _LOGE("Out of Memory!!!\n");
6806                 ret = PMINFO_R_ERROR;
6807                 goto err;
6808         }
6809         /*To get detail app info for each member of filtered list*/
6810         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6811         if (filtinfo == NULL) {
6812                 _LOGE("Out of Memory!!!\n");
6813                 ret = PMINFO_R_ERROR;
6814                 goto err;
6815         }
6816         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6817         if (filtinfo->manifest_info == NULL) {
6818                 _LOGE("Out of Memory!!!\n");
6819                 ret = PMINFO_R_ERROR;
6820                 goto err;
6821         }
6822         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
6823         if (appinfo == NULL) {
6824                 _LOGE("Out of Memory!!!\n");
6825                 ret = PMINFO_R_ERROR;
6826                 goto err;
6827         }
6828         if (SQLITE_OK !=
6829             sqlite3_exec(GET_DB(manifest_db), query, __app_list_cb, (void *)info, &error_message)) {
6830                 _LOGE("Don't execute query = %s error message = %s\n", query,
6831                        error_message);
6832                 sqlite3_free(error_message);
6833                 ret = PMINFO_R_ERROR;
6834                 goto err;
6835         }
6836         memset(query, '\0', MAX_QUERY_LEN);
6837         if (info->manifest_info->uiapplication) {
6838                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
6839                 info->manifest_info->uiapplication = ptr1;
6840         }
6841         if (info->manifest_info->serviceapplication) {
6842                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
6843                 info->manifest_info->serviceapplication = ptr2;
6844         }
6845         /*Filtered UI Apps*/
6846         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
6847         {
6848                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6849                                                         ptr1->appid, "uiapp");
6850                 if (SQLITE_OK !=
6851                 sqlite3_exec(GET_DB(manifest_db), query, __uiapp_list_cb, (void *)filtinfo, &error_message)) {
6852                         _LOGE("Don't execute query = %s error message = %s\n", query,
6853                                error_message);
6854                         sqlite3_free(error_message);
6855                         ret = PMINFO_R_ERROR;
6856                         goto err;
6857                 }
6858         }
6859         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
6860         {
6861                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6862                                                         ptr2->appid, "svcapp");
6863                 if (SQLITE_OK !=
6864                 sqlite3_exec(GET_DB(manifest_db), query, __svcapp_list_cb, (void *)filtinfo, &error_message)) {
6865                         _LOGE("Don't execute query = %s error message = %s\n", query,
6866                                error_message);
6867                         sqlite3_free(error_message);
6868                         ret = PMINFO_R_ERROR;
6869                         goto err;
6870                 }
6871         }
6872         if (filtinfo->manifest_info->uiapplication) {
6873                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
6874                 filtinfo->manifest_info->uiapplication = ptr1;
6875         }
6876         /*If the callback func return < 0 we break and no more call back is called*/
6877         while(ptr1 != NULL)
6878         {
6879                 appinfo->package = strdup(ptr1->package);
6880                 appinfo->locale = strdup(locale);
6881                 appinfo->uiapp_info = ptr1;
6882                 appinfo->app_component = PMINFO_UI_APP;
6883                 ret = app_cb((void *)appinfo, user_data);
6884                 if (ret < 0)
6885                         break;
6886                 ptr1 = ptr1->next;
6887         }
6888         /*Filtered Service Apps*/
6889         if (filtinfo->manifest_info->serviceapplication) {
6890                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
6891                 filtinfo->manifest_info->serviceapplication = ptr2;
6892         }
6893         /*If the callback func return < 0 we break and no more call back is called*/
6894         while(ptr2 != NULL)
6895         {
6896                 appinfo->package = strdup(ptr2->package);
6897                 appinfo->locale = strdup(locale);
6898                 appinfo->svcapp_info = ptr2;
6899                 appinfo->app_component = PMINFO_SVC_APP;
6900                 ret = app_cb((void *)appinfo, user_data);
6901                 if (ret < 0)
6902                         break;
6903                 ptr2 = ptr2->next;
6904         }
6905         ret = PMINFO_R_OK;
6906 err:
6907         if (locale) {
6908                 free(locale);
6909                 locale = NULL;
6910         }
6911         if (syslocale) {
6912                 free(syslocale);
6913                 syslocale = NULL;
6914         }
6915         __close_manifest_db();
6916         if (appinfo) {
6917                 free(appinfo);
6918                 appinfo = NULL;
6919         }
6920         __cleanup_pkginfo(info);
6921         __cleanup_pkginfo(filtinfo);
6922         return ret;
6923 }
6924
6925 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
6926                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
6927 {
6928         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
6929 }
6930
6931 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
6932 {
6933         return (pkgmgrinfo_pkginfo_filter_create(handle));
6934 }
6935
6936 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
6937 {
6938         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
6939 }
6940
6941 API int pkgmgrinfo_appinfo_metadata_filter_add(pkgmgrinfo_appinfo_metadata_filter_h handle,
6942                 const char *key, const char *value)
6943 {
6944         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
6945         retvm_if(key == NULL, PMINFO_R_EINVAL, "metadata key supplied is NULL\n");
6946         /*value can be NULL. In that case all apps with specified key should be displayed*/
6947         int ret = 0;
6948         char *k = NULL;
6949         char *v = NULL;
6950         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6951         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6952         retvm_if(node == NULL, PMINFO_R_ERROR, "Out of Memory!!!\n");
6953         k = strdup(key);
6954         tryvm_if(k == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6955         node->key = k;
6956         if (value) {
6957                 v = strdup(value);
6958                 tryvm_if(v == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6959         }
6960         node->value = v;
6961         /*If API is called multiple times, we should OR all conditions.*/
6962         filter->list = g_slist_append(filter->list, (gpointer)node);
6963         /*All memory will be freed in destroy API*/
6964         return PMINFO_R_OK;
6965 catch:
6966         if (node) {
6967                 if (node->key) {
6968                         free(node->key);
6969                         node->key = NULL;
6970                 }
6971                 if (node->value) {
6972                         free(node->value);
6973                         node->value = NULL;
6974                 }
6975                 free(node);
6976                 node = NULL;
6977         }
6978         return ret;
6979 }
6980
6981 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
6982                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
6983 {
6984         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
6985         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Callback function supplied is NULL\n");
6986         char *syslocale = NULL;
6987         char *locale = NULL;
6988         char *condition = NULL;
6989         char *error_message = NULL;
6990         char query[MAX_QUERY_LEN] = {'\0'};
6991         char where[MAX_QUERY_LEN] = {'\0'};
6992         GSList *list;
6993         int ret = 0;
6994         pkgmgr_pkginfo_x *info = NULL;
6995         pkgmgr_pkginfo_x *filtinfo = NULL;
6996         pkgmgr_appinfo_x *appinfo = NULL;
6997         uiapplication_x *ptr1 = NULL;
6998         serviceapplication_x *ptr2 = NULL;
6999         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
7000
7001         /*Get current locale*/
7002         syslocale = vconf_get_str(VCONFKEY_LANGSET);
7003         retvm_if(syslocale == NULL, PMINFO_R_ERROR, "current locale is NULL\n");
7004         locale = __convert_system_locale_to_manifest_locale(syslocale);
7005         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL\n");
7006
7007         ret = __open_manifest_db(uid);
7008         if (ret == -1) {
7009                 _LOGE("Fail to open manifest DB\n");
7010                 free(syslocale);
7011                 free(locale);
7012                 return PMINFO_R_ERROR;
7013         }
7014         /*Start constructing query*/
7015         memset(where, '\0', MAX_QUERY_LEN);
7016         memset(query, '\0', MAX_QUERY_LEN);
7017         snprintf(query, MAX_QUERY_LEN - 1, METADATA_FILTER_QUERY_SELECT_CLAUSE);
7018         /*Get where clause*/
7019         for (list = filter->list; list; list = g_slist_next(list)) {
7020                 __get_metadata_filter_condition(list->data, &condition);
7021                 if (condition) {
7022                         strncat(where, condition, sizeof(where) - strlen(where) -1);
7023                         free(condition);
7024                         condition = NULL;
7025                 }
7026                 if (g_slist_next(list)) {
7027                         strncat(where, METADATA_FILTER_QUERY_UNION_CLAUSE, sizeof(where) - strlen(where) - 1);
7028                 }
7029         }
7030         if (strlen(where) > 0) {
7031                 strncat(query, where, sizeof(query) - strlen(query) - 1);
7032         }
7033         /*To get filtered list*/
7034         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
7035         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7036
7037         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
7038         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7039
7040         /*To get detail app info for each member of filtered list*/
7041         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
7042         tryvm_if(filtinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7043
7044         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
7045         tryvm_if(filtinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7046
7047         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
7048         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7049
7050         ret = sqlite3_exec(GET_DB(manifest_db), query, __app_list_cb, (void *)info, &error_message);
7051         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7052         memset(query, '\0', MAX_QUERY_LEN);
7053
7054         if (info->manifest_info->uiapplication) {
7055                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
7056                 info->manifest_info->uiapplication = ptr1;
7057         }
7058         if (info->manifest_info->serviceapplication) {
7059                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
7060                 info->manifest_info->serviceapplication = ptr2;
7061         }
7062
7063         /*UI Apps*/
7064         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
7065         {
7066                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7067                                                         ptr1->appid, "uiapp");
7068                 ret = sqlite3_exec(GET_DB(manifest_db), query, __uiapp_list_cb, (void *)filtinfo, &error_message);
7069                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7070                 memset(query, '\0', MAX_QUERY_LEN);
7071         }
7072         /*Service Apps*/
7073         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
7074         {
7075                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7076                                                         ptr2->appid, "svcapp");
7077                 ret = sqlite3_exec(GET_DB(manifest_db), query, __svcapp_list_cb, (void *)filtinfo, &error_message);
7078                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7079                 memset(query, '\0', MAX_QUERY_LEN);
7080         }
7081         /*Filtered UI Apps*/
7082         if (filtinfo->manifest_info->uiapplication) {
7083                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
7084                 filtinfo->manifest_info->uiapplication = ptr1;
7085         }
7086         /*If the callback func return < 0 we break and no more call back is called*/
7087         while(ptr1 != NULL)
7088         {
7089                 appinfo->locale = strdup(locale);
7090                 appinfo->uiapp_info = ptr1;
7091                 appinfo->app_component = PMINFO_UI_APP;
7092                 ret = app_cb((void *)appinfo, user_data);
7093                 if (ret < 0)
7094                         break;
7095                 ptr1 = ptr1->next;
7096         }
7097         /*Filtered Service Apps*/
7098         if (filtinfo->manifest_info->serviceapplication) {
7099                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
7100                 filtinfo->manifest_info->serviceapplication = ptr2;
7101         }
7102         /*If the callback func return < 0 we break and no more call back is called*/
7103         while(ptr2 != NULL)
7104         {
7105                 appinfo->locale = strdup(locale);
7106                 appinfo->svcapp_info = ptr2;
7107                 appinfo->app_component = PMINFO_SVC_APP;
7108                 ret = app_cb((void *)appinfo, user_data);
7109                 if (ret < 0)
7110                         break;
7111                 ptr2 = ptr2->next;
7112         }
7113         ret = PMINFO_R_OK;
7114 catch:
7115         if (locale) {
7116                 free(locale);
7117                 locale = NULL;
7118         }
7119         if (syslocale) {
7120                 free(syslocale);
7121                 syslocale = NULL;
7122         }
7123         sqlite3_free(error_message);
7124         __close_manifest_db();
7125         if (appinfo) {
7126                 free(appinfo);
7127                 appinfo = NULL;
7128         }
7129         __cleanup_pkginfo(info);
7130         __cleanup_pkginfo(filtinfo);
7131         return ret;
7132 }
7133
7134 API int pkgmgrinfo_appinfo_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
7135                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
7136 {
7137         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, GLOBAL_USER);
7138 }
7139
7140 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
7141 {
7142         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7143         pkgmgr_certinfo_x *certinfo = NULL;
7144         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
7145         *handle = NULL;
7146         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7147         *handle = (void *)certinfo;
7148         return PMINFO_R_OK;
7149 }
7150
7151 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle, uid_t uid)
7152 {
7153         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7154         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7155         pkgmgr_certinfo_x *certinfo = NULL;
7156         char *error_message = NULL;
7157         int ret = PMINFO_R_OK;
7158         char query[MAX_QUERY_LEN] = {'\0'};
7159         int exist = 0;
7160         int i = 0;
7161
7162         /*Open db.*/
7163         ret = __open_cert_db(uid,"r");
7164         if (ret != SQLITE_OK) {
7165                 _LOGE("connect db [%s] failed!\n");
7166                 ret = PMINFO_R_ERROR;
7167                 goto err;
7168         }
7169         _check_create_Cert_db(GET_DB(cert_db));
7170         /*validate pkgid*/
7171         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7172         if (SQLITE_OK !=
7173             sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
7174                 _LOGE("Don't execute query = %s error message = %s\n", query,
7175                        error_message);
7176                 sqlite3_free(error_message);
7177                 ret = PMINFO_R_ERROR;
7178                 goto err;
7179         }
7180         if (exist == 0) {
7181                 _LOGE("Package for user[%d] is not found in DB\n", uid);
7182                 ret = PMINFO_R_ERROR;
7183                 goto err;
7184         }
7185         certinfo = (pkgmgr_certinfo_x *)handle;
7186         /*populate certinfo from DB*/
7187         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
7188         ret = __exec_certinfo_query(query, (void *)certinfo);
7189         if (ret == -1) {
7190                 _LOGE("Package Cert Info DB Information retrieval failed\n");
7191                 ret = PMINFO_R_ERROR;
7192                 goto err;
7193         }
7194         for (i = 0; i < MAX_CERT_TYPE; i++) {
7195                 memset(query, '\0', MAX_QUERY_LEN);
7196                 if (uid == GLOBAL_USER || uid == ROOT_UID)
7197                         snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d", (certinfo->cert_id)[i]);
7198                 else
7199                         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);
7200                 ret = __exec_certinfo_query(query, (void *)certinfo);
7201                 if (ret == -1) {
7202                         _LOGE("Cert Info DB Information retrieval failed\n");
7203                         ret = PMINFO_R_ERROR;
7204                         goto err;
7205                 }
7206                 if (certinfo->cert_value) {
7207                         (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
7208                         free(certinfo->cert_value);
7209                         certinfo->cert_value = NULL;
7210                 }
7211         }
7212 err:
7213         __close_cert_db();
7214         return ret;
7215 }
7216
7217 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
7218 {
7219         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7220         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7221         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7222         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7223         pkgmgr_certinfo_x *certinfo = NULL;
7224         certinfo = (pkgmgr_certinfo_x *)handle;
7225         if ((certinfo->cert_info)[cert_type])
7226                 *cert_value = (certinfo->cert_info)[cert_type];
7227         else
7228                 *cert_value = NULL;
7229         return PMINFO_R_OK;
7230 }
7231
7232 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
7233 {
7234         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7235         int i = 0;
7236         pkgmgr_certinfo_x *certinfo = NULL;
7237         certinfo = (pkgmgr_certinfo_x *)handle;
7238         if (certinfo->pkgid) {
7239                 free(certinfo->pkgid);
7240                 certinfo->pkgid = NULL;
7241         }
7242         for (i = 0; i < MAX_CERT_TYPE; i++) {
7243                 if ((certinfo->cert_info)[i]) {
7244                         free((certinfo->cert_info)[i]);
7245                         (certinfo->cert_info)[i] = NULL;
7246                 }
7247         }
7248         free(certinfo);
7249         certinfo = NULL;
7250         return PMINFO_R_OK;
7251 }
7252
7253 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
7254 {
7255         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7256         pkgmgr_instcertinfo_x *certinfo = NULL;
7257         *handle = NULL;
7258         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
7259         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7260         *handle = (void *)certinfo;
7261         return PMINFO_R_OK;
7262 }
7263
7264 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
7265 {
7266         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7267         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7268         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7269         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7270         pkgmgr_instcertinfo_x *certinfo = NULL;
7271         certinfo = (pkgmgr_instcertinfo_x *)handle;
7272         (certinfo->cert_info)[cert_type] = strdup(cert_value);
7273         return PMINFO_R_OK;
7274 }
7275
7276 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle, uid_t uid)
7277 {
7278         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7279         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7280         char *error_message = NULL;
7281         char query[MAX_QUERY_LEN] = {'\0'};
7282         char *vquery = NULL;
7283         int len = 0;
7284         int i = 0;
7285         int j = 0;
7286         int c = 0;
7287         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
7288         int newid = 0;
7289         int is_new = 0;
7290         int exist = -1;
7291         int ret = -1;
7292         int maxid = 0;
7293         int flag = 0;
7294         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
7295         pkgmgr_certindexinfo_x *indexinfo = NULL;
7296         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
7297         if (indexinfo == NULL) {
7298                 _LOGE("Out of Memory!!!");
7299                 return PMINFO_R_ERROR;
7300         }
7301         info->pkgid = strdup(pkgid);
7302
7303         /*Open db.*/
7304         ret =__open_cert_db(uid, "w");
7305         if (ret != 0) {
7306                 ret = PMINFO_R_ERROR;
7307                 _LOGE("Failed to open cert db \n");
7308                 goto err;
7309         }
7310         _check_create_Cert_db(GET_DB(cert_db));
7311         /*Begin Transaction*/
7312         ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7313         if (ret == -1) {
7314                 _LOGE("Failed to begin transaction %s\n");
7315                 ret = PMINFO_R_ERROR;
7316                 goto err;
7317         }
7318
7319         /*Check if request is to insert/update*/
7320         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7321         if (SQLITE_OK !=
7322             sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
7323                 _LOGE("Don't execute query = %s error message = %s\n", query,
7324                        error_message);
7325                 sqlite3_free(error_message);
7326                 ret = PMINFO_R_ERROR;
7327                 goto err;
7328         }
7329         if (exist) {
7330                 /*Update request.
7331                 We cant just issue update query directly. We need to manage index table also.
7332                 Hence it is better to delete and insert again in case of update*/
7333                 ret = __delete_certinfo(pkgid, uid);
7334                 if (ret < 0)
7335                         _LOGE("Certificate Deletion Failed\n");
7336         }
7337         for (i = 0; i < MAX_CERT_TYPE; i++) {
7338                 if ((info->cert_info)[i]) {
7339                         for (j = 0; j < i; j++) {
7340                                 if ( (info->cert_info)[j]) {
7341                                         if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
7342                                                 (info->cert_id)[i] = (info->cert_id)[j];
7343                                                 (info->is_new)[i] = 0;
7344                                                 (info->ref_count)[i] = (info->ref_count)[j];
7345                                                 break;
7346                                         }
7347                                 }
7348                         }
7349                         if (j < i)
7350                                 continue;
7351                         memset(query, '\0', MAX_QUERY_LEN);
7352                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info " \
7353                                 "where cert_info='%s'",(info->cert_info)[i]);
7354                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
7355                         if (ret == -1) {
7356                                 _LOGE("Cert Info DB Information retrieval failed\n");
7357                                 ret = PMINFO_R_ERROR;
7358                                 goto err;
7359                         }
7360                         if (indexinfo->cert_id == 0) {
7361                                 /*New certificate. Get newid*/
7362                                 memset(query, '\0', MAX_QUERY_LEN);
7363                                 snprintf(query, MAX_QUERY_LEN, "select MAX(cert_id) from package_cert_index_info ");
7364                                 if (SQLITE_OK !=
7365                                     sqlite3_exec(GET_DB(cert_db), query, __maxid_cb, (void *)&newid, &error_message)) {
7366                                         _LOGE("Don't execute query = %s error message = %s\n", query,
7367                                                error_message);
7368                                         sqlite3_free(error_message);
7369                                         ret = PMINFO_R_ERROR;
7370                                         goto err;
7371                                 }
7372                                 newid = newid + 1;
7373                                 if (flag == 0) {
7374                                         maxid = newid;
7375                                         flag = 1;
7376                                 }
7377                                 indexinfo->cert_id = maxid;
7378                                 indexinfo->cert_ref_count = 1;
7379                                 is_new = 1;
7380                                 maxid = maxid + 1;
7381                         }
7382                         (info->cert_id)[i] = indexinfo->cert_id;
7383                         (info->is_new)[i] = is_new;
7384                         (info->ref_count)[i] = indexinfo->cert_ref_count;
7385                         indexinfo->cert_id = 0;
7386                         indexinfo->cert_ref_count = 0;
7387                         is_new = 0;
7388                 }
7389         }
7390         len = MAX_QUERY_LEN;
7391         for (i = 0; i < MAX_CERT_TYPE; i++) {
7392                 if ((info->cert_info)[i])
7393                         len+= strlen((info->cert_info)[i]);
7394         }
7395         vquery = (char *)calloc(1, len);
7396         /*insert*/
7397         snprintf(vquery, len,
7398                  "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
7399                 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
7400                 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
7401                  info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
7402                 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
7403                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
7404                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
7405                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
7406         if (SQLITE_OK !=
7407             sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
7408                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7409                        error_message);
7410                 sqlite3_free(error_message);
7411                 ret = PMINFO_R_ERROR;
7412                 goto err;
7413         }
7414         /*Update index table info*/
7415         /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
7416         for (i = 0; i < MAX_CERT_TYPE; i++) {
7417                 if ((info->cert_info)[i]) {
7418                         memset(vquery, '\0', len);
7419                         if ((info->is_new)[i]) {
7420                                 snprintf(vquery, len, "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
7421                                 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
7422                                 unique_id[c++] = (info->cert_id)[i];
7423                         } else {
7424                                 /*Update*/
7425                                 for (j = 0; j < MAX_CERT_TYPE; j++) {
7426                                         if ((info->cert_id)[i] == unique_id[j]) {
7427                                                 /*Ref count has already been increased. Just continue*/
7428                                                 break;
7429                                         }
7430                                 }
7431                                 if (j == MAX_CERT_TYPE)
7432                                         unique_id[c++] = (info->cert_id)[i];
7433                                 else
7434                                         continue;
7435                                 snprintf(vquery, len, "update package_cert_index_info set cert_ref_count=%d " \
7436                                 "where cert_id=%d",  (info->ref_count)[i] + 1, (info->cert_id)[i]);
7437                         }
7438                         if (SQLITE_OK !=
7439                             sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
7440                                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7441                                        error_message);
7442                                 sqlite3_free(error_message);
7443                                 ret = PMINFO_R_ERROR;
7444                                 goto err;
7445                         }
7446                 }
7447         }
7448         /*Commit transaction*/
7449         ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
7450         if (ret != SQLITE_OK) {
7451                 _LOGE("Failed to commit transaction, Rollback now\n");
7452                 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
7453                 ret = PMINFO_R_ERROR;
7454                 goto err;
7455         }
7456
7457         ret =  PMINFO_R_OK;
7458 err:
7459         __close_cert_db();
7460         if (vquery) {
7461                 free(vquery);
7462                 vquery = NULL;
7463         }
7464         if (indexinfo) {
7465                 free(indexinfo);
7466                 indexinfo = NULL;
7467         }
7468         return ret;
7469 }
7470
7471 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
7472 {
7473         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7474         int i = 0;
7475         pkgmgr_instcertinfo_x *certinfo = NULL;
7476         certinfo = (pkgmgr_instcertinfo_x *)handle;
7477         if (certinfo->pkgid) {
7478                 free(certinfo->pkgid);
7479                 certinfo->pkgid = NULL;
7480         }
7481         for (i = 0; i < MAX_CERT_TYPE; i++) {
7482                 if ((certinfo->cert_info)[i]) {
7483                         free((certinfo->cert_info)[i]);
7484                         (certinfo->cert_info)[i] = NULL;
7485                 }
7486         }
7487         free(certinfo);
7488         certinfo = NULL;
7489         return PMINFO_R_OK;
7490 }
7491
7492 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
7493 {
7494         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7495         int ret = -1;
7496         /*Open db.*/
7497         ret = __open_cert_db(uid, "w");
7498         if (ret != 0) {
7499                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
7500                 ret = PMINFO_R_ERROR;
7501                 goto err;
7502         }
7503         _check_create_Cert_db(GET_DB(cert_db));
7504         /*Begin Transaction*/
7505         ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7506         if (ret != SQLITE_OK) {
7507                 _LOGE("Failed to begin transaction\n");
7508                 ret = PMINFO_R_ERROR;
7509                 goto err;
7510         }
7511         _LOGE("Transaction Begin\n");
7512         ret = __delete_certinfo(pkgid, uid);
7513         if (ret < 0) {
7514                 _LOGE("Certificate Deletion Failed\n");
7515         } else {
7516                 _LOGE("Certificate Deletion Success\n");
7517         }
7518         /*Commit transaction*/
7519         ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
7520         if (ret != SQLITE_OK) {
7521                 _LOGE("Failed to commit transaction, Rollback now\n");
7522                 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
7523                 ret = PMINFO_R_ERROR;
7524                 goto err;
7525         }
7526         _LOGE("Transaction Commit and End\n");
7527         ret = PMINFO_R_OK;
7528 err:
7529         __close_cert_db();
7530         return ret;
7531 }
7532
7533
7534 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
7535 {
7536         return pkgmgrinfo_delete_usr_certinfo(pkgid, GLOBAL_USER);
7537 }
7538
7539 API int pkgmgrinfo_create_pkgusrdbinfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkgdbinfo_h *handle)
7540 {
7541         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7542         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7543
7544         char *manifest = NULL;
7545         manifest_x *mfx = NULL;
7546         *handle = NULL;
7547         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
7548         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7549
7550         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
7551         if (manifest) {
7552                 free(manifest);
7553                 manifest = NULL;
7554         }
7555         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7556
7557         *handle = (void *)mfx;
7558
7559         return PMINFO_R_OK;
7560 }
7561
7562 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
7563 {
7564         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7565         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7566
7567         char *manifest = NULL;
7568         manifest_x *mfx = NULL;
7569         *handle = NULL;
7570         manifest = pkgmgr_parser_get_manifest_file(pkgid);
7571         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7572
7573         mfx = pkgmgr_parser_process_manifest_xml(manifest);
7574         if (manifest) {
7575                 free(manifest);
7576                 manifest = NULL;
7577         }
7578         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7579
7580         *handle = (void *)mfx;
7581
7582         return PMINFO_R_OK;
7583 }
7584
7585 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
7586 {
7587         int len;
7588         manifest_x *mfx = (manifest_x *)handle;
7589
7590         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
7591         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7592
7593         len = strlen(type);
7594         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7595
7596         if (mfx->type)
7597                 free((void *)mfx->type);
7598
7599         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
7600
7601         return PMINFO_R_OK;
7602 }
7603
7604 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
7605 {
7606         int len;
7607         manifest_x *mfx = (manifest_x *)handle;
7608
7609         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
7610         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7611
7612         len = strlen(version);
7613         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7614
7615         if (mfx->version)
7616                 free((void *)mfx->version);
7617
7618         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
7619         return PMINFO_R_OK;
7620 }
7621
7622 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7623 {
7624         manifest_x *mfx = (manifest_x *)handle;
7625
7626         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7627         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7628
7629         if (mfx->installlocation)
7630                 free((void *)mfx->installlocation);
7631
7632         if (location == INSTALL_INTERNAL)
7633                 mfx->installlocation = strdup("internal-only");
7634         else if (location == INSTALL_EXTERNAL)
7635                 mfx->installlocation = strdup("prefer-external");
7636
7637         return PMINFO_R_OK;
7638 }
7639
7640 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
7641 {
7642         manifest_x *mfx = (manifest_x *)handle;
7643
7644         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7645         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7646
7647         if (mfx->package_size)
7648                 free((void *)mfx->package_size);
7649
7650         mfx->package_size = strdup(size);
7651
7652         return PMINFO_R_OK;
7653 }
7654
7655 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
7656 {
7657         int len;
7658         manifest_x *mfx = (manifest_x *)handle;
7659         label_x *label;
7660
7661         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7662         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7663
7664         len = strlen(label_txt);
7665         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7666
7667         label = calloc(1, sizeof(label_x));
7668         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7669
7670         LISTADD(mfx->label, label);
7671         if (locale)
7672                 mfx->label->lang = strdup(locale);
7673         else
7674                 mfx->label->lang = strdup(DEFAULT_LOCALE);
7675         mfx->label->text = strdup(label_txt);
7676
7677         return PMINFO_R_OK;
7678 }
7679
7680 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
7681 {
7682         int len;
7683         manifest_x *mfx = (manifest_x *)handle;
7684         icon_x *icon;
7685
7686         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7687         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7688
7689         len = strlen(icon_txt);
7690         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7691
7692         icon = calloc(1, sizeof(icon_x));
7693         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7694
7695         LISTADD(mfx->icon, icon);
7696         if (locale)
7697                 mfx->icon->lang = strdup(locale);
7698         else
7699                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
7700         mfx->icon->text = strdup(icon_txt);
7701
7702         return PMINFO_R_OK;
7703 }
7704
7705 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
7706 {
7707         int len = strlen(desc_txt);
7708         manifest_x *mfx = (manifest_x *)handle;
7709         description_x *description;
7710
7711         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7712         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7713
7714         len = strlen(desc_txt);
7715         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7716
7717         description = calloc(1, sizeof(description_x));
7718         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7719
7720         LISTADD(mfx->description, description);
7721         if (locale)
7722                 mfx->description->lang = strdup(locale);
7723         else
7724                 mfx->description->lang = strdup(DEFAULT_LOCALE);
7725         mfx->description->text = strdup(desc_txt);
7726
7727         return PMINFO_R_OK;
7728 }
7729
7730 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
7731                 const char *author_email, const char *author_href, const char *locale)
7732 {
7733         manifest_x *mfx = (manifest_x *)handle;
7734         author_x *author;
7735
7736         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7737
7738         author = calloc(1, sizeof(author_x));
7739         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7740
7741         LISTADD(mfx->author, author);
7742         if (author_name)
7743                 mfx->author->text = strdup(author_name);
7744         if (author_email)
7745                 mfx->author->email = strdup(author_email);
7746         if (author_href)
7747                 mfx->author->href = strdup(author_href);
7748         if (locale)
7749                 mfx->author->lang = strdup(locale);
7750         else
7751                 mfx->author->lang = strdup(DEFAULT_LOCALE);
7752         return PMINFO_R_OK;
7753 }
7754
7755 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
7756 {
7757         manifest_x *mfx = (manifest_x *)handle;
7758
7759         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7760         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7761
7762         if (mfx->removable)
7763                 free((void *)mfx->removable);
7764
7765         if (removable == 0)
7766                 mfx->removable = strdup("false");
7767         else if (removable == 1)
7768                 mfx->removable = strdup("true");
7769
7770         return PMINFO_R_OK;
7771 }
7772
7773 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
7774 {
7775         manifest_x *mfx = (manifest_x *)handle;
7776
7777         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7778         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7779
7780         if (mfx->preload)
7781                 free((void *)mfx->preload);
7782
7783         if (preload == 0)
7784                 mfx->preload = strdup("false");
7785         else if (preload == 1)
7786                 mfx->preload = strdup("true");
7787
7788         return PMINFO_R_OK;
7789 }
7790
7791 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7792 {
7793         manifest_x *mfx = (manifest_x *)handle;
7794
7795         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7796         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7797
7798         if (mfx->installed_storage)
7799                 free((void *)mfx->installed_storage);
7800
7801         if (location == INSTALL_INTERNAL)
7802                 mfx->installed_storage = strdup("installed_internal");
7803         else if (location == INSTALL_EXTERNAL)
7804                 mfx->installed_storage = strdup("installed_external");
7805
7806         return PMINFO_R_OK;
7807 }
7808
7809 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7810 {
7811         int ret;
7812         manifest_x *mfx = (manifest_x *)handle;
7813         mfx = (manifest_x *)handle;
7814
7815         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7816
7817         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
7818         if (ret == 0) {
7819                 _LOGE("Successfully stored info in DB\n");
7820                 return PMINFO_R_OK;
7821         } else {
7822                 _LOGE("Failed to store info in DB\n");
7823                 return PMINFO_R_ERROR;
7824         }
7825 }
7826
7827 API int pkgmgrinfo_save_pkgusrdbinfo(pkgmgrinfo_pkgdbinfo_h handle, uid_t uid)
7828 {
7829         int ret;
7830         manifest_x *mfx = (manifest_x *)handle;
7831
7832         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7833
7834         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
7835         if (ret == 0) {
7836                 _LOGE("Successfully stored info in DB\n");
7837                 return PMINFO_R_OK;
7838         } else {
7839                 _LOGE("Failed to store info in DB\n");
7840                 return PMINFO_R_ERROR;
7841         }
7842 }
7843
7844 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7845 {
7846         manifest_x *mfx = (manifest_x *)handle;
7847
7848         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7849
7850         pkgmgr_parser_free_manifest_xml(mfx);
7851
7852         return PMINFO_R_OK;
7853 }
7854
7855 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
7856 {
7857         /* Should be implemented later */
7858         return 0;
7859 }
7860
7861 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
7862 {
7863         int ret;
7864         char query[MAX_QUERY_LEN] = {'\0'};
7865         char *error_message;
7866
7867         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
7868
7869         /* Open db.*/
7870         ret = __open_manifest_db(uid);
7871         if (ret != SQLITE_OK) {
7872                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
7873                 return PMINFO_R_ERROR;
7874         }
7875
7876         /*Begin transaction*/
7877         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7878         if (ret != SQLITE_OK) {
7879                 _LOGE("Failed to begin transaction\n");
7880                 __close_manifest_db();
7881                 return PMINFO_R_ERROR;
7882         }
7883         _LOGD("Transaction Begin\n");
7884
7885         memset(query, '\0', MAX_QUERY_LEN);
7886         snprintf(query, MAX_QUERY_LEN,
7887                 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
7888
7889         if (SQLITE_OK !=
7890             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
7891                 _LOGE("Don't execute query = %s error message = %s\n", query,
7892                        error_message);
7893                 sqlite3_free(error_message);
7894                 return PMINFO_R_ERROR;
7895         }
7896         sqlite3_free(error_message);
7897
7898         /*Commit transaction*/
7899         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
7900         if (ret != SQLITE_OK) {
7901                 _LOGE("Failed to commit transaction. Rollback now\n");
7902                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
7903                 __close_manifest_db();
7904                 return PMINFO_R_ERROR;
7905         }
7906         _LOGD("Transaction Commit and End\n");
7907         __close_manifest_db();
7908         return PMINFO_R_OK;
7909 }
7910
7911 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
7912 {
7913         return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
7914 }
7915
7916 API int pkgmgrinfo_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
7917 {
7918         int ret;
7919         char query[MAX_QUERY_LEN] = {'\0'};
7920         char *error_message;
7921         pkgmgr_datacontrol_x *data;
7922
7923         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7924         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7925         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7926         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7927
7928         ret = __open_datacontrol_db();
7929         if (ret == -1) {
7930                 _LOGE("Fail to open datacontrol DB\n");
7931                 return PMINFO_R_ERROR;
7932         }
7933
7934         data = (pkgmgr_datacontrol_x *)calloc(1, sizeof(pkgmgr_datacontrol_x));
7935         if (data == NULL) {
7936                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
7937                 __close_datacontrol_db();
7938                 return PMINFO_R_ERROR;
7939         }
7940
7941         snprintf(query, MAX_QUERY_LEN, 
7942                 "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",
7943                 providerid, type);
7944
7945         if (SQLITE_OK !=
7946                 sqlite3_exec(GET_DB(datacontrol_db), query, __datacontrol_cb, (void *)data, &error_message)) {
7947                 _LOGE("Don't execute query = %s error message = %s\n", query,
7948                            error_message);
7949                 sqlite3_free(error_message);
7950                 __close_datacontrol_db();
7951                 return PMINFO_R_ERROR;
7952         }
7953
7954         *appid = (char *)data->appid;
7955         *access = (char *)data->access;
7956         free(data);
7957         __close_datacontrol_db();
7958
7959         return PMINFO_R_OK;
7960 }
7961
7962 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
7963 {
7964         int ret;
7965         char query[MAX_QUERY_LEN] = {'\0'};
7966         char *error_message;
7967
7968         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
7969
7970         ret = __open_manifest_db(uid);
7971         if (ret == -1) {
7972                 _LOGE("Fail to open manifest DB\n");
7973                 return PMINFO_R_ERROR;
7974         }
7975
7976         /*Begin transaction*/
7977         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7978         if (ret != SQLITE_OK) {
7979                 _LOGE("Failed to begin transaction\n");
7980                 __close_manifest_db();
7981                 return PMINFO_R_ERROR;
7982         }
7983         _LOGD("Transaction Begin\n");
7984
7985         memset(query, '\0', MAX_QUERY_LEN);
7986         snprintf(query, MAX_QUERY_LEN,
7987                 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
7988
7989         if (SQLITE_OK !=
7990             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
7991                 _LOGE("Don't execute query = %s error message = %s\n", query,
7992                        error_message);
7993                 sqlite3_free(error_message);
7994                 return PMINFO_R_ERROR;
7995         }
7996
7997         /*Commit transaction*/
7998         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
7999         if (ret != SQLITE_OK) {
8000                 _LOGE("Failed to commit transaction. Rollback now\n");
8001                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
8002                 __close_manifest_db();
8003                 return PMINFO_R_ERROR;
8004         }
8005         _LOGD("Transaction Commit and End\n");
8006         __close_manifest_db();
8007         return PMINFO_R_OK;
8008 }
8009
8010 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
8011 {
8012         return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
8013 }
8014
8015 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
8016 {
8017         const char *val;
8018         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
8019
8020         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
8021         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
8022
8023         val = info->uiapp_info->guestmode_visibility;
8024         if (val) {
8025                 if (strcasecmp(val, "true") == 0){
8026                         *status = 1;
8027                 }else if (strcasecmp(val, "false") == 0){
8028                         *status = 0;
8029                 }else{
8030                         *status = 1;
8031                 }
8032         }
8033         return PMINFO_R_OK;
8034 }
8035
8036 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
8037 {
8038         const char *val;
8039         int ret;
8040         char query[MAX_QUERY_LEN] = {'\0'};
8041         char *errmsg;
8042         sqlite3 *pkgmgr_parser_db;
8043
8044         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
8045
8046         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
8047         val = info->uiapp_info->guestmode_visibility;
8048         if (val) {
8049                 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
8050                                 SQLITE_OPEN_READWRITE, NULL);
8051                 if (ret != SQLITE_OK) {
8052                         _LOGE("DB Open Failed\n");
8053                         return PMINFO_R_ERROR;
8054                 }
8055
8056                 /*TODO: Write to DB here*/
8057                 if (status == true)
8058                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->uiapp_info->appid);
8059                 else
8060                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->uiapp_info->appid);
8061
8062                 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
8063                 sqlite3_close(pkgmgr_parser_db);
8064                 if (ret != SQLITE_OK) {
8065                         _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
8066                         free(errmsg);
8067                         return PMINFO_R_ERROR;
8068                 }
8069         }
8070         return PMINFO_R_OK;
8071 }
8072
8073 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
8074 {
8075         return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);
8076 }
8077
8078 /* pkgmgrinfo client start*/
8079 API pkgmgrinfo_client *pkgmgrinfo_client_new(pkgmgrinfo_client_type ctype)
8080 {
8081         char *errmsg;
8082         void *pc = NULL;
8083         void *handle;
8084         pkgmgrinfo_client *(*__pkgmgr_client_new)(pkgmgrinfo_client_type ctype) = NULL;
8085
8086         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8087         retvm_if(!handle, NULL, "dlopen() failed. [%s]", dlerror());
8088
8089         __pkgmgr_client_new = dlsym(handle, "pkgmgr_client_new");
8090         errmsg = dlerror();
8091         trym_if((errmsg != NULL) || (__pkgmgr_client_new == NULL), "dlsym() failed. [%s]", errmsg);
8092
8093         pc = __pkgmgr_client_new(ctype);
8094         trym_if(pc == NULL, "pkgmgr_client_new failed.");
8095
8096 catch:
8097         dlclose(handle);
8098         return (pkgmgrinfo_client *) pc;
8099 }
8100
8101 API int pkgmgrinfo_client_set_status_type(pkgmgrinfo_client *pc, int status_type)
8102 {
8103         int ret;
8104         char *errmsg;
8105         void *handle;
8106         int (*__pkgmgr_client_set_status_type)(pkgmgrinfo_client *pc, int status_type) = NULL;
8107
8108         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8109         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8110
8111         __pkgmgr_client_set_status_type = dlsym(handle, "pkgmgr_client_set_status_type");
8112         errmsg = dlerror();
8113         tryvm_if((errmsg != NULL) || (__pkgmgr_client_set_status_type == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8114
8115         ret = __pkgmgr_client_set_status_type(pc, status_type);
8116         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8117
8118 catch:
8119         /*
8120          * Do not close libpkgmgr-client.so.0 to avoid munmap registered callback
8121          *
8122          * The lib dependency chain like below
8123          * amd --> pkgmgr-info -- dlopen --> libpkgmgr-client --> libpkgmgr-installer-client
8124          *
8125          * And there is a function in libpkgmgr-installer-client named _on_signal_handle_filter()
8126          * which will registered to dbus callback in amd though in fact amd doesn't direct depends
8127          * on libpkgmgr-installer-client.
8128          *
8129          * So when the dlcose happen, then libpkgmgr-installer-client been closed too since no one
8130          * link to it then.
8131          *
8132          * However, when the libdbus call into the callback function, it suddenly fond that the
8133          * function address is gone (unmapped), then we receive a SIGSEGV.
8134          *
8135          * I'm not sure why we're using dlopen/dlclose in this case, I think it's much simple and
8136          * robust if we just link to the well-known lib.
8137          *
8138          * See https://bugs.tizen.org/jira/browse/PTREL-591
8139         dlclose(handle);
8140          */
8141         return ret;
8142 }
8143
8144 API int pkgmgrinfo_client_listen_status(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data)
8145 {
8146         int ret = 0;
8147         char *errmsg = NULL;
8148         void *handle = NULL;
8149         int (*__pkgmgr_client_listen_status)(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data) = NULL;
8150
8151         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8152         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8153
8154         __pkgmgr_client_listen_status = dlsym(handle, "pkgmgr_client_listen_status");
8155         errmsg = dlerror();
8156         tryvm_if((errmsg != NULL) || (__pkgmgr_client_listen_status == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8157
8158         ret = __pkgmgr_client_listen_status(pc, event_cb, data);
8159         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8160
8161 catch:
8162         /* same as pkgmgrinfo_client_new */
8163         return ret;
8164 }
8165
8166 API int pkgmgrinfo_client_free(pkgmgrinfo_client *pc)
8167 {
8168         int ret = 0;
8169         char *errmsg = NULL;
8170         void *handle = NULL;
8171         int (*__pkgmgr_client_free)(pkgmgrinfo_client *pc) = NULL;
8172
8173         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8174         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8175
8176         __pkgmgr_client_free = dlsym(handle, "pkgmgr_client_free");
8177         errmsg = dlerror();
8178         tryvm_if((errmsg != NULL) || (__pkgmgr_client_free == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8179
8180         ret = __pkgmgr_client_free(pc);
8181         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8182
8183 catch:
8184         /* same as pkgmgrinfo_client_new */
8185         return ret;
8186 }
8187
8188 API int pkgmgrinfo_client_request_enable_external_pkg(char *pkgid)
8189 {
8190         DBusConnection *bus;
8191         DBusMessage *message = NULL;
8192         DBusMessage *reply = NULL;
8193
8194         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
8195
8196         if(__get_pkg_location(pkgid) != PMINFO_EXTERNAL_STORAGE)
8197                 return PMINFO_R_OK;
8198
8199         bus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
8200         retvm_if(bus == NULL, PMINFO_R_EINVAL, "dbus_bus_get() failed.");
8201
8202         message = dbus_message_new_method_call (SERVICE_NAME, PATH_NAME, INTERFACE_NAME, METHOD_NAME);
8203         trym_if(message == NULL, "dbus_message_new_method_call() failed.");
8204
8205         dbus_message_append_args(message, DBUS_TYPE_STRING, &pkgid, DBUS_TYPE_INVALID);
8206
8207         reply = dbus_connection_send_with_reply_and_block(bus, message, -1, NULL);
8208         trym_if(reply == NULL, "connection_send dbus fail");
8209
8210 catch:
8211         dbus_connection_flush(bus);
8212         if (message)
8213                 dbus_message_unref(message);
8214         if (reply)
8215                 dbus_message_unref(reply);
8216
8217         return PMINFO_R_OK;
8218 }
8219
8220 /* pkgmgrinfo client end*/
8221