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 "print_attributes.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
PrintAttributes()21 PrintAttributes::PrintAttributes()
22     : hasCopyNumber_(false), copyNumber_(0), hasPageRange_(false), hasSequential_(false), isSequential_(false),
23     hasPageSize_(false), hasLandscape_(false), isLandscape_(false), hasDirectionMode_(false), directionMode_(0),
24     hasColorMode_(false), colorMode_(0), hasDuplexMode_(false), duplexMode_(0),
25     hasMargin_(false), hasOption_(false), option_("") {
26     pageRange_.Reset();
27     pageSize_.Reset();
28     margin_.Reset();
29 }
30 
PrintAttributes(const PrintAttributes & right)31 PrintAttributes::PrintAttributes(const PrintAttributes &right)
32 {
33     hasCopyNumber_ = right.hasCopyNumber_;
34     copyNumber_ = right.copyNumber_;
35     hasPageRange_ = right.hasPageRange_;
36     pageRange_ = right.pageRange_;
37     hasSequential_ = right.hasSequential_;
38     isSequential_ = right.isSequential_;
39     hasPageSize_ = right.hasPageSize_;
40     pageSize_ = right.pageSize_;
41     hasLandscape_ = right.hasLandscape_;
42     isLandscape_ = right.isLandscape_;
43     hasDirectionMode_ = right.hasDirectionMode_;
44     directionMode_ = right.directionMode_;
45     hasColorMode_ = right.hasColorMode_;
46     colorMode_ = right.colorMode_;
47     hasDuplexMode_ = right.hasDuplexMode_;
48     duplexMode_ = right.duplexMode_;
49     hasMargin_ = right.hasMargin_;
50     margin_ = right.margin_;
51     hasOption_ = right.hasOption_;
52     option_ = right.option_;
53 }
54 
operator =(const PrintAttributes & right)55 PrintAttributes &PrintAttributes::operator=(const PrintAttributes &right)
56 {
57     if (this != &right) {
58         hasCopyNumber_ = right.hasCopyNumber_;
59         copyNumber_ = right.copyNumber_;
60         hasPageRange_ = right.hasPageRange_;
61         pageRange_ = right.pageRange_;
62         hasSequential_ = right.hasSequential_;
63         isSequential_ = right.isSequential_;
64         hasPageSize_ = right.hasPageSize_;
65         pageSize_ = right.pageSize_;
66         hasLandscape_ = right.hasLandscape_;
67         isLandscape_ = right.isLandscape_;
68         hasDirectionMode_ = right.hasDirectionMode_;
69         directionMode_ = right.directionMode_;
70         hasColorMode_ = right.hasColorMode_;
71         colorMode_ = right.colorMode_;
72         hasDuplexMode_ = right.hasDuplexMode_;
73         duplexMode_ = right.duplexMode_;
74         hasMargin_ = right.hasMargin_;
75         margin_ = right.margin_;
76         hasOption_ = right.hasOption_;
77         option_ = right.option_;
78     }
79     return *this;
80 }
81 
~PrintAttributes()82 PrintAttributes::~PrintAttributes()
83 {
84 }
85 
SetCopyNumber(uint32_t copyNumber)86 void PrintAttributes::SetCopyNumber(uint32_t copyNumber)
87 {
88     hasCopyNumber_ = true;
89     copyNumber_ = copyNumber;
90 }
91 
SetPageRange(const PrintRange & pageRange)92 void PrintAttributes::SetPageRange(const PrintRange &pageRange)
93 {
94     hasPageRange_ = true;
95     pageRange_ = pageRange;
96 }
97 
SetIsSequential(bool isSequential)98 void PrintAttributes::SetIsSequential(bool isSequential)
99 {
100     hasSequential_ = true;
101     isSequential_ = isSequential;
102 }
103 
SetPageSize(const PrintPageSize & pageSize)104 void PrintAttributes::SetPageSize(const PrintPageSize &pageSize)
105 {
106     hasPageSize_ = true;
107     pageSize_ = pageSize;
108 }
109 
SetIsLandscape(bool isLandscape)110 void PrintAttributes::SetIsLandscape(bool isLandscape)
111 {
112     hasLandscape_ = true;
113     isLandscape_ = isLandscape;
114 }
115 
SetDirectionMode(uint32_t directionMode)116 void PrintAttributes::SetDirectionMode(uint32_t directionMode)
117 {
118     hasDirectionMode_ = true;
119     directionMode_ = directionMode;
120 }
121 
SetColorMode(uint32_t colorMode)122 void PrintAttributes::SetColorMode(uint32_t colorMode)
123 {
124     hasColorMode_ = true;
125     colorMode_ = colorMode;
126 }
127 
SetDuplexMode(uint32_t duplexmode)128 void PrintAttributes::SetDuplexMode(uint32_t duplexmode)
129 {
130     hasDuplexMode_ = true;
131     duplexMode_ = duplexmode;
132 }
133 
SetMargin(const PrintMargin & margin)134 void PrintAttributes::SetMargin(const PrintMargin &margin)
135 {
136     hasMargin_ = true;
137     margin_ = margin;
138 }
139 
SetOption(const std::string & option)140 void PrintAttributes::SetOption(const std::string &option)
141 {
142     hasOption_ = true;
143     option_ = option;
144 }
145 
UpdateParams(const PrintAttributes & jobInfo)146 void PrintAttributes::UpdateParams(const PrintAttributes &jobInfo)
147 {
148     hasCopyNumber_ = jobInfo.hasCopyNumber_;
149     copyNumber_ = jobInfo.copyNumber_;
150     hasPageRange_ = jobInfo.hasPageRange_;
151     pageRange_ = jobInfo.pageRange_;
152     hasSequential_ = jobInfo.hasSequential_;
153     isSequential_ = jobInfo.isSequential_;
154     hasPageSize_ = jobInfo.hasPageSize_;
155     pageSize_ = jobInfo.pageSize_;
156     hasLandscape_ = jobInfo.hasLandscape_;
157     isLandscape_ = jobInfo.isLandscape_;
158     hasDirectionMode_ = jobInfo.hasDirectionMode_;
159     directionMode_ = jobInfo.directionMode_;
160     hasColorMode_ = jobInfo.hasColorMode_;
161     colorMode_ = jobInfo.colorMode_;
162     hasDuplexMode_ = jobInfo.hasDuplexMode_;
163     duplexMode_ = jobInfo.duplexMode_;
164     hasMargin_ = jobInfo.hasMargin_;
165     margin_ = jobInfo.margin_;
166     hasOption_ = jobInfo.hasOption_;
167     option_ = jobInfo.option_;
168 }
169 
GetCopyNumber() const170 uint32_t PrintAttributes::GetCopyNumber() const
171 {
172     return copyNumber_;
173 }
174 
GetPageRange(PrintRange & range) const175 void PrintAttributes::GetPageRange(PrintRange &range) const
176 {
177     range = pageRange_;
178 }
179 
GetIsSequential() const180 bool PrintAttributes::GetIsSequential() const
181 {
182     return isSequential_;
183 }
GetPageSize(PrintPageSize & pageSize) const184 void PrintAttributes::GetPageSize(PrintPageSize &pageSize) const
185 {
186     pageSize = pageSize_;
187 }
188 
GetIsLandscape() const189 bool PrintAttributes::GetIsLandscape() const
190 {
191     return isLandscape_;
192 }
193 
GetDirectionMode() const194 uint32_t PrintAttributes::GetDirectionMode() const
195 {
196     return directionMode_;
197 }
198 
GetColorMode() const199 uint32_t PrintAttributes::GetColorMode() const
200 {
201     return colorMode_;
202 }
203 
GetDuplexMode() const204 uint32_t PrintAttributes::GetDuplexMode() const
205 {
206     return duplexMode_;
207 }
208 
GetMargin(PrintMargin & margin) const209 void PrintAttributes::GetMargin(PrintMargin &margin) const
210 {
211     margin = margin_;
212 }
213 
GetOption() const214 const std::string &PrintAttributes::GetOption() const
215 {
216     return option_;
217 }
218 
HasCopyNumber() const219 bool PrintAttributes::HasCopyNumber() const
220 {
221     return hasCopyNumber_;
222 }
223 
HasPageRange() const224 bool PrintAttributes::HasPageRange() const
225 {
226     return hasPageRange_;
227 }
228 
HasSequential() const229 bool PrintAttributes::HasSequential() const
230 {
231     return hasSequential_;
232 }
233 
HasPageSize() const234 bool PrintAttributes::HasPageSize() const
235 {
236     return hasPageSize_;
237 }
238 
HasLandscape() const239 bool PrintAttributes::HasLandscape() const
240 {
241     return hasLandscape_;
242 }
243 
HasDirectionMode() const244 bool PrintAttributes::HasDirectionMode() const
245 {
246     return hasDirectionMode_;
247 }
248 
HasColorMode() const249 bool PrintAttributes::HasColorMode() const
250 {
251     return hasColorMode_;
252 }
253 
HasDuplexMode() const254 bool PrintAttributes::HasDuplexMode() const
255 {
256     return hasDuplexMode_;
257 }
258 
HasMargin() const259 bool PrintAttributes::HasMargin() const
260 {
261     return hasMargin_;
262 }
263 
HasOption() const264 bool PrintAttributes::HasOption() const
265 {
266     return hasOption_;
267 }
268 
ReadFromParcel(Parcel & parcel)269 bool PrintAttributes::ReadFromParcel(Parcel &parcel)
270 {
271     if (parcel.GetReadableBytes() == 0) {
272         PRINT_HILOGE("no data in parcel");
273         return false;
274     }
275     hasCopyNumber_ = parcel.ReadBool();
276     if (hasCopyNumber_) {
277         SetCopyNumber(parcel.ReadUint32());
278     }
279     hasPageRange_ = parcel.ReadBool();
280     if (hasPageRange_) {
281         auto rangePtr = PrintRange::Unmarshalling(parcel);
282         if (rangePtr == nullptr) {
283             PRINT_HILOGE("Failed to restore page range");
284             return false;
285         }
286         SetPageRange(*rangePtr);
287     }
288     hasSequential_ = parcel.ReadBool();
289     if (hasSequential_) {
290         SetIsSequential(parcel.ReadBool());
291     }
292     hasPageSize_ = parcel.ReadBool();
293     if (hasPageSize_) {
294         auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
295         if (pageSizePtr == nullptr) {
296             PRINT_HILOGE("Failed to restore page size");
297             return false;
298         }
299         SetPageSize(*pageSizePtr);
300     }
301     hasLandscape_ = parcel.ReadBool();
302     if (hasLandscape_) {
303         SetIsLandscape(parcel.ReadBool());
304     }
305     hasDirectionMode_ = parcel.ReadBool();
306     if (hasDirectionMode_) {
307         SetDirectionMode(parcel.ReadUint32());
308     }
309     hasColorMode_ = parcel.ReadBool();
310     if (hasColorMode_) {
311         SetColorMode(parcel.ReadUint32());
312     }
313     hasDuplexMode_ = parcel.ReadBool();
314     if (hasDuplexMode_) {
315         SetDuplexMode(parcel.ReadUint32());
316     }
317     return ReadNextDataFromParcel(parcel);
318 }
319 
ReadNextDataFromParcel(Parcel & parcel)320 bool PrintAttributes::ReadNextDataFromParcel(Parcel &parcel)
321 {
322     hasMargin_ = parcel.ReadBool();
323     if (hasMargin_) {
324         auto marginPtr = PrintMargin::Unmarshalling(parcel);
325         if (marginPtr == nullptr) {
326             PRINT_HILOGE("Failed to restore margin");
327             return false;
328         }
329         margin_ = *marginPtr;
330     }
331     hasOption_ = parcel.ReadBool();
332     if (hasOption_) {
333         SetOption(parcel.ReadString());
334     }
335     return true;
336 }
337 
Marshalling(Parcel & parcel) const338 bool PrintAttributes::Marshalling(Parcel &parcel) const
339 {
340     parcel.WriteBool(hasCopyNumber_);
341     if (hasCopyNumber_) {
342         parcel.WriteUint32(GetCopyNumber());
343     }
344 
345     parcel.WriteBool(hasPageRange_);
346     if (hasPageRange_) {
347         pageRange_.Marshalling(parcel);
348     }
349 
350     parcel.WriteBool(hasSequential_);
351     if (hasSequential_) {
352         parcel.WriteBool(GetIsSequential());
353     }
354 
355     return MarshallingParam(parcel);
356 }
357 
MarshallingParam(Parcel & parcel) const358 bool PrintAttributes::MarshallingParam(Parcel &parcel) const
359 {
360     parcel.WriteBool(hasPageSize_);
361     if (hasPageSize_) {
362         pageSize_.Marshalling(parcel);
363     }
364 
365     parcel.WriteBool(hasLandscape_);
366     if (hasLandscape_) {
367         parcel.WriteBool(GetIsLandscape());
368     }
369 
370     parcel.WriteBool(hasDirectionMode_);
371     if (hasDirectionMode_) {
372         parcel.WriteUint32(GetDirectionMode());
373     }
374 
375     parcel.WriteBool(hasColorMode_);
376     if (hasColorMode_) {
377         parcel.WriteUint32(GetColorMode());
378     }
379 
380     parcel.WriteBool(hasDuplexMode_);
381     if (hasDuplexMode_) {
382         parcel.WriteUint32(GetDuplexMode());
383     }
384 
385     parcel.WriteBool(hasMargin_);
386     if (hasMargin_) {
387         margin_.Marshalling(parcel);
388     }
389 
390     parcel.WriteBool(hasOption_);
391     if (hasOption_) {
392         parcel.WriteString(GetOption());
393     }
394 
395     return true;
396 }
397 
Unmarshalling(Parcel & parcel)398 std::shared_ptr<PrintAttributes> PrintAttributes::Unmarshalling(Parcel &parcel)
399 {
400     auto nativeObj = std::make_shared<PrintAttributes>();
401     if (!nativeObj->ReadFromParcel(parcel)) {
402         PRINT_HILOGE("Failed to unmarshalling PrintAttributes");
403         return nullptr;
404     }
405     return nativeObj;
406 }
407 
Dump()408 void PrintAttributes::Dump()
409 {
410     if (hasCopyNumber_) {
411         PRINT_HILOGD("copyNumber_ = %{public}d", copyNumber_);
412     }
413     if (hasSequential_) {
414         PRINT_HILOGD("isSequential_ = %{public}d", isSequential_);
415     }
416     if (hasLandscape_) {
417         PRINT_HILOGD("isLandscape_ = %{public}d", isLandscape_);
418     }
419     if (hasDirectionMode_) {
420         PRINT_HILOGD("directionMode_ = %{public}d", directionMode_);
421     }
422     if (hasColorMode_) {
423         PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
424     }
425     if (hasDuplexMode_) {
426         PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
427     }
428     if (hasPageRange_) {
429         pageRange_.Dump();
430     }
431     if (hasPageSize_) {
432         pageSize_.Dump();
433     }
434     if (hasMargin_) {
435         margin_.Dump();
436     }
437     if (hasOption_) {
438         PRINT_HILOGD("option: %{private}s", option_.c_str());
439     }
440 }
441 } // namespace OHOS::Print
442