1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "printer_capability.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19
20 namespace OHOS::Print {
PrinterCapability()21 PrinterCapability::PrinterCapability() : colorMode_(0), duplexMode_(0),
22 hasResolution_(false), hasMargin_(false), hasOption_(false), option_("")
23 {
24 pageSizeList_.clear();
25 resolutionList_.clear();
26 minMargin_.Reset();
27 }
28
PrinterCapability(const PrinterCapability & right)29 PrinterCapability::PrinterCapability(const PrinterCapability &right)
30 {
31 colorMode_ = right.colorMode_;
32 duplexMode_ = right.duplexMode_;
33 pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
34 hasResolution_ = right.hasResolution_;
35 resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
36 hasMargin_ = right.hasMargin_;
37 minMargin_ = right.minMargin_;
38 hasOption_ = right.hasOption_;
39 option_= right.option_;
40 }
41
operator =(const PrinterCapability & right)42 PrinterCapability &PrinterCapability::operator=(const PrinterCapability &right)
43 {
44 if (this != &right) {
45 colorMode_ = right.colorMode_;
46 duplexMode_ = right.duplexMode_;
47 pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
48 hasResolution_ = right.hasResolution_;
49 resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
50 hasMargin_ = right.hasMargin_;
51 minMargin_ = right.minMargin_;
52 hasOption_ = right.hasOption_;
53 option_ = right.option_;
54 }
55 return *this;
56 }
57
~PrinterCapability()58 PrinterCapability::~PrinterCapability()
59 {
60 }
61
Reset()62 void PrinterCapability::Reset()
63 {
64 SetColorMode(0);
65 SetDuplexMode(0);
66 pageSizeList_.clear();
67 hasResolution_ = false;
68 resolutionList_.clear();
69 hasMargin_ = false;
70 minMargin_.Reset();
71 }
72
SetMinMargin(const PrintMargin & minMargin)73 void PrinterCapability::SetMinMargin(const PrintMargin &minMargin)
74 {
75 hasMargin_ = true;
76 minMargin_ = minMargin;
77 }
78
SetPageSize(const std::vector<PrintPageSize> & pageSizeList)79 void PrinterCapability::SetPageSize(const std::vector<PrintPageSize> &pageSizeList)
80 {
81 pageSizeList_.assign(pageSizeList.begin(), pageSizeList.end());
82 }
83
SetResolution(const std::vector<PrintResolution> & resolutionList)84 void PrinterCapability::SetResolution(const std::vector<PrintResolution> &resolutionList)
85 {
86 hasResolution_ = true;
87 resolutionList_.assign(resolutionList.begin(), resolutionList.end());
88 }
89
SetColorMode(uint32_t colorMode)90 void PrinterCapability::SetColorMode(uint32_t colorMode)
91 {
92 colorMode_ = colorMode;
93 }
94
SetDuplexMode(uint32_t duplexMode)95 void PrinterCapability::SetDuplexMode(uint32_t duplexMode)
96 {
97 duplexMode_ = duplexMode;
98 }
99
SetOption(const std::string & option)100 void PrinterCapability::SetOption(const std::string &option)
101 {
102 hasOption_ = true;
103 option_ = option;
104 }
105
HasMargin() const106 bool PrinterCapability::HasMargin() const
107 {
108 return hasMargin_;
109 }
110
GetMinMargin(PrintMargin & margin) const111 void PrinterCapability::GetMinMargin(PrintMargin &margin) const
112 {
113 margin = minMargin_;
114 }
115
GetPageSize(std::vector<PrintPageSize> & pageSizeList) const116 void PrinterCapability::GetPageSize(std::vector<PrintPageSize> &pageSizeList) const
117 {
118 pageSizeList.assign(pageSizeList_.begin(), pageSizeList_.end());
119 }
120
HasResolution() const121 bool PrinterCapability::HasResolution() const
122 {
123 return hasResolution_;
124 }
125
GetResolution(std::vector<PrintResolution> & resolutionList) const126 void PrinterCapability::GetResolution(std::vector<PrintResolution> &resolutionList) const
127 {
128 resolutionList.assign(resolutionList_.begin(), resolutionList_.end());
129 }
130
GetColorMode() const131 uint32_t PrinterCapability::GetColorMode() const
132 {
133 return colorMode_;
134 }
135
GetDuplexMode() const136 uint32_t PrinterCapability::GetDuplexMode() const
137 {
138 return duplexMode_;
139 }
140
HasOption() const141 bool PrinterCapability::HasOption() const
142 {
143 return hasOption_;
144 }
145
GetOption() const146 std::string PrinterCapability::GetOption() const
147 {
148 return option_;
149 }
150
ReadFromParcel(Parcel & parcel)151 bool PrinterCapability::ReadFromParcel(Parcel &parcel)
152 {
153 SetColorMode(parcel.ReadUint32());
154 SetDuplexMode(parcel.ReadUint32());
155
156 uint32_t vecSize = parcel.ReadUint32();
157
158 CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize);
159 std::vector<PrintPageSize> pageSizeList;
160 for (uint32_t index = 0; index < vecSize; index++) {
161 auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
162 if (pageSizePtr != nullptr) {
163 pageSizeList.emplace_back(*pageSizePtr);
164 }
165 }
166 SetPageSize(pageSizeList);
167
168 hasResolution_ = parcel.ReadBool();
169 if (hasResolution_) {
170 std::vector<PrintResolution> resolutionList;
171 vecSize = parcel.ReadUint32();
172 CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize);
173 for (uint32_t index = 0; index < vecSize; index++) {
174 auto resolutionPtr = PrintResolution::Unmarshalling(parcel);
175 if (resolutionPtr != nullptr) {
176 resolutionList.emplace_back(*resolutionPtr);
177 }
178 }
179 SetResolution(resolutionList);
180 }
181
182 hasMargin_ = parcel.ReadBool();
183 if (hasMargin_) {
184 auto marginPtr = PrintMargin::Unmarshalling(parcel);
185 if (marginPtr != nullptr) {
186 minMargin_ = *marginPtr;
187 }
188 }
189 hasOption_ = parcel.ReadBool();
190 if (hasOption_) {
191 SetOption(parcel.ReadString());
192 }
193 return true;
194 }
195
Marshalling(Parcel & parcel) const196 bool PrinterCapability::Marshalling(Parcel &parcel) const
197 {
198 parcel.WriteUint32(GetColorMode());
199 parcel.WriteUint32(GetDuplexMode());
200
201 uint32_t vecSize = static_cast<uint32_t>(pageSizeList_.size());
202 parcel.WriteUint32(vecSize);
203 for (uint32_t index = 0; index < vecSize; index++) {
204 pageSizeList_[index].Marshalling(parcel);
205 }
206
207 parcel.WriteBool(hasResolution_);
208 if (hasResolution_) {
209 vecSize = static_cast<uint32_t>(resolutionList_.size());
210 parcel.WriteUint32(vecSize);
211 for (uint32_t index = 0; index < vecSize; index++) {
212 resolutionList_[index].Marshalling(parcel);
213 }
214 }
215
216 parcel.WriteBool(hasMargin_);
217 if (hasMargin_) {
218 minMargin_.Marshalling(parcel);
219 }
220 parcel.WriteBool(hasOption_);
221 if (hasOption_) {
222 parcel.WriteString(GetOption());
223 }
224 return true;
225 }
226
Unmarshalling(Parcel & parcel)227 std::shared_ptr<PrinterCapability> PrinterCapability::Unmarshalling(Parcel &parcel)
228 {
229 auto nativeObj = std::make_shared<PrinterCapability>();
230 if (nativeObj != nullptr) {
231 nativeObj->ReadFromParcel(parcel);
232 }
233 return nativeObj;
234 }
235
Dump()236 void PrinterCapability::Dump()
237 {
238 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
239 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
240
241 for (auto pageItem : pageSizeList_) {
242 pageItem.Dump();
243 }
244
245 if (hasResolution_) {
246 for (auto resolutionItem : resolutionList_) {
247 resolutionItem.Dump();
248 }
249 }
250
251 if (hasMargin_) {
252 minMargin_.Dump();
253 }
254 if (hasOption_) {
255 PRINT_HILOGD("option: %{private}s", option_.c_str());
256 }
257 }
258 } // namespace OHOS::Print
259