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() : printerId_(""), printerName_(""), printerState_(PRINTER_UNKNOWN),
22 printerIcon_(PRINT_INVALID_ID), description_(""), hasCapability_(false), hasOption_(false), option_("") {
23 capability_.Reset();
24 }
25
PrinterInfo(const PrinterInfo & right)26 PrinterInfo::PrinterInfo(const PrinterInfo &right)
27 {
28 printerId_ = right.printerId_;
29 printerName_ = right.printerName_;
30 printerState_ = right.printerState_;
31 printerIcon_ = right.printerIcon_;
32 description_ = right.description_;
33 hasCapability_ = right.hasCapability_;
34 capability_ = right.capability_;
35 hasOption_ = right.hasOption_;
36 option_= right.option_;
37 }
38
operator =(const PrinterInfo & right)39 PrinterInfo &PrinterInfo::operator=(const PrinterInfo &right)
40 {
41 if (this != &right) {
42 printerId_ = right.printerId_;
43 printerName_ = right.printerName_;
44 printerState_ = right.printerState_;
45 printerIcon_ = right.printerIcon_;
46 description_ = right.description_;
47 hasCapability_ = right.hasCapability_;
48 capability_ = right.capability_;
49 hasOption_ = right.hasOption_;
50 option_ = right.option_;
51 }
52 return *this;
53 }
54
~PrinterInfo()55 PrinterInfo::~PrinterInfo()
56 {
57 }
58
SetPrinterId(const std::string & printerId)59 void PrinterInfo::SetPrinterId(const std::string &printerId)
60 {
61 printerId_ = printerId;
62 }
63
SetPrinterName(std::string printerName)64 void PrinterInfo::SetPrinterName(std::string printerName)
65 {
66 printerName_ = printerName;
67 }
68
SetPrinterIcon(uint32_t printIcon)69 void PrinterInfo::SetPrinterIcon(uint32_t printIcon)
70 {
71 printerIcon_ = printIcon;
72 }
73
SetPrinterState(uint32_t printerState)74 void PrinterInfo::SetPrinterState(uint32_t printerState)
75 {
76 printerState_ = printerState;
77 }
78
SetDescription(std::string description)79 void PrinterInfo::SetDescription(std::string description)
80 {
81 description_ = description;
82 }
83
SetCapability(const PrinterCapability & capability)84 void PrinterInfo::SetCapability(const PrinterCapability &capability)
85 {
86 hasCapability_ = true;
87 capability_ = capability;
88 }
89
SetOption(const std::string & option)90 void PrinterInfo::SetOption(const std::string &option)
91 {
92 hasOption_ = true;
93 option_ = option;
94 }
95
GetPrinterId() const96 const std::string &PrinterInfo::GetPrinterId() const
97 {
98 return printerId_;
99 }
100
GetPrinterName() const101 const std::string &PrinterInfo::GetPrinterName() const
102 {
103 return printerName_;
104 }
105
GetPrinterIcon() const106 uint32_t PrinterInfo::GetPrinterIcon() const
107 {
108 return printerIcon_;
109 }
110
GetPrinterState() const111 uint32_t PrinterInfo::GetPrinterState() const
112 {
113 return printerState_;
114 }
115
GetDescription() const116 const std::string &PrinterInfo::GetDescription() const
117 {
118 return description_;
119 }
120
HasCapability() const121 bool PrinterInfo::HasCapability() const
122 {
123 return hasCapability_;
124 }
125
GetCapability(PrinterCapability & cap) const126 void PrinterInfo::GetCapability(PrinterCapability &cap) const
127 {
128 cap = capability_;
129 }
130
HasOption() const131 bool PrinterInfo::HasOption() const
132 {
133 return hasOption_;
134 }
135
GetOption() const136 std::string PrinterInfo::GetOption() const
137 {
138 return option_;
139 }
140
ReadFromParcel(Parcel & parcel)141 bool PrinterInfo::ReadFromParcel(Parcel &parcel)
142 {
143 SetPrinterId(parcel.ReadString());
144 SetPrinterName(parcel.ReadString());
145 SetPrinterState(parcel.ReadUint32());
146
147 uint32_t iconId = PRINT_INVALID_ID;
148 if (parcel.ReadBool()) {
149 iconId = parcel.ReadUint32();
150 }
151 SetPrinterIcon(iconId);
152
153 std::string desc = "";
154 if (parcel.ReadBool()) {
155 desc = parcel.ReadString();
156 }
157 SetDescription(desc);
158
159 hasCapability_ = parcel.ReadBool();
160 if (hasCapability_) {
161 auto capPtr = PrinterCapability::Unmarshalling(parcel);
162 if (capPtr == nullptr) {
163 PRINT_HILOGE("failed to build capability from printer info");
164 return false;
165 }
166 capability_ = *capPtr;
167 }
168
169 hasOption_ = parcel.ReadBool();
170 if (hasOption_) {
171 SetOption(parcel.ReadString());
172 }
173
174 return true;
175 }
176
Marshalling(Parcel & parcel) const177 bool PrinterInfo::Marshalling(Parcel &parcel) const
178 {
179 parcel.WriteString(GetPrinterId());
180 parcel.WriteString(GetPrinterName());
181 parcel.WriteUint32(GetPrinterState());
182
183 if (GetPrinterIcon() != PRINT_INVALID_ID) {
184 parcel.WriteBool(true);
185 parcel.WriteUint32(GetPrinterIcon());
186 } else {
187 parcel.WriteBool(false);
188 }
189
190 if (GetDescription() != "") {
191 parcel.WriteBool(true);
192 parcel.WriteString(GetDescription());
193 } else {
194 parcel.WriteBool(false);
195 }
196
197 parcel.WriteBool(hasCapability_);
198 if (hasCapability_) {
199 capability_.Marshalling(parcel);
200 }
201
202 parcel.WriteBool(hasOption_);
203 if (hasOption_) {
204 parcel.WriteString(GetOption());
205 }
206 return true;
207 }
208
Unmarshalling(Parcel & parcel)209 std::shared_ptr<PrinterInfo> PrinterInfo::Unmarshalling(Parcel &parcel)
210 {
211 auto nativeObj = std::make_shared<PrinterInfo>();
212 if (nativeObj != nullptr) {
213 nativeObj->ReadFromParcel(parcel);
214 }
215 return nativeObj;
216 }
217
Dump()218 void PrinterInfo::Dump()
219 {
220 PRINT_HILOGD("printerId: %{private}s", printerId_.c_str());
221 PRINT_HILOGD("printerName: %{private}s", printerName_.c_str());
222 PRINT_HILOGD("printerIcon: %{public}d", printerIcon_);
223 PRINT_HILOGD("printerState: %{public}d", printerState_);
224 if (description_ != "") {
225 PRINT_HILOGD("description: %{private}s", description_.c_str());
226 }
227 PRINT_HILOGD("description: %{private}s", description_.c_str());
228 if (hasCapability_) {
229 capability_.Dump();
230 }
231 if (hasOption_) {
232 PRINT_HILOGD("option: %{private}s", option_.c_str());
233 }
234 }
235 } // namespace OHOS::Print