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