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