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 }
113 if (jobState_ == PRINT_JOB_BLOCKED &&
114 (subState < PRINT_JOB_BLOCKED_UNKNOWN && subState > PRINT_JOB_COMPLETED_FILE_CORRUPT)) {
115 subState_ = subState;
116 }
117 if (jobState_ == PRINT_JOB_RUNNING &&
118 (subState < PRINT_JOB_BLOCKED_UNKNOWN && subState > PRINT_JOB_COMPLETED_FILE_CORRUPT)) {
119 subState_ = subState;
120 }
121
122 if (jobState_ == PRINT_JOB_CREATE_FILE_COMPLETED &&
123 (subState == PRINT_JOB_CREATE_FILE_COMPLETED_SUCCESS || subState == PRINT_JOB_CREATE_FILE_COMPLETED_FAILED)) {
124 subState_ = subState;
125 }
126 }
127
SetCopyNumber(uint32_t copyNumber)128 void PrintJob::SetCopyNumber(uint32_t copyNumber)
129 {
130 copyNumber_ = copyNumber;
131 }
132
SetPageRange(const PrintRange & pageRange)133 void PrintJob::SetPageRange(const PrintRange &pageRange)
134 {
135 pageRange_ = pageRange;
136 }
137
SetIsSequential(bool isSequential)138 void PrintJob::SetIsSequential(bool isSequential)
139 {
140 isSequential_ = isSequential;
141 }
142
SetPageSize(const PrintPageSize & pageSize)143 void PrintJob::SetPageSize(const PrintPageSize &pageSize)
144 {
145 pageSize_ = pageSize;
146 }
147
SetIsLandscape(bool isLandscape)148 void PrintJob::SetIsLandscape(bool isLandscape)
149 {
150 isLandscape_ = isLandscape;
151 }
152
SetColorMode(uint32_t colorMode)153 void PrintJob::SetColorMode(uint32_t colorMode)
154 {
155 colorMode_ = colorMode;
156 }
157
SetDuplexMode(uint32_t duplexmode)158 void PrintJob::SetDuplexMode(uint32_t duplexmode)
159 {
160 duplexMode_ = duplexmode;
161 }
162
SetMargin(const PrintMargin & margin)163 void PrintJob::SetMargin(const PrintMargin &margin)
164 {
165 hasMargin_ = true;
166 margin_ = margin;
167 }
168
SetOption(const std::string & option)169 void PrintJob::SetOption(const std::string &option)
170 {
171 hasOption_ = true;
172 option_ = option;
173 }
174
SetPreview(const PrintPreviewAttribute & preview)175 void PrintJob::SetPreview(const PrintPreviewAttribute &preview)
176 {
177 hasPreview_ = true;
178 preview_ = preview;
179 }
180
UpdateParams(const PrintJob & jobInfo)181 void PrintJob::UpdateParams(const PrintJob &jobInfo)
182 {
183 fdList_.clear();
184 fdList_.assign(jobInfo.fdList_.begin(), jobInfo.fdList_.end());
185
186 jobId_ = jobInfo.jobId_;
187 printerId_ = jobInfo.printerId_;
188 copyNumber_ = jobInfo.copyNumber_;
189 pageRange_ = jobInfo.pageRange_;
190 isSequential_ = jobInfo.isSequential_;
191 pageSize_ = jobInfo.pageSize_;
192 isLandscape_ = jobInfo.isLandscape_;
193 colorMode_ = jobInfo.colorMode_;
194 duplexMode_ = jobInfo.duplexMode_;
195 hasMargin_ = jobInfo.hasMargin_;
196 margin_ = jobInfo.margin_;
197 hasPreview_ = jobInfo.hasPreview_;
198 preview_ = jobInfo.preview_;
199 hasOption_ = jobInfo.hasOption_;
200 option_ = jobInfo.option_;
201 }
202
GetFdList(std::vector<uint32_t> & fdList) const203 void PrintJob::GetFdList(std::vector<uint32_t> &fdList) const
204 {
205 fdList.clear();
206 fdList.assign(fdList_.begin(), fdList_.end());
207 }
208
GetJobId() const209 const std::string &PrintJob::GetJobId() const
210 {
211 return jobId_;
212 }
213
GetPrinterId() const214 const std::string &PrintJob::GetPrinterId() const
215 {
216 return printerId_;
217 }
218
GetJobState() const219 uint32_t PrintJob::GetJobState() const
220 {
221 return jobState_;
222 }
223
GetSubState() const224 uint32_t PrintJob::GetSubState() const
225 {
226 return subState_;
227 }
228
GetCopyNumber() const229 uint32_t PrintJob::GetCopyNumber() const
230 {
231 return copyNumber_;
232 }
233
GetPageRange(PrintRange & range) const234 void PrintJob::GetPageRange(PrintRange &range) const
235 {
236 range = pageRange_;
237 }
238
GetIsSequential() const239 bool PrintJob::GetIsSequential() const
240 {
241 return isSequential_;
242 }
GetPageSize(PrintPageSize & pageSize) const243 void PrintJob::GetPageSize(PrintPageSize &pageSize) const
244 {
245 pageSize = pageSize_;
246 }
247
GetIsLandscape() const248 bool PrintJob::GetIsLandscape() const
249 {
250 return isLandscape_;
251 }
252
GetColorMode() const253 uint32_t PrintJob::GetColorMode() const
254 {
255 return colorMode_;
256 }
257
GetDuplexMode() const258 uint32_t PrintJob::GetDuplexMode() const
259 {
260 return duplexMode_;
261 }
262
HasMargin() const263 bool PrintJob::HasMargin() const
264 {
265 return hasMargin_;
266 }
267
GetMargin(PrintMargin & margin) const268 void PrintJob::GetMargin(PrintMargin &margin) const
269 {
270 margin = margin_;
271 }
272
HasPreview() const273 bool PrintJob::HasPreview() const
274 {
275 return hasPreview_;
276 }
277
GetPreview(PrintPreviewAttribute & previewAttr) const278 void PrintJob::GetPreview(PrintPreviewAttribute &previewAttr) const
279 {
280 previewAttr = preview_;
281 }
282
HasOption() const283 bool PrintJob::HasOption() const
284 {
285 return hasOption_;
286 }
287
GetOption() const288 const std::string &PrintJob::GetOption() const
289 {
290 return option_;
291 }
292
ReadParcelFD(Parcel & parcel)293 void PrintJob::ReadParcelFD(Parcel &parcel)
294 {
295 uint32_t fdSize = parcel.ReadUint32();
296 fdList_.clear();
297
298 CHECK_IS_EXCEED_PRINT_RANGE_VOID(fdSize);
299 auto msgParcel = static_cast<MessageParcel*>(&parcel);
300 for (uint32_t index = 0; index < fdSize; index++) {
301 auto fd = msgParcel->ReadFileDescriptor();
302 PRINT_HILOGD("fd[%{public}d] = %{public}d", index, fd);
303 fdList_.emplace_back(fd);
304 }
305 }
306
ReadFromParcel(Parcel & parcel)307 void PrintJob::ReadFromParcel(Parcel &parcel)
308 {
309 ReadParcelFD(parcel);
310 SetJobId(parcel.ReadString());
311 SetPrinterId(parcel.ReadString());
312 SetJobState(parcel.ReadUint32());
313 SetSubState(parcel.ReadUint32());
314 SetCopyNumber(parcel.ReadUint32());
315 auto rangePtr = PrintRange::Unmarshalling(parcel);
316 if (rangePtr != nullptr) {
317 SetPageRange(*rangePtr);
318 }
319 SetIsSequential(parcel.ReadBool());
320 auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
321 if (pageSizePtr != nullptr) {
322 SetPageSize(*pageSizePtr);
323 }
324 SetIsLandscape(parcel.ReadBool());
325 SetColorMode(parcel.ReadUint32());
326 SetDuplexMode(parcel.ReadUint32());
327 hasMargin_ = parcel.ReadBool();
328 if (hasMargin_) {
329 auto marginPtr = PrintMargin::Unmarshalling(parcel);
330 if (marginPtr != nullptr) {
331 margin_ = *marginPtr;
332 }
333 }
334 hasPreview_ = parcel.ReadBool();
335 if (hasPreview_) {
336 auto previewPtr = PrintPreviewAttribute::Unmarshalling(parcel);
337 if (previewPtr != nullptr) {
338 preview_ = *previewPtr;
339 }
340 }
341 hasOption_ = parcel.ReadBool();
342 if (hasOption_) {
343 SetOption(parcel.ReadString());
344 }
345 }
346
Marshalling(Parcel & parcel) const347 bool PrintJob::Marshalling(Parcel &parcel) const
348 {
349 parcel.WriteUint32(fdList_.size());
350 auto msgParcel = static_cast<MessageParcel*>(&parcel);
351 if (msgParcel != nullptr) {
352 for (auto fd : fdList_) {
353 msgParcel->WriteFileDescriptor(fd);
354 }
355 }
356
357 parcel.WriteString(GetJobId());
358 parcel.WriteString(GetPrinterId());
359 parcel.WriteUint32(GetJobState());
360 parcel.WriteUint32(GetSubState());
361 parcel.WriteUint32(GetCopyNumber());
362 pageRange_.Marshalling(parcel);
363 parcel.WriteBool(GetIsSequential());
364 return MarshallingParam(parcel);
365 }
366
MarshallingParam(Parcel & parcel) const367 bool PrintJob::MarshallingParam(Parcel &parcel) const
368 {
369 pageSize_.Marshalling(parcel);
370 parcel.WriteBool(GetIsLandscape());
371 parcel.WriteUint32(GetColorMode());
372 parcel.WriteUint32(GetDuplexMode());
373
374 parcel.WriteBool(hasMargin_);
375 if (hasMargin_) {
376 margin_.Marshalling(parcel);
377 }
378
379 parcel.WriteBool(hasPreview_);
380 if (hasPreview_) {
381 preview_.Marshalling(parcel);
382 }
383
384 parcel.WriteBool(hasOption_);
385 if (hasOption_) {
386 parcel.WriteString(GetOption());
387 }
388
389 return true;
390 }
391
Unmarshalling(Parcel & parcel)392 std::shared_ptr<PrintJob> PrintJob::Unmarshalling(Parcel &parcel)
393 {
394 auto nativeObj = std::make_shared<PrintJob>();
395 nativeObj->ReadFromParcel(parcel);
396 return nativeObj;
397 }
398
Dump()399 void PrintJob::Dump()
400 {
401 uint32_t fileLength = fdList_.size();
402 for (uint32_t i = 0; i < fileLength; i++) {
403 PRINT_HILOGD("fd = %{public}d", fdList_[i]);
404 }
405
406 PRINT_HILOGD("jobId_ = %{public}s", jobId_.c_str());
407 PRINT_HILOGD("printerId_ = %{private}s", printerId_.c_str());
408 PRINT_HILOGD("jobState_ = %{public}d", jobState_);
409 PRINT_HILOGD("subState_ = %{public}d", subState_);
410 PRINT_HILOGD("copyNumber_ = %{public}d", copyNumber_);
411 PRINT_HILOGD("isSequential_ = %{public}d", isSequential_);
412 PRINT_HILOGD("isLandscape_ = %{public}d", isLandscape_);
413 PRINT_HILOGD("colorMode_ = %{public}d", colorMode_);
414 PRINT_HILOGD("duplexMode_ = %{public}d", duplexMode_);
415
416 pageRange_.Dump();
417 pageSize_.Dump();
418 if (hasMargin_) {
419 margin_.Dump();
420 }
421 if (hasPreview_) {
422 preview_.Dump();
423 }
424 if (hasOption_) {
425 PRINT_HILOGD("option: %{private}s", option_.c_str());
426 }
427 }
428 } // namespace OHOS::Print
429