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