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