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_info.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19
20 namespace OHOS::Print {
PrinterInfo()21 PrinterInfo::PrinterInfo()
22 : printerId_(""),
23 printerName_(""),
24 printerState_(PRINTER_UNKNOWN),
25 hasPrinterIcon_(false),
26 printerIcon_(PRINT_INVALID_ID),
27 hasDescription_(false),
28 description_(""),
29 hasPrinterStatus_(false),
30 printerStatus_(PRINTER_STATUS_UNAVAILABLE),
31 hasCapability_(false),
32 hasUri_(false),
33 uri_(""),
34 hasPrinterMake_(false),
35 printerMake_(""),
36 hasPreferences_(false),
37 hasAlias_(false),
38 alias_(""),
39 hasOption_(false),
40 option_(""),
41 hasPrinterUuid_(false),
42 printerUuid_(""),
43 hasIsDefaultPrinter_(false),
44 isDefaultPrinter_(false),
45 hasIsLastUsedPrinter_(false),
46 isLastUsedPrinter_(false)
47 {
48 capability_.Reset();
49 preferences_.Reset();
50 }
51
PrinterInfo(const PrinterInfo & right)52 PrinterInfo::PrinterInfo(const PrinterInfo &right)
53 : printerId_(right.printerId_),
54 printerName_(right.printerName_),
55 printerState_(right.printerState_),
56 hasPrinterIcon_(right.hasPrinterIcon_),
57 printerIcon_(right.printerIcon_),
58 hasDescription_(right.hasDescription_),
59 description_(right.description_),
60 hasPrinterStatus_(right.hasPrinterStatus_),
61 printerStatus_(right.printerStatus_),
62 hasCapability_(right.hasCapability_),
63 capability_(right.capability_),
64 hasUri_(right.hasUri_),
65 uri_(right.uri_),
66 hasPrinterMake_(right.hasPrinterMake_),
67 printerMake_(right.printerMake_),
68 hasPreferences_(right.hasPreferences_),
69 preferences_(right.preferences_),
70 hasAlias_(right.hasAlias_),
71 alias_(right.alias_),
72 hasOption_(right.hasOption_),
73 option_(right.option_),
74 hasPrinterUuid_(right.hasPrinterUuid_),
75 printerUuid_(right.printerUuid_),
76 hasIsDefaultPrinter_(right.hasIsDefaultPrinter_),
77 isDefaultPrinter_(right.isDefaultPrinter_),
78 hasIsLastUsedPrinter_(right.hasIsLastUsedPrinter_),
79 isLastUsedPrinter_(right.isLastUsedPrinter_),
80 ppdHashCode_(right.ppdHashCode_)
81 {
82 }
operator =(const PrinterInfo & right)83 PrinterInfo &PrinterInfo::operator=(const PrinterInfo &right)
84 {
85 if (this != &right) {
86 printerId_ = right.printerId_;
87 printerName_ = right.printerName_;
88 printerState_ = right.printerState_;
89 hasPrinterIcon_ = right.hasPrinterIcon_;
90 printerIcon_ = right.printerIcon_;
91 hasDescription_ = right.hasDescription_;
92 description_ = right.description_;
93 hasCapability_ = right.hasCapability_;
94 capability_ = right.capability_;
95 hasOption_ = right.hasOption_;
96 hasUri_ = right.hasUri_;
97 uri_ = right.uri_;
98 hasPrinterMake_ = right.hasPrinterMake_;
99 printerMake_ = right.printerMake_;
100 hasPreferences_ = right.hasPreferences_;
101 preferences_ = right.preferences_;
102 hasAlias_ = right.hasAlias_;
103 alias_ = right.alias_;
104 hasPrinterUuid_ = right.hasPrinterUuid_,
105 printerUuid_ = right.printerUuid_,
106 option_ = right.option_;
107 hasIsDefaultPrinter_ = right.hasIsDefaultPrinter_;
108 isDefaultPrinter_ = right.isDefaultPrinter_;
109 hasIsLastUsedPrinter_ = right.hasIsLastUsedPrinter_;
110 isLastUsedPrinter_ = right.isLastUsedPrinter_;
111 hasPrinterStatus_ = right.hasPrinterStatus_;
112 printerStatus_ = right.printerStatus_;
113 ppdHashCode_ = right.ppdHashCode_;
114 }
115 return *this;
116 }
117
~PrinterInfo()118 PrinterInfo::~PrinterInfo() {}
119
SetPrinterId(const std::string & printerId)120 void PrinterInfo::SetPrinterId(const std::string &printerId)
121 {
122 printerId_ = printerId;
123 }
124
SetPrinterName(std::string printerName)125 void PrinterInfo::SetPrinterName(std::string printerName)
126 {
127 printerName_ = printerName;
128 }
129
SetPrinterIcon(uint32_t printIcon)130 void PrinterInfo::SetPrinterIcon(uint32_t printIcon)
131 {
132 hasPrinterIcon_ = true;
133 printerIcon_ = printIcon;
134 }
135
SetPrinterState(uint32_t printerState)136 void PrinterInfo::SetPrinterState(uint32_t printerState)
137 {
138 printerState_ = printerState;
139 }
140
SetDescription(std::string description)141 void PrinterInfo::SetDescription(std::string description)
142 {
143 hasDescription_ = true;
144 description_ = description;
145 }
146
SetCapability(const PrinterCapability & capability)147 void PrinterInfo::SetCapability(const PrinterCapability &capability)
148 {
149 hasCapability_ = true;
150 capability_ = capability;
151 }
152
SetUri(const std::string & uri)153 void PrinterInfo::SetUri(const std::string &uri)
154 {
155 hasUri_ = true;
156 uri_ = uri;
157 }
158
SetPrinterMake(const std::string & printerMake)159 void PrinterInfo::SetPrinterMake(const std::string &printerMake)
160 {
161 hasPrinterMake_ = true;
162 printerMake_ = printerMake;
163 }
164
SetPreferences(const PrinterPreferences & preferences)165 void PrinterInfo::SetPreferences(const PrinterPreferences &preferences)
166 {
167 hasPreferences_ = true;
168 preferences_ = preferences;
169 }
170
SetAlias(const std::string & alias)171 void PrinterInfo::SetAlias(const std::string &alias)
172 {
173 hasAlias_ = true;
174 alias_ = alias;
175 }
176
SetPrinterUuid(const std::string & printerUuid)177 void PrinterInfo::SetPrinterUuid(const std::string &printerUuid)
178 {
179 hasPrinterUuid_ = true;
180 printerUuid_ = printerUuid;
181 }
182
SetOption(const std::string & option)183 void PrinterInfo::SetOption(const std::string &option)
184 {
185 hasOption_ = true;
186 option_ = option;
187 }
188
SetIsDefaultPrinter(bool isDefaultPrinter)189 void PrinterInfo::SetIsDefaultPrinter(bool isDefaultPrinter)
190 {
191 hasIsDefaultPrinter_ = true;
192 isDefaultPrinter_ = isDefaultPrinter;
193 }
194
SetIsLastUsedPrinter(bool isLastUsedPrinter)195 void PrinterInfo::SetIsLastUsedPrinter(bool isLastUsedPrinter)
196 {
197 hasIsLastUsedPrinter_ = true;
198 isLastUsedPrinter_ = isLastUsedPrinter;
199 }
200
SetPrinterStatus(uint32_t printerStatus)201 void PrinterInfo::SetPrinterStatus(uint32_t printerStatus)
202 {
203 hasPrinterStatus_ = true;
204 printerStatus_ = printerStatus;
205 }
206
SetPpdHashCode(const std::string & ppdHashCode)207 void PrinterInfo::SetPpdHashCode(const std::string &ppdHashCode)
208 {
209 ppdHashCode_ = ppdHashCode;
210 }
211
GetPrinterId() const212 const std::string &PrinterInfo::GetPrinterId() const
213 {
214 return printerId_;
215 }
216
GetPrinterName() const217 const std::string &PrinterInfo::GetPrinterName() const
218 {
219 return printerName_;
220 }
221
GetPrinterIcon() const222 uint32_t PrinterInfo::GetPrinterIcon() const
223 {
224 return printerIcon_;
225 }
226
GetPrinterState() const227 uint32_t PrinterInfo::GetPrinterState() const
228 {
229 return printerState_;
230 }
231
GetDescription() const232 const std::string &PrinterInfo::GetDescription() const
233 {
234 return description_;
235 }
236
HasCapability() const237 bool PrinterInfo::HasCapability() const
238 {
239 return hasCapability_;
240 }
241
GetCapability(PrinterCapability & cap) const242 void PrinterInfo::GetCapability(PrinterCapability &cap) const
243 {
244 cap = capability_;
245 }
246
HasUri() const247 bool PrinterInfo::HasUri() const
248 {
249 return hasUri_;
250 }
251
GetUri() const252 std::string PrinterInfo::GetUri() const
253 {
254 return uri_;
255 }
256
HasPrinterMake() const257 bool PrinterInfo::HasPrinterMake() const
258 {
259 return hasPrinterMake_;
260 }
261
GetPrinterMake() const262 std::string PrinterInfo::GetPrinterMake() const
263 {
264 return printerMake_;
265 }
266
HasPreferences() const267 bool PrinterInfo::HasPreferences() const
268 {
269 return hasPreferences_;
270 }
271
GetPreferences(PrinterPreferences & preferences) const272 void PrinterInfo::GetPreferences(PrinterPreferences &preferences) const
273 {
274 preferences = preferences_;
275 }
276
HasAlias() const277 bool PrinterInfo::HasAlias() const
278 {
279 return hasAlias_;
280 }
281
GetAlias() const282 std::string PrinterInfo::GetAlias() const
283 {
284 return alias_;
285 }
286
GetPpdHashCode() const287 std::string PrinterInfo::GetPpdHashCode() const
288 {
289 return ppdHashCode_;
290 }
291
HasPrinterUuid() const292 bool PrinterInfo::HasPrinterUuid() const
293 {
294 return hasPrinterUuid_;
295 }
296
GetPrinterUuid() const297 std::string PrinterInfo::GetPrinterUuid() const
298 {
299 return printerUuid_;
300 }
301
HasOption() const302 bool PrinterInfo::HasOption() const
303 {
304 return hasOption_;
305 }
306
GetOption() const307 std::string PrinterInfo::GetOption() const
308 {
309 return option_;
310 }
311
HasIsDefaultPrinter() const312 bool PrinterInfo::HasIsDefaultPrinter() const
313 {
314 return hasIsDefaultPrinter_;
315 }
316
GetIsDefaultPrinter() const317 bool PrinterInfo::GetIsDefaultPrinter() const
318 {
319 return isDefaultPrinter_;
320 }
321
HasIsLastUsedPrinter() const322 bool PrinterInfo::HasIsLastUsedPrinter() const
323 {
324 return hasIsLastUsedPrinter_;
325 }
326
GetIsLastUsedPrinter() const327 bool PrinterInfo::GetIsLastUsedPrinter() const
328 {
329 return isLastUsedPrinter_;
330 }
331
HasPrinterStatus() const332 bool PrinterInfo::HasPrinterStatus() const
333 {
334 return hasPrinterStatus_;
335 }
336
GetPrinterStatus() const337 uint32_t PrinterInfo::GetPrinterStatus() const
338 {
339 return printerStatus_;
340 }
341
ReadFromParcel(Parcel & parcel)342 bool PrinterInfo::ReadFromParcel(Parcel &parcel)
343 {
344 PrinterInfo right;
345 if (parcel.GetReadableBytes() == 0) {
346 PRINT_HILOGE("no data in parcel");
347 return false;
348 }
349 right.SetPrinterId(parcel.ReadString());
350 right.SetPrinterName(parcel.ReadString());
351 right.SetPrinterState(parcel.ReadUint32());
352
353 uint32_t iconId = PRINT_INVALID_ID;
354 right.hasPrinterIcon_ = parcel.ReadBool();
355 if (right.hasPrinterIcon_) {
356 iconId = parcel.ReadUint32();
357 }
358 right.SetPrinterIcon(iconId);
359
360 right.hasDescription_ = parcel.ReadBool();
361 if (right.hasDescription_) {
362 right.SetDescription(parcel.ReadString());
363 }
364
365 right.hasPrinterStatus_ = parcel.ReadBool();
366 if (right.hasPrinterStatus_) {
367 right.SetPrinterStatus(parcel.ReadUint32());
368 }
369
370 right.hasCapability_ = parcel.ReadBool();
371 if (right.hasCapability_) {
372 auto capPtr = PrinterCapability::Unmarshalling(parcel);
373 if (capPtr == nullptr) {
374 PRINT_HILOGE("failed to build capability from printer info");
375 return false;
376 }
377 right.SetCapability(*capPtr);
378 }
379
380 right.hasUri_ = parcel.ReadBool();
381 if (right.hasUri_) {
382 right.SetUri(parcel.ReadString());
383 }
384
385 right.hasPrinterMake_ = parcel.ReadBool();
386 if (right.hasPrinterMake_) {
387 right.SetPrinterMake(parcel.ReadString());
388 }
389
390 ReadInnerPropertyFromParcel(right, parcel);
391
392 right.Dump();
393 *this = right;
394 return true;
395 }
396
ReadInnerPropertyFromParcel(PrinterInfo & right,Parcel & parcel)397 bool PrinterInfo::ReadInnerPropertyFromParcel(PrinterInfo& right, Parcel& parcel)
398 {
399 right.hasPrinterUuid_ = parcel.ReadBool();
400 if (right.hasPrinterUuid_) {
401 right.SetPrinterUuid(parcel.ReadString());
402 }
403
404 right.hasPreferences_ = parcel.ReadBool();
405 if (right.hasPreferences_) {
406 auto preferencesPtr = PrinterPreferences::Unmarshalling(parcel);
407 if (preferencesPtr == nullptr) {
408 PRINT_HILOGE("failed to build preferences from printer info");
409 return false;
410 }
411 right.SetPreferences(*preferencesPtr);
412 }
413
414 right.hasAlias_ = parcel.ReadBool();
415 if (right.hasAlias_) {
416 right.SetAlias(parcel.ReadString());
417 }
418
419 right.hasOption_ = parcel.ReadBool();
420 if (right.hasOption_) {
421 right.SetOption(parcel.ReadString());
422 }
423
424 right.hasIsDefaultPrinter_ = parcel.ReadBool();
425 if (right.hasIsDefaultPrinter_) {
426 right.isDefaultPrinter_ = parcel.ReadBool();
427 }
428
429 right.hasIsLastUsedPrinter_ = parcel.ReadBool();
430 if (right.hasIsLastUsedPrinter_) {
431 right.isLastUsedPrinter_ = parcel.ReadBool();
432 }
433
434 return true;
435 }
436
Marshalling(Parcel & parcel) const437 bool PrinterInfo::Marshalling(Parcel &parcel) const
438 {
439 parcel.WriteString(GetPrinterId());
440 parcel.WriteString(GetPrinterName());
441 parcel.WriteUint32(GetPrinterState());
442
443 parcel.WriteBool(hasPrinterIcon_);
444 if (hasPrinterIcon_) {
445 parcel.WriteUint32(GetPrinterIcon());
446 }
447
448 parcel.WriteBool(hasDescription_);
449 if (hasDescription_) {
450 parcel.WriteString(GetDescription());
451 }
452
453 parcel.WriteBool(hasPrinterStatus_);
454 if (hasPrinterStatus_) {
455 parcel.WriteUint32(GetPrinterStatus());
456 }
457
458 parcel.WriteBool(hasCapability_);
459 if (hasCapability_) {
460 capability_.Marshalling(parcel);
461 }
462
463 parcel.WriteBool(hasUri_);
464 if (hasUri_) {
465 parcel.WriteString(GetUri());
466 }
467
468 parcel.WriteBool(hasPrinterMake_);
469 if (hasPrinterMake_) {
470 parcel.WriteString(GetPrinterMake());
471 }
472
473 parcel.WriteBool(hasPrinterUuid_);
474 if (hasPrinterUuid_) {
475 parcel.WriteString(GetPrinterUuid());
476 }
477
478 MarshallingInnerProperty(parcel);
479
480 return true;
481 }
482
MarshallingInnerProperty(Parcel & parcel) const483 void PrinterInfo::MarshallingInnerProperty(Parcel &parcel) const
484 {
485 parcel.WriteBool(hasPreferences_);
486 if (hasPreferences_) {
487 preferences_.Marshalling(parcel);
488 }
489
490 parcel.WriteBool(hasAlias_);
491 if (hasAlias_) {
492 parcel.WriteString(GetAlias());
493 }
494
495 parcel.WriteBool(hasOption_);
496 if (hasOption_) {
497 parcel.WriteString(GetOption());
498 }
499
500 parcel.WriteBool(hasIsDefaultPrinter_);
501 if (hasIsDefaultPrinter_) {
502 parcel.WriteBool(isDefaultPrinter_);
503 }
504
505 parcel.WriteBool(hasIsLastUsedPrinter_);
506 if (hasIsLastUsedPrinter_) {
507 parcel.WriteBool(isLastUsedPrinter_);
508 }
509 }
510
Unmarshalling(Parcel & parcel)511 std::shared_ptr<PrinterInfo> PrinterInfo::Unmarshalling(Parcel &parcel)
512 {
513 auto nativeObj = std::make_shared<PrinterInfo>();
514 nativeObj->ReadFromParcel(parcel);
515 return nativeObj;
516 }
517
Dump() const518 void PrinterInfo::Dump() const
519 {
520 PRINT_HILOGD("printerId: %{private}s", printerId_.c_str());
521 PRINT_HILOGD("printerName: %{private}s", printerName_.c_str());
522 PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
523 PRINT_HILOGD("printerState: %{public}d", printerState_);
524
525 if (hasPrinterIcon_) {
526 PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
527 }
528 if (hasDescription_) {
529 PRINT_HILOGD("description: %{private}s", description_.c_str());
530 }
531 if (hasPrinterStatus_) {
532 PRINT_HILOGD("printerStatus: %{public}d", printerStatus_);
533 }
534 if (hasCapability_) {
535 capability_.Dump();
536 }
537 if (hasUri_) {
538 PRINT_HILOGD("uri: %{private}s", uri_.c_str());
539 }
540 if (hasPrinterMake_) {
541 PRINT_HILOGD("printerMake: %{private}s", printerMake_.c_str());
542 }
543 if (hasPreferences_) {
544 preferences_.Dump();
545 }
546 if (hasAlias_) {
547 PRINT_HILOGD("alias: %{private}s", alias_.c_str());
548 }
549 if (hasPrinterUuid_) {
550 PRINT_HILOGD("printerUuid: %{private}s", printerUuid_.c_str());
551 }
552 if (hasOption_) {
553 PRINT_HILOGD("option: %{private}s", option_.c_str());
554 }
555 if (hasIsDefaultPrinter_) {
556 PRINT_HILOGD("isDefaultPrinter: %{public}d", isDefaultPrinter_);
557 }
558 if (hasIsLastUsedPrinter_) {
559 PRINT_HILOGD("isLastUsedPrinter: %{public}d", isLastUsedPrinter_);
560 }
561 }
562 } // namespace OHOS::Print