Refactor pkgmgr-info
[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   GSList* list;
386
387   if (!filter)
388     return PMINFO_R_OK;
389
390   if (g_slist_length(filter->list) == 0) {
391     joined = E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO |
392       E_PMINFO_APPINFO_JOIN_CATEGORY |
393       E_PMINFO_APPINFO_JOIN_APP_CONTROL |
394       E_PMINFO_APPINFO_JOIN_METADATA |
395       E_PMINFO_APPINFO_JOIN_PRIVILEGE;
396   }
397
398   strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
399
400   for (list = filter->list; list; list = list->next) {
401     joined |= __get_filter_condition(list->data,
402         uid, &condition, bind_params);
403     if (condition == nullptr)
404       continue;
405
406     strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
407
408     strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
409     free(condition);
410     condition = nullptr;
411   }
412
413   if (filter->list_metadata)
414     strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
415   for (list = filter->list_metadata; list; list = list->next) {
416     joined |= __get_metadata_filter_condition(list->data,
417         &condition, bind_params);
418     if (condition == nullptr)
419       continue;
420     strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
421     free(condition);
422     condition = nullptr;
423
424     strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
425   }
426   if (filter->list_metadata)
427     strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
428
429   if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
430     strncat(tmp_query, join_localized_info,
431         sizeof(tmp_query) - strlen(tmp_query) - 1);
432     *bind_params = g_list_append(*bind_params, strdup(locale));
433   }
434   if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
435     strncat(tmp_query, join_category,
436         sizeof(tmp_query) - strlen(tmp_query) - 1);
437   if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
438     strncat(tmp_query, join_app_control,
439         sizeof(tmp_query) - strlen(tmp_query) - 1);
440   if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
441     strncat(tmp_query, join_metadata,
442         sizeof(tmp_query) - strlen(tmp_query) - 1);
443   if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
444     strncat(tmp_query, join_privilege,
445         sizeof(tmp_query) - strlen(tmp_query) - 1);
446
447   size = strlen(tmp_query) + strlen(buf) + 1;
448   *query = static_cast<char*>(calloc(1, size));
449   if (*query == nullptr)
450     return PMINFO_R_ERROR;
451   snprintf(*query, size, "%s%s", tmp_query, buf);
452
453   return PMINFO_R_OK;
454 }
455
456 bool __check_app_storage_status(pkgmgrinfo_filter_x* tmp_filter) {
457   GSList* tmp_list = nullptr;
458   pkgmgrinfo_node_x* tmp_node = nullptr;
459   int property = -1;
460
461   if (tmp_filter == nullptr)
462     return true;
463
464   property = _pminfo_appinfo_convert_to_prop_bool(
465       PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
466   for (tmp_list = tmp_filter->list; tmp_list != nullptr;
467       tmp_list = g_slist_next(tmp_list)) {
468     tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
469     if (property == tmp_node->prop) {
470       if (strcmp(tmp_node->value, "true") == 0)
471         return true;
472       else
473         return false;
474     }
475   }
476
477   return true;
478 }
479
480 int _appinfo_get_applications(sqlite3* db, uid_t db_uid, uid_t uid,
481     const char* locale, pkgmgrinfo_filter_x* filter, int flag,
482     std::vector<std::shared_ptr<application_x>>& applications) {
483   static const char query_raw[] =
484       "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
485       "ai.app_external_path";
486   static const char query_basic[] =
487       ", ai.app_component, ai.app_exec, "
488       "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
489       "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
490       "ai.app_hwacceleration, ai.app_screenreader, "
491       "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
492       "ai.app_indicatordisplay, ai.app_portraitimg, "
493       "ai.app_landscapeimg, ai.app_guestmodevisibility, "
494       "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
495       "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
496       "ai.app_support_disable, ai.app_process_pool, "
497       "ai.app_background_category, ai.app_package_type, "
498       "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
499       "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
500       "ai.app_zip_mount_file, ai.component_type, ai.package, "
501       "ai.app_package_system, ai.app_removable, "
502       "ai.app_package_installed_time, ai.app_support_mode, "
503       "ai.app_support_ambient, ai.app_setup_appid";
504   static const char query_uid_info[] =
505       ", ui.is_disabled, ui.is_splash_screen_enabled";
506   static const char query_label[] =
507       ", COALESCE("
508       "(SELECT app_label FROM package_app_localized_info WHERE "
509       "ai.app_id=app_id AND app_locale=?), "
510       "(SELECT app_label FROM package_app_localized_info WHERE "
511       "ai.app_id=app_id AND app_locale='No Locale'))";
512   static const char query_icon[] =
513       ", COALESCE("
514       "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id "
515       "AND app_locale=?), "
516       "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id "
517       "AND app_locale='No Locale'))";
518   static const char query_from_clause[] = " FROM package_app_info as ai";
519   static const char query_uid_info_clause[] =
520       " LEFT OUTER JOIN package_app_info_for_uid AS ui "
521       "ON (ai.app_id=ui.app_id AND ui.uid=?)";
522   int ret = PMINFO_R_ERROR;
523   int idx;
524   char* bg_category_str = nullptr;
525   char* constraint = nullptr;
526   char* tmp_record = nullptr;
527   char query[MAX_QUERY_LEN] = {'\0'};
528   char buf[BUFSIZE] = {'\0'};
529   application_x* info = nullptr;
530   GList* bind_params = nullptr;
531   sqlite3_stmt* stmt = nullptr;
532   bool is_check_storage = true;
533   const uid_t global_user_uid = GLOBAL_USER;
534
535   snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
536
537   if (flag & PMINFO_APPINFO_GET_BASICINFO) {
538     strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
539     strncat(query, query_uid_info,
540         sizeof(query) - strlen(query) - 1);
541   }
542   if (flag & PMINFO_APPINFO_GET_LABEL) {
543     strncat(query, query_label, sizeof(query) - strlen(query) - 1);
544     bind_params = g_list_append(bind_params, strdup(locale));
545   }
546   if (flag & PMINFO_APPINFO_GET_ICON) {
547     strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
548     bind_params = g_list_append(bind_params, strdup(locale));
549   }
550
551   snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
552   bind_params = g_list_append(bind_params, strdup(buf));
553
554   is_check_storage = __check_app_storage_status(filter);
555
556   ret = _get_filtered_query(filter, locale,
557       uid, &constraint, &bind_params);
558   if (ret != PMINFO_R_OK) {
559     LOGE("Failed to get WHERE clause");
560     goto __catch;
561   }
562   strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
563
564   strncat(query, query_uid_info_clause,
565       sizeof(query) - strlen(query) - 1);
566
567   if (constraint)
568     strncat(query, constraint, sizeof(query) - strlen(query) - 1);
569
570   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
571   if (ret != SQLITE_OK) {
572     LOGE("prepare failed: %s", sqlite3_errmsg(db));
573     ret = PMINFO_R_ERROR;
574     goto __catch;
575   }
576
577   if (g_list_length(bind_params) != 0) {
578     ret = __bind_params(stmt, bind_params);
579     if (ret != SQLITE_OK) {
580       LOGE("Failed to bind parameters");
581       goto __catch;
582     }
583   }
584
585   while (sqlite3_step(stmt) == SQLITE_ROW) {
586     info = static_cast<application_x*>(calloc(1, sizeof(application_x)));
587     if (info == nullptr) {
588       LOGE("out of memory");
589       ret = PMINFO_R_ERROR;
590       goto __catch;
591     }
592     info->locale = strdup(locale);
593     if (info->locale == nullptr) {
594       LOGE("Out of memory");
595       ret = PMINFO_R_ERROR;
596       goto __catch;
597     }
598
599     idx = 0;
600     _save_column_str(stmt, idx++, &info->appid);
601     _save_column_str(stmt, idx++, &info->installed_storage);
602     _save_column_str(stmt, idx++, &info->external_path);
603
604     if (flag & PMINFO_APPINFO_GET_BASICINFO) {
605       _save_column_str(stmt, idx++, &info->component);
606       _save_column_str(stmt, idx++, &info->exec);
607       _save_column_str(stmt, idx++, &info->nodisplay);
608       _save_column_str(stmt, idx++, &info->type);
609       _save_column_str(stmt, idx++, &info->onboot);
610       _save_column_str(stmt, idx++, &info->multiple);
611       _save_column_str(stmt, idx++, &info->autorestart);
612       _save_column_str(stmt, idx++, &info->taskmanage);
613       _save_column_str(stmt, idx++, &info->hwacceleration);
614       _save_column_str(stmt, idx++, &info->screenreader);
615       _save_column_str(stmt, idx++, &info->mainapp);
616       _save_column_str(stmt, idx++, &info->recentimage);
617       _save_column_str(stmt, idx++, &info->launchcondition);
618       _save_column_str(stmt, idx++, &info->indicatordisplay);
619       _save_column_str(stmt, idx++, &info->portraitimg);
620       _save_column_str(stmt, idx++, &info->landscapeimg);
621       _save_column_str(stmt, idx++,
622           &info->guestmode_visibility);
623       _save_column_str(stmt, idx++, &info->permission_type);
624       _save_column_str(stmt, idx++, &info->preload);
625       _save_column_str(stmt, idx++, &info->submode);
626       _save_column_str(stmt, idx++, &info->submode_mainid);
627       _save_column_str(stmt, idx++, &info->launch_mode);
628       _save_column_str(stmt, idx++, &info->ui_gadget);
629       _save_column_str(stmt, idx++, &info->support_disable);
630       _save_column_str(stmt, idx++, &info->process_pool);
631       _save_column_str(stmt, idx++, &bg_category_str);
632       _save_column_str(stmt, idx++, &info->package_type);
633       _save_column_str(stmt, idx++, &info->root_path);
634       _save_column_str(stmt, idx++, &info->api_version);
635       _save_column_str(stmt, idx++, &info->effective_appid);
636       _save_column_str(stmt, idx++, &info->is_disabled);
637       _save_column_str(stmt, idx++,
638           &info->splash_screen_display);
639       _save_column_str(stmt, idx++, &info->tep_name);
640       _save_column_str(stmt, idx++, &info->zip_mount_file);
641       _save_column_str(stmt, idx++, &info->component_type);
642       _save_column_str(stmt, idx++, &info->package);
643       _save_column_str(stmt, idx++, &info->package_system);
644       _save_column_str(stmt, idx++, &info->removable);
645       _save_column_str(stmt, idx++,
646           &info->package_installed_time);
647       _save_column_str(stmt, idx++, &info->support_mode);
648       _save_column_str(stmt, idx++, &info->support_ambient);
649       _save_column_str(stmt, idx++, &info->setup_appid);
650       info->background_category = __get_background_category(
651             bg_category_str);
652       free(bg_category_str);
653       bg_category_str = nullptr;
654     }
655
656     info->for_all_users =
657         strdup((db_uid != global_user_uid) ?
658             "false" : "true");
659
660     if (db_uid != global_user_uid) {
661       idx = idx + 2;
662     } else {
663       tmp_record = nullptr;
664       _save_column_str(stmt, idx++, &tmp_record);
665       if (tmp_record != nullptr) {
666         if (strcasecmp(info->is_disabled, "false") == 0 &&
667             strcasecmp(tmp_record, "false") == 0) {
668           free(info->is_disabled);
669           info->is_disabled = tmp_record;
670         } else {
671           free(tmp_record);
672         }
673       }
674       tmp_record = nullptr;
675       _save_column_str(stmt, idx++, &tmp_record);
676       if (tmp_record != nullptr) {
677         if (strcasecmp(info->splash_screen_display, "false") == 0 &&
678             strcasecmp(tmp_record, "false") == 0) {
679           free(info->splash_screen_display);
680           info->splash_screen_display = tmp_record;
681         } else {
682           free(tmp_record);
683         }
684       }
685     }
686
687     if (flag & PMINFO_APPINFO_GET_LABEL) {
688       tmp_record = nullptr;
689       _save_column_str(stmt, idx++, &tmp_record);
690       if (_add_label_info_into_list(locale, tmp_record,
691           &info->label)) {
692         ret = PMINFO_R_ERROR;
693         goto __catch;
694       }
695     }
696
697     if (flag & PMINFO_APPINFO_GET_ICON) {
698       tmp_record = nullptr;
699       _save_column_str(stmt, idx++, &tmp_record);
700       if (_add_icon_info_into_list(locale, tmp_record,
701           &info->icon)) {
702         ret = PMINFO_R_ERROR;
703         goto __catch;
704       }
705     }
706
707     if (flag & PMINFO_APPINFO_GET_CATEGORY) {
708       if (_appinfo_get_category(db, info->appid,
709           &info->category)) {
710         ret = PMINFO_R_ERROR;
711         goto __catch;
712       }
713     }
714
715     if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
716       if (_appinfo_get_app_control(db, info->appid,
717           &info->appcontrol)) {
718         ret = PMINFO_R_ERROR;
719         goto __catch;
720       }
721     }
722
723     if (flag & PMINFO_APPINFO_GET_METADATA) {
724       if (_appinfo_get_metadata(db, info->appid,
725           &info->metadata)) {
726         ret = PMINFO_R_ERROR;
727         goto __catch;
728       }
729     }
730
731     if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
732       if (_appinfo_get_splashscreens(db, info->appid,
733           &info->splashscreens)) {
734         ret = PMINFO_R_ERROR;
735         goto __catch;
736       }
737     }
738
739     if (flag & PMINFO_APPINFO_GET_RES_CONTROL) {
740       if (_appinfo_get_res_control(db, info->appid,
741           &info->res_control)) {
742         ret = PMINFO_R_ERROR;
743         goto __catch;
744       }
745     }
746
747     if (is_check_storage &&
748         __appinfo_check_installed_storage(info) !=
749             PMINFO_R_OK) {
750       ret = PMINFO_R_ERROR;
751       pkgmgrinfo_basic_free_application(info);
752       info = nullptr;
753       continue;
754     }
755
756     applications.emplace_back(info, pkgmgrinfo_basic_free_application);
757   }
758
759   ret = PMINFO_R_OK;
760
761 __catch:
762   sqlite3_finalize(stmt);
763
764   if (constraint)
765     free(constraint);
766
767   if (ret != PMINFO_R_OK && info != nullptr)
768     pkgmgrinfo_basic_free_application(info);
769
770   g_list_free_full(bind_params, free);
771
772   return ret;
773 }
774
775 }  // namespace
776
777 namespace pkgmgr_server {
778 namespace internal {
779
780 API int appinfo_internal_filter_get_list(sqlite3* db,
781     pkgmgrinfo_appinfo_filter_h filter, uid_t db_uid, uid_t uid,
782     const char* locale,
783     std::vector<std::shared_ptr<application_x>>& appinfo_list) {
784   if (db == nullptr || filter == nullptr) {
785     LOGE("Invalid argument");
786     return PMINFO_R_EINVAL;
787   }
788
789   return ::_appinfo_get_applications(db, db_uid, uid, locale,
790       static_cast<pkgmgrinfo_filter_x*>(filter), PMINFO_APPINFO_GET_ALL,
791       appinfo_list);
792 }
793
794 }  // namesapce internal
795 }  // namesapce pkgmgr_server