1 /*
2 * Copyright (c) 2025 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_preferences.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19 #include "print_json_util.h"
20
21 namespace OHOS::Print {
PrinterPreferences()22 PrinterPreferences::PrinterPreferences()
23 : hasDefaultDuplexMode_(false),
24 defaultDuplexMode_(DUPLEX_MODE_NONE),
25 hasDefaultPrintQuality_(false),
26 defaultPrintQuality_(PRINT_QUALITY_NORMAL),
27 hasDefaultMediaType_(false),
28 defaultMediaType_(""),
29 hasDefaultPageSizeId_(false),
30 defaultPageSizeId_(""),
31 hasDefaultOrientation_(false),
32 defaultOrientation_(0),
33 hasBorderless_(false),
34 borderless_(false),
35 hasOption_(false),
36 option_("")
37 {
38 }
39
PrinterPreferences(const PrinterPreferences & right)40 PrinterPreferences::PrinterPreferences(const PrinterPreferences &right)
41 : hasDefaultDuplexMode_(right.hasDefaultDuplexMode_),
42 defaultDuplexMode_(right.defaultDuplexMode_),
43 hasDefaultPrintQuality_(right.hasDefaultPrintQuality_),
44 defaultPrintQuality_(right.defaultPrintQuality_),
45 hasDefaultMediaType_(right.hasDefaultMediaType_),
46 defaultMediaType_(right.defaultMediaType_),
47 hasDefaultPageSizeId_(right.hasDefaultPageSizeId_),
48 defaultPageSizeId_(right.defaultPageSizeId_),
49 hasDefaultOrientation_(right.hasDefaultOrientation_),
50 defaultOrientation_(right.defaultOrientation_),
51 hasBorderless_(right.hasBorderless_),
52 borderless_(right.borderless_),
53 hasOption_(right.hasOption_),
54 option_(right.option_)
55 {
56 }
57
operator =(const PrinterPreferences & right)58 PrinterPreferences &PrinterPreferences::operator=(const PrinterPreferences &right)
59 {
60 if (this != &right) {
61 hasDefaultDuplexMode_ = right.hasDefaultDuplexMode_;
62 defaultDuplexMode_ = right.defaultDuplexMode_;
63 hasDefaultPrintQuality_ = right.hasDefaultPrintQuality_;
64 defaultPrintQuality_ = right.defaultPrintQuality_;
65 hasDefaultMediaType_ = right.hasDefaultMediaType_;
66 defaultMediaType_ = right.defaultMediaType_;
67 hasDefaultPageSizeId_ = right.hasDefaultPageSizeId_;
68 defaultPageSizeId_ = right.defaultPageSizeId_;
69 hasDefaultOrientation_ = right.hasDefaultOrientation_;
70 defaultOrientation_ = right.defaultOrientation_;
71 hasBorderless_ = right.hasBorderless_;
72 borderless_ = right.borderless_;
73 hasOption_ = right.hasOption_;
74 option_ = right.option_;
75 }
76 return *this;
77 }
78
~PrinterPreferences()79 PrinterPreferences::~PrinterPreferences() {}
80
Reset()81 void PrinterPreferences::Reset()
82 {
83 hasDefaultDuplexMode_ = false;
84 defaultDuplexMode_ = DUPLEX_MODE_NONE;
85 hasDefaultPrintQuality_ = false;
86 defaultPrintQuality_ = PRINT_QUALITY_NORMAL;
87 hasDefaultMediaType_ = false;
88 defaultMediaType_ = "";
89 hasDefaultPageSizeId_ = false;
90 defaultPageSizeId_ = "";
91 hasDefaultOrientation_ = false;
92 defaultOrientation_ = PRINT_ORIENTATION_MODE_NONE;
93 hasBorderless_ = false;
94 borderless_ = false;
95 hasOption_ = false;
96 option_ = "";
97 }
98
SetDefaultDuplexMode(uint32_t defaultDuplexMode)99 void PrinterPreferences::SetDefaultDuplexMode(uint32_t defaultDuplexMode)
100 {
101 hasDefaultDuplexMode_ = true;
102 defaultDuplexMode_ = defaultDuplexMode;
103 }
104
SetDefaultPrintQuality(uint32_t defaultPrintQuality)105 void PrinterPreferences::SetDefaultPrintQuality(uint32_t defaultPrintQuality)
106 {
107 hasDefaultPrintQuality_ = true;
108 defaultPrintQuality_ = defaultPrintQuality;
109 }
110
SetDefaultMediaType(const std::string & defaultMediaType)111 void PrinterPreferences::SetDefaultMediaType(const std::string &defaultMediaType)
112 {
113 hasDefaultMediaType_ = true;
114 defaultMediaType_ = defaultMediaType;
115 }
116
SetDefaultPageSizeId(const std::string & defaultPageSizeId)117 void PrinterPreferences::SetDefaultPageSizeId(const std::string &defaultPageSizeId)
118 {
119 hasDefaultPageSizeId_ = true;
120 defaultPageSizeId_ = defaultPageSizeId;
121 }
122
SetDefaultOrientation(uint32_t defaultOrientation)123 void PrinterPreferences::SetDefaultOrientation(uint32_t defaultOrientation)
124 {
125 hasDefaultOrientation_ = true;
126 defaultOrientation_ = defaultOrientation;
127 }
128
SetBorderless(bool borderless)129 void PrinterPreferences::SetBorderless(bool borderless)
130 {
131 hasBorderless_ = true;
132 borderless_ = borderless;
133 }
134
SetOption(const std::string & option)135 void PrinterPreferences::SetOption(const std::string &option)
136 {
137 hasOption_ = true;
138 option_ = option;
139 }
140
HasDefaultDuplexMode() const141 bool PrinterPreferences::HasDefaultDuplexMode() const
142 {
143 return hasDefaultDuplexMode_;
144 }
145
GetDefaultDuplexMode() const146 uint32_t PrinterPreferences::GetDefaultDuplexMode() const
147 {
148 return defaultDuplexMode_;
149 }
150
HasDefaultPrintQuality() const151 bool PrinterPreferences::HasDefaultPrintQuality() const
152 {
153 return hasDefaultPrintQuality_;
154 }
155
GetDefaultPrintQuality() const156 uint32_t PrinterPreferences::GetDefaultPrintQuality() const
157 {
158 return defaultPrintQuality_;
159 }
160
HasDefaultMediaType() const161 bool PrinterPreferences::HasDefaultMediaType() const
162 {
163 return hasDefaultMediaType_;
164 }
165
GetDefaultMediaType() const166 const std::string &PrinterPreferences::GetDefaultMediaType() const
167 {
168 return defaultMediaType_;
169 }
170
HasDefaultPageSizeId() const171 bool PrinterPreferences::HasDefaultPageSizeId() const
172 {
173 return hasDefaultPageSizeId_;
174 }
175
GetDefaultPageSizeId() const176 const std::string &PrinterPreferences::GetDefaultPageSizeId() const
177 {
178 return defaultPageSizeId_;
179 }
180
HasDefaultOrientation() const181 bool PrinterPreferences::HasDefaultOrientation() const
182 {
183 return hasDefaultOrientation_;
184 }
185
GetDefaultOrientation() const186 uint32_t PrinterPreferences::GetDefaultOrientation() const
187 {
188 return defaultOrientation_;
189 }
190
HasBorderless() const191 bool PrinterPreferences::HasBorderless() const
192 {
193 return hasBorderless_;
194 }
195
GetBorderless() const196 bool PrinterPreferences::GetBorderless() const
197 {
198 return borderless_;
199 }
200
HasOption() const201 bool PrinterPreferences::HasOption() const
202 {
203 return hasOption_;
204 }
205
GetOption() const206 std::string PrinterPreferences::GetOption() const
207 {
208 return option_;
209 }
210
ReadFromParcel(Parcel & parcel)211 bool PrinterPreferences::ReadFromParcel(Parcel &parcel)
212 {
213 PrinterPreferences right;
214 if (parcel.GetReadableBytes() == 0) {
215 PRINT_HILOGE("no data in parcel");
216 return false;
217 }
218 right.hasDefaultDuplexMode_ = parcel.ReadBool();
219 if (right.hasDefaultDuplexMode_) {
220 right.SetDefaultDuplexMode(parcel.ReadUint32());
221 }
222
223 right.hasDefaultPrintQuality_ = parcel.ReadBool();
224 if (right.hasDefaultPrintQuality_) {
225 right.SetDefaultPrintQuality(parcel.ReadUint32());
226 }
227
228 right.hasDefaultMediaType_ = parcel.ReadBool();
229 if (right.hasDefaultMediaType_) {
230 right.SetDefaultMediaType(parcel.ReadString());
231 }
232
233 right.hasDefaultPageSizeId_ = parcel.ReadBool();
234 if (right.hasDefaultPageSizeId_) {
235 right.SetDefaultPageSizeId(parcel.ReadString());
236 }
237
238 right.hasDefaultOrientation_ = parcel.ReadBool();
239 if (right.hasDefaultOrientation_) {
240 right.SetDefaultOrientation(parcel.ReadUint32());
241 }
242
243 right.hasBorderless_ = parcel.ReadBool();
244 if (right.hasBorderless_) {
245 right.SetBorderless(parcel.ReadBool());
246 }
247
248 right.hasOption_ = parcel.ReadBool();
249 if (right.hasOption_) {
250 right.SetOption(parcel.ReadString());
251 }
252
253 *this = right;
254 return true;
255 }
256
Marshalling(Parcel & parcel) const257 bool PrinterPreferences::Marshalling(Parcel &parcel) const
258 {
259 parcel.WriteBool(hasDefaultDuplexMode_);
260 if (hasDefaultDuplexMode_) {
261 parcel.WriteUint32(GetDefaultDuplexMode());
262 }
263
264 parcel.WriteBool(hasDefaultPrintQuality_);
265 if (hasDefaultPrintQuality_) {
266 parcel.WriteUint32(GetDefaultPrintQuality());
267 }
268
269 parcel.WriteBool(hasDefaultMediaType_);
270 if (hasDefaultMediaType_) {
271 parcel.WriteString(GetDefaultMediaType());
272 }
273
274 parcel.WriteBool(hasDefaultPageSizeId_);
275 if (hasDefaultPageSizeId_) {
276 parcel.WriteString(GetDefaultPageSizeId());
277 }
278
279 parcel.WriteBool(hasDefaultOrientation_);
280 if (hasDefaultOrientation_) {
281 parcel.WriteUint32(GetDefaultOrientation());
282 }
283
284 parcel.WriteBool(hasBorderless_);
285 if (hasBorderless_) {
286 parcel.WriteBool(GetBorderless());
287 }
288
289 parcel.WriteBool(hasOption_);
290 if (hasOption_) {
291 parcel.WriteString(GetOption());
292 }
293
294 return true;
295 }
296
Unmarshalling(Parcel & parcel)297 std::shared_ptr<PrinterPreferences> PrinterPreferences::Unmarshalling(Parcel &parcel)
298 {
299 auto nativeObj = std::make_shared<PrinterPreferences>();
300 nativeObj->ReadFromParcel(parcel);
301 return nativeObj;
302 }
303
Dump() const304 void PrinterPreferences::Dump() const
305 {
306 if (hasDefaultDuplexMode_) {
307 PRINT_HILOGI("defaultDuplexMode: %{public}d", defaultDuplexMode_);
308 }
309 if (hasDefaultPrintQuality_) {
310 PRINT_HILOGI("defaultPrintQuality: %{public}d", defaultPrintQuality_);
311 }
312 if (hasDefaultMediaType_) {
313 PRINT_HILOGI("defaultMediaType: %{public}s", defaultMediaType_.c_str());
314 }
315 if (hasDefaultPageSizeId_) {
316 PRINT_HILOGI("defaultPageSizeId: %{public}s", defaultPageSizeId_.c_str());
317 }
318 if (hasDefaultOrientation_) {
319 PRINT_HILOGI("defaultOrientation: %{public}d", defaultOrientation_);
320 }
321 if (hasBorderless_) {
322 PRINT_HILOGI("borderless: %{public}d", borderless_);
323 }
324 if (hasOption_) {
325 PRINT_HILOGD("option: %{private}s", option_.c_str());
326 }
327 }
328
ConvertToJson()329 Json::Value PrinterPreferences::ConvertToJson()
330 {
331 Json::Value preferencesJson;
332 preferencesJson["defaultDuplexMode"] = defaultDuplexMode_;
333 preferencesJson["defaultPrintQuality"] = defaultPrintQuality_;
334 preferencesJson["defaultMediaType"] = defaultMediaType_;
335 preferencesJson["defaultPageSizeId"] = defaultPageSizeId_;
336 preferencesJson["defaultOrientation"] = defaultOrientation_;
337 preferencesJson["borderless"] = borderless_;
338
339 if (hasOption_) {
340 if (!PrintJsonUtil::Parse(option_, preferencesJson["options"])) {
341 PRINT_HILOGE("json accept preferences options fail");
342 }
343 }
344 return preferencesJson;
345 }
346
ConvertJsonToPrinterPreferences(Json::Value & preferencesJson)347 void PrinterPreferences::ConvertJsonToPrinterPreferences(Json::Value &preferencesJson)
348 {
349 if (preferencesJson.isMember("defaultDuplexMode") && preferencesJson["defaultDuplexMode"].isInt()) {
350 SetDefaultDuplexMode(preferencesJson["defaultDuplexMode"].asInt());
351 }
352
353 if (preferencesJson.isMember("defaultPrintQuality") && preferencesJson["defaultPrintQuality"].isInt()) {
354 SetDefaultPrintQuality(preferencesJson["defaultPrintQuality"].asInt());
355 }
356
357 if (preferencesJson.isMember("defaultMediaType") && preferencesJson["defaultMediaType"].isString()) {
358 SetDefaultMediaType(preferencesJson["defaultMediaType"].asString());
359 }
360
361 if (preferencesJson.isMember("defaultPageSizeId") && preferencesJson["defaultPageSizeId"].isString()) {
362 SetDefaultPageSizeId(preferencesJson["defaultPageSizeId"].asString());
363 }
364
365 if (preferencesJson.isMember("defaultOrientation") && preferencesJson["defaultOrientation"].isInt()) {
366 SetDefaultOrientation(preferencesJson["defaultOrientation"].asInt());
367 }
368
369 if (preferencesJson.isMember("borderless") && preferencesJson["borderless"].isBool()) {
370 SetBorderless(preferencesJson["borderless"].asBool());
371 }
372
373 if (preferencesJson.isMember("options") && preferencesJson["options"].isObject()) {
374 PRINT_HILOGD("find options");
375 SetOption(PrintJsonUtil::WriteString(preferencesJson["options"]));
376 }
377 }
378 } // namespace OHOS::Print