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