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 hasCopyNumber_ = parcel.ReadBool();
272 if (hasCopyNumber_) {
273 SetCopyNumber(parcel.ReadUint32());
274 }
275 hasPageRange_ = parcel.ReadBool();
276 if (hasPageRange_) {
277 auto rangePtr = PrintRange::Unmarshalling(parcel);
278 if (rangePtr == nullptr) {
279 PRINT_HILOGE("Failed to restore page range");
280 return false;
281 }
282 SetPageRange(*rangePtr);
283 }
284 hasSequential_ = parcel.ReadBool();
285 if (hasSequential_) {
286 SetIsSequential(parcel.ReadBool());
287 }
288 hasPageSize_ = parcel.ReadBool();
289 if (hasPageSize_) {
290 auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
291 if (pageSizePtr == nullptr) {
292 PRINT_HILOGE("Failed to restore page size");
293 return false;
294 }
295 SetPageSize(*pageSizePtr);
296 }
297 hasLandscape_ = parcel.ReadBool();
298 if (hasLandscape_) {
299 SetIsLandscape(parcel.ReadBool());
300 }
301 hasDirectionMode_ = parcel.ReadBool();
302 if (hasDirectionMode_) {
303 SetDirectionMode(parcel.ReadUint32());
304 }
305 hasColorMode_ = parcel.ReadBool();
306 if (hasColorMode_) {
307 SetColorMode(parcel.ReadUint32());
308 }
309 hasDuplexMode_ = parcel.ReadBool();
310 if (hasDuplexMode_) {
311 SetDuplexMode(parcel.ReadUint32());
312 }
313 return ReadNextDataFromParcel(parcel);
314 }
315
ReadNextDataFromParcel(Parcel & parcel)316 bool PrintAttributes::ReadNextDataFromParcel(Parcel &parcel)
317 {
318 hasMargin_ = parcel.ReadBool();
319 if (hasMargin_) {
320 auto marginPtr = PrintMargin::Unmarshalling(parcel);
321 if (marginPtr == nullptr) {
322 PRINT_HILOGE("Failed to restore margin");
323 return false;
324 }
325 margin_ = *marginPtr;
326 }
327 hasOption_ = parcel.ReadBool();
328 if (hasOption_) {
329 SetOption(parcel.ReadString());
330 }
331 return true;
332 }
333
Marshalling(Parcel & parcel) const334 bool PrintAttributes::Marshalling(Parcel &parcel) const
335 {
336 parcel.WriteBool(hasCopyNumber_);
337 if (hasCopyNumber_) {
338 parcel.WriteUint32(GetCopyNumber());
339 }
340
341 parcel.WriteBool(hasPageRange_);
342 if (hasPageRange_) {
343 pageRange_.Marshalling(parcel);
344 }
345
346 parcel.WriteBool(hasSequential_);
347 if (hasSequential_) {
348 parcel.WriteBool(GetIsSequential());
349 }
350
351 return MarshallingParam(parcel);
352 }
353
MarshallingParam(Parcel & parcel) const354 bool PrintAttributes::MarshallingParam(Parcel &parcel) const
355 {
356 parcel.WriteBool(hasPageSize_);
357 if (hasPageSize_) {
358 pageSize_.Marshalling(parcel);
359 }
360
361 parcel.WriteBool(hasLandscape_);
362 if (hasLandscape_) {
363 parcel.WriteBool(GetIsLandscape());
364 }
365
366 parcel.WriteBool(hasDirectionMode_);
367 if (hasDirectionMode_) {
368 parcel.WriteUint32(GetDirectionMode());
369 }
370
371 parcel.WriteBool(hasColorMode_);
372 if (hasColorMode_) {
373 parcel.WriteUint32(GetColorMode());
374 }
375
376 parcel.WriteBool(hasDuplexMode_);
377 if (hasDuplexMode_) {
378 parcel.WriteUint32(GetDuplexMode());
379 }
380
381 parcel.WriteBool(hasMargin_);
382 if (hasMargin_) {
383 margin_.Marshalling(parcel);
384 }
385
386 parcel.WriteBool(hasOption_);
387 if (hasOption_) {
388 parcel.WriteString(GetOption());
389 }
390
391 return true;
392 }
393
Unmarshalling(Parcel & parcel)394 std::shared_ptr<PrintAttributes> PrintAttributes::Unmarshalling(Parcel &parcel)
395 {
396 auto nativeObj = std::make_shared<PrintAttributes>();
397 if (!nativeObj->ReadFromParcel(parcel)) {
398 PRINT_HILOGE("Failed to unmarshalling PrintAttributes");
399 return nullptr;
400 }
401 return nativeObj;
402 }
403
Dump()404 void PrintAttributes::Dump()
405 {
406 if (hasCopyNumber_) {
407 PRINT_HILOGD("copyNumber_ = %{public}d", copyNumber_);
408 }
409 if (hasSequential_) {
410 PRINT_HILOGD("isSequential_ = %{public}d", isSequential_);
411 }
412 if (hasLandscape_) {
413 PRINT_HILOGD("isLandscape_ = %{public}d", isLandscape_);
414 }
415 if (hasDirectionMode_) {
416 PRINT_HILOGD("directionMode_ = %{public}d", directionMode_);
417 }
418 if (hasColorMode_) {
419 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
420 }
421 if (hasDuplexMode_) {
422 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
423 }
424 if (hasPageRange_) {
425 pageRange_.Dump();
426 }
427 if (hasPageSize_) {
428 pageSize_.Dump();
429 }
430 if (hasMargin_) {
431 margin_.Dump();
432 }
433 if (hasOption_) {
434 PRINT_HILOGD("option: %{private}s", option_.c_str());
435 }
436 }
437 } // namespace OHOS::Print
438