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 "print_job.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19
20 namespace OHOS::Print {
PrintJob()21 PrintJob::PrintJob()
22 : jobId_(""), printerId_(""), jobState_(PRINT_JOB_PREPARED),
23 subState_(PRINT_JOB_BLOCKED_UNKNOWN), copyNumber_(0),
24 isSequential_(false), isLandscape_(false), colorMode_(0), duplexMode_(0),
25 hasMargin_(false), hasPreview_(false), hasOption_(false), option_("") {
26 margin_.Reset();
27 preview_.Reset();
28 }
29
PrintJob(const PrintJob & right)30 PrintJob::PrintJob(const PrintJob &right)
31 {
32 fdList_.clear();
33 fdList_.assign(right.fdList_.begin(), right.fdList_.end());
34
35 printerId_ = right.printerId_;
36 jobId_ = right.jobId_;
37 jobState_ = right.jobState_;
38 subState_ = right.subState_;
39 copyNumber_ = right.copyNumber_;
40 pageRange_ = right.pageRange_;
41 isSequential_ = right.isSequential_;
42 pageSize_ = right.pageSize_;
43 isLandscape_ = right.isLandscape_;
44 colorMode_ = right.colorMode_;
45 duplexMode_ = right.duplexMode_;
46 hasMargin_ = right.hasMargin_;
47 margin_ = right.margin_;
48 hasPreview_ = right.hasPreview_;
49 preview_ = right.preview_;
50 hasOption_ = right.hasOption_;
51 option_ = right.option_;
52 }
53
operator =(const PrintJob & right)54 PrintJob &PrintJob::operator=(const PrintJob &right)
55 {
56 if (this != &right) {
57 fdList_.clear();
58 fdList_.assign(right.fdList_.begin(), right.fdList_.end());
59
60 printerId_ = right.printerId_;
61 jobId_ = right.jobId_;
62 jobState_ = right.jobState_;
63 subState_ = right.subState_;
64 copyNumber_ = right.copyNumber_;
65 pageRange_ = right.pageRange_;
66 isSequential_ = right.isSequential_;
67 pageSize_ = right.pageSize_;
68 isLandscape_ = right.isLandscape_;
69 colorMode_ = right.colorMode_;
70 duplexMode_ = right.duplexMode_;
71 hasMargin_ = right.hasMargin_;
72 margin_ = right.margin_;
73 hasPreview_ = right.hasPreview_;
74 preview_ = right.preview_;
75 hasOption_ = right.hasOption_;
76 option_ = right.option_;
77 }
78 return *this;
79 }
80
~PrintJob()81 PrintJob::~PrintJob()
82 {
83 }
84
SetFdList(const std::vector<uint32_t> & fdList)85 void PrintJob::SetFdList(const std::vector<uint32_t> &fdList)
86 {
87 fdList_.clear();
88 fdList_.assign(fdList.begin(), fdList.end());
89 }
90
SetJobId(const std::string & jobId)91 void PrintJob::SetJobId(const std::string &jobId)
92 {
93 jobId_ = jobId;
94 }
95
SetPrinterId(const std::string & printerid)96 void PrintJob::SetPrinterId(const std::string &printerid)
97 {
98 printerId_ = printerid;
99 }
100
SetJobState(uint32_t jobState)101 void PrintJob::SetJobState(uint32_t jobState)
102 {
103 if (jobState < PRINT_JOB_UNKNOWN) {
104 jobState_ = jobState;
105 }
106 }
107
SetSubState(uint32_t subState)108 void PrintJob::SetSubState(uint32_t subState)
109 {
110 if (jobState_ == PRINT_JOB_COMPLETED && subState <= PRINT_JOB_COMPLETED_FILE_CORRUPT) {
111 subState_ = subState;
112 } else {
113 subState_ = subState;
114 }
115 }
116
SetCopyNumber(uint32_t copyNumber)117 void PrintJob::SetCopyNumber(uint32_t copyNumber)
118 {
119 copyNumber_ = copyNumber;
120 }
121
SetPageRange(const PrintRange & pageRange)122 void PrintJob::SetPageRange(const PrintRange &pageRange)
123 {
124 pageRange_ = pageRange;
125 }
126
SetIsSequential(bool isSequential)127 void PrintJob::SetIsSequential(bool isSequential)
128 {
129 isSequential_ = isSequential;
130 }
131
SetPageSize(const PrintPageSize & pageSize)132 void PrintJob::SetPageSize(const PrintPageSize &pageSize)
133 {
134 pageSize_ = pageSize;
135 }
136
SetIsLandscape(bool isLandscape)137 void PrintJob::SetIsLandscape(bool isLandscape)
138 {
139 isLandscape_ = isLandscape;
140 }
141
SetColorMode(uint32_t colorMode)142 void PrintJob::SetColorMode(uint32_t colorMode)
143 {
144 colorMode_ = colorMode;
145 }
146
SetDuplexMode(uint32_t duplexmode)147 void PrintJob::SetDuplexMode(uint32_t duplexmode)
148 {
149 duplexMode_ = duplexmode;
150 }
151
SetMargin(const PrintMargin & margin)152 void PrintJob::SetMargin(const PrintMargin &margin)
153 {
154 hasMargin_ = true;
155 margin_ = margin;
156 }
157
SetOption(const std::string & option)158 void PrintJob::SetOption(const std::string &option)
159 {
160 hasOption_ = true;
161 option_ = option;
162 }
163
SetPreview(const PrintPreviewAttribute & preview)164 void PrintJob::SetPreview(const PrintPreviewAttribute &preview)
165 {
166 hasPreview_ = true;
167 preview_ = preview;
168 }
169
UpdateParams(const PrintJob & jobInfo)170 void PrintJob::UpdateParams(const PrintJob &jobInfo)
171 {
172 fdList_.clear();
173 fdList_.assign(jobInfo.fdList_.begin(), jobInfo.fdList_.end());
174
175 jobId_ = jobInfo.jobId_;
176 printerId_ = jobInfo.printerId_;
177 copyNumber_ = jobInfo.copyNumber_;
178 pageRange_ = jobInfo.pageRange_;
179 isSequential_ = jobInfo.isSequential_;
180 pageSize_ = jobInfo.pageSize_;
181 isLandscape_ = jobInfo.isLandscape_;
182 colorMode_ = jobInfo.colorMode_;
183 duplexMode_ = jobInfo.duplexMode_;
184 hasMargin_ = jobInfo.hasMargin_;
185 margin_ = jobInfo.margin_;
186 hasPreview_ = jobInfo.hasPreview_;
187 preview_ = jobInfo.preview_;
188 hasOption_ = jobInfo.hasOption_;
189 option_ = jobInfo.option_;
190 }
191
GetFdList(std::vector<uint32_t> & fdList) const192 void PrintJob::GetFdList(std::vector<uint32_t> &fdList) const
193 {
194 fdList.clear();
195 fdList.assign(fdList_.begin(), fdList_.end());
196 }
197
DupFdList(std::vector<uint32_t> & fdList) const198 void PrintJob::DupFdList(std::vector<uint32_t> &fdList) const
199 {
200 fdList.clear();
201 for (uint32_t fd : fdList_) {
202 int32_t dupFd = dup(fd);
203 if (dupFd < 0) {
204 PRINT_HILOGW("dup fd failed");
205 fdList.push_back(fd);
206 continue;
207 }
208 fdList.push_back(static_cast<uint32_t>(dupFd));
209 }
210 }
211
GetJobId() const212 const std::string &PrintJob::GetJobId() const
213 {
214 return jobId_;
215 }
216
GetPrinterId() const217 const std::string &PrintJob::GetPrinterId() const
218 {
219 return printerId_;
220 }
221
GetJobState() const222 uint32_t PrintJob::GetJobState() const
223 {
224 return jobState_;
225 }
226
GetSubState() const227 uint32_t PrintJob::GetSubState() const
228 {
229 return subState_;
230 }
231
GetCopyNumber() const232 uint32_t PrintJob::GetCopyNumber() const
233 {
234 return copyNumber_;
235 }
236
GetPageRange(PrintRange & range) const237 void PrintJob::GetPageRange(PrintRange &range) const
238 {
239 range = pageRange_;
240 }
241
GetIsSequential() const242 bool PrintJob::GetIsSequential() const
243 {
244 return isSequential_;
245 }
GetPageSize(PrintPageSize & pageSize) const246 void PrintJob::GetPageSize(PrintPageSize &pageSize) const
247 {
248 pageSize = pageSize_;
249 }
250
GetIsLandscape() const251 bool PrintJob::GetIsLandscape() const
252 {
253 return isLandscape_;
254 }
255
GetColorMode() const256 uint32_t PrintJob::GetColorMode() const
257 {
258 return colorMode_;
259 }
260
GetDuplexMode() const261 uint32_t PrintJob::GetDuplexMode() const
262 {
263 return duplexMode_;
264 }
265
HasMargin() const266 bool PrintJob::HasMargin() const
267 {
268 return hasMargin_;
269 }
270
GetMargin(PrintMargin & margin) const271 void PrintJob::GetMargin(PrintMargin &margin) const
272 {
273 margin = margin_;
274 }
275
HasPreview() const276 bool PrintJob::HasPreview() const
277 {
278 return hasPreview_;
279 }
280
GetPreview(PrintPreviewAttribute & previewAttr) const281 void PrintJob::GetPreview(PrintPreviewAttribute &previewAttr) const
282 {
283 previewAttr = preview_;
284 }
285
HasOption() const286 bool PrintJob::HasOption() const
287 {
288 return hasOption_;
289 }
290
GetOption() const291 const std::string &PrintJob::GetOption() const
292 {
293 return option_;
294 }
295
ReadParcelFD(Parcel & parcel)296 void PrintJob::ReadParcelFD(Parcel &parcel)
297 {
298 uint32_t fdSize = parcel.ReadUint32();
299 fdList_.clear();
300
301 CHECK_IS_EXCEED_PRINT_RANGE_VOID(fdSize);
302 auto msgParcel = static_cast<MessageParcel*>(&parcel);
303 for (uint32_t index = 0; index < fdSize; index++) {
304 auto fd = msgParcel->ReadFileDescriptor();
305 PRINT_HILOGD("fd[%{public}d] = %{public}d", index, fd);
306 fdList_.emplace_back(fd);
307 }
308 }
309
ReadFromParcel(Parcel & parcel)310 void PrintJob::ReadFromParcel(Parcel &parcel)
311 {
312 if (parcel.GetReadableBytes() == 0) {
313 PRINT_HILOGE("no data in parcel");
314 return;
315 }
316 ReadParcelFD(parcel);
317 SetJobId(parcel.ReadString());
318 SetPrinterId(parcel.ReadString());
319 SetJobState(parcel.ReadUint32());
320 SetSubState(parcel.ReadUint32());
321 SetCopyNumber(parcel.ReadUint32());
322 auto rangePtr = PrintRange::Unmarshalling(parcel);
323 if (rangePtr != nullptr) {
324 SetPageRange(*rangePtr);
325 }
326 SetIsSequential(parcel.ReadBool());
327 auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
328 if (pageSizePtr != nullptr) {
329 SetPageSize(*pageSizePtr);
330 }
331 SetIsLandscape(parcel.ReadBool());
332 SetColorMode(parcel.ReadUint32());
333 SetDuplexMode(parcel.ReadUint32());
334 hasMargin_ = parcel.ReadBool();
335 if (hasMargin_) {
336 auto marginPtr = PrintMargin::Unmarshalling(parcel);
337 if (marginPtr != nullptr) {
338 margin_ = *marginPtr;
339 }
340 }
341 hasPreview_ = parcel.ReadBool();
342 if (hasPreview_) {
343 auto previewPtr = PrintPreviewAttribute::Unmarshalling(parcel);
344 if (previewPtr != nullptr) {
345 preview_ = *previewPtr;
346 }
347 }
348 hasOption_ = parcel.ReadBool();
349 if (hasOption_) {
350 SetOption(parcel.ReadString());
351 }
352 }
353
Marshalling(Parcel & parcel) const354 bool PrintJob::Marshalling(Parcel &parcel) const
355 {
356 parcel.WriteUint32(fdList_.size());
357 auto msgParcel = static_cast<MessageParcel*>(&parcel);
358 if (msgParcel != nullptr) {
359 for (auto fd : fdList_) {
360 msgParcel->WriteFileDescriptor(fd);
361 }
362 }
363
364 parcel.WriteString(GetJobId());
365 parcel.WriteString(GetPrinterId());
366 parcel.WriteUint32(GetJobState());
367 parcel.WriteUint32(GetSubState());
368 parcel.WriteUint32(GetCopyNumber());
369 pageRange_.Marshalling(parcel);
370 parcel.WriteBool(GetIsSequential());
371 return MarshallingParam(parcel);
372 }
373
MarshallingParam(Parcel & parcel) const374 bool PrintJob::MarshallingParam(Parcel &parcel) const
375 {
376 pageSize_.Marshalling(parcel);
377 parcel.WriteBool(GetIsLandscape());
378 parcel.WriteUint32(GetColorMode());
379 parcel.WriteUint32(GetDuplexMode());
380
381 parcel.WriteBool(hasMargin_);
382 if (hasMargin_) {
383 margin_.Marshalling(parcel);
384 }
385
386 parcel.WriteBool(hasPreview_);
387 if (hasPreview_) {
388 preview_.Marshalling(parcel);
389 }
390
391 parcel.WriteBool(hasOption_);
392 if (hasOption_) {
393 parcel.WriteString(GetOption());
394 }
395
396 return true;
397 }
398
Unmarshalling(Parcel & parcel)399 std::shared_ptr<PrintJob> PrintJob::Unmarshalling(Parcel &parcel)
400 {
401 auto nativeObj = std::make_shared<PrintJob>();
402 if (nativeObj == nullptr) {
403 PRINT_HILOGE("nativeObj is nullptr");
404 return nullptr;
405 }
406 nativeObj->ReadFromParcel(parcel);
407 return nativeObj;
408 }
409
Dump()410 void PrintJob::Dump()
411 {
412 uint32_t fileLength = fdList_.size();
413 for (uint32_t i = 0; i < fileLength; i++) {
414 PRINT_HILOGD("fd = %{public}d", fdList_[i]);
415 }
416
417 PRINT_HILOGD("jobId_ = %{public}s", jobId_.c_str());
418 PRINT_HILOGD("printerId_ = %{private}s", printerId_.c_str());
419 PRINT_HILOGD("jobState_ = %{public}d", jobState_);
420 PRINT_HILOGD("subState_ = %{public}d", subState_);
421 PRINT_HILOGD("copyNumber_ = %{public}d", copyNumber_);
422 PRINT_HILOGD("isSequential_ = %{public}d", isSequential_);
423 PRINT_HILOGD("isLandscape_ = %{public}d", isLandscape_);
424 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
425 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
426
427 pageRange_.Dump();
428 pageSize_.Dump();
429 if (hasMargin_) {
430 margin_.Dump();
431 }
432 if (hasPreview_) {
433 preview_.Dump();
434 }
435 if (hasOption_) {
436 PRINT_HILOGD("option: %{private}s", option_.c_str());
437 }
438 }
439
ConvertToJsonString() const440 std::string PrintJob::ConvertToJsonString() const
441 {
442 Json::Value json;
443 json["printerId"] = printerId_;
444 json["jobId"] = jobId_;
445 json["copyNumber"] = copyNumber_;
446 json["colorMode"] = colorMode_;
447 json["duplexMode"] = duplexMode_;
448 json["pageSize"] = pageSize_.GetName();
449 if (hasOption_) {
450 json["option"] = option_;
451 }
452 Json::StreamWriterBuilder wBuilder;
453 std::string jsonString = Json::writeString(wBuilder, json);
454 return jsonString;
455 }
456
ConvertToJsonObject() const457 Json::Value PrintJob::ConvertToJsonObject() const
458 {
459 Json::Value jsonObject;
460 jsonObject["jobId"] = jobId_;
461 jsonObject["printerId"] = printerId_;
462 jsonObject["jobState"] = jobState_;
463 jsonObject["subState"] = subState_;
464 jsonObject["copyNumber"] = copyNumber_;
465 jsonObject["pageRange"] = pageRange_.ConvertToJsonObject();
466 jsonObject["isSequential"] = isSequential_;
467 jsonObject["pageSize"] = pageSize_.ConvertToJsonObject();
468 jsonObject["isLandscape"] = isLandscape_;
469 jsonObject["colorMode"] = colorMode_;
470 jsonObject["duplexMode"] = duplexMode_;
471 jsonObject["hasMargin"] = hasMargin_;
472 if (hasMargin_) {
473 jsonObject["margin"] = margin_.ConvertToJsonObject();
474 }
475 jsonObject["hasPreview"] = hasPreview_;
476 if (hasPreview_) {
477 jsonObject["preview"] = preview_.ConvertToJsonObject();
478 }
479 jsonObject["hasOption"] = hasOption_;
480 if (hasOption_) {
481 jsonObject["option"] = option_;
482 }
483 return jsonObject;
484 }
485 } // namespace OHOS::Print
486