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