Remove event-system apis
[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 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
5895                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
5896 {
5897         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5898         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5899         int ret = -1;
5900         permission_x *ptr = NULL;
5901         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5902         if (info->app_component == PMINFO_UI_APP)
5903                 ptr = info->uiapp_info->permission;
5904         else if (info->app_component == PMINFO_SVC_APP)
5905                 ptr = info->svcapp_info->permission;
5906         else
5907                 return PMINFO_R_EINVAL;
5908         for (; ptr; ptr = ptr->next) {
5909                 if (ptr->value) {
5910                         ret = permission_func(ptr->value, user_data);
5911                         if (ret < 0)
5912                                 break;
5913                 }
5914         }
5915         return PMINFO_R_OK;
5916 }
5917
5918 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
5919                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
5920 {
5921         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5922         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5923         int ret = -1;
5924         category_x *ptr = NULL;
5925         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5926         if (info->app_component == PMINFO_UI_APP)
5927                 ptr = info->uiapp_info->category;
5928         else if (info->app_component == PMINFO_SVC_APP)
5929                 ptr = info->svcapp_info->category;
5930         else
5931                 return PMINFO_R_EINVAL;
5932         for (; ptr; ptr = ptr->next) {
5933                 if (ptr->name) {
5934                         ret = category_func(ptr->name, user_data);
5935                         if (ret < 0)
5936                                 break;
5937                 }
5938         }
5939         return PMINFO_R_OK;
5940 }
5941
5942 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
5943                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
5944 {
5945         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5946         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5947         int ret = -1;
5948         metadata_x *ptr = NULL;
5949         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5950         if (info->app_component == PMINFO_UI_APP)
5951                 ptr = info->uiapp_info->metadata;
5952         else if (info->app_component == PMINFO_SVC_APP)
5953                 ptr = info->svcapp_info->metadata;
5954         else
5955                 return PMINFO_R_EINVAL;
5956         for (; ptr; ptr = ptr->next) {
5957                 if (ptr->key) {
5958                         ret = metadata_func(ptr->key, ptr->value, user_data);
5959                         if (ret < 0)
5960                                 break;
5961                 }
5962         }
5963         return PMINFO_R_OK;
5964 }
5965
5966 API int pkgmgrinfo_usr_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
5967                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data, uid_t uid)
5968 {
5969         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5970         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5971         int i = 0;
5972         int ret = -1;
5973         int oc = 0;
5974         int mc = 0;
5975         int uc = 0;
5976         int sc = 0;
5977         char *pkgid = NULL;
5978         char *manifest = NULL;
5979         char **operation = NULL;
5980         char **uri = NULL;
5981         char **mime = NULL;
5982         char **subapp = NULL;
5983         appcontrol_x *appcontrol = NULL;
5984         manifest_x *mfx = NULL;
5985         operation_x *op = NULL;
5986         uri_x *ui = NULL;
5987         mime_x *mi = NULL;
5988         subapp_x *sa = NULL;
5989         pkgmgrinfo_app_component component;
5990         pkgmgrinfo_appcontrol_x *ptr = NULL;
5991         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
5992         if (ret < 0) {
5993                 _LOGE("Failed to get package name\n");
5994                 return PMINFO_R_ERROR;
5995         }
5996         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
5997         if (ret < 0) {
5998                 _LOGE("Failed to get app component name\n");
5999                 return PMINFO_R_ERROR;
6000         }
6001         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
6002         if (manifest == NULL) {
6003                 _LOGE("Failed to fetch package manifest file\n");
6004                 return PMINFO_R_ERROR;
6005         }
6006         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
6007         if (mfx == NULL) {
6008                 _LOGE("Failed to parse package manifest file\n");
6009                 free(manifest);
6010                 manifest = NULL;
6011                 return PMINFO_R_ERROR;
6012         }
6013         free(manifest);
6014         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
6015         if (ptr == NULL) {
6016                 _LOGE("Out of Memory!!!\n");
6017                 pkgmgr_parser_free_manifest_xml(mfx);
6018                 return PMINFO_R_ERROR;
6019         }
6020         /*Get Operation, Uri, Mime*/
6021         switch (component) {
6022         case PMINFO_UI_APP:
6023                 if (mfx->uiapplication) {
6024                         if (mfx->uiapplication->appsvc) {
6025                                 appcontrol = mfx->uiapplication->appsvc;
6026                         }
6027                 }
6028                 break;
6029         case PMINFO_SVC_APP:
6030                 if (mfx->serviceapplication) {
6031                         if (mfx->serviceapplication->appsvc) {
6032                                 appcontrol = mfx->serviceapplication->appsvc;
6033                         }
6034                 }
6035                 break;
6036         default:
6037                 break;
6038         }
6039         for (; appcontrol; appcontrol = appcontrol->next) {
6040                 op = appcontrol->operation;
6041                 for (; op; op = op->next)
6042                         oc = oc + 1;
6043                 op = appcontrol->operation;
6044
6045                 ui = appcontrol->uri;
6046                 for (; ui; ui = ui->next)
6047                         uc = uc + 1;
6048                 ui = appcontrol->uri;
6049
6050                 mi = appcontrol->mime;
6051                 for (; mi; mi = mi->next)
6052                         mc = mc + 1;
6053                 mi = appcontrol->mime;
6054
6055                 sa = appcontrol->subapp;
6056                 for (; sa; sa = sa->next)
6057                         sc = sc + 1;
6058                 sa = appcontrol->subapp;
6059
6060                 operation = (char **)calloc(oc, sizeof(char *));
6061                 for (i = 0; i < oc; i++) {
6062                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
6063                         op = op->next;
6064                 }
6065
6066                 uri = (char **)calloc(uc, sizeof(char *));
6067                 for (i = 0; i < uc; i++) {
6068                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
6069                         ui = ui->next;
6070                 }
6071
6072                 mime = (char **)calloc(mc, sizeof(char *));
6073                 for (i = 0; i < mc; i++) {
6074                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
6075                         mi = mi->next;
6076                 }
6077
6078                 subapp = (char **)calloc(sc, sizeof(char *));
6079                 for (i = 0; i < sc; i++) {
6080                         subapp[i] = strndup(sa->name, PKG_STRING_LEN_MAX - 1);
6081                         sa = sa->next;
6082                 }
6083
6084                 /*populate appcontrol handle*/
6085                 ptr->operation_count = oc;
6086                 ptr->uri_count = uc;
6087                 ptr->mime_count = mc;
6088                 ptr->subapp_count = sc;
6089                 ptr->operation = operation;
6090                 ptr->uri = uri;
6091                 ptr->mime = mime;
6092                 ptr->subapp = subapp;
6093
6094                 ret = appcontrol_func((void *)ptr, user_data);
6095                 for (i = 0; i < oc; i++) {
6096                         if (operation[i]) {
6097                                 free(operation[i]);
6098                                 operation[i] = NULL;
6099                         }
6100                 }
6101                 if (operation) {
6102                         free(operation);
6103                         operation = NULL;
6104                 }
6105                 for (i = 0; i < uc; i++) {
6106                         if (uri[i]) {
6107                                 free(uri[i]);
6108                                 uri[i] = NULL;
6109                         }
6110                 }
6111                 if (uri) {
6112                         free(uri);
6113                         uri = NULL;
6114                 }
6115                 for (i = 0; i < mc; i++) {
6116                         if (mime[i]) {
6117                                 free(mime[i]);
6118                                 mime[i] = NULL;
6119                         }
6120                 }
6121                 if (mime) {
6122                         free(mime);
6123                         mime = NULL;
6124                 }
6125                 for (i = 0; i < sc; i++) {
6126                         if (subapp[i]) {
6127                                 free(subapp[i]);
6128                                 subapp[i] = NULL;
6129                         }
6130                 }
6131                 if (subapp) {
6132                         free(subapp);
6133                         subapp = NULL;
6134                 }
6135                 if (ret < 0)
6136                         break;
6137                 uc = 0;
6138                 mc = 0;
6139                 oc = 0;
6140                 sc = 0;
6141         }
6142         pkgmgr_parser_free_manifest_xml(mfx);
6143         if (ptr) {
6144                 free(ptr);
6145                 ptr = NULL;
6146         }
6147         return PMINFO_R_OK;
6148 }
6149
6150 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
6151                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
6152 {
6153         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6154         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6155         int i = 0;
6156         int ret = -1;
6157         int oc = 0;
6158         int mc = 0;
6159         int uc = 0;
6160         int sc = 0;
6161         char *pkgid = NULL;
6162         char *manifest = NULL;
6163         char **operation = NULL;
6164         char **uri = NULL;
6165         char **mime = NULL;
6166         char **subapp = NULL;
6167         appcontrol_x *appcontrol = NULL;
6168         manifest_x *mfx = NULL;
6169         operation_x *op = NULL;
6170         uri_x *ui = NULL;
6171         mime_x *mi = NULL;
6172         subapp_x *sa = NULL;
6173         pkgmgrinfo_app_component component;
6174         pkgmgrinfo_appcontrol_x *ptr = NULL;
6175         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
6176         if (ret < 0) {
6177                 _LOGE("Failed to get package name\n");
6178                 return PMINFO_R_ERROR;
6179         }
6180         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
6181         if (ret < 0) {
6182                 _LOGE("Failed to get app component name\n");
6183                 return PMINFO_R_ERROR;
6184         }
6185         manifest = pkgmgr_parser_get_manifest_file(pkgid);
6186         if (manifest == NULL) {
6187                 _LOGE("Failed to fetch package manifest file\n");
6188                 return PMINFO_R_ERROR;
6189         }
6190         mfx = pkgmgr_parser_process_manifest_xml(manifest);
6191         if (mfx == NULL) {
6192                 _LOGE("Failed to parse package manifest file\n");
6193                 free(manifest);
6194                 manifest = NULL;
6195                 return PMINFO_R_ERROR;
6196         }
6197         free(manifest);
6198         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
6199         if (ptr == NULL) {
6200                 _LOGE("Out of Memory!!!\n");
6201                 pkgmgr_parser_free_manifest_xml(mfx);
6202                 return PMINFO_R_ERROR;
6203         }
6204         /*Get Operation, Uri, Mime*/
6205         switch (component) {
6206         case PMINFO_UI_APP:
6207                 if (mfx->uiapplication) {
6208                         if (mfx->uiapplication->appsvc) {
6209                                 appcontrol = mfx->uiapplication->appsvc;
6210                         }
6211                 }
6212                 break;
6213         case PMINFO_SVC_APP:
6214                 if (mfx->serviceapplication) {
6215                         if (mfx->serviceapplication->appsvc) {
6216                                 appcontrol = mfx->serviceapplication->appsvc;
6217                         }
6218                 }
6219                 break;
6220         default:
6221                 break;
6222         }
6223         for (; appcontrol; appcontrol = appcontrol->next) {
6224                 op = appcontrol->operation;
6225                 for (; op; op = op->next)
6226                         oc = oc + 1;
6227                 op = appcontrol->operation;
6228
6229                 ui = appcontrol->uri;
6230                 for (; ui; ui = ui->next)
6231                         uc = uc + 1;
6232                 ui = appcontrol->uri;
6233
6234                 mi = appcontrol->mime;
6235                 for (; mi; mi = mi->next)
6236                         mc = mc + 1;
6237                 mi = appcontrol->mime;
6238
6239                 sa = appcontrol->subapp;
6240                 for (; sa; sa = sa->next)
6241                         sc = sc + 1;
6242                 sa = appcontrol->subapp;
6243
6244                 operation = (char **)calloc(oc, sizeof(char *));
6245                 for (i = 0; i < oc; i++) {
6246                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
6247                         op = op->next;
6248                 }
6249
6250                 uri = (char **)calloc(uc, sizeof(char *));
6251                 for (i = 0; i < uc; i++) {
6252                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
6253                         ui = ui->next;
6254                 }
6255
6256                 mime = (char **)calloc(mc, sizeof(char *));
6257                 for (i = 0; i < mc; i++) {
6258                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
6259                         mi = mi->next;
6260                 }
6261
6262                 subapp = (char **)calloc(sc, sizeof(char *));
6263                 for (i = 0; i < sc; i++) {
6264                         subapp[i] = strndup(sa->name, PKG_STRING_LEN_MAX - 1);
6265                         sa = sa->next;
6266                 }
6267
6268                 /*populate appcontrol handle*/
6269                 ptr->operation_count = oc;
6270                 ptr->uri_count = uc;
6271                 ptr->mime_count = mc;
6272                 ptr->subapp_count = sc;
6273                 ptr->operation = operation;
6274                 ptr->uri = uri;
6275                 ptr->mime = mime;
6276                 ptr->subapp = subapp;
6277
6278                 ret = appcontrol_func((void *)ptr, user_data);
6279                 for (i = 0; i < oc; i++) {
6280                         if (operation[i]) {
6281                                 free(operation[i]);
6282                                 operation[i] = NULL;
6283                         }
6284                 }
6285                 if (operation) {
6286                         free(operation);
6287                         operation = NULL;
6288                 }
6289                 for (i = 0; i < uc; i++) {
6290                         if (uri[i]) {
6291                                 free(uri[i]);
6292                                 uri[i] = NULL;
6293                         }
6294                 }
6295                 if (uri) {
6296                         free(uri);
6297                         uri = NULL;
6298                 }
6299                 for (i = 0; i < mc; i++) {
6300                         if (mime[i]) {
6301                                 free(mime[i]);
6302                                 mime[i] = NULL;
6303                         }
6304                 }
6305                 if (mime) {
6306                         free(mime);
6307                         mime = NULL;
6308                 }
6309                 for (i = 0; i < sc; i++) {
6310                         if (subapp[i]) {
6311                                 free(subapp[i]);
6312                                 subapp[i] = NULL;
6313                         }
6314                 }
6315                 if (subapp) {
6316                         free(subapp);
6317                         subapp = NULL;
6318                 }
6319                 if (ret < 0)
6320                         break;
6321                 uc = 0;
6322                 mc = 0;
6323                 oc = 0;
6324                 sc = 0;
6325         }
6326         pkgmgr_parser_free_manifest_xml(mfx);
6327         if (ptr) {
6328                 free(ptr);
6329                 ptr = NULL;
6330         }
6331         return PMINFO_R_OK;
6332 }
6333
6334 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h  handle, bool *nodisplay)
6335 {
6336         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6337         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6338         char *val = NULL;
6339         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6340         val = (char *)info->uiapp_info->nodisplay;
6341         if (val) {
6342                 if (strcasecmp(val, "true") == 0)
6343                         *nodisplay = 1;
6344                 else if (strcasecmp(val, "false") == 0)
6345                         *nodisplay = 0;
6346                 else
6347                         *nodisplay = 0;
6348         }
6349         return PMINFO_R_OK;
6350 }
6351
6352 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h  handle, bool *multiple)
6353 {
6354         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6355         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6356         char *val = NULL;
6357         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6358         val = (char *)info->uiapp_info->multiple;
6359         if (val) {
6360                 if (strcasecmp(val, "true") == 0)
6361                         *multiple = 1;
6362                 else if (strcasecmp(val, "false") == 0)
6363                         *multiple = 0;
6364                 else
6365                         *multiple = 0;
6366         }
6367         return PMINFO_R_OK;
6368 }
6369
6370 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
6371 {
6372         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6373         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6374         char *val = NULL;
6375         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6376         val = (char *)info->uiapp_info->indicatordisplay;
6377         if (val) {
6378                 if (strcasecmp(val, "true") == 0){
6379                         *indicator_disp = 1;
6380                 }else if (strcasecmp(val, "false") == 0){
6381                         *indicator_disp = 0;
6382                 }else{
6383                         *indicator_disp = 0;
6384                 }
6385         }
6386         return PMINFO_R_OK;
6387 }
6388
6389 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
6390 {
6391         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6392         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6393         char *val = NULL;
6394         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6395         val = (char *)info->uiapp_info->taskmanage;
6396         if (val) {
6397                 if (strcasecmp(val, "true") == 0)
6398                         *taskmanage = 1;
6399                 else if (strcasecmp(val, "false") == 0)
6400                         *taskmanage = 0;
6401                 else
6402                         *taskmanage = 0;
6403         }
6404         return PMINFO_R_OK;
6405 }
6406
6407 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
6408 {
6409         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6410         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6411         char *val = NULL;
6412         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6413         if (info->app_component == PMINFO_UI_APP)
6414                 val = (char *)info->uiapp_info->enabled;
6415         else if (info->app_component == PMINFO_SVC_APP)
6416                 val = (char *)info->uiapp_info->enabled;
6417         else {
6418                 _LOGE("invalid component type\n");
6419                 return PMINFO_R_EINVAL;
6420         }
6421
6422         if (val) {
6423                 if (strcasecmp(val, "true") == 0)
6424                         *enabled = 1;
6425                 else if (strcasecmp(val, "false") == 0)
6426                         *enabled = 0;
6427                 else
6428                         *enabled = 1;
6429         }
6430         return PMINFO_R_OK;
6431
6432 }
6433
6434 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
6435 {
6436         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6437         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6438         char *val = NULL;
6439         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6440         val = (char *)info->svcapp_info->onboot;
6441         if (val) {
6442                 if (strcasecmp(val, "true") == 0)
6443                         *onboot = 1;
6444                 else if (strcasecmp(val, "false") == 0)
6445                         *onboot = 0;
6446                 else
6447                         *onboot = 0;
6448         }
6449         return PMINFO_R_OK;
6450 }
6451
6452 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
6453 {
6454         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6455         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6456         char *val = NULL;
6457         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6458         val = (char *)info->svcapp_info->autorestart;
6459         if (val) {
6460                 if (strcasecmp(val, "true") == 0)
6461                         *autorestart = 1;
6462                 else if (strcasecmp(val, "false") == 0)
6463                         *autorestart = 0;
6464                 else
6465                         *autorestart = 0;
6466         }
6467         return PMINFO_R_OK;
6468 }
6469
6470 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
6471 {
6472         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6473         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6474         char *val = NULL;
6475         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6476         val = (char *)info->uiapp_info->mainapp;
6477         if (val) {
6478                 if (strcasecmp(val, "true") == 0)
6479                         *mainapp = 1;
6480                 else if (strcasecmp(val, "false") == 0)
6481                         *mainapp = 0;
6482                 else
6483                         *mainapp = 0;
6484         }
6485         return PMINFO_R_OK;
6486 }
6487
6488 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
6489 {
6490         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6491         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6492         char *val = NULL;
6493         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6494         val = (char *)info->uiapp_info->preload;
6495         if (val) {
6496                 if (strcasecmp(val, "true") == 0)
6497                         *preload = 1;
6498                 else if (strcasecmp(val, "false") == 0)
6499                         *preload = 0;
6500                 else
6501                         *preload = 0;
6502         }
6503         return PMINFO_R_OK;
6504 }
6505
6506 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
6507 {
6508         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6509         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6510         char *val = NULL;
6511         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6512         val = (char *)info->uiapp_info->submode;
6513         if (val) {
6514                 if (strcasecmp(val, "true") == 0)
6515                         *submode = 1;
6516                 else if (strcasecmp(val, "false") == 0)
6517                         *submode = 0;
6518                 else
6519                         *submode = 0;
6520         }
6521         return PMINFO_R_OK;
6522 }
6523
6524 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
6525 {
6526         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6527         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
6528         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
6529
6530         category_x *ptr = NULL;
6531         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6532
6533         *exist = 0;
6534
6535         ptr = info->uiapp_info->category;
6536
6537         for (; ptr; ptr = ptr->next) {
6538                 if (ptr->name) {
6539                         if (strcasecmp(ptr->name, category) == 0)
6540                         {
6541                                 *exist = 1;
6542                                 break;
6543                         }
6544                 }
6545         }
6546
6547         return PMINFO_R_OK;
6548 }
6549
6550 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h  handle)
6551 {
6552         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6553         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6554         __cleanup_appinfo(info);
6555         return PMINFO_R_OK;
6556 }
6557
6558 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
6559 {
6560         return (pkgmgrinfo_pkginfo_filter_create(handle));
6561 }
6562
6563 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
6564 {
6565         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
6566 }
6567
6568 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
6569                                 const char *property, const int value)
6570 {
6571         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6572         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6573         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
6574         char *val = NULL;
6575         GSList *link = NULL;
6576         int prop = -1;
6577         prop = _pminfo_appinfo_convert_to_prop_int(property);
6578         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
6579                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
6580                 _LOGE("Invalid Integer Property\n");
6581                 return PMINFO_R_EINVAL;
6582         }
6583         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6584         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6585         if (node == NULL) {
6586                 _LOGE("Out of Memory!!!\n");
6587                 return PMINFO_R_ERROR;
6588         }
6589         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
6590         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
6591         if (val == NULL) {
6592                 _LOGE("Out of Memory\n");
6593                 free(node);
6594                 node = NULL;
6595                 return PMINFO_R_ERROR;
6596         }
6597         node->prop = prop;
6598         node->value = val;
6599         /*If API is called multiple times for same property, we should override the previous values.
6600         Last value set will be used for filtering.*/
6601         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6602         if (link)
6603                 filter->list = g_slist_delete_link(filter->list, link);
6604         filter->list = g_slist_append(filter->list, (gpointer)node);
6605         return PMINFO_R_OK;
6606
6607 }
6608
6609 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
6610                                 const char *property, const bool value)
6611 {
6612         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6613         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6614         char *val = NULL;
6615         GSList *link = NULL;
6616         int prop = -1;
6617         prop = _pminfo_appinfo_convert_to_prop_bool(property);
6618         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
6619                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
6620                 _LOGE("Invalid Boolean Property\n");
6621                 return PMINFO_R_EINVAL;
6622         }
6623         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6624         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6625         if (node == NULL) {
6626                 _LOGE("Out of Memory!!!\n");
6627                 return PMINFO_R_ERROR;
6628         }
6629         if (value)
6630                 val = strndup("('true','True')", 15);
6631         else
6632                 val = strndup("('false','False')", 17);
6633         if (val == NULL) {
6634                 _LOGE("Out of Memory\n");
6635                 free(node);
6636                 node = NULL;
6637                 return PMINFO_R_ERROR;
6638         }
6639         node->prop = prop;
6640         node->value = val;
6641         /*If API is called multiple times for same property, we should override the previous values.
6642         Last value set will be used for filtering.*/
6643         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6644         if (link)
6645                 filter->list = g_slist_delete_link(filter->list, link);
6646         filter->list = g_slist_append(filter->list, (gpointer)node);
6647         return PMINFO_R_OK;
6648
6649 }
6650
6651 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
6652                                 const char *property, const char *value)
6653 {
6654         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6655         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6656         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6657         char *val = NULL;
6658         pkgmgrinfo_node_x *ptr = NULL;
6659         char prev[PKG_STRING_LEN_MAX] = {'\0'};
6660         char temp[PKG_STRING_LEN_MAX] = {'\0'};
6661         GSList *link = NULL;
6662         int prop = -1;
6663         prop = _pminfo_appinfo_convert_to_prop_str(property);
6664         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
6665                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
6666                 _LOGE("Invalid String Property\n");
6667                 return PMINFO_R_EINVAL;
6668         }
6669         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6670         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6671         if (node == NULL) {
6672                 _LOGE("Out of Memory!!!\n");
6673                 return PMINFO_R_ERROR;
6674         }
6675         node->prop = prop;
6676         switch (prop) {
6677         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
6678                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
6679                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
6680                 else
6681                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
6682                 node->value = val;
6683                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6684                 if (link)
6685                         filter->list = g_slist_delete_link(filter->list, link);
6686                 filter->list = g_slist_append(filter->list, (gpointer)node);
6687                 break;
6688         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
6689         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
6690         case E_PMINFO_APPINFO_PROP_APP_URI:
6691         case E_PMINFO_APPINFO_PROP_APP_MIME:
6692                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
6693                 if (val == NULL) {
6694                         _LOGE("Out of Memory\n");
6695                         free(node);
6696                         node = NULL;
6697                         return PMINFO_R_ERROR;
6698                 }
6699                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6700                 if (link) {
6701                         ptr = (pkgmgrinfo_node_x *)link->data;
6702                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
6703                         _LOGE("Previous value is %s\n", prev);
6704                         filter->list = g_slist_delete_link(filter->list, link);
6705                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
6706                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6707                         _LOGE("New value is %s\n", val);
6708                         node->value = val;
6709                         filter->list = g_slist_append(filter->list, (gpointer)node);
6710                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6711                 } else {
6712                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
6713                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6714                         _LOGE("First value is %s\n", val);
6715                         node->value = val;
6716                         filter->list = g_slist_append(filter->list, (gpointer)node);
6717                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6718                 }
6719                 break;
6720         default:
6721                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
6722                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6723                 if (link)
6724                         filter->list = g_slist_delete_link(filter->list, link);
6725                 filter->list = g_slist_append(filter->list, (gpointer)node);
6726                 break;
6727         }
6728         return PMINFO_R_OK;
6729 }
6730
6731 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
6732 {
6733         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6734         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6735         char *syslocale = NULL;
6736         char *locale = NULL;
6737         char *condition = NULL;
6738         char *error_message = NULL;
6739         char query[MAX_QUERY_LEN] = {'\0'};
6740         char where[MAX_QUERY_LEN] = {'\0'};
6741         GSList *list;
6742         int ret = 0;
6743
6744         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6745         /*Get current locale*/
6746         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6747         if (syslocale == NULL) {
6748                 _LOGE("current locale is NULL\n");
6749                 return PMINFO_R_ERROR;
6750         }
6751         locale = __convert_system_locale_to_manifest_locale(syslocale);
6752         if (locale == NULL) {
6753                 _LOGE("manifest locale is NULL\n");
6754                 free(syslocale);
6755                 return PMINFO_R_ERROR;
6756         }
6757
6758         ret = __open_manifest_db(uid);
6759         if (ret == -1) {
6760                 _LOGE("Fail to open manifest DB\n");
6761                 free(syslocale);
6762                 free(locale);
6763                 return PMINFO_R_ERROR;
6764         }
6765
6766         /*Start constructing query*/
6767         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
6768
6769         /*Get where clause*/
6770         for (list = filter->list; list; list = g_slist_next(list)) {
6771                 __get_filter_condition(list->data, &condition);
6772                 if (condition) {
6773                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6774                         where[sizeof(where) - 1] = '\0';
6775                         free(condition);
6776                         condition = NULL;
6777                 }
6778                 if (g_slist_next(list)) {
6779                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6780                         where[sizeof(where) - 1] = '\0';
6781                 }
6782         }
6783         _LOGE("where = %s\n", where);
6784         if (strlen(where) > 0) {
6785                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6786                 query[sizeof(query) - 1] = '\0';
6787         }
6788         _LOGE("query = %s\n", query);
6789
6790         /*Execute Query*/
6791         if (SQLITE_OK !=
6792             sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
6793                 _LOGE("Don't execute query = %s error message = %s\n", query,
6794                        error_message);
6795                 sqlite3_free(error_message);
6796                 ret = PMINFO_R_ERROR;
6797                 *count = 0;
6798                 goto err;
6799         }
6800         ret = PMINFO_R_OK;
6801 err:
6802         if (locale) {
6803                 free(locale);
6804                 locale = NULL;
6805         }
6806         if (syslocale) {
6807                 free(syslocale);
6808                 syslocale = NULL;
6809         }
6810         __close_manifest_db();
6811         return ret;
6812 }
6813
6814 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
6815 {
6816         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
6817 }
6818
6819 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
6820                                 pkgmgrinfo_app_list_cb app_cb, void * user_data, uid_t uid)
6821 {
6822         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6823         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6824         char *syslocale = NULL;
6825         char *locale = NULL;
6826         char *condition = NULL;
6827         char *error_message = NULL;
6828         char query[MAX_QUERY_LEN] = {'\0'};
6829         char where[MAX_QUERY_LEN] = {'\0'};
6830         GSList *list;
6831         int ret = 0;
6832         uiapplication_x *ptr1 = NULL;
6833         serviceapplication_x *ptr2 = NULL;
6834         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6835         /*Get current locale*/
6836         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6837         if (syslocale == NULL) {
6838                 _LOGE("current locale is NULL\n");
6839                 return PMINFO_R_ERROR;
6840         }
6841         locale = __convert_system_locale_to_manifest_locale(syslocale);
6842         if (locale == NULL) {
6843                 _LOGE("manifest locale is NULL\n");
6844                 free(syslocale);
6845                 return PMINFO_R_ERROR;
6846         }
6847
6848         ret = __open_manifest_db(uid);
6849         if (ret == -1) {
6850                 _LOGE("Fail to open manifest DB\n");
6851                 free(syslocale);
6852                 free(locale);
6853                 return PMINFO_R_ERROR;
6854         }
6855         /*Start constructing query*/
6856         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_APP, locale);
6857         /*Get where clause*/
6858         for (list = filter->list; list; list = g_slist_next(list)) {
6859                 __get_filter_condition(list->data, &condition);
6860                 if (condition) {
6861                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6862                         where[sizeof(where) - 1] = '\0';
6863                         free(condition);
6864                         condition = NULL;
6865                 }
6866                 if (g_slist_next(list)) {
6867                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6868                         where[sizeof(where) - 1] = '\0';
6869                 }
6870         }
6871         _LOGE("where = %s\n", where);
6872         if (strlen(where) > 0) {
6873                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6874                 query[sizeof(query) - 1] = '\0';
6875         }
6876         _LOGE("query = %s\n", query);
6877         /*To get filtered list*/
6878         pkgmgr_pkginfo_x *info = NULL;
6879         pkgmgr_pkginfo_x *filtinfo = NULL;
6880         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6881         if (info == NULL) {
6882                 _LOGE("Out of Memory!!!\n");
6883                 ret = PMINFO_R_ERROR;
6884                 goto err;
6885         }
6886         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6887         if (info->manifest_info == NULL) {
6888                 _LOGE("Out of Memory!!!\n");
6889                 ret = PMINFO_R_ERROR;
6890                 goto err;
6891         }
6892         /*To get detail app info for each member of filtered list*/
6893         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6894         if (filtinfo == NULL) {
6895                 _LOGE("Out of Memory!!!\n");
6896                 ret = PMINFO_R_ERROR;
6897                 goto err;
6898         }
6899         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6900         if (filtinfo->manifest_info == NULL) {
6901                 _LOGE("Out of Memory!!!\n");
6902                 ret = PMINFO_R_ERROR;
6903                 goto err;
6904         }
6905         pkgmgr_appinfo_x *appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
6906         if (appinfo == NULL) {
6907                 _LOGE("Out of Memory!!!\n");
6908                 ret = PMINFO_R_ERROR;
6909                 goto err;
6910         }
6911         if (SQLITE_OK !=
6912             sqlite3_exec(GET_DB(manifest_db), query, __app_list_cb, (void *)info, &error_message)) {
6913                 _LOGE("Don't execute query = %s error message = %s\n", query,
6914                        error_message);
6915                 sqlite3_free(error_message);
6916                 ret = PMINFO_R_ERROR;
6917                 goto err;
6918         }
6919         memset(query, '\0', MAX_QUERY_LEN);
6920         if (info->manifest_info->uiapplication) {
6921                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
6922                 info->manifest_info->uiapplication = ptr1;
6923         }
6924         if (info->manifest_info->serviceapplication) {
6925                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
6926                 info->manifest_info->serviceapplication = ptr2;
6927         }
6928         /*Filtered UI Apps*/
6929         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
6930         {
6931                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6932                                                         ptr1->appid, "uiapp");
6933                 if (SQLITE_OK !=
6934                 sqlite3_exec(GET_DB(manifest_db), query, __uiapp_list_cb, (void *)filtinfo, &error_message)) {
6935                         _LOGE("Don't execute query = %s error message = %s\n", query,
6936                                error_message);
6937                         sqlite3_free(error_message);
6938                         ret = PMINFO_R_ERROR;
6939                         goto err;
6940                 }
6941         }
6942         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
6943         {
6944                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6945                                                         ptr2->appid, "svcapp");
6946                 if (SQLITE_OK !=
6947                 sqlite3_exec(GET_DB(manifest_db), query, __svcapp_list_cb, (void *)filtinfo, &error_message)) {
6948                         _LOGE("Don't execute query = %s error message = %s\n", query,
6949                                error_message);
6950                         sqlite3_free(error_message);
6951                         ret = PMINFO_R_ERROR;
6952                         goto err;
6953                 }
6954         }
6955         if (filtinfo->manifest_info->uiapplication) {
6956                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
6957                 filtinfo->manifest_info->uiapplication = ptr1;
6958         }
6959         /*If the callback func return < 0 we break and no more call back is called*/
6960         while(ptr1 != NULL)
6961         {
6962                 appinfo->locale = strdup(locale);
6963                 appinfo->uiapp_info = ptr1;
6964                 appinfo->app_component = PMINFO_UI_APP;
6965                 ret = app_cb((void *)appinfo, user_data);
6966                 if (ret < 0)
6967                         break;
6968                 ptr1 = ptr1->next;
6969         }
6970         /*Filtered Service Apps*/
6971         if (filtinfo->manifest_info->serviceapplication) {
6972                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
6973                 filtinfo->manifest_info->serviceapplication = ptr2;
6974         }
6975         /*If the callback func return < 0 we break and no more call back is called*/
6976         while(ptr2 != NULL)
6977         {
6978                 appinfo->locale = strdup(locale);
6979                 appinfo->svcapp_info = ptr2;
6980                 appinfo->app_component = PMINFO_SVC_APP;
6981                 ret = app_cb((void *)appinfo, user_data);
6982                 if (ret < 0)
6983                         break;
6984                 ptr2 = ptr2->next;
6985         }
6986         ret = PMINFO_R_OK;
6987 err:
6988         if (locale) {
6989                 free(locale);
6990                 locale = NULL;
6991         }
6992         if (syslocale) {
6993                 free(syslocale);
6994                 syslocale = NULL;
6995         }
6996         __close_manifest_db();
6997         if (appinfo) {
6998                 free(appinfo);
6999                 appinfo = NULL;
7000         }
7001         __cleanup_pkginfo(info);
7002         __cleanup_pkginfo(filtinfo);
7003         return ret;
7004 }
7005
7006 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
7007                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
7008 {
7009         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
7010 }
7011
7012 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
7013 {
7014         return (pkgmgrinfo_pkginfo_filter_create(handle));
7015 }
7016
7017 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
7018 {
7019         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
7020 }
7021
7022 API int pkgmgrinfo_appinfo_metadata_filter_add(pkgmgrinfo_appinfo_metadata_filter_h handle,
7023                 const char *key, const char *value)
7024 {
7025         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
7026         retvm_if(key == NULL, PMINFO_R_EINVAL, "metadata key supplied is NULL\n");
7027         /*value can be NULL. In that case all apps with specified key should be displayed*/
7028         int ret = 0;
7029         char *k = NULL;
7030         char *v = NULL;
7031         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
7032         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
7033         retvm_if(node == NULL, PMINFO_R_ERROR, "Out of Memory!!!\n");
7034         k = strdup(key);
7035         tryvm_if(k == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7036         node->key = k;
7037         if (value) {
7038                 v = strdup(value);
7039                 tryvm_if(v == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7040         }
7041         node->value = v;
7042         /*If API is called multiple times, we should OR all conditions.*/
7043         filter->list = g_slist_append(filter->list, (gpointer)node);
7044         /*All memory will be freed in destroy API*/
7045         return PMINFO_R_OK;
7046 catch:
7047         if (node) {
7048                 if (node->key) {
7049                         free(node->key);
7050                         node->key = NULL;
7051                 }
7052                 if (node->value) {
7053                         free(node->value);
7054                         node->value = NULL;
7055                 }
7056                 free(node);
7057                 node = NULL;
7058         }
7059         return ret;
7060 }
7061
7062 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
7063                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
7064 {
7065         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
7066         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Callback function supplied is NULL\n");
7067         char *syslocale = NULL;
7068         char *locale = NULL;
7069         char *condition = NULL;
7070         char *error_message = NULL;
7071         char query[MAX_QUERY_LEN] = {'\0'};
7072         char where[MAX_QUERY_LEN] = {'\0'};
7073         GSList *list;
7074         int ret = 0;
7075         pkgmgr_pkginfo_x *info = NULL;
7076         pkgmgr_pkginfo_x *filtinfo = NULL;
7077         pkgmgr_appinfo_x *appinfo = NULL;
7078         uiapplication_x *ptr1 = NULL;
7079         serviceapplication_x *ptr2 = NULL;
7080         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
7081
7082         /*Get current locale*/
7083         syslocale = vconf_get_str(VCONFKEY_LANGSET);
7084         retvm_if(syslocale == NULL, PMINFO_R_ERROR, "current locale is NULL\n");
7085         locale = __convert_system_locale_to_manifest_locale(syslocale);
7086         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL\n");
7087
7088         ret = __open_manifest_db(uid);
7089         if (ret == -1) {
7090                 _LOGE("Fail to open manifest DB\n");
7091                 free(syslocale);
7092                 free(locale);
7093                 return PMINFO_R_ERROR;
7094         }
7095         /*Start constructing query*/
7096         memset(where, '\0', MAX_QUERY_LEN);
7097         memset(query, '\0', MAX_QUERY_LEN);
7098         snprintf(query, MAX_QUERY_LEN - 1, METADATA_FILTER_QUERY_SELECT_CLAUSE);
7099         /*Get where clause*/
7100         for (list = filter->list; list; list = g_slist_next(list)) {
7101                 __get_metadata_filter_condition(list->data, &condition);
7102                 if (condition) {
7103                         strncat(where, condition, sizeof(where) - strlen(where) -1);
7104                         free(condition);
7105                         condition = NULL;
7106                 }
7107                 if (g_slist_next(list)) {
7108                         strncat(where, METADATA_FILTER_QUERY_UNION_CLAUSE, sizeof(where) - strlen(where) - 1);
7109                 }
7110         }
7111         _LOGE("where = %s (%d)\n", where, strlen(where));
7112         if (strlen(where) > 0) {
7113                 strncat(query, where, sizeof(query) - strlen(query) - 1);
7114         }
7115         _LOGE("query = %s (%d)\n", query, strlen(query));
7116         /*To get filtered list*/
7117         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
7118         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7119
7120         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
7121         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7122
7123         /*To get detail app info for each member of filtered list*/
7124         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
7125         tryvm_if(filtinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7126
7127         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
7128         tryvm_if(filtinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7129
7130         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
7131         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
7132
7133         ret = sqlite3_exec(GET_DB(manifest_db), query, __app_list_cb, (void *)info, &error_message);
7134         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7135         memset(query, '\0', MAX_QUERY_LEN);
7136
7137         if (info->manifest_info->uiapplication) {
7138                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
7139                 info->manifest_info->uiapplication = ptr1;
7140         }
7141         if (info->manifest_info->serviceapplication) {
7142                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
7143                 info->manifest_info->serviceapplication = ptr2;
7144         }
7145
7146         /*UI Apps*/
7147         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
7148         {
7149                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7150                                                         ptr1->appid, "uiapp");
7151                 ret = sqlite3_exec(GET_DB(manifest_db), query, __uiapp_list_cb, (void *)filtinfo, &error_message);
7152                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7153                 memset(query, '\0', MAX_QUERY_LEN);
7154         }
7155         /*Service Apps*/
7156         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
7157         {
7158                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7159                                                         ptr2->appid, "svcapp");
7160                 ret = sqlite3_exec(GET_DB(manifest_db), query, __svcapp_list_cb, (void *)filtinfo, &error_message);
7161                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7162                 memset(query, '\0', MAX_QUERY_LEN);
7163         }
7164         /*Filtered UI Apps*/
7165         if (filtinfo->manifest_info->uiapplication) {
7166                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
7167                 filtinfo->manifest_info->uiapplication = ptr1;
7168         }
7169         /*If the callback func return < 0 we break and no more call back is called*/
7170         while(ptr1 != NULL)
7171         {
7172                 appinfo->locale = strdup(locale);
7173                 appinfo->uiapp_info = ptr1;
7174                 appinfo->app_component = PMINFO_UI_APP;
7175                 ret = app_cb((void *)appinfo, user_data);
7176                 if (ret < 0)
7177                         break;
7178                 ptr1 = ptr1->next;
7179         }
7180         /*Filtered Service Apps*/
7181         if (filtinfo->manifest_info->serviceapplication) {
7182                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
7183                 filtinfo->manifest_info->serviceapplication = ptr2;
7184         }
7185         /*If the callback func return < 0 we break and no more call back is called*/
7186         while(ptr2 != NULL)
7187         {
7188                 appinfo->locale = strdup(locale);
7189                 appinfo->svcapp_info = ptr2;
7190                 appinfo->app_component = PMINFO_SVC_APP;
7191                 ret = app_cb((void *)appinfo, user_data);
7192                 if (ret < 0)
7193                         break;
7194                 ptr2 = ptr2->next;
7195         }
7196         ret = PMINFO_R_OK;
7197 catch:
7198         if (locale) {
7199                 free(locale);
7200                 locale = NULL;
7201         }
7202         if (syslocale) {
7203                 free(syslocale);
7204                 syslocale = NULL;
7205         }
7206         sqlite3_free(error_message);
7207         __close_manifest_db();
7208         if (appinfo) {
7209                 free(appinfo);
7210                 appinfo = NULL;
7211         }
7212         __cleanup_pkginfo(info);
7213         __cleanup_pkginfo(filtinfo);
7214         return ret;
7215 }
7216
7217 API int pkgmgrinfo_appinfo_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
7218                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
7219 {
7220         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, GLOBAL_USER);
7221 }
7222
7223 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
7224 {
7225         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7226         pkgmgr_certinfo_x *certinfo = NULL;
7227         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
7228         *handle = NULL;
7229         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7230         *handle = (void *)certinfo;
7231         return PMINFO_R_OK;
7232 }
7233
7234 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle, uid_t uid)
7235 {
7236         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7237         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7238         pkgmgr_certinfo_x *certinfo = NULL;
7239         char *error_message = NULL;
7240         int ret = PMINFO_R_OK;
7241         char query[MAX_QUERY_LEN] = {'\0'};
7242         int exist = 0;
7243         int i = 0;
7244
7245         /*Open db.*/
7246         ret = __open_cert_db(uid,"r");
7247         if (ret != SQLITE_OK) {
7248                 _LOGE("connect db [%s] failed!\n");
7249                 ret = PMINFO_R_ERROR;
7250                 goto err;
7251         }
7252         _check_create_Cert_db(GET_DB(cert_db));
7253         /*validate pkgid*/
7254         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7255         if (SQLITE_OK !=
7256             sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
7257                 _LOGE("Don't execute query = %s error message = %s\n", query,
7258                        error_message);
7259                 sqlite3_free(error_message);
7260                 ret = PMINFO_R_ERROR;
7261                 goto err;
7262         }
7263         if (exist == 0) {
7264                 _LOGE("Package not found in DB\n");
7265                 ret = PMINFO_R_ERROR;
7266                 goto err;
7267         }
7268         certinfo = (pkgmgr_certinfo_x *)handle;
7269         /*populate certinfo from DB*/
7270         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
7271         ret = __exec_certinfo_query(query, (void *)certinfo);
7272         if (ret == -1) {
7273                 _LOGE("Package Cert Info DB Information retrieval failed\n");
7274                 ret = PMINFO_R_ERROR;
7275                 goto err;
7276         }
7277         for (i = 0; i < MAX_CERT_TYPE; i++) {
7278                 memset(query, '\0', MAX_QUERY_LEN);
7279                 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
7280                 ret = __exec_certinfo_query(query, (void *)certinfo);
7281                 if (ret == -1) {
7282                         _LOGE("Cert Info DB Information retrieval failed\n");
7283                         ret = PMINFO_R_ERROR;
7284                         goto err;
7285                 }
7286                 if (certinfo->cert_value) {
7287                         (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
7288                         free(certinfo->cert_value);
7289                         certinfo->cert_value = NULL;
7290                 }
7291         }
7292 err:
7293         __close_cert_db();
7294         return ret;
7295 }
7296
7297 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
7298 {
7299         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7300         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7301         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7302         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7303         pkgmgr_certinfo_x *certinfo = NULL;
7304         certinfo = (pkgmgr_certinfo_x *)handle;
7305         if ((certinfo->cert_info)[cert_type])
7306                 *cert_value = (certinfo->cert_info)[cert_type];
7307         else
7308                 *cert_value = NULL;
7309         return PMINFO_R_OK;
7310 }
7311
7312 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
7313 {
7314         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7315         int i = 0;
7316         pkgmgr_certinfo_x *certinfo = NULL;
7317         certinfo = (pkgmgr_certinfo_x *)handle;
7318         if (certinfo->pkgid) {
7319                 free(certinfo->pkgid);
7320                 certinfo->pkgid = NULL;
7321         }
7322         for (i = 0; i < MAX_CERT_TYPE; i++) {
7323                 if ((certinfo->cert_info)[i]) {
7324                         free((certinfo->cert_info)[i]);
7325                         (certinfo->cert_info)[i] = NULL;
7326                 }
7327         }
7328         free(certinfo);
7329         certinfo = NULL;
7330         return PMINFO_R_OK;
7331 }
7332
7333 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
7334 {
7335         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7336         pkgmgr_instcertinfo_x *certinfo = NULL;
7337         *handle = NULL;
7338         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
7339         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7340         *handle = (void *)certinfo;
7341         return PMINFO_R_OK;
7342 }
7343
7344 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
7345 {
7346         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7347         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7348         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7349         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7350         pkgmgr_instcertinfo_x *certinfo = NULL;
7351         certinfo = (pkgmgr_instcertinfo_x *)handle;
7352         (certinfo->cert_info)[cert_type] = strdup(cert_value);
7353         return PMINFO_R_OK;
7354 }
7355
7356 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle, uid_t uid)
7357 {
7358         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7359         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7360         char *error_message = NULL;
7361         char query[MAX_QUERY_LEN] = {'\0'};
7362         char *vquery = NULL;
7363         int len = 0;
7364         int i = 0;
7365         int j = 0;
7366         int c = 0;
7367         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
7368         int newid = 0;
7369         int is_new = 0;
7370         int exist = -1;
7371         int ret = -1;
7372         int maxid = 0;
7373         int flag = 0;
7374         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
7375         pkgmgr_certindexinfo_x *indexinfo = NULL;
7376         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
7377         if (indexinfo == NULL) {
7378                 _LOGE("Out of Memory!!!");
7379                 return PMINFO_R_ERROR;
7380         }
7381         info->pkgid = strdup(pkgid);
7382
7383         /*Open db.*/
7384         ret =__open_cert_db(uid, "w");
7385         if (ret != 0) {
7386                 ret = PMINFO_R_ERROR;
7387                 _LOGE("Failed to open cert db \n");
7388                 goto err;
7389         }
7390         _check_create_Cert_db(GET_DB(cert_db));
7391         /*Begin Transaction*/
7392         ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7393         if (ret == -1) {
7394                 _LOGE("Failed to begin transaction %s\n");
7395                 ret = PMINFO_R_ERROR;
7396                 goto err;
7397         }
7398
7399         /*Check if request is to insert/update*/
7400         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7401         if (SQLITE_OK !=
7402             sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
7403                 _LOGE("Don't execute query = %s error message = %s\n", query,
7404                        error_message);
7405                 sqlite3_free(error_message);
7406                 ret = PMINFO_R_ERROR;
7407                 goto err;
7408         }
7409         if (exist) {
7410                 /*Update request.
7411                 We cant just issue update query directly. We need to manage index table also.
7412                 Hence it is better to delete and insert again in case of update*/
7413                 ret = __delete_certinfo(pkgid, uid);
7414                 if (ret < 0)
7415                         _LOGE("Certificate Deletion Failed\n");
7416         }
7417         for (i = 0; i < MAX_CERT_TYPE; i++) {
7418                 if ((info->cert_info)[i]) {
7419                         for (j = 0; j < i; j++) {
7420                                 if ( (info->cert_info)[j]) {
7421                                         if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
7422                                                 (info->cert_id)[i] = (info->cert_id)[j];
7423                                                 (info->is_new)[i] = 0;
7424                                                 (info->ref_count)[i] = (info->ref_count)[j];
7425                                                 break;
7426                                         }
7427                                 }
7428                         }
7429                         if (j < i)
7430                                 continue;
7431                         memset(query, '\0', MAX_QUERY_LEN);
7432                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info " \
7433                                 "where cert_info='%s'",(info->cert_info)[i]);
7434                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
7435                         if (ret == -1) {
7436                                 _LOGE("Cert Info DB Information retrieval failed\n");
7437                                 ret = PMINFO_R_ERROR;
7438                                 goto err;
7439                         }
7440                         if (indexinfo->cert_id == 0) {
7441                                 /*New certificate. Get newid*/
7442                                 memset(query, '\0', MAX_QUERY_LEN);
7443                                 snprintf(query, MAX_QUERY_LEN, "select MAX(cert_id) from package_cert_index_info ");
7444                                 if (SQLITE_OK !=
7445                                     sqlite3_exec(GET_DB(cert_db), query, __maxid_cb, (void *)&newid, &error_message)) {
7446                                         _LOGE("Don't execute query = %s error message = %s\n", query,
7447                                                error_message);
7448                                         sqlite3_free(error_message);
7449                                         ret = PMINFO_R_ERROR;
7450                                         goto err;
7451                                 }
7452                                 newid = newid + 1;
7453                                 if (flag == 0) {
7454                                         maxid = newid;
7455                                         flag = 1;
7456                                 }
7457                                 indexinfo->cert_id = maxid;
7458                                 indexinfo->cert_ref_count = 1;
7459                                 is_new = 1;
7460                                 maxid = maxid + 1;
7461                         }
7462                         (info->cert_id)[i] = indexinfo->cert_id;
7463                         (info->is_new)[i] = is_new;
7464                         (info->ref_count)[i] = indexinfo->cert_ref_count;
7465                         indexinfo->cert_id = 0;
7466                         indexinfo->cert_ref_count = 0;
7467                         is_new = 0;
7468                 }
7469         }
7470         len = MAX_QUERY_LEN;
7471         for (i = 0; i < MAX_CERT_TYPE; i++) {
7472                 if ((info->cert_info)[i])
7473                         len+= strlen((info->cert_info)[i]);
7474         }
7475         vquery = (char *)calloc(1, len);
7476         /*insert*/
7477         snprintf(vquery, len,
7478                  "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
7479                 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
7480                 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
7481                  info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
7482                 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
7483                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
7484                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
7485                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
7486         if (SQLITE_OK !=
7487             sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
7488                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7489                        error_message);
7490                 sqlite3_free(error_message);
7491                 ret = PMINFO_R_ERROR;
7492                 goto err;
7493         }
7494         /*Update index table info*/
7495         /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
7496         for (i = 0; i < MAX_CERT_TYPE; i++) {
7497                 if ((info->cert_info)[i]) {
7498                         memset(vquery, '\0', len);
7499                         if ((info->is_new)[i]) {
7500                                 snprintf(vquery, len, "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
7501                                 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
7502                                 unique_id[c++] = (info->cert_id)[i];
7503                         } else {
7504                                 /*Update*/
7505                                 for (j = 0; j < MAX_CERT_TYPE; j++) {
7506                                         if ((info->cert_id)[i] == unique_id[j]) {
7507                                                 /*Ref count has already been increased. Just continue*/
7508                                                 break;
7509                                         }
7510                                 }
7511                                 if (j == MAX_CERT_TYPE)
7512                                         unique_id[c++] = (info->cert_id)[i];
7513                                 else
7514                                         continue;
7515                                 snprintf(vquery, len, "update package_cert_index_info set cert_ref_count=%d " \
7516                                 "where cert_id=%d",  (info->ref_count)[i] + 1, (info->cert_id)[i]);
7517                         }
7518                         if (SQLITE_OK !=
7519                             sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
7520                                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7521                                        error_message);
7522                                 sqlite3_free(error_message);
7523                                 ret = PMINFO_R_ERROR;
7524                                 goto err;
7525                         }
7526                 }
7527         }
7528         /*Commit transaction*/
7529         ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
7530         if (ret != SQLITE_OK) {
7531                 _LOGE("Failed to commit transaction, Rollback now\n");
7532                 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
7533                 ret = PMINFO_R_ERROR;
7534                 goto err;
7535         }
7536
7537         ret =  PMINFO_R_OK;
7538 err:
7539         __close_cert_db();
7540         if (vquery) {
7541                 free(vquery);
7542                 vquery = NULL;
7543         }
7544         if (indexinfo) {
7545                 free(indexinfo);
7546                 indexinfo = NULL;
7547         }
7548         return ret;
7549 }
7550
7551 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
7552 {
7553         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7554         int i = 0;
7555         pkgmgr_instcertinfo_x *certinfo = NULL;
7556         certinfo = (pkgmgr_instcertinfo_x *)handle;
7557         if (certinfo->pkgid) {
7558                 free(certinfo->pkgid);
7559                 certinfo->pkgid = NULL;
7560         }
7561         for (i = 0; i < MAX_CERT_TYPE; i++) {
7562                 if ((certinfo->cert_info)[i]) {
7563                         free((certinfo->cert_info)[i]);
7564                         (certinfo->cert_info)[i] = NULL;
7565                 }
7566         }
7567         free(certinfo);
7568         certinfo = NULL;
7569         return PMINFO_R_OK;
7570 }
7571
7572 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
7573 {
7574         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7575         int ret = -1;
7576         /*Open db.*/
7577         ret = __open_cert_db(uid, "w");
7578         if (ret != 0) {
7579                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
7580                 ret = PMINFO_R_ERROR;
7581                 goto err;
7582         }
7583         _check_create_Cert_db(GET_DB(cert_db));
7584         /*Begin Transaction*/
7585         ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7586         if (ret != SQLITE_OK) {
7587                 _LOGE("Failed to begin transaction\n");
7588                 ret = PMINFO_R_ERROR;
7589                 goto err;
7590         }
7591         _LOGE("Transaction Begin\n");
7592         ret = __delete_certinfo(pkgid, uid);
7593         if (ret < 0) {
7594                 _LOGE("Certificate Deletion Failed\n");
7595         } else {
7596                 _LOGE("Certificate Deletion Success\n");
7597         }
7598         /*Commit transaction*/
7599         ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
7600         if (ret != SQLITE_OK) {
7601                 _LOGE("Failed to commit transaction, Rollback now\n");
7602                 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
7603                 ret = PMINFO_R_ERROR;
7604                 goto err;
7605         }
7606         _LOGE("Transaction Commit and End\n");
7607         ret = PMINFO_R_OK;
7608 err:
7609         __close_cert_db();
7610         return ret;
7611 }
7612
7613
7614 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
7615 {
7616         return pkgmgrinfo_delete_usr_certinfo(pkgid, GLOBAL_USER);
7617 }
7618
7619 API int pkgmgrinfo_create_pkgusrdbinfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkgdbinfo_h *handle)
7620 {
7621         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7622         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7623
7624         char *manifest = NULL;
7625         manifest_x *mfx = NULL;
7626         *handle = NULL;
7627         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
7628         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7629
7630         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
7631         if (manifest) {
7632                 free(manifest);
7633                 manifest = NULL;
7634         }
7635         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7636
7637         *handle = (void *)mfx;
7638
7639         return PMINFO_R_OK;
7640 }
7641
7642 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
7643 {
7644         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7645         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7646
7647         char *manifest = NULL;
7648         manifest_x *mfx = NULL;
7649         *handle = NULL;
7650         manifest = pkgmgr_parser_get_manifest_file(pkgid);
7651         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7652
7653         mfx = pkgmgr_parser_process_manifest_xml(manifest);
7654         if (manifest) {
7655                 free(manifest);
7656                 manifest = NULL;
7657         }
7658         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7659
7660         *handle = (void *)mfx;
7661
7662         return PMINFO_R_OK;
7663 }
7664
7665 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
7666 {
7667         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
7668         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7669
7670         int len = strlen(type);
7671         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7672
7673         manifest_x *mfx = (manifest_x *)handle;
7674
7675         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
7676         return PMINFO_R_OK;
7677 }
7678
7679 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
7680 {
7681         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
7682         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7683
7684         int len = strlen(version);
7685         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7686
7687         manifest_x *mfx = (manifest_x *)handle;
7688
7689         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
7690         return PMINFO_R_OK;
7691 }
7692
7693 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7694 {
7695         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7696         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7697
7698         manifest_x *mfx = (manifest_x *)handle;
7699
7700         if (location == INSTALL_INTERNAL)
7701                 strcpy(mfx->installlocation, "internal-only");
7702         else if (location == INSTALL_EXTERNAL)
7703                 strcpy(mfx->installlocation, "prefer-external");
7704
7705         return PMINFO_R_OK;
7706 }
7707
7708 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
7709 {
7710         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7711         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7712
7713         manifest_x *mfx = (manifest_x *)handle;
7714
7715         mfx->package_size = strdup(size);
7716
7717         return PMINFO_R_OK;
7718 }
7719
7720 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
7721 {
7722         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7723         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7724
7725         int len = strlen(label_txt);
7726         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7727
7728         manifest_x *mfx = (manifest_x *)handle;
7729
7730         label_x *label = calloc(1, sizeof(label_x));
7731         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7732
7733         LISTADD(mfx->label, label);
7734         if (locale)
7735                 mfx->label->lang = strdup(locale);
7736         else
7737                 mfx->label->lang = strdup(DEFAULT_LOCALE);
7738         mfx->label->text = strdup(label_txt);
7739
7740         return PMINFO_R_OK;
7741 }
7742
7743 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
7744 {
7745         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7746         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7747
7748         int len = strlen(icon_txt);
7749         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7750
7751         manifest_x *mfx = (manifest_x *)handle;
7752
7753         icon_x *icon = calloc(1, sizeof(icon_x));
7754         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7755
7756         LISTADD(mfx->icon, icon);
7757         if (locale)
7758                 mfx->icon->lang = strdup(locale);
7759         else
7760                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
7761         mfx->icon->text = strdup(icon_txt);
7762
7763         return PMINFO_R_OK;
7764 }
7765
7766 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
7767 {
7768         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7769         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7770
7771         int len = strlen(desc_txt);
7772         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7773
7774         manifest_x *mfx = (manifest_x *)handle;
7775
7776         description_x *description = calloc(1, sizeof(description_x));
7777         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7778
7779         LISTADD(mfx->description, description);
7780         if (locale)
7781                 mfx->description->lang = strdup(locale);
7782         else
7783                 mfx->description->lang = strdup(DEFAULT_LOCALE);
7784         mfx->description->text = strdup(desc_txt);
7785
7786         return PMINFO_R_OK;
7787 }
7788
7789 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
7790                 const char *author_email, const char *author_href, const char *locale)
7791 {
7792         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7793         manifest_x *mfx = (manifest_x *)handle;
7794         author_x *author = calloc(1, sizeof(author_x));
7795         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7796
7797         LISTADD(mfx->author, author);
7798         if (author_name)
7799                 mfx->author->text = strdup(author_name);
7800         if (author_email)
7801                 mfx->author->email = strdup(author_email);
7802         if (author_href)
7803                 mfx->author->href = strdup(author_href);
7804         if (locale)
7805                 mfx->author->lang = strdup(locale);
7806         else
7807                 mfx->author->lang = strdup(DEFAULT_LOCALE);
7808         return PMINFO_R_OK;
7809 }
7810
7811 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
7812 {
7813         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7814         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7815
7816         manifest_x *mfx = (manifest_x *)handle;
7817
7818         if (removable == 0)
7819                 strcpy(mfx->removable, "false");
7820         else if (removable == 1)
7821                 strcpy(mfx->removable, "true");
7822
7823         return PMINFO_R_OK;
7824 }
7825
7826 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
7827 {
7828         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7829         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7830
7831         manifest_x *mfx = (manifest_x *)handle;
7832
7833         if (preload == 0)
7834                 strcpy(mfx->preload, "false");
7835         else if (preload == 1)
7836                 strcpy(mfx->preload, "true");
7837
7838         return PMINFO_R_OK;
7839 }
7840
7841 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7842 {
7843         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7844         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7845
7846         manifest_x *mfx = (manifest_x *)handle;
7847
7848         if (location == INSTALL_INTERNAL)
7849                 strcpy(mfx->installed_storage, "installed_internal");
7850         else if (location == INSTALL_EXTERNAL)
7851                 strcpy(mfx->installed_storage, "installed_external");
7852
7853         return PMINFO_R_OK;
7854 }
7855
7856 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7857 {
7858         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7859
7860         int ret = 0;
7861         manifest_x *mfx = NULL;
7862         mfx = (manifest_x *)handle;
7863
7864         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
7865         if (ret == 0) {
7866                 _LOGE("Successfully stored info in DB\n");
7867                 return PMINFO_R_OK;
7868         } else {
7869                 _LOGE("Failed to store info in DB\n");
7870                 return PMINFO_R_ERROR;
7871         }
7872 }
7873
7874 API int pkgmgrinfo_save_pkgusrdbinfo(pkgmgrinfo_pkgdbinfo_h handle, uid_t uid)
7875 {
7876         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7877
7878         int ret = 0;
7879         manifest_x *mfx = NULL;
7880         mfx = (manifest_x *)handle;
7881
7882         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
7883         if (ret == 0) {
7884                 _LOGE("Successfully stored info in DB\n");
7885                 return PMINFO_R_OK;
7886         } else {
7887                 _LOGE("Failed to store info in DB\n");
7888                 return PMINFO_R_ERROR;
7889         }
7890 }
7891
7892 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7893 {
7894         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7895
7896         manifest_x *mfx = NULL;
7897         mfx = (manifest_x *)handle;
7898         pkgmgr_parser_free_manifest_xml(mfx);
7899         return PMINFO_R_OK;
7900 }
7901
7902 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
7903 {
7904         /* Should be implemented later */
7905         return 0;
7906 }
7907
7908 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
7909 {
7910         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
7911         int ret = -1;
7912         char query[MAX_QUERY_LEN] = {'\0'};
7913
7914         /* Open db.*/
7915         ret = __open_manifest_db(uid);
7916         if (ret != SQLITE_OK) {
7917                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
7918                 return PMINFO_R_ERROR;
7919         }
7920
7921         /*Begin transaction*/
7922         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7923         if (ret != SQLITE_OK) {
7924                 _LOGE("Failed to begin transaction\n");
7925                 __close_manifest_db();
7926                 return PMINFO_R_ERROR;
7927         }
7928         _LOGD("Transaction Begin\n");
7929
7930         memset(query, '\0', MAX_QUERY_LEN);
7931         snprintf(query, MAX_QUERY_LEN,
7932                 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
7933
7934         char *error_message = NULL;
7935         if (SQLITE_OK !=
7936             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
7937                 _LOGE("Don't execute query = %s error message = %s\n", query,
7938                        error_message);
7939                 sqlite3_free(error_message);
7940                 return PMINFO_R_ERROR;
7941         }
7942         sqlite3_free(error_message);
7943
7944         /*Commit transaction*/
7945         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
7946         if (ret != SQLITE_OK) {
7947                 _LOGE("Failed to commit transaction. Rollback now\n");
7948                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
7949                 __close_manifest_db();
7950                 return PMINFO_R_ERROR;
7951         }
7952         _LOGD("Transaction Commit and End\n");
7953         __close_manifest_db();
7954         return PMINFO_R_OK;
7955 }
7956
7957 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
7958 {
7959         return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
7960 }
7961
7962 API int pkgmgrinfo_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
7963 {
7964         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7965         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7966         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7967         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7968         int ret = PMINFO_R_OK;
7969         char query[MAX_QUERY_LEN] = {'\0'};
7970         char *error_message = NULL;
7971         pkgmgr_datacontrol_x *data = NULL;
7972
7973         ret = __open_datacontrol_db();
7974         if (ret == -1) {
7975                 _LOGE("Fail to open datacontrol DB\n");
7976                 return PMINFO_R_ERROR;
7977         }
7978
7979         data = (pkgmgr_datacontrol_x *)calloc(1, sizeof(pkgmgr_datacontrol_x));
7980         if (data == NULL) {
7981                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
7982                 __close_datacontrol_db();
7983                 return PMINFO_R_ERROR;
7984         }
7985
7986         snprintf(query, MAX_QUERY_LEN, 
7987                 "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",
7988                 providerid, type);
7989
7990         if (SQLITE_OK !=
7991                 sqlite3_exec(GET_DB(datacontrol_db), query, __datacontrol_cb, (void *)data, &error_message)) {
7992                 _LOGE("Don't execute query = %s error message = %s\n", query,
7993                            error_message);
7994                 sqlite3_free(error_message);
7995                 __close_datacontrol_db();
7996                 return PMINFO_R_ERROR;
7997         }
7998
7999         *appid = (char *)data->appid;
8000         *access = (char *)data->access;
8001         free(data);
8002     __close_datacontrol_db();
8003
8004         return PMINFO_R_OK;
8005 }
8006
8007 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
8008 {
8009         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
8010         int ret = -1;
8011         char query[MAX_QUERY_LEN] = {'\0'};
8012         char *error_message = NULL;
8013         ret = __open_manifest_db(uid);
8014
8015
8016         /*Begin transaction*/
8017         ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
8018         if (ret != SQLITE_OK) {
8019                 _LOGE("Failed to begin transaction\n");
8020                 __close_manifest_db();
8021                 return PMINFO_R_ERROR;
8022         }
8023         _LOGD("Transaction Begin\n");
8024
8025         memset(query, '\0', MAX_QUERY_LEN);
8026         snprintf(query, MAX_QUERY_LEN,
8027                 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
8028
8029         if (SQLITE_OK !=
8030             sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
8031                 _LOGE("Don't execute query = %s error message = %s\n", query,
8032                        error_message);
8033                 sqlite3_free(error_message);
8034                 return PMINFO_R_ERROR;
8035         }
8036
8037         /*Commit transaction*/
8038         ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
8039         if (ret != SQLITE_OK) {
8040                 _LOGE("Failed to commit transaction. Rollback now\n");
8041                 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
8042                 __close_manifest_db();
8043                 return PMINFO_R_ERROR;
8044         }
8045         _LOGD("Transaction Commit and End\n");
8046         __close_manifest_db();
8047         return PMINFO_R_OK;
8048 }
8049
8050 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
8051 {
8052         return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
8053 }
8054
8055 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
8056 {
8057         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
8058         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
8059         char *val = NULL;
8060         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
8061         val = (char *)info->uiapp_info->guestmode_visibility;
8062         if (val) {
8063                 if (strcasecmp(val, "true") == 0){
8064                         *status = 1;
8065                 }else if (strcasecmp(val, "false") == 0){
8066                         *status = 0;
8067                 }else{
8068                         *status = 1;
8069                 }
8070         }
8071         return PMINFO_R_OK;
8072 }
8073
8074 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
8075 {
8076         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
8077         char *val = NULL;
8078         int ret = 0;
8079         char *noti_string = NULL;
8080         int len = 0;
8081         char query[MAX_QUERY_LEN] = {'\0'};
8082         char *errmsg = NULL;
8083         sqlite3 *pkgmgr_parser_db;
8084
8085         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
8086         val = (char *)info->uiapp_info->guestmode_visibility;
8087         if (val ) {
8088                 ret =
8089                   db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
8090                                 SQLITE_OPEN_READWRITE, NULL);
8091
8092                 if (ret != SQLITE_OK) {
8093                         _LOGE("DB Open Failed\n");
8094                         return PMINFO_R_ERROR;
8095                 }
8096
8097                 /*TODO: Write to DB here*/
8098                 if (status == true)
8099                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->uiapp_info->appid);
8100                 else
8101                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->uiapp_info->appid);
8102
8103                 if (SQLITE_OK != sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg)) {
8104                         _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
8105                         free(errmsg);
8106                         sqlite3_close(pkgmgr_parser_db);
8107                         return PMINFO_R_ERROR;
8108                 }else{
8109                         sqlite3_close(pkgmgr_parser_db);
8110                         len = strlen((char *)info->uiapp_info->appid) + 8;
8111                         noti_string = calloc(1, len);
8112                         if (noti_string == NULL){
8113                                 return PMINFO_R_ERROR;
8114                         }
8115                         snprintf(noti_string, len, "update:%s", (char *)info->uiapp_info->appid);
8116                 vconf_set_str(VCONFKEY_AIL_INFO_STATE, noti_string);
8117                         vconf_set_str(VCONFKEY_MENUSCREEN_DESKTOP, noti_string); // duplicate, will be removed
8118                         free(noti_string);
8119                 }
8120         }
8121         return PMINFO_R_OK;
8122 }
8123
8124 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
8125 {
8126         return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);
8127 }
8128
8129 /* pkgmgrinfo client start*/
8130 API pkgmgrinfo_client *pkgmgrinfo_client_new(pkgmgrinfo_client_type ctype)
8131 {
8132         int ret = 0;
8133         char *errmsg = NULL;
8134         void *pc = NULL;
8135         void *handle = NULL;
8136         pkgmgrinfo_client *(*__pkgmgr_client_new)(pkgmgrinfo_client_type ctype) = NULL;
8137
8138         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8139         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8140
8141         __pkgmgr_client_new = dlsym(handle, "pkgmgr_client_new");
8142         errmsg = dlerror();
8143         tryvm_if((errmsg != NULL) || (__pkgmgr_client_new == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8144
8145         pc = __pkgmgr_client_new(ctype);
8146         tryvm_if(pc == NULL, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8147
8148 catch:
8149         dlclose(handle);
8150         return (pkgmgrinfo_client *) pc;
8151 }
8152
8153 API int pkgmgrinfo_client_set_status_type(pkgmgrinfo_client *pc, int status_type)
8154 {
8155         int ret = 0;
8156         char *errmsg = NULL;
8157         void *handle = NULL;
8158         int (*__pkgmgr_client_set_status_type)(pkgmgrinfo_client *pc, int status_type) = NULL;
8159
8160         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8161         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8162
8163         __pkgmgr_client_set_status_type = dlsym(handle, "pkgmgr_client_set_status_type");
8164         errmsg = dlerror();
8165         tryvm_if((errmsg != NULL) || (__pkgmgr_client_set_status_type == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8166
8167         ret = __pkgmgr_client_set_status_type(pc, status_type);
8168         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8169
8170 catch:
8171         /*
8172          * Do not close libpkgmgr-client.so.0 to avoid munmap registered callback
8173          *
8174          * The lib dependency chain like below
8175          * amd --> pkgmgr-info -- dlopen --> libpkgmgr-client --> libpkgmgr-installer-client
8176          *
8177          * And there is a function in libpkgmgr-installer-client named _on_signal_handle_filter()
8178          * which will registered to dbus callback in amd though in fact amd doesn't direct depends
8179          * on libpkgmgr-installer-client.
8180          *
8181          * So when the dlcose happen, then libpkgmgr-installer-client been closed too since no one
8182          * link to it then.
8183          *
8184          * However, when the libdbus call into the callback function, it suddenly fond that the
8185          * function address is gone (unmapped), then we receive a SIGSEGV.
8186          *
8187          * I'm not sure why we're using dlopen/dlclose in this case, I think it's much simple and
8188          * robust if we just link to the well-known lib.
8189          *
8190          * See https://bugs.tizen.org/jira/browse/PTREL-591
8191         dlclose(handle);
8192          */
8193         return ret;
8194 }
8195
8196 API int pkgmgrinfo_client_listen_status(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data)
8197 {
8198         int ret = 0;
8199         char *errmsg = NULL;
8200         void *handle = NULL;
8201         int (*__pkgmgr_client_listen_status)(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data) = NULL;
8202
8203         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8204         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8205
8206         __pkgmgr_client_listen_status = dlsym(handle, "pkgmgr_client_listen_status");
8207         errmsg = dlerror();
8208         tryvm_if((errmsg != NULL) || (__pkgmgr_client_listen_status == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8209
8210         ret = __pkgmgr_client_listen_status(pc, event_cb, data);
8211         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8212
8213 catch:
8214         /* same as pkgmgrinfo_client_new */
8215         return ret;
8216 }
8217
8218 API int pkgmgrinfo_client_free(pkgmgrinfo_client *pc)
8219 {
8220         int ret = 0;
8221         char *errmsg = NULL;
8222         void *handle = NULL;
8223         int (*__pkgmgr_client_free)(pkgmgrinfo_client *pc) = NULL;
8224
8225         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8226         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8227
8228         __pkgmgr_client_free = dlsym(handle, "pkgmgr_client_free");
8229         errmsg = dlerror();
8230         tryvm_if((errmsg != NULL) || (__pkgmgr_client_free == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8231
8232         ret = __pkgmgr_client_free(pc);
8233         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8234
8235 catch:
8236         /* same as pkgmgrinfo_client_new */
8237         return ret;
8238 }
8239
8240 API int pkgmgrinfo_client_request_enable_external_pkg(char *pkgid)
8241 {
8242         int ret = 0;
8243         DBusConnection *bus;
8244         DBusMessage *message;
8245
8246         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
8247
8248         if(__get_pkg_location(pkgid) != PMINFO_EXTERNAL_STORAGE)
8249                 return PMINFO_R_OK;
8250
8251         bus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
8252         retvm_if(bus == NULL, PMINFO_R_EINVAL, "dbus_bus_get() failed.");
8253
8254         message = dbus_message_new_method_call (SERVICE_NAME, PATH_NAME, INTERFACE_NAME, METHOD_NAME);
8255         retvm_if(message == NULL, PMINFO_R_EINVAL, "dbus_message_new_method_call() failed.");
8256
8257         dbus_message_append_args(message, DBUS_TYPE_STRING, &pkgid, DBUS_TYPE_INVALID);
8258
8259         ret = dbus_connection_send_with_reply_and_block(bus, message, -1, NULL);
8260         retvm_if(!ret, ret = PMINFO_R_EINVAL, "connection_send dbus fail");
8261
8262         dbus_connection_flush(bus);
8263         dbus_message_unref(message);
8264
8265         return PMINFO_R_OK;
8266 }
8267
8268 /* pkgmgrinfo client end*/
8269