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