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