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