beb5a223803796c7928894427e037c288197978b
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_appinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <dlfcn.h>
9
10 #include <sqlite3.h>
11 #include <glib.h>
12
13 #include "pkgmgr-info.h"
14 #include "pkgmgrinfo_debug.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
17
18 static bool _get_bool_value(const char *str)
19 {
20         if (str && !strcmp(str, "true"))
21                 return true;
22         else
23                 return false;
24 }
25
26 static void __cleanup_appinfo(pkgmgr_appinfo_x *data)
27 {
28         pkgmgr_appinfo_x *info = data;
29
30         if (info != NULL) {
31                 if (info->package)
32                         free((void *)info->package);
33                 if (info->locale)
34                         free((void *)info->locale);
35
36                 pkgmgrinfo_basic_free_application(info->app_info);
37                 free((void *)info);
38         }
39         return;
40 }
41
42 static const char join_localized_info[] =
43         " LEFT OUTER JOIN package_app_localized_info"
44         "  ON ai.app_id=package_app_localized_info.app_id"
45         "  AND package_app_localized_info.app_locale=?";
46 static const char join_category[] =
47         " LEFT OUTER JOIN package_app_app_category"
48         " ON ai.app_id=package_app_app_category.app_id";
49 static const char join_app_control[] =
50         " LEFT OUTER JOIN package_app_app_control"
51         "  ON ai.app_id=package_app_app_control.app_id";
52 static const char join_metadata[] =
53         " LEFT OUTER JOIN package_app_app_metadata"
54         "  ON ai.app_id=package_app_app_metadata.app_id ";
55
56 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
57         const char *locale, uid_t uid, char **query, GList **bind_params)
58 {
59         int joined = 0;
60         size_t len = 0;
61         char *condition = NULL;
62         char buf[MAX_QUERY_LEN] = { '\0' };
63         char tmp_query[MAX_QUERY_LEN] = { '\0' };
64         GSList *list;
65
66         if (!filter)
67                 return PMINFO_R_OK;
68         strncat(buf, " WHERE 1=1", MAX_QUERY_LEN - len - 1);
69         len += strlen(" WHERE 1=1");
70
71         for (list = filter->list; list; list = list->next) {
72                 joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
73                 if (condition == NULL)
74                         continue;
75
76                 strncat(buf, " AND ", MAX_QUERY_LEN - len - 1);
77                 len += strlen(" AND ");
78
79                 strncat(buf, condition, sizeof(buf) - len - 1);
80                 len += strlen(condition);
81                 free(condition);
82                 condition = NULL;
83         }
84
85         if (filter->list_metadata) {
86                 strncat(buf, " AND (", MAX_QUERY_LEN - len - 1);
87                 len += strlen(" AND (");
88         }
89         for (list = filter->list_metadata; list; list = list->next) {
90                 joined |= __get_metadata_filter_condition(list->data,
91                                 &condition, bind_params);
92                 if (condition == NULL)
93                         continue;
94                 strncat(buf, condition, sizeof(buf) - len - 1);
95                 len += strlen(condition);
96                 free(condition);
97                 condition = NULL;
98
99                 strncat(buf, " OR ", MAX_QUERY_LEN - len - 1);
100                 len += strlen(" OR ");
101         }
102         if (filter->list_metadata) {
103                 strncat(buf, "1=0)", MAX_QUERY_LEN - len - 1);
104                 len += strlen("1=0)");
105         }
106
107         if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
108                 strncat(tmp_query, join_localized_info, MAX_QUERY_LEN - len - 1);
109                 len += strlen(join_localized_info);
110                 *bind_params = g_list_append(*bind_params, strdup(locale));
111         }
112         if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY) {
113                 strncat(tmp_query, join_category, MAX_QUERY_LEN - len - 1);
114                 len += strlen(join_category);
115         }
116         if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL) {
117                 strncat(tmp_query, join_app_control, MAX_QUERY_LEN - len - 1);
118                 len += strlen(join_app_control);
119         }
120         if (joined & E_PMINFO_APPINFO_JOIN_METADATA) {
121                 strncat(tmp_query, join_metadata, MAX_QUERY_LEN - len - 1);
122                 len += strlen(join_metadata);
123         }
124
125         strncat(tmp_query, buf, MAX_QUERY_LEN - len - 1);
126         len += strlen(buf);
127
128         *query = strdup(tmp_query);
129         if (*query == NULL)
130                 return PMINFO_R_ERROR;
131
132         return PMINFO_R_OK;
133 }
134
135 static int _appinfo_get_category(sqlite3 *db, const char *appid,
136                 GList **category)
137 {
138         static const char query_raw[] =
139                 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
140         int ret;
141         char *query;
142         sqlite3_stmt *stmt;
143         char *val;
144
145         query = sqlite3_mprintf(query_raw, appid);
146         if (query == NULL) {
147                 LOGE("out of memory");
148                 return PMINFO_R_ERROR;
149         }
150
151         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
152         sqlite3_free(query);
153         if (ret != SQLITE_OK) {
154                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
155                 return PMINFO_R_ERROR;
156         }
157
158         while (sqlite3_step(stmt) == SQLITE_ROW) {
159                 val = NULL;
160                 _save_column_str(stmt, 0, &val);
161                 if (val)
162                         *category = g_list_append(*category, (gpointer)val);
163         }
164
165         sqlite3_finalize(stmt);
166
167         return PMINFO_R_OK;
168 }
169
170 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str)
171 {
172         char *dup;
173         char *token;
174         char *ptr = NULL;
175         appcontrol_x *ac;
176
177         if (appcontrol_str == NULL)
178                 return;
179
180         dup = strdup(appcontrol_str);
181         do {
182                 ac = calloc(1, sizeof(appcontrol_x));
183                 if (ac == NULL) {
184                         _LOGE("out of memory");
185                         break;
186                 }
187                 token = strtok_r(dup, "|", &ptr);
188                 if (token && strcmp(token, "NULL"))
189                         ac->operation = strdup(token);
190                 token = strtok_r(NULL, "|", &ptr);
191                 if (token && strcmp(token, "NULL"))
192                         ac->uri = strdup(token);
193                 token = strtok_r(NULL, "|", &ptr);
194                 if (token && strcmp(token, "NULL"))
195                         ac->mime = strdup(token);
196                 *appcontrol = g_list_append(*appcontrol, ac);
197         } while ((token = strtok_r(NULL, ";", &ptr)));
198
199         free(dup);
200 }
201
202 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
203                 GList **appcontrol)
204 {
205         static const char query_raw[] =
206                 "SELECT app_control FROM package_app_app_control "
207                 "WHERE app_id=%Q";
208         int ret;
209         char *query;
210         sqlite3_stmt *stmt;
211         char *str;
212
213         query = sqlite3_mprintf(query_raw, appid);
214         if (query == NULL) {
215                 LOGE("out of memory");
216                 return PMINFO_R_ERROR;
217         }
218
219         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
220         sqlite3_free(query);
221         if (ret != SQLITE_OK) {
222                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
223                 return PMINFO_R_ERROR;
224         }
225
226         while (sqlite3_step(stmt) == SQLITE_ROW) {
227                 str = NULL;
228                 _save_column_str(stmt, 0, &str);
229                 /* TODO: revise */
230                 __parse_appcontrol(appcontrol, str);
231                 free(str);
232         }
233
234         sqlite3_finalize(stmt);
235
236         return PMINFO_R_OK;
237 }
238
239 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
240                 GList **metadata)
241 {
242         static const char query_raw[] =
243                 "SELECT md_key, md_value "
244                 "FROM package_app_app_metadata WHERE app_id=%Q";
245         int ret;
246         char *query;
247         sqlite3_stmt *stmt;
248         int idx;
249         metadata_x *info;
250
251         query = sqlite3_mprintf(query_raw, appid);
252         if (query == NULL) {
253                 LOGE("out of memory");
254                 return PMINFO_R_ERROR;
255         }
256
257         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
258         sqlite3_free(query);
259         if (ret != SQLITE_OK) {
260                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
261                 return PMINFO_R_ERROR;
262         }
263
264         while (sqlite3_step(stmt) == SQLITE_ROW) {
265                 info = calloc(1, sizeof(metadata_x));
266                 if (info == NULL) {
267                         LOGE("out of memory");
268                         sqlite3_finalize(stmt);
269                         return PMINFO_R_ERROR;
270                 }
271                 idx = 0;
272                 _save_column_str(stmt, idx++, &info->key);
273                 _save_column_str(stmt, idx++, &info->value);
274                 *metadata = g_list_append(*metadata, info);
275         }
276
277         sqlite3_finalize(stmt);
278
279         return PMINFO_R_OK;
280
281 }
282
283 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
284                 GList **splashscreens)
285 {
286         static const char query_raw[] =
287                 "SELECT src, type, orientation, indicatordisplay, operation, color_depth "
288                 "FROM package_app_splash_screen WHERE app_id=%Q";
289         int ret;
290         char *query;
291         sqlite3_stmt *stmt;
292         int idx;
293         splashscreen_x *info;
294
295         query = sqlite3_mprintf(query_raw, appid);
296         if (query == NULL) {
297                 LOGE("out of memory");
298                 return PMINFO_R_ERROR;
299         }
300
301         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
302         sqlite3_free(query);
303         if (ret != SQLITE_OK) {
304                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
305                 return PMINFO_R_ERROR;
306         }
307
308         while (sqlite3_step(stmt) == SQLITE_ROW) {
309                 info = calloc(1, sizeof(splashscreen_x));
310                 if (info == NULL) {
311                         LOGE("out of memory");
312                         sqlite3_finalize(stmt);
313                         return PMINFO_R_ERROR;
314                 }
315                 idx = 0;
316                 _save_column_str(stmt, idx++, &info->src);
317                 _save_column_str(stmt, idx++, &info->type);
318                 _save_column_str(stmt, idx++, &info->orientation);
319                 _save_column_str(stmt, idx++, &info->indicatordisplay);
320                 _save_column_str(stmt, idx++, &info->operation);
321                 _save_column_str(stmt, idx++, &info->color_depth);
322                 *splashscreens = g_list_append(*splashscreens, info);
323         }
324
325         sqlite3_finalize(stmt);
326
327         return PMINFO_R_OK;
328 }
329
330 static GList *__get_background_category(const char *value)
331 {
332         GList *category_list = NULL;
333         int convert_value = 0;
334         if (!value || strlen(value) == 0)
335                 return NULL;
336
337         convert_value = atoi(value);
338         if (convert_value < 0)
339                 return NULL;
340
341         if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
342                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
343         else
344                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
345
346         if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
347                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
348
349         if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
350                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
351
352         if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
353                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
354
355         if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
356                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
357
358         if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
359                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
360
361         if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
362                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
363
364         if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
365                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
366
367         return category_list;
368
369 }
370
371 static void __free_applications(gpointer data)
372 {
373         pkgmgrinfo_basic_free_application((application_x *)data);
374 }
375
376 static gint __disable_chk_func(gconstpointer data1, gconstpointer data2)
377 {
378         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data1;
379         pkgmgrinfo_appinfo_disable_type *value =
380                         (pkgmgrinfo_appinfo_disable_type *)data2;
381
382         if (value == E_APPINFO_DISABLE_TYPE_PKG)
383                 return (node->prop == E_PMINFO_APPINFO_PROP_PKG_DISABLE)
384                                 ? 0 : 1;
385         else
386                 return (node->prop == E_PMINFO_APPINFO_PROP_APP_DISABLE)
387                                 ? 0 : 1;
388 }
389
390 static bool __check_disable_filter_exist(pkgmgrinfo_filter_x *filter,
391                 pkgmgrinfo_appinfo_disable_type type)
392 {
393         GSList *link;
394
395         if (filter == NULL)
396                 return false;
397
398         link = g_slist_find_custom(filter->list, &type, __disable_chk_func);
399         if (link)
400                 return true;
401
402         return false;
403 }
404
405 static int __bind_params(sqlite3_stmt *stmt, GList *params)
406 {
407         GList *tmp_list = NULL;
408         int idx = 0;
409         int ret;
410
411         if (stmt == NULL || params == NULL)
412                 return PMINFO_R_EINVAL;
413
414         tmp_list = params;
415         while (tmp_list) {
416                 ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
417                 if (ret != SQLITE_OK)
418                         return PMINFO_R_ERROR;
419                 tmp_list = tmp_list->next;
420         }
421
422         return PMINFO_R_OK;
423 }
424
425 static bool __check_app_storage_status(pkgmgrinfo_filter_x *tmp_filter)
426 {
427         GSList *tmp_list = NULL;
428         pkgmgrinfo_node_x *tmp_node = NULL;
429         int property = -1;
430
431         if (tmp_filter == NULL)
432                 return true;
433
434         property = _pminfo_appinfo_convert_to_prop_bool(PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
435         for (tmp_list = tmp_filter->list; tmp_list != NULL;
436                         tmp_list = g_slist_next(tmp_list)) {
437                 tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
438                 if (property == tmp_node->prop) {
439                         if (strcmp(tmp_node->value, "true") == 0)
440                                 return true;
441                         else
442                                 return false;
443                 }
444         }
445
446         return true;
447 }
448
449 static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
450                 const char *locale, pkgmgrinfo_filter_x *filter, int flag,
451                 GHashTable *applications)
452 {
453         static const char query_raw[] =
454                 "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
455                 "ai.app_external_path";
456         static const char query_basic[] =
457                 ", ai.app_component, ai.app_exec, "
458                 "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
459                 "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
460                 "ai.app_hwacceleration, ai.app_screenreader, "
461                 "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
462                 "ai.app_indicatordisplay, ai.app_portraitimg, "
463                 "ai.app_landscapeimg, ai.app_guestmodevisibility, "
464                 "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
465                 "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
466                 "ai.app_support_disable, ai.app_process_pool, "
467                 "ai.app_background_category, ai.app_package_type, "
468                 "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
469                 "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
470                 "ai.app_zip_mount_file, ai.component_type, ai.package, "
471                 "ai.app_package_system, ai.app_removable, "
472                 "ai.app_package_installed_time, ai.app_support_mode, "
473                 "ai.app_support_ambient, ai.app_setup_appid";
474         static const char query_uid_info[] =
475                 ", ui.is_disabled, ui.is_splash_screen_enabled";
476         static const char query_label[] =
477                 ", COALESCE("
478                 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
479                 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
480         static const char query_icon[] =
481                 ", COALESCE("
482                 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
483                 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
484         static const char query_from_clause[] = " FROM package_app_info as ai";
485         static const char query_uid_info_clause[] =
486                 " LEFT OUTER JOIN package_app_info_for_uid AS ui "
487                 "ON (ai.app_id=ui.app_id AND ui.uid=?)";
488         int ret = PMINFO_R_ERROR;
489         int idx;
490         int len = 0;
491         char *dbpath;
492         char *bg_category_str = NULL;
493         char *constraint = NULL;
494         char *tmp_record = NULL;
495         char query[MAX_QUERY_LEN] = { '\0' };
496         char buf[BUFSIZE] = { '\0' };
497         application_x *info = NULL;
498         GList *bind_params = NULL;
499         sqlite3 *db = NULL;
500         sqlite3_stmt *stmt = NULL;
501         bool is_check_storage = true;
502         const uid_t global_user_uid = GLOBAL_USER;
503
504         dbpath = getUserPkgParserDBPathUID(db_uid);
505         if (dbpath == NULL)
506                 return PMINFO_R_ERROR;
507
508         ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
509         if (ret != SQLITE_OK) {
510                 _LOGE("failed to open db(%s): %d", dbpath, ret);
511                 free(dbpath);
512                 return PMINFO_R_ERROR;
513         }
514         free(dbpath);
515
516         len = strlen(query_raw);
517         snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
518
519         if (flag & PMINFO_APPINFO_GET_BASICINFO) {
520                 strncat(query, query_basic, MAX_QUERY_LEN - len - 1);
521                 len += strlen(query_basic);
522                 strncat(query, query_uid_info, MAX_QUERY_LEN - len - 1);
523                 len += strlen(query_uid_info);
524         }
525         if (flag & PMINFO_APPINFO_GET_LABEL) {
526                 strncat(query, query_label, MAX_QUERY_LEN - len - 1);
527                 len += strlen(query_label);
528                 bind_params = g_list_append(bind_params, strdup(locale));
529         }
530         if (flag & PMINFO_APPINFO_GET_ICON) {
531                 strncat(query, query_icon, MAX_QUERY_LEN - len - 1);
532                 len += strlen(query_icon);
533                 bind_params = g_list_append(bind_params, strdup(locale));
534         }
535
536         snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
537         bind_params = g_list_append(bind_params, strdup(buf));
538
539         is_check_storage = __check_app_storage_status(filter);
540
541         ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
542         if (ret != PMINFO_R_OK) {
543                 LOGE("Failed to get WHERE clause");
544                 goto catch;
545         }
546         strncat(query, query_from_clause, MAX_QUERY_LEN - len - 1);
547         len += strlen(query_from_clause);
548
549         strncat(query, query_uid_info_clause, MAX_QUERY_LEN - len - 1);
550         len += strlen(query_uid_info_clause);
551
552         if (constraint)
553                 strncat(query, constraint, MAX_QUERY_LEN - len - 1);
554
555         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
556         if (ret != SQLITE_OK) {
557                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
558                 ret = PMINFO_R_ERROR;
559                 goto catch;
560         }
561
562         if (g_list_length(bind_params) != 0) {
563                 ret = __bind_params(stmt, bind_params);
564                 if (ret != SQLITE_OK) {
565                         LOGE("Failed to bind parameters");
566                         goto catch;
567                 }
568         }
569
570         while (sqlite3_step(stmt) == SQLITE_ROW) {
571                 info = calloc(1, sizeof(application_x));
572                 if (info == NULL) {
573                         LOGE("out of memory");
574                         ret = PMINFO_R_ERROR;
575                         goto catch;
576                 }
577                 idx = 0;
578                 _save_column_str(stmt, idx++, &info->appid);
579                 _save_column_str(stmt, idx++, &info->installed_storage);
580                 _save_column_str(stmt, idx++ , &info->external_path);
581
582                 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
583                         _save_column_str(stmt, idx++, &info->component);
584                         _save_column_str(stmt, idx++, &info->exec);
585                         _save_column_str(stmt, idx++, &info->nodisplay);
586                         _save_column_str(stmt, idx++, &info->type);
587                         _save_column_str(stmt, idx++, &info->onboot);
588                         _save_column_str(stmt, idx++, &info->multiple);
589                         _save_column_str(stmt, idx++, &info->autorestart);
590                         _save_column_str(stmt, idx++, &info->taskmanage);
591                         _save_column_str(stmt, idx++, &info->hwacceleration);
592                         _save_column_str(stmt, idx++, &info->screenreader);
593                         _save_column_str(stmt, idx++, &info->mainapp);
594                         _save_column_str(stmt, idx++, &info->recentimage);
595                         _save_column_str(stmt, idx++, &info->launchcondition);
596                         _save_column_str(stmt, idx++, &info->indicatordisplay);
597                         _save_column_str(stmt, idx++, &info->portraitimg);
598                         _save_column_str(stmt, idx++, &info->landscapeimg);
599                         _save_column_str(stmt, idx++, &info->guestmode_visibility);
600                         _save_column_str(stmt, idx++, &info->permission_type);
601                         _save_column_str(stmt, idx++, &info->preload);
602                         _save_column_str(stmt, idx++, &info->submode);
603                         _save_column_str(stmt, idx++, &info->submode_mainid);
604                         _save_column_str(stmt, idx++, &info->launch_mode);
605                         _save_column_str(stmt, idx++, &info->ui_gadget);
606                         _save_column_str(stmt, idx++, &info->support_disable);
607                         _save_column_str(stmt, idx++, &info->process_pool);
608                         _save_column_str(stmt, idx++, &bg_category_str);
609                         _save_column_str(stmt, idx++, &info->package_type);
610                         _save_column_str(stmt, idx++, &info->root_path);
611                         _save_column_str(stmt, idx++, &info->api_version);
612                         _save_column_str(stmt, idx++, &info->effective_appid);
613                         _save_column_str(stmt, idx++, &info->is_disabled);
614                         _save_column_str(stmt, idx++, &info->splash_screen_display);
615                         _save_column_str(stmt, idx++, &info->tep_name);
616                         _save_column_str(stmt, idx++, &info->zip_mount_file);
617                         _save_column_str(stmt, idx++, &info->component_type);
618                         _save_column_str(stmt, idx++, &info->package);
619                         _save_column_str(stmt, idx++, &info->package_system);
620                         _save_column_str(stmt, idx++, &info->removable);
621                         _save_column_str(stmt, idx++, &info->package_installed_time);
622                         _save_column_str(stmt, idx++, &info->support_mode);
623                         _save_column_str(stmt, idx++, &info->support_ambient);
624                         _save_column_str(stmt, idx++, &info->setup_appid);
625                         info->background_category = __get_background_category(
626                                         bg_category_str);
627                         free(bg_category_str);
628                 }
629
630                 info->for_all_users =
631                         strdup((db_uid != global_user_uid) ? "false" : "true");
632
633                 if (db_uid != global_user_uid) {
634                         idx = idx + 2;
635                 } else {
636                         tmp_record = NULL;
637                         _save_column_str(stmt, idx++, &tmp_record);
638                         if (tmp_record != NULL) {
639                                 if (strcasecmp(info->is_disabled, "false") == 0 &&
640                                                 strcasecmp(tmp_record, "false") == 0) {
641                                         free(info->is_disabled);
642                                         info->is_disabled = tmp_record;
643                                 }
644                         }
645                         tmp_record = NULL;
646                         _save_column_str(stmt, idx++, &tmp_record);
647                         if (tmp_record != NULL) {
648                                 if (strcasecmp(info->splash_screen_display, "false") == 0 &&
649                                                 strcasecmp(tmp_record, "false") == 0) {
650                                         free(info->splash_screen_display);
651                                         info->splash_screen_display = tmp_record;
652                                 }
653                         }
654                 }
655
656                 if (flag & PMINFO_APPINFO_GET_LABEL) {
657                         tmp_record = NULL;
658                         _save_column_str(stmt, idx++, &tmp_record);
659                         if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
660                                 ret = PMINFO_R_ERROR;
661                                 goto catch;
662                         }
663                 }
664
665                 if (flag & PMINFO_APPINFO_GET_ICON) {
666                         tmp_record = NULL;
667                         _save_column_str(stmt, idx++, &tmp_record);
668                         if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
669                                 ret = PMINFO_R_ERROR;
670                                 goto catch;
671                         }
672                 }
673
674                 if (flag & PMINFO_APPINFO_GET_CATEGORY) {
675                         if (_appinfo_get_category(db, info->appid,
676                                                 &info->category)) {
677                                 ret = PMINFO_R_ERROR;
678                                 goto catch;
679                         }
680                 }
681
682                 if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
683                         if (_appinfo_get_app_control(db, info->appid,
684                                                 &info->appcontrol)) {
685                                 ret = PMINFO_R_ERROR;
686                                 goto catch;
687                         }
688                 }
689
690                 if (flag & PMINFO_APPINFO_GET_METADATA) {
691                         if (_appinfo_get_metadata(db, info->appid,
692                                                 &info->metadata)) {
693                                 ret = PMINFO_R_ERROR;
694                                 goto catch;
695                         }
696                 }
697
698                 if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
699                         if (_appinfo_get_splashscreens(db, info->appid,
700                                                 &info->splashscreens)) {
701                                 ret = PMINFO_R_ERROR;
702                                 goto catch;
703                         }
704                 }
705
706                 if (is_check_storage &&
707                                 __appinfo_check_installed_storage(info) != PMINFO_R_OK) {
708                         ret = PMINFO_R_ERROR;
709                         pkgmgrinfo_basic_free_application(info);
710                         info = NULL;
711                         continue;
712                 }
713
714                 g_hash_table_insert(applications, (gpointer)info->appid,
715                                 (gpointer)info);
716         }
717
718         ret = PMINFO_R_OK;
719
720 catch:
721         if (constraint)
722                 free(constraint);
723
724         if (ret != PMINFO_R_OK && info != NULL)
725                 pkgmgrinfo_basic_free_application(info);
726
727         g_list_free_full(bind_params, free);
728         sqlite3_close_v2(db);
729         sqlite3_finalize(stmt);
730
731         return ret;
732 }
733
734 static int _pkgmgrinfo_get_appinfo(const char *appid, uid_t uid,
735         pkgmgrinfo_appinfo_filter_h filter, pkgmgrinfo_appinfo_h *handle)
736 {
737         int ret;
738         char *locale;
739         GHashTable *list;
740         pkgmgr_appinfo_x *info;
741
742         if (appid == NULL || filter == NULL || handle == NULL) {
743                         LOGE("invalid parameter");
744                         return PMINFO_R_EINVAL;
745         }
746
747         locale = _get_system_locale();
748         if (locale == NULL)
749                 return PMINFO_R_ERROR;
750
751         list = g_hash_table_new(g_str_hash, g_str_equal);
752         if (list == NULL) {
753                 free(locale);
754                 return PMINFO_R_ERROR;
755         }
756
757         ret = _appinfo_get_applications(uid, uid, locale, filter,
758                         PMINFO_APPINFO_GET_ALL, list);
759         if (!g_hash_table_size(list) && uid != GLOBAL_USER)
760                 ret = _appinfo_get_applications(GLOBAL_USER, uid, locale, filter,
761                                 PMINFO_APPINFO_GET_ALL, list);
762
763         if (!g_hash_table_size(list)) {
764                 _LOGI("appinfo for [%s] is not existed for user [%d]",
765                                 appid, uid);
766                 g_hash_table_destroy(list);
767                 free(locale);
768                 return PMINFO_R_ENOENT;
769         }
770
771         info = calloc(1, sizeof(pkgmgr_appinfo_x));
772         if (info == NULL) {
773                 _LOGE("out of memory");
774                 g_hash_table_destroy(list);
775                 free(locale);
776                 return PMINFO_R_ERROR;
777         }
778
779         info->app_info = (application_x *)g_hash_table_lookup(list, appid);
780         info->locale = locale;
781         info->package = strdup(info->app_info->package);
782
783         /* just free list only */
784         g_hash_table_destroy(list);
785
786         *handle = info;
787
788         return ret;
789 }
790
791 API int pkgmgrinfo_appinfo_get_usr_disabled_appinfo(const char *appid, uid_t uid,
792                 pkgmgrinfo_appinfo_h *handle)
793 {
794         int ret;
795         pkgmgrinfo_appinfo_filter_h filter;
796
797         if (appid == NULL || handle == NULL) {
798                 LOGE("invalid parameter");
799                 return PMINFO_R_EINVAL;
800         }
801
802         ret = pkgmgrinfo_appinfo_filter_create(&filter);
803         if (ret != PMINFO_R_OK)
804                 return ret;
805
806         ret = pkgmgrinfo_appinfo_filter_add_string(filter,
807                         PMINFO_APPINFO_PROP_APP_ID, appid);
808         if (ret != PMINFO_R_OK) {
809                 pkgmgrinfo_appinfo_filter_destroy(filter);
810                 return PMINFO_R_ERROR;
811         }
812
813         ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
814                         PMINFO_APPINFO_PROP_APP_DISABLE, true);
815         if (ret != PMINFO_R_OK) {
816                 pkgmgrinfo_appinfo_filter_destroy(filter);
817                 return PMINFO_R_ERROR;
818         }
819
820         ret = _pkgmgrinfo_get_appinfo(appid, uid, filter, handle);
821         pkgmgrinfo_appinfo_filter_destroy(filter);
822
823         return ret;
824 }
825
826 API int pkgmgrinfo_appinfo_get_disabled_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
827 {
828         return pkgmgrinfo_appinfo_get_usr_disabled_appinfo(appid, _getuid(), handle);
829 }
830
831 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
832                 pkgmgrinfo_appinfo_h *handle)
833 {
834         int ret;
835         pkgmgrinfo_appinfo_filter_h filter;
836
837         if (appid == NULL || handle == NULL) {
838                 LOGE("invalid parameter");
839                 return PMINFO_R_EINVAL;
840         }
841
842         ret = pkgmgrinfo_appinfo_filter_create(&filter);
843         if (ret != PMINFO_R_OK)
844                 return ret;
845
846         ret = pkgmgrinfo_appinfo_filter_add_string(filter,
847                         PMINFO_APPINFO_PROP_APP_ID, appid);
848         if (ret != PMINFO_R_OK) {
849                 pkgmgrinfo_appinfo_filter_destroy(filter);
850                 return PMINFO_R_ERROR;
851         }
852
853         ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
854                         PMINFO_APPINFO_PROP_APP_DISABLE, false);
855         if (ret != PMINFO_R_OK) {
856                 pkgmgrinfo_appinfo_filter_destroy(filter);
857                 return PMINFO_R_ERROR;
858         }
859
860         ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
861                         PMINFO_APPINFO_PROP_PKG_DISABLE, false);
862         if (ret != PMINFO_R_OK) {
863                 pkgmgrinfo_appinfo_filter_destroy(filter);
864                 return PMINFO_R_ERROR;
865         }
866
867         ret = _pkgmgrinfo_get_appinfo(appid, uid, filter, handle);
868         pkgmgrinfo_appinfo_filter_destroy(filter);
869         return ret;
870 }
871
872 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
873 {
874         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, _getuid(), handle);
875 }
876
877 API int pkgmgrinfo_appinfo_get_usr_all_appinfo(const char *appid, uid_t uid,
878                 pkgmgrinfo_appinfo_h *handle)
879 {
880         int ret;
881         pkgmgrinfo_appinfo_filter_h filter;
882
883         if (appid == NULL || handle == NULL) {
884                 LOGE("invalid parameter");
885                 return PMINFO_R_EINVAL;
886         }
887
888         ret = pkgmgrinfo_appinfo_filter_create(&filter);
889         if (ret != PMINFO_R_OK)
890                 return ret;
891
892         ret = pkgmgrinfo_appinfo_filter_add_string(filter,
893                         PMINFO_APPINFO_PROP_APP_ID, appid);
894         if (ret != PMINFO_R_OK) {
895                 pkgmgrinfo_appinfo_filter_destroy(filter);
896                 return PMINFO_R_ERROR;
897         }
898
899         ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
900                         PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, false);
901         if (ret != PMINFO_R_OK) {
902                 pkgmgrinfo_appinfo_filter_destroy(filter);
903                 return PMINFO_R_ERROR;
904         }
905
906         ret = _pkgmgrinfo_get_appinfo(appid, uid, filter, handle);
907         pkgmgrinfo_appinfo_filter_destroy(filter);
908
909         return ret;
910 }
911
912 API int pkgmgrinfo_appinfo_get_all_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
913 {
914         return pkgmgrinfo_appinfo_get_usr_all_appinfo(appid, _getuid(), handle);
915 }
916
917 static gpointer __copy_str(gconstpointer src, gpointer data)
918 {
919         const char *tmp = (const char *)src;
920         char *buffer;
921
922         buffer = strdup(tmp);
923         if (buffer == NULL) {
924                 LOGE("memory alloc failed");
925                 *(int *)data = -1;
926                 return NULL;
927         }
928
929         return buffer;
930 }
931
932 static gpointer __copy_label(gconstpointer src, gpointer data)
933 {
934         label_x *tmp = (label_x *)src;
935         label_x *label;
936
937         label = calloc(1, sizeof(label_x));
938         if (label == NULL) {
939                 LOGE("memory alloc failed");
940                 *(int *)data = -1;
941                 return NULL;
942         }
943
944         if (tmp->name)
945                 label->name = strdup(tmp->name);
946         if (tmp->text)
947                 label->text = strdup(tmp->text);
948         if (tmp->lang)
949                 label->lang = strdup(tmp->lang);
950
951         return label;
952 }
953
954 static gpointer __copy_icon(gconstpointer src, gpointer data)
955 {
956         icon_x *tmp = (icon_x *)src;
957         icon_x *icon;
958
959         icon = calloc(1, sizeof(icon_x));
960         if (icon == NULL) {
961                 LOGE("memory alloc failed");
962                 *(int *)data = -1;
963                 return NULL;
964         }
965
966         if (tmp->text)
967                 icon->text = strdup(tmp->text);
968         if (tmp->lang)
969                 icon->lang = strdup(tmp->lang);
970         if (tmp->section)
971                 icon->section = strdup(tmp->section);
972         if (tmp->size)
973                 icon->size = strdup(tmp->size);
974         if (tmp->resolution)
975                 icon->resolution = strdup(tmp->resolution);
976
977         return icon;
978 }
979
980 static gpointer __copy_metadata(gconstpointer src, gpointer data)
981 {
982         metadata_x *tmp = (metadata_x *)src;
983         metadata_x *metadata;
984
985         metadata = calloc(1, sizeof(metadata_x));
986         if (metadata == NULL) {
987                 LOGE("memory alloc failed");
988                 *(int *)data = -1;
989                 return NULL;
990         }
991
992         if (tmp->key)
993                 metadata->key = strdup(tmp->key);
994         if (tmp->value)
995                 metadata->value = strdup(tmp->value);
996
997         return metadata;
998 }
999
1000 static gpointer __copy_datacontrol(gconstpointer src, gpointer data)
1001 {
1002         datacontrol_x *tmp = (datacontrol_x *)src;
1003         datacontrol_x *datacontrol;
1004
1005         datacontrol = calloc(1, sizeof(datacontrol_x));
1006         if (datacontrol == NULL) {
1007                 LOGE("memory alloc failed");
1008                 *(int *)data = -1;
1009                 return NULL;
1010         }
1011
1012         if (tmp->providerid)
1013                 datacontrol->providerid = strdup(tmp->providerid);
1014         if (tmp->access)
1015                 datacontrol->access = strdup(tmp->access);
1016         if (tmp->type)
1017                 datacontrol->type = strdup(tmp->type);
1018         if (tmp->trusted)
1019                 datacontrol->trusted = strdup(tmp->trusted);
1020
1021         return datacontrol;
1022 }
1023
1024 static gpointer __copy_appcontrol(gconstpointer src, gpointer data)
1025 {
1026         appcontrol_x *tmp = (appcontrol_x *)src;
1027         appcontrol_x *appcontrol;
1028
1029         appcontrol = calloc(1, sizeof(appcontrol_x));
1030         if (appcontrol == NULL) {
1031                 LOGE("memory alloc failed");
1032                 *(int *)data = -1;
1033                 return NULL;
1034         }
1035
1036         if (tmp->operation)
1037                 appcontrol->operation = strdup(tmp->operation);
1038         if (tmp->uri)
1039                 appcontrol->uri = strdup(tmp->uri);
1040         if (tmp->mime)
1041                 appcontrol->mime = strdup(tmp->mime);
1042
1043         return appcontrol;
1044 }
1045
1046 static gpointer __copy_splashscreens(gconstpointer src, gpointer data)
1047 {
1048         splashscreen_x *tmp = (splashscreen_x *)src;
1049         splashscreen_x *splashscreen;
1050
1051         splashscreen = (splashscreen_x *)calloc(1, sizeof(splashscreen_x));
1052         if (splashscreen == NULL) {
1053                 LOGE("memory alloc failed");
1054                 *(int *)data = -1;
1055                 return NULL;
1056         }
1057
1058         if (tmp->src)
1059                 splashscreen->src = strdup(tmp->src);
1060         if (tmp->type)
1061                 splashscreen->type = strdup(tmp->type);
1062         if (tmp->orientation)
1063                 splashscreen->orientation = strdup(tmp->orientation);
1064         if (tmp->indicatordisplay)
1065                 splashscreen->indicatordisplay = strdup(tmp->indicatordisplay);
1066         if (tmp->operation)
1067                 splashscreen->operation = strdup(tmp->operation);
1068         if (tmp->color_depth)
1069                 splashscreen->color_depth = strdup(tmp->color_depth);
1070
1071         return splashscreen;
1072 }
1073
1074 static int _appinfo_copy_appinfo(application_x **application, application_x *data)
1075 {
1076         application_x *app_info;
1077         int ret;
1078
1079         app_info = calloc(1, sizeof(application_x));
1080         if (app_info == NULL) {
1081                 LOGE("memory alloc failed");
1082                 return PMINFO_R_ERROR;
1083         }
1084
1085         if (data->appid != NULL)
1086                 app_info->appid = strdup(data->appid);
1087         if (data->exec != NULL)
1088                 app_info->exec = strdup(data->exec);
1089         if (data->nodisplay != NULL)
1090                 app_info->nodisplay = strdup(data->nodisplay);
1091         if (data->multiple != NULL)
1092                 app_info->multiple = strdup(data->multiple);
1093         if (data->taskmanage != NULL)
1094                 app_info->taskmanage = strdup(data->taskmanage);
1095         if (data->type != NULL)
1096                 app_info->type = strdup(data->type);
1097         if (data->categories != NULL)
1098                 app_info->categories = strdup(data->categories);
1099         if (data->hwacceleration != NULL)
1100                 app_info->hwacceleration = strdup(data->hwacceleration);
1101         if (data->screenreader != NULL)
1102                 app_info->screenreader = strdup(data->screenreader);
1103         if (data->mainapp != NULL)
1104                 app_info->mainapp = strdup(data->mainapp);
1105         if (data->package != NULL)
1106                 app_info->package = strdup(data->package);
1107         if (data->recentimage != NULL)
1108                 app_info->recentimage = strdup(data->recentimage);
1109         if (data->launchcondition != NULL)
1110                 app_info->launchcondition = strdup(data->launchcondition);
1111         if (data->indicatordisplay != NULL)
1112                 app_info->indicatordisplay = strdup(data->indicatordisplay);
1113         if (data->portraitimg != NULL)
1114                 app_info->portraitimg = strdup(data->portraitimg);
1115         if (data->landscapeimg != NULL)
1116                 app_info->landscapeimg = strdup(data->landscapeimg);
1117         if (data->guestmode_visibility != NULL)
1118                 app_info->guestmode_visibility = strdup(data->guestmode_visibility);
1119         if (data->component != NULL)
1120                 app_info->component = strdup(data->component);
1121         if (data->permission_type != NULL)
1122                 app_info->permission_type = strdup(data->permission_type);
1123         if (data->component_type != NULL)
1124                 app_info->component_type = strdup(data->component_type);
1125         if (data->preload != NULL)
1126                 app_info->preload = strdup(data->preload);
1127         if (data->submode != NULL)
1128                 app_info->submode = strdup(data->submode);
1129         if (data->submode_mainid != NULL)
1130                 app_info->submode_mainid = strdup(data->submode_mainid);
1131         if (data->process_pool != NULL)
1132                 app_info->process_pool = strdup(data->process_pool);
1133         if (data->installed_storage != NULL)
1134                 app_info->installed_storage = strdup(data->installed_storage);
1135         if (data->autorestart != NULL)
1136                 app_info->autorestart = strdup(data->autorestart);
1137         if (data->onboot != NULL)
1138                 app_info->onboot = strdup(data->onboot);
1139         if (data->support_disable != NULL)
1140                 app_info->support_disable = strdup(data->support_disable);
1141         if (data->ui_gadget != NULL)
1142                 app_info->ui_gadget = strdup(data->ui_gadget);
1143         if (data->launch_mode != NULL)
1144                 app_info->launch_mode = strdup(data->launch_mode);
1145         if (data->package_type != NULL)
1146                 app_info->package_type = strdup(data->package_type);
1147         if (data->effective_appid != NULL)
1148                 app_info->effective_appid = strdup(data->effective_appid);
1149         if (data->splash_screen_display != NULL)
1150                 app_info->splash_screen_display = strdup(data->splash_screen_display);
1151
1152         /* GList */
1153         ret = 0;
1154         app_info->label = g_list_copy_deep(data->label, __copy_label, &ret);
1155         if (ret < 0) {
1156                 LOGE("memory alloc failed");
1157                 pkgmgrinfo_basic_free_application(app_info);
1158                 return PMINFO_R_ERROR;
1159         }
1160
1161         ret = 0;
1162         app_info->icon = g_list_copy_deep(data->icon, __copy_icon, &ret);
1163         if (ret < 0) {
1164                 LOGE("memory alloc failed");
1165                 pkgmgrinfo_basic_free_application(app_info);
1166                 return PMINFO_R_ERROR;
1167         }
1168
1169         ret = 0;
1170         app_info->category = g_list_copy_deep(data->category, __copy_str, &ret);
1171         if (ret < 0) {
1172                 LOGE("memory alloc failed");
1173                 pkgmgrinfo_basic_free_application(app_info);
1174                 return PMINFO_R_ERROR;
1175         }
1176
1177         ret = 0;
1178         app_info->metadata = g_list_copy_deep(data->metadata, __copy_metadata, &ret);
1179         if (ret < 0) {
1180                 LOGE("memory alloc failed");
1181                 pkgmgrinfo_basic_free_application(app_info);
1182                 return PMINFO_R_ERROR;
1183         }
1184
1185         ret = 0;
1186         app_info->datacontrol = g_list_copy_deep(data->datacontrol, __copy_datacontrol, &ret);
1187         if (ret < 0) {
1188                 LOGE("memory alloc failed");
1189                 pkgmgrinfo_basic_free_application(app_info);
1190                 return PMINFO_R_ERROR;
1191         }
1192
1193         ret = 0;
1194         app_info->appcontrol = g_list_copy_deep(data->appcontrol, __copy_appcontrol, &ret);
1195         if (ret < 0) {
1196                 LOGE("memory alloc failed");
1197                 pkgmgrinfo_basic_free_application(app_info);
1198                 return PMINFO_R_ERROR;
1199         }
1200
1201         ret = 0;
1202         app_info->background_category = g_list_copy_deep(data->background_category, __copy_str, &ret);
1203         if (ret < 0) {
1204                 LOGE("memory alloc failed");
1205                 pkgmgrinfo_basic_free_application(app_info);
1206                 return PMINFO_R_ERROR;
1207         }
1208
1209         ret = 0;
1210         app_info->splashscreens = g_list_copy_deep(data->splashscreens, __copy_splashscreens, &ret);
1211         if (ret < 0) {
1212                 LOGE("memory alloc failed");
1213                 pkgmgrinfo_basic_free_application(app_info);
1214                 return PMINFO_R_ERROR;
1215         }
1216
1217         *application = app_info;
1218
1219         return PMINFO_R_OK;
1220 }
1221
1222 API int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle,
1223                 pkgmgrinfo_appinfo_h *clone)
1224 {
1225         pkgmgr_appinfo_x *info;
1226         pkgmgr_appinfo_x *temp = (pkgmgr_appinfo_x *)handle;
1227
1228         if (handle == NULL)
1229                 return PMINFO_R_EINVAL;
1230
1231         info = calloc(1, sizeof(pkgmgr_appinfo_x));
1232         if (info == NULL) {
1233                 LOGE("memory alloc failed");
1234                 return PMINFO_R_ERROR;
1235         }
1236
1237         if (temp->package != NULL)
1238                 info->package = strdup(temp->package);
1239         if (temp->locale != NULL)
1240                 info->locale = strdup(temp->locale);
1241
1242         info->app_component = temp->app_component;
1243
1244         if (_appinfo_copy_appinfo(&info->app_info, temp->app_info) < 0) {
1245                 LOGE("appinfo copy failed");
1246                 if (info->package)
1247                         free((void *)info->package);
1248                 if (info->locale)
1249                         free(info->locale);
1250                 free(info);
1251                 return PMINFO_R_ERROR;
1252         }
1253
1254         *clone = info;
1255
1256         return PMINFO_R_OK;
1257 }
1258
1259 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
1260                 pkgmgrinfo_filter_x *filter, int flag, pkgmgrinfo_app_list_cb app_list_cb,
1261                 void *user_data)
1262 {
1263         int ret;
1264         char *locale;
1265         application_x *app;
1266         pkgmgr_appinfo_x info;
1267         GHashTable *list;
1268         GHashTableIter iter;
1269         gpointer value;
1270
1271         locale = _get_system_locale();
1272         if (locale == NULL)
1273                 return PMINFO_R_ERROR;
1274
1275         list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1276                         __free_applications);
1277         if (list == NULL) {
1278                 free(locale);
1279                 return PMINFO_R_ERROR;
1280         }
1281
1282         ret = _appinfo_get_applications(uid, uid, locale, filter,
1283                         flag | PMINFO_APPINFO_GET_BASICINFO, list);
1284         if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
1285                 ret = _appinfo_get_applications(GLOBAL_USER, uid, locale,
1286                                 filter, flag | PMINFO_APPINFO_GET_BASICINFO, list);
1287
1288         if (ret != PMINFO_R_OK) {
1289                 g_hash_table_destroy(list);
1290                 free(locale);
1291                 return ret;
1292         }
1293
1294         g_hash_table_iter_init(&iter, list);
1295         while (g_hash_table_iter_next(&iter, NULL, &value)) {
1296                 app = (application_x *)value;
1297                 info.app_info = app;
1298                 info.locale = locale;
1299                 info.package = app->package;
1300                 if (app_list_cb(&info, user_data) < 0)
1301                         break;
1302         }
1303         g_hash_table_destroy(list);
1304         free(locale);
1305
1306         return PMINFO_R_OK;
1307 }
1308
1309 static const char *__appcomponent_str(pkgmgrinfo_app_component comp);
1310
1311 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
1312                 pkgmgrinfo_app_component component,
1313                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
1314 {
1315         int ret;
1316         pkgmgrinfo_appinfo_filter_h filter;
1317         char *pkgid;
1318         const char *comp_str = NULL;
1319
1320         if (handle == NULL || app_func == NULL) {
1321                 LOGE("invalid parameter");
1322                 return PMINFO_R_EINVAL;
1323         }
1324
1325         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
1326                 LOGE("invalid parameter");
1327                 return PMINFO_R_EINVAL;
1328         }
1329
1330         if (pkgmgrinfo_appinfo_filter_create(&filter))
1331                 return PMINFO_R_ERROR;
1332
1333         if (pkgmgrinfo_appinfo_filter_add_string(filter,
1334                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
1335                 pkgmgrinfo_appinfo_filter_destroy(filter);
1336                 return PMINFO_R_ERROR;
1337         }
1338
1339         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1340                         PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
1341                 pkgmgrinfo_appinfo_filter_destroy(filter);
1342                 return PMINFO_R_ERROR;
1343         }
1344
1345         comp_str = __appcomponent_str(component);
1346
1347         if (comp_str) {
1348                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
1349                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
1350                                         comp_str)) {
1351                         pkgmgrinfo_appinfo_filter_destroy(filter);
1352                         return PMINFO_R_ERROR;
1353                 }
1354         }
1355
1356         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter,
1357                         PMINFO_APPINFO_GET_ALL, app_func, user_data);
1358
1359         pkgmgrinfo_appinfo_filter_destroy(filter);
1360
1361         return ret;
1362 }
1363
1364 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle,
1365                 pkgmgrinfo_app_component component,
1366                 pkgmgrinfo_app_list_cb app_func, void *user_data)
1367 {
1368         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, _getuid());
1369 }
1370
1371 API int pkgmgrinfo_appinfo_get_usr_installed_list_full(
1372                 pkgmgrinfo_app_list_cb app_func, uid_t uid, int flag,
1373                 void *user_data)
1374 {
1375         int ret;
1376         pkgmgrinfo_appinfo_filter_h filter;
1377
1378         if (app_func == NULL) {
1379                 LOGE("invalid parameter");
1380                 return PMINFO_R_EINVAL;
1381         }
1382
1383         if (pkgmgrinfo_appinfo_filter_create(&filter))
1384                 return PMINFO_R_ERROR;
1385
1386         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1387                         PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
1388                 pkgmgrinfo_appinfo_filter_destroy(filter);
1389                 return PMINFO_R_ERROR;
1390         }
1391
1392         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1393                         PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
1394                 pkgmgrinfo_appinfo_filter_destroy(filter);
1395                 return PMINFO_R_ERROR;
1396         }
1397
1398         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1399                         PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, false)) {
1400                 pkgmgrinfo_appinfo_filter_destroy(filter);
1401                 return PMINFO_R_ERROR;
1402         }
1403
1404         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, flag, app_func,
1405                         user_data);
1406
1407         pkgmgrinfo_appinfo_filter_destroy(filter);
1408
1409         return ret;
1410 }
1411
1412 API int pkgmgrinfo_appinfo_get_installed_list_full(
1413                 pkgmgrinfo_app_list_cb app_func, int flag, void *user_data)
1414 {
1415         return pkgmgrinfo_appinfo_get_usr_installed_list_full(app_func,
1416                         _getuid(), flag, user_data);
1417 }
1418
1419 API int pkgmgrinfo_appinfo_get_usr_installed_list(
1420                 pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
1421 {
1422         int ret;
1423         pkgmgrinfo_appinfo_filter_h filter;
1424
1425         if (app_func == NULL) {
1426                 LOGE("invalid parameter");
1427                 return PMINFO_R_EINVAL;
1428         }
1429
1430         /* create an empty filter */
1431         ret = pkgmgrinfo_appinfo_filter_create(&filter);
1432         if (ret != PMINFO_R_OK)
1433                 return ret;
1434
1435         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1436                         PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
1437                 pkgmgrinfo_appinfo_filter_destroy(filter);
1438                 return PMINFO_R_ERROR;
1439         }
1440
1441         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
1442                         PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
1443                 pkgmgrinfo_appinfo_filter_destroy(filter);
1444                 return PMINFO_R_ERROR;
1445         }
1446
1447         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter,
1448                         PMINFO_APPINFO_GET_ALL, app_func, user_data);
1449
1450         pkgmgrinfo_appinfo_filter_destroy(filter);
1451
1452         return ret;
1453 }
1454
1455 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func,
1456                 void *user_data)
1457 {
1458         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, _getuid(),
1459                         user_data);
1460 }
1461
1462 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
1463 {
1464         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1465
1466         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1467         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1468
1469         if (info->app_info == NULL || info->app_info->appid == NULL)
1470                 return PMINFO_R_ERROR;
1471         *appid = (char *)info->app_info->appid;
1472
1473         return PMINFO_R_OK;
1474 }
1475
1476 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
1477 {
1478         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1479
1480         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1481         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1482
1483         if (info->package == NULL)
1484                 return PMINFO_R_ERROR;
1485
1486         *pkg_name = (char *)info->package;
1487
1488         return PMINFO_R_OK;
1489 }
1490
1491 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
1492 {
1493         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1494
1495         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1496         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1497
1498         if (info->package == NULL)
1499                 return PMINFO_R_ERROR;
1500
1501         *pkgid = (char *)info->package;
1502
1503         return PMINFO_R_OK;
1504 }
1505
1506 API int pkgmgrinfo_appinfo_get_pkgtype(pkgmgrinfo_appinfo_h  handle, char **pkgtype)
1507 {
1508         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1509         retvm_if(pkgtype == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1510         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1511
1512         *pkgtype = (char *)info->app_info->package_type;
1513
1514         return PMINFO_R_OK;
1515 }
1516
1517 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
1518 {
1519         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1520
1521         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1522         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1523
1524         if (info->app_info == NULL || info->app_info->exec == NULL)
1525                 return PMINFO_R_ERROR;
1526         *exec = (char *)info->app_info->exec;
1527
1528         return PMINFO_R_OK;
1529 }
1530
1531
1532 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1533 {
1534         icon_x *ptr;
1535         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1536
1537         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1538         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1539
1540         if (info->app_info == NULL)
1541                 return PMINFO_R_ERROR;
1542
1543         if (info->app_info->icon == NULL) {
1544                 *icon = "";
1545                 return PMINFO_R_OK;
1546         }
1547
1548         ptr = (icon_x *)info->app_info->icon->data;
1549         if (ptr == NULL)
1550                 return PMINFO_R_ERROR;
1551
1552         if (ptr->text == NULL)
1553                 return PMINFO_R_ERROR;
1554                 else
1555                         *icon = ptr->text;
1556
1557         return PMINFO_R_OK;
1558 }
1559
1560
1561 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
1562 {
1563         label_x *ptr;
1564         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1565         char *lbl = NULL;
1566         const char *locale;
1567         GList *tmp;
1568
1569         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1570         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1571
1572         if (info->app_info == NULL)
1573                 return PMINFO_R_ERROR;
1574
1575         locale = info->locale;
1576         if (locale == NULL)
1577                 locale = DEFAULT_LOCALE;
1578
1579         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1580                 ptr = (label_x *)tmp->data;
1581                 if (ptr == NULL || strcmp(locale, ptr->lang) != 0)
1582                         continue;
1583                 lbl = ptr->text;
1584                 break;
1585         }
1586
1587         if (lbl != NULL) {
1588                 *label = lbl;
1589                 return PMINFO_R_OK;
1590         }
1591
1592         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1593                 ptr = (label_x *)tmp->data;
1594                 if (ptr == NULL || strcmp(DEFAULT_LOCALE, ptr->lang) != 0)
1595                         continue;
1596                 lbl = ptr->text;
1597                 break;
1598         }
1599
1600         *label = lbl ? lbl : "";
1601
1602         return PMINFO_R_OK;
1603 }
1604
1605 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1606 {
1607         char *result = NULL;
1608         char *query = NULL;
1609         sqlite3_stmt *stmt = NULL;
1610         sqlite3 *db = NULL;
1611         char *val;
1612         char *parser_db;
1613
1614         parser_db = getUserPkgParserDBPathUID(uid);
1615         if (parser_db == NULL) {
1616                 _LOGE("Failed to get parser db path");
1617                 goto err;
1618         }
1619
1620         if (__open_db(parser_db, &db, SQLITE_OPEN_READONLY) != SQLITE_OK) {
1621                 _LOGE("DB open fail\n");
1622                 free(parser_db);
1623                 goto err;
1624         }
1625         free(parser_db);
1626
1627         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1628         if (query == NULL) {
1629                 _LOGE("Out of memory");
1630                 goto err;
1631         }
1632
1633         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1634                 _LOGE("prepare_v2 fail\n");
1635                 goto err;
1636         }
1637
1638         if (sqlite3_step(stmt) == SQLITE_ROW) {
1639                 val = (char *)sqlite3_column_text(stmt, 0);
1640                 if (val != NULL)
1641                         result = strdup(val);
1642         }
1643
1644 err:
1645         sqlite3_finalize(stmt);
1646         sqlite3_free(query);
1647         sqlite3_close(db);
1648
1649         return result;
1650 }
1651
1652 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1653 {
1654         char *val;
1655
1656         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1657
1658         val = _get_localed_label(appid, locale, uid);
1659         if (val == NULL)
1660                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1661
1662         if (val == NULL) {
1663                 val = _get_localed_label(appid, locale, GLOBAL_USER);
1664                 if (val == NULL)
1665                         val = _get_localed_label(appid, DEFAULT_LOCALE, GLOBAL_USER);
1666         }
1667
1668         if (val == NULL)
1669                 return PMINFO_R_ERROR;
1670
1671         *label = val;
1672
1673         return PMINFO_R_OK;
1674 }
1675
1676 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1677 {
1678         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, _getuid(), label);
1679 }
1680
1681 API int pkgmgrinfo_appinfo_get_metadata_value(pkgmgrinfo_appinfo_h handle, const char *metadata_key, char **metadata_value)
1682 {
1683         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1684         retvm_if(metadata_key == NULL, PMINFO_R_EINVAL, "metadata_key is NULL");
1685         retvm_if(metadata_value == NULL, PMINFO_R_EINVAL, "metadata_value is NULL");
1686
1687         GList *list_md = NULL;
1688         metadata_x *metadata = NULL;
1689         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1690
1691         list_md = info->app_info->metadata;
1692
1693         for (; list_md; list_md = list_md->next) {
1694                 metadata = (metadata_x *)list_md->data;
1695                 if (metadata && metadata->key) {
1696                         if (strcasecmp(metadata->key, metadata_key) == 0) {
1697                                 if (metadata->value == NULL)
1698                                         *metadata_value = "";
1699                                 else
1700                                         *metadata_value = (char *)metadata->value;
1701                                 return PMINFO_R_OK;
1702                         }
1703                 }
1704         }
1705
1706         return PMINFO_R_EINVAL;
1707 }
1708
1709 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1710 {
1711         if (strcasecmp(comp, "uiapp") == 0)
1712                 return PMINFO_UI_APP;
1713         else if (strcasecmp(comp, "svcapp") == 0)
1714                 return PMINFO_SVC_APP;
1715         else if (strcasecmp(comp, "widgetapp") == 0)
1716                 return PMINFO_WIDGET_APP;
1717         else if (strcasecmp(comp, "watchapp") == 0)
1718                 return PMINFO_WATCH_APP;
1719         else
1720                 return -1;
1721 }
1722
1723 static const char *__appcomponent_str(pkgmgrinfo_app_component comp)
1724 {
1725         switch (comp) {
1726         case PMINFO_UI_APP:
1727                 return "uiapp";
1728         case PMINFO_SVC_APP:
1729                 return "svcapp";
1730         case PMINFO_WIDGET_APP:
1731                 return "widgetapp";
1732         case PMINFO_WATCH_APP:
1733                 return "watchapp";
1734         default:
1735                 return NULL;
1736         }
1737 }
1738
1739 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1740 {
1741         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1742         int comp;
1743
1744         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1745         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1746
1747         if (info->app_info == NULL)
1748                 return PMINFO_R_ERROR;
1749
1750         comp = __appcomponent_convert(info->app_info->component);
1751         if (comp < 0)
1752                 return PMINFO_R_ERROR;
1753
1754         *component = comp;
1755
1756         return PMINFO_R_OK;
1757 }
1758
1759 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1760 {
1761         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1762
1763         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1764         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1765
1766         if (info->app_info == NULL || info->app_info->type == NULL)
1767                 return PMINFO_R_ERROR;
1768         *app_type = (char *)info->app_info->type;
1769
1770         return PMINFO_R_OK;
1771 }
1772
1773 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1774                                         int *operation_count, char ***operation)
1775 {
1776         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1777         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1778         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1779         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1780         *operation_count = data->operation_count;
1781         *operation = data->operation;
1782         return PMINFO_R_OK;
1783 }
1784
1785 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1786                                         int *uri_count, char ***uri)
1787 {
1788         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1789         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1790         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1791         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1792         *uri_count = data->uri_count;
1793         *uri = data->uri;
1794         return PMINFO_R_OK;
1795 }
1796
1797 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1798                                         int *mime_count, char ***mime)
1799 {
1800         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1801         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1802         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1803         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1804         *mime_count = data->mime_count;
1805         *mime = data->mime;
1806         return PMINFO_R_OK;
1807 }
1808
1809 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1810                                         int *subapp_count, char ***subapp)
1811 {
1812         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1813         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1814         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1815         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1816         *subapp_count = data->subapp_count;
1817         *subapp = data->subapp;
1818         return PMINFO_R_OK;
1819 }
1820
1821 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1822 {
1823         char *val;
1824         icon_x *ptr;
1825         GList *tmp;
1826         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1827
1828         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1829         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1830
1831         if (info->app_info == NULL)
1832                 return PMINFO_R_ERROR;
1833
1834         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1835                 ptr = (icon_x *)tmp->data;
1836                 if (ptr == NULL || ptr->section == NULL)
1837                         continue;
1838
1839                 val = (char *)ptr->section;
1840                 if (val && strcmp(val, "notification") == 0) {
1841                         *icon = (char *)ptr->text;
1842                         return PMINFO_R_OK;
1843                 }
1844         }
1845
1846         return PMINFO_R_ERROR;
1847 }
1848
1849 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1850 {
1851         char *val;
1852         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1853
1854         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1855         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1856
1857         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1858                 return PMINFO_R_ERROR;
1859
1860         val = (char *)info->app_info->recentimage;
1861         if (strcasecmp(val, "capture") == 0)
1862                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1863         else if (strcasecmp(val, "icon") == 0)
1864                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1865         else
1866                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1867
1868         return PMINFO_R_OK;
1869 }
1870
1871 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1872 {
1873         char *val;
1874         image_x *ptr;
1875         GList *tmp;
1876         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1877
1878         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1879         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1880
1881         if (info->app_info == NULL)
1882                 return PMINFO_R_ERROR;
1883
1884         for (tmp = info->app_info->image; tmp; tmp = tmp->next) {
1885                 ptr = (image_x *)tmp->data;
1886                 if (ptr == NULL || ptr->section == NULL)
1887                         continue;
1888
1889                 val = (char *)ptr->section;
1890                 if (val && strcmp(val, "preview") == 0) {
1891                         *preview_img = (char *)ptr->text;
1892                         return PMINFO_R_OK;
1893                 }
1894         }
1895
1896         return PMINFO_R_ERROR;
1897 }
1898
1899 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1900 {
1901         const char *val;
1902         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1903
1904         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1905         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1906
1907         val = info->app_info->permission_type;
1908         if (val == NULL)
1909                 return PMINFO_R_ERROR;
1910
1911         if (strcmp(val, "signature") == 0)
1912                 *permission = PMINFO_PERMISSION_SIGNATURE;
1913         else if (strcmp(val, "privilege") == 0)
1914                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1915         else
1916                 *permission = PMINFO_PERMISSION_NORMAL;
1917
1918         return PMINFO_R_OK;
1919 }
1920
1921 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1922 {
1923         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1924
1925         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1926         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1927
1928         if (info->app_info == NULL || info->app_info->component_type == NULL)
1929                 return PMINFO_R_ERROR;
1930
1931         *component_type = (char *)info->app_info->component_type;
1932
1933         return PMINFO_R_OK;
1934 }
1935
1936 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1937 {
1938         char *val;
1939         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1940
1941         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1942         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1943
1944         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1945                 return PMINFO_R_ERROR;
1946
1947         val = (char *)info->app_info->hwacceleration;
1948         if (strcasecmp(val, "off") == 0)
1949                 *hwacceleration = PMINFO_HWACCELERATION_OFF;
1950         else if (strcasecmp(val, "on") == 0)
1951                 *hwacceleration = PMINFO_HWACCELERATION_ON;
1952         else
1953                 *hwacceleration = PMINFO_HWACCELERATION_DEFAULT;
1954
1955         return PMINFO_R_OK;
1956 }
1957
1958 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1959 {
1960         char *val;
1961         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1962
1963         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1964         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1965
1966         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1967                 return PMINFO_R_ERROR;
1968
1969         val = (char *)info->app_info->screenreader;
1970         if (strcasecmp(val, "screenreader-off") == 0)
1971                 *screenreader = PMINFO_SCREENREADER_OFF;
1972         else if (strcasecmp(val, "screenreader-on") == 0)
1973                 *screenreader = PMINFO_SCREENREADER_ON;
1974         else
1975                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1976
1977         return PMINFO_R_OK;
1978 }
1979
1980 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1981 {
1982         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1983
1984         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1985         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1986         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1987
1988         if (info->app_info == NULL)
1989                 return PMINFO_R_ERROR;
1990
1991         if (info->app_info->portraitimg == NULL)
1992                 *portrait_img = "";
1993         else
1994                 *portrait_img = (char *)info->app_info->portraitimg;
1995
1996         if (info->app_info->landscapeimg == NULL)
1997                 *landscape_img = "";
1998         else
1999                 *landscape_img = (char *)info->app_info->landscapeimg;
2000
2001         return PMINFO_R_OK;
2002 }
2003
2004 API int pkgmgrinfo_appinfo_get_effectimage_type(pkgmgrinfo_appinfo_h handle, char **effectimage_type)
2005 {
2006         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2007
2008         if (handle == NULL || effectimage_type == NULL) {
2009                 LOGE("invalid parameter");
2010                 return PMINFO_R_EINVAL;
2011         }
2012
2013         if (info->app_info == NULL || info->app_info->effectimage_type == NULL)
2014                 return PMINFO_R_ERROR;
2015
2016         *effectimage_type = (char *)info->app_info->effectimage_type;
2017
2018         return PMINFO_R_OK;
2019 }
2020
2021 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
2022 {
2023         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2024
2025         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2026         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2027
2028         if (info->app_info == NULL)
2029                 return PMINFO_R_ERROR;
2030
2031         if (info->app_info->submode_mainid == NULL)
2032                 *submode_mainid = "";
2033         else
2034                 *submode_mainid = (char *)info->app_info->submode_mainid;
2035
2036         return PMINFO_R_OK;
2037 }
2038
2039 API int pkgmgrinfo_appinfo_get_installed_storage_location(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_installed_storage *storage)
2040 {
2041         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2042         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2043
2044         if (info->app_info && info->app_info->installed_storage) {
2045                  if (strcmp(info->app_info->installed_storage, "installed_internal") == 0)
2046                         *storage = PMINFO_INTERNAL_STORAGE;
2047                  else if (strcmp(info->app_info->installed_storage, "installed_external") == 0)
2048                          *storage = PMINFO_EXTERNAL_STORAGE;
2049                  else
2050                          return PMINFO_R_ERROR;
2051         } else {
2052                 return PMINFO_R_ERROR;
2053         }
2054
2055         return PMINFO_R_OK;
2056 }
2057
2058 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
2059 {
2060         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2061
2062         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2063         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2064
2065         if (info->app_info->launch_mode == NULL)
2066                 return PMINFO_R_ERROR;
2067
2068         *mode = (char *)(info->app_info->launch_mode);
2069
2070         return PMINFO_R_OK;
2071 }
2072
2073 API int pkgmgrinfo_appinfo_get_alias_appid(pkgmgrinfo_appinfo_h handle, char **alias_appid)
2074 {
2075         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2076
2077         if (handle == NULL || alias_appid == NULL) {
2078                 LOGE("invalid parameter");
2079                 return PMINFO_R_EINVAL;
2080         }
2081
2082         if (info->app_info == NULL || info->app_info->alias_appid == NULL)
2083                 return PMINFO_R_ERROR;
2084
2085         *alias_appid = (char *)info->app_info->alias_appid;
2086
2087         return PMINFO_R_OK;
2088 }
2089
2090 API int pkgmgrinfo_appinfo_get_effective_appid(pkgmgrinfo_appinfo_h handle, char **effective_appid)
2091 {
2092         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2093
2094         if (handle == NULL || effective_appid == NULL) {
2095                 LOGE("invalid parameter");
2096                 return PMINFO_R_EINVAL;
2097         }
2098
2099         if (info->app_info == NULL)
2100                 return PMINFO_R_ERROR;
2101
2102         if (info->app_info->effective_appid == NULL)
2103                 *effective_appid = "";
2104         else
2105                 *effective_appid = (char *)info->app_info->effective_appid;
2106
2107         return PMINFO_R_OK;
2108 }
2109
2110 API int pkgmgrinfo_appinfo_get_tep_name(pkgmgrinfo_appinfo_h handle, char **tep_name)
2111 {
2112         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2113
2114         if (handle == NULL || tep_name == NULL) {
2115                 LOGE("invalid parameter");
2116                 return PMINFO_R_EINVAL;
2117         }
2118
2119         if (info->app_info == NULL)
2120                 return PMINFO_R_ERROR;
2121
2122         if (info->app_info->tep_name == NULL)
2123                 *tep_name = "";
2124         else
2125                 *tep_name = (char *)info->app_info->tep_name;
2126
2127         return PMINFO_R_OK;
2128 }
2129
2130 API int pkgmgrinfo_appinfo_get_zip_mount_file(pkgmgrinfo_appinfo_h handle, char **zip_mount_file)
2131 {
2132         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2133
2134         if (handle == NULL || zip_mount_file == NULL) {
2135                 LOGE("invalid parameter");
2136                 return PMINFO_R_EINVAL;
2137         }
2138
2139         if (info->app_info == NULL)
2140                 return PMINFO_R_ERROR;
2141
2142         if (info->app_info->zip_mount_file == NULL)
2143                 *zip_mount_file = "";
2144         else
2145                 *zip_mount_file = (char *)info->app_info->zip_mount_file;
2146
2147         return PMINFO_R_OK;
2148 }
2149
2150 API int pkgmgrinfo_appinfo_get_root_path(pkgmgrinfo_appinfo_h handle, char **root_path)
2151 {
2152         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2153
2154         if (handle == NULL || root_path == NULL) {
2155                 LOGE("invalid parameter");
2156                 return PMINFO_R_EINVAL;
2157         }
2158
2159         if (info->app_info == NULL || info->app_info->root_path == NULL)
2160                 return PMINFO_R_ERROR;
2161
2162         *root_path = (char *)info->app_info->root_path;
2163
2164         return PMINFO_R_OK;
2165 }
2166
2167 API int pkgmgrinfo_appinfo_get_api_version(pkgmgrinfo_appinfo_h handle, char **api_version)
2168 {
2169         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2170
2171         if (handle == NULL || api_version == NULL) {
2172                 LOGE("invalid parameter");
2173                 return PMINFO_R_EINVAL;
2174         }
2175
2176         if (info->app_info == NULL || info->app_info->api_version == NULL)
2177                 return PMINFO_R_ERROR;
2178
2179         *api_version = (char *)info->app_info->api_version;
2180
2181         return PMINFO_R_OK;
2182 }
2183
2184 API int pkgmgrinfo_appinfo_get_installed_time(pkgmgrinfo_appinfo_h handle, int *installed_time)
2185 {
2186         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2187
2188         if (handle == NULL || installed_time == NULL) {
2189                 LOGE("invalid parameter");
2190                 return PMINFO_R_EINVAL;
2191         }
2192
2193         if (info->app_info == NULL || info->app_info->package_installed_time == NULL)
2194                 return PMINFO_R_ERROR;
2195
2196         *installed_time = atoi(info->app_info->package_installed_time);
2197
2198         return PMINFO_R_OK;
2199 }
2200
2201 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid,
2202                 const char *type, uid_t uid, char **appid, char **access)
2203 {
2204         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
2205         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
2206         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2207         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2208
2209         int ret = PMINFO_R_OK;
2210         char *query = NULL;
2211         sqlite3_stmt *stmt = NULL;
2212
2213         /*open db*/
2214         ret = __open_manifest_db(uid, true);
2215         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
2216
2217         /*Start constructing query*/
2218         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
2219         tryvm_if(query == NULL, ret = PMINFO_R_ERROR, "Out of memory");
2220
2221         /*prepare query*/
2222         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
2223         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
2224
2225         /*step query*/
2226         ret = sqlite3_step(stmt);
2227         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
2228
2229         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
2230         *access = strdup((char *)sqlite3_column_text(stmt, 2));
2231
2232         ret = PMINFO_R_OK;
2233
2234 catch:
2235         sqlite3_free(query);
2236         sqlite3_finalize(stmt);
2237         __close_manifest_db();
2238         return ret;
2239 }
2240
2241 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid,
2242                 const char *type, char **appid, char **access)
2243 {
2244         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid,
2245                         type, _getuid(), appid, access);
2246 }
2247
2248 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
2249 {
2250         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
2251         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2252
2253         int ret = PMINFO_R_OK;
2254         char *query = NULL;
2255         sqlite3_stmt *stmt = NULL;
2256
2257         /*open db*/
2258         ret = __open_manifest_db(uid, true);
2259         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
2260
2261         /*Start constructing query*/
2262         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
2263         tryvm_if(query == NULL, ret = PMINFO_R_ERROR, "Out of memory");
2264
2265         /*prepare query*/
2266         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
2267         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
2268
2269         /*step query*/
2270         ret = sqlite3_step(stmt);
2271         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
2272
2273         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
2274
2275         ret = PMINFO_R_OK;
2276
2277 catch:
2278         sqlite3_free(query);
2279         sqlite3_finalize(stmt);
2280         __close_manifest_db();
2281         return ret;
2282 }
2283
2284 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
2285 {
2286         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, _getuid(), appid);
2287 }
2288
2289 API int pkgmgrinfo_appinfo_usr_get_datacontrol_trusted_info(const char *providerid,
2290                 const char *type, uid_t uid, char **appid, bool *is_trusted)
2291 {
2292         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
2293         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2294
2295         int ret = PMINFO_R_OK;
2296         char *query = NULL;
2297         sqlite3_stmt *stmt = NULL;
2298
2299         /*open db*/
2300         ret = __open_manifest_db(uid, true);
2301         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
2302
2303         /*Start constructing query*/
2304         query = sqlite3_mprintf("SELECT app_id, trusted FROM package_app_data_control WHERE providerid=%Q AND type=%Q", providerid, type);
2305         tryvm_if(query == NULL, ret = PMINFO_R_ERROR, "Out of memory");
2306
2307         /*prepare query*/
2308         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
2309         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
2310
2311         /*step query*/
2312         ret = sqlite3_step(stmt);
2313         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
2314
2315         _save_column_str(stmt, 0, appid);
2316         *is_trusted = _get_bool_value((char *)sqlite3_column_text(stmt, 1));
2317
2318         ret = PMINFO_R_OK;
2319
2320 catch:
2321         sqlite3_free(query);
2322         sqlite3_finalize(stmt);
2323         __close_manifest_db();
2324         return ret;
2325 }
2326
2327 API int pkgmgrinfo_appinfo_get_datacontrol_trsuted_info(const char *providerid,
2328                 const char *type, char **appid, bool *is_trusted)
2329 {
2330         return pkgmgrinfo_appinfo_usr_get_datacontrol_trusted_info(providerid,
2331                         type, _getuid(), appid, is_trusted);
2332 }
2333
2334 API int pkgmgrinfo_appinfo_usr_foreach_datacontrol_privileges(const char *providerid, const char *type,
2335                 pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data, uid_t uid)
2336 {
2337         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
2338         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2339
2340         int ret = PMINFO_R_OK;
2341         char *query = NULL;
2342         sqlite3_stmt *stmt = NULL;
2343
2344         /*open db*/
2345         ret = __open_manifest_db(uid, true);
2346         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
2347
2348         /*Start constructing query*/
2349         query = sqlite3_mprintf("SELECT privilege FROM package_app_data_control_privilege "
2350                         "WHERE providerid=%Q AND type=%Q", providerid, type);
2351         tryvm_if(query == NULL, ret = PMINFO_R_ERROR, "Out of memory");
2352
2353         /*prepare query*/
2354         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
2355         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
2356
2357         while (sqlite3_step(stmt) == SQLITE_ROW) {
2358                 char *privilege;
2359                 privilege = (char *)sqlite3_column_text(stmt, 0);
2360                 ret = privilege_func(privilege, user_data);
2361                 if (ret < 0)
2362                         break;
2363         }
2364
2365         ret = PMINFO_R_OK;
2366
2367 catch:
2368         sqlite3_free(query);
2369         sqlite3_finalize(stmt);
2370         __close_manifest_db();
2371         return ret;
2372 }
2373
2374 API int pkgmgrinfo_appinfo_foreach_datacontrol_privileges(const char *providerid, const char *type,
2375                 pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
2376 {
2377         return pkgmgrinfo_appinfo_usr_foreach_datacontrol_privileges(providerid, type, privilege_func,
2378                         user_data, _getuid());
2379 }
2380
2381 API int pkgmgrinfo_appinfo_get_support_mode(pkgmgrinfo_appinfo_h  handle, int *support_mode)
2382 {
2383         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2384         retvm_if(support_mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2385
2386         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2387         if (info->app_info->support_mode)
2388                 *support_mode = atoi(info->app_info->support_mode);
2389         else
2390                 *support_mode = 0;
2391
2392         return PMINFO_R_OK;
2393 }
2394
2395 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
2396                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
2397 {
2398         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2399         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2400         int ret = -1;
2401         const char *category;
2402         GList *tmp;
2403         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2404
2405         if (info->app_info == NULL)
2406                 return PMINFO_R_ERROR;
2407
2408         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2409                 category = (const char *)tmp->data;
2410                 if (category) {
2411                         ret = category_func(category, user_data);
2412                         if (ret < 0)
2413                                 break;
2414                 }
2415         }
2416         return PMINFO_R_OK;
2417 }
2418
2419 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
2420                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
2421 {
2422         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2423         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2424         int ret = -1;
2425         metadata_x *ptr;
2426         GList *tmp;
2427         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2428
2429         if (info->app_info == NULL)
2430                 return PMINFO_R_ERROR;
2431
2432         for (tmp = info->app_info->metadata; tmp; tmp = tmp->next) {
2433                 ptr = (metadata_x *)tmp->data;
2434                 if (ptr == NULL)
2435                         continue;
2436                 if (ptr->key) {
2437                         ret = metadata_func(ptr->key, ptr->value ? ptr->value : "", user_data);
2438                         if (ret < 0)
2439                                 break;
2440                 }
2441         }
2442         return PMINFO_R_OK;
2443 }
2444
2445 API int pkgmgrinfo_appinfo_usr_foreach_appcontrol_privileges(const char *appid,
2446                 const char *operation, const char *uri, const char *mime,
2447                 pkgmgrinfo_pkg_privilege_list_cb privilege_func,
2448                 void *user_data, uid_t uid)
2449 {
2450         static const char query_raw[] =
2451                 "SELECT privilege FROM package_app_app_control_privilege "
2452                 "WHERE app_id=%Q AND app_control=%Q";
2453         int ret;
2454         char *query;
2455         sqlite3_stmt *stmt;
2456         char *privilege;
2457         char app_control[BUFSIZE];
2458
2459         if (appid == NULL || operation == NULL || privilege_func == NULL) {
2460                 LOGE("invalid parameter");
2461                 return PMINFO_R_EINVAL;
2462         }
2463
2464         snprintf(app_control, sizeof(app_control), "%s|%s|%s", operation,
2465                         uri ? uri : "NULL", mime ? mime : "NULL");
2466
2467         ret = __open_manifest_db(uid, true);
2468         if (ret != SQLITE_OK) {
2469                 LOGE("open db failed");
2470                 return PMINFO_R_ERROR;
2471         }
2472
2473         query = sqlite3_mprintf(query_raw, appid, app_control);
2474         if (query == NULL) {
2475                 LOGE("out of memory");
2476                 __close_manifest_db();
2477                 return PMINFO_R_ERROR;
2478         }
2479
2480         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
2481                         &stmt, NULL);
2482         sqlite3_free(query);
2483         if (ret != PMINFO_R_OK) {
2484                 LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
2485                 __close_manifest_db();
2486                 return PMINFO_R_ERROR;
2487         }
2488
2489         while (sqlite3_step(stmt) == SQLITE_ROW) {
2490                 privilege = (char *)sqlite3_column_text(stmt, 0);
2491                 ret = privilege_func(privilege, user_data);
2492                 if (ret < 0)
2493                         break;
2494         }
2495
2496         sqlite3_finalize(stmt);
2497         __close_manifest_db();
2498
2499         return PMINFO_R_OK;
2500 }
2501
2502 API int pkgmgrinfo_appinfo_foreach_appcontrol_privileges(const char *appid,
2503                 const char *operation, const char *uri, const char *mime,
2504                 pkgmgrinfo_pkg_privilege_list_cb privilege_func,
2505                 void *user_data)
2506 {
2507         return pkgmgrinfo_appinfo_usr_foreach_appcontrol_privileges(appid,
2508                         operation, uri, mime, privilege_func, user_data,
2509                         _getuid());
2510 }
2511
2512 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
2513                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
2514 {
2515         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2516         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2517         int ret;
2518         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2519         appcontrol_x *appcontrol;
2520         GList *tmp;
2521
2522         if (info->app_info == NULL)
2523                 return PMINFO_R_ERROR;
2524
2525         for (tmp = info->app_info->appcontrol; tmp; tmp = tmp->next) {
2526                 appcontrol = (appcontrol_x *)tmp->data;
2527                 if (appcontrol == NULL)
2528                         continue;
2529                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
2530                 if (ret < 0)
2531                         break;
2532         }
2533
2534         return PMINFO_R_OK;
2535 }
2536
2537 API int pkgmgrinfo_appinfo_foreach_background_category(
2538                 pkgmgrinfo_appinfo_h handle,
2539                 pkgmgrinfo_app_background_category_list_cb category_func,
2540                 void *user_data)
2541 {
2542         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2543         GList *tmp;
2544         char *category;
2545
2546         if (handle == NULL || category_func == NULL || info->app_info == NULL) {
2547                 LOGE("invalid parameter");
2548                 return PMINFO_R_EINVAL;
2549         }
2550
2551         for (tmp = info->app_info->background_category; tmp; tmp = tmp->next) {
2552                 category = (char *)tmp->data;
2553                 if (category == NULL)
2554                         continue;
2555
2556                 if (category_func(category, user_data) < 0)
2557                         break;
2558         }
2559
2560         return PMINFO_R_OK;
2561 }
2562
2563 API int pkgmgrinfo_appinfo_foreach_splash_screen(pkgmgrinfo_appinfo_h handle,
2564                 pkgmgrinfo_app_splash_screen_list_cb splash_screen_func,
2565                 void *user_data)
2566 {
2567         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2568         splashscreen_x *splashscreen;
2569         GList *tmp;
2570         int ret;
2571
2572         if (info == NULL || info->app_info == NULL
2573                         || splash_screen_func == NULL) {
2574                 LOGE("invalid parameter");
2575                 return PMINFO_R_EINVAL;
2576         }
2577
2578         for (tmp = info->app_info->splashscreens; tmp; tmp = tmp->next) {
2579                 splashscreen = (splashscreen_x *)tmp->data;
2580                 if (splashscreen == NULL)
2581                         continue;
2582                 ret = splash_screen_func(splashscreen->src,
2583                                 splashscreen->type,
2584                                 splashscreen->orientation,
2585                                 splashscreen->indicatordisplay,
2586                                 splashscreen->operation,
2587                                 splashscreen->color_depth,
2588                                 user_data);
2589                 if (ret < 0)
2590                         break;
2591         }
2592
2593         return PMINFO_R_OK;
2594 }
2595
2596 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
2597 {
2598         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2599         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2600         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2601
2602         if (info->app_info == NULL || info->app_info->nodisplay == NULL)
2603                 return PMINFO_R_ERROR;
2604
2605         *nodisplay = _get_bool_value(info->app_info->nodisplay);
2606
2607         return PMINFO_R_OK;
2608 }
2609
2610 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
2611 {
2612         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2613         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2614         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2615
2616         if (info->app_info == NULL || info->app_info->multiple == NULL)
2617                 return PMINFO_R_ERROR;
2618
2619         *multiple = _get_bool_value(info->app_info->multiple);
2620
2621         return PMINFO_R_OK;
2622 }
2623
2624 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
2625 {
2626         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2627         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2628         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2629
2630         if (info->app_info == NULL || info->app_info->indicatordisplay == NULL)
2631                 return PMINFO_R_ERROR;
2632
2633         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
2634
2635         return PMINFO_R_OK;
2636 }
2637
2638 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
2639 {
2640         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2641         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2642         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2643
2644         if (info->app_info == NULL || info->app_info->taskmanage == NULL)
2645                 return PMINFO_R_ERROR;
2646
2647         *taskmanage = _get_bool_value(info->app_info->taskmanage);
2648
2649         return PMINFO_R_OK;
2650 }
2651
2652 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
2653 {
2654         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2655         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2656         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2657
2658         if (info->app_info == NULL || info->app_info->is_disabled == NULL)
2659                 return PMINFO_R_ERROR;
2660
2661         *enabled = !_get_bool_value(info->app_info->is_disabled);
2662
2663         return PMINFO_R_OK;
2664 }
2665
2666 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
2667 {
2668         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2669         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2670         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2671
2672         if (info->app_info == NULL || info->app_info->onboot == NULL)
2673                 return PMINFO_R_ERROR;
2674
2675         *onboot = _get_bool_value(info->app_info->onboot);
2676
2677         return PMINFO_R_OK;
2678 }
2679
2680 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
2681 {
2682         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2683         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2684         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2685
2686         if (info->app_info == NULL || info->app_info->autorestart == NULL)
2687                 return PMINFO_R_ERROR;
2688
2689         *autorestart = _get_bool_value(info->app_info->autorestart);
2690
2691         return PMINFO_R_OK;
2692 }
2693
2694 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
2695 {
2696         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2697         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2698         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2699
2700         if (info->app_info == NULL || info->app_info->mainapp == NULL)
2701                 return PMINFO_R_ERROR;
2702
2703         *mainapp = _get_bool_value(info->app_info->mainapp);
2704
2705         return PMINFO_R_OK;
2706 }
2707
2708 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
2709 {
2710         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2711         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2712         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2713
2714         if (info->app_info == NULL || info->app_info->preload == NULL)
2715                 return PMINFO_R_ERROR;
2716
2717         *preload = _get_bool_value(info->app_info->preload);
2718
2719         return PMINFO_R_OK;
2720 }
2721
2722 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
2723 {
2724         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2725         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2726         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2727
2728         if (info->app_info == NULL || info->app_info->submode == NULL)
2729                 return PMINFO_R_ERROR;
2730
2731         *submode = _get_bool_value(info->app_info->submode);
2732
2733         return PMINFO_R_OK;
2734 }
2735
2736 API int pkgmgrinfo_appinfo_is_process_pool(pkgmgrinfo_appinfo_h handle, bool *process_pool)
2737 {
2738         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2739
2740         if (handle == NULL || process_pool == NULL) {
2741                 LOGE("invalid parameter");
2742                 return PMINFO_R_EINVAL;
2743         }
2744
2745         if (info->app_info == NULL)
2746                 return PMINFO_R_ERROR;
2747
2748         *process_pool = _get_bool_value(info->app_info->process_pool);
2749
2750         return PMINFO_R_OK;
2751 }
2752
2753 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
2754 {
2755         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2756         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
2757         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
2758
2759         const char *val;
2760         GList *tmp;
2761         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2762
2763         if (info->app_info == NULL)
2764                 return PMINFO_R_ERROR;
2765
2766         *exist = 0;
2767         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2768                 val = (const char *)tmp->data;
2769                 if (val == NULL)
2770                         continue;
2771                 if (strcasecmp(val, category) == 0) {
2772                         *exist = 1;
2773                         break;
2774                 }
2775         }
2776
2777         return PMINFO_R_OK;
2778 }
2779
2780 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
2781                 bool *ui_gadget)
2782 {
2783         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2784
2785         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
2786                 _LOGE("invalid parameter");
2787                 return PMINFO_R_EINVAL;
2788         }
2789         if (info->app_info->ui_gadget == NULL)
2790                 info->app_info->ui_gadget = strdup("false");
2791
2792         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
2793
2794         return PMINFO_R_OK;
2795 }
2796
2797 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
2798                 bool *support_disable)
2799 {
2800         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2801
2802         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
2803                 _LOGE("invalid parameter");
2804                 return PMINFO_R_EINVAL;
2805         }
2806
2807         *support_disable = _get_bool_value(info->app_info->support_disable);
2808
2809         return PMINFO_R_OK;
2810 }
2811
2812 API int pkgmgrinfo_appinfo_is_removable(pkgmgrinfo_appinfo_h handle,
2813                 bool *removable)
2814 {
2815         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2816
2817         if (info == NULL || info->app_info == NULL || removable == NULL) {
2818                 _LOGE("invalid parameter");
2819                 return PMINFO_R_EINVAL;
2820         }
2821
2822         *removable = _get_bool_value(info->app_info->removable);
2823
2824         return PMINFO_R_OK;
2825 }
2826
2827 API int pkgmgrinfo_appinfo_is_system(pkgmgrinfo_appinfo_h handle, bool *system)
2828 {
2829         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2830
2831         if (info == NULL || info->app_info == NULL || system == NULL) {
2832                 _LOGE("invalid parameter");
2833                 return PMINFO_R_EINVAL;
2834         }
2835
2836         *system = _get_bool_value(info->app_info->package_system);
2837
2838         return PMINFO_R_OK;
2839 }
2840
2841 API int pkgmgrinfo_appinfo_is_disabled(pkgmgrinfo_appinfo_h handle, bool *disabled)
2842 {
2843         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2844         retvm_if(disabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2845         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2846
2847         if (info->app_info == NULL || info->app_info->is_disabled == NULL)
2848                 return PMINFO_R_ERROR;
2849
2850         *disabled = _get_bool_value(info->app_info->is_disabled);
2851
2852         return PMINFO_R_OK;
2853 }
2854
2855 API int pkgmgrinfo_appinfo_is_global(pkgmgrinfo_appinfo_h handle, bool *global)
2856 {
2857         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2858
2859         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2860         retvm_if(global == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2861
2862         if (info->app_info == NULL || info->app_info->for_all_users == NULL)
2863                 return PMINFO_R_ERROR;
2864
2865         *global = _get_bool_value(info->app_info->for_all_users);
2866
2867         return PMINFO_R_OK;
2868 }
2869
2870 API int pkgmgrinfo_appinfo_get_splash_screen_display(pkgmgrinfo_appinfo_h handle, bool *splash_screen_display)
2871 {
2872         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2873
2874         if (info == NULL || splash_screen_display == NULL) {
2875                 _LOGE("Invalid parameter");
2876                 return PMINFO_R_EINVAL;
2877         }
2878
2879         if (info->app_info == NULL || info->app_info->splash_screen_display == NULL)
2880                 return PMINFO_R_ERROR;
2881
2882         *splash_screen_display = _get_bool_value(info->app_info->splash_screen_display);
2883
2884         return PMINFO_R_OK;
2885 }
2886
2887 API int pkgmgrinfo_appinfo_get_setup_appid(pkgmgrinfo_appinfo_h handle, char **setup_appid)
2888 {
2889         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2890
2891         if (info == NULL || setup_appid == NULL) {
2892                 _LOGE("Invalid parameter");
2893                 return PMINFO_R_EINVAL;
2894         }
2895
2896         if (info->app_info == NULL || info->app_info->setup_appid == NULL)
2897                 return PMINFO_R_ERROR;
2898
2899         *setup_appid = info->app_info->setup_appid;
2900         return PMINFO_R_OK;
2901 }
2902
2903 API int pkgmgrinfo_appinfo_is_support_ambient(pkgmgrinfo_appinfo_h handle,
2904                 bool *support_ambient)
2905 {
2906         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2907
2908         if (info == NULL || support_ambient == NULL) {
2909                 _LOGE("Invalid parameter");
2910                 return PMINFO_R_EINVAL;
2911         }
2912
2913         if (info->app_info == NULL || info->app_info->support_ambient == NULL)
2914                 return PMINFO_R_ERROR;
2915
2916         *support_ambient = _get_bool_value(info->app_info->support_ambient);
2917
2918         return PMINFO_R_OK;
2919 }
2920
2921 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
2922 {
2923         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2924         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2925         __cleanup_appinfo(info);
2926         return PMINFO_R_OK;
2927 }
2928
2929 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
2930 {
2931         return (pkgmgrinfo_pkginfo_filter_create(handle));
2932 }
2933
2934 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
2935 {
2936         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2937 }
2938
2939 static gint __compare_func(gconstpointer data1, gconstpointer data2)
2940 {
2941         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x *)data1;
2942         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x *)data2;
2943         if (node1->prop == node2->prop)
2944                 return 0;
2945         else if (node1->prop > node2->prop)
2946                 return 1;
2947         else
2948                 return -1;
2949 }
2950
2951 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
2952                                 const char *property, const int value)
2953 {
2954         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2955         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2956         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
2957         char *val = NULL;
2958         GSList *link = NULL;
2959         int prop = -1;
2960         prop = _pminfo_appinfo_convert_to_prop_int(property);
2961         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
2962                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
2963                 _LOGE("Invalid Integer Property\n");
2964                 return PMINFO_R_EINVAL;
2965         }
2966         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
2967         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
2968         if (node == NULL) {
2969                 _LOGE("Out of Memory!!!\n");
2970                 return PMINFO_R_ERROR;
2971         }
2972         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
2973         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
2974         if (val == NULL) {
2975                 _LOGE("Out of Memory\n");
2976                 free(node);
2977                 node = NULL;
2978                 return PMINFO_R_ERROR;
2979         }
2980         node->prop = prop;
2981         node->value = val;
2982         /*If API is called multiple times for same property, we should override the previous values.
2983         Last value set will be used for filtering.*/
2984         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2985         if (link)
2986                 filter->list = g_slist_delete_link(filter->list, link);
2987         filter->list = g_slist_append(filter->list, (gpointer)node);
2988         return PMINFO_R_OK;
2989
2990 }
2991
2992 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
2993                                 const char *property, const bool value)
2994 {
2995         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2996         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2997         char *val = NULL;
2998         GSList *link = NULL;
2999         int prop = -1;
3000         prop = _pminfo_appinfo_convert_to_prop_bool(property);
3001         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
3002                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
3003                 _LOGE("Invalid Boolean Property\n");
3004                 return PMINFO_R_EINVAL;
3005         }
3006         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
3007         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
3008         if (node == NULL) {
3009                 _LOGE("Out of Memory!!!\n");
3010                 return PMINFO_R_ERROR;
3011         }
3012         if (value)
3013                 val = strndup("true", 4);
3014         else
3015                 val = strndup("false", 5);
3016         if (val == NULL) {
3017                 _LOGE("Out of Memory\n");
3018                 free(node);
3019                 node = NULL;
3020                 return PMINFO_R_ERROR;
3021         }
3022         node->prop = prop;
3023         node->value = val;
3024         /*If API is called multiple times for same property, we should override the previous values.
3025         Last value set will be used for filtering.*/
3026         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3027         if (link)
3028                 filter->list = g_slist_delete_link(filter->list, link);
3029         filter->list = g_slist_append(filter->list, (gpointer)node);
3030         return PMINFO_R_OK;
3031
3032 }
3033
3034 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
3035                                 const char *property, const char *value)
3036 {
3037         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3038         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3039         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
3040         char *val = NULL;
3041         pkgmgrinfo_node_x *ptr = NULL;
3042         char prev[PKG_STRING_LEN_MAX] = {'\0'};
3043         char temp[PKG_STRING_LEN_MAX] = {'\0'};
3044         GSList *link = NULL;
3045         int prop = -1;
3046         prop = _pminfo_appinfo_convert_to_prop_str(property);
3047         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
3048                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
3049                 _LOGE("Invalid String Property\n");
3050                 return PMINFO_R_EINVAL;
3051         }
3052         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
3053         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
3054         if (node == NULL) {
3055                 _LOGE("Out of Memory!!!\n");
3056                 return PMINFO_R_ERROR;
3057         }
3058         node->prop = prop;
3059         switch (prop) {
3060         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
3061                 node->value = strdup(value);
3062                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3063                 if (link)
3064                         filter->list = g_slist_delete_link(filter->list, link);
3065                 filter->list = g_slist_append(filter->list, (gpointer)node);
3066                 break;
3067         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
3068                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
3069                 if (val == NULL) {
3070                         _LOGE("Out of Memory\n");
3071                         free(node);
3072                         node = NULL;
3073                         return PMINFO_R_ERROR;
3074                 }
3075                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3076                 if (link) {
3077                         ptr = (pkgmgrinfo_node_x *)link->data;
3078                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
3079                         _LOGI("Previous value is %s\n", prev);
3080                         filter->list = g_slist_delete_link(filter->list, link);
3081                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s,%s", prev, value);
3082                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
3083                         _LOGI("New value is %s\n", val);
3084                         node->value = val;
3085                         filter->list = g_slist_append(filter->list, (gpointer)node);
3086                         memset(temp, '\0', PKG_STRING_LEN_MAX);
3087                 } else {
3088                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s", value);
3089                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
3090                         _LOGI("First value is %s\n", val);
3091                         node->value = val;
3092                         filter->list = g_slist_append(filter->list, (gpointer)node);
3093                         memset(temp, '\0', PKG_STRING_LEN_MAX);
3094                 }
3095                 break;
3096         default:
3097                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
3098                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3099                 if (link)
3100                         filter->list = g_slist_delete_link(filter->list, link);
3101                 filter->list = g_slist_append(filter->list, (gpointer)node);
3102                 break;
3103         }
3104         return PMINFO_R_OK;
3105 }
3106
3107 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
3108 {
3109         int ret;
3110         char *locale;
3111         GHashTable *list;
3112
3113         if (handle == NULL || count == NULL) {
3114                 _LOGE("invalid parameter");
3115                 return PMINFO_R_EINVAL;
3116         }
3117
3118         locale = _get_system_locale();
3119         if (locale == NULL)
3120                 return PMINFO_R_ERROR;
3121
3122         list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
3123                         __free_applications);
3124         if (list == NULL) {
3125                 free(locale);
3126                 return PMINFO_R_ERROR;
3127         }
3128
3129         if (__check_disable_filter_exist(
3130                         handle, E_APPINFO_DISABLE_TYPE_APP) == false) {
3131                 if (pkgmgrinfo_appinfo_filter_add_bool(handle,
3132                                 PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
3133                         g_hash_table_destroy(list);
3134                         free(locale);
3135                         return PMINFO_R_ERROR;
3136                 }
3137         }
3138
3139         if (__check_disable_filter_exist(
3140                         handle, E_APPINFO_DISABLE_TYPE_PKG) == false) {
3141                 if (pkgmgrinfo_appinfo_filter_add_bool(handle,
3142                                 PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
3143                         g_hash_table_destroy(list);
3144                         free(locale);
3145                         return PMINFO_R_ERROR;
3146                 }
3147         }
3148
3149         ret = _appinfo_get_applications(uid, uid, locale, handle, 0, list);
3150         if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
3151                 ret = _appinfo_get_applications(GLOBAL_USER, uid, locale,
3152                                 handle, 0, list);
3153
3154         if (ret != PMINFO_R_OK) {
3155                 g_hash_table_destroy(list);
3156                 free(locale);
3157                 return PMINFO_R_ERROR;
3158         }
3159
3160         *count = g_hash_table_size(list);
3161         g_hash_table_destroy(list);
3162         free(locale);
3163
3164         return PMINFO_R_OK;
3165 }
3166
3167 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
3168 {
3169         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, _getuid());
3170 }
3171
3172 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
3173                 pkgmgrinfo_appinfo_filter_h handle,
3174                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
3175 {
3176         if (handle == NULL || app_cb == NULL) {
3177                 LOGE("invalid parameter");
3178                 return PMINFO_R_EINVAL;
3179         }
3180
3181         if (__check_disable_filter_exist(
3182                         handle, E_APPINFO_DISABLE_TYPE_APP) == false) {
3183                 if (pkgmgrinfo_appinfo_filter_add_bool(handle,
3184                                 PMINFO_APPINFO_PROP_APP_DISABLE, false))
3185                         return PMINFO_R_ERROR;
3186         }
3187
3188         if (__check_disable_filter_exist(
3189                         handle, E_APPINFO_DISABLE_TYPE_PKG) == false) {
3190                 if (pkgmgrinfo_appinfo_filter_add_bool(handle,
3191                                 PMINFO_APPINFO_PROP_PKG_DISABLE, false))
3192                         return PMINFO_R_ERROR;
3193         }
3194
3195         return _appinfo_get_filtered_foreach_appinfo(uid, handle, PMINFO_APPINFO_GET_ALL, app_cb,
3196                         user_data);
3197 }
3198
3199 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
3200                                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
3201 {
3202         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, _getuid());
3203 }
3204
3205 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
3206 {
3207         return (pkgmgrinfo_pkginfo_filter_create(handle));
3208 }
3209
3210 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
3211 {
3212         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
3213 }
3214
3215 API int pkgmgrinfo_appinfo_metadata_filter_add(
3216                 pkgmgrinfo_appinfo_metadata_filter_h handle,
3217                 const char *key, const char *value)
3218 {
3219         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
3220         pkgmgrinfo_metadata_node_x *node;
3221
3222         /* value can be NULL.
3223          * In that case all apps with specified key should be displayed
3224          */
3225         if (key == NULL) {
3226                 LOGE("invalid parameter");
3227                 return PMINFO_R_EINVAL;
3228         }
3229
3230         node = calloc(1, sizeof(pkgmgrinfo_metadata_node_x));
3231         if (node == NULL) {
3232                 LOGE("out of memory");
3233                 return PMINFO_R_ERROR;
3234         }
3235
3236         node->key = strdup(key);
3237         if (value && strlen(value))
3238                 node->value = strdup(value);
3239
3240         filter->list_metadata = g_slist_append(filter->list_metadata,
3241                         (gpointer)node);
3242
3243         return PMINFO_R_OK;
3244 }
3245
3246 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
3247                 pkgmgrinfo_appinfo_metadata_filter_h handle,
3248                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
3249 {
3250         if (handle == NULL || app_cb == NULL) {
3251                 LOGE("invalid parameter");
3252                 return PMINFO_R_EINVAL;
3253         }
3254
3255         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
3256         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
3257                         PMINFO_APPINFO_PROP_APP_DISABLE, false))
3258                 return PMINFO_R_ERROR;
3259
3260         if (pkgmgrinfo_appinfo_filter_add_bool(filter,
3261                         PMINFO_APPINFO_PROP_PKG_DISABLE, false))
3262                 return PMINFO_R_ERROR;
3263
3264         return _appinfo_get_filtered_foreach_appinfo(uid, handle, PMINFO_APPINFO_GET_ALL, app_cb,
3265                         user_data);
3266 }
3267
3268 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
3269                 pkgmgrinfo_appinfo_metadata_filter_h handle,
3270                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
3271 {
3272         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
3273                         user_data, _getuid());
3274 }
3275
3276 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
3277 {
3278         const char *val;
3279         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
3280
3281         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
3282         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
3283
3284         val = info->app_info->guestmode_visibility;
3285         *status = _get_bool_value(val);
3286         return PMINFO_R_OK;
3287 }