Add initialization
[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   _LOGE("jungh move pkgid[%s]", appinfo->package);
33   return true;
34 }
35
36 void __free_packages(gpointer data) {
37   pkgmgrinfo_basic_free_package((package_x*)data);
38 }
39
40 }  // namespace
41
42 namespace pkgmgr_common {
43 namespace database {
44
45 PkgGetDBHandler::PkgGetDBHandler(uid_t uid, int pid)
46     : AbstractDBHandler(uid, pid), uid_(uid) {}
47
48 PkgGetDBHandler::~PkgGetDBHandler() {}
49
50 std::vector<package_x*> PkgGetDBHandler::GetPkgHandle() {
51   return std::move(handle_list_);
52 }
53
54 void PkgGetDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
55   filter_ = filter;
56 }
57
58 int PkgGetDBHandler::Execute() {
59   SetOpType(OPERATION_TYPE_READ);
60   SetDBType(DB_TYPE_FILE_PKGDB);
61   if (!Connect()) {
62     _LOGE("Failed to connect database");
63     return PMINFO_R_ERROR;
64   }
65
66   GHashTable* list = g_hash_table_new_full(g_str_hash, g_str_equal,
67       NULL, __free_packages);
68   std::vector<sqlite3*> conn_list = GetConnection();
69   int ret = PMINFO_R_OK;
70   for (auto& conn : conn_list) {
71     ret = pkginfo_internal_filter_get_list(conn, filter_, uid_,
72                                             GetLocale().c_str(), list);
73     if (ret == PMINFO_R_ERROR)
74       _LOGE("Failed to pkginfo_internal_filter_get_list (%d)", ret);
75       break;
76   }
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