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