Change logic of delivering db path and handle
[platform/core/appfw/pkgmgr-info.git] / src / common / database / appinfo_db_handler.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 "appinfo_db_handler.hh"
18
19 #include <vector>
20
21 #include "pkgmgrinfo_basic.h"
22 #include "pkgmgrinfo_internal.h"
23 #include "pkgmgrinfo_debug.h"
24
25 namespace {
26
27 gboolean _move_func(gpointer key, gpointer value, gpointer user_data) {
28   application_x* appinfo = static_cast<application_x*>(value);
29   std::vector<application_x*>* app_list =
30       static_cast<std::vector<application_x*>*>(user_data);
31   app_list->emplace_back(appinfo);
32   return true;
33 }
34
35 void __free_applications(gpointer data) {
36   pkgmgrinfo_basic_free_application((application_x*)data);
37 }
38
39 }  // namespace
40
41 namespace pkgmgr_common {
42 namespace database {
43
44 AppInfoDBHandler::AppInfoDBHandler(uid_t uid, int pid)
45     : AbstractDBHandler(uid, pid), uid_(uid) {}
46
47 AppInfoDBHandler::~AppInfoDBHandler() {}
48
49 std::vector<application_x*> AppInfoDBHandler::GetAppHandle() {
50   return std::move(handle_list_);
51 }
52
53 void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
54   filter_ = filter;
55 }
56
57 int AppInfoDBHandler::Execute() {
58   SetOpType(OPERATION_TYPE_READ);
59   SetDBType(DB_TYPE_FILE_PKGDB);
60   if (!Connect()) return PMINFO_R_ERROR;
61
62   GHashTable* list = g_hash_table_new_full(g_str_hash, g_str_equal,
63       NULL, __free_applications);
64   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
65   int ret = PMINFO_R_OK;
66   for (auto conn : conn_list) {
67     ret = appinfo_internal_filter_get_list(conn.first, filter_, conn.second,
68                                              GetLocale().c_str(), list);
69     if (ret == PMINFO_R_ERROR) {
70       _LOGE("Failed to appinfo_internal_filter_get_list (%d)", ret);
71       break;
72     }
73   }
74
75   if (g_hash_table_size(list) == 0)
76     ret = PMINFO_R_ENOENT;
77
78   if (ret == PMINFO_R_OK)
79     g_hash_table_foreach_steal(list, _move_func, &handle_list_);
80
81   g_hash_table_destroy(list);
82   return ret;
83 }
84
85 }  // namespace database
86 }  // namespace pkgmgr_common