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