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