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