• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_(""), printerName_(""), printerState_(PRINTER_UNKNOWN), printerIcon_(PRINT_INVALID_ID),
23       description_(""), hasCapability_(false), hasOption_(false), option_(""), hasIsDefaultPrinter_(false),
24       isDefaultPrinter_(false), hasIsLastUsedPrinter_(false), isLastUsedPrinter_(false), hasPrinterStatus_(false),
25       printerStatus_(PRINTER_STATUS_UNAVAILABLE)
26 {
27     capability_.Reset();
28 }
29 
PrinterInfo(const PrinterInfo & right)30 PrinterInfo::PrinterInfo(const PrinterInfo &right)
31 {
32     printerId_ = right.printerId_;
33     printerName_ = right.printerName_;
34     printerState_ = right.printerState_;
35     printerIcon_ = right.printerIcon_;
36     description_ = right.description_;
37     hasCapability_ = right.hasCapability_;
38     capability_ = right.capability_;
39     hasOption_ = right.hasOption_;
40     option_= right.option_;
41     hasIsDefaultPrinter_ = right.hasIsDefaultPrinter_;
42     isDefaultPrinter_ = right.isDefaultPrinter_;
43     hasIsLastUsedPrinter_ = right.hasIsLastUsedPrinter_;
44     isLastUsedPrinter_ = right.isLastUsedPrinter_;
45     hasPrinterStatus_ = right.hasPrinterStatus_;
46     printerStatus_ = right.printerStatus_;
47 }
48 
operator =(const PrinterInfo & right)49 PrinterInfo &PrinterInfo::operator=(const PrinterInfo &right)
50 {
51     if (this != &right) {
52         printerId_ = right.printerId_;
53         printerName_ = right.printerName_;
54         printerState_ = right.printerState_;
55         printerIcon_ = right.printerIcon_;
56         description_ = right.description_;
57         hasCapability_ = right.hasCapability_;
58         capability_ = right.capability_;
59         hasOption_ = right.hasOption_;
60         option_ = right.option_;
61         hasIsDefaultPrinter_ = right.hasIsDefaultPrinter_;
62         isDefaultPrinter_ = right.isDefaultPrinter_;
63         hasIsLastUsedPrinter_ = right.hasIsLastUsedPrinter_;
64         isLastUsedPrinter_ = right.isLastUsedPrinter_;
65         hasPrinterStatus_ = right.hasPrinterStatus_;
66         printerStatus_ = right.printerStatus_;
67     }
68     return *this;
69 }
70 
~PrinterInfo()71 PrinterInfo::~PrinterInfo()
72 {
73 }
74 
SetPrinterId(const std::string & printerId)75 void PrinterInfo::SetPrinterId(const std::string &printerId)
76 {
77     printerId_ = printerId;
78 }
79 
SetPrinterName(std::string printerName)80 void PrinterInfo::SetPrinterName(std::string printerName)
81 {
82     printerName_ = printerName;
83 }
84 
SetPrinterIcon(uint32_t printIcon)85 void PrinterInfo::SetPrinterIcon(uint32_t printIcon)
86 {
87     printerIcon_ = printIcon;
88 }
89 
SetPrinterState(uint32_t printerState)90 void PrinterInfo::SetPrinterState(uint32_t printerState)
91 {
92     printerState_ = printerState;
93 }
94 
SetDescription(std::string description)95 void PrinterInfo::SetDescription(std::string description)
96 {
97     description_ = description;
98 }
99 
SetCapability(const PrinterCapability & capability)100 void PrinterInfo::SetCapability(const PrinterCapability &capability)
101 {
102     hasCapability_ = true;
103     capability_ = capability;
104 }
105 
SetOption(const std::string & option)106 void PrinterInfo::SetOption(const std::string &option)
107 {
108     hasOption_ = true;
109     option_ = option;
110 }
111 
SetIsDefaultPrinter(bool isDefaultPrinter)112 void PrinterInfo::SetIsDefaultPrinter(bool isDefaultPrinter)
113 {
114     hasIsDefaultPrinter_ = true;
115     isDefaultPrinter_ = true;
116 }
117 
SetIsLastUsedPrinter(bool isLastUsedPrinter)118 void PrinterInfo::SetIsLastUsedPrinter(bool isLastUsedPrinter)
119 {
120     hasIsLastUsedPrinter_ = true;
121     isLastUsedPrinter_ = true;
122 }
123 
SetPrinterStatus(uint32_t printerStatus)124 void PrinterInfo::SetPrinterStatus(uint32_t printerStatus)
125 {
126     hasPrinterStatus_ = true;
127     printerStatus_ = printerStatus;
128 }
129 
GetPrinterId() const130 const std::string &PrinterInfo::GetPrinterId() const
131 {
132     return printerId_;
133 }
134 
GetPrinterName() const135 const std::string &PrinterInfo::GetPrinterName() const
136 {
137     return printerName_;
138 }
139 
GetPrinterIcon() const140 uint32_t PrinterInfo::GetPrinterIcon() const
141 {
142     return printerIcon_;
143 }
144 
GetPrinterState() const145 uint32_t PrinterInfo::GetPrinterState() const
146 {
147     return printerState_;
148 }
149 
GetDescription() const150 const std::string &PrinterInfo::GetDescription() const
151 {
152     return description_;
153 }
154 
HasCapability() const155 bool PrinterInfo::HasCapability() const
156 {
157     return hasCapability_;
158 }
159 
GetCapability(PrinterCapability & cap) const160 void PrinterInfo::GetCapability(PrinterCapability &cap) const
161 {
162     cap = capability_;
163 }
164 
HasOption() const165 bool PrinterInfo::HasOption() const
166 {
167     return hasOption_;
168 }
169 
GetOption() const170 std::string PrinterInfo::GetOption() const
171 {
172     return option_;
173 }
174 
HasIsDefaultPrinter() const175 bool PrinterInfo::HasIsDefaultPrinter() const
176 {
177     return hasIsDefaultPrinter_;
178 }
179 
GetIsDefaultPrinter() const180 bool PrinterInfo::GetIsDefaultPrinter() const
181 {
182     return isDefaultPrinter_;
183 }
184 
HasIsLastUsedPrinter() const185 bool PrinterInfo::HasIsLastUsedPrinter() const
186 {
187     return hasIsLastUsedPrinter_;
188 }
189 
GetIsLastUsedPrinter() const190 bool PrinterInfo::GetIsLastUsedPrinter() const
191 {
192     return isLastUsedPrinter_;
193 }
194 
HasPrinterStatus() const195 bool PrinterInfo::HasPrinterStatus() const
196 {
197     return hasPrinterStatus_;
198 }
199 
GetPrinterStatus() const200 uint32_t PrinterInfo::GetPrinterStatus() const
201 {
202     return printerStatus_;
203 }
204 
ReadFromParcel(Parcel & parcel)205 bool PrinterInfo::ReadFromParcel(Parcel &parcel)
206 {
207     if (parcel.GetReadableBytes() == 0) {
208         PRINT_HILOGE("no data in parcel");
209         return false;
210     }
211     SetPrinterId(parcel.ReadString());
212     SetPrinterName(parcel.ReadString());
213     SetPrinterState(parcel.ReadUint32());
214 
215     uint32_t iconId = PRINT_INVALID_ID;
216     if (parcel.ReadBool()) {
217         iconId = parcel.ReadUint32();
218     }
219     SetPrinterIcon(iconId);
220 
221     std::string desc = "";
222     if (parcel.ReadBool()) {
223         desc = parcel.ReadString();
224     }
225     SetDescription(desc);
226 
227     hasCapability_ = parcel.ReadBool();
228     if (hasCapability_) {
229         auto capPtr = PrinterCapability::Unmarshalling(parcel);
230         if (capPtr == nullptr) {
231             PRINT_HILOGE("failed to build capability from printer info");
232             return false;
233         }
234         capability_ = *capPtr;
235     }
236 
237     hasOption_ = parcel.ReadBool();
238     if (hasOption_) {
239         SetOption(parcel.ReadString());
240     }
241 
242     hasIsDefaultPrinter_ = parcel.ReadBool();
243     if (hasIsDefaultPrinter_) {
244         isDefaultPrinter_ = parcel.ReadBool();
245     }
246 
247     hasIsLastUsedPrinter_ = parcel.ReadBool();
248     if (hasIsLastUsedPrinter_) {
249         isLastUsedPrinter_ = parcel.ReadBool();
250     }
251 
252     hasPrinterStatus_ = parcel.ReadBool();
253     PRINT_HILOGD("hasPrinterStatus_: %{public}d", hasPrinterStatus_);
254     if (hasPrinterStatus_) {
255         SetPrinterStatus(parcel.ReadUint32());
256         PRINT_HILOGD("GetPrinterStatus(): %{public}d", GetPrinterStatus());
257     }
258 
259     return true;
260 }
261 
Marshalling(Parcel & parcel) const262 bool PrinterInfo::Marshalling(Parcel &parcel) const
263 {
264     parcel.WriteString(GetPrinterId());
265     parcel.WriteString(GetPrinterName());
266     parcel.WriteUint32(GetPrinterState());
267 
268     if (GetPrinterIcon() != PRINT_INVALID_ID) {
269         parcel.WriteBool(true);
270         parcel.WriteUint32(GetPrinterIcon());
271     } else {
272         parcel.WriteBool(false);
273     }
274 
275     if (GetDescription() != "") {
276         parcel.WriteBool(true);
277         parcel.WriteString(GetDescription());
278     } else {
279         parcel.WriteBool(false);
280     }
281 
282     parcel.WriteBool(hasCapability_);
283     if (hasCapability_) {
284         capability_.Marshalling(parcel);
285     }
286 
287     parcel.WriteBool(hasOption_);
288     if (hasOption_) {
289         parcel.WriteString(GetOption());
290     }
291 
292     parcel.WriteBool(hasIsDefaultPrinter_);
293     if (hasIsDefaultPrinter_) {
294         parcel.WriteBool(isDefaultPrinter_);
295     }
296 
297     parcel.WriteBool(hasIsLastUsedPrinter_);
298     if (hasIsLastUsedPrinter_) {
299         parcel.WriteBool(isLastUsedPrinter_);
300     }
301 
302     parcel.WriteBool(hasPrinterStatus_);
303     PRINT_HILOGD("hasPrinterStatus_: %{public}d", hasPrinterStatus_);
304     if (hasPrinterStatus_) {
305         PRINT_HILOGD("GetPrinterStatus(): %{public}d", GetPrinterStatus());
306         parcel.WriteUint32(GetPrinterStatus());
307     }
308     return true;
309 }
310 
Unmarshalling(Parcel & parcel)311 std::shared_ptr<PrinterInfo> PrinterInfo::Unmarshalling(Parcel &parcel)
312 {
313     auto nativeObj = std::make_shared<PrinterInfo>();
314     nativeObj->ReadFromParcel(parcel);
315     return nativeObj;
316 }
317 
Dump()318 void PrinterInfo::Dump()
319 {
320     PRINT_HILOGD("printerId: %{private}s", printerId_.c_str());
321     PRINT_HILOGD("printerName: %{private}s", printerName_.c_str());
322     PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
323     PRINT_HILOGD("printerState: %{public}d", printerState_);
324     if (description_ != "") {
325         PRINT_HILOGD("description: %{private}s", description_.c_str());
326     }
327     PRINT_HILOGD("description: %{private}s", description_.c_str());
328     if (hasCapability_) {
329         capability_.Dump();
330     }
331     if (hasOption_) {
332         PRINT_HILOGD("option: %{private}s", option_.c_str());
333     }
334     if (hasIsDefaultPrinter_) {
335         PRINT_HILOGD("isDefaultPrinter: %{public}d", isDefaultPrinter_);
336     }
337     if (hasIsLastUsedPrinter_) {
338         PRINT_HILOGD("isLastUsedPrinter: %{public}d", isLastUsedPrinter_);
339     }
340     if (hasPrinterStatus_) {
341         PRINT_HILOGD("printerStatus: %{public}d", printerStatus_);
342     }
343 }
344 }  // namespace OHOS::Print