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