• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using json = nlohmann::json;
21 namespace OHOS::Print {
PrinterCapability()22 PrinterCapability::PrinterCapability() : colorMode_(0), duplexMode_(0),
23     hasResolution_(false), hasMargin_(false), hasOption_(false), option_("")
24 {
25     pageSizeList_.clear();
26     resolutionList_.clear();
27     minMargin_.Reset();
28 }
29 
PrinterCapability(const PrinterCapability & right)30 PrinterCapability::PrinterCapability(const PrinterCapability &right)
31 {
32     colorMode_ = right.colorMode_;
33     duplexMode_ = right.duplexMode_;
34     pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
35     hasResolution_ = right.hasResolution_;
36     resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
37     hasMargin_ = right.hasMargin_;
38     minMargin_ = right.minMargin_;
39     hasOption_ = right.hasOption_;
40     option_= right.option_;
41 }
42 
operator =(const PrinterCapability & right)43 PrinterCapability &PrinterCapability::operator=(const PrinterCapability &right)
44 {
45     if (this != &right) {
46         colorMode_ = right.colorMode_;
47         duplexMode_ = right.duplexMode_;
48         pageSizeList_.assign(right.pageSizeList_.begin(), right.pageSizeList_.end());
49         hasResolution_ = right.hasResolution_;
50         resolutionList_.assign(right.resolutionList_.begin(), right.resolutionList_.end());
51         hasMargin_ = right.hasMargin_;
52         minMargin_ = right.minMargin_;
53         hasOption_ = right.hasOption_;
54         option_ = right.option_;
55     }
56     return *this;
57 }
58 
~PrinterCapability()59 PrinterCapability::~PrinterCapability()
60 {
61 }
62 
Reset()63 void PrinterCapability::Reset()
64 {
65     SetColorMode(0);
66     SetDuplexMode(0);
67     pageSizeList_.clear();
68     hasResolution_ = false;
69     resolutionList_.clear();
70     hasMargin_ = false;
71     minMargin_.Reset();
72 }
73 
SetMinMargin(const PrintMargin & minMargin)74 void PrinterCapability::SetMinMargin(const PrintMargin &minMargin)
75 {
76     hasMargin_ = true;
77     minMargin_ = minMargin;
78 }
79 
SetPageSize(const std::vector<PrintPageSize> & pageSizeList)80 void PrinterCapability::SetPageSize(const std::vector<PrintPageSize> &pageSizeList)
81 {
82     pageSizeList_.assign(pageSizeList.begin(), pageSizeList.end());
83 }
84 
SetResolution(const std::vector<PrintResolution> & resolutionList)85 void PrinterCapability::SetResolution(const std::vector<PrintResolution> &resolutionList)
86 {
87     hasResolution_ = true;
88     resolutionList_.assign(resolutionList.begin(), resolutionList.end());
89 }
90 
SetColorMode(uint32_t colorMode)91 void PrinterCapability::SetColorMode(uint32_t colorMode)
92 {
93     colorMode_ = colorMode;
94 }
95 
SetDuplexMode(uint32_t duplexMode)96 void PrinterCapability::SetDuplexMode(uint32_t duplexMode)
97 {
98     duplexMode_ = duplexMode;
99 }
100 
SetOption(const std::string & option)101 void PrinterCapability::SetOption(const std::string &option)
102 {
103     hasOption_ = true;
104     option_ = option;
105 }
106 
HasMargin() const107 bool PrinterCapability::HasMargin() const
108 {
109     return hasMargin_;
110 }
111 
GetMinMargin(PrintMargin & margin) const112 void PrinterCapability::GetMinMargin(PrintMargin &margin) const
113 {
114     margin = minMargin_;
115 }
116 
GetPageSize(std::vector<PrintPageSize> & pageSizeList) const117 void PrinterCapability::GetPageSize(std::vector<PrintPageSize> &pageSizeList) const
118 {
119     pageSizeList.assign(pageSizeList_.begin(), pageSizeList_.end());
120 }
121 
HasResolution() const122 bool PrinterCapability::HasResolution() const
123 {
124     return hasResolution_;
125 }
126 
GetResolution(std::vector<PrintResolution> & resolutionList) const127 void PrinterCapability::GetResolution(std::vector<PrintResolution> &resolutionList) const
128 {
129     resolutionList.assign(resolutionList_.begin(), resolutionList_.end());
130 }
131 
GetColorMode() const132 uint32_t PrinterCapability::GetColorMode() const
133 {
134     return colorMode_;
135 }
136 
GetDuplexMode() const137 uint32_t PrinterCapability::GetDuplexMode() const
138 {
139     return duplexMode_;
140 }
141 
HasOption() const142 bool PrinterCapability::HasOption() const
143 {
144     return hasOption_;
145 }
146 
GetOption() const147 std::string PrinterCapability::GetOption() const
148 {
149     return option_;
150 }
151 
ReadFromParcel(Parcel & parcel)152 bool PrinterCapability::ReadFromParcel(Parcel &parcel)
153 {
154     SetColorMode(parcel.ReadUint32());
155     SetDuplexMode(parcel.ReadUint32());
156 
157     uint32_t vecSize = parcel.ReadUint32();
158 
159     CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize);
160     std::vector<PrintPageSize> pageSizeList;
161     for (uint32_t index = 0; index < vecSize; index++) {
162         auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
163         if (pageSizePtr != nullptr) {
164             pageSizeList.emplace_back(*pageSizePtr);
165         }
166     }
167     SetPageSize(pageSizeList);
168 
169     hasResolution_ = parcel.ReadBool();
170     if (hasResolution_) {
171         std::vector<PrintResolution> resolutionList;
172         vecSize = parcel.ReadUint32();
173         CHECK_IS_EXCEED_PRINT_RANGE_BOOL(vecSize);
174         for (uint32_t index = 0; index < vecSize; index++) {
175             auto resolutionPtr = PrintResolution::Unmarshalling(parcel);
176             if (resolutionPtr != nullptr) {
177                 resolutionList.emplace_back(*resolutionPtr);
178             }
179         }
180         SetResolution(resolutionList);
181     }
182 
183     hasMargin_ = parcel.ReadBool();
184     if (hasMargin_) {
185         auto marginPtr = PrintMargin::Unmarshalling(parcel);
186         if (marginPtr != nullptr) {
187             minMargin_ = *marginPtr;
188         }
189     }
190     hasOption_ = parcel.ReadBool();
191     if (hasOption_) {
192         SetOption(parcel.ReadString());
193     }
194     return true;
195 }
196 
Marshalling(Parcel & parcel) const197 bool PrinterCapability::Marshalling(Parcel &parcel) const
198 {
199     parcel.WriteUint32(GetColorMode());
200     parcel.WriteUint32(GetDuplexMode());
201 
202     uint32_t vecSize = static_cast<uint32_t>(pageSizeList_.size());
203     parcel.WriteUint32(vecSize);
204     for (uint32_t index = 0; index < vecSize; index++) {
205         pageSizeList_[index].Marshalling(parcel);
206     }
207 
208     parcel.WriteBool(hasResolution_);
209     if (hasResolution_) {
210         vecSize = static_cast<uint32_t>(resolutionList_.size());
211         parcel.WriteUint32(vecSize);
212         for (uint32_t index = 0; index < vecSize; index++) {
213             resolutionList_[index].Marshalling(parcel);
214         }
215     }
216 
217     parcel.WriteBool(hasMargin_);
218     if (hasMargin_) {
219         minMargin_.Marshalling(parcel);
220     }
221     parcel.WriteBool(hasOption_);
222     if (hasOption_) {
223         parcel.WriteString(GetOption());
224     }
225     return true;
226 }
227 
Unmarshalling(Parcel & parcel)228 std::shared_ptr<PrinterCapability> PrinterCapability::Unmarshalling(Parcel &parcel)
229 {
230     auto nativeObj = std::make_shared<PrinterCapability>();
231     nativeObj->ReadFromParcel(parcel);
232     return nativeObj;
233 }
234 
Dump()235 void PrinterCapability::Dump()
236 {
237     PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
238     PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
239 
240     for (auto pageItem : pageSizeList_) {
241         pageItem.Dump();
242     }
243 
244     if (hasResolution_) {
245         for (auto resolutionItem : resolutionList_) {
246             resolutionItem.Dump();
247         }
248     }
249 
250     if (hasMargin_) {
251         minMargin_.Dump();
252     }
253     if (hasOption_) {
254         PRINT_HILOGD("option: %{private}s", option_.c_str());
255     }
256 }
257 
GetPrinterAttrValue(const char * name)258 const char* PrinterCapability::GetPrinterAttrValue(const char* name)
259 {
260     auto iter = printerAttr_group.find(name);
261     if (iter != printerAttr_group.end()) {
262         return iter->second.c_str();
263     } else {
264         return "";
265     }
266 }
267 
SetPrinterAttrNameAndValue(const char * name,const char * value)268 void PrinterCapability::SetPrinterAttrNameAndValue(const char* name, const char* value)
269 {
270     printerAttr_group[name] = value;
271 }
272 
GetPrinterAttrGroupJson()273 nlohmann::json PrinterCapability::GetPrinterAttrGroupJson()
274 {
275     if (printerAttr_group.size() < 1) {
276         PRINT_HILOGI("no printerAttr_group");
277         return "";
278     }
279     nlohmann::json printerAttrGroupJson;
280     for (auto iter = printerAttr_group.begin(); iter != printerAttr_group.end(); iter++) {
281         printerAttrGroupJson[iter->first] = iter->second;
282     }
283     return printerAttrGroupJson;
284 }
285 
ClearCurPrinterAttrGroup()286 void PrinterCapability::ClearCurPrinterAttrGroup()
287 {
288     PRINT_HILOGI("printerAttr_group reset");
289     printerAttr_group.clear();
290 }
291 } // namespace OHOS::Print
292