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