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