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 #include "print_json_util.h"
21 #include <sstream>
22
23 namespace OHOS::Print {
PrinterCapability()24 PrinterCapability::PrinterCapability()
25 : colorMode_(0),
26 duplexMode_(0),
27 hasResolution_(false),
28 hasSupportedColorMode_(false),
29 hasSupportedDuplexMode_(false),
30 hasSupportedMediaType_(false),
31 hasSupportedQuality_(false),
32 hasSupportedOrientation_(false),
33 hasMargin_(false),
34 hasOption_(false),
35 option_("")
36 {
37 pageSizeList_.clear();
38 resolutionList_.clear();
39 minMargin_.Reset();
40 supportedPageSizeList_.clear();
41 supportedColorModeList_.clear();
42 supportedDuplexModeList_.clear();
43 supportedMediaTypeList_.clear();
44 supportedQualityList_.clear();
45 supportedOrientationList_.clear();
46 printerAttr_group.clear();
47 }
48
PrinterCapability(const PrinterCapability & right)49 PrinterCapability::PrinterCapability(const PrinterCapability &right)
50 {
51 *this = right;
52 }
53
operator =(const PrinterCapability & right)54 PrinterCapability &PrinterCapability::operator=(const PrinterCapability &right)
55 {
56 if (this != &right) {
57 colorMode_ = right.colorMode_;
58 duplexMode_ = right.duplexMode_;
59 pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
60 hasResolution_ = right.hasResolution_;
61 resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
62 hasMargin_ = right.hasMargin_;
63 minMargin_ = right.minMargin_;
64 hasOption_ = right.hasOption_;
65 option_ = right.option_;
66 hasSupportedColorMode_ = right.hasSupportedColorMode_;
67 hasSupportedDuplexMode_ = right.hasSupportedDuplexMode_;
68 hasSupportedMediaType_ = right.hasSupportedMediaType_;
69 hasSupportedQuality_ = right.hasSupportedQuality_;
70 hasSupportedOrientation_ = right.hasSupportedOrientation_;
71 supportedPageSizeList_.assign(right.supportedPageSizeList_.begin(), right.supportedPageSizeList_.end());
72 supportedColorModeList_.assign(right.supportedColorModeList_.begin(), right.supportedColorModeList_.end());
73 supportedDuplexModeList_.assign(right.supportedDuplexModeList_.begin(), right.supportedDuplexModeList_.end());
74 supportedMediaTypeList_.assign(right.supportedMediaTypeList_.begin(), right.supportedMediaTypeList_.end());
75 supportedQualityList_.assign(right.supportedQualityList_.begin(), right.supportedQualityList_.end());
76 supportedOrientationList_.assign(right.supportedOrientationList_.begin(),
77 right.supportedOrientationList_.end());
78 }
79 return *this;
80 }
81
~PrinterCapability()82 PrinterCapability::~PrinterCapability() {}
83
Reset()84 void PrinterCapability::Reset()
85 {
86 SetColorMode(0);
87 SetDuplexMode(0);
88 pageSizeList_.clear();
89 hasResolution_ = false;
90 resolutionList_.clear();
91 hasMargin_ = false;
92 minMargin_.Reset();
93 hasSupportedColorMode_ = false;
94 hasSupportedDuplexMode_ = false;
95 hasSupportedMediaType_ = false;
96 hasSupportedQuality_ = false;
97 hasSupportedOrientation_ = false;
98 supportedPageSizeList_.clear();
99 supportedColorModeList_.clear();
100 supportedDuplexModeList_.clear();
101 supportedMediaTypeList_.clear();
102 supportedQualityList_.clear();
103 supportedOrientationList_.clear();
104 }
105
SetMinMargin(const PrintMargin & minMargin)106 void PrinterCapability::SetMinMargin(const PrintMargin &minMargin)
107 {
108 hasMargin_ = true;
109 minMargin_ = minMargin;
110 }
111
SetResolution(const std::vector<PrintResolution> & resolutionList)112 void PrinterCapability::SetResolution(const std::vector<PrintResolution> &resolutionList)
113 {
114 hasResolution_ = true;
115 resolutionList_.assign(resolutionList.begin(), resolutionList.end());
116 }
117
SetColorMode(uint32_t colorMode)118 void PrinterCapability::SetColorMode(uint32_t colorMode)
119 {
120 colorMode_ = colorMode;
121 }
122
SetDuplexMode(uint32_t duplexMode)123 void PrinterCapability::SetDuplexMode(uint32_t duplexMode)
124 {
125 duplexMode_ = duplexMode;
126 }
127
SetOption(const std::string & option)128 void PrinterCapability::SetOption(const std::string &option)
129 {
130 hasOption_ = true;
131 option_ = option;
132 }
133
HasMargin() const134 bool PrinterCapability::HasMargin() const
135 {
136 return hasMargin_;
137 }
138
GetMinMargin(PrintMargin & margin) const139 void PrinterCapability::GetMinMargin(PrintMargin &margin) const
140 {
141 margin = minMargin_;
142 }
143
GetPageSize(std::vector<PrintPageSize> & pageSizeList) const144 void PrinterCapability::GetPageSize(std::vector<PrintPageSize> &pageSizeList) const
145 {
146 pageSizeList.assign(pageSizeList_.begin(), pageSizeList_.end());
147 }
148
HasResolution() const149 bool PrinterCapability::HasResolution() const
150 {
151 return hasResolution_;
152 }
153
GetResolution(std::vector<PrintResolution> & resolutionList) const154 void PrinterCapability::GetResolution(std::vector<PrintResolution> &resolutionList) const
155 {
156 resolutionList.assign(resolutionList_.begin(), resolutionList_.end());
157 }
158
GetColorMode() const159 uint32_t PrinterCapability::GetColorMode() const
160 {
161 return colorMode_;
162 }
163
GetDuplexMode() const164 uint32_t PrinterCapability::GetDuplexMode() const
165 {
166 return duplexMode_;
167 }
168
HasOption() const169 bool PrinterCapability::HasOption() const
170 {
171 return hasOption_;
172 }
173
HasSupportedColorMode() const174 bool PrinterCapability::HasSupportedColorMode() const
175 {
176 return hasSupportedColorMode_;
177 }
178
HasSupportedDuplexMode() const179 bool PrinterCapability::HasSupportedDuplexMode() const
180 {
181 return hasSupportedDuplexMode_;
182 }
183
HasSupportedMediaType() const184 bool PrinterCapability::HasSupportedMediaType() const
185 {
186 return hasSupportedMediaType_;
187 }
188
HasSupportedQuality() const189 bool PrinterCapability::HasSupportedQuality() const
190 {
191 return hasSupportedQuality_;
192 }
193
HasSupportedOrientation() const194 bool PrinterCapability::HasSupportedOrientation() const
195 {
196 return hasSupportedOrientation_;
197 }
198
GetSupportedPageSize(std::vector<PrintPageSize> & supportedPageSizeList) const199 void PrinterCapability::GetSupportedPageSize(std::vector<PrintPageSize> &supportedPageSizeList) const
200 {
201 supportedPageSizeList.assign(supportedPageSizeList_.begin(), supportedPageSizeList_.end());
202 }
203
GetSupportedColorMode(std::vector<uint32_t> & supportedColorModeList) const204 void PrinterCapability::GetSupportedColorMode(std::vector<uint32_t> &supportedColorModeList) const
205 {
206 supportedColorModeList.assign(supportedColorModeList_.begin(), supportedColorModeList_.end());
207 }
208
GetSupportedDuplexMode(std::vector<uint32_t> & supportedDuplexModeList) const209 void PrinterCapability::GetSupportedDuplexMode(std::vector<uint32_t> &supportedDuplexModeList) const
210 {
211 supportedDuplexModeList.assign(supportedDuplexModeList_.begin(), supportedDuplexModeList_.end());
212 }
213
GetSupportedMediaType(std::vector<std::string> & supportedMediaTypeList) const214 void PrinterCapability::GetSupportedMediaType(std::vector<std::string> &supportedMediaTypeList) const
215 {
216 supportedMediaTypeList.assign(supportedMediaTypeList_.begin(), supportedMediaTypeList_.end());
217 }
218
GetSupportedQuality(std::vector<uint32_t> & supportedQualityList) const219 void PrinterCapability::GetSupportedQuality(std::vector<uint32_t> &supportedQualityList) const
220 {
221 supportedQualityList.assign(supportedQualityList_.begin(), supportedQualityList_.end());
222 }
223
GetSupportedOrientation(std::vector<uint32_t> & supportedOrientationList) const224 void PrinterCapability::GetSupportedOrientation(std::vector<uint32_t> &supportedOrientationList) const
225 {
226 supportedOrientationList.assign(supportedOrientationList_.begin(), supportedOrientationList_.end());
227 }
228
SetSupportedPageSize(const std::vector<PrintPageSize> & supportedPageSizeList)229 void PrinterCapability::SetSupportedPageSize(const std::vector<PrintPageSize> &supportedPageSizeList)
230 {
231 std::vector<PrintPageSize> uniquePageSizeList = RemoveDuplicatePageSize(supportedPageSizeList);
232 supportedPageSizeList_.assign(uniquePageSizeList.begin(), uniquePageSizeList.end());
233 }
234
SetSupportedColorMode(const std::vector<uint32_t> & supportedColorModeList)235 void PrinterCapability::SetSupportedColorMode(const std::vector<uint32_t> &supportedColorModeList)
236 {
237 hasSupportedColorMode_ = true;
238 supportedColorModeList_.assign(supportedColorModeList.begin(), supportedColorModeList.end());
239 }
240
SetSupportedDuplexMode(const std::vector<uint32_t> & supportedDuplexModeList)241 void PrinterCapability::SetSupportedDuplexMode(const std::vector<uint32_t> &supportedDuplexModeList)
242 {
243 hasSupportedDuplexMode_ = true;
244 supportedDuplexModeList_.assign(supportedDuplexModeList.begin(), supportedDuplexModeList.end());
245 }
246
SetSupportedMediaType(const std::vector<std::string> & supportedMediaTypeList)247 void PrinterCapability::SetSupportedMediaType(const std::vector<std::string> &supportedMediaTypeList)
248 {
249 hasSupportedMediaType_ = true;
250 supportedMediaTypeList_.assign(supportedMediaTypeList.begin(), supportedMediaTypeList.end());
251 }
252
SetSupportedQuality(const std::vector<uint32_t> & supportedQualityList)253 void PrinterCapability::SetSupportedQuality(const std::vector<uint32_t> &supportedQualityList)
254 {
255 hasSupportedQuality_ = true;
256 supportedQualityList_.assign(supportedQualityList.begin(), supportedQualityList.end());
257 }
258
SetSupportedOrientation(const std::vector<uint32_t> & supportedOrientationList)259 void PrinterCapability::SetSupportedOrientation(const std::vector<uint32_t> &supportedOrientationList)
260 {
261 hasSupportedOrientation_ = true;
262 supportedOrientationList_.assign(supportedOrientationList.begin(), supportedOrientationList.end());
263 }
264
GetOption() const265 std::string PrinterCapability::GetOption() const
266 {
267 return option_;
268 }
269
ReadFromParcel(Parcel & parcel)270 bool PrinterCapability::ReadFromParcel(Parcel &parcel)
271 {
272 PrinterCapability right;
273 right.SetColorMode(parcel.ReadUint32());
274 right.SetDuplexMode(parcel.ReadUint32());
275
276 PrintUtils::readListFromParcel<PrintPageSize>(parcel, right.supportedPageSizeList_,
277 [](Parcel& p) -> std::optional<PrintPageSize> {
278 auto ptr = PrintPageSize::Unmarshalling(p);
279 if (ptr) {
280 return std::optional<PrintPageSize>(*ptr);
281 }
282 return std::nullopt;
283 });
284
285 PrintUtils::readListFromParcel<PrintResolution>(parcel, right.resolutionList_,
286 [](Parcel& p) -> std::optional<PrintResolution> {
287 auto ptr = PrintResolution::Unmarshalling(p);
288 if (ptr) {
289 return std::optional<PrintResolution>(*ptr);
290 }
291 return std::nullopt;
292 }, &right.hasResolution_);
293
294 PrintUtils::readListFromParcel<uint32_t>(
295 parcel, right.supportedColorModeList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
296 &right.hasSupportedColorMode_);
297
298 PrintUtils::readListFromParcel<uint32_t>(
299 parcel, right.supportedDuplexModeList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
300 &right.hasSupportedDuplexMode_);
301
302 PrintUtils::readListFromParcel<std::string>(
303 parcel, right.supportedMediaTypeList_, [](Parcel &p) { return std::make_optional(p.ReadString()); },
304 &right.hasSupportedMediaType_);
305
306 PrintUtils::readListFromParcel<uint32_t>(
307 parcel, right.supportedQualityList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
308 &right.hasSupportedQuality_);
309
310 PrintUtils::readListFromParcel<uint32_t>(
311 parcel, right.supportedOrientationList_, [](Parcel &p) { return std::make_optional(p.ReadUint32()); },
312 &right.hasSupportedOrientation_);
313
314 right.hasMargin_ = parcel.ReadBool();
315 if (right.hasMargin_) {
316 auto marginPtr = PrintMargin::Unmarshalling(parcel);
317 if (marginPtr != nullptr) {
318 right.SetMinMargin(*marginPtr);
319 }
320 }
321
322 right.hasOption_ = parcel.ReadBool();
323 if (right.hasOption_) {
324 right.SetOption(parcel.ReadString());
325 }
326
327 *this = right;
328 return true;
329 }
330
Marshalling(Parcel & parcel) const331 bool PrinterCapability::Marshalling(Parcel &parcel) const
332 {
333 parcel.WriteUint32(GetColorMode());
334 parcel.WriteUint32(GetDuplexMode());
335
336 PrintUtils::WriteListToParcel(
337 parcel, supportedPageSizeList_,
338 [](Parcel &p, const PrintPageSize& item) { item.Marshalling(p); });
339
340 PrintUtils::WriteListToParcel(
341 parcel, resolutionList_, [](Parcel& p, const PrintResolution& item) { item.Marshalling(p); },
342 hasResolution_);
343
344 PrintUtils::WriteListToParcel(
345 parcel, supportedColorModeList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
346 hasSupportedColorMode_);
347
348 PrintUtils::WriteListToParcel(
349 parcel, supportedDuplexModeList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
350 hasSupportedDuplexMode_);
351
352 PrintUtils::WriteListToParcel(
353 parcel, supportedMediaTypeList_, [](Parcel& p, const std::string& item) { p.WriteString(item); },
354 hasSupportedMediaType_);
355
356 PrintUtils::WriteListToParcel(
357 parcel, supportedQualityList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
358 hasSupportedQuality_);
359
360 PrintUtils::WriteListToParcel(
361 parcel, supportedOrientationList_, [](Parcel& p, const int& item) { p.WriteUint32(item); },
362 hasSupportedOrientation_);
363
364 parcel.WriteBool(hasMargin_);
365 if (hasMargin_) {
366 minMargin_.Marshalling(parcel);
367 }
368 parcel.WriteBool(hasOption_);
369 if (hasOption_) {
370 parcel.WriteString(GetOption());
371 }
372 return true;
373 }
374
Unmarshalling(Parcel & parcel)375 std::shared_ptr<PrinterCapability> PrinterCapability::Unmarshalling(Parcel &parcel)
376 {
377 auto nativeObj = std::make_shared<PrinterCapability>();
378 nativeObj->ReadFromParcel(parcel);
379 return nativeObj;
380 }
381
Dump() const382 void PrinterCapability::Dump() const
383 {
384 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
385 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
386
387 for (auto pageItem : supportedPageSizeList_) {
388 pageItem.Dump();
389 }
390
391 if (hasResolution_) {
392 for (auto resolutionItem : resolutionList_) {
393 resolutionItem.Dump();
394 }
395 }
396
397 if (hasSupportedColorMode_) {
398 for (auto item : supportedColorModeList_) {
399 PRINT_HILOGD("supportedColorModeItem = %{public}d", item);
400 }
401 }
402
403 if (hasSupportedDuplexMode_) {
404 for (auto item : supportedDuplexModeList_) {
405 PRINT_HILOGD("supportedDuplexModeItem = %{public}d", item);
406 }
407 }
408
409 if (hasSupportedMediaType_) {
410 for (auto item : supportedMediaTypeList_) {
411 PRINT_HILOGD("supportedMediaTypeItem = %{public}s", item.c_str());
412 }
413 }
414
415 if (hasSupportedQuality_) {
416 for (auto item : supportedQualityList_) {
417 PRINT_HILOGD("supportedQualityItem = %{public}d", item);
418 }
419 }
420
421 if (hasSupportedOrientation_) {
422 for (auto item : supportedOrientationList_) {
423 PRINT_HILOGD("supportedOrientationItem = %{public}d", item);
424 }
425 }
426
427 if (hasMargin_) {
428 minMargin_.Dump();
429 }
430 if (hasOption_) {
431 PRINT_HILOGD("option: %{private}s", option_.c_str());
432 }
433 }
434
GetPrinterAttrValue(const char * name)435 const char *PrinterCapability::GetPrinterAttrValue(const char *name)
436 {
437 auto iter = printerAttr_group.find(name);
438 if (iter != printerAttr_group.end()) {
439 return iter->second.c_str();
440 } else {
441 return "";
442 }
443 }
444
SetPrinterAttrNameAndValue(const char * name,const char * value)445 void PrinterCapability::SetPrinterAttrNameAndValue(const char *name, const char *value)
446 {
447 printerAttr_group[name] = value;
448 }
449
GetPrinterAttrGroupJson()450 Json::Value PrinterCapability::GetPrinterAttrGroupJson()
451 {
452 if (printerAttr_group.size() < 1) {
453 PRINT_HILOGI("no printerAttr_group");
454 return "";
455 }
456 Json::Value printerAttrGroupJson;
457 for (auto iter = printerAttr_group.begin(); iter != printerAttr_group.end(); iter++) {
458 printerAttrGroupJson[iter->first] = iter->second;
459 }
460 return printerAttrGroupJson;
461 }
462
ClearCurPrinterAttrGroup()463 void PrinterCapability::ClearCurPrinterAttrGroup()
464 {
465 PRINT_HILOGI("printerAttr_group reset");
466 printerAttr_group.clear();
467 }
468
RemoveDuplicatePageSize(const std::vector<PrintPageSize> & supportedPageSizeList)469 std::vector<PrintPageSize> PrinterCapability::RemoveDuplicatePageSize
470 (const std::vector<PrintPageSize> &supportedPageSizeList)
471 {
472 std::vector<PrintPageSize> uniquePageSizeList = supportedPageSizeList;
473 // Move custom to the end to show standard sizes preferentially
474 std::stable_partition(uniquePageSizeList.begin(), uniquePageSizeList.end(),
475 [](const PrintPageSize& p) { return p.GetName().find(CUSTOM_PREFIX) == std::string::npos; });
476 std::unordered_set<std::string> uniquePageSizeSet;
477 auto it = uniquePageSizeList.begin();
478 while (it != uniquePageSizeList.end()) {
479 std::stringstream widthAndHeight;
480 widthAndHeight << round(it->GetWidth() / HUNDRED_OF_MILLIMETRE_TO_INCH) << "x" <<
481 round(it->GetHeight() / HUNDRED_OF_MILLIMETRE_TO_INCH);
482 PRINT_HILOGI("SetSupportedPageSize: %{public}s, uniqueFlag: %{public}s",
483 it->GetName().c_str(), widthAndHeight.str().c_str());
484 if (uniquePageSizeSet.insert(widthAndHeight.str()).second) {
485 ++it;
486 } else {
487 // if already has the same withAndHeight, replace it with the last element, and shorten the list
488 PRINT_HILOGI("SetSupportedPageSize find duplicate!");
489 *it = std::move(uniquePageSizeList.back());
490 uniquePageSizeList.pop_back();
491 }
492 }
493 return uniquePageSizeList;
494 }
495 } // namespace OHOS::Print
496