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