Refactor pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / src / common / parcel / query_parcelable.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  * }
6  *
7  *  you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "query_parcelable.hh"
20
21 #include <vector>
22
23 #include "pkgmgrinfo_private.h"
24
25 namespace pkgmgr_common {
26 namespace parcel {
27
28 QueryParcelable::QueryParcelable()
29     : AbstractParcelable(0, ParcelableType::Query),
30     db_type_(AbstractDBHandler::DBType::DB_TYPE_NONE),
31     op_type_(AbstractDBHandler::OperationType::OPERATION_TYPE_NONE) {}
32
33
34 QueryParcelable::QueryParcelable(uid_t uid,
35     std::pair<int, std::vector<std::string>> query_args,
36     AbstractDBHandler::DBType db_type,
37     AbstractDBHandler::OperationType op_type)
38     : AbstractParcelable(0, ParcelableType::Query),
39     query_args_(std::vector<std::pair<int, std::vector<std::string>>>{query_args}),
40     db_type_(db_type), op_type_(op_type) {}
41
42 QueryParcelable::QueryParcelable(uid_t uid,
43     std::vector<std::pair<int, std::vector<std::string>>> query_args,
44     AbstractDBHandler::DBType db_type,
45     AbstractDBHandler::OperationType op_type)
46     : AbstractParcelable(uid, ParcelableType::Query),
47     query_args_(std::move(query_args)), db_type_(db_type), op_type_(op_type) {}
48
49 void QueryParcelable::WriteToParcel(tizen_base::Parcel* parcel) const {
50   AbstractParcelable::WriteToParcel(parcel);
51   WriteInt(parcel, query_args_.size());
52   for (const auto& query_info : query_args_) {
53     WriteInt(parcel, query_info.first);
54     WriteInt(parcel, query_info.second.size());
55     for (const auto& args : query_info.second)
56       parcel->WriteString(args);
57   }
58   WriteInt(parcel, db_type_);
59   WriteInt(parcel, op_type_);
60 }
61
62 void QueryParcelable::ReadFromParcel(tizen_base::Parcel* parcel) {
63   int query_size = 0;
64   int op_type = 0;
65   int db_type = 0;
66
67   AbstractParcelable::ReadFromParcel(parcel);
68   ReadInt(parcel, &query_size);
69   for (int i = 0; i < query_size; ++i) {
70     std::vector<std::string> args;
71     int index = -1;
72     int arg_cnt = 0;
73     ReadInt(parcel, &index);
74     ReadInt(parcel, &arg_cnt);
75
76     for (int j = 0; j < arg_cnt; ++j) {
77       args.push_back(parcel->ReadString());
78     }
79     query_args_.push_back(std::pair<int, std::vector<std::string>>(index, std::move(args)));
80   }
81   ReadInt(parcel, &db_type);
82   db_type_ = static_cast<AbstractDBHandler::DBType>(db_type);
83   ReadInt(parcel, &op_type);
84   op_type_ = static_cast<AbstractDBHandler::OperationType>(op_type);
85 }
86
87 const std::vector<std::pair<int, std::vector<std::string>>>& QueryParcelable::GetQueryArgs() {
88   return query_args_;
89 }
90
91 AbstractDBHandler::DBType QueryParcelable::GetDBType() {
92   return db_type_;
93 }
94
95 AbstractDBHandler::OperationType QueryParcelable::GetOpType() {
96   return op_type_;
97 }
98
99 }  // namespace parcel
100 }  // namespace pkgmgr_common