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