Preintialize the returned handle values to NULL
[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         *handle = NULL;
2908         /*validate pkgid*/
2909         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkginfo_db, SQLITE_OPEN_READONLY, NULL);
2910         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", getUserPkgParserDBPathUID(uid));
2911
2912         /*check pkgid exist on db*/
2913         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_info where package='%s')", pkgid);
2914         ret = __exec_db_query(pkginfo_db, query, __validate_cb, (void *)&exist);
2915         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec[%s] fail", pkgid);
2916         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "pkgid[%s] not found in DB", pkgid);
2917
2918         /*get system locale*/
2919         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2920         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
2921
2922         /*get locale on db*/
2923         locale = __convert_system_locale_to_manifest_locale(syslocale);
2924         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
2925
2926         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2927         tryvm_if(pkginfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for pkginfo");
2928
2929         pkginfo->locale = strdup(locale);
2930
2931         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
2932         tryvm_if(pkginfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for manifest info");
2933
2934         pkginfo->manifest_info->package = strdup(pkgid);
2935         pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
2936         tryvm_if(pkginfo->manifest_info->privileges == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for privileges info");
2937
2938         /*populate manifest_info from DB*/
2939         snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkgid);
2940         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2941         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2942
2943         memset(query, '\0', MAX_QUERY_LEN);
2944         /*populate privilege_info from DB*/
2945         snprintf(query, MAX_QUERY_LEN, "select * from package_privilege_info where package='%s' ", pkgid);
2946         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2947         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Privilege Info DB Information retrieval failed");
2948
2949         memset(query, '\0', MAX_QUERY_LEN);
2950         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2951                 " package='%s' and package_locale='%s'", pkgid, locale);
2952         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2953         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2954
2955         /*Also store the values corresponding to default locales*/
2956         memset(query, '\0', MAX_QUERY_LEN);
2957         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2958                 " package='%s' and package_locale='%s'", pkgid, DEFAULT_LOCALE);
2959         ret = __exec_db_query(pkginfo_db, query, __pkginfo_cb, (void *)pkginfo);
2960         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "Package Info DB Information retrieval failed");
2961
2962         if (pkginfo->manifest_info->label) {
2963                 LISTHEAD(pkginfo->manifest_info->label, tmp1);
2964                 pkginfo->manifest_info->label = tmp1;
2965         }
2966         if (pkginfo->manifest_info->icon) {
2967                 LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2968                 pkginfo->manifest_info->icon = tmp2;
2969         }
2970         if (pkginfo->manifest_info->description) {
2971                 LISTHEAD(pkginfo->manifest_info->description, tmp3);
2972                 pkginfo->manifest_info->description = tmp3;
2973         }
2974         if (pkginfo->manifest_info->author) {
2975                 LISTHEAD(pkginfo->manifest_info->author, tmp4);
2976                 pkginfo->manifest_info->author = tmp4;
2977         }
2978         if (pkginfo->manifest_info->privileges->privilege) {
2979                 LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
2980                 pkginfo->manifest_info->privileges->privilege = tmp5;
2981         }
2982
2983 catch:
2984         if (ret == PMINFO_R_OK)
2985                 *handle = (void*)pkginfo;
2986         else {
2987                 *handle = NULL;
2988                 __cleanup_pkginfo(pkginfo);
2989         }
2990         sqlite3_close(pkginfo_db);
2991
2992         if (syslocale) {
2993                 free(syslocale);
2994                 syslocale = NULL;
2995         }
2996         if (locale) {
2997                 free(locale);
2998                 locale = NULL;
2999         }
3000         return ret;
3001 }
3002
3003 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
3004 {
3005         return pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, GLOBAL_USER, handle);
3006 }
3007
3008 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
3009 {
3010         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3011         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3012         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3013         if (info->manifest_info->package)
3014                 *pkg_name = (char *)info->manifest_info->package;
3015         else
3016                 return PMINFO_R_ERROR;
3017
3018         return PMINFO_R_OK;
3019 }
3020
3021 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
3022 {
3023         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3024         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3025         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3026         if (info->manifest_info->package)
3027                 *pkgid = (char *)info->manifest_info->package;
3028         else
3029                 return PMINFO_R_ERROR;
3030
3031         return PMINFO_R_OK;
3032 }
3033
3034 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
3035 {
3036         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3037         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3038         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3039         if (info->manifest_info->type)
3040                 *type = (char *)info->manifest_info->type;
3041         else
3042                 *type = pkgtype;
3043         return PMINFO_R_OK;
3044 }
3045
3046 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
3047 {
3048         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3049         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3050         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3051         *version = (char *)info->manifest_info->version;
3052         return PMINFO_R_OK;
3053 }
3054
3055 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
3056 {
3057         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3058         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3059         char *val = NULL;
3060         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3061         val = (char *)info->manifest_info->installlocation;
3062         if (val) {
3063                 if (strcmp(val, "internal-only") == 0)
3064                         *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
3065                 else if (strcmp(val, "prefer-external") == 0)
3066                         *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
3067                 else
3068                         *location = PMINFO_INSTALL_LOCATION_AUTO;
3069         }
3070         return PMINFO_R_OK;
3071 }
3072
3073 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
3074 {
3075         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3076         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3077         char *val = NULL;
3078         char *location = NULL;
3079         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3080         location = (char *)info->manifest_info->installlocation;
3081         val = (char *)info->manifest_info->package_size;
3082         if (val) {
3083                 *size = atoi(val);
3084         } else {
3085                 *size = 0;
3086                 _LOGE("package size is not specified\n");
3087                 return PMINFO_R_ERROR;
3088         }
3089         return PMINFO_R_OK;
3090 }
3091
3092 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
3093 {
3094         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3095         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3096
3097         char *pkgid = NULL;
3098         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
3099         long long rw_size = 0;
3100         long long ro_size= 0;
3101         long long tmp_size= 0;
3102         long long total_size= 0;
3103         struct stat fileinfo;
3104         int ret = -1;
3105
3106         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
3107         if(ret < 0)
3108                 return PMINFO_R_ERROR;
3109
3110         /* RW area */
3111         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
3112         if (lstat(device_path, &fileinfo) == 0) {
3113                 if (!S_ISLNK(fileinfo.st_mode)) {
3114                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3115                         if (tmp_size > 0)
3116                                 rw_size += tmp_size;
3117                 }
3118         }
3119
3120         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
3121         if (lstat(device_path, &fileinfo) == 0) {
3122                 if (!S_ISLNK(fileinfo.st_mode)) {
3123                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3124                         if (tmp_size > 0)
3125                         rw_size += tmp_size;
3126                 }
3127         }
3128
3129         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
3130         if (lstat(device_path, &fileinfo) == 0) {
3131                 if (!S_ISLNK(fileinfo.st_mode)) {
3132                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3133                         if (tmp_size > 0)
3134                         rw_size += tmp_size;
3135                 }
3136         }
3137
3138         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
3139         if (lstat(device_path, &fileinfo) == 0) {
3140                 if (!S_ISLNK(fileinfo.st_mode)) {
3141                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3142                         if (tmp_size > 0)
3143                                 rw_size += tmp_size;
3144                 }
3145         }
3146
3147         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
3148         if (lstat(device_path, &fileinfo) == 0) {
3149                 if (!S_ISLNK(fileinfo.st_mode)) {
3150                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3151                         if (tmp_size > 0)
3152                                 rw_size += tmp_size;
3153         }
3154         }
3155
3156         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
3157         if (lstat(device_path, &fileinfo) == 0) {
3158                 if (!S_ISLNK(fileinfo.st_mode)) {
3159                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3160                         if (tmp_size > 0)
3161                                 rw_size += tmp_size;
3162                 }
3163         }
3164
3165         /* RO area */
3166         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
3167         if (lstat(device_path, &fileinfo) == 0) {
3168                 if (!S_ISLNK(fileinfo.st_mode)) {
3169                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3170                         if (tmp_size > 0)
3171                                 ro_size += tmp_size;
3172                 }
3173         }
3174
3175         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
3176         if (lstat(device_path, &fileinfo) == 0) {
3177                 if (!S_ISLNK(fileinfo.st_mode)) {
3178                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3179                         if (tmp_size > 0)
3180                                 ro_size += tmp_size;
3181                 }
3182         }
3183
3184         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
3185         if (lstat(device_path, &fileinfo) == 0) {
3186                 if (!S_ISLNK(fileinfo.st_mode)) {
3187                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3188                         if (tmp_size > 0)
3189                                 ro_size += tmp_size;
3190                 }
3191         }
3192
3193         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
3194         if (lstat(device_path, &fileinfo) == 0) {
3195                 if (!S_ISLNK(fileinfo.st_mode)) {
3196                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3197                         if (tmp_size > 0)
3198                                 ro_size += tmp_size;
3199                 }
3200         }
3201
3202         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
3203         if (lstat(device_path, &fileinfo) == 0) {
3204                 if (!S_ISLNK(fileinfo.st_mode)) {
3205                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3206                         if (tmp_size > 0)
3207                                 ro_size += tmp_size;
3208         }
3209         }
3210
3211         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
3212         if (lstat(device_path, &fileinfo) == 0) {
3213                 if (!S_ISLNK(fileinfo.st_mode)) {
3214                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
3215                         if (tmp_size > 0)
3216                                 ro_size += tmp_size;
3217                 }
3218         }
3219
3220         /* Total size */
3221         total_size = rw_size + ro_size;
3222         *size = (int)total_size;
3223
3224         return PMINFO_R_OK;
3225 }
3226
3227 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
3228 {
3229         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3230         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3231
3232         char *pkgid = NULL;
3233         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
3234         long long total_size= 0;
3235         int ret = -1;
3236
3237         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
3238         if(ret < 0)
3239                 return PMINFO_R_ERROR;
3240
3241         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
3242         if (access(device_path, R_OK) == 0)
3243                 total_size = _pkgmgr_calculate_dir_size(device_path);
3244         if (total_size < 0)
3245                 return PMINFO_R_ERROR;
3246
3247         *size = (int)total_size;
3248
3249         return PMINFO_R_OK;
3250 }
3251
3252 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
3253 {
3254         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3255         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3256         int ret = PMINFO_R_OK;
3257         char *locale = NULL;
3258         icon_x *ptr = NULL;
3259         *icon = NULL;
3260
3261         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3262
3263         locale = info->locale;
3264         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3265
3266         for(ptr = info->manifest_info->icon; ptr != NULL; ptr = ptr->next)
3267         {
3268                 if (ptr->lang) {
3269                         if (strcmp(ptr->lang, locale) == 0) {
3270                                 *icon = (char *)ptr->text;
3271                                 if (strcasecmp(*icon, "(null)") == 0) {
3272                                         locale = DEFAULT_LOCALE;
3273                                         continue;
3274                                 } else
3275                                         break;
3276                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3277                                 *icon = (char *)ptr->text;
3278                                 break;
3279                         }
3280                 }
3281         }
3282
3283         return ret;
3284 }
3285
3286 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
3287 {
3288         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
3289         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3290         int ret = PMINFO_R_OK;
3291         char *locale = NULL;
3292         label_x *ptr = NULL;
3293         *label = NULL;
3294
3295         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3296         locale = info->locale;
3297         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3298
3299         for(ptr = info->manifest_info->label; ptr != NULL; ptr = ptr->next)
3300         {
3301                 if (ptr->lang) {
3302                         if (strcmp(ptr->lang, locale) == 0) {
3303                                 *label = (char *)ptr->text;
3304                                 if (strcasecmp(*label, "(null)") == 0) {
3305                                         locale = DEFAULT_LOCALE;
3306                                         continue;
3307                                 } else
3308                                         break;
3309                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3310                                 *label = (char *)ptr->text;
3311                                 break;
3312                         }
3313                 }
3314         }
3315
3316         return ret;
3317 }
3318
3319 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
3320 {
3321         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3322         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3323         char *locale = NULL;
3324         description_x *ptr = NULL;
3325         *description = NULL;
3326
3327         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3328         locale = info->locale;
3329         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3330
3331         for(ptr = info->manifest_info->description; ptr != NULL; ptr = ptr->next)
3332         {
3333                 if (ptr->lang) {
3334                         if (strcmp(ptr->lang, locale) == 0) {
3335                                 *description = (char *)ptr->text;
3336                                 if (strcasecmp(*description, PKGMGR_PARSER_EMPTY_STR) == 0) {
3337                                         locale = DEFAULT_LOCALE;
3338                                         continue;
3339                                 } else
3340                                         break;
3341                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3342                                 *description = (char *)ptr->text;
3343                                 break;
3344                         }
3345                 }
3346         }
3347         return PMINFO_R_OK;
3348 }
3349
3350 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
3351 {
3352         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3353         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3354         char *locale = NULL;
3355         author_x *ptr = NULL;
3356         *author_name = NULL;
3357
3358         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3359         locale = info->locale;
3360         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
3361
3362         for(ptr = info->manifest_info->author; ptr != NULL; ptr = ptr->next)
3363         {
3364                 if (ptr->lang) {
3365                         if (strcmp(ptr->lang, locale) == 0) {
3366                                 *author_name = (char *)ptr->text;
3367                                 if (strcasecmp(*author_name, PKGMGR_PARSER_EMPTY_STR) == 0) {
3368                                         locale = DEFAULT_LOCALE;
3369                                         continue;
3370                                 } else
3371                                         break;
3372                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
3373                                 *author_name = (char *)ptr->text;
3374                                 break;
3375                         }
3376                 }
3377         }
3378         return PMINFO_R_OK;
3379 }
3380
3381 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
3382 {
3383         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3384         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3385         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3386         *author_email = (char *)info->manifest_info->author->email;
3387         return PMINFO_R_OK;
3388 }
3389
3390 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
3391 {
3392         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3393         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3394         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3395         *author_href = (char *)info->manifest_info->author->href;
3396         return PMINFO_R_OK;
3397 }
3398
3399 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
3400 {
3401         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3402         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3403
3404         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3405
3406          if (strcmp(info->manifest_info->installed_storage,"installed_internal") == 0)
3407                 *storage = PMINFO_INTERNAL_STORAGE;
3408          else if (strcmp(info->manifest_info->installed_storage,"installed_external") == 0)
3409                  *storage = PMINFO_EXTERNAL_STORAGE;
3410          else
3411                  return PMINFO_R_ERROR;
3412
3413         return PMINFO_R_OK;
3414 }
3415
3416 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
3417 {
3418         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3419         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3420         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3421         if (info->manifest_info->installed_time)
3422                 *installed_time = atoi(info->manifest_info->installed_time);
3423         else
3424                 return PMINFO_R_ERROR;
3425
3426         return PMINFO_R_OK;
3427 }
3428
3429 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
3430 {
3431         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3432         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3433         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3434         *storeclientid = (char *)info->manifest_info->storeclient_id;
3435         return PMINFO_R_OK;
3436 }
3437
3438 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
3439 {
3440         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3441         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3442         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3443         *mainappid = (char *)info->manifest_info->mainapp_id;
3444         return PMINFO_R_OK;
3445 }
3446
3447 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
3448 {
3449         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3450         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3451         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3452         *url = (char *)info->manifest_info->package_url;
3453         return PMINFO_R_OK;
3454 }
3455
3456 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
3457 {
3458         const char *val = NULL;
3459         const xmlChar *node;
3460         xmlTextReaderPtr reader;
3461         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
3462         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3463
3464         xmlInitParser();
3465         reader = xmlReaderForFile(manifest, NULL, 0);
3466
3467         if (reader){
3468                 if (__child_element(reader, -1)) {
3469                         node = xmlTextReaderConstName(reader);
3470                         if (!node) {
3471                                 _LOGE("xmlTextReaderConstName value is NULL\n");
3472                                 xmlFreeTextReader(reader);
3473                                 xmlCleanupParser();
3474                                 return PMINFO_R_ERROR;
3475                         }
3476
3477                         if (!strcmp(ASC_CHAR(node), "manifest")) {
3478                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
3479                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
3480
3481                                 if (val) {
3482                                         *size = atoi(val);
3483                                 } else {
3484                                         *size = 0;
3485                                         _LOGE("package size is not specified\n");
3486                                         xmlFreeTextReader(reader);
3487                                         xmlCleanupParser();
3488                                         return PMINFO_R_ERROR;
3489                                 }
3490                         } else {
3491                                 _LOGE("Unable to create xml reader\n");
3492                                 xmlFreeTextReader(reader);
3493                                 xmlCleanupParser();
3494                                 return PMINFO_R_ERROR;
3495                         }
3496                 }
3497         } else {
3498                 _LOGE("xmlReaderForFile value is NULL\n");
3499                 xmlCleanupParser();
3500                 return PMINFO_R_ERROR;
3501         }
3502
3503         xmlFreeTextReader(reader);
3504         xmlCleanupParser();
3505
3506         return PMINFO_R_OK;
3507 }
3508
3509 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
3510 {
3511         const char *val = NULL;
3512         const xmlChar *node;
3513         xmlTextReaderPtr reader;
3514         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
3515         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3516
3517         xmlInitParser();
3518         reader = xmlReaderForFile(manifest, NULL, 0);
3519
3520         if (reader){
3521                 if ( __child_element(reader, -1)) {
3522                         node = xmlTextReaderConstName(reader);
3523                         if (!node) {
3524                                 _LOGE("xmlTextReaderConstName value is NULL\n");
3525                                 xmlFreeTextReader(reader);
3526                                 xmlCleanupParser();
3527                                 return PMINFO_R_ERROR;
3528                         }
3529
3530                         if (!strcmp(ASC_CHAR(node), "manifest")) {
3531                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
3532                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
3533
3534                                 if (val) {
3535                                         if (strcmp(val, "internal-only") == 0)
3536                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
3537                                         else if (strcmp(val, "prefer-external") == 0)
3538                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
3539                                         else
3540                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
3541                                 }
3542                         } else {
3543                                 _LOGE("Unable to create xml reader\n");
3544                                 xmlFreeTextReader(reader);
3545                                 xmlCleanupParser();
3546                                 return PMINFO_R_ERROR;
3547                         }
3548                 }
3549         } else {
3550                 _LOGE("xmlReaderForFile value is NULL\n");
3551                 xmlCleanupParser();
3552                 return PMINFO_R_ERROR;
3553         }
3554
3555         xmlFreeTextReader(reader);
3556         xmlCleanupParser();
3557
3558         return PMINFO_R_OK;
3559 }
3560
3561
3562 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
3563 {
3564         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3565         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3566
3567         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3568         if (info->manifest_info->root_path)
3569                 *path = (char *)info->manifest_info->root_path;
3570         else
3571                 return PMINFO_R_ERROR;
3572
3573         return PMINFO_R_OK;
3574 }
3575
3576 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
3577 {
3578         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3579         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3580
3581         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3582         if (info->manifest_info->csc_path)
3583                 *path = (char *)info->manifest_info->csc_path;
3584         else
3585                 *path = (char *)info->manifest_info->csc_path;
3586
3587         return PMINFO_R_OK;
3588 }
3589
3590 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)
3591 {
3592         retvm_if(lhs_package_id == NULL, PMINFO_R_EINVAL, "lhs package ID is NULL");
3593         retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
3594         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3595
3596         int ret = PMINFO_R_OK;
3597         char query[MAX_QUERY_LEN] = {'\0'};
3598         char *error_message = NULL;
3599         pkgmgr_cert_x *info= NULL;
3600         int lcert = 0;
3601         int rcert = 0;
3602         int exist = -1;
3603         *compare_result = PMINFO_CERT_COMPARE_ERROR;
3604         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3605         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3606
3607         ret = db_util_open_with_options(getUserPkgCertDBPathUID(uid), &cert_db,
3608                                         SQLITE_OPEN_READWRITE, NULL);
3609         if (ret != SQLITE_OK) {
3610                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
3611                 ret = PMINFO_R_ERROR;
3612                 goto err;
3613         }
3614         _check_create_Cert_db(cert_db);
3615         _pkgmgr_parser_attach_create_view_certdb(cert_db,uid);
3616         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", lhs_package_id);
3617         if (SQLITE_OK !=
3618             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3619                 _LOGE("Don't execute query = %s error message = %s\n", query,
3620                        error_message);
3621                 ret = PMINFO_R_ERROR;
3622                 goto err;
3623         }
3624
3625         if (exist == 0) {
3626                 lcert = 0;
3627         } else {
3628                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", lhs_package_id);
3629                 if (SQLITE_OK !=
3630                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3631                         _LOGE("Don't execute query = %s error message = %s\n", query,
3632                                    error_message);
3633                         ret = PMINFO_R_ERROR;
3634                         goto err;
3635                 }
3636                 lcert = info->cert_id;
3637         }
3638
3639         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", rhs_package_id);
3640         if (SQLITE_OK !=
3641                 sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3642                 _LOGE("Don't execute query = %s error message = %s\n", query,
3643                            error_message);
3644                 ret = PMINFO_R_ERROR;
3645                 goto err;
3646         }
3647
3648         if (exist == 0) {
3649                 rcert = 0;
3650         } else {
3651                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", rhs_package_id);
3652                 if (SQLITE_OK !=
3653                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3654                         _LOGE("Don't execute query = %s error message = %s\n", query,
3655                                    error_message);
3656                         ret = PMINFO_R_ERROR;
3657                         goto err;
3658                 }
3659                 rcert = info->cert_id;
3660         }
3661
3662         if ((lcert == 0) || (rcert == 0))
3663         {
3664                 if ((lcert == 0) && (rcert == 0))
3665                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
3666                 else if (lcert == 0)
3667                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
3668                 else if (rcert == 0)
3669                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
3670         } else {
3671                 if (lcert == rcert)
3672                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
3673                 else
3674                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
3675         }
3676
3677 err:
3678         sqlite3_free(error_message);
3679         sqlite3_close(cert_db);
3680         if (info) {
3681                 if (info->pkgid) {
3682                         free(info->pkgid);
3683                         info->pkgid = NULL;
3684                 }
3685                 free(info);
3686                 info = NULL;
3687         }
3688         return ret;
3689 }
3690
3691 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)
3692 {
3693         return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_package_id, rhs_package_id, GLOBAL_USER, compare_result);
3694 }
3695
3696 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)
3697 {
3698         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
3699         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
3700         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3701
3702         int ret = PMINFO_R_OK;
3703         char query[MAX_QUERY_LEN] = {'\0'};
3704         char *error_message = NULL;
3705         pkgmgr_cert_x *info= NULL;
3706         int exist = -1;
3707         char *lpkgid = NULL;
3708         char *rpkgid = NULL;
3709
3710         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3711         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3712
3713         ret = db_util_open_with_options(getUserPkgParserDBPath(), &manifest_db,
3714                                         SQLITE_OPEN_READONLY, NULL);
3715         if (ret != SQLITE_OK) {
3716                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPath());
3717                 ret = PMINFO_R_ERROR;
3718                 goto err;
3719         }
3720
3721         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3722         if (SQLITE_OK !=
3723             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3724                 _LOGE("Don't execute query = %s error message = %s\n", query,
3725                        error_message);
3726                 ret = PMINFO_R_ERROR;
3727                 goto err;
3728         }
3729
3730         if (exist == 0) {
3731                 lpkgid = NULL;
3732         } else {
3733                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3734                 if (SQLITE_OK !=
3735                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3736                         _LOGE("Don't execute query = %s error message = %s\n", query,
3737                                    error_message);
3738                         ret = PMINFO_R_ERROR;
3739                         goto err;
3740                 }
3741                 lpkgid = strdup(info->pkgid);
3742                 if (lpkgid == NULL) {
3743                         _LOGE("Out of Memory\n");
3744                         ret = PMINFO_R_ERROR;
3745                         goto err;
3746                 }
3747                 free(info->pkgid);
3748                 info->pkgid = NULL;
3749         }
3750
3751         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3752         if (SQLITE_OK !=
3753             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3754                 _LOGE("Don't execute query = %s error message = %s\n", query,
3755                        error_message);
3756                 ret = PMINFO_R_ERROR;
3757                 goto err;
3758         }
3759
3760         if (exist == 0) {
3761                 rpkgid = NULL;
3762         } else {
3763                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3764                 if (SQLITE_OK !=
3765                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3766                         _LOGE("Don't execute query = %s error message = %s\n", query,
3767                                    error_message);
3768                         ret = PMINFO_R_ERROR;
3769                         goto err;
3770                 }
3771                 rpkgid = strdup(info->pkgid);
3772                 if (rpkgid == NULL) {
3773                         _LOGE("Out of Memory\n");
3774                         ret = PMINFO_R_ERROR;
3775                         goto err;
3776                 }
3777                 free(info->pkgid);
3778                 info->pkgid = NULL;
3779         }
3780         ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lpkgid, rpkgid, compare_result);
3781  err:
3782         sqlite3_free(error_message);
3783         sqlite3_close(manifest_db);
3784         if (info) {
3785                 if (info->pkgid) {
3786                         free(info->pkgid);
3787                         info->pkgid = NULL;
3788                 }
3789                 free(info);
3790                 info = NULL;
3791         }
3792         if (lpkgid) {
3793                 free(lpkgid);
3794                 lpkgid = NULL;
3795         }
3796         if (rpkgid) {
3797                 free(rpkgid);
3798                 rpkgid = NULL;
3799         }
3800         return ret;
3801 }
3802
3803 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)
3804 {
3805         retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
3806         retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
3807         retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
3808
3809         int ret = PMINFO_R_OK;
3810         char query[MAX_QUERY_LEN] = {'\0'};
3811         char *error_message = NULL;
3812         pkgmgr_cert_x *info= NULL;
3813         int exist = -1;
3814         char *lpkgid = NULL;
3815         char *rpkgid = NULL;
3816
3817         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3818         retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
3819
3820         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &manifest_db,
3821                                         SQLITE_OPEN_READONLY, NULL);
3822         if (ret != SQLITE_OK) {
3823                 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
3824                 ret = PMINFO_R_ERROR;
3825                 goto err;
3826         }
3827     _pkgmgr_parser_attach_create_view_parserdb(manifest_db,uid);
3828
3829         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3830         if (SQLITE_OK !=
3831             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3832                 _LOGE("Don't execute query = %s error message = %s\n", query,
3833                        error_message);
3834                 ret = PMINFO_R_ERROR;
3835                 goto err;
3836         }
3837
3838         if (exist == 0) {
3839                 lpkgid = NULL;
3840         } else {
3841                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3842                 if (SQLITE_OK !=
3843                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3844                         _LOGE("Don't execute query = %s error message = %s\n", query,
3845                                    error_message);
3846                         ret = PMINFO_R_ERROR;
3847                         goto err;
3848                 }
3849                 lpkgid = strdup(info->pkgid);
3850                 if (lpkgid == NULL) {
3851                         _LOGE("Out of Memory\n");
3852                         ret = PMINFO_R_ERROR;
3853                         goto err;
3854                 }
3855                 free(info->pkgid);
3856                 info->pkgid = NULL;
3857         }
3858
3859         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3860         if (SQLITE_OK !=
3861             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3862                 _LOGE("Don't execute query = %s error message = %s\n", query,
3863                        error_message);
3864                 ret = PMINFO_R_ERROR;
3865                 goto err;
3866         }
3867
3868         if (exist == 0) {
3869                 rpkgid = NULL;
3870         } else {
3871                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3872                 if (SQLITE_OK !=
3873                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3874                         _LOGE("Don't execute query = %s error message = %s\n", query,
3875                                    error_message);
3876                         ret = PMINFO_R_ERROR;
3877                         goto err;
3878                 }
3879                 rpkgid = strdup(info->pkgid);
3880                 if (rpkgid == NULL) {
3881                         _LOGE("Out of Memory\n");
3882                         ret = PMINFO_R_ERROR;
3883                         goto err;
3884                 }
3885                 free(info->pkgid);
3886                 info->pkgid = NULL;
3887         }
3888         ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lpkgid, rpkgid, uid, compare_result);
3889  err:
3890         sqlite3_free(error_message);
3891         sqlite3_close(manifest_db);
3892         if (info) {
3893                 if (info->pkgid) {
3894                         free(info->pkgid);
3895                         info->pkgid = NULL;
3896                 }
3897                 free(info);
3898                 info = NULL;
3899         }
3900         if (lpkgid) {
3901                 free(lpkgid);
3902                 lpkgid = NULL;
3903         }
3904         if (rpkgid) {
3905                 free(rpkgid);
3906                 rpkgid = NULL;
3907         }
3908         return ret;
3909 }
3910
3911 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
3912 {
3913         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3914         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3915
3916 #if 0 //smack issue occured, check later
3917         char *pkgid = NULL;
3918         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
3919         if (pkgid == NULL){
3920                  _LOGD("invalid func parameters\n");
3921                  return PMINFO_R_ERROR;
3922         }
3923          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
3924
3925         FILE *fp = NULL;
3926         char app_mmc_path[FILENAME_MAX] = { 0, };
3927         char app_dir_path[FILENAME_MAX] = { 0, };
3928         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
3929         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
3930         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
3931         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
3932
3933         /*check whether application is in external memory or not */
3934         fp = fopen(app_mmc_path, "r");
3935         if (fp == NULL){
3936                 _LOGD(" app path in external memory not accesible\n");
3937         } else {
3938                 fclose(fp);
3939                 fp = NULL;
3940                 *accessible = 1;
3941                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
3942                 return PMINFO_R_OK;
3943         }
3944
3945         /*check whether application is in internal or not */
3946         fp = fopen(app_dir_path, "r");
3947         if (fp == NULL) {
3948                 _LOGD(" app path in internal memory not accesible\n");
3949                 *accessible = 0;
3950                 return PMINFO_R_ERROR;
3951         } else {
3952                 fclose(fp);
3953                 /*check whether the application is installed in SD card
3954                 but SD card is not present*/
3955                 fp = fopen(app_mmc_internal_path, "r");
3956                 if (fp == NULL){
3957                         *accessible = 1;
3958                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
3959                         return PMINFO_R_OK;
3960                 }
3961                 else{
3962                         *accessible = 0;
3963                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
3964                 }
3965                 fclose(fp);
3966         }
3967
3968         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
3969 #endif
3970
3971         *accessible = 1;
3972         return PMINFO_R_OK;
3973 }
3974
3975 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
3976 {
3977         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3978         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3979         char *val = NULL;
3980         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3981         val = (char *)info->manifest_info->removable;
3982         if (val) {
3983                 if (strcasecmp(val, "true") == 0)
3984                         *removable = 1;
3985                 else if (strcasecmp(val, "false") == 0)
3986                         *removable = 0;
3987                 else
3988                         *removable = 1;
3989         }
3990         return PMINFO_R_OK;
3991 }
3992
3993 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
3994 {
3995         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
3996         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3997
3998         char *val = NULL;
3999         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4000
4001         val = (char *)info->manifest_info->installlocation;
4002         if (val) {
4003                 if (strcmp(val, "internal-only") == 0)
4004                         *movable = 0;
4005                 else if (strcmp(val, "prefer-external") == 0)
4006                         *movable = 1;
4007                 else
4008                         *movable = 1;
4009         }
4010
4011         return PMINFO_R_OK;
4012 }
4013
4014 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
4015 {
4016         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4017         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4018         char *val = NULL;
4019         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4020         val = (char *)info->manifest_info->preload;
4021         if (val) {
4022                 if (strcasecmp(val, "true") == 0)
4023                         *preload = 1;
4024                 else if (strcasecmp(val, "false") == 0)
4025                         *preload = 0;
4026                 else
4027                         *preload = 0;
4028         }
4029         return PMINFO_R_OK;
4030 }
4031
4032 API int pkgmgrinfo_pkginfo_is_system(pkgmgrinfo_pkginfo_h handle, bool *system)
4033 {
4034         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4035         retvm_if(system == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4036
4037         char *val = NULL;
4038         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4039         val = (char *)info->manifest_info->system;
4040         if (val) {
4041                 if (strcasecmp(val, "true") == 0)
4042                         *system = 1;
4043                 else if (strcasecmp(val, "false") == 0)
4044                         *system = 0;
4045                 else
4046                         *system = 0;
4047         }
4048
4049         return PMINFO_R_OK;
4050 }
4051
4052 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
4053 {
4054         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4055         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4056         char *val = NULL;
4057         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4058         val = (char *)info->manifest_info->readonly;
4059         if (val) {
4060                 if (strcasecmp(val, "true") == 0)
4061                         *readonly = 1;
4062                 else if (strcasecmp(val, "false") == 0)
4063                         *readonly = 0;
4064                 else
4065                         *readonly = 0;
4066         }
4067         return PMINFO_R_OK;
4068 }
4069
4070 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
4071 {
4072         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4073         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
4074
4075         char *val = NULL;
4076         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4077         val = (char *)info->manifest_info->update;
4078         if (val) {
4079                 if (strcasecmp(val, "true") == 0)
4080                         *update = 1;
4081                 else if (strcasecmp(val, "false") == 0)
4082                         *update = 0;
4083                 else
4084                         *update = 1;
4085         }
4086         return PMINFO_R_OK;
4087 }
4088
4089 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
4090 {
4091         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
4092         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4093         __cleanup_pkginfo(info);
4094         return PMINFO_R_OK;
4095 }
4096
4097 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
4098 {
4099         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
4100         *handle = NULL;
4101         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
4102         if (filter == NULL) {
4103                 _LOGE("Out of Memory!!!");
4104                 return PMINFO_R_ERROR;
4105         }
4106         *handle = filter;
4107         return PMINFO_R_OK;
4108 }
4109
4110 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
4111 {
4112         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4113         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4114         if (filter->list){
4115                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
4116                 g_slist_free(filter->list);
4117         }
4118         free(filter);
4119         filter = NULL;
4120         return PMINFO_R_OK;
4121 }
4122
4123 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
4124                                 const char *property, const int value)
4125 {
4126         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4127         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4128         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
4129         char *val = NULL;
4130         GSList *link = NULL;
4131         int prop = -1;
4132         prop = _pminfo_pkginfo_convert_to_prop_int(property);
4133         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
4134                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
4135                 _LOGE("Invalid Integer Property\n");
4136                 return PMINFO_R_EINVAL;
4137         }
4138         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4139         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
4140         if (node == NULL) {
4141                 _LOGE("Out of Memory!!!\n");
4142                 return PMINFO_R_ERROR;
4143         }
4144         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
4145         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
4146         if (val == NULL) {
4147                 _LOGE("Out of Memory\n");
4148                 free(node);
4149                 node = NULL;
4150                 return PMINFO_R_ERROR;
4151         }
4152         node->prop = prop;
4153         node->value = val;
4154         /*If API is called multiple times for same property, we should override the previous values.
4155         Last value set will be used for filtering.*/
4156         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4157         if (link)
4158                 filter->list = g_slist_delete_link(filter->list, link);
4159         filter->list = g_slist_append(filter->list, (gpointer)node);
4160         return PMINFO_R_OK;
4161
4162 }
4163
4164 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
4165                                 const char *property, const bool value)
4166 {
4167         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4168         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4169         char *val = NULL;
4170         GSList *link = NULL;
4171         int prop = -1;
4172         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
4173         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
4174                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
4175                 _LOGE("Invalid Boolean Property\n");
4176                 return PMINFO_R_EINVAL;
4177         }
4178         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4179         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
4180         if (node == NULL) {
4181                 _LOGE("Out of Memory!!!\n");
4182                 return PMINFO_R_ERROR;
4183         }
4184         if (value)
4185                 val = strndup("('true','True')", 15);
4186         else
4187                 val = strndup("('false','False')", 17);
4188         if (val == NULL) {
4189                 _LOGE("Out of Memory\n");
4190                 free(node);
4191                 node = NULL;
4192                 return PMINFO_R_ERROR;
4193         }
4194         node->prop = prop;
4195         node->value = val;
4196         /*If API is called multiple times for same property, we should override the previous values.
4197         Last value set will be used for filtering.*/
4198         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4199         if (link)
4200                 filter->list = g_slist_delete_link(filter->list, link);
4201         filter->list = g_slist_append(filter->list, (gpointer)node);
4202         return PMINFO_R_OK;
4203
4204 }
4205
4206 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
4207                                 const char *property, const char *value)
4208 {
4209         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4210         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4211         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4212         char *val = NULL;
4213         GSList *link = NULL;
4214         int prop = -1;
4215         prop = _pminfo_pkginfo_convert_to_prop_str(property);
4216         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
4217                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
4218                 _LOGE("Invalid String Property\n");
4219                 return PMINFO_R_EINVAL;
4220         }
4221         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4222         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
4223         if (node == NULL) {
4224                 _LOGE("Out of Memory!!!\n");
4225                 return PMINFO_R_ERROR;
4226         }
4227         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
4228                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
4229         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
4230                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
4231         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
4232                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
4233         else if (strcmp(value, "installed_internal") == 0)
4234                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
4235         else if (strcmp(value, "installed_external") == 0)
4236                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
4237         else
4238                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
4239         if (val == NULL) {
4240                 _LOGE("Out of Memory\n");
4241                 free(node);
4242                 node = NULL;
4243                 return PMINFO_R_ERROR;
4244         }
4245         node->prop = prop;
4246         node->value = val;
4247         /*If API is called multiple times for same property, we should override the previous values.
4248         Last value set will be used for filtering.*/
4249         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
4250         if (link)
4251                 filter->list = g_slist_delete_link(filter->list, link);
4252         filter->list = g_slist_append(filter->list, (gpointer)node);
4253         return PMINFO_R_OK;
4254
4255 }
4256
4257 API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count, uid_t uid)
4258 {
4259         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4260         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4261         char *syslocale = NULL;
4262         char *locale = NULL;
4263         char *condition = NULL;
4264         char *error_message = NULL;
4265         char query[MAX_QUERY_LEN] = {'\0'};
4266         char where[MAX_QUERY_LEN] = {'\0'};
4267         GSList *list;
4268         int ret = 0;
4269
4270         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4271         /*Get current locale*/
4272         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4273         if (syslocale == NULL) {
4274                 _LOGE("current locale is NULL\n");
4275                 return PMINFO_R_ERROR;
4276         }
4277         locale = __convert_system_locale_to_manifest_locale(syslocale);
4278         if (locale == NULL) {
4279                 _LOGE("manifest locale is NULL\n");
4280                 free(syslocale);
4281                 return PMINFO_R_ERROR;
4282         }
4283
4284         ret = __open_manifest_db(uid);
4285         if (ret == -1) {
4286                 _LOGE("Fail to open manifest DB\n");
4287                 free(syslocale);
4288                 free(locale);
4289                 return PMINFO_R_ERROR;
4290         }
4291
4292         /*Start constructing query*/
4293         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
4294
4295         /*Get where clause*/
4296         for (list = filter->list; list; list = g_slist_next(list)) {
4297                 __get_filter_condition(list->data, &condition);
4298                 if (condition) {
4299                         strncat(where, condition, sizeof(where) - strlen(where) -1);
4300                         where[sizeof(where) - 1] = '\0';
4301                         free(condition);
4302                         condition = NULL;
4303                 }
4304                 if (g_slist_next(list)) {
4305                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
4306                         where[sizeof(where) - 1] = '\0';
4307                 }
4308         }
4309         _LOGE("where = %s\n", where);
4310         if (strlen(where) > 0) {
4311                 strncat(query, where, sizeof(query) - strlen(query) - 1);
4312                 query[sizeof(query) - 1] = '\0';
4313         }
4314         _LOGE("query = %s\n", query);
4315
4316         /*Execute Query*/
4317         if (SQLITE_OK !=
4318             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
4319                 _LOGE("Don't execute query = %s error message = %s\n", query,
4320                        error_message);
4321                 sqlite3_free(error_message);
4322                 sqlite3_close(manifest_db);
4323                 ret = PMINFO_R_ERROR;
4324                 *count = 0;
4325                 goto err;
4326         }
4327         ret = PMINFO_R_OK;
4328 err:
4329         if (locale) {
4330                 free(locale);
4331                 locale = NULL;
4332         }
4333         if (syslocale) {
4334                 free(syslocale);
4335                 syslocale = NULL;
4336         }
4337         sqlite3_close(manifest_db);
4338         return ret;
4339 }
4340
4341 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
4342 {
4343         return pkgmgrinfo_pkginfo_usr_filter_count(handle, count, GLOBAL_USER);
4344 }
4345
4346 API int pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
4347                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data, uid_t uid)
4348 {
4349         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4350         retvm_if(pkg_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
4351         char *syslocale = NULL;
4352         char *locale = NULL;
4353         char *condition = NULL;
4354         char *error_message = NULL;
4355         char query[MAX_QUERY_LEN] = {'\0'};
4356         char where[MAX_QUERY_LEN] = {'\0'};
4357         GSList *list;
4358         int ret = 0;
4359         label_x *tmp1 = NULL;
4360         icon_x *tmp2 = NULL;
4361         description_x *tmp3 = NULL;
4362         author_x *tmp4 = NULL;
4363         privilege_x *tmp5 = NULL;
4364         pkgmgr_pkginfo_x *node = NULL;
4365         pkgmgr_pkginfo_x *tmphead = NULL;
4366         pkgmgr_pkginfo_x *pkginfo = NULL;
4367
4368         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
4369         /*Get current locale*/
4370         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4371         if (syslocale == NULL) {
4372                 _LOGE("current locale is NULL\n");
4373                 return PMINFO_R_ERROR;
4374         }
4375         locale = __convert_system_locale_to_manifest_locale(syslocale);
4376         if (locale == NULL) {
4377                 _LOGE("manifest locale is NULL\n");
4378                 free(syslocale);
4379                 return PMINFO_R_ERROR;
4380         }
4381
4382         ret = __open_manifest_db(uid);
4383         if (ret == -1) {
4384                 _LOGE("Fail to open manifest DB\n");
4385                 free(syslocale);
4386                 free(locale);
4387                 return PMINFO_R_ERROR;
4388         }
4389         /*Start constructing query*/
4390         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, locale);
4391
4392         /*Get where clause*/
4393         for (list = filter->list; list; list = g_slist_next(list)) {
4394                 __get_filter_condition(list->data, &condition);
4395                 if (condition) {
4396                         strncat(where, condition, sizeof(where) - strlen(where) -1);
4397                         where[sizeof(where) - 1] = '\0';
4398                         free(condition);
4399                         condition = NULL;
4400                 }
4401                 if (g_slist_next(list)) {
4402                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
4403                         where[sizeof(where) - 1] = '\0';
4404                 }
4405         }
4406         _LOGE("where = %s\n", where);
4407         if (strlen(where) > 0) {
4408                 strncat(query, where, sizeof(query) - strlen(query) - 1);
4409                 query[sizeof(query) - 1] = '\0';
4410         }
4411         _LOGE("query = %s\n", query);
4412         tmphead = calloc(1, sizeof(pkgmgr_pkginfo_x));
4413         if (tmphead == NULL) {
4414                 _LOGE("Out of Memory!!!\n");
4415                 ret = PMINFO_R_ERROR;
4416                 goto err;
4417         }
4418
4419         if (SQLITE_OK !=
4420             sqlite3_exec(manifest_db, query, __pkg_list_cb, (void *)tmphead, &error_message)) {
4421                 _LOGE("Don't execute query = %s error message = %s\n", query,
4422                        error_message);
4423                 sqlite3_free(error_message);
4424                 ret = PMINFO_R_ERROR;
4425                 goto err;
4426         }
4427
4428         LISTHEAD(tmphead, node);
4429         for(node = node->next ; node ; node = node->next) {
4430                 pkginfo = node;
4431                 pkginfo->locale = strdup(locale);
4432                 pkginfo->manifest_info->privileges = (privileges_x *)calloc(1, sizeof(privileges_x));
4433                 if (pkginfo->manifest_info->privileges == NULL) {
4434                         _LOGE("Failed to allocate memory for privileges info\n");
4435                         ret = PMINFO_R_ERROR;
4436                         goto err;
4437                 }
4438
4439                 /*populate manifest_info from DB*/
4440                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
4441                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4442                 if (ret == -1) {
4443                         _LOGE("Package Info DB Information retrieval failed\n");
4444                         ret = PMINFO_R_ERROR;
4445                         goto err;
4446                 }
4447                 memset(query, '\0', MAX_QUERY_LEN);
4448                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
4449                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
4450                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4451                 if (ret == -1) {
4452                         _LOGE("Package Info DB Information retrieval failed\n");
4453                         ret = PMINFO_R_ERROR;
4454                         goto err;
4455                 }
4456                 /*Also store the values corresponding to default locales*/
4457                 memset(query, '\0', MAX_QUERY_LEN);
4458                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
4459                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
4460                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
4461                 if (ret == -1) {
4462                         _LOGE("Package Info DB Information retrieval failed\n");
4463                         ret = PMINFO_R_ERROR;
4464                         goto err;
4465                 }
4466                 if (pkginfo->manifest_info->label) {
4467                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
4468                         pkginfo->manifest_info->label = tmp1;
4469                 }
4470                 if (pkginfo->manifest_info->icon) {
4471                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
4472                         pkginfo->manifest_info->icon = tmp2;
4473                 }
4474                 if (pkginfo->manifest_info->description) {
4475                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
4476                         pkginfo->manifest_info->description = tmp3;
4477                 }
4478                 if (pkginfo->manifest_info->author) {
4479                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
4480                         pkginfo->manifest_info->author = tmp4;
4481                 }
4482                 if (pkginfo->manifest_info->privileges->privilege) {
4483                         LISTHEAD(pkginfo->manifest_info->privileges->privilege, tmp5);
4484                         pkginfo->manifest_info->privileges->privilege = tmp5;
4485                 }
4486         }
4487
4488         LISTHEAD(tmphead, node);
4489
4490         for(node = node->next ; node ; node = node->next) {
4491                 pkginfo = node;
4492                 ret = pkg_cb( (void *)pkginfo, user_data);
4493                 if(ret < 0)
4494                         break;
4495         }
4496         ret = PMINFO_R_OK;
4497
4498 err:
4499         if (locale) {
4500                 free(locale);
4501                 locale = NULL;
4502         }
4503         if (syslocale) {
4504                 free(syslocale);
4505                 syslocale = NULL;
4506         }
4507         sqlite3_close(manifest_db);
4508         __cleanup_pkginfo(tmphead);
4509         return ret;
4510 }
4511
4512 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
4513                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
4514 {
4515         return pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, pkg_cb, user_data, GLOBAL_USER);
4516 }
4517
4518 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
4519                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
4520 {
4521         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
4522         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
4523         int ret = -1;
4524         privilege_x *ptr = NULL;
4525         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4526         ptr = info->manifest_info->privileges->privilege;
4527         for (; ptr; ptr = ptr->next) {
4528                 if (ptr->text){
4529                         ret = privilege_func(ptr->text, user_data);
4530                         if (ret < 0)
4531                                 break;
4532                 }
4533         }
4534         return PMINFO_R_OK;
4535 }
4536
4537 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
4538                                                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
4539 {
4540         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
4541         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback pointer is NULL");
4542         retvm_if((component != PMINFO_UI_APP) && (component != PMINFO_SVC_APP) && (component != PMINFO_ALL_APP), PMINFO_R_EINVAL, "Invalid App Component Type");
4543
4544         char *syslocale = NULL;
4545         char *locale = NULL;
4546         int ret = -1;
4547         char query[MAX_QUERY_LEN] = {'\0'};
4548         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
4549         pkgmgr_pkginfo_x *allinfo = NULL;
4550         pkgmgr_appinfo_x *appinfo = NULL;
4551         icon_x *ptr1 = NULL;
4552         label_x *ptr2 = NULL;
4553         category_x *ptr3 = NULL;
4554         metadata_x *ptr4 = NULL;
4555         permission_x *ptr5 = NULL;
4556         image_x *ptr6 = NULL;
4557         sqlite3 *appinfo_db = NULL;
4558
4559         /*get system locale*/
4560         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4561         retvm_if(syslocale == NULL, PMINFO_R_EINVAL, "current locale is NULL");
4562
4563         /*get locale on db*/
4564         locale = __convert_system_locale_to_manifest_locale(syslocale);
4565         tryvm_if(locale == NULL, ret = PMINFO_R_EINVAL, "manifest locale is NULL");
4566
4567         /*calloc allinfo*/
4568         allinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
4569         tryvm_if(allinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
4570
4571         /*calloc manifest_info*/
4572         allinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
4573         tryvm_if(allinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4574
4575         /*calloc appinfo*/
4576         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4577         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
4578
4579         /*set component type*/
4580         if (component == PMINFO_UI_APP)
4581                 appinfo->app_component = PMINFO_UI_APP;
4582         if (component == PMINFO_SVC_APP)
4583                 appinfo->app_component = PMINFO_SVC_APP;
4584         if (component == PMINFO_ALL_APP)
4585                 appinfo->app_component = PMINFO_ALL_APP;
4586
4587         /*open db */
4588         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4589         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", getUserPkgParserDBPathUID(uid));
4590
4591         appinfo->package = strdup(info->manifest_info->package);
4592         snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4593                         "from package_app_info where " \
4594                         "package='%s' and app_component='%s'",
4595                         info->manifest_info->package,
4596                         (appinfo->app_component==PMINFO_UI_APP ? "uiapp" : "svcapp"));
4597
4598         switch(component) {
4599         case PMINFO_UI_APP:
4600                 /*Populate ui app info */
4601                 ret = __exec_db_query(appinfo_db, query, __uiapp_list_cb, (void *)info);
4602                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4603
4604                 uiapplication_x *tmp = NULL;
4605                 if (info->manifest_info->uiapplication) {
4606                         LISTHEAD(info->manifest_info->uiapplication, tmp);
4607                         info->manifest_info->uiapplication = tmp;
4608                 }
4609                 /*Populate localized info for default locales and call callback*/
4610                 /*If the callback func return < 0 we break and no more call back is called*/
4611                 while(tmp != NULL)
4612                 {
4613                         appinfo->locale = strdup(locale);
4614                         appinfo->uiapp_info = tmp;
4615                         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
4616                                 if (locale) {
4617                                         free(locale);
4618                                 }
4619                                 locale = __get_app_locale_by_fallback(appinfo_db, appinfo->uiapp_info->appid, syslocale);
4620                         }
4621
4622                         memset(query, '\0', MAX_QUERY_LEN);
4623                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
4624                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4625                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4626
4627                         memset(query, '\0', MAX_QUERY_LEN);
4628                         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);
4629                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4630                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4631
4632                         /*store setting notification icon section*/
4633                         memset(query, '\0', MAX_QUERY_LEN);
4634                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
4635                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4636                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4637                         
4638                         /*store app preview image info*/
4639                         memset(query, '\0', MAX_QUERY_LEN);
4640                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
4641                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4642                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4643
4644                         if (appinfo->uiapp_info->label) {
4645                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
4646                                 appinfo->uiapp_info->label = ptr2;
4647                         }
4648                         if (appinfo->uiapp_info->icon) {
4649                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
4650                                 appinfo->uiapp_info->icon = ptr1;
4651                         }
4652                         if (appinfo->uiapp_info->category) {
4653                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
4654                                 appinfo->uiapp_info->category = ptr3;
4655                         }
4656                         if (appinfo->uiapp_info->metadata) {
4657                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
4658                                 appinfo->uiapp_info->metadata = ptr4;
4659                         }
4660                         if (appinfo->uiapp_info->permission) {
4661                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
4662                                 appinfo->uiapp_info->permission = ptr5;
4663                         }
4664                         if (appinfo->uiapp_info->image) {
4665                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
4666                                 appinfo->uiapp_info->image = ptr6;
4667                         }
4668                         ret = app_func((void *)appinfo, user_data);
4669                         if (ret < 0)
4670                                 break;
4671                         tmp = tmp->next;
4672                 }
4673                 break;
4674         case PMINFO_SVC_APP:
4675                 /*Populate svc app info */
4676                 ret = __exec_db_query(appinfo_db, query, __svcapp_list_cb, (void *)info);
4677                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4678
4679                 serviceapplication_x *tmp1 = NULL;
4680                 if (info->manifest_info->serviceapplication) {
4681                         LISTHEAD(info->manifest_info->serviceapplication, tmp1);
4682                         info->manifest_info->serviceapplication = tmp1;
4683                 }
4684                 /*Populate localized info for default locales and call callback*/
4685                 /*If the callback func return < 0 we break and no more call back is called*/
4686                 while(tmp1 != NULL)
4687                 {
4688                         appinfo->locale = strdup(locale);
4689                         appinfo->svcapp_info = tmp1;
4690                         memset(query, '\0', MAX_QUERY_LEN);
4691                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4692                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4693                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4694
4695                         memset(query, '\0', MAX_QUERY_LEN);
4696                         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);
4697                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4698                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4699
4700                         if (appinfo->svcapp_info->label) {
4701                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4702                                 appinfo->svcapp_info->label = ptr2;
4703                         }
4704                         if (appinfo->svcapp_info->icon) {
4705                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4706                                 appinfo->svcapp_info->icon = ptr1;
4707                         }
4708                         if (appinfo->svcapp_info->category) {
4709                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
4710                                 appinfo->svcapp_info->category = ptr3;
4711                         }
4712                         if (appinfo->svcapp_info->metadata) {
4713                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
4714                                 appinfo->svcapp_info->metadata = ptr4;
4715                         }
4716                         if (appinfo->svcapp_info->permission) {
4717                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
4718                                 appinfo->svcapp_info->permission = ptr5;
4719                         }
4720                         ret = app_func((void *)appinfo, user_data);
4721                         if (ret < 0)
4722                                 break;
4723                         tmp1 = tmp1->next;
4724                 }
4725                 break;
4726         case PMINFO_ALL_APP:
4727                 memset(query, '\0', MAX_QUERY_LEN);
4728                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where package='%s'", info->manifest_info->package);
4729
4730                 /*Populate all app info */
4731                 ret = __exec_db_query(appinfo_db, query, __allapp_list_cb, (void *)allinfo);
4732                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info list retrieval failed");
4733
4734                 /*UI Apps*/
4735                 appinfo->app_component = PMINFO_UI_APP;
4736                 uiapplication_x *tmp2 = NULL;
4737                 if (allinfo->manifest_info->uiapplication) {
4738                         LISTHEAD(allinfo->manifest_info->uiapplication, tmp2);
4739                         allinfo->manifest_info->uiapplication = tmp2;
4740                 }
4741                 /*Populate localized info for default locales and call callback*/
4742                 /*If the callback func return < 0 we break and no more call back is called*/
4743                 while(tmp2 != NULL)
4744                 {
4745                         appinfo->locale = strdup(locale);
4746                         appinfo->uiapp_info = tmp2;
4747                         memset(query, '\0', MAX_QUERY_LEN);
4748                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
4749                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4750                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4751
4752                         memset(query, '\0', MAX_QUERY_LEN);
4753                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
4754                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4755                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4756
4757                         /*store setting notification icon section*/
4758                         memset(query, '\0', MAX_QUERY_LEN);
4759                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appinfo->uiapp_info->appid);
4760                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4761                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
4762                         
4763                         /*store app preview image info*/
4764                         memset(query, '\0', MAX_QUERY_LEN);
4765                         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appinfo->uiapp_info->appid);
4766                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4767                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
4768
4769                         if (appinfo->uiapp_info->label) {
4770                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
4771                                 appinfo->uiapp_info->label = ptr2;
4772                         }
4773                         if (appinfo->uiapp_info->icon) {
4774                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
4775                                 appinfo->uiapp_info->icon = ptr1;
4776                         }
4777                         if (appinfo->uiapp_info->category) {
4778                                 LISTHEAD(appinfo->uiapp_info->category, ptr3);
4779                                 appinfo->uiapp_info->category = ptr3;
4780                         }
4781                         if (appinfo->uiapp_info->metadata) {
4782                                 LISTHEAD(appinfo->uiapp_info->metadata, ptr4);
4783                                 appinfo->uiapp_info->metadata = ptr4;
4784                         }
4785                         if (appinfo->uiapp_info->permission) {
4786                                 LISTHEAD(appinfo->uiapp_info->permission, ptr5);
4787                                 appinfo->uiapp_info->permission = ptr5;
4788                         }
4789                         if (appinfo->uiapp_info->image) {
4790                                 LISTHEAD(appinfo->uiapp_info->image, ptr6);
4791                                 appinfo->uiapp_info->image = ptr6;
4792                         }
4793                         ret = app_func((void *)appinfo, user_data);
4794                         if (ret < 0)
4795                                 break;
4796                         tmp2 = tmp2->next;
4797                 }
4798
4799                 /*SVC Apps*/
4800                 appinfo->app_component = PMINFO_SVC_APP;
4801                 serviceapplication_x *tmp3 = NULL;
4802                 if (allinfo->manifest_info->serviceapplication) {
4803                         LISTHEAD(allinfo->manifest_info->serviceapplication, tmp3);
4804                         allinfo->manifest_info->serviceapplication = tmp3;
4805                 }
4806                 /*Populate localized info for default locales and call callback*/
4807                 /*If the callback func return < 0 we break and no more call back is called*/
4808                 while(tmp3 != NULL)
4809                 {
4810                         appinfo->locale = strdup(locale);
4811                         appinfo->svcapp_info = tmp3;
4812                         memset(query, '\0', MAX_QUERY_LEN);
4813                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4814                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4815                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4816
4817                         memset(query, '\0', MAX_QUERY_LEN);
4818                         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);
4819                         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
4820                         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
4821
4822                         if (appinfo->svcapp_info->label) {
4823                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4824                                 appinfo->svcapp_info->label = ptr2;
4825                         }
4826                         if (appinfo->svcapp_info->icon) {
4827                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4828                                 appinfo->svcapp_info->icon = ptr1;
4829                         }
4830                         if (appinfo->svcapp_info->category) {
4831                                 LISTHEAD(appinfo->svcapp_info->category, ptr3);
4832                                 appinfo->svcapp_info->category = ptr3;
4833                         }
4834                         if (appinfo->svcapp_info->metadata) {
4835                                 LISTHEAD(appinfo->svcapp_info->metadata, ptr4);
4836                                 appinfo->svcapp_info->metadata = ptr4;
4837                         }
4838                         if (appinfo->svcapp_info->permission) {
4839                                 LISTHEAD(appinfo->svcapp_info->permission, ptr5);
4840                                 appinfo->svcapp_info->permission = ptr5;
4841                         }
4842                         ret = app_func((void *)appinfo, user_data);
4843                         if (ret < 0)
4844                                 break;
4845                         tmp3 = tmp3->next;
4846                 }
4847                 appinfo->app_component = PMINFO_ALL_APP;
4848                 break;
4849
4850         }
4851
4852         ret = PMINFO_R_OK;
4853 catch:
4854         if (locale) {
4855                 free(locale);
4856                 locale = NULL;
4857         }
4858         if (syslocale) {
4859                 free(syslocale);
4860                 syslocale = NULL;
4861         }
4862         if (appinfo) {
4863                 if (appinfo->package) {
4864                         free((void *)appinfo->package);
4865                         appinfo->package = NULL;
4866                 }
4867                 free(appinfo);
4868                 appinfo = NULL;
4869         }
4870         __cleanup_pkginfo(allinfo);
4871
4872         sqlite3_close(appinfo_db);
4873         return ret;
4874 }
4875
4876 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
4877                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
4878 {
4879         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
4880 }
4881
4882 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
4883 {
4884         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
4885
4886         int ret = PMINFO_R_OK;
4887         char query[MAX_QUERY_LEN] = {'\0'};
4888         pkgmgr_appinfo_x *appinfo = NULL;
4889         uiapplication_x *ptr1 = NULL;
4890         serviceapplication_x *ptr2 = NULL;
4891         sqlite3 *appinfo_db = NULL;
4892
4893         /*open db*/
4894         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4895         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", getUserPkgParserDBPathUID(uid));
4896
4897         /*calloc pkginfo*/
4898         pkgmgr_pkginfo_x *info = NULL;
4899         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
4900         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4901
4902         /*calloc manifest_info*/
4903         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
4904         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4905
4906         /*calloc appinfo*/
4907         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4908         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
4909
4910         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
4911         ret = __exec_db_query(appinfo_db, query, __mini_appinfo_cb, (void *)info);
4912         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
4913
4914         if (info->manifest_info->uiapplication) {
4915                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
4916                 info->manifest_info->uiapplication = ptr1;
4917         }
4918         if (info->manifest_info->serviceapplication) {
4919                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
4920                 info->manifest_info->serviceapplication = ptr2;
4921         }
4922
4923         /*UI Apps*/
4924         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
4925         {
4926                 appinfo->app_component = PMINFO_UI_APP;
4927                 appinfo->package = strdup(ptr1->package);
4928                 appinfo->uiapp_info = ptr1;
4929
4930                 ret = app_func((void *)appinfo, user_data);
4931                 if (ret < 0)
4932                         break;
4933                 free((void *)appinfo->package);
4934                 appinfo->package = NULL;
4935         }
4936         /*Service Apps*/
4937         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
4938         {
4939                 appinfo->app_component = PMINFO_SVC_APP;
4940                 appinfo->package = strdup(ptr2->package);
4941                 appinfo->svcapp_info = ptr2;
4942
4943                 ret = app_func((void *)appinfo, user_data);
4944                 if (ret < 0)
4945                         break;
4946                 free((void *)appinfo->package);
4947                 appinfo->package = NULL;
4948         }
4949         ret = PMINFO_R_OK;
4950
4951 catch:
4952         sqlite3_close(appinfo_db);
4953
4954         if (appinfo) {
4955                 free(appinfo);
4956                 appinfo = NULL;
4957         }
4958         __cleanup_pkginfo(info);
4959         return ret;
4960 }
4961
4962 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
4963 {
4964         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
4965 }
4966
4967 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
4968 {
4969         retvm_if(app_func == NULL, PMINFO_R_EINVAL, "callback function is NULL");
4970
4971         int ret = PMINFO_R_OK;
4972         char query[MAX_QUERY_LEN] = {'\0'};
4973         char *syslocale = NULL;
4974         char *locale = NULL;
4975         pkgmgr_appinfo_x *appinfo = NULL;
4976         uiapplication_x *ptr1 = NULL;
4977         serviceapplication_x *ptr2 = NULL;
4978         label_x *tmp1 = NULL;
4979         icon_x *tmp2 = NULL;
4980         category_x *tmp3 = NULL;
4981         metadata_x *tmp4 = NULL;
4982         permission_x *tmp5 = NULL;
4983         image_x *tmp6 = NULL;
4984         sqlite3 *appinfo_db = NULL;
4985
4986         /*get system locale*/
4987         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4988         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
4989
4990         /*get locale on db*/
4991         locale = __convert_system_locale_to_manifest_locale(syslocale);
4992         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
4993
4994         /*open db*/
4995         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &appinfo_db, SQLITE_OPEN_READONLY, NULL);
4996         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", getUserPkgParserDBPathUID(uid));
4997
4998         /*calloc pkginfo*/
4999         pkgmgr_pkginfo_x *info = NULL;
5000         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5001         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5002
5003         /*calloc manifest_info*/
5004         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5005         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5006
5007         /*calloc appinfo*/
5008         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5009         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!");
5010
5011         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
5012         ret = __exec_db_query(appinfo_db, query, __app_list_cb, (void *)info);
5013         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5014
5015         if (info->manifest_info->uiapplication) {
5016                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5017                 info->manifest_info->uiapplication = ptr1;
5018         }
5019         if (info->manifest_info->serviceapplication) {
5020                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5021                 info->manifest_info->serviceapplication = ptr2;
5022         }
5023
5024         /*UI Apps*/
5025         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5026         {
5027                 appinfo->locale = strdup(locale);
5028                 appinfo->app_component = PMINFO_UI_APP;
5029                 appinfo->package = strdup(ptr1->package);
5030                 appinfo->uiapp_info = ptr1;
5031                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5032                                 "from package_app_info where " \
5033                                 "app_id='%s'", ptr1->appid);
5034                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5035                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5036
5037                 if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
5038                         if (locale) {
5039                                 free(locale);
5040                         }
5041                         locale = __get_app_locale_by_fallback(appinfo_db, ptr1->appid, syslocale);
5042                 }
5043
5044                 memset(query, '\0', MAX_QUERY_LEN);
5045                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5046                                 "from package_app_localized_info where " \
5047                                 "app_id='%s' and app_locale='%s'",
5048                                 ptr1->appid, locale);
5049                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5050                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5051
5052                 memset(query, '\0', MAX_QUERY_LEN);
5053                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5054                                 "from package_app_localized_info where " \
5055                                 "app_id='%s' and app_locale='%s'",
5056                                 ptr1->appid, DEFAULT_LOCALE);
5057
5058                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5059                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5060
5061                 /*store setting notification icon section*/
5062                 memset(query, '\0', MAX_QUERY_LEN);
5063                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", ptr1->appid);
5064                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5065                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
5066                 
5067                 /*store app preview image info*/
5068                 memset(query, '\0', MAX_QUERY_LEN);
5069                 snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", ptr1->appid);
5070                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5071                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
5072
5073                 if (appinfo->uiapp_info->label) {
5074                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
5075                         appinfo->uiapp_info->label = tmp1;
5076                 }
5077                 if (appinfo->uiapp_info->icon) {
5078                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
5079                         appinfo->uiapp_info->icon= tmp2;
5080                 }
5081                 if (appinfo->uiapp_info->category) {
5082                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
5083                         appinfo->uiapp_info->category = tmp3;
5084                 }
5085                 if (appinfo->uiapp_info->metadata) {
5086                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
5087                         appinfo->uiapp_info->metadata = tmp4;
5088                 }
5089                 if (appinfo->uiapp_info->permission) {
5090                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
5091                         appinfo->uiapp_info->permission = tmp5;
5092                 }
5093                 if (appinfo->uiapp_info->image) {
5094                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
5095                         appinfo->uiapp_info->image = tmp6;
5096                 }
5097                 ret = app_func((void *)appinfo, user_data);
5098                 if (ret < 0)
5099                         break;
5100                 free((void *)appinfo->package);
5101                 appinfo->package = NULL;
5102         }
5103         /*Service Apps*/
5104         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5105         {
5106                 appinfo->locale = strdup(locale);
5107                 appinfo->app_component = PMINFO_SVC_APP;
5108                 appinfo->package = strdup(ptr2->package);
5109                 appinfo->svcapp_info = ptr2;
5110                 memset(query, '\0', MAX_QUERY_LEN);
5111                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5112                                 "from package_app_info where " \
5113                                 "app_id='%s'", ptr2->appid);
5114                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5115                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5116
5117                 memset(query, '\0', MAX_QUERY_LEN);
5118                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5119                                 "from package_app_localized_info where " \
5120                                 "app_id='%s' and app_locale='%s'",
5121                                 ptr2->appid, locale);
5122                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5123                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5124
5125                 memset(query, '\0', MAX_QUERY_LEN);
5126                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
5127                                 "from package_app_localized_info where " \
5128                                 "app_id='%s' and app_locale='%s'",
5129                                 ptr2->appid, DEFAULT_LOCALE);
5130                 ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5131                 tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5132
5133                 if (appinfo->svcapp_info->label) {
5134                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
5135                         appinfo->svcapp_info->label = tmp1;
5136                 }
5137                 if (appinfo->svcapp_info->icon) {
5138                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
5139                         appinfo->svcapp_info->icon= tmp2;
5140                 }
5141                 if (appinfo->svcapp_info->category) {
5142                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
5143                         appinfo->svcapp_info->category = tmp3;
5144                 }
5145                 if (appinfo->svcapp_info->metadata) {
5146                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
5147                         appinfo->svcapp_info->metadata = tmp4;
5148                 }
5149                 if (appinfo->svcapp_info->permission) {
5150                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
5151                         appinfo->svcapp_info->permission = tmp5;
5152                 }
5153                 ret = app_func((void *)appinfo, user_data);
5154                 if (ret < 0)
5155                         break;
5156                 free((void *)appinfo->package);
5157                 appinfo->package = NULL;
5158         }
5159         ret = PMINFO_R_OK;
5160
5161 catch:
5162         if (locale) {
5163                 free(locale);
5164                 locale = NULL;
5165         }
5166         if (syslocale) {
5167                 free(syslocale);
5168                 syslocale = NULL;
5169         }
5170         sqlite3_close(appinfo_db);
5171         if (appinfo) {
5172                 free(appinfo);
5173                 appinfo = NULL;
5174         }
5175         __cleanup_pkginfo(info);
5176         return ret;
5177 }
5178
5179 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
5180 {
5181         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
5182 }
5183
5184 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid, pkgmgrinfo_appinfo_h *handle)
5185 {
5186         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL");
5187         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5188
5189         pkgmgr_appinfo_x *appinfo = NULL;
5190         char *syslocale = NULL;
5191         char *locale = NULL;
5192         int ret = -1;
5193         int exist = 0;
5194         label_x *tmp1 = NULL;
5195         icon_x *tmp2 = NULL;
5196         category_x *tmp3 = NULL;
5197         metadata_x *tmp4 = NULL;
5198         permission_x *tmp5 = NULL;
5199         image_x *tmp6 = NULL;
5200         char query[MAX_QUERY_LEN] = {'\0'};
5201         sqlite3 *appinfo_db = NULL;
5202
5203         *handle = NULL;
5204         /*open db*/
5205         _LOGD("getUserPkgParserDBPathUID(%d) returns: [%s]", uid, getUserPkgParserDBPathUID(uid));
5206         ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &appinfo_db, SQLITE_OPEN_READONLY, NULL);
5207         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", getUserPkgParserDBPathUID(uid));
5208
5209   
5210         /*check appid exist on db*/
5211         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", appid);
5212         ret = __exec_db_query(appinfo_db, query, __validate_cb, (void *)&exist);
5213         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "sqlite3_exec fail");
5214         tryvm_if(exist == 0, ret = PMINFO_R_ERROR, "Appid[%s] not found in DB", appid);
5215
5216         /*get system locale*/
5217         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5218         tryvm_if(syslocale == NULL, ret = PMINFO_R_ERROR, "current locale is NULL");
5219
5220         /*get locale on db*/
5221         locale = __convert_system_locale_to_manifest_locale(syslocale);
5222         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL");
5223
5224         /*calloc appinfo*/
5225         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5226         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for appinfo");
5227
5228         /*check app_component from DB*/
5229         memset(query, '\0', MAX_QUERY_LEN);
5230         snprintf(query, MAX_QUERY_LEN, "select app_component, package from package_app_info where app_id='%s' ", appid);
5231         ret = __exec_db_query(appinfo_db, query, __appcomponent_cb, (void *)appinfo);
5232         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5233
5234         /*calloc app_component*/
5235         if (appinfo->app_component == PMINFO_UI_APP) {
5236                 appinfo->uiapp_info = (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
5237                 tryvm_if(appinfo->uiapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for uiapp info");
5238         } else {
5239                 appinfo->svcapp_info = (serviceapplication_x *)calloc(1, sizeof(serviceapplication_x));
5240                 tryvm_if(appinfo->svcapp_info == NULL, ret = PMINFO_R_ERROR, "Failed to allocate memory for svcapp info");
5241         }
5242         appinfo->locale = strdup(locale);
5243
5244         /*populate app_info from DB*/
5245         memset(query, '\0', MAX_QUERY_LEN);
5246         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' ", appid);
5247         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5248         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5249
5250         memset(query, '\0', MAX_QUERY_LEN);
5251         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, locale);
5252         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5253         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Info DB Information retrieval failed");
5254
5255         /*Also store the values corresponding to default locales*/
5256         memset(query, '\0', MAX_QUERY_LEN);
5257         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where app_id='%s' and app_locale='%s'", appid, DEFAULT_LOCALE);
5258         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5259         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Localized Info DB Information retrieval failed");
5260
5261         /*Populate app category*/
5262         memset(query, '\0', MAX_QUERY_LEN);
5263         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_category where app_id='%s'", appid);
5264         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5265         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Category Info DB Information retrieval failed");
5266
5267         /*Populate app metadata*/
5268         memset(query, '\0', MAX_QUERY_LEN);
5269         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_metadata where app_id='%s'", appid);
5270         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5271         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App Metadata Info DB Information retrieval failed");
5272
5273         /*Populate app permission*/
5274         memset(query, '\0', MAX_QUERY_LEN);
5275         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_permission where app_id='%s'", appid);
5276         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5277         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App permission Info DB Information retrieval failed");
5278
5279         /*store setting notification icon section*/
5280         memset(query, '\0', MAX_QUERY_LEN);
5281         snprintf(query, MAX_QUERY_LEN, "select * from package_app_icon_section_info where app_id='%s'", appid);
5282         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5283         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App icon section Info DB Information retrieval failed");
5284
5285         /*store app preview image info*/
5286         memset(query, '\0', MAX_QUERY_LEN);
5287         snprintf(query, MAX_QUERY_LEN, "select app_image_section, app_image from package_app_image_info where app_id='%s'", appid);
5288         ret = __exec_db_query(appinfo_db, query, __appinfo_cb, (void *)appinfo);
5289         tryvm_if(ret == -1, ret = PMINFO_R_ERROR, "App image Info DB Information retrieval failed");
5290
5291         switch (appinfo->app_component) {
5292         case PMINFO_UI_APP:
5293                 if (appinfo->uiapp_info->label) {
5294                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
5295                         appinfo->uiapp_info->label = tmp1;
5296                 }
5297                 if (appinfo->uiapp_info->icon) {
5298                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
5299                         appinfo->uiapp_info->icon = tmp2;
5300                 }
5301                 if (appinfo->uiapp_info->category) {
5302                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
5303                         appinfo->uiapp_info->category = tmp3;
5304                 }
5305                 if (appinfo->uiapp_info->metadata) {
5306                         LISTHEAD(appinfo->uiapp_info->metadata, tmp4);
5307                         appinfo->uiapp_info->metadata = tmp4;
5308                 }
5309                 if (appinfo->uiapp_info->permission) {
5310                         LISTHEAD(appinfo->uiapp_info->permission, tmp5);
5311                         appinfo->uiapp_info->permission = tmp5;
5312                 }
5313                 if (appinfo->uiapp_info->image) {
5314                         LISTHEAD(appinfo->uiapp_info->image, tmp6);
5315                         appinfo->uiapp_info->image = tmp6;
5316                 }
5317                 break;
5318         case PMINFO_SVC_APP:
5319                 if (appinfo->svcapp_info->label) {
5320                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
5321                         appinfo->svcapp_info->label = tmp1;
5322                 }
5323                 if (appinfo->svcapp_info->icon) {
5324                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
5325                         appinfo->svcapp_info->icon = tmp2;
5326                 }
5327                 if (appinfo->svcapp_info->category) {
5328                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
5329                         appinfo->svcapp_info->category = tmp3;
5330                 }
5331                 if (appinfo->svcapp_info->metadata) {
5332                         LISTHEAD(appinfo->svcapp_info->metadata, tmp4);
5333                         appinfo->svcapp_info->metadata = tmp4;
5334                 }
5335                 if (appinfo->svcapp_info->permission) {
5336                         LISTHEAD(appinfo->svcapp_info->permission, tmp5);
5337                         appinfo->svcapp_info->permission = tmp5;
5338                 }
5339                 break;
5340         default:
5341                 break;
5342         }
5343
5344         ret = PMINFO_R_OK;
5345
5346 catch:
5347         if (ret == PMINFO_R_OK)
5348                 *handle = (void*)appinfo;
5349         else {
5350                 *handle = NULL;
5351                 __cleanup_appinfo(appinfo);
5352         }
5353
5354         sqlite3_close(appinfo_db);
5355         if (syslocale) {
5356                 free(syslocale);
5357                 syslocale = NULL;
5358         }
5359         if (locale) {
5360                 free(locale);
5361                 locale = NULL;
5362         }
5363         return ret;
5364 }
5365
5366 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
5367 {
5368         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
5369 }
5370
5371 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h  handle, char **appid)
5372 {
5373         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5374         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5375         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5376
5377         if (info->app_component == PMINFO_UI_APP)
5378                 *appid = (char *)info->uiapp_info->appid;
5379         else if (info->app_component == PMINFO_SVC_APP)
5380                 *appid = (char *)info->svcapp_info->appid;
5381
5382         return PMINFO_R_OK;
5383 }
5384
5385 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h  handle, char **pkg_name)
5386 {
5387         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5388         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5389         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5390
5391         *pkg_name = (char *)info->package;
5392
5393         return PMINFO_R_OK;
5394 }
5395
5396 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h  handle, char **pkgid)
5397 {
5398         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5399         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5400         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5401
5402         *pkgid = (char *)info->package;
5403
5404         return PMINFO_R_OK;
5405 }
5406
5407 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h  handle, char **exec)
5408 {
5409         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5410         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5411         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5412
5413         if (info->app_component == PMINFO_UI_APP)
5414                 *exec = (char *)info->uiapp_info->exec;
5415         if (info->app_component == PMINFO_SVC_APP)
5416                 *exec = (char *)info->svcapp_info->exec;
5417
5418         return PMINFO_R_OK;
5419 }
5420
5421
5422 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
5423 {
5424         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5425         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5426         char *locale = NULL;
5427         icon_x *ptr = NULL;
5428         icon_x *start = NULL;
5429         *icon = NULL;
5430
5431         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5432                 locale = info->locale;
5433                 retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
5434
5435         if (info->app_component == PMINFO_UI_APP)
5436                 start = info->uiapp_info->icon;
5437         if (info->app_component == PMINFO_SVC_APP)
5438                 start = info->svcapp_info->icon;
5439         for(ptr = start; ptr != NULL; ptr = ptr->next)
5440         {
5441                 if (ptr->lang) {
5442                         if (strcmp(ptr->lang, locale) == 0) {
5443                                 *icon = (char *)ptr->text;
5444                                 if (strcasecmp(*icon, "(null)") == 0) {
5445                                         locale = DEFAULT_LOCALE;
5446                                         continue;
5447                                 } else
5448                                         break;
5449                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
5450                                 *icon = (char *)ptr->text;
5451                                 break;
5452                         }
5453                 }
5454         }
5455         return PMINFO_R_OK;
5456 }
5457
5458
5459 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h  handle, char **label)
5460 {
5461         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5462         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5463         char *locale = NULL;
5464         label_x *ptr = NULL;
5465         label_x *start = NULL;
5466         *label = NULL;
5467
5468         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5469         locale = info->locale;
5470         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
5471
5472         if (info->app_component == PMINFO_UI_APP)
5473                 start = info->uiapp_info->label;
5474         if (info->app_component == PMINFO_SVC_APP)
5475                 start = info->svcapp_info->label;
5476         for(ptr = start; ptr != NULL; ptr = ptr->next)
5477         {
5478                 if (ptr->lang) {
5479                         if (strcmp(ptr->lang, locale) == 0) {
5480                                 *label = (char *)ptr->text;
5481                                 if (strcasecmp(*label, "(null)") == 0) {
5482                                         locale = DEFAULT_LOCALE;
5483                                         continue;
5484                                 } else
5485                                         break;
5486                         } else if (strncasecmp(ptr->lang, locale, 2) == 0) {
5487                                 *label = (char *)ptr->text;
5488                                 if (strcasecmp(*label, "(null)") == 0) {
5489                                                 locale = DEFAULT_LOCALE;
5490                                                 continue;
5491                                 } else
5492                                                 break;
5493                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
5494                                 *label = (char *)ptr->text;
5495                                 break;
5496                         }
5497                 }
5498         }
5499         return PMINFO_R_OK;
5500 }
5501
5502
5503 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_component *component)
5504 {
5505         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5506         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5507         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5508
5509         if (info->app_component == PMINFO_UI_APP)
5510                 *component = PMINFO_UI_APP;
5511         else if (info->app_component == PMINFO_SVC_APP)
5512                 *component = PMINFO_SVC_APP;
5513         else
5514                 return PMINFO_R_ERROR;
5515
5516         return PMINFO_R_OK;
5517 }
5518
5519 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h  handle, char **app_type)
5520 {
5521         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5522         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5523         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5524
5525         if (info->app_component == PMINFO_UI_APP)
5526                 *app_type = (char *)info->uiapp_info->type;
5527         if (info->app_component == PMINFO_SVC_APP)
5528                 *app_type = (char *)info->svcapp_info->type;
5529
5530         return PMINFO_R_OK;
5531 }
5532
5533 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
5534                                         int *operation_count, char ***operation)
5535 {
5536         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5537         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5538         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5539         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5540         *operation_count = data->operation_count;
5541         *operation = data->operation;
5542         return PMINFO_R_OK;
5543 }
5544
5545 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
5546                                         int *uri_count, char ***uri)
5547 {
5548         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5549         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5550         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5551         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5552         *uri_count = data->uri_count;
5553         *uri = data->uri;
5554         return PMINFO_R_OK;
5555 }
5556
5557 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
5558                                         int *mime_count, char ***mime)
5559 {
5560         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5561         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5562         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5563         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5564         *mime_count = data->mime_count;
5565         *mime = data->mime;
5566         return PMINFO_R_OK;
5567 }
5568
5569 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
5570                                         int *subapp_count, char ***subapp)
5571 {
5572         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5573         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5574         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5575         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
5576         *subapp_count = data->subapp_count;
5577         *subapp = data->subapp;
5578         return PMINFO_R_OK;
5579 }
5580
5581 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
5582 {
5583         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5584         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5585
5586         char *val = NULL;
5587         icon_x *ptr = NULL;
5588         icon_x *start = NULL;
5589         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5590
5591         start = info->uiapp_info->icon;
5592
5593         for(ptr = start; ptr != NULL; ptr = ptr->next)
5594         {
5595                 if (ptr->section) {
5596                         val = (char *)ptr->section;
5597                         if (strcmp(val, "setting") == 0){
5598                                 *icon = (char *)ptr->text;
5599                                 break;
5600                         }
5601                 }
5602         }
5603         return PMINFO_R_OK;
5604 }
5605
5606
5607 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
5608 {
5609         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5610         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5611
5612         char *val = NULL;
5613         icon_x *ptr = NULL;
5614         icon_x *start = NULL;
5615         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5616
5617         start = info->uiapp_info->icon;
5618
5619         for(ptr = start; ptr != NULL; ptr = ptr->next)
5620         {
5621                 if (ptr->section) {
5622                         val = (char *)ptr->section;
5623
5624                         if (strcmp(val, "notification") == 0){
5625                                 *icon = (char *)ptr->text;
5626                                 break;
5627                         }
5628                 }
5629         }
5630
5631         return PMINFO_R_OK;
5632 }
5633
5634 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_recentimage *type)
5635 {
5636         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5637         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5638         char *val = NULL;
5639         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5640         val = (char *)info->uiapp_info->recentimage;
5641         if (val) {
5642                 if (strcasecmp(val, "capture") == 0)
5643                         *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
5644                 else if (strcasecmp(val, "icon") == 0)
5645                         *type = PMINFO_RECENTIMAGE_USE_ICON;
5646                 else
5647                         *type = PMINFO_RECENTIMAGE_USE_NOTHING;
5648         }
5649
5650         return PMINFO_R_OK;
5651 }
5652
5653 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h  handle, char **preview_img)
5654 {
5655         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5656         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5657
5658         char *val = NULL;
5659         image_x *ptr = NULL;
5660         image_x *start = NULL;
5661         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5662
5663         start = info->uiapp_info->image;
5664
5665         for(ptr = start; ptr != NULL; ptr = ptr->next)
5666         {
5667                 if (ptr->section) {
5668                         val = (char *)ptr->section;
5669
5670                         if (strcmp(val, "preview") == 0)
5671                                 *preview_img = (char *)ptr->text;
5672
5673                         break;
5674                 }
5675         }
5676         return PMINFO_R_OK;
5677 }
5678
5679 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_permission_type *permission)
5680 {
5681         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
5682         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
5683
5684         char *val = NULL;
5685         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5686
5687         if (info->app_component == PMINFO_UI_APP)
5688                 val = info->uiapp_info->permission_type;
5689         else if (info->app_component == PMINFO_SVC_APP)
5690                 val = info->svcapp_info->permission_type;
5691         else
5692                 return PMINFO_R_ERROR;
5693
5694         if (strcmp(val, "signature") == 0)
5695                 *permission = PMINFO_PERMISSION_SIGNATURE;
5696         else if (strcmp(val, "privilege") == 0)
5697                 *permission = PMINFO_PERMISSION_PRIVILEGE;
5698         else
5699                 *permission = PMINFO_PERMISSION_NORMAL;
5700
5701         return PMINFO_R_OK;
5702 }
5703
5704 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h  handle, char **component_type)
5705 {
5706         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5707         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5708         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5709
5710         *component_type = (char *)info->uiapp_info->component_type;
5711
5712         return PMINFO_R_OK;
5713 }
5714
5715 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
5716 {
5717         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5718         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5719         char *val = NULL;
5720         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5721         val = (char *)info->uiapp_info->hwacceleration;
5722         if (val) {
5723                 if (strcasecmp(val, "not-use-GL") == 0)
5724                         *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
5725                 else if (strcasecmp(val, "use-GL") == 0)
5726                         *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
5727                 else
5728                         *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
5729         }
5730         return PMINFO_R_OK;
5731 }
5732
5733 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_screenreader *screenreader)
5734 {
5735         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5736         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5737         char *val = NULL;
5738         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5739         val = (char *)info->uiapp_info->screenreader;
5740         if (val) {
5741                 if (strcasecmp(val, "screenreader-off") == 0)
5742                         *screenreader = PMINFO_SCREENREADER_OFF;
5743                 else if (strcasecmp(val, "screenreader-on") == 0)
5744                         *screenreader = PMINFO_SCREENREADER_ON;
5745                 else
5746                         *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
5747         }
5748         return PMINFO_R_OK;
5749 }
5750
5751 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h  handle, char **portrait_img, char **landscape_img)
5752 {
5753         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5754         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5755         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5756         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5757
5758         if (info->app_component == PMINFO_UI_APP){
5759                 *portrait_img = (char *)info->uiapp_info->portraitimg;
5760                 *landscape_img = (char *)info->uiapp_info->landscapeimg;
5761         }
5762
5763         return PMINFO_R_OK;
5764 }
5765
5766 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
5767 {
5768         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5769         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
5770         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5771
5772         *submode_mainid = (char *)info->uiapp_info->submode_mainid;
5773
5774         return PMINFO_R_OK;
5775 }
5776
5777 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
5778                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
5779 {
5780         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5781         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5782         int ret = -1;
5783         permission_x *ptr = NULL;
5784         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5785         if (info->app_component == PMINFO_UI_APP)
5786                 ptr = info->uiapp_info->permission;
5787         else if (info->app_component == PMINFO_SVC_APP)
5788                 ptr = info->svcapp_info->permission;
5789         else
5790                 return PMINFO_R_EINVAL;
5791         for (; ptr; ptr = ptr->next) {
5792                 if (ptr->value) {
5793                         ret = permission_func(ptr->value, user_data);
5794                         if (ret < 0)
5795                                 break;
5796                 }
5797         }
5798         return PMINFO_R_OK;
5799 }
5800
5801 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
5802                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
5803 {
5804         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5805         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5806         int ret = -1;
5807         category_x *ptr = NULL;
5808         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5809         if (info->app_component == PMINFO_UI_APP)
5810                 ptr = info->uiapp_info->category;
5811         else if (info->app_component == PMINFO_SVC_APP)
5812                 ptr = info->svcapp_info->category;
5813         else
5814                 return PMINFO_R_EINVAL;
5815         for (; ptr; ptr = ptr->next) {
5816                 if (ptr->name) {
5817                         ret = category_func(ptr->name, user_data);
5818                         if (ret < 0)
5819                                 break;
5820                 }
5821         }
5822         return PMINFO_R_OK;
5823 }
5824
5825 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
5826                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
5827 {
5828         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5829         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5830         int ret = -1;
5831         metadata_x *ptr = NULL;
5832         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5833         if (info->app_component == PMINFO_UI_APP)
5834                 ptr = info->uiapp_info->metadata;
5835         else if (info->app_component == PMINFO_SVC_APP)
5836                 ptr = info->svcapp_info->metadata;
5837         else
5838                 return PMINFO_R_EINVAL;
5839         for (; ptr; ptr = ptr->next) {
5840                 if (ptr->key) {
5841                         ret = metadata_func(ptr->key, ptr->value, user_data);
5842                         if (ret < 0)
5843                                 break;
5844                 }
5845         }
5846         return PMINFO_R_OK;
5847 }
5848
5849 API int pkgmgrinfo_usr_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
5850                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data, uid_t uid)
5851 {
5852         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
5853         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
5854         int i = 0;
5855         int ret = -1;
5856         int oc = 0;
5857         int mc = 0;
5858         int uc = 0;
5859         int sc = 0;
5860         char *pkgid = NULL;
5861         char *manifest = NULL;
5862         char **operation = NULL;
5863         char **uri = NULL;
5864         char **mime = NULL;
5865         char **subapp = NULL;
5866         appcontrol_x *appcontrol = NULL;
5867         manifest_x *mfx = NULL;
5868         operation_x *op = NULL;
5869         uri_x *ui = NULL;
5870         mime_x *mi = NULL;
5871         subapp_x *sa = NULL;
5872         pkgmgrinfo_app_component component;
5873         pkgmgrinfo_appcontrol_x *ptr = NULL;
5874         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
5875         if (ret < 0) {
5876                 _LOGE("Failed to get package name\n");
5877                 return PMINFO_R_ERROR;
5878         }
5879         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
5880         if (ret < 0) {
5881                 _LOGE("Failed to get app component name\n");
5882                 return PMINFO_R_ERROR;
5883         }
5884         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
5885         if (manifest == NULL) {
5886                 _LOGE("Failed to fetch package manifest file\n");
5887                 return PMINFO_R_ERROR;
5888         }
5889         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
5890         if (mfx == NULL) {
5891                 _LOGE("Failed to parse package manifest file\n");
5892                 free(manifest);
5893                 manifest = NULL;
5894                 return PMINFO_R_ERROR;
5895         }
5896         free(manifest);
5897         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
5898         if (ptr == NULL) {
5899                 _LOGE("Out of Memory!!!\n");
5900                 pkgmgr_parser_free_manifest_xml(mfx);
5901                 return PMINFO_R_ERROR;
5902         }
5903         /*Get Operation, Uri, Mime*/
5904         switch (component) {
5905         case PMINFO_UI_APP:
5906                 if (mfx->uiapplication) {
5907                         if (mfx->uiapplication->appsvc) {
5908                                 appcontrol = mfx->uiapplication->appsvc;
5909                         }
5910                 }
5911                 break;
5912         case PMINFO_SVC_APP:
5913                 if (mfx->serviceapplication) {
5914                         if (mfx->serviceapplication->appsvc) {
5915                                 appcontrol = mfx->serviceapplication->appsvc;
5916                         }
5917                 }
5918                 break;
5919         default:
5920                 break;
5921         }
5922         for (; appcontrol; appcontrol = appcontrol->next) {
5923                 op = appcontrol->operation;
5924                 for (; op; op = op->next)
5925                         oc = oc + 1;
5926                 op = appcontrol->operation;
5927
5928                 ui = appcontrol->uri;
5929                 for (; ui; ui = ui->next)
5930                         uc = uc + 1;
5931                 ui = appcontrol->uri;
5932
5933                 mi = appcontrol->mime;
5934                 for (; mi; mi = mi->next)
5935                         mc = mc + 1;
5936                 mi = appcontrol->mime;
5937
5938                 sa = appcontrol->subapp;
5939                 for (; sa; sa = sa->next)
5940                         sc = sc + 1;
5941                 sa = appcontrol->subapp;
5942
5943                 operation = (char **)calloc(oc, sizeof(char *));
5944                 for (i = 0; i < oc; i++) {
5945                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
5946                         op = op->next;
5947                 }
5948
5949                 uri = (char **)calloc(uc, sizeof(char *));
5950                 for (i = 0; i < uc; i++) {
5951                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
5952                         ui = ui->next;
5953                 }
5954
5955                 mime = (char **)calloc(mc, sizeof(char *));
5956                 for (i = 0; i < mc; i++) {
5957                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
5958                         mi = mi->next;
5959                 }
5960
5961                 subapp = (char **)calloc(sc, sizeof(char *));
5962                 for (i = 0; i < sc; i++) {
5963                         subapp[i] = strndup(sa->name, PKG_STRING_LEN_MAX - 1);
5964                         sa = sa->next;
5965                 }
5966
5967                 /*populate appcontrol handle*/
5968                 ptr->operation_count = oc;
5969                 ptr->uri_count = uc;
5970                 ptr->mime_count = mc;
5971                 ptr->subapp_count = sc;
5972                 ptr->operation = operation;
5973                 ptr->uri = uri;
5974                 ptr->mime = mime;
5975                 ptr->subapp = subapp;
5976
5977                 ret = appcontrol_func((void *)ptr, user_data);
5978                 for (i = 0; i < oc; i++) {
5979                         if (operation[i]) {
5980                                 free(operation[i]);
5981                                 operation[i] = NULL;
5982                         }
5983                 }
5984                 if (operation) {
5985                         free(operation);
5986                         operation = NULL;
5987                 }
5988                 for (i = 0; i < uc; i++) {
5989                         if (uri[i]) {
5990                                 free(uri[i]);
5991                                 uri[i] = NULL;
5992                         }
5993                 }
5994                 if (uri) {
5995                         free(uri);
5996                         uri = NULL;
5997                 }
5998                 for (i = 0; i < mc; i++) {
5999                         if (mime[i]) {
6000                                 free(mime[i]);
6001                                 mime[i] = NULL;
6002                         }
6003                 }
6004                 if (mime) {
6005                         free(mime);
6006                         mime = NULL;
6007                 }
6008                 for (i = 0; i < sc; i++) {
6009                         if (subapp[i]) {
6010                                 free(subapp[i]);
6011                                 subapp[i] = NULL;
6012                         }
6013                 }
6014                 if (subapp) {
6015                         free(subapp);
6016                         subapp = NULL;
6017                 }
6018                 if (ret < 0)
6019                         break;
6020                 uc = 0;
6021                 mc = 0;
6022                 oc = 0;
6023                 sc = 0;
6024         }
6025         pkgmgr_parser_free_manifest_xml(mfx);
6026         if (ptr) {
6027                 free(ptr);
6028                 ptr = NULL;
6029         }
6030         return PMINFO_R_OK;
6031 }
6032
6033 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
6034                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
6035 {
6036         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6037         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
6038         int i = 0;
6039         int ret = -1;
6040         int oc = 0;
6041         int mc = 0;
6042         int uc = 0;
6043         int sc = 0;
6044         char *pkgid = NULL;
6045         char *manifest = NULL;
6046         char **operation = NULL;
6047         char **uri = NULL;
6048         char **mime = NULL;
6049         char **subapp = NULL;
6050         appcontrol_x *appcontrol = NULL;
6051         manifest_x *mfx = NULL;
6052         operation_x *op = NULL;
6053         uri_x *ui = NULL;
6054         mime_x *mi = NULL;
6055         subapp_x *sa = NULL;
6056         pkgmgrinfo_app_component component;
6057         pkgmgrinfo_appcontrol_x *ptr = NULL;
6058         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
6059         if (ret < 0) {
6060                 _LOGE("Failed to get package name\n");
6061                 return PMINFO_R_ERROR;
6062         }
6063         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
6064         if (ret < 0) {
6065                 _LOGE("Failed to get app component name\n");
6066                 return PMINFO_R_ERROR;
6067         }
6068         manifest = pkgmgr_parser_get_manifest_file(pkgid);
6069         if (manifest == NULL) {
6070                 _LOGE("Failed to fetch package manifest file\n");
6071                 return PMINFO_R_ERROR;
6072         }
6073         mfx = pkgmgr_parser_process_manifest_xml(manifest);
6074         if (mfx == NULL) {
6075                 _LOGE("Failed to parse package manifest file\n");
6076                 free(manifest);
6077                 manifest = NULL;
6078                 return PMINFO_R_ERROR;
6079         }
6080         free(manifest);
6081         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
6082         if (ptr == NULL) {
6083                 _LOGE("Out of Memory!!!\n");
6084                 pkgmgr_parser_free_manifest_xml(mfx);
6085                 return PMINFO_R_ERROR;
6086         }
6087         /*Get Operation, Uri, Mime*/
6088         switch (component) {
6089         case PMINFO_UI_APP:
6090                 if (mfx->uiapplication) {
6091                         if (mfx->uiapplication->appsvc) {
6092                                 appcontrol = mfx->uiapplication->appsvc;
6093                         }
6094                 }
6095                 break;
6096         case PMINFO_SVC_APP:
6097                 if (mfx->serviceapplication) {
6098                         if (mfx->serviceapplication->appsvc) {
6099                                 appcontrol = mfx->serviceapplication->appsvc;
6100                         }
6101                 }
6102                 break;
6103         default:
6104                 break;
6105         }
6106         for (; appcontrol; appcontrol = appcontrol->next) {
6107                 op = appcontrol->operation;
6108                 for (; op; op = op->next)
6109                         oc = oc + 1;
6110                 op = appcontrol->operation;
6111
6112                 ui = appcontrol->uri;
6113                 for (; ui; ui = ui->next)
6114                         uc = uc + 1;
6115                 ui = appcontrol->uri;
6116
6117                 mi = appcontrol->mime;
6118                 for (; mi; mi = mi->next)
6119                         mc = mc + 1;
6120                 mi = appcontrol->mime;
6121
6122                 sa = appcontrol->subapp;
6123                 for (; sa; sa = sa->next)
6124                         sc = sc + 1;
6125                 sa = appcontrol->subapp;
6126
6127                 operation = (char **)calloc(oc, sizeof(char *));
6128                 for (i = 0; i < oc; i++) {
6129                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
6130                         op = op->next;
6131                 }
6132
6133                 uri = (char **)calloc(uc, sizeof(char *));
6134                 for (i = 0; i < uc; i++) {
6135                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
6136                         ui = ui->next;
6137                 }
6138
6139                 mime = (char **)calloc(mc, sizeof(char *));
6140                 for (i = 0; i < mc; i++) {
6141                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
6142                         mi = mi->next;
6143                 }
6144
6145                 subapp = (char **)calloc(sc, sizeof(char *));
6146                 for (i = 0; i < sc; i++) {
6147                         subapp[i] = strndup(sa->name, PKG_STRING_LEN_MAX - 1);
6148                         sa = sa->next;
6149                 }
6150
6151                 /*populate appcontrol handle*/
6152                 ptr->operation_count = oc;
6153                 ptr->uri_count = uc;
6154                 ptr->mime_count = mc;
6155                 ptr->subapp_count = sc;
6156                 ptr->operation = operation;
6157                 ptr->uri = uri;
6158                 ptr->mime = mime;
6159                 ptr->subapp = subapp;
6160
6161                 ret = appcontrol_func((void *)ptr, user_data);
6162                 for (i = 0; i < oc; i++) {
6163                         if (operation[i]) {
6164                                 free(operation[i]);
6165                                 operation[i] = NULL;
6166                         }
6167                 }
6168                 if (operation) {
6169                         free(operation);
6170                         operation = NULL;
6171                 }
6172                 for (i = 0; i < uc; i++) {
6173                         if (uri[i]) {
6174                                 free(uri[i]);
6175                                 uri[i] = NULL;
6176                         }
6177                 }
6178                 if (uri) {
6179                         free(uri);
6180                         uri = NULL;
6181                 }
6182                 for (i = 0; i < mc; i++) {
6183                         if (mime[i]) {
6184                                 free(mime[i]);
6185                                 mime[i] = NULL;
6186                         }
6187                 }
6188                 if (mime) {
6189                         free(mime);
6190                         mime = NULL;
6191                 }
6192                 for (i = 0; i < sc; i++) {
6193                         if (subapp[i]) {
6194                                 free(subapp[i]);
6195                                 subapp[i] = NULL;
6196                         }
6197                 }
6198                 if (subapp) {
6199                         free(subapp);
6200                         subapp = NULL;
6201                 }
6202                 if (ret < 0)
6203                         break;
6204                 uc = 0;
6205                 mc = 0;
6206                 oc = 0;
6207                 sc = 0;
6208         }
6209         pkgmgr_parser_free_manifest_xml(mfx);
6210         if (ptr) {
6211                 free(ptr);
6212                 ptr = NULL;
6213         }
6214         return PMINFO_R_OK;
6215 }
6216
6217 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h  handle, bool *nodisplay)
6218 {
6219         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6220         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6221         char *val = NULL;
6222         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6223         val = (char *)info->uiapp_info->nodisplay;
6224         if (val) {
6225                 if (strcasecmp(val, "true") == 0)
6226                         *nodisplay = 1;
6227                 else if (strcasecmp(val, "false") == 0)
6228                         *nodisplay = 0;
6229                 else
6230                         *nodisplay = 0;
6231         }
6232         return PMINFO_R_OK;
6233 }
6234
6235 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h  handle, bool *multiple)
6236 {
6237         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6238         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6239         char *val = NULL;
6240         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6241         val = (char *)info->uiapp_info->multiple;
6242         if (val) {
6243                 if (strcasecmp(val, "true") == 0)
6244                         *multiple = 1;
6245                 else if (strcasecmp(val, "false") == 0)
6246                         *multiple = 0;
6247                 else
6248                         *multiple = 0;
6249         }
6250         return PMINFO_R_OK;
6251 }
6252
6253 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
6254 {
6255         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6256         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6257         char *val = NULL;
6258         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6259         val = (char *)info->uiapp_info->indicatordisplay;
6260         if (val) {
6261                 if (strcasecmp(val, "true") == 0){
6262                         *indicator_disp = 1;
6263                 }else if (strcasecmp(val, "false") == 0){
6264                         *indicator_disp = 0;
6265                 }else{
6266                         *indicator_disp = 0;
6267                 }
6268         }
6269         return PMINFO_R_OK;
6270 }
6271
6272 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
6273 {
6274         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6275         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6276         char *val = NULL;
6277         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6278         val = (char *)info->uiapp_info->taskmanage;
6279         if (val) {
6280                 if (strcasecmp(val, "true") == 0)
6281                         *taskmanage = 1;
6282                 else if (strcasecmp(val, "false") == 0)
6283                         *taskmanage = 0;
6284                 else
6285                         *taskmanage = 0;
6286         }
6287         return PMINFO_R_OK;
6288 }
6289
6290 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
6291 {
6292         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6293         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6294         char *val = NULL;
6295         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6296         if (info->app_component == PMINFO_UI_APP)
6297                 val = (char *)info->uiapp_info->enabled;
6298         else if (info->app_component == PMINFO_SVC_APP)
6299                 val = (char *)info->uiapp_info->enabled;
6300         else {
6301                 _LOGE("invalid component type\n");
6302                 return PMINFO_R_EINVAL;
6303         }
6304
6305         if (val) {
6306                 if (strcasecmp(val, "true") == 0)
6307                         *enabled = 1;
6308                 else if (strcasecmp(val, "false") == 0)
6309                         *enabled = 0;
6310                 else
6311                         *enabled = 1;
6312         }
6313         return PMINFO_R_OK;
6314
6315 }
6316
6317 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
6318 {
6319         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6320         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6321         char *val = NULL;
6322         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6323         val = (char *)info->svcapp_info->onboot;
6324         if (val) {
6325                 if (strcasecmp(val, "true") == 0)
6326                         *onboot = 1;
6327                 else if (strcasecmp(val, "false") == 0)
6328                         *onboot = 0;
6329                 else
6330                         *onboot = 0;
6331         }
6332         return PMINFO_R_OK;
6333 }
6334
6335 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
6336 {
6337         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6338         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6339         char *val = NULL;
6340         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6341         val = (char *)info->svcapp_info->autorestart;
6342         if (val) {
6343                 if (strcasecmp(val, "true") == 0)
6344                         *autorestart = 1;
6345                 else if (strcasecmp(val, "false") == 0)
6346                         *autorestart = 0;
6347                 else
6348                         *autorestart = 0;
6349         }
6350         return PMINFO_R_OK;
6351 }
6352
6353 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
6354 {
6355         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6356         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
6357         char *val = NULL;
6358         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6359         val = (char *)info->uiapp_info->mainapp;
6360         if (val) {
6361                 if (strcasecmp(val, "true") == 0)
6362                         *mainapp = 1;
6363                 else if (strcasecmp(val, "false") == 0)
6364                         *mainapp = 0;
6365                 else
6366                         *mainapp = 0;
6367         }
6368         return PMINFO_R_OK;
6369 }
6370
6371 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
6372 {
6373         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6374         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6375         char *val = NULL;
6376         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6377         val = (char *)info->uiapp_info->preload;
6378         if (val) {
6379                 if (strcasecmp(val, "true") == 0)
6380                         *preload = 1;
6381                 else if (strcasecmp(val, "false") == 0)
6382                         *preload = 0;
6383                 else
6384                         *preload = 0;
6385         }
6386         return PMINFO_R_OK;
6387 }
6388
6389 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
6390 {
6391         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
6392         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
6393         char *val = NULL;
6394         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6395         val = (char *)info->uiapp_info->submode;
6396         if (val) {
6397                 if (strcasecmp(val, "true") == 0)
6398                         *submode = 1;
6399                 else if (strcasecmp(val, "false") == 0)
6400                         *submode = 0;
6401                 else
6402                         *submode = 0;
6403         }
6404         return PMINFO_R_OK;
6405 }
6406
6407 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h  handle)
6408 {
6409         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
6410         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
6411         __cleanup_appinfo(info);
6412         return PMINFO_R_OK;
6413 }
6414
6415 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
6416 {
6417         return (pkgmgrinfo_pkginfo_filter_create(handle));
6418 }
6419
6420 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
6421 {
6422         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
6423 }
6424
6425 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
6426                                 const char *property, const int value)
6427 {
6428         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6429         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6430         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
6431         char *val = NULL;
6432         GSList *link = NULL;
6433         int prop = -1;
6434         prop = _pminfo_appinfo_convert_to_prop_int(property);
6435         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
6436                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
6437                 _LOGE("Invalid Integer Property\n");
6438                 return PMINFO_R_EINVAL;
6439         }
6440         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6441         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6442         if (node == NULL) {
6443                 _LOGE("Out of Memory!!!\n");
6444                 return PMINFO_R_ERROR;
6445         }
6446         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
6447         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
6448         if (val == NULL) {
6449                 _LOGE("Out of Memory\n");
6450                 free(node);
6451                 node = NULL;
6452                 return PMINFO_R_ERROR;
6453         }
6454         node->prop = prop;
6455         node->value = val;
6456         /*If API is called multiple times for same property, we should override the previous values.
6457         Last value set will be used for filtering.*/
6458         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6459         if (link)
6460                 filter->list = g_slist_delete_link(filter->list, link);
6461         filter->list = g_slist_append(filter->list, (gpointer)node);
6462         return PMINFO_R_OK;
6463
6464 }
6465
6466 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
6467                                 const char *property, const bool value)
6468 {
6469         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6470         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6471         char *val = NULL;
6472         GSList *link = NULL;
6473         int prop = -1;
6474         prop = _pminfo_appinfo_convert_to_prop_bool(property);
6475         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
6476                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
6477                 _LOGE("Invalid Boolean Property\n");
6478                 return PMINFO_R_EINVAL;
6479         }
6480         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6481         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6482         if (node == NULL) {
6483                 _LOGE("Out of Memory!!!\n");
6484                 return PMINFO_R_ERROR;
6485         }
6486         if (value)
6487                 val = strndup("('true','True')", 15);
6488         else
6489                 val = strndup("('false','False')", 17);
6490         if (val == NULL) {
6491                 _LOGE("Out of Memory\n");
6492                 free(node);
6493                 node = NULL;
6494                 return PMINFO_R_ERROR;
6495         }
6496         node->prop = prop;
6497         node->value = val;
6498         /*If API is called multiple times for same property, we should override the previous values.
6499         Last value set will be used for filtering.*/
6500         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6501         if (link)
6502                 filter->list = g_slist_delete_link(filter->list, link);
6503         filter->list = g_slist_append(filter->list, (gpointer)node);
6504         return PMINFO_R_OK;
6505
6506 }
6507
6508 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
6509                                 const char *property, const char *value)
6510 {
6511         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6512         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6513         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6514         char *val = NULL;
6515         pkgmgrinfo_node_x *ptr = NULL;
6516         char prev[PKG_STRING_LEN_MAX] = {'\0'};
6517         char temp[PKG_STRING_LEN_MAX] = {'\0'};
6518         GSList *link = NULL;
6519         int prop = -1;
6520         prop = _pminfo_appinfo_convert_to_prop_str(property);
6521         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
6522                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
6523                 _LOGE("Invalid String Property\n");
6524                 return PMINFO_R_EINVAL;
6525         }
6526         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6527         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6528         if (node == NULL) {
6529                 _LOGE("Out of Memory!!!\n");
6530                 return PMINFO_R_ERROR;
6531         }
6532         node->prop = prop;
6533         switch (prop) {
6534         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
6535                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
6536                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
6537                 else
6538                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
6539                 node->value = val;
6540                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6541                 if (link)
6542                         filter->list = g_slist_delete_link(filter->list, link);
6543                 filter->list = g_slist_append(filter->list, (gpointer)node);
6544                 break;
6545         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
6546         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
6547         case E_PMINFO_APPINFO_PROP_APP_URI:
6548         case E_PMINFO_APPINFO_PROP_APP_MIME:
6549                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
6550                 if (val == NULL) {
6551                         _LOGE("Out of Memory\n");
6552                         free(node);
6553                         node = NULL;
6554                         return PMINFO_R_ERROR;
6555                 }
6556                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6557                 if (link) {
6558                         ptr = (pkgmgrinfo_node_x *)link->data;
6559                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
6560                         _LOGE("Previous value is %s\n", prev);
6561                         filter->list = g_slist_delete_link(filter->list, link);
6562                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
6563                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6564                         _LOGE("New value is %s\n", val);
6565                         node->value = val;
6566                         filter->list = g_slist_append(filter->list, (gpointer)node);
6567                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6568                 } else {
6569                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
6570                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
6571                         _LOGE("First value is %s\n", val);
6572                         node->value = val;
6573                         filter->list = g_slist_append(filter->list, (gpointer)node);
6574                         memset(temp, '\0', PKG_STRING_LEN_MAX);
6575                 }
6576                 break;
6577         default:
6578                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
6579                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
6580                 if (link)
6581                         filter->list = g_slist_delete_link(filter->list, link);
6582                 filter->list = g_slist_append(filter->list, (gpointer)node);
6583                 break;
6584         }
6585         return PMINFO_R_OK;
6586 }
6587
6588 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
6589 {
6590         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6591         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6592         char *syslocale = NULL;
6593         char *locale = NULL;
6594         char *condition = NULL;
6595         char *error_message = NULL;
6596         char query[MAX_QUERY_LEN] = {'\0'};
6597         char where[MAX_QUERY_LEN] = {'\0'};
6598         GSList *list;
6599         int ret = 0;
6600
6601         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6602         /*Get current locale*/
6603         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6604         if (syslocale == NULL) {
6605                 _LOGE("current locale is NULL\n");
6606                 return PMINFO_R_ERROR;
6607         }
6608         locale = __convert_system_locale_to_manifest_locale(syslocale);
6609         if (locale == NULL) {
6610                 _LOGE("manifest locale is NULL\n");
6611                 free(syslocale);
6612                 return PMINFO_R_ERROR;
6613         }
6614
6615         ret = __open_manifest_db(uid);
6616         if (ret == -1) {
6617                 _LOGE("Fail to open manifest DB\n");
6618                 free(syslocale);
6619                 free(locale);
6620                 return PMINFO_R_ERROR;
6621         }
6622
6623         /*Start constructing query*/
6624         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
6625
6626         /*Get where clause*/
6627         for (list = filter->list; list; list = g_slist_next(list)) {
6628                 __get_filter_condition(list->data, &condition);
6629                 if (condition) {
6630                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6631                         where[sizeof(where) - 1] = '\0';
6632                         free(condition);
6633                         condition = NULL;
6634                 }
6635                 if (g_slist_next(list)) {
6636                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6637                         where[sizeof(where) - 1] = '\0';
6638                 }
6639         }
6640         _LOGE("where = %s\n", where);
6641         if (strlen(where) > 0) {
6642                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6643                 query[sizeof(query) - 1] = '\0';
6644         }
6645         _LOGE("query = %s\n", query);
6646
6647         /*Execute Query*/
6648         if (SQLITE_OK !=
6649             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
6650                 _LOGE("Don't execute query = %s error message = %s\n", query,
6651                        error_message);
6652                 sqlite3_free(error_message);
6653                 sqlite3_close(manifest_db);
6654                 ret = PMINFO_R_ERROR;
6655                 *count = 0;
6656                 goto err;
6657         }
6658         ret = PMINFO_R_OK;
6659 err:
6660         if (locale) {
6661                 free(locale);
6662                 locale = NULL;
6663         }
6664         if (syslocale) {
6665                 free(syslocale);
6666                 syslocale = NULL;
6667         }
6668         sqlite3_close(manifest_db);
6669         return ret;
6670 }
6671
6672 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
6673 {
6674         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
6675 }
6676
6677 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
6678                                 pkgmgrinfo_app_list_cb app_cb, void * user_data, uid_t uid)
6679 {
6680         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6681         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
6682         char *syslocale = NULL;
6683         char *locale = NULL;
6684         char *condition = NULL;
6685         char *error_message = NULL;
6686         char query[MAX_QUERY_LEN] = {'\0'};
6687         char where[MAX_QUERY_LEN] = {'\0'};
6688         GSList *list;
6689         int ret = 0;
6690         uiapplication_x *ptr1 = NULL;
6691         serviceapplication_x *ptr2 = NULL;
6692         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6693         /*Get current locale*/
6694         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6695         if (syslocale == NULL) {
6696                 _LOGE("current locale is NULL\n");
6697                 return PMINFO_R_ERROR;
6698         }
6699         locale = __convert_system_locale_to_manifest_locale(syslocale);
6700         if (locale == NULL) {
6701                 _LOGE("manifest locale is NULL\n");
6702                 free(syslocale);
6703                 return PMINFO_R_ERROR;
6704         }
6705
6706         ret = __open_manifest_db(uid);
6707         if (ret == -1) {
6708                 _LOGE("Fail to open manifest DB\n");
6709                 free(syslocale);
6710                 free(locale);
6711                 return PMINFO_R_ERROR;
6712         }
6713         /*Start constructing query*/
6714         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_APP, locale);
6715         /*Get where clause*/
6716         for (list = filter->list; list; list = g_slist_next(list)) {
6717                 __get_filter_condition(list->data, &condition);
6718                 if (condition) {
6719                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6720                         where[sizeof(where) - 1] = '\0';
6721                         free(condition);
6722                         condition = NULL;
6723                 }
6724                 if (g_slist_next(list)) {
6725                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
6726                         where[sizeof(where) - 1] = '\0';
6727                 }
6728         }
6729         _LOGE("where = %s\n", where);
6730         if (strlen(where) > 0) {
6731                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6732                 query[sizeof(query) - 1] = '\0';
6733         }
6734         _LOGE("query = %s\n", query);
6735         /*To get filtered list*/
6736         pkgmgr_pkginfo_x *info = NULL;
6737         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6738         if (info == NULL) {
6739                 _LOGE("Out of Memory!!!\n");
6740                 ret = PMINFO_R_ERROR;
6741                 goto err;
6742         }
6743         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6744         if (info->manifest_info == NULL) {
6745                 _LOGE("Out of Memory!!!\n");
6746                 ret = PMINFO_R_ERROR;
6747                 goto err;
6748         }
6749         /*To get detail app info for each member of filtered list*/
6750         pkgmgr_pkginfo_x *filtinfo = NULL;
6751         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6752         if (filtinfo == NULL) {
6753                 _LOGE("Out of Memory!!!\n");
6754                 ret = PMINFO_R_ERROR;
6755                 goto err;
6756         }
6757         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6758         if (filtinfo->manifest_info == NULL) {
6759                 _LOGE("Out of Memory!!!\n");
6760                 ret = PMINFO_R_ERROR;
6761                 goto err;
6762         }
6763         pkgmgr_appinfo_x *appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
6764         if (appinfo == NULL) {
6765                 _LOGE("Out of Memory!!!\n");
6766                 ret = PMINFO_R_ERROR;
6767                 goto err;
6768         }
6769         if (SQLITE_OK !=
6770             sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message)) {
6771                 _LOGE("Don't execute query = %s error message = %s\n", query,
6772                        error_message);
6773                 sqlite3_free(error_message);
6774                 sqlite3_close(manifest_db);
6775                 ret = PMINFO_R_ERROR;
6776                 goto err;
6777         }
6778         memset(query, '\0', MAX_QUERY_LEN);
6779         if (info->manifest_info->uiapplication) {
6780                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
6781                 info->manifest_info->uiapplication = ptr1;
6782         }
6783         if (info->manifest_info->serviceapplication) {
6784                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
6785                 info->manifest_info->serviceapplication = ptr2;
6786         }
6787         /*Filtered UI Apps*/
6788         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
6789         {
6790                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6791                                                         ptr1->appid, "uiapp");
6792                 if (SQLITE_OK !=
6793                 sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)filtinfo, &error_message)) {
6794                         _LOGE("Don't execute query = %s error message = %s\n", query,
6795                                error_message);
6796                         sqlite3_free(error_message);
6797                         sqlite3_close(manifest_db);
6798                         ret = PMINFO_R_ERROR;
6799                         goto err;
6800                 }
6801         }
6802         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
6803         {
6804                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
6805                                                         ptr2->appid, "svcapp");
6806                 if (SQLITE_OK !=
6807                 sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)filtinfo, &error_message)) {
6808                         _LOGE("Don't execute query = %s error message = %s\n", query,
6809                                error_message);
6810                         sqlite3_free(error_message);
6811                         sqlite3_close(manifest_db);
6812                         ret = PMINFO_R_ERROR;
6813                         goto err;
6814                 }
6815         }
6816         if (filtinfo->manifest_info->uiapplication) {
6817                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
6818                 filtinfo->manifest_info->uiapplication = ptr1;
6819         }
6820         /*If the callback func return < 0 we break and no more call back is called*/
6821         while(ptr1 != NULL)
6822         {
6823                 appinfo->locale = strdup(locale);
6824                 appinfo->uiapp_info = ptr1;
6825                 appinfo->app_component = PMINFO_UI_APP;
6826                 ret = app_cb((void *)appinfo, user_data);
6827                 if (ret < 0)
6828                         break;
6829                 ptr1 = ptr1->next;
6830         }
6831         /*Filtered Service Apps*/
6832         if (filtinfo->manifest_info->serviceapplication) {
6833                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
6834                 filtinfo->manifest_info->serviceapplication = ptr2;
6835         }
6836         /*If the callback func return < 0 we break and no more call back is called*/
6837         while(ptr2 != NULL)
6838         {
6839                 appinfo->locale = strdup(locale);
6840                 appinfo->svcapp_info = ptr2;
6841                 appinfo->app_component = PMINFO_SVC_APP;
6842                 ret = app_cb((void *)appinfo, user_data);
6843                 if (ret < 0)
6844                         break;
6845                 ptr2 = ptr2->next;
6846         }
6847         ret = PMINFO_R_OK;
6848 err:
6849         if (locale) {
6850                 free(locale);
6851                 locale = NULL;
6852         }
6853         if (syslocale) {
6854                 free(syslocale);
6855                 syslocale = NULL;
6856         }
6857         sqlite3_close(manifest_db);
6858         if (appinfo) {
6859                 free(appinfo);
6860                 appinfo = NULL;
6861         }
6862         __cleanup_pkginfo(info);
6863         __cleanup_pkginfo(filtinfo);
6864         return ret;
6865 }
6866
6867 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
6868                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
6869 {
6870         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
6871 }
6872
6873 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
6874 {
6875         return (pkgmgrinfo_pkginfo_filter_create(handle));
6876 }
6877
6878 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
6879 {
6880         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
6881 }
6882
6883 API int pkgmgrinfo_appinfo_metadata_filter_add(pkgmgrinfo_appinfo_metadata_filter_h handle,
6884                 const char *key, const char *value)
6885 {
6886         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
6887         retvm_if(key == NULL, PMINFO_R_EINVAL, "metadata key supplied is NULL\n");
6888         /*value can be NULL. In that case all apps with specified key should be displayed*/
6889         int ret = 0;
6890         char *k = NULL;
6891         char *v = NULL;
6892         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6893         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
6894         retvm_if(node == NULL, PMINFO_R_ERROR, "Out of Memory!!!\n");
6895         k = strdup(key);
6896         tryvm_if(k == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6897         node->key = k;
6898         if (value) {
6899                 v = strdup(value);
6900                 tryvm_if(v == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6901         }
6902         node->value = v;
6903         /*If API is called multiple times, we should OR all conditions.*/
6904         filter->list = g_slist_append(filter->list, (gpointer)node);
6905         /*All memory will be freed in destroy API*/
6906         return PMINFO_R_OK;
6907 catch:
6908         if (node) {
6909                 if (node->key) {
6910                         free(node->key);
6911                         node->key = NULL;
6912                 }
6913                 if (node->value) {
6914                         free(node->value);
6915                         node->value = NULL;
6916                 }
6917                 free(node);
6918                 node = NULL;
6919         }
6920         return ret;
6921 }
6922
6923 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
6924                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
6925 {
6926         retvm_if(handle == NULL, PMINFO_R_EINVAL, "filter handle is NULL\n");
6927         retvm_if(app_cb == NULL, PMINFO_R_EINVAL, "Callback function supplied is NULL\n");
6928         char *syslocale = NULL;
6929         char *locale = NULL;
6930         char *condition = NULL;
6931         char *error_message = NULL;
6932         char query[MAX_QUERY_LEN] = {'\0'};
6933         char where[MAX_QUERY_LEN] = {'\0'};
6934         GSList *list;
6935         int ret = 0;
6936         pkgmgr_pkginfo_x *info = NULL;
6937         pkgmgr_pkginfo_x *filtinfo = NULL;
6938         pkgmgr_appinfo_x *appinfo = NULL;
6939         uiapplication_x *ptr1 = NULL;
6940         serviceapplication_x *ptr2 = NULL;
6941         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
6942
6943         /*Get current locale*/
6944         syslocale = vconf_get_str(VCONFKEY_LANGSET);
6945         retvm_if(syslocale == NULL, PMINFO_R_ERROR, "current locale is NULL\n");
6946         locale = __convert_system_locale_to_manifest_locale(syslocale);
6947         tryvm_if(locale == NULL, ret = PMINFO_R_ERROR, "manifest locale is NULL\n");
6948
6949         ret = __open_manifest_db(uid);
6950         if (ret == -1) {
6951                 _LOGE("Fail to open manifest DB\n");
6952                 free(syslocale);
6953                 free(locale);
6954                 return PMINFO_R_ERROR;
6955         }
6956         /*Start constructing query*/
6957         memset(where, '\0', MAX_QUERY_LEN);
6958         memset(query, '\0', MAX_QUERY_LEN);
6959         snprintf(query, MAX_QUERY_LEN - 1, METADATA_FILTER_QUERY_SELECT_CLAUSE);
6960         /*Get where clause*/
6961         for (list = filter->list; list; list = g_slist_next(list)) {
6962                 __get_metadata_filter_condition(list->data, &condition);
6963                 if (condition) {
6964                         strncat(where, condition, sizeof(where) - strlen(where) -1);
6965                         free(condition);
6966                         condition = NULL;
6967                 }
6968                 if (g_slist_next(list)) {
6969                         strncat(where, METADATA_FILTER_QUERY_UNION_CLAUSE, sizeof(where) - strlen(where) - 1);
6970                 }
6971         }
6972         _LOGE("where = %s (%d)\n", where, strlen(where));
6973         if (strlen(where) > 0) {
6974                 strncat(query, where, sizeof(query) - strlen(query) - 1);
6975         }
6976         _LOGE("query = %s (%d)\n", query, strlen(query));
6977         /*To get filtered list*/
6978         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6979         tryvm_if(info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6980
6981         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6982         tryvm_if(info->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6983
6984         /*To get detail app info for each member of filtered list*/
6985         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
6986         tryvm_if(filtinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6987
6988         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
6989         tryvm_if(filtinfo->manifest_info == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6990
6991         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
6992         tryvm_if(appinfo == NULL, ret = PMINFO_R_ERROR, "Out of Memory!!!\n");
6993
6994         ret = sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message);
6995         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
6996         memset(query, '\0', MAX_QUERY_LEN);
6997
6998         if (info->manifest_info->uiapplication) {
6999                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
7000                 info->manifest_info->uiapplication = ptr1;
7001         }
7002         if (info->manifest_info->serviceapplication) {
7003                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
7004                 info->manifest_info->serviceapplication = ptr2;
7005         }
7006
7007         /*UI Apps*/
7008         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
7009         {
7010                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7011                                                         ptr1->appid, "uiapp");
7012                 ret = sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)filtinfo, &error_message);
7013                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7014                 memset(query, '\0', MAX_QUERY_LEN);
7015         }
7016         /*Service Apps*/
7017         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
7018         {
7019                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
7020                                                         ptr2->appid, "svcapp");
7021                 ret = sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)filtinfo, &error_message);
7022                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s error message = %s\n", query, error_message);
7023                 memset(query, '\0', MAX_QUERY_LEN);
7024         }
7025         /*Filtered UI Apps*/
7026         if (filtinfo->manifest_info->uiapplication) {
7027                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
7028                 filtinfo->manifest_info->uiapplication = ptr1;
7029         }
7030         /*If the callback func return < 0 we break and no more call back is called*/
7031         while(ptr1 != NULL)
7032         {
7033                 appinfo->locale = strdup(locale);
7034                 appinfo->uiapp_info = ptr1;
7035                 appinfo->app_component = PMINFO_UI_APP;
7036                 ret = app_cb((void *)appinfo, user_data);
7037                 if (ret < 0)
7038                         break;
7039                 ptr1 = ptr1->next;
7040         }
7041         /*Filtered Service Apps*/
7042         if (filtinfo->manifest_info->serviceapplication) {
7043                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
7044                 filtinfo->manifest_info->serviceapplication = ptr2;
7045         }
7046         /*If the callback func return < 0 we break and no more call back is called*/
7047         while(ptr2 != NULL)
7048         {
7049                 appinfo->locale = strdup(locale);
7050                 appinfo->svcapp_info = ptr2;
7051                 appinfo->app_component = PMINFO_SVC_APP;
7052                 ret = app_cb((void *)appinfo, user_data);
7053                 if (ret < 0)
7054                         break;
7055                 ptr2 = ptr2->next;
7056         }
7057         ret = PMINFO_R_OK;
7058 catch:
7059         if (locale) {
7060                 free(locale);
7061                 locale = NULL;
7062         }
7063         if (syslocale) {
7064                 free(syslocale);
7065                 syslocale = NULL;
7066         }
7067         sqlite3_free(error_message);
7068         sqlite3_close(manifest_db);
7069         if (appinfo) {
7070                 free(appinfo);
7071                 appinfo = NULL;
7072         }
7073         __cleanup_pkginfo(info);
7074         __cleanup_pkginfo(filtinfo);
7075         return ret;
7076 }
7077
7078 API int pkgmgrinfo_appinfo_metadata_filter_foreach(pkgmgrinfo_appinfo_metadata_filter_h handle,
7079                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
7080 {
7081         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, GLOBAL_USER);
7082 }
7083
7084 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
7085 {
7086         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7087         pkgmgr_certinfo_x *certinfo = NULL;
7088         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
7089         *handle = NULL;
7090         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7091         *handle = (void *)certinfo;
7092         return PMINFO_R_OK;
7093 }
7094
7095 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle)
7096 {
7097         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7098         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7099         pkgmgr_certinfo_x *certinfo = NULL;
7100         char *error_message = NULL;
7101         int ret = PMINFO_R_OK;
7102         char query[MAX_QUERY_LEN] = {'\0'};
7103         int exist = 0;
7104         int i = 0;
7105
7106         /*Open db.*/
7107         ret = db_util_open_with_options(getUserPkgCertDBPath(), &cert_db,
7108                                         SQLITE_OPEN_READWRITE, NULL);
7109         if (ret != SQLITE_OK) {
7110                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPath());
7111                 return PMINFO_R_ERROR;
7112         }
7113         _check_create_Cert_db(cert_db);
7114         /*validate pkgid*/
7115         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7116         if (SQLITE_OK !=
7117             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
7118                 _LOGE("Don't execute query = %s error message = %s\n", query,
7119                        error_message);
7120                 sqlite3_free(error_message);
7121                 ret = PMINFO_R_ERROR;
7122                 goto err;
7123         }
7124         if (exist == 0) {
7125                 _LOGE("Package not found in DB\n");
7126                 ret = PMINFO_R_ERROR;
7127                 goto err;
7128         }
7129         certinfo = (pkgmgr_certinfo_x *)handle;
7130         /*populate certinfo from DB*/
7131         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
7132         ret = __exec_certinfo_query(query, (void *)certinfo);
7133         if (ret == -1) {
7134                 _LOGE("Package Cert Info DB Information retrieval failed\n");
7135                 ret = PMINFO_R_ERROR;
7136                 goto err;
7137         }
7138         for (i = 0; i < MAX_CERT_TYPE; i++) {
7139                 memset(query, '\0', MAX_QUERY_LEN);
7140                 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
7141                 ret = __exec_certinfo_query(query, (void *)certinfo);
7142                 if (ret == -1) {
7143                         _LOGE("Cert Info DB Information retrieval failed\n");
7144                         ret = PMINFO_R_ERROR;
7145                         goto err;
7146                 }
7147                 if (certinfo->cert_value) {
7148                         (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
7149                         free(certinfo->cert_value);
7150                         certinfo->cert_value = NULL;
7151                 }
7152         }
7153 err:
7154         sqlite3_close(cert_db);
7155         return ret;
7156 }
7157
7158 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
7159 {
7160         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7161         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7162         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7163         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7164         pkgmgr_certinfo_x *certinfo = NULL;
7165         certinfo = (pkgmgr_certinfo_x *)handle;
7166         if ((certinfo->cert_info)[cert_type])
7167                 *cert_value = (certinfo->cert_info)[cert_type];
7168         else
7169                 *cert_value = NULL;
7170         return PMINFO_R_OK;
7171 }
7172
7173 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
7174 {
7175         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7176         int i = 0;
7177         pkgmgr_certinfo_x *certinfo = NULL;
7178         certinfo = (pkgmgr_certinfo_x *)handle;
7179         if (certinfo->pkgid) {
7180                 free(certinfo->pkgid);
7181                 certinfo->pkgid = NULL;
7182         }
7183         for (i = 0; i < MAX_CERT_TYPE; i++) {
7184                 if ((certinfo->cert_info)[i]) {
7185                         free((certinfo->cert_info)[i]);
7186                         (certinfo->cert_info)[i] = NULL;
7187                 }
7188         }
7189         free(certinfo);
7190         certinfo = NULL;
7191         return PMINFO_R_OK;
7192 }
7193
7194 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
7195 {
7196         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7197         pkgmgr_instcertinfo_x *certinfo = NULL;
7198         *handle = NULL;
7199         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
7200         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
7201         *handle = (void *)certinfo;
7202         return PMINFO_R_OK;
7203 }
7204
7205 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
7206 {
7207         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7208         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7209         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7210         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
7211         pkgmgr_instcertinfo_x *certinfo = NULL;
7212         certinfo = (pkgmgr_instcertinfo_x *)handle;
7213         (certinfo->cert_info)[cert_type] = strdup(cert_value);
7214         return PMINFO_R_OK;
7215 }
7216
7217 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle)
7218 {
7219         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
7220         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
7221         char *error_message = NULL;
7222         char query[MAX_QUERY_LEN] = {'\0'};
7223         char *vquery = NULL;
7224         int len = 0;
7225         int i = 0;
7226         int j = 0;
7227         int c = 0;
7228         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
7229         int newid = 0;
7230         int is_new = 0;
7231         int exist = -1;
7232         int ret = -1;
7233         int maxid = 0;
7234         int flag = 0;
7235         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
7236         pkgmgr_certindexinfo_x *indexinfo = NULL;
7237         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
7238         if (indexinfo == NULL) {
7239                 _LOGE("Out of Memory!!!");
7240                 return PMINFO_R_ERROR;
7241         }
7242         info->pkgid = strdup(pkgid);
7243
7244         /*Open db.*/
7245         ret = db_util_open_with_options(getUserPkgCertDBPath(), &cert_db,
7246                                         SQLITE_OPEN_READWRITE, NULL);
7247         if (ret != SQLITE_OK) {
7248                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPath());
7249                 ret = PMINFO_R_ERROR;
7250                 goto err;
7251         }
7252         _check_create_Cert_db(cert_db);
7253         /*Begin Transaction*/
7254         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7255         if (ret != SQLITE_OK) {
7256                 _LOGE("Failed to begin transaction\n");
7257                 ret = PMINFO_R_ERROR;
7258                 goto err;
7259         }
7260         _LOGE("Transaction Begin\n");
7261         /*Check if request is to insert/update*/
7262         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
7263         if (SQLITE_OK !=
7264             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
7265                 _LOGE("Don't execute query = %s error message = %s\n", query,
7266                        error_message);
7267                 sqlite3_free(error_message);
7268                 ret = PMINFO_R_ERROR;
7269                 goto err;
7270         }
7271         if (exist) {
7272                 /*Update request.
7273                 We cant just issue update query directly. We need to manage index table also.
7274                 Hence it is better to delete and insert again in case of update*/
7275                 ret = __delete_certinfo(pkgid);
7276                 if (ret < 0)
7277                         _LOGE("Certificate Deletion Failed\n");
7278         }
7279         for (i = 0; i < MAX_CERT_TYPE; i++) {
7280                 if ((info->cert_info)[i]) {
7281                         for (j = 0; j < i; j++) {
7282                                 if ( (info->cert_info)[j]) {
7283                                         if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
7284                                                 (info->cert_id)[i] = (info->cert_id)[j];
7285                                                 (info->is_new)[i] = 0;
7286                                                 (info->ref_count)[i] = (info->ref_count)[j];
7287                                                 break;
7288                                         }
7289                                 }
7290                         }
7291                         if (j < i)
7292                                 continue;
7293                         memset(query, '\0', MAX_QUERY_LEN);
7294                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info " \
7295                                 "where cert_info='%s'",(info->cert_info)[i]);
7296                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
7297                         if (ret == -1) {
7298                                 _LOGE("Cert Info DB Information retrieval failed\n");
7299                                 ret = PMINFO_R_ERROR;
7300                                 goto err;
7301                         }
7302                         if (indexinfo->cert_id == 0) {
7303                                 /*New certificate. Get newid*/
7304                                 memset(query, '\0', MAX_QUERY_LEN);
7305                                 snprintf(query, MAX_QUERY_LEN, "select MAX(cert_id) from package_cert_index_info ");
7306                                 if (SQLITE_OK !=
7307                                     sqlite3_exec(cert_db, query, __maxid_cb, (void *)&newid, &error_message)) {
7308                                         _LOGE("Don't execute query = %s error message = %s\n", query,
7309                                                error_message);
7310                                         sqlite3_free(error_message);
7311                                         ret = PMINFO_R_ERROR;
7312                                         goto err;
7313                                 }
7314                                 newid = newid + 1;
7315                                 if (flag == 0) {
7316                                         maxid = newid;
7317                                         flag = 1;
7318                                 }
7319                                 indexinfo->cert_id = maxid;
7320                                 indexinfo->cert_ref_count = 1;
7321                                 is_new = 1;
7322                                 maxid = maxid + 1;
7323                         }
7324                         (info->cert_id)[i] = indexinfo->cert_id;
7325                         (info->is_new)[i] = is_new;
7326                         (info->ref_count)[i] = indexinfo->cert_ref_count;
7327                         _LOGE("Id:Count = %d %d\n", indexinfo->cert_id, indexinfo->cert_ref_count);
7328                         indexinfo->cert_id = 0;
7329                         indexinfo->cert_ref_count = 0;
7330                         is_new = 0;
7331                 }
7332         }
7333         len = MAX_QUERY_LEN;
7334         for (i = 0; i < MAX_CERT_TYPE; i++) {
7335                 if ((info->cert_info)[i])
7336                         len+= strlen((info->cert_info)[i]);
7337         }
7338         vquery = (char *)calloc(1, len);
7339         /*insert*/
7340         snprintf(vquery, len,
7341                  "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
7342                 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
7343                 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
7344                  info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
7345                 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
7346                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
7347                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
7348                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
7349         if (SQLITE_OK !=
7350             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
7351                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7352                        error_message);
7353                 sqlite3_free(error_message);
7354                 ret = PMINFO_R_ERROR;
7355                 goto err;
7356         }
7357         /*Update index table info*/
7358         /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
7359         for (i = 0; i < MAX_CERT_TYPE; i++) {
7360                 if ((info->cert_info)[i]) {
7361                         memset(vquery, '\0', len);
7362                         if ((info->is_new)[i]) {
7363                                 snprintf(vquery, len, "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
7364                                 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
7365                                 unique_id[c++] = (info->cert_id)[i];
7366                         } else {
7367                                 /*Update*/
7368                                 for (j = 0; j < MAX_CERT_TYPE; j++) {
7369                                         if ((info->cert_id)[i] == unique_id[j]) {
7370                                                 /*Ref count has already been increased. Just continue*/
7371                                                 break;
7372                                         }
7373                                 }
7374                                 if (j == MAX_CERT_TYPE)
7375                                         unique_id[c++] = (info->cert_id)[i];
7376                                 else
7377                                         continue;
7378                                 snprintf(vquery, len, "update package_cert_index_info set cert_ref_count=%d " \
7379                                 "where cert_id=%d",  (info->ref_count)[i] + 1, (info->cert_id)[i]);
7380                         }
7381                         if (SQLITE_OK !=
7382                             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
7383                                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
7384                                        error_message);
7385                                 sqlite3_free(error_message);
7386                                 ret = PMINFO_R_ERROR;
7387                                 goto err;
7388                         }
7389                 }
7390         }
7391         /*Commit transaction*/
7392         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
7393         if (ret != SQLITE_OK) {
7394                 _LOGE("Failed to commit transaction, Rollback now\n");
7395                 sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
7396                 ret = PMINFO_R_ERROR;
7397                 goto err;
7398         }
7399         _LOGE("Transaction Commit and End\n");
7400         ret =  PMINFO_R_OK;
7401 err:
7402         sqlite3_close(cert_db);
7403         if (vquery) {
7404                 free(vquery);
7405                 vquery = NULL;
7406         }
7407         if (indexinfo) {
7408                 free(indexinfo);
7409                 indexinfo = NULL;
7410         }
7411         return ret;
7412 }
7413
7414 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
7415 {
7416         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7417         int i = 0;
7418         pkgmgr_instcertinfo_x *certinfo = NULL;
7419         certinfo = (pkgmgr_instcertinfo_x *)handle;
7420         if (certinfo->pkgid) {
7421                 free(certinfo->pkgid);
7422                 certinfo->pkgid = NULL;
7423         }
7424         for (i = 0; i < MAX_CERT_TYPE; i++) {
7425                 if ((certinfo->cert_info)[i]) {
7426                         free((certinfo->cert_info)[i]);
7427                         (certinfo->cert_info)[i] = NULL;
7428                 }
7429         }
7430         free(certinfo);
7431         certinfo = NULL;
7432         return PMINFO_R_OK;
7433 }
7434
7435 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
7436 {
7437         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7438         int ret = -1;
7439         /*Open db.*/
7440         ret = db_util_open_with_options(getUserPkgCertDBPathUID(uid), &cert_db,
7441                                         SQLITE_OPEN_READWRITE, NULL);
7442         if (ret != SQLITE_OK) {
7443                 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
7444                 ret = PMINFO_R_ERROR;
7445                 goto err;
7446         }
7447         _check_create_Cert_db(cert_db);
7448         /*Begin Transaction*/
7449         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7450         if (ret != SQLITE_OK) {
7451                 _LOGE("Failed to begin transaction\n");
7452                 ret = PMINFO_R_ERROR;
7453                 goto err;
7454         }
7455         _LOGE("Transaction Begin\n");
7456         ret = __delete_certinfo(pkgid);
7457         if (ret < 0) {
7458                 _LOGE("Certificate Deletion Failed\n");
7459         } else {
7460                 _LOGE("Certificate Deletion Success\n");
7461         }
7462         /*Commit transaction*/
7463         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
7464         if (ret != SQLITE_OK) {
7465                 _LOGE("Failed to commit transaction, Rollback now\n");
7466                 sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
7467                 ret = PMINFO_R_ERROR;
7468                 goto err;
7469         }
7470         _LOGE("Transaction Commit and End\n");
7471         ret = PMINFO_R_OK;
7472 err:
7473         sqlite3_close(cert_db);
7474         return ret;
7475 }
7476
7477
7478 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
7479 {
7480         return pkgmgrinfo_delete_usr_certinfo(pkgid, GLOBAL_USER);
7481 }
7482
7483 API int pkgmgrinfo_create_pkgusrdbinfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkgdbinfo_h *handle)
7484 {
7485         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7486         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7487
7488         char *manifest = NULL;
7489         manifest_x *mfx = NULL;
7490         *handle = NULL;
7491         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
7492         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7493
7494         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
7495         if (manifest) {
7496                 free(manifest);
7497                 manifest = NULL;
7498         }
7499         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7500
7501         *handle = (void *)mfx;
7502
7503         return PMINFO_R_OK;
7504 }
7505
7506 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
7507 {
7508         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
7509         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7510
7511         char *manifest = NULL;
7512         manifest_x *mfx = NULL;
7513         *handle = NULL;
7514         manifest = pkgmgr_parser_get_manifest_file(pkgid);
7515         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
7516
7517         mfx = pkgmgr_parser_process_manifest_xml(manifest);
7518         if (manifest) {
7519                 free(manifest);
7520                 manifest = NULL;
7521         }
7522         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
7523
7524         *handle = (void *)mfx;
7525
7526         return PMINFO_R_OK;
7527 }
7528
7529 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
7530 {
7531         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
7532         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7533
7534         int len = strlen(type);
7535         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7536
7537         manifest_x *mfx = (manifest_x *)handle;
7538
7539         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
7540         return PMINFO_R_OK;
7541 }
7542
7543 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
7544 {
7545         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
7546         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7547
7548         int len = strlen(version);
7549         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7550
7551         manifest_x *mfx = (manifest_x *)handle;
7552
7553         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
7554         return PMINFO_R_OK;
7555 }
7556
7557 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7558 {
7559         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7560         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7561
7562         manifest_x *mfx = (manifest_x *)handle;
7563
7564         if (location == INSTALL_INTERNAL)
7565                 strcpy(mfx->installlocation, "internal-only");
7566         else if (location == INSTALL_EXTERNAL)
7567                 strcpy(mfx->installlocation, "prefer-external");
7568
7569         return PMINFO_R_OK;
7570 }
7571
7572 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
7573 {
7574         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7575         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7576
7577         manifest_x *mfx = (manifest_x *)handle;
7578
7579         mfx->package_size = strdup(size);
7580
7581         return PMINFO_R_OK;
7582 }
7583
7584 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
7585 {
7586         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7587         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7588
7589         int len = strlen(label_txt);
7590         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7591
7592         manifest_x *mfx = (manifest_x *)handle;
7593
7594         label_x *label = calloc(1, sizeof(label_x));
7595         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7596
7597         LISTADD(mfx->label, label);
7598         if (locale)
7599                 mfx->label->lang = strdup(locale);
7600         else
7601                 mfx->label->lang = strdup(DEFAULT_LOCALE);
7602         mfx->label->text = strdup(label_txt);
7603
7604         return PMINFO_R_OK;
7605 }
7606
7607 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
7608 {
7609         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7610         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7611
7612         int len = strlen(icon_txt);
7613         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7614
7615         manifest_x *mfx = (manifest_x *)handle;
7616
7617         icon_x *icon = calloc(1, sizeof(icon_x));
7618         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7619
7620         LISTADD(mfx->icon, icon);
7621         if (locale)
7622                 mfx->icon->lang = strdup(locale);
7623         else
7624                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
7625         mfx->icon->text = strdup(icon_txt);
7626
7627         return PMINFO_R_OK;
7628 }
7629
7630 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
7631 {
7632         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7633         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
7634
7635         int len = strlen(desc_txt);
7636         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
7637
7638         manifest_x *mfx = (manifest_x *)handle;
7639
7640         description_x *description = calloc(1, sizeof(description_x));
7641         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
7642
7643         LISTADD(mfx->description, description);
7644         if (locale)
7645                 mfx->description->lang = strdup(locale);
7646         else
7647                 mfx->description->lang = strdup(DEFAULT_LOCALE);
7648         mfx->description->text = strdup(desc_txt);
7649
7650         return PMINFO_R_OK;
7651 }
7652
7653 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
7654                 const char *author_email, const char *author_href, const char *locale)
7655 {
7656         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7657         manifest_x *mfx = (manifest_x *)handle;
7658         author_x *author = calloc(1, sizeof(author_x));
7659         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
7660
7661         LISTADD(mfx->author, author);
7662         if (author_name)
7663                 mfx->author->text = strdup(author_name);
7664         if (author_email)
7665                 mfx->author->email = strdup(author_email);
7666         if (author_href)
7667                 mfx->author->href = strdup(author_href);
7668         if (locale)
7669                 mfx->author->lang = strdup(locale);
7670         else
7671                 mfx->author->lang = strdup(DEFAULT_LOCALE);
7672         return PMINFO_R_OK;
7673 }
7674
7675 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
7676 {
7677         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7678         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7679
7680         manifest_x *mfx = (manifest_x *)handle;
7681
7682         if (removable == 0)
7683                 strcpy(mfx->removable, "false");
7684         else if (removable == 1)
7685                 strcpy(mfx->removable, "true");
7686
7687         return PMINFO_R_OK;
7688 }
7689
7690 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
7691 {
7692         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7693         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7694
7695         manifest_x *mfx = (manifest_x *)handle;
7696
7697         if (preload == 0)
7698                 strcpy(mfx->preload, "false");
7699         else if (preload == 1)
7700                 strcpy(mfx->preload, "true");
7701
7702         return PMINFO_R_OK;
7703 }
7704
7705 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
7706 {
7707         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7708         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
7709
7710         manifest_x *mfx = (manifest_x *)handle;
7711
7712         if (location == INSTALL_INTERNAL)
7713                 strcpy(mfx->installed_storage, "installed_internal");
7714         else if (location == INSTALL_EXTERNAL)
7715                 strcpy(mfx->installed_storage, "installed_external");
7716
7717         return PMINFO_R_OK;
7718 }
7719
7720 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7721 {
7722         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7723
7724         int ret = 0;
7725         manifest_x *mfx = NULL;
7726         mfx = (manifest_x *)handle;
7727
7728         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
7729         if (ret == 0) {
7730                 _LOGE("Successfully stored info in DB\n");
7731                 return PMINFO_R_OK;
7732         } else {
7733                 _LOGE("Failed to store info in DB\n");
7734                 return PMINFO_R_ERROR;
7735         }
7736 }
7737
7738 API int pkgmgrinfo_save_pkgusrdbinfo(pkgmgrinfo_pkgdbinfo_h handle, uid_t uid)
7739 {
7740         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7741
7742         int ret = 0;
7743         manifest_x *mfx = NULL;
7744         mfx = (manifest_x *)handle;
7745
7746         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
7747         if (ret == 0) {
7748                 _LOGE("Successfully stored info in DB\n");
7749                 return PMINFO_R_OK;
7750         } else {
7751                 _LOGE("Failed to store info in DB\n");
7752                 return PMINFO_R_ERROR;
7753         }
7754 }
7755
7756 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
7757 {
7758         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
7759
7760         manifest_x *mfx = NULL;
7761         mfx = (manifest_x *)handle;
7762         pkgmgr_parser_free_manifest_xml(mfx);
7763         return PMINFO_R_OK;
7764 }
7765
7766 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
7767 {
7768         /* Should be implemented later */
7769         return 0;
7770 }
7771
7772 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
7773 {
7774         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
7775         int ret = -1;
7776         char query[MAX_QUERY_LEN] = {'\0'};
7777         ret = __open_manifest_db(uid);
7778
7779         /*Begin transaction*/
7780         ret = sqlite3_exec(manifest_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7781         if (ret != SQLITE_OK) {
7782                 _LOGE("Failed to begin transaction\n");
7783                 sqlite3_close(manifest_db);
7784                 return PMINFO_R_ERROR;
7785         }
7786         _LOGD("Transaction Begin\n");
7787
7788         memset(query, '\0', MAX_QUERY_LEN);
7789         snprintf(query, MAX_QUERY_LEN,
7790                 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
7791
7792         char *error_message = NULL;
7793         if (SQLITE_OK !=
7794             sqlite3_exec(manifest_db, query, NULL, NULL, &error_message)) {
7795                 _LOGE("Don't execute query = %s error message = %s\n", query,
7796                        error_message);
7797                 sqlite3_free(error_message);
7798                 return PMINFO_R_ERROR;
7799         }
7800         sqlite3_free(error_message);
7801
7802         /*Commit transaction*/
7803         ret = sqlite3_exec(manifest_db, "COMMIT", NULL, NULL, NULL);
7804         if (ret != SQLITE_OK) {
7805                 _LOGE("Failed to commit transaction. Rollback now\n");
7806                 sqlite3_exec(manifest_db, "ROLLBACK", NULL, NULL, NULL);
7807                 sqlite3_close(manifest_db);
7808                 return PMINFO_R_ERROR;
7809         }
7810         _LOGD("Transaction Commit and End\n");
7811         sqlite3_close(manifest_db);
7812
7813         return PMINFO_R_OK;
7814 }
7815
7816 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
7817 {
7818         return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
7819 }
7820
7821 API int pkgmgrinfo_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
7822 {
7823         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7824         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
7825         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7826         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7827         int ret = PMINFO_R_OK;
7828         char query[MAX_QUERY_LEN] = {'\0'};
7829         char *error_message = NULL;
7830         pkgmgr_datacontrol_x *data = NULL;
7831
7832         ret = __open_datacontrol_db();
7833         if (ret == -1) {
7834                 _LOGE("Fail to open datacontrol DB\n");
7835                 return PMINFO_R_ERROR;
7836         }
7837
7838         data = (pkgmgr_datacontrol_x *)calloc(1, sizeof(pkgmgr_datacontrol_x));
7839         if (data == NULL) {
7840                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
7841                 sqlite3_close(datacontrol_db);
7842                 return PMINFO_R_ERROR;
7843         }
7844
7845         snprintf(query, MAX_QUERY_LEN, 
7846                 "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",
7847                 providerid, type);
7848
7849         if (SQLITE_OK !=
7850                 sqlite3_exec(datacontrol_db, query, __datacontrol_cb, (void *)data, &error_message)) {
7851                 _LOGE("Don't execute query = %s error message = %s\n", query,
7852                            error_message);
7853                 sqlite3_free(error_message);
7854                 sqlite3_close(datacontrol_db);
7855                 return PMINFO_R_ERROR;
7856         }
7857
7858         *appid = (char *)data->appid;
7859         *access = (char *)data->access;
7860         free(data);
7861         sqlite3_close(datacontrol_db);
7862
7863         return PMINFO_R_OK;
7864 }
7865
7866 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
7867 {
7868         retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
7869         int ret = -1;
7870         char query[MAX_QUERY_LEN] = {'\0'};
7871         char *error_message = NULL;
7872         ret = __open_manifest_db(uid);
7873
7874
7875         /*Begin transaction*/
7876         ret = sqlite3_exec(manifest_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
7877         if (ret != SQLITE_OK) {
7878                 _LOGE("Failed to begin transaction\n");
7879                 sqlite3_close(manifest_db);
7880                 return PMINFO_R_ERROR;
7881         }
7882         _LOGD("Transaction Begin\n");
7883
7884         memset(query, '\0', MAX_QUERY_LEN);
7885         snprintf(query, MAX_QUERY_LEN,
7886                 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
7887
7888         if (SQLITE_OK !=
7889             sqlite3_exec(manifest_db, query, NULL, NULL, &error_message)) {
7890                 _LOGE("Don't execute query = %s error message = %s\n", query,
7891                        error_message);
7892                 sqlite3_free(error_message);
7893                 return PMINFO_R_ERROR;
7894         }
7895
7896         /*Commit transaction*/
7897         ret = sqlite3_exec(manifest_db, "COMMIT", NULL, NULL, NULL);
7898         if (ret != SQLITE_OK) {
7899                 _LOGE("Failed to commit transaction. Rollback now\n");
7900                 sqlite3_exec(manifest_db, "ROLLBACK", NULL, NULL, NULL);
7901                 sqlite3_close(manifest_db);
7902                 return PMINFO_R_ERROR;
7903         }
7904         _LOGD("Transaction Commit and End\n");
7905         sqlite3_close(manifest_db);
7906
7907         return PMINFO_R_OK;
7908 }
7909
7910 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
7911 {
7912         return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
7913 }
7914
7915 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
7916 {
7917         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
7918         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
7919         char *val = NULL;
7920         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
7921         val = (char *)info->uiapp_info->guestmode_visibility;
7922         if (val) {
7923                 if (strcasecmp(val, "true") == 0){
7924                         *status = 1;
7925                 }else if (strcasecmp(val, "false") == 0){
7926                         *status = 0;
7927                 }else{
7928                         *status = 1;
7929                 }
7930         }
7931         return PMINFO_R_OK;
7932 }
7933
7934 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
7935 {
7936         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
7937         char *val = NULL;
7938         int ret = 0;
7939         char *noti_string = NULL;
7940         int len = 0;
7941         char query[MAX_QUERY_LEN] = {'\0'};
7942         char *errmsg = NULL;
7943         sqlite3 *pkgmgr_parser_db;
7944
7945         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
7946         val = (char *)info->uiapp_info->guestmode_visibility;
7947         if (val ) {
7948                 ret =
7949                     db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
7950                                  SQLITE_OPEN_READWRITE, NULL);
7951
7952                 if (ret != SQLITE_OK) {
7953                         _LOGE("DB Open Failed\n");
7954                         return PMINFO_R_ERROR;
7955                 }
7956
7957                 /*TODO: Write to DB here*/
7958                 if (status == true)
7959                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->uiapp_info->appid);
7960                 else
7961                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->uiapp_info->appid);
7962
7963                 if (SQLITE_OK != sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg)) {
7964                         _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
7965                         free(errmsg);
7966                         sqlite3_close(pkgmgr_parser_db);
7967                         return PMINFO_R_ERROR;
7968                 }else{
7969                         sqlite3_close(pkgmgr_parser_db);
7970                         len = strlen((char *)info->uiapp_info->appid) + 8;
7971                         noti_string = calloc(1, len);
7972                         if (noti_string == NULL){
7973                                 return PMINFO_R_ERROR;
7974                         }
7975                         snprintf(noti_string, len, "update:%s", (char *)info->uiapp_info->appid);
7976                 vconf_set_str(VCONFKEY_AIL_INFO_STATE, noti_string);
7977                         vconf_set_str(VCONFKEY_MENUSCREEN_DESKTOP, noti_string); // duplicate, will be removed
7978                         free(noti_string);
7979                 }
7980         }
7981         return PMINFO_R_OK;
7982 }
7983
7984 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
7985 {
7986         return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);
7987 }
7988
7989 /* pkgmgrinfo client start*/
7990 API pkgmgrinfo_client *pkgmgrinfo_client_new(pkgmgrinfo_client_type ctype)
7991 {
7992         int ret = 0;
7993         char *errmsg = NULL;
7994         void *pc = NULL;
7995         void *handle = NULL;
7996         pkgmgrinfo_client *(*__pkgmgr_client_new)(pkgmgrinfo_client_type ctype) = NULL;
7997
7998         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
7999         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8000
8001         __pkgmgr_client_new = dlsym(handle, "pkgmgr_client_new");
8002         errmsg = dlerror();
8003         tryvm_if((errmsg != NULL) || (__pkgmgr_client_new == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8004
8005         pc = __pkgmgr_client_new(ctype);
8006         tryvm_if(pc == NULL, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8007
8008 catch:
8009         dlclose(handle);
8010         return (pkgmgrinfo_client *) pc;
8011 }
8012
8013 API int pkgmgrinfo_client_set_status_type(pkgmgrinfo_client *pc, int status_type)
8014 {
8015         int ret = 0;
8016         char *errmsg = NULL;
8017         void *handle = NULL;
8018         int (*__pkgmgr_client_set_status_type)(pkgmgrinfo_client *pc, int status_type) = NULL;
8019
8020         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8021         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8022
8023         __pkgmgr_client_set_status_type = dlsym(handle, "pkgmgr_client_set_status_type");
8024         errmsg = dlerror();
8025         tryvm_if((errmsg != NULL) || (__pkgmgr_client_set_status_type == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8026
8027         ret = __pkgmgr_client_set_status_type(pc, status_type);
8028         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8029
8030 catch:
8031         /*
8032          * Do not close libpkgmgr-client.so.0 to avoid munmap registered callback
8033          *
8034          * The lib dependency chain like below
8035          * amd --> pkgmgr-info -- dlopen --> libpkgmgr-client --> libpkgmgr-installer-client
8036          *
8037          * And there is a function in libpkgmgr-installer-client named _on_signal_handle_filter()
8038          * which will registered to dbus callback in amd though in fact amd doesn't direct depends
8039          * on libpkgmgr-installer-client.
8040          *
8041          * So when the dlcose happen, then libpkgmgr-installer-client been closed too since no one
8042          * link to it then.
8043          *
8044          * However, when the libdbus call into the callback function, it suddenly fond that the
8045          * function address is gone (unmapped), then we receive a SIGSEGV.
8046          *
8047          * I'm not sure why we're using dlopen/dlclose in this case, I think it's much simple and
8048          * robust if we just link to the well-known lib.
8049          *
8050          * See https://bugs.tizen.org/jira/browse/PTREL-591
8051         dlclose(handle);
8052          */
8053         return ret;
8054 }
8055
8056 API int pkgmgrinfo_client_listen_status(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data)
8057 {
8058         int ret = 0;
8059         char *errmsg = NULL;
8060         void *handle = NULL;
8061         int (*__pkgmgr_client_listen_status)(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data) = NULL;
8062
8063         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8064         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8065
8066         __pkgmgr_client_listen_status = dlsym(handle, "pkgmgr_client_listen_status");
8067         errmsg = dlerror();
8068         tryvm_if((errmsg != NULL) || (__pkgmgr_client_listen_status == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8069
8070         ret = __pkgmgr_client_listen_status(pc, event_cb, data);
8071         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8072
8073 catch:
8074         /* same as pkgmgrinfo_client_new */
8075         return ret;
8076 }
8077
8078 API int pkgmgrinfo_client_free(pkgmgrinfo_client *pc)
8079 {
8080         int ret = 0;
8081         char *errmsg = NULL;
8082         void *handle = NULL;
8083         int (*__pkgmgr_client_free)(pkgmgrinfo_client *pc) = NULL;
8084
8085         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
8086         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
8087
8088         __pkgmgr_client_free = dlsym(handle, "pkgmgr_client_free");
8089         errmsg = dlerror();
8090         tryvm_if((errmsg != NULL) || (__pkgmgr_client_free == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
8091
8092         ret = __pkgmgr_client_free(pc);
8093         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
8094
8095 catch:
8096         /* same as pkgmgrinfo_client_new */
8097         return ret;
8098 }
8099
8100 API int pkgmgrinfo_client_request_enable_external_pkg(char *pkgid)
8101 {
8102         int ret = 0;
8103         DBusConnection *bus;
8104         DBusMessage *message;
8105
8106         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
8107
8108         if(__get_pkg_location(pkgid) != PMINFO_EXTERNAL_STORAGE)
8109                 return PMINFO_R_OK;
8110
8111         bus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
8112         retvm_if(bus == NULL, PMINFO_R_EINVAL, "dbus_bus_get() failed.");
8113
8114         message = dbus_message_new_method_call (SERVICE_NAME, PATH_NAME, INTERFACE_NAME, METHOD_NAME);
8115         retvm_if(message == NULL, PMINFO_R_EINVAL, "dbus_message_new_method_call() failed.");
8116
8117         dbus_message_append_args(message, DBUS_TYPE_STRING, &pkgid, DBUS_TYPE_INVALID);
8118
8119         ret = dbus_connection_send_with_reply_and_block(bus, message, -1, NULL);
8120         retvm_if(!ret, ret = PMINFO_R_EINVAL, "connection_send dbus fail");
8121
8122         dbus_connection_flush(bus);
8123         dbus_message_unref(message);
8124
8125         return PMINFO_R_OK;
8126 }
8127
8128 /* pkgmgrinfo client end*/
8129