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 {
81 }
operator =(const PrinterInfo & right)82 PrinterInfo &PrinterInfo::operator=(const PrinterInfo &right)
83 {
84 if (this != &right) {
85 printerId_ = right.printerId_;
86 printerName_ = right.printerName_;
87 printerState_ = right.printerState_;
88 hasPrinterIcon_ = right.hasPrinterIcon_;
89 printerIcon_ = right.printerIcon_;
90 hasDescription_ = right.hasDescription_;
91 description_ = right.description_;
92 hasCapability_ = right.hasCapability_;
93 capability_ = right.capability_;
94 hasOption_ = right.hasOption_;
95 hasUri_ = right.hasUri_;
96 uri_ = right.uri_;
97 hasPrinterMake_ = right.hasPrinterMake_;
98 printerMake_ = right.printerMake_;
99 hasPreferences_ = right.hasPreferences_;
100 preferences_ = right.preferences_;
101 hasAlias_ = right.hasAlias_;
102 alias_ = right.alias_;
103 hasPrinterUuid_ = right.hasPrinterUuid_,
104 printerUuid_ = right.printerUuid_,
105 option_ = right.option_;
106 hasIsDefaultPrinter_ = right.hasIsDefaultPrinter_;
107 isDefaultPrinter_ = right.isDefaultPrinter_;
108 hasIsLastUsedPrinter_ = right.hasIsLastUsedPrinter_;
109 isLastUsedPrinter_ = right.isLastUsedPrinter_;
110 hasPrinterStatus_ = right.hasPrinterStatus_;
111 printerStatus_ = right.printerStatus_;
112 }
113 return *this;
114 }
115
~PrinterInfo()116 PrinterInfo::~PrinterInfo() {}
117
SetPrinterId(const std::string & printerId)118 void PrinterInfo::SetPrinterId(const std::string &printerId)
119 {
120 printerId_ = printerId;
121 }
122
SetPrinterName(std::string printerName)123 void PrinterInfo::SetPrinterName(std::string printerName)
124 {
125 printerName_ = printerName;
126 }
127
SetPrinterIcon(uint32_t printIcon)128 void PrinterInfo::SetPrinterIcon(uint32_t printIcon)
129 {
130 hasPrinterIcon_ = true;
131 printerIcon_ = printIcon;
132 }
133
SetPrinterState(uint32_t printerState)134 void PrinterInfo::SetPrinterState(uint32_t printerState)
135 {
136 printerState_ = printerState;
137 }
138
SetDescription(std::string description)139 void PrinterInfo::SetDescription(std::string description)
140 {
141 hasDescription_ = true;
142 description_ = description;
143 }
144
SetCapability(const PrinterCapability & capability)145 void PrinterInfo::SetCapability(const PrinterCapability &capability)
146 {
147 hasCapability_ = true;
148 capability_ = capability;
149 }
150
SetUri(const std::string & uri)151 void PrinterInfo::SetUri(const std::string &uri)
152 {
153 hasUri_ = true;
154 uri_ = uri;
155 }
156
SetPrinterMake(const std::string & printerMake)157 void PrinterInfo::SetPrinterMake(const std::string &printerMake)
158 {
159 hasPrinterMake_ = true;
160 printerMake_ = printerMake;
161 }
162
SetPreferences(const PrinterPreferences & preferences)163 void PrinterInfo::SetPreferences(const PrinterPreferences &preferences)
164 {
165 hasPreferences_ = true;
166 preferences_ = preferences;
167 }
168
SetAlias(const std::string & alias)169 void PrinterInfo::SetAlias(const std::string &alias)
170 {
171 hasAlias_ = true;
172 alias_ = alias;
173 }
174
SetPrinterUuid(const std::string & printerUuid)175 void PrinterInfo::SetPrinterUuid(const std::string &printerUuid)
176 {
177 hasPrinterUuid_ = true;
178 printerUuid_ = printerUuid;
179 }
180
SetOption(const std::string & option)181 void PrinterInfo::SetOption(const std::string &option)
182 {
183 hasOption_ = true;
184 option_ = option;
185 }
186
SetIsDefaultPrinter(bool isDefaultPrinter)187 void PrinterInfo::SetIsDefaultPrinter(bool isDefaultPrinter)
188 {
189 hasIsDefaultPrinter_ = true;
190 isDefaultPrinter_ = true;
191 }
192
SetIsLastUsedPrinter(bool isLastUsedPrinter)193 void PrinterInfo::SetIsLastUsedPrinter(bool isLastUsedPrinter)
194 {
195 hasIsLastUsedPrinter_ = true;
196 isLastUsedPrinter_ = true;
197 }
198
SetPrinterStatus(uint32_t printerStatus)199 void PrinterInfo::SetPrinterStatus(uint32_t printerStatus)
200 {
201 hasPrinterStatus_ = true;
202 printerStatus_ = printerStatus;
203 }
204
GetPrinterId() const205 const std::string &PrinterInfo::GetPrinterId() const
206 {
207 return printerId_;
208 }
209
GetPrinterName() const210 const std::string &PrinterInfo::GetPrinterName() const
211 {
212 return printerName_;
213 }
214
GetPrinterIcon() const215 uint32_t PrinterInfo::GetPrinterIcon() const
216 {
217 return printerIcon_;
218 }
219
GetPrinterState() const220 uint32_t PrinterInfo::GetPrinterState() const
221 {
222 return printerState_;
223 }
224
GetDescription() const225 const std::string &PrinterInfo::GetDescription() const
226 {
227 return description_;
228 }
229
HasCapability() const230 bool PrinterInfo::HasCapability() const
231 {
232 return hasCapability_;
233 }
234
GetCapability(PrinterCapability & cap) const235 void PrinterInfo::GetCapability(PrinterCapability &cap) const
236 {
237 cap = capability_;
238 }
239
HasUri() const240 bool PrinterInfo::HasUri() const
241 {
242 return hasUri_;
243 }
244
GetUri() const245 std::string PrinterInfo::GetUri() const
246 {
247 return uri_;
248 }
249
HasPrinterMake() const250 bool PrinterInfo::HasPrinterMake() const
251 {
252 return hasPrinterMake_;
253 }
254
GetPrinterMake() const255 std::string PrinterInfo::GetPrinterMake() const
256 {
257 return printerMake_;
258 }
259
HasPreferences() const260 bool PrinterInfo::HasPreferences() const
261 {
262 return hasPreferences_;
263 }
264
GetPreferences(PrinterPreferences & preferences) const265 void PrinterInfo::GetPreferences(PrinterPreferences &preferences) const
266 {
267 preferences = preferences_;
268 }
269
HasAlias() const270 bool PrinterInfo::HasAlias() const
271 {
272 return hasAlias_;
273 }
274
GetAlias() const275 std::string PrinterInfo::GetAlias() const
276 {
277 return alias_;
278 }
279
HasPrinterUuid() const280 bool PrinterInfo::HasPrinterUuid() const
281 {
282 return hasPrinterUuid_;
283 }
284
GetPrinterUuid() const285 std::string PrinterInfo::GetPrinterUuid() const
286 {
287 return printerUuid_;
288 }
289
HasOption() const290 bool PrinterInfo::HasOption() const
291 {
292 return hasOption_;
293 }
294
GetOption() const295 std::string PrinterInfo::GetOption() const
296 {
297 return option_;
298 }
299
HasIsDefaultPrinter() const300 bool PrinterInfo::HasIsDefaultPrinter() const
301 {
302 return hasIsDefaultPrinter_;
303 }
304
GetIsDefaultPrinter() const305 bool PrinterInfo::GetIsDefaultPrinter() const
306 {
307 return isDefaultPrinter_;
308 }
309
HasIsLastUsedPrinter() const310 bool PrinterInfo::HasIsLastUsedPrinter() const
311 {
312 return hasIsLastUsedPrinter_;
313 }
314
GetIsLastUsedPrinter() const315 bool PrinterInfo::GetIsLastUsedPrinter() const
316 {
317 return isLastUsedPrinter_;
318 }
319
HasPrinterStatus() const320 bool PrinterInfo::HasPrinterStatus() const
321 {
322 return hasPrinterStatus_;
323 }
324
GetPrinterStatus() const325 uint32_t PrinterInfo::GetPrinterStatus() const
326 {
327 return printerStatus_;
328 }
329
ReadFromParcel(Parcel & parcel)330 bool PrinterInfo::ReadFromParcel(Parcel &parcel)
331 {
332 PrinterInfo right;
333 if (parcel.GetReadableBytes() == 0) {
334 PRINT_HILOGE("no data in parcel");
335 return false;
336 }
337 right.SetPrinterId(parcel.ReadString());
338 right.SetPrinterName(parcel.ReadString());
339 right.SetPrinterState(parcel.ReadUint32());
340
341 uint32_t iconId = PRINT_INVALID_ID;
342 right.hasPrinterIcon_ = parcel.ReadBool();
343 if (right.hasPrinterIcon_) {
344 iconId = parcel.ReadUint32();
345 }
346 right.SetPrinterIcon(iconId);
347
348 right.hasDescription_ = parcel.ReadBool();
349 if (right.hasDescription_) {
350 right.SetDescription(parcel.ReadString());
351 }
352
353 right.hasPrinterStatus_ = parcel.ReadBool();
354 if (right.hasPrinterStatus_) {
355 right.SetPrinterStatus(parcel.ReadUint32());
356 }
357
358 right.hasCapability_ = parcel.ReadBool();
359 if (right.hasCapability_) {
360 auto capPtr = PrinterCapability::Unmarshalling(parcel);
361 if (capPtr == nullptr) {
362 PRINT_HILOGE("failed to build capability from printer info");
363 return false;
364 }
365 right.SetCapability(*capPtr);
366 }
367
368 right.hasUri_ = parcel.ReadBool();
369 if (right.hasUri_) {
370 right.SetUri(parcel.ReadString());
371 }
372
373 right.hasPrinterMake_ = parcel.ReadBool();
374 if (right.hasPrinterMake_) {
375 right.SetPrinterMake(parcel.ReadString());
376 }
377
378 ReadInnerPropertyFromParcel(right, parcel);
379
380 right.Dump();
381 *this = right;
382 return true;
383 }
384
ReadInnerPropertyFromParcel(PrinterInfo & right,Parcel & parcel)385 bool PrinterInfo::ReadInnerPropertyFromParcel(PrinterInfo& right, Parcel& parcel)
386 {
387 right.hasPrinterUuid_ = parcel.ReadBool();
388 if (right.hasPrinterUuid_) {
389 right.SetPrinterUuid(parcel.ReadString());
390 }
391
392 right.hasPreferences_ = parcel.ReadBool();
393 if (right.hasPreferences_) {
394 auto preferencesPtr = PrinterPreferences::Unmarshalling(parcel);
395 if (preferencesPtr == nullptr) {
396 PRINT_HILOGE("failed to build preferences from printer info");
397 return false;
398 }
399 right.SetPreferences(*preferencesPtr);
400 }
401
402 right.hasAlias_ = parcel.ReadBool();
403 if (right.hasAlias_) {
404 right.SetAlias(parcel.ReadString());
405 }
406
407 right.hasOption_ = parcel.ReadBool();
408 if (right.hasOption_) {
409 right.SetOption(parcel.ReadString());
410 }
411
412 right.hasIsDefaultPrinter_ = parcel.ReadBool();
413 if (right.hasIsDefaultPrinter_) {
414 right.isDefaultPrinter_ = parcel.ReadBool();
415 }
416
417 right.hasIsLastUsedPrinter_ = parcel.ReadBool();
418 if (right.hasIsLastUsedPrinter_) {
419 right.isLastUsedPrinter_ = parcel.ReadBool();
420 }
421
422 return true;
423 }
424
Marshalling(Parcel & parcel) const425 bool PrinterInfo::Marshalling(Parcel &parcel) const
426 {
427 parcel.WriteString(GetPrinterId());
428 parcel.WriteString(GetPrinterName());
429 parcel.WriteUint32(GetPrinterState());
430
431 parcel.WriteBool(hasPrinterIcon_);
432 if (hasPrinterIcon_) {
433 parcel.WriteUint32(GetPrinterIcon());
434 }
435
436 parcel.WriteBool(hasDescription_);
437 if (hasDescription_) {
438 parcel.WriteString(GetDescription());
439 }
440
441 parcel.WriteBool(hasPrinterStatus_);
442 if (hasPrinterStatus_) {
443 parcel.WriteUint32(GetPrinterStatus());
444 }
445
446 parcel.WriteBool(hasCapability_);
447 if (hasCapability_) {
448 capability_.Marshalling(parcel);
449 }
450
451 parcel.WriteBool(hasUri_);
452 if (hasUri_) {
453 parcel.WriteString(GetUri());
454 }
455
456 parcel.WriteBool(hasPrinterMake_);
457 if (hasPrinterMake_) {
458 parcel.WriteString(GetPrinterMake());
459 }
460
461 parcel.WriteBool(hasPrinterUuid_);
462 if (hasPrinterUuid_) {
463 parcel.WriteString(GetPrinterUuid());
464 }
465
466 MarshallingInnerProperty(parcel);
467
468 return true;
469 }
470
MarshallingInnerProperty(Parcel & parcel) const471 void PrinterInfo::MarshallingInnerProperty(Parcel &parcel) const
472 {
473 parcel.WriteBool(hasPreferences_);
474 if (hasPreferences_) {
475 preferences_.Marshalling(parcel);
476 }
477
478 parcel.WriteBool(hasAlias_);
479 if (hasAlias_) {
480 parcel.WriteString(GetAlias());
481 }
482
483 parcel.WriteBool(hasOption_);
484 if (hasOption_) {
485 parcel.WriteString(GetOption());
486 }
487
488 parcel.WriteBool(hasIsDefaultPrinter_);
489 if (hasIsDefaultPrinter_) {
490 parcel.WriteBool(isDefaultPrinter_);
491 }
492
493 parcel.WriteBool(hasIsLastUsedPrinter_);
494 if (hasIsLastUsedPrinter_) {
495 parcel.WriteBool(isLastUsedPrinter_);
496 }
497 }
498
Unmarshalling(Parcel & parcel)499 std::shared_ptr<PrinterInfo> PrinterInfo::Unmarshalling(Parcel &parcel)
500 {
501 auto nativeObj = std::make_shared<PrinterInfo>();
502 nativeObj->ReadFromParcel(parcel);
503 return nativeObj;
504 }
505
Dump() const506 void PrinterInfo::Dump() const
507 {
508 PRINT_HILOGD("printerId: %{private}s", printerId_.c_str());
509 PRINT_HILOGD("printerName: %{private}s", printerName_.c_str());
510 PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
511 PRINT_HILOGD("printerState: %{public}d", printerState_);
512
513 if (hasPrinterIcon_) {
514 PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
515 }
516 if (hasDescription_) {
517 PRINT_HILOGD("description: %{private}s", description_.c_str());
518 }
519 if (hasPrinterStatus_) {
520 PRINT_HILOGD("printerStatus: %{public}d", printerStatus_);
521 }
522 if (hasCapability_) {
523 capability_.Dump();
524 }
525 if (hasUri_) {
526 PRINT_HILOGD("uri: %{private}s", uri_.c_str());
527 }
528 if (hasPrinterMake_) {
529 PRINT_HILOGD("printerMake: %{private}s", printerMake_.c_str());
530 }
531 if (hasPreferences_) {
532 preferences_.Dump();
533 }
534 if (hasAlias_) {
535 PRINT_HILOGD("alias: %{private}s", alias_.c_str());
536 }
537 if (hasPrinterUuid_) {
538 PRINT_HILOGD("printerUuid: %{private}s", printerUuid_.c_str());
539 }
540 if (hasOption_) {
541 PRINT_HILOGD("option: %{private}s", option_.c_str());
542 }
543 if (hasIsDefaultPrinter_) {
544 PRINT_HILOGD("isDefaultPrinter: %{public}d", isDefaultPrinter_);
545 }
546 if (hasIsLastUsedPrinter_) {
547 PRINT_HILOGD("isLastUsedPrinter: %{public}d", isLastUsedPrinter_);
548 }
549 }
550 } // namespace OHOS::Print