Separate pkgmgr-info legacy library and implement dbhandler (#52)
[platform/core/appfw/pkgmgr-info.git] / src / appinfo_internal.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
19 static void __free_applications(gpointer data)
20 {
21         pkgmgrinfo_basic_free_application((application_x *)data);
22 }
23
24 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str,
25                 char *visibility, char *id)
26 {
27         char *dup;
28         char *token;
29         char *ptr = NULL;
30         appcontrol_x *ac;
31
32         if (appcontrol_str == NULL)
33                 return;
34
35         dup = strdup(appcontrol_str);
36         if (dup == NULL) {
37                 _LOGE("out of memory");
38                 return;
39         }
40
41         do {
42                 ac = calloc(1, sizeof(appcontrol_x));
43                 if (ac == NULL) {
44                         _LOGE("out of memory");
45                         break;
46                 }
47                 token = strtok_r(dup, "|", &ptr);
48                 if (token && strcmp(token, "NULL"))
49                         ac->operation = strdup(token);
50                 token = strtok_r(NULL, "|", &ptr);
51                 if (token && strcmp(token, "NULL"))
52                         ac->uri = strdup(token);
53                 token = strtok_r(NULL, "|", &ptr);
54                 if (token && strcmp(token, "NULL"))
55                         ac->mime = strdup(token);
56                 ac->visibility = strdup(visibility);
57                 ac->id = strdup(id);
58                 *appcontrol = g_list_prepend(*appcontrol, ac);
59         } while ((token = strtok_r(NULL, ";", &ptr)));
60
61         free(dup);
62 }
63
64 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
65                 GList **splashscreens)
66 {
67         static const char query_raw[] =
68                 "SELECT src, type, orientation, indicatordisplay, operation, color_depth "
69                 "FROM package_app_splash_screen WHERE app_id=%Q";
70         int ret;
71         char *query;
72         sqlite3_stmt *stmt;
73         int idx;
74         splashscreen_x *info;
75
76         query = sqlite3_mprintf(query_raw, appid);
77         if (query == NULL) {
78                 LOGE("out of memory");
79                 return PMINFO_R_ERROR;
80         }
81
82         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
83         sqlite3_free(query);
84         if (ret != SQLITE_OK) {
85                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
86                 return PMINFO_R_ERROR;
87         }
88
89         while (sqlite3_step(stmt) == SQLITE_ROW) {
90                 info = calloc(1, sizeof(splashscreen_x));
91                 if (info == NULL) {
92                         LOGE("out of memory");
93                         sqlite3_finalize(stmt);
94                         return PMINFO_R_ERROR;
95                 }
96                 idx = 0;
97                 _save_column_str(stmt, idx++, &info->src);
98                 _save_column_str(stmt, idx++, &info->type);
99                 _save_column_str(stmt, idx++, &info->orientation);
100                 _save_column_str(stmt, idx++, &info->indicatordisplay);
101                 _save_column_str(stmt, idx++, &info->operation);
102                 _save_column_str(stmt, idx++, &info->color_depth);
103                 *splashscreens = g_list_prepend(*splashscreens, info);
104         }
105
106         sqlite3_finalize(stmt);
107
108         return PMINFO_R_OK;
109 }
110
111 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
112                 GList **metadata)
113 {
114         static const char query_raw[] =
115                 "SELECT md_key, md_value "
116                 "FROM package_app_app_metadata WHERE app_id=%Q";
117         int ret;
118         char *query;
119         sqlite3_stmt *stmt;
120         int idx;
121         metadata_x *info;
122
123         query = sqlite3_mprintf(query_raw, appid);
124         if (query == NULL) {
125                 LOGE("out of memory");
126                 return PMINFO_R_ERROR;
127         }
128
129         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
130         sqlite3_free(query);
131         if (ret != SQLITE_OK) {
132                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
133                 return PMINFO_R_ERROR;
134         }
135
136         while (sqlite3_step(stmt) == SQLITE_ROW) {
137                 info = calloc(1, sizeof(metadata_x));
138                 if (info == NULL) {
139                         LOGE("out of memory");
140                         sqlite3_finalize(stmt);
141                         return PMINFO_R_ERROR;
142                 }
143                 idx = 0;
144                 _save_column_str(stmt, idx++, &info->key);
145                 _save_column_str(stmt, idx++, &info->value);
146                 *metadata = g_list_prepend(*metadata, info);
147         }
148
149         sqlite3_finalize(stmt);
150
151         return PMINFO_R_OK;
152
153 }
154
155 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
156                 GList **appcontrol)
157 {
158         static const char query_raw[] =
159                 "SELECT app_control, visibility, app_control_id "
160                 "FROM package_app_app_control WHERE app_id=%Q";
161         int ret;
162         int idx;
163         char *query;
164         sqlite3_stmt *stmt;
165         char *str;
166         char *visibility;
167         char *id;
168
169         query = sqlite3_mprintf(query_raw, appid);
170         if (query == NULL) {
171                 LOGE("out of memory");
172                 return PMINFO_R_ERROR;
173         }
174
175         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
176         sqlite3_free(query);
177         if (ret != SQLITE_OK) {
178                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
179                 return PMINFO_R_ERROR;
180         }
181
182         while (sqlite3_step(stmt) == SQLITE_ROW) {
183                 str = NULL;
184                 visibility = NULL;
185                 id = NULL;
186                 idx = 0;
187                 _save_column_str(stmt, idx++, &str);
188                 _save_column_str(stmt, idx++, &visibility);
189                 _save_column_str(stmt, idx++, &id);
190                 /* TODO: revise */
191                 __parse_appcontrol(appcontrol, str, visibility, id);
192                 free(str);
193                 free(visibility);
194                 free(id);
195         }
196
197         sqlite3_finalize(stmt);
198
199         return PMINFO_R_OK;
200 }
201
202 static int _appinfo_get_category(sqlite3 *db, const char *appid,
203                 GList **category)
204 {
205         static const char query_raw[] =
206                 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
207         int ret;
208         char *query;
209         sqlite3_stmt *stmt;
210         char *val;
211
212         query = sqlite3_mprintf(query_raw, appid);
213         if (query == NULL) {
214                 LOGE("out of memory");
215                 return PMINFO_R_ERROR;
216         }
217
218         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
219         sqlite3_free(query);
220         if (ret != SQLITE_OK) {
221                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
222                 return PMINFO_R_ERROR;
223         }
224
225         while (sqlite3_step(stmt) == SQLITE_ROW) {
226                 val = NULL;
227                 _save_column_str(stmt, 0, &val);
228                 if (val)
229                         *category = g_list_prepend(*category, (gpointer)val);
230         }
231
232         sqlite3_finalize(stmt);
233
234         return PMINFO_R_OK;
235 }
236
237 static GList *__get_background_category(const char *value)
238 {
239         GList *category_list = NULL;
240         int convert_value = 0;
241         if (!value || strlen(value) == 0)
242                 return NULL;
243
244         convert_value = atoi(value);
245         if (convert_value < 0)
246                 return NULL;
247
248         if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
249                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
250         else
251                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
252
253         if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
254                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
255
256         if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
257                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
258
259         if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
260                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
261
262         if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
263                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
264
265         if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
266                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
267
268         if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
269                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
270
271         if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
272                 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
273
274         return category_list;
275
276 }
277
278 static int __bind_params(sqlite3_stmt *stmt, GList *params)
279 {
280         GList *tmp_list = NULL;
281         int idx = 0;
282         int ret;
283
284         if (stmt == NULL || params == NULL)
285                 return PMINFO_R_EINVAL;
286
287         tmp_list = params;
288         while (tmp_list) {
289                 ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
290                 if (ret != SQLITE_OK)
291                         return PMINFO_R_ERROR;
292                 tmp_list = tmp_list->next;
293         }
294
295         return PMINFO_R_OK;
296 }
297
298 static const char join_localized_info[] =
299         " LEFT OUTER JOIN package_app_localized_info"
300         "  ON ai.app_id=package_app_localized_info.app_id"
301         "  AND package_app_localized_info.app_locale=?";
302 static const char join_category[] =
303         " LEFT OUTER JOIN package_app_app_category"
304         " ON ai.app_id=package_app_app_category.app_id";
305 static const char join_app_control[] =
306         " LEFT OUTER JOIN package_app_app_control"
307         "  ON ai.app_id=package_app_app_control.app_id";
308 static const char join_metadata[] =
309         " LEFT OUTER JOIN package_app_app_metadata"
310         "  ON ai.app_id=package_app_app_metadata.app_id ";
311 static const char join_privilege[] =
312         " LEFT OUTER JOIN package_privilege_info"
313         " ON ai.package=package_privilege_info.package ";
314
315 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
316         const char *locale, uid_t uid, char **query, GList **bind_params)
317 {
318         int joined = 0;
319         int size;
320         char *condition = NULL;
321         char buf[MAX_QUERY_LEN] = { '\0' };
322         char tmp_query[MAX_QUERY_LEN] = { '\0' };
323         GSList *list;
324
325         if (!filter)
326                 return PMINFO_R_OK;
327         strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
328
329         for (list = filter->list; list; list = list->next) {
330                 joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
331                 if (condition == NULL)
332                         continue;
333
334                 strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
335
336                 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
337                 free(condition);
338                 condition = NULL;
339         }
340
341         if (filter->list_metadata)
342                 strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
343         for (list = filter->list_metadata; list; list = list->next) {
344                 joined |= __get_metadata_filter_condition(list->data,
345                                 &condition, bind_params);
346                 if (condition == NULL)
347                         continue;
348                 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
349                 free(condition);
350                 condition = NULL;
351
352                 strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
353         }
354         if (filter->list_metadata)
355                 strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
356
357         if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
358                 strncat(tmp_query, join_localized_info, sizeof(tmp_query) - strlen(tmp_query) - 1);
359                 *bind_params = g_list_append(*bind_params, strdup(locale));
360         }
361         if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
362                 strncat(tmp_query, join_category, sizeof(tmp_query) - strlen(tmp_query) - 1);
363         if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
364                 strncat(tmp_query, join_app_control, sizeof(tmp_query) - strlen(tmp_query) - 1);
365         if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
366                 strncat(tmp_query, join_metadata, sizeof(tmp_query) - strlen(tmp_query) - 1);
367         if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
368                 strncat(tmp_query, join_privilege, sizeof(tmp_query) - strlen(tmp_query) - 1);
369
370         size = strlen(tmp_query) + strlen(buf) + 1;
371         *query = (char *)calloc(1, size);
372         if (*query == NULL)
373                 return PMINFO_R_ERROR;
374         snprintf(*query, size, "%s%s", tmp_query, buf);
375
376         return PMINFO_R_OK;
377 }
378
379 static bool __check_app_storage_status(pkgmgrinfo_filter_x *tmp_filter)
380 {
381         GSList *tmp_list = NULL;
382         pkgmgrinfo_node_x *tmp_node = NULL;
383         int property = -1;
384
385         if (tmp_filter == NULL)
386                 return true;
387
388         property = _pminfo_appinfo_convert_to_prop_bool(PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
389         for (tmp_list = tmp_filter->list; tmp_list != NULL;
390                         tmp_list = g_slist_next(tmp_list)) {
391                 tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
392                 if (property == tmp_node->prop) {
393                         if (strcmp(tmp_node->value, "true") == 0)
394                                 return true;
395                         else
396                                 return false;
397                 }
398         }
399
400         return true;
401 }
402
403 static int _appinfo_get_applications(sqlite3 *db, uid_t db_uid, uid_t uid,
404                 const char *locale, pkgmgrinfo_filter_x *filter, int flag,
405                 GHashTable *applications)
406 {
407         static const char query_raw[] =
408                 "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
409                 "ai.app_external_path";
410         static const char query_basic[] =
411                 ", ai.app_component, ai.app_exec, "
412                 "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
413                 "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
414                 "ai.app_hwacceleration, ai.app_screenreader, "
415                 "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
416                 "ai.app_indicatordisplay, ai.app_portraitimg, "
417                 "ai.app_landscapeimg, ai.app_guestmodevisibility, "
418                 "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
419                 "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
420                 "ai.app_support_disable, ai.app_process_pool, "
421                 "ai.app_background_category, ai.app_package_type, "
422                 "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
423                 "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
424                 "ai.app_zip_mount_file, ai.component_type, ai.package, "
425                 "ai.app_package_system, ai.app_removable, "
426                 "ai.app_package_installed_time, ai.app_support_mode, "
427                 "ai.app_support_ambient, ai.app_setup_appid";
428         static const char query_uid_info[] =
429                 ", ui.is_disabled, ui.is_splash_screen_enabled";
430         static const char query_label[] =
431                 ", COALESCE("
432                 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
433                 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
434         static const char query_icon[] =
435                 ", COALESCE("
436                 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
437                 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
438         static const char query_from_clause[] = " FROM package_app_info as ai";
439         static const char query_uid_info_clause[] =
440                 " LEFT OUTER JOIN package_app_info_for_uid AS ui "
441                 "ON (ai.app_id=ui.app_id AND ui.uid=?)";
442         int ret = PMINFO_R_ERROR;
443         int idx;
444         char *bg_category_str = NULL;
445         char *constraint = NULL;
446         char *tmp_record = NULL;
447         char query[MAX_QUERY_LEN] = { '\0' };
448         char buf[BUFSIZE] = { '\0' };
449         application_x *info = NULL;
450         GList *bind_params = NULL;
451         sqlite3_stmt *stmt = NULL;
452         bool is_check_storage = true;
453         const uid_t global_user_uid = GLOBAL_USER;
454
455         snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
456
457         if (flag & PMINFO_APPINFO_GET_BASICINFO) {
458                 strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
459                 strncat(query, query_uid_info, sizeof(query) - strlen(query) - 1);
460         }
461         if (flag & PMINFO_APPINFO_GET_LABEL) {
462                 strncat(query, query_label, sizeof(query) - strlen(query) - 1);
463                 bind_params = g_list_append(bind_params, strdup(locale));
464         }
465         if (flag & PMINFO_APPINFO_GET_ICON) {
466                 strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
467                 bind_params = g_list_append(bind_params, strdup(locale));
468         }
469
470         snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
471         bind_params = g_list_append(bind_params, strdup(buf));
472
473         is_check_storage = __check_app_storage_status(filter);
474
475         ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
476         if (ret != PMINFO_R_OK) {
477                 LOGE("Failed to get WHERE clause");
478                 goto catch;
479         }
480         strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
481
482         strncat(query, query_uid_info_clause, sizeof(query) - strlen(query) - 1);
483
484         if (constraint)
485                 strncat(query, constraint, sizeof(query) - strlen(query) - 1);
486
487         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
488         if (ret != SQLITE_OK) {
489                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
490                 ret = PMINFO_R_ERROR;
491                 goto catch;
492         }
493
494         if (g_list_length(bind_params) != 0) {
495                 ret = __bind_params(stmt, bind_params);
496                 if (ret != SQLITE_OK) {
497                         LOGE("Failed to bind parameters");
498                         goto catch;
499                 }
500         }
501
502         while (sqlite3_step(stmt) == SQLITE_ROW) {
503                 info = calloc(1, sizeof(application_x));
504                 if (info == NULL) {
505                         LOGE("out of memory");
506                         ret = PMINFO_R_ERROR;
507                         goto catch;
508                 }
509                 idx = 0;
510                 _save_column_str(stmt, idx++, &info->appid);
511                 _save_column_str(stmt, idx++, &info->installed_storage);
512                 _save_column_str(stmt, idx++ , &info->external_path);
513
514                 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
515                         _save_column_str(stmt, idx++, &info->component);
516                         _save_column_str(stmt, idx++, &info->exec);
517                         _save_column_str(stmt, idx++, &info->nodisplay);
518                         _save_column_str(stmt, idx++, &info->type);
519                         _save_column_str(stmt, idx++, &info->onboot);
520                         _save_column_str(stmt, idx++, &info->multiple);
521                         _save_column_str(stmt, idx++, &info->autorestart);
522                         _save_column_str(stmt, idx++, &info->taskmanage);
523                         _save_column_str(stmt, idx++, &info->hwacceleration);
524                         _save_column_str(stmt, idx++, &info->screenreader);
525                         _save_column_str(stmt, idx++, &info->mainapp);
526                         _save_column_str(stmt, idx++, &info->recentimage);
527                         _save_column_str(stmt, idx++, &info->launchcondition);
528                         _save_column_str(stmt, idx++, &info->indicatordisplay);
529                         _save_column_str(stmt, idx++, &info->portraitimg);
530                         _save_column_str(stmt, idx++, &info->landscapeimg);
531                         _save_column_str(stmt, idx++, &info->guestmode_visibility);
532                         _save_column_str(stmt, idx++, &info->permission_type);
533                         _save_column_str(stmt, idx++, &info->preload);
534                         _save_column_str(stmt, idx++, &info->submode);
535                         _save_column_str(stmt, idx++, &info->submode_mainid);
536                         _save_column_str(stmt, idx++, &info->launch_mode);
537                         _save_column_str(stmt, idx++, &info->ui_gadget);
538                         _save_column_str(stmt, idx++, &info->support_disable);
539                         _save_column_str(stmt, idx++, &info->process_pool);
540                         _save_column_str(stmt, idx++, &bg_category_str);
541                         _save_column_str(stmt, idx++, &info->package_type);
542                         _save_column_str(stmt, idx++, &info->root_path);
543                         _save_column_str(stmt, idx++, &info->api_version);
544                         _save_column_str(stmt, idx++, &info->effective_appid);
545                         _save_column_str(stmt, idx++, &info->is_disabled);
546                         _save_column_str(stmt, idx++, &info->splash_screen_display);
547                         _save_column_str(stmt, idx++, &info->tep_name);
548                         _save_column_str(stmt, idx++, &info->zip_mount_file);
549                         _save_column_str(stmt, idx++, &info->component_type);
550                         _save_column_str(stmt, idx++, &info->package);
551                         _save_column_str(stmt, idx++, &info->package_system);
552                         _save_column_str(stmt, idx++, &info->removable);
553                         _save_column_str(stmt, idx++, &info->package_installed_time);
554                         _save_column_str(stmt, idx++, &info->support_mode);
555                         _save_column_str(stmt, idx++, &info->support_ambient);
556                         _save_column_str(stmt, idx++, &info->setup_appid);
557                         info->background_category = __get_background_category(
558                                         bg_category_str);
559                         free(bg_category_str);
560                         bg_category_str = NULL;
561                 }
562
563                 info->for_all_users =
564                         strdup((db_uid != global_user_uid) ? "false" : "true");
565
566                 if (db_uid != global_user_uid) {
567                         idx = idx + 2;
568                 } else {
569                         tmp_record = NULL;
570                         _save_column_str(stmt, idx++, &tmp_record);
571                         if (tmp_record != NULL) {
572                                 if (strcasecmp(info->is_disabled, "false") == 0 &&
573                                                 strcasecmp(tmp_record, "false") == 0) {
574                                         free(info->is_disabled);
575                                         info->is_disabled = tmp_record;
576                                 } else {
577                                         free(tmp_record);
578                                 }
579                         }
580                         tmp_record = NULL;
581                         _save_column_str(stmt, idx++, &tmp_record);
582                         if (tmp_record != NULL) {
583                                 if (strcasecmp(info->splash_screen_display, "false") == 0 &&
584                                                 strcasecmp(tmp_record, "false") == 0) {
585                                         free(info->splash_screen_display);
586                                         info->splash_screen_display = tmp_record;
587                                 } else {
588                                         free(tmp_record);
589                                 }
590                         }
591                 }
592
593                 if (flag & PMINFO_APPINFO_GET_LABEL) {
594                         tmp_record = NULL;
595                         _save_column_str(stmt, idx++, &tmp_record);
596                         if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
597                                 ret = PMINFO_R_ERROR;
598                                 goto catch;
599                         }
600                 }
601
602                 if (flag & PMINFO_APPINFO_GET_ICON) {
603                         tmp_record = NULL;
604                         _save_column_str(stmt, idx++, &tmp_record);
605                         if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
606                                 ret = PMINFO_R_ERROR;
607                                 goto catch;
608                         }
609                 }
610
611                 if (flag & PMINFO_APPINFO_GET_CATEGORY) {
612                         if (_appinfo_get_category(db, info->appid,
613                                                 &info->category)) {
614                                 ret = PMINFO_R_ERROR;
615                                 goto catch;
616                         }
617                 }
618
619                 if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
620                         if (_appinfo_get_app_control(db, info->appid,
621                                                 &info->appcontrol)) {
622                                 ret = PMINFO_R_ERROR;
623                                 goto catch;
624                         }
625                 }
626
627                 if (flag & PMINFO_APPINFO_GET_METADATA) {
628                         if (_appinfo_get_metadata(db, info->appid,
629                                                 &info->metadata)) {
630                                 ret = PMINFO_R_ERROR;
631                                 goto catch;
632                         }
633                 }
634
635                 if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
636                         if (_appinfo_get_splashscreens(db, info->appid,
637                                                 &info->splashscreens)) {
638                                 ret = PMINFO_R_ERROR;
639                                 goto catch;
640                         }
641                 }
642
643                 if (is_check_storage &&
644                                 __appinfo_check_installed_storage(info) != PMINFO_R_OK) {
645                         ret = PMINFO_R_ERROR;
646                         pkgmgrinfo_basic_free_application(info);
647                         info = NULL;
648                         continue;
649                 }
650
651                 g_hash_table_insert(applications, (gpointer)info->appid,
652                                 (gpointer)info);
653         }
654
655         ret = PMINFO_R_OK;
656
657 catch:
658         sqlite3_finalize(stmt);
659
660         if (constraint)
661                 free(constraint);
662
663         if (ret != PMINFO_R_OK && info != NULL)
664                 pkgmgrinfo_basic_free_application(info);
665
666         g_list_free_full(bind_params, free);
667
668         return ret;
669 }
670
671 API int appinfo_internal_filter_get_list(
672                 sqlite3 *db,
673                 pkgmgrinfo_appinfo_filter_h filter,
674                 uid_t uid, GHashTable **appinfo_list) {
675         int ret;
676         char *locale;
677         GHashTable *list;
678
679         if (db == NULL || filter == NULL || appinfo_list == NULL) {
680                         // TODO: add log
681                         return PMINFO_R_EINVAL;
682         }
683
684         locale = _get_system_locale();
685         if (locale == NULL)
686                 return PMINFO_R_ERROR;
687
688         list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
689                         __free_applications);
690         if (list == NULL) {
691                 free(locale);
692                 return PMINFO_R_ERROR;
693         }
694
695         ret = _appinfo_get_applications(db, uid, uid, locale, filter,
696                         PMINFO_APPINFO_GET_ALL, list);
697         if (!g_hash_table_size(list) && uid != GLOBAL_USER)
698                 ret = _appinfo_get_applications(db, GLOBAL_USER, uid, locale, filter,
699                                 PMINFO_APPINFO_GET_ALL, list);
700
701         if (ret != PMINFO_R_OK) {
702                 g_hash_table_destroy(list);
703                 free(locale);
704                 return ret;
705         }
706
707         if (!g_hash_table_size(list)) {
708                 // TODO: add logs that failed to find appid
709                 g_hash_table_destroy(list);
710                 free(locale);
711                 return PMINFO_R_ENOENT;
712         }
713
714         *appinfo_list = list;
715         // TODO: locale should be delivered
716         free(locale);
717         return ret;
718 }