1 /*
2 * Copyright (c) 2025 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_preferences.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19 #include "print_json_util.h"
20
21 namespace OHOS::Print {
PrinterPreferences()22 PrinterPreferences::PrinterPreferences()
23 : hasDefaultDuplexMode_(false),
24 defaultDuplexMode_(DUPLEX_MODE_NONE),
25 hasDefaultPrintQuality_(false),
26 defaultPrintQuality_(PRINT_QUALITY_NORMAL),
27 hasDefaultMediaType_(false),
28 defaultMediaType_(""),
29 hasDefaultPageSizeId_(false),
30 defaultPageSizeId_(""),
31 hasDefaultOrientation_(false),
32 defaultOrientation_(PRINT_ORIENTATION_MODE_NONE),
33 hasDefaultColorMode_(false),
34 defaultColorMode_(PRINT_COLOR_MODE_MONOCHROME),
35 hasBorderless_(false),
36 borderless_(false),
37 hasDefaultCollate_(false),
38 defaultCollate_(true),
39 hasDefaultReverse_(false),
40 defaultReverse_(false),
41 hasOption_(false),
42 option_("")
43 {
44 }
45
PrinterPreferences(const PrinterPreferences & right)46 PrinterPreferences::PrinterPreferences(const PrinterPreferences &right)
47 : hasDefaultDuplexMode_(right.hasDefaultDuplexMode_),
48 defaultDuplexMode_(right.defaultDuplexMode_),
49 hasDefaultPrintQuality_(right.hasDefaultPrintQuality_),
50 defaultPrintQuality_(right.defaultPrintQuality_),
51 hasDefaultMediaType_(right.hasDefaultMediaType_),
52 defaultMediaType_(right.defaultMediaType_),
53 hasDefaultPageSizeId_(right.hasDefaultPageSizeId_),
54 defaultPageSizeId_(right.defaultPageSizeId_),
55 hasDefaultOrientation_(right.hasDefaultOrientation_),
56 defaultOrientation_(right.defaultOrientation_),
57 hasDefaultColorMode_(right.hasDefaultColorMode_),
58 defaultColorMode_(right.defaultColorMode_),
59 hasBorderless_(right.hasBorderless_),
60 borderless_(right.borderless_),
61 hasDefaultCollate_(right.hasDefaultCollate_),
62 defaultCollate_(right.defaultCollate_),
63 hasDefaultReverse_(right.hasDefaultReverse_),
64 defaultReverse_(right.defaultReverse_),
65 hasOption_(right.hasOption_),
66 option_(right.option_)
67 {
68 }
69
operator =(const PrinterPreferences & right)70 PrinterPreferences &PrinterPreferences::operator=(const PrinterPreferences &right)
71 {
72 if (this != &right) {
73 hasDefaultDuplexMode_ = right.hasDefaultDuplexMode_;
74 defaultDuplexMode_ = right.defaultDuplexMode_;
75 hasDefaultPrintQuality_ = right.hasDefaultPrintQuality_;
76 defaultPrintQuality_ = right.defaultPrintQuality_;
77 hasDefaultMediaType_ = right.hasDefaultMediaType_;
78 defaultMediaType_ = right.defaultMediaType_;
79 hasDefaultPageSizeId_ = right.hasDefaultPageSizeId_;
80 defaultPageSizeId_ = right.defaultPageSizeId_;
81 hasDefaultOrientation_ = right.hasDefaultOrientation_;
82 defaultOrientation_ = right.defaultOrientation_;
83 hasDefaultColorMode_ = right.hasDefaultColorMode_;
84 defaultColorMode_ = right.defaultColorMode_;
85 hasBorderless_ = right.hasBorderless_;
86 borderless_ = right.borderless_;
87 hasDefaultCollate_ = right.hasDefaultCollate_;
88 defaultCollate_ = right.defaultCollate_;
89 hasDefaultReverse_ = right.hasDefaultReverse_;
90 defaultReverse_ = right.defaultReverse_;
91 hasOption_ = right.hasOption_;
92 option_ = right.option_;
93 }
94 return *this;
95 }
96
~PrinterPreferences()97 PrinterPreferences::~PrinterPreferences() {}
98
Reset()99 void PrinterPreferences::Reset()
100 {
101 hasDefaultDuplexMode_ = false;
102 defaultDuplexMode_ = DUPLEX_MODE_NONE;
103 hasDefaultPrintQuality_ = false;
104 defaultPrintQuality_ = PRINT_QUALITY_NORMAL;
105 hasDefaultMediaType_ = false;
106 defaultMediaType_ = "";
107 hasDefaultPageSizeId_ = false;
108 defaultPageSizeId_ = "";
109 hasDefaultOrientation_ = false;
110 defaultOrientation_ = PRINT_ORIENTATION_MODE_NONE;
111 hasDefaultColorMode_ = false;
112 defaultColorMode_ = PRINT_COLOR_MODE_MONOCHROME;
113 hasBorderless_ = false;
114 borderless_ = false;
115 hasDefaultCollate_ = false;
116 defaultCollate_ = true;
117 hasDefaultReverse_ = false;
118 defaultReverse_ = false;
119 hasOption_ = false;
120 option_ = "";
121 }
122
SetDefaultDuplexMode(uint32_t defaultDuplexMode)123 void PrinterPreferences::SetDefaultDuplexMode(uint32_t defaultDuplexMode)
124 {
125 hasDefaultDuplexMode_ = true;
126 defaultDuplexMode_ = defaultDuplexMode;
127 }
128
SetDefaultPrintQuality(uint32_t defaultPrintQuality)129 void PrinterPreferences::SetDefaultPrintQuality(uint32_t defaultPrintQuality)
130 {
131 hasDefaultPrintQuality_ = true;
132 defaultPrintQuality_ = defaultPrintQuality;
133 }
134
SetDefaultMediaType(const std::string & defaultMediaType)135 void PrinterPreferences::SetDefaultMediaType(const std::string &defaultMediaType)
136 {
137 hasDefaultMediaType_ = true;
138 defaultMediaType_ = defaultMediaType;
139 }
140
SetDefaultPageSizeId(const std::string & defaultPageSizeId)141 void PrinterPreferences::SetDefaultPageSizeId(const std::string &defaultPageSizeId)
142 {
143 hasDefaultPageSizeId_ = true;
144 defaultPageSizeId_ = defaultPageSizeId;
145 }
146
SetDefaultOrientation(uint32_t defaultOrientation)147 void PrinterPreferences::SetDefaultOrientation(uint32_t defaultOrientation)
148 {
149 hasDefaultOrientation_ = true;
150 defaultOrientation_ = defaultOrientation;
151 }
152
SetDefaultColorMode(uint32_t defaultColorMode)153 void PrinterPreferences::SetDefaultColorMode(uint32_t defaultColorMode)
154 {
155 hasDefaultColorMode_ = true;
156 defaultColorMode_ = defaultColorMode;
157 }
158
SetBorderless(bool borderless)159 void PrinterPreferences::SetBorderless(bool borderless)
160 {
161 hasBorderless_ = true;
162 borderless_ = borderless;
163 }
164
SetDefaultCollate(bool collate)165 void PrinterPreferences::SetDefaultCollate(bool collate)
166 {
167 hasDefaultCollate_ = true;
168 defaultCollate_ = collate;
169 }
170
SetDefaultReverse(bool reverse)171 void PrinterPreferences::SetDefaultReverse(bool reverse)
172 {
173 hasDefaultReverse_ = true;
174 defaultReverse_ = reverse;
175 }
176
SetOption(const std::string & option)177 void PrinterPreferences::SetOption(const std::string &option)
178 {
179 hasOption_ = true;
180 option_ = option;
181 }
182
HasDefaultDuplexMode() const183 bool PrinterPreferences::HasDefaultDuplexMode() const
184 {
185 return hasDefaultDuplexMode_;
186 }
187
GetDefaultDuplexMode() const188 uint32_t PrinterPreferences::GetDefaultDuplexMode() const
189 {
190 return defaultDuplexMode_;
191 }
192
HasDefaultPrintQuality() const193 bool PrinterPreferences::HasDefaultPrintQuality() const
194 {
195 return hasDefaultPrintQuality_;
196 }
197
GetDefaultPrintQuality() const198 uint32_t PrinterPreferences::GetDefaultPrintQuality() const
199 {
200 return defaultPrintQuality_;
201 }
202
HasDefaultMediaType() const203 bool PrinterPreferences::HasDefaultMediaType() const
204 {
205 return hasDefaultMediaType_;
206 }
207
GetDefaultMediaType() const208 const std::string &PrinterPreferences::GetDefaultMediaType() const
209 {
210 return defaultMediaType_;
211 }
212
HasDefaultPageSizeId() const213 bool PrinterPreferences::HasDefaultPageSizeId() const
214 {
215 return hasDefaultPageSizeId_;
216 }
217
GetDefaultPageSizeId() const218 const std::string &PrinterPreferences::GetDefaultPageSizeId() const
219 {
220 return defaultPageSizeId_;
221 }
222
HasDefaultOrientation() const223 bool PrinterPreferences::HasDefaultOrientation() const
224 {
225 return hasDefaultOrientation_;
226 }
227
GetDefaultOrientation() const228 uint32_t PrinterPreferences::GetDefaultOrientation() const
229 {
230 return defaultOrientation_;
231 }
232
HasDefaultColorMode() const233 bool PrinterPreferences::HasDefaultColorMode() const
234 {
235 return hasDefaultColorMode_;
236 }
237
GetDefaultColorMode() const238 uint32_t PrinterPreferences::GetDefaultColorMode() const
239 {
240 return defaultColorMode_;
241 }
242
HasBorderless() const243 bool PrinterPreferences::HasBorderless() const
244 {
245 return hasBorderless_;
246 }
247
GetBorderless() const248 bool PrinterPreferences::GetBorderless() const
249 {
250 return borderless_;
251 }
252
HasDefaultCollate() const253 bool PrinterPreferences::HasDefaultCollate() const
254 {
255 return hasDefaultCollate_;
256 }
257
GetDefaultCollate() const258 bool PrinterPreferences::GetDefaultCollate() const
259 {
260 return defaultCollate_;
261 }
262
HasDefaultReverse() const263 bool PrinterPreferences::HasDefaultReverse() const
264 {
265 return hasDefaultReverse_;
266 }
267
GetDefaultReverse() const268 bool PrinterPreferences::GetDefaultReverse() const
269 {
270 return defaultReverse_;
271 }
272
HasOption() const273 bool PrinterPreferences::HasOption() const
274 {
275 return hasOption_;
276 }
277
GetOption() const278 std::string PrinterPreferences::GetOption() const
279 {
280 return option_;
281 }
282
ReadFromParcel(Parcel & parcel)283 bool PrinterPreferences::ReadFromParcel(Parcel &parcel)
284 {
285 PrinterPreferences right;
286 if (parcel.GetReadableBytes() == 0) {
287 PRINT_HILOGE("no data in parcel");
288 return false;
289 }
290 right.hasDefaultDuplexMode_ = parcel.ReadBool();
291 if (right.hasDefaultDuplexMode_) {
292 right.SetDefaultDuplexMode(parcel.ReadUint32());
293 }
294
295 right.hasDefaultPrintQuality_ = parcel.ReadBool();
296 if (right.hasDefaultPrintQuality_) {
297 right.SetDefaultPrintQuality(parcel.ReadUint32());
298 }
299
300 right.hasDefaultMediaType_ = parcel.ReadBool();
301 if (right.hasDefaultMediaType_) {
302 right.SetDefaultMediaType(parcel.ReadString());
303 }
304
305 right.hasDefaultPageSizeId_ = parcel.ReadBool();
306 if (right.hasDefaultPageSizeId_) {
307 right.SetDefaultPageSizeId(parcel.ReadString());
308 }
309
310 right.hasDefaultOrientation_ = parcel.ReadBool();
311 if (right.hasDefaultOrientation_) {
312 right.SetDefaultOrientation(parcel.ReadUint32());
313 }
314
315 right.hasDefaultColorMode_ = parcel.ReadBool();
316 if (right.hasDefaultColorMode_) {
317 right.SetDefaultColorMode(parcel.ReadUint32());
318 }
319
320 right.hasBorderless_ = parcel.ReadBool();
321 if (right.hasBorderless_) {
322 right.SetBorderless(parcel.ReadBool());
323 }
324
325 right.hasDefaultCollate_ = parcel.ReadBool();
326 if (right.hasDefaultCollate_) {
327 right.SetDefaultCollate(parcel.ReadBool());
328 }
329
330 right.hasDefaultReverse_ = parcel.ReadBool();
331 if (right.hasDefaultReverse_) {
332 right.SetDefaultReverse(parcel.ReadBool());
333 }
334
335 right.hasOption_ = parcel.ReadBool();
336 if (right.hasOption_) {
337 right.SetOption(parcel.ReadString());
338 }
339
340 *this = right;
341 return true;
342 }
343
Marshalling(Parcel & parcel) const344 bool PrinterPreferences::Marshalling(Parcel &parcel) const
345 {
346 parcel.WriteBool(hasDefaultDuplexMode_);
347 if (hasDefaultDuplexMode_) {
348 parcel.WriteUint32(GetDefaultDuplexMode());
349 }
350
351 parcel.WriteBool(hasDefaultPrintQuality_);
352 if (hasDefaultPrintQuality_) {
353 parcel.WriteUint32(GetDefaultPrintQuality());
354 }
355
356 parcel.WriteBool(hasDefaultMediaType_);
357 if (hasDefaultMediaType_) {
358 parcel.WriteString(GetDefaultMediaType());
359 }
360
361 parcel.WriteBool(hasDefaultPageSizeId_);
362 if (hasDefaultPageSizeId_) {
363 parcel.WriteString(GetDefaultPageSizeId());
364 }
365
366 parcel.WriteBool(hasDefaultOrientation_);
367 if (hasDefaultOrientation_) {
368 parcel.WriteUint32(GetDefaultOrientation());
369 }
370
371 parcel.WriteBool(hasDefaultColorMode_);
372 if (hasDefaultColorMode_) {
373 parcel.WriteUint32(GetDefaultColorMode());
374 }
375
376 parcel.WriteBool(hasBorderless_);
377 if (hasBorderless_) {
378 parcel.WriteBool(GetBorderless());
379 }
380
381 parcel.WriteBool(hasDefaultCollate_);
382 if (hasDefaultCollate_) {
383 parcel.WriteBool(GetDefaultCollate());
384 }
385
386 parcel.WriteBool(hasDefaultReverse_);
387 if (hasDefaultReverse_) {
388 parcel.WriteBool(GetDefaultReverse());
389 }
390
391 parcel.WriteBool(hasOption_);
392 if (hasOption_) {
393 parcel.WriteString(GetOption());
394 }
395
396 return true;
397 }
398
Unmarshalling(Parcel & parcel)399 std::shared_ptr<PrinterPreferences> PrinterPreferences::Unmarshalling(Parcel &parcel)
400 {
401 auto nativeObj = std::make_shared<PrinterPreferences>();
402 nativeObj->ReadFromParcel(parcel);
403 return nativeObj;
404 }
405
Dump() const406 void PrinterPreferences::Dump() const
407 {
408 if (hasDefaultDuplexMode_) {
409 PRINT_HILOGI("defaultDuplexMode: %{public}d", defaultDuplexMode_);
410 }
411 if (hasDefaultPrintQuality_) {
412 PRINT_HILOGI("defaultPrintQuality: %{public}d", defaultPrintQuality_);
413 }
414 if (hasDefaultMediaType_) {
415 PRINT_HILOGI("defaultMediaType: %{public}s", defaultMediaType_.c_str());
416 }
417 if (hasDefaultPageSizeId_) {
418 PRINT_HILOGI("defaultPageSizeId: %{public}s", defaultPageSizeId_.c_str());
419 }
420 if (hasDefaultOrientation_) {
421 PRINT_HILOGI("defaultOrientation: %{public}d", defaultOrientation_);
422 }
423 if (hasDefaultColorMode_) {
424 PRINT_HILOGI("defaultColorMode: %{public}d", defaultColorMode_);
425 }
426 if (hasBorderless_) {
427 PRINT_HILOGI("borderless: %{public}d", borderless_);
428 }
429 if (hasDefaultCollate_) {
430 PRINT_HILOGI("defaultCollate: %{public}d", defaultCollate_);
431 }
432 if (hasDefaultReverse_) {
433 PRINT_HILOGI("defaultReverse: %{public}d", defaultReverse_);
434 }
435 if (hasOption_) {
436 PRINT_HILOGD("option: %{private}s", option_.c_str());
437 }
438 }
439
ConvertToJson()440 Json::Value PrinterPreferences::ConvertToJson()
441 {
442 Json::Value preferencesJson;
443 if (hasDefaultDuplexMode_) {
444 preferencesJson["defaultDuplexMode"] = defaultDuplexMode_;
445 }
446 if (hasDefaultPrintQuality_) {
447 preferencesJson["defaultPrintQuality"] = defaultPrintQuality_;
448 }
449 if (hasDefaultMediaType_) {
450 preferencesJson["defaultMediaType"] = defaultMediaType_;
451 }
452 if (hasDefaultPageSizeId_) {
453 preferencesJson["defaultPageSizeId"] = defaultPageSizeId_;
454 }
455 if (hasDefaultOrientation_) {
456 preferencesJson["defaultOrientation"] = defaultOrientation_;
457 }
458 if (hasDefaultColorMode_) {
459 preferencesJson["defaultColorMode"] = defaultColorMode_;
460 }
461 if (hasBorderless_) {
462 preferencesJson["borderless"] = borderless_;
463 }
464 if (hasDefaultCollate_) {
465 preferencesJson["defaultCollate"] = defaultCollate_;
466 }
467 if (hasDefaultReverse_) {
468 preferencesJson["defaultReverse"] = defaultReverse_;
469 }
470
471 if (hasOption_) {
472 if (!PrintJsonUtil::Parse(option_, preferencesJson["options"])) {
473 PRINT_HILOGE("json accept preferences options fail");
474 }
475 }
476 return preferencesJson;
477 }
478
ConvertBoolDefaultJsonToPrinterPreferences(Json::Value & preferencesJson)479 void PrinterPreferences::ConvertBoolDefaultJsonToPrinterPreferences(Json::Value &preferencesJson)
480 {
481 if (PrintJsonUtil::IsMember(preferencesJson, "borderless") && preferencesJson["borderless"].isBool()) {
482 SetBorderless(preferencesJson["borderless"].asBool());
483 }
484
485 if (PrintJsonUtil::IsMember(preferencesJson, "defaultCollate") && preferencesJson["defaultCollate"].isBool()) {
486 SetBorderless(preferencesJson["defaultCollate"].asBool());
487 }
488
489 if (PrintJsonUtil::IsMember(preferencesJson, "defaultReverse") && preferencesJson["defaultReverse"].isBool()) {
490 SetBorderless(preferencesJson["defaultReverse"].asBool());
491 }
492 }
493
ConvertJsonToPrinterPreferences(Json::Value & preferencesJson)494 void PrinterPreferences::ConvertJsonToPrinterPreferences(Json::Value &preferencesJson)
495 {
496 if (PrintJsonUtil::IsMember(preferencesJson, "defaultDuplexMode") &&
497 preferencesJson["defaultDuplexMode"].isInt()) {
498 SetDefaultDuplexMode(preferencesJson["defaultDuplexMode"].asInt());
499 }
500
501 if (PrintJsonUtil::IsMember(preferencesJson, "defaultPrintQuality") &&
502 preferencesJson["defaultPrintQuality"].isInt()) {
503 SetDefaultPrintQuality(preferencesJson["defaultPrintQuality"].asInt());
504 }
505
506 if (PrintJsonUtil::IsMember(preferencesJson, "defaultMediaType") &&
507 preferencesJson["defaultMediaType"].isString()) {
508 SetDefaultMediaType(preferencesJson["defaultMediaType"].asString());
509 }
510
511 if (PrintJsonUtil::IsMember(preferencesJson, "defaultPageSizeId") &&
512 preferencesJson["defaultPageSizeId"].isString()) {
513 SetDefaultPageSizeId(preferencesJson["defaultPageSizeId"].asString());
514 }
515
516 if (PrintJsonUtil::IsMember(preferencesJson, "defaultOrientation") &&
517 preferencesJson["defaultOrientation"].isInt()) {
518 SetDefaultOrientation(preferencesJson["defaultOrientation"].asInt());
519 }
520
521 if (PrintJsonUtil::IsMember(preferencesJson, "defaultColorMode") &&
522 preferencesJson["defaultColorMode"].isInt()) {
523 SetDefaultColorMode(preferencesJson["defaultColorMode"].asInt());
524 }
525
526 if (PrintJsonUtil::IsMember(preferencesJson, "options") && preferencesJson["options"].isObject()) {
527 PRINT_HILOGD("find options");
528 SetOption(PrintJsonUtil::WriteString(preferencesJson["options"]));
529 }
530
531 ConvertBoolDefaultJsonToPrinterPreferences(preferencesJson);
532 }
533 } // namespace OHOS::Print