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