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