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