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 #include "print_utils.h"
20
21 using json = nlohmann::json;
22 namespace OHOS::Print {
PrinterCapability()23 PrinterCapability::PrinterCapability()
24 : colorMode_(0),
25 duplexMode_(0),
26 hasResolution_(false),
27 hasSupportedColorMode_(false),
28 hasSupportedDuplexMode_(false),
29 hasSupportedMediaType_(false),
30 hasSupportedQuality_(false),
31 hasSupportedOrientation_(false),
32 hasMargin_(false),
33 hasOption_(false),
34 option_("")
35 {
36 pageSizeList_.clear();
37 resolutionList_.clear();
38 minMargin_.Reset();
39 supportedPageSizeList_.clear();
40 supportedColorModeList_.clear();
41 supportedDuplexModeList_.clear();
42 supportedMediaTypeList_.clear();
43 supportedQualityList_.clear();
44 supportedOrientationList_.clear();
45 printerAttr_group.clear();
46 }
47
PrinterCapability(const PrinterCapability & right)48 PrinterCapability::PrinterCapability(const PrinterCapability &right)
49 {
50 *this = right;
51 }
52
operator =(const PrinterCapability & right)53 PrinterCapability &PrinterCapability::operator=(const PrinterCapability &right)
54 {
55 if (this != &right) {
56 colorMode_ = right.colorMode_;
57 duplexMode_ = right.duplexMode_;
58 pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
59 hasResolution_ = right.hasResolution_;
60 resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
61 hasMargin_ = right.hasMargin_;
62 minMargin_ = right.minMargin_;
63 hasOption_ = right.hasOption_;
64 option_ = right.option_;
65 hasSupportedColorMode_ = right.hasSupportedColorMode_;
66 hasSupportedDuplexMode_ = right.hasSupportedDuplexMode_;
67 hasSupportedMediaType_ = right.hasSupportedMediaType_;
68 hasSupportedQuality_ = right.hasSupportedQuality_;
69 hasSupportedOrientation_ = right.hasSupportedOrientation_;
70 supportedPageSizeList_.assign(right.supportedPageSizeList_.begin(), right.supportedPageSizeList_.end());
71 supportedColorModeList_.assign(right.supportedColorModeList_.begin(), right.supportedColorModeList_.end());
72 supportedDuplexModeList_.assign(right.supportedDuplexModeList_.begin(), right.supportedDuplexModeList_.end());
73 supportedMediaTypeList_.assign(right.supportedMediaTypeList_.begin(), right.supportedMediaTypeList_.end());
74 supportedQualityList_.assign(right.supportedQualityList_.begin(), right.supportedQualityList_.end());
75 supportedOrientationList_.assign(right.supportedOrientationList_.begin(),
76 right.supportedOrientationList_.end());
77 }
78 return *this;
79 }
80
~PrinterCapability()81 PrinterCapability::~PrinterCapability() {}
82
Reset()83 void PrinterCapability::Reset()
84 {
85 SetColorMode(0);
86 SetDuplexMode(0);
87 pageSizeList_.clear();
88 hasResolution_ = false;
89 resolutionList_.clear();
90 hasMargin_ = false;
91 minMargin_.Reset();
92 hasSupportedColorMode_ = false;
93 hasSupportedDuplexMode_ = false;
94 hasSupportedMediaType_ = false;
95 hasSupportedQuality_ = false;
96 hasSupportedOrientation_ = false;
97 supportedPageSizeList_.clear();
98 supportedColorModeList_.clear();
99 supportedDuplexModeList_.clear();
100 supportedMediaTypeList_.clear();
101 supportedQualityList_.clear();
102 supportedOrientationList_.clear();
103 }
104
SetMinMargin(const PrintMargin & minMargin)105 void PrinterCapability::SetMinMargin(const PrintMargin &minMargin)
106 {
107 hasMargin_ = true;
108 minMargin_ = minMargin;
109 }
110
SetResolution(const std::vector<PrintResolution> & resolutionList)111 void PrinterCapability::SetResolution(const std::vector<PrintResolution> &resolutionList)
112 {
113 hasResolution_ = true;
114 resolutionList_.assign(resolutionList.begin(), resolutionList.end());
115 }
116
SetColorMode(uint32_t colorMode)117 void PrinterCapability::SetColorMode(uint32_t colorMode)
118 {
119 colorMode_ = colorMode;
120 }
121
SetDuplexMode(uint32_t duplexMode)122 void PrinterCapability::SetDuplexMode(uint32_t duplexMode)
123 {
124 duplexMode_ = duplexMode;
125 }
126
SetOption(const std::string & option)127 void PrinterCapability::SetOption(const std::string &option)
128 {
129 hasOption_ = true;
130 option_ = option;
131 }
132
HasMargin() const133 bool PrinterCapability::HasMargin() const
134 {
135 return hasMargin_;
136 }
137
GetMinMargin(PrintMargin & margin) const138 void PrinterCapability::GetMinMargin(PrintMargin &margin) const
139 {
140 margin = minMargin_;
141 }
142
GetPageSize(std::vector<PrintPageSize> & pageSizeList) const143 void PrinterCapability::GetPageSize(std::vector<PrintPageSize> &pageSizeList) const
144 {
145 pageSizeList.assign(pageSizeList_.begin(), pageSizeList_.end());
146 }
147
HasResolution() const148 bool PrinterCapability::HasResolution() const
149 {
150 return hasResolution_;
151 }
152
GetResolution(std::vector<PrintResolution> & resolutionList) const153 void PrinterCapability::GetResolution(std::vector<PrintResolution> &resolutionList) const
154 {
155 resolutionList.assign(resolutionList_.begin(), resolutionList_.end());
156 }
157
GetColorMode() const158 uint32_t PrinterCapability::GetColorMode() const
159 {
160 return colorMode_;
161 }
162
GetDuplexMode() const163 uint32_t PrinterCapability::GetDuplexMode() const
164 {
165 return duplexMode_;
166 }
167
HasOption() const168 bool PrinterCapability::HasOption() const
169 {
170 return hasOption_;
171 }
172
HasSupportedColorMode() const173 bool PrinterCapability::HasSupportedColorMode() const
174 {
175 return hasSupportedColorMode_;
176 }
177
HasSupportedDuplexMode() const178 bool PrinterCapability::HasSupportedDuplexMode() const
179 {
180 return hasSupportedDuplexMode_;
181 }
182
HasSupportedMediaType() const183 bool PrinterCapability::HasSupportedMediaType() const
184 {
185 return hasSupportedMediaType_;
186 }
187
HasSupportedQuality() const188 bool PrinterCapability::HasSupportedQuality() const
189 {
190 return hasSupportedQuality_;
191 }
192
HasSupportedOrientation() const193 bool PrinterCapability::HasSupportedOrientation() const
194 {
195 return hasSupportedOrientation_;
196 }
197
GetSupportedPageSize(std::vector<PrintPageSize> & supportedPageSizeList) const198 void PrinterCapability::GetSupportedPageSize(std::vector<PrintPageSize> &supportedPageSizeList) const
199 {
200 supportedPageSizeList.assign(supportedPageSizeList_.begin(), supportedPageSizeList_.end());
201 }
202
GetSupportedColorMode(std::vector<uint32_t> & supportedColorModeList) const203 void PrinterCapability::GetSupportedColorMode(std::vector<uint32_t> &supportedColorModeList) const
204 {
205 supportedColorModeList.assign(supportedColorModeList_.begin(), supportedColorModeList_.end());
206 }
207
GetSupportedDuplexMode(std::vector<uint32_t> & supportedDuplexModeList) const208 void PrinterCapability::GetSupportedDuplexMode(std::vector<uint32_t> &supportedDuplexModeList) const
209 {
210 supportedDuplexModeList.assign(supportedDuplexModeList_.begin(), supportedDuplexModeList_.end());
211 }
212
GetSupportedMediaType(std::vector<std::string> & supportedMediaTypeList) const213 void PrinterCapability::GetSupportedMediaType(std::vector<std::string> &supportedMediaTypeList) const
214 {
215 supportedMediaTypeList.assign(supportedMediaTypeList_.begin(), supportedMediaTypeList_.end());
216 }
217
GetSupportedQuality(std::vector<uint32_t> & supportedQualityList) const218 void PrinterCapability::GetSupportedQuality(std::vector<uint32_t> &supportedQualityList) const
219 {
220 supportedQualityList.assign(supportedQualityList_.begin(), supportedQualityList_.end());
221 }
222
GetSupportedOrientation(std::vector<uint32_t> & supportedOrientationList) const223 void PrinterCapability::GetSupportedOrientation(std::vector<uint32_t> &supportedOrientationList) const
224 {
225 supportedOrientationList.assign(supportedOrientationList_.begin(), supportedOrientationList_.end());
226 }
227
SetSupportedPageSize(const std::vector<PrintPageSize> & supportedPageSizeList)228 void PrinterCapability::SetSupportedPageSize(const std::vector<PrintPageSize> &supportedPageSizeList)
229 {
230 supportedPageSizeList_.assign(supportedPageSizeList.begin(), supportedPageSizeList.end());
231 }
232
SetSupportedColorMode(const std::vector<uint32_t> & supportedColorModeList)233 void PrinterCapability::SetSupportedColorMode(const std::vector<uint32_t> &supportedColorModeList)
234 {
235 hasSupportedColorMode_ = true;
236 supportedColorModeList_.assign(supportedColorModeList.begin(), supportedColorModeList.end());
237 }
238
SetSupportedDuplexMode(const std::vector<uint32_t> & supportedDuplexModeList)239 void PrinterCapability::SetSupportedDuplexMode(const std::vector<uint32_t> &supportedDuplexModeList)
240 {
241 hasSupportedDuplexMode_ = true;
242 supportedDuplexModeList_.assign(supportedDuplexModeList.begin(), supportedDuplexModeList.end());
243 }
244
SetSupportedMediaType(const std::vector<std::string> & supportedMediaTypeList)245 void PrinterCapability::SetSupportedMediaType(const std::vector<std::string> &supportedMediaTypeList)
246 {
247 hasSupportedMediaType_ = true;
248 supportedMediaTypeList_.assign(supportedMediaTypeList.begin(), supportedMediaTypeList.end());
249 }
250
SetSupportedQuality(const std::vector<uint32_t> & supportedQualityList)251 void PrinterCapability::SetSupportedQuality(const std::vector<uint32_t> &supportedQualityList)
252 {
253 hasSupportedQuality_ = true;
254 supportedQualityList_.assign(supportedQualityList.begin(), supportedQualityList.end());
255 }
256
SetSupportedOrientation(const std::vector<uint32_t> & supportedOrientationList)257 void PrinterCapability::SetSupportedOrientation(const std::vector<uint32_t> &supportedOrientationList)
258 {
259 hasSupportedOrientation_ = true;
260 supportedOrientationList_.assign(supportedOrientationList.begin(), supportedOrientationList.end());
261 }
262
GetOption() const263 std::string PrinterCapability::GetOption() const
264 {
265 return option_;
266 }
267
ReadFromParcel(Parcel & parcel)268 bool PrinterCapability::ReadFromParcel(Parcel &parcel)
269 {
270 PrinterCapability right;
271 right.SetColorMode(parcel.ReadUint32());
272 right.SetDuplexMode(parcel.ReadUint32());
273
274 PrintUtils::readListFromParcel<PrintPageSize>(parcel, right.supportedPageSizeList_,
275 [](Parcel& p) -> std::optional<PrintPageSize> {
276 auto ptr = PrintPageSize::Unmarshalling(p);
277 if (ptr) {
278 return std::optional<PrintPageSize>(*ptr);
279 }
280 return std::nullopt;
281 });
282
283 PrintUtils::readListFromParcel<PrintResolution>(parcel, right.resolutionList_,
284 [](Parcel& p) -> std::optional<PrintResolution> {
285 auto ptr = PrintResolution::Unmarshalling(p);
286 if (ptr) {
287 return std::optional<PrintResolution>(*ptr);
288 }
289 return std::nullopt;
290 }, &right.hasResolution_);
291
292 PrintUtils::readListFromParcel<uint32_t>(
293 parcel, right.supportedColorModeList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
294 &right.hasSupportedColorMode_);
295
296 PrintUtils::readListFromParcel<uint32_t>(
297 parcel, right.supportedDuplexModeList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
298 &right.hasSupportedDuplexMode_);
299
300 PrintUtils::readListFromParcel<std::string>(
301 parcel, right.supportedMediaTypeList_, [](Parcel &p) { return std::make_optional(p.ReadString()); },
302 &right.hasSupportedMediaType_);
303
304 PrintUtils::readListFromParcel<uint32_t>(
305 parcel, right.supportedQualityList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
306 &right.hasSupportedQuality_);
307
308 PrintUtils::readListFromParcel<uint32_t>(
309 parcel, right.supportedOrientationList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
310 &right.hasSupportedOrientation_);
311
312 right.hasMargin_ = parcel.ReadBool();
313 if (right.hasMargin_) {
314 auto marginPtr = PrintMargin::Unmarshalling(parcel);
315 if (marginPtr != nullptr) {
316 right.SetMinMargin(*marginPtr);
317 }
318 }
319
320 right.hasOption_ = parcel.ReadBool();
321 if (right.hasOption_) {
322 right.SetOption(parcel.ReadString());
323 }
324
325 *this = right;
326 return true;
327 }
328
Marshalling(Parcel & parcel) const329 bool PrinterCapability::Marshalling(Parcel &parcel) const
330 {
331 parcel.WriteUint32(GetColorMode());
332 parcel.WriteUint32(GetDuplexMode());
333
334 PrintUtils::WriteListToParcel(
335 parcel, supportedPageSizeList_,
336 [](Parcel &p, const PrintPageSize& item) { item.Marshalling(p); });
337
338 PrintUtils::WriteListToParcel(
339 parcel, resolutionList_, [](Parcel& p, const PrintResolution& item) { item.Marshalling(p); },
340 hasResolution_);
341
342 PrintUtils::WriteListToParcel(
343 parcel, supportedColorModeList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
344 hasSupportedColorMode_);
345
346 PrintUtils::WriteListToParcel(
347 parcel, supportedDuplexModeList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
348 hasSupportedDuplexMode_);
349
350 PrintUtils::WriteListToParcel(
351 parcel, supportedMediaTypeList_, [](Parcel& p, const std::string& item) { p.WriteString(item); },
352 hasSupportedMediaType_);
353
354 PrintUtils::WriteListToParcel(
355 parcel, supportedQualityList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
356 hasSupportedQuality_);
357
358 PrintUtils::WriteListToParcel(
359 parcel, supportedOrientationList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
360 hasSupportedOrientation_);
361
362 parcel.WriteBool(hasMargin_);
363 if (hasMargin_) {
364 minMargin_.Marshalling(parcel);
365 }
366 parcel.WriteBool(hasOption_);
367 if (hasOption_) {
368 parcel.WriteString(GetOption());
369 }
370 return true;
371 }
372
Unmarshalling(Parcel & parcel)373 std::shared_ptr<PrinterCapability> PrinterCapability::Unmarshalling(Parcel &parcel)
374 {
375 auto nativeObj = std::make_shared<PrinterCapability>();
376 nativeObj->ReadFromParcel(parcel);
377 return nativeObj;
378 }
379
Dump() const380 void PrinterCapability::Dump() const
381 {
382 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
383 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
384
385 for (auto pageItem : supportedPageSizeList_) {
386 pageItem.Dump();
387 }
388
389 if (hasResolution_) {
390 for (auto resolutionItem : resolutionList_) {
391 resolutionItem.Dump();
392 }
393 }
394
395 if (hasSupportedColorMode_) {
396 for (auto item : supportedColorModeList_) {
397 PRINT_HILOGD("supportedColorModeItem = %{public}d", item);
398 }
399 }
400
401 if (hasSupportedDuplexMode_) {
402 for (auto item : supportedDuplexModeList_) {
403 PRINT_HILOGD("supportedDuplexModeItem = %{public}d", item);
404 }
405 }
406
407 if (hasSupportedMediaType_) {
408 for (auto item : supportedMediaTypeList_) {
409 PRINT_HILOGD("supportedMediaTypeItem = %{public}s", item.c_str());
410 }
411 }
412
413 if (hasSupportedQuality_) {
414 for (auto item : supportedQualityList_) {
415 PRINT_HILOGD("supportedQualityItem = %{public}d", item);
416 }
417 }
418
419 if (hasSupportedOrientation_) {
420 for (auto item : supportedOrientationList_) {
421 PRINT_HILOGD("supportedOrientationItem = %{public}d", item);
422 }
423 }
424
425 if (hasMargin_) {
426 minMargin_.Dump();
427 }
428 if (hasOption_) {
429 PRINT_HILOGD("option: %{private}s", option_.c_str());
430 }
431 }
432
GetPrinterAttrValue(const char * name)433 const char *PrinterCapability::GetPrinterAttrValue(const char *name)
434 {
435 auto iter = printerAttr_group.find(name);
436 if (iter != printerAttr_group.end()) {
437 return iter->second.c_str();
438 } else {
439 return "";
440 }
441 }
442
SetPrinterAttrNameAndValue(const char * name,const char * value)443 void PrinterCapability::SetPrinterAttrNameAndValue(const char *name, const char *value)
444 {
445 printerAttr_group[name] = value;
446 }
447
GetPrinterAttrGroupJson()448 nlohmann::json PrinterCapability::GetPrinterAttrGroupJson()
449 {
450 if (printerAttr_group.size() < 1) {
451 PRINT_HILOGI("no printerAttr_group");
452 return "";
453 }
454 nlohmann::json printerAttrGroupJson;
455 for (auto iter = printerAttr_group.begin(); iter != printerAttr_group.end(); iter++) {
456 printerAttrGroupJson[iter->first] = iter->second;
457 }
458 return printerAttrGroupJson;
459 }
460
ClearCurPrinterAttrGroup()461 void PrinterCapability::ClearCurPrinterAttrGroup()
462 {
463 PRINT_HILOGI("printerAttr_group reset");
464 printerAttr_group.clear();
465 }
466 } // namespace OHOS::Print
467