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