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       hasOption_(false),
37       option_(""),
38       hasIsDefaultPrinter_(false),
39       isDefaultPrinter_(false),
40       hasIsLastUsedPrinter_(false),
41       isLastUsedPrinter_(false)
42 {
43     capability_.Reset();
44 }
45 
PrinterInfo(const PrinterInfo & right)46 PrinterInfo::PrinterInfo(const PrinterInfo &right)
47     : printerId_(right.printerId_),
48       printerName_(right.printerName_),
49       printerState_(right.printerState_),
50       hasPrinterIcon_(right.hasPrinterIcon_),
51       printerIcon_(right.printerIcon_),
52       hasDescription_(right.hasDescription_),
53       description_(right.description_),
54       hasPrinterStatus_(right.hasPrinterStatus_),
55       printerStatus_(right.printerStatus_),
56       hasCapability_(right.hasCapability_),
57       capability_(right.capability_),
58       hasUri_(right.hasUri_),
59       uri_(right.uri_),
60       hasPrinterMake_(right.hasPrinterMake_),
61       printerMake_(right.printerMake_),
62       hasOption_(right.hasOption_),
63       option_(right.option_),
64       hasIsDefaultPrinter_(right.hasIsDefaultPrinter_),
65       isDefaultPrinter_(right.isDefaultPrinter_),
66       hasIsLastUsedPrinter_(right.hasIsLastUsedPrinter_),
67       isLastUsedPrinter_(right.isLastUsedPrinter_)
68 {
69 }
operator =(const PrinterInfo & right)70 PrinterInfo &PrinterInfo::operator=(const PrinterInfo &right)
71 {
72     if (this != &right) {
73         printerId_ = right.printerId_;
74         printerName_ = right.printerName_;
75         printerState_ = right.printerState_;
76         hasPrinterIcon_ = right.hasPrinterIcon_;
77         printerIcon_ = right.printerIcon_;
78         hasDescription_ = right.hasDescription_;
79         description_ = right.description_;
80         hasCapability_ = right.hasCapability_;
81         capability_ = right.capability_;
82         hasOption_ = right.hasOption_;
83         hasUri_ = right.hasUri_;
84         uri_ = right.uri_;
85         hasPrinterMake_ = right.hasPrinterMake_;
86         printerMake_ = right.printerMake_;
87         option_ = right.option_;
88         hasIsDefaultPrinter_ = right.hasIsDefaultPrinter_;
89         isDefaultPrinter_ = right.isDefaultPrinter_;
90         hasIsLastUsedPrinter_ = right.hasIsLastUsedPrinter_;
91         isLastUsedPrinter_ = right.isLastUsedPrinter_;
92         hasPrinterStatus_ = right.hasPrinterStatus_;
93         printerStatus_ = right.printerStatus_;
94     }
95     return *this;
96 }
97 
~PrinterInfo()98 PrinterInfo::~PrinterInfo() {}
99 
SetPrinterId(const std::string & printerId)100 void PrinterInfo::SetPrinterId(const std::string &printerId)
101 {
102     printerId_ = printerId;
103 }
104 
SetPrinterName(std::string printerName)105 void PrinterInfo::SetPrinterName(std::string printerName)
106 {
107     printerName_ = printerName;
108 }
109 
SetPrinterIcon(uint32_t printIcon)110 void PrinterInfo::SetPrinterIcon(uint32_t printIcon)
111 {
112     printerIcon_ = printIcon;
113 }
114 
SetPrinterState(uint32_t printerState)115 void PrinterInfo::SetPrinterState(uint32_t printerState)
116 {
117     printerState_ = printerState;
118 }
119 
SetDescription(std::string description)120 void PrinterInfo::SetDescription(std::string description)
121 {
122     description_ = description;
123 }
124 
SetCapability(const PrinterCapability & capability)125 void PrinterInfo::SetCapability(const PrinterCapability &capability)
126 {
127     hasCapability_ = true;
128     capability_ = capability;
129 }
130 
SetUri(const std::string & uri)131 void PrinterInfo::SetUri(const std::string &uri)
132 {
133     hasUri_ = true;
134     uri_ = uri;
135 }
136 
SetPrinterMake(const std::string & printerMake)137 void PrinterInfo::SetPrinterMake(const std::string &printerMake)
138 {
139     hasPrinterMake_ = true;
140     printerMake_ = printerMake;
141 }
142 
SetOption(const std::string & option)143 void PrinterInfo::SetOption(const std::string &option)
144 {
145     hasOption_ = true;
146     option_ = option;
147 }
148 
SetIsDefaultPrinter(bool isDefaultPrinter)149 void PrinterInfo::SetIsDefaultPrinter(bool isDefaultPrinter)
150 {
151     hasIsDefaultPrinter_ = true;
152     isDefaultPrinter_ = true;
153 }
154 
SetIsLastUsedPrinter(bool isLastUsedPrinter)155 void PrinterInfo::SetIsLastUsedPrinter(bool isLastUsedPrinter)
156 {
157     hasIsLastUsedPrinter_ = true;
158     isLastUsedPrinter_ = true;
159 }
160 
SetPrinterStatus(uint32_t printerStatus)161 void PrinterInfo::SetPrinterStatus(uint32_t printerStatus)
162 {
163     hasPrinterStatus_ = true;
164     printerStatus_ = printerStatus;
165 }
166 
GetPrinterId() const167 const std::string &PrinterInfo::GetPrinterId() const
168 {
169     return printerId_;
170 }
171 
GetPrinterName() const172 const std::string &PrinterInfo::GetPrinterName() const
173 {
174     return printerName_;
175 }
176 
GetPrinterIcon() const177 uint32_t PrinterInfo::GetPrinterIcon() const
178 {
179     return printerIcon_;
180 }
181 
GetPrinterState() const182 uint32_t PrinterInfo::GetPrinterState() const
183 {
184     return printerState_;
185 }
186 
GetDescription() const187 const std::string &PrinterInfo::GetDescription() const
188 {
189     return description_;
190 }
191 
HasCapability() const192 bool PrinterInfo::HasCapability() const
193 {
194     return hasCapability_;
195 }
196 
GetCapability(PrinterCapability & cap) const197 void PrinterInfo::GetCapability(PrinterCapability &cap) const
198 {
199     cap = capability_;
200 }
201 
HasUri() const202 bool PrinterInfo::HasUri() const
203 {
204     return hasUri_;
205 }
206 
GetUri() const207 std::string PrinterInfo::GetUri() const
208 {
209     return uri_;
210 }
211 
HasPrinterMake() const212 bool PrinterInfo::HasPrinterMake() const
213 {
214     return hasPrinterMake_;
215 }
216 
GetPrinterMake() const217 std::string PrinterInfo::GetPrinterMake() const
218 {
219     return printerMake_;
220 }
221 
HasOption() const222 bool PrinterInfo::HasOption() const
223 {
224     return hasOption_;
225 }
226 
GetOption() const227 std::string PrinterInfo::GetOption() const
228 {
229     return option_;
230 }
231 
HasIsDefaultPrinter() const232 bool PrinterInfo::HasIsDefaultPrinter() const
233 {
234     return hasIsDefaultPrinter_;
235 }
236 
GetIsDefaultPrinter() const237 bool PrinterInfo::GetIsDefaultPrinter() const
238 {
239     return isDefaultPrinter_;
240 }
241 
HasIsLastUsedPrinter() const242 bool PrinterInfo::HasIsLastUsedPrinter() const
243 {
244     return hasIsLastUsedPrinter_;
245 }
246 
GetIsLastUsedPrinter() const247 bool PrinterInfo::GetIsLastUsedPrinter() const
248 {
249     return isLastUsedPrinter_;
250 }
251 
HasPrinterStatus() const252 bool PrinterInfo::HasPrinterStatus() const
253 {
254     return hasPrinterStatus_;
255 }
256 
GetPrinterStatus() const257 uint32_t PrinterInfo::GetPrinterStatus() const
258 {
259     return printerStatus_;
260 }
261 
ReadFromParcel(Parcel & parcel)262 bool PrinterInfo::ReadFromParcel(Parcel &parcel)
263 {
264     PrinterInfo right;
265     if (parcel.GetReadableBytes() == 0) {
266         PRINT_HILOGE("no data in parcel");
267         return false;
268     }
269     right.SetPrinterId(parcel.ReadString());
270     right.SetPrinterName(parcel.ReadString());
271     right.SetPrinterState(parcel.ReadUint32());
272 
273     uint32_t iconId = PRINT_INVALID_ID;
274     right.hasPrinterIcon_ = parcel.ReadBool();
275     if (right.hasPrinterIcon_) {
276         iconId = parcel.ReadUint32();
277     }
278     right.SetPrinterIcon(iconId);
279 
280     right.hasDescription_ = parcel.ReadBool();
281     if (right.hasDescription_) {
282         right.SetDescription(parcel.ReadString());
283     }
284 
285     right.hasPrinterStatus_ = parcel.ReadBool();
286     if (right.hasPrinterStatus_) {
287         right.SetPrinterStatus(parcel.ReadUint32());
288     }
289 
290     right.hasCapability_ = parcel.ReadBool();
291     if (right.hasCapability_) {
292         auto capPtr = PrinterCapability::Unmarshalling(parcel);
293         if (capPtr == nullptr) {
294             PRINT_HILOGE("failed to build capability from printer info");
295             return false;
296         }
297         right.SetCapability(*capPtr);
298     }
299 
300     right.hasUri_ = parcel.ReadBool();
301     if (right.hasUri_) {
302         right.SetUri(parcel.ReadString());
303     }
304 
305     right.hasPrinterMake_ = parcel.ReadBool();
306     if (right.hasPrinterMake_) {
307         right.SetPrinterMake(parcel.ReadString());
308     }
309 
310     right.hasOption_ = parcel.ReadBool();
311     if (right.hasOption_) {
312         right.SetOption(parcel.ReadString());
313     }
314     ReadInnerPropertyFromParcel(right, parcel);
315 
316     right.Dump();
317     *this = right;
318     return true;
319 }
320 
ReadInnerPropertyFromParcel(PrinterInfo & right,Parcel & parcel)321 void PrinterInfo::ReadInnerPropertyFromParcel(PrinterInfo& right, Parcel& parcel)
322 {
323     right.hasIsDefaultPrinter_ = parcel.ReadBool();
324     if (right.hasIsDefaultPrinter_) {
325         right.isDefaultPrinter_ = parcel.ReadBool();
326     }
327 
328     right.hasIsLastUsedPrinter_ = parcel.ReadBool();
329     if (right.hasIsLastUsedPrinter_) {
330         right.isLastUsedPrinter_ = parcel.ReadBool();
331     }
332 }
333 
Marshalling(Parcel & parcel) const334 bool PrinterInfo::Marshalling(Parcel &parcel) const
335 {
336     parcel.WriteString(GetPrinterId());
337     parcel.WriteString(GetPrinterName());
338     parcel.WriteUint32(GetPrinterState());
339 
340     parcel.WriteBool(hasPrinterIcon_);
341     if (hasPrinterIcon_) {
342         parcel.WriteUint32(GetPrinterIcon());
343     }
344 
345     parcel.WriteBool(hasDescription_);
346     if (hasDescription_) {
347         parcel.WriteString(GetDescription());
348     }
349 
350     parcel.WriteBool(hasPrinterStatus_);
351     if (hasPrinterStatus_) {
352         parcel.WriteUint32(GetPrinterStatus());
353     }
354 
355     parcel.WriteBool(hasCapability_);
356     if (hasCapability_) {
357         capability_.Marshalling(parcel);
358     }
359 
360     parcel.WriteBool(hasUri_);
361     if (hasUri_) {
362         parcel.WriteString(GetUri());
363     }
364 
365     parcel.WriteBool(hasPrinterMake_);
366     if (hasPrinterMake_) {
367         parcel.WriteString(GetPrinterMake());
368     }
369 
370     parcel.WriteBool(hasOption_);
371     if (hasOption_) {
372         parcel.WriteString(GetOption());
373     }
374 
375     parcel.WriteBool(hasIsDefaultPrinter_);
376     if (hasIsDefaultPrinter_) {
377         parcel.WriteBool(isDefaultPrinter_);
378     }
379 
380     parcel.WriteBool(hasIsLastUsedPrinter_);
381     if (hasIsLastUsedPrinter_) {
382         parcel.WriteBool(isLastUsedPrinter_);
383     }
384 
385     return true;
386 }
387 
Unmarshalling(Parcel & parcel)388 std::shared_ptr<PrinterInfo> PrinterInfo::Unmarshalling(Parcel &parcel)
389 {
390     auto nativeObj = std::make_shared<PrinterInfo>();
391     nativeObj->ReadFromParcel(parcel);
392     return nativeObj;
393 }
394 
Dump() const395 void PrinterInfo::Dump() const
396 {
397     PRINT_HILOGD("printerId: %{private}s", printerId_.c_str());
398     PRINT_HILOGD("printerName: %{private}s", printerName_.c_str());
399     PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
400     PRINT_HILOGD("printerState: %{public}d", printerState_);
401 
402     if (hasPrinterIcon_) {
403         PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
404     }
405     if (hasDescription_) {
406         PRINT_HILOGD("description: %{private}s", description_.c_str());
407     }
408     if (hasPrinterStatus_) {
409         PRINT_HILOGD("printerStatus: %{public}d", printerStatus_);
410     }
411     if (hasCapability_) {
412         capability_.Dump();
413     }
414     if (hasUri_) {
415         PRINT_HILOGD("uri: %{private}s", uri_.c_str());
416     }
417     if (hasPrinterMake_) {
418         PRINT_HILOGD("printerMake: %{private}s", printerMake_.c_str());
419     }
420     if (hasOption_) {
421         PRINT_HILOGD("option: %{private}s", option_.c_str());
422     }
423     if (hasIsDefaultPrinter_) {
424         PRINT_HILOGD("isDefaultPrinter: %{public}d", isDefaultPrinter_);
425     }
426     if (hasIsLastUsedPrinter_) {
427         PRINT_HILOGD("isLastUsedPrinter: %{public}d", isLastUsedPrinter_);
428     }
429 }
430 }  // namespace OHOS::Print