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