• 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     } 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 
GetJobId() const198 const std::string &PrintJob::GetJobId() const
199 {
200     return jobId_;
201 }
202 
GetPrinterId() const203 const std::string &PrintJob::GetPrinterId() const
204 {
205     return printerId_;
206 }
207 
GetJobState() const208 uint32_t PrintJob::GetJobState() const
209 {
210     return jobState_;
211 }
212 
GetSubState() const213 uint32_t PrintJob::GetSubState() const
214 {
215     return subState_;
216 }
217 
GetCopyNumber() const218 uint32_t PrintJob::GetCopyNumber() const
219 {
220     return copyNumber_;
221 }
222 
GetPageRange(PrintRange & range) const223 void PrintJob::GetPageRange(PrintRange &range) const
224 {
225     range = pageRange_;
226 }
227 
GetIsSequential() const228 bool PrintJob::GetIsSequential() const
229 {
230     return isSequential_;
231 }
GetPageSize(PrintPageSize & pageSize) const232 void PrintJob::GetPageSize(PrintPageSize &pageSize) const
233 {
234     pageSize = pageSize_;
235 }
236 
GetIsLandscape() const237 bool PrintJob::GetIsLandscape() const
238 {
239     return isLandscape_;
240 }
241 
GetColorMode() const242 uint32_t PrintJob::GetColorMode() const
243 {
244     return colorMode_;
245 }
246 
GetDuplexMode() const247 uint32_t PrintJob::GetDuplexMode() const
248 {
249     return duplexMode_;
250 }
251 
HasMargin() const252 bool PrintJob::HasMargin() const
253 {
254     return hasMargin_;
255 }
256 
GetMargin(PrintMargin & margin) const257 void PrintJob::GetMargin(PrintMargin &margin) const
258 {
259     margin = margin_;
260 }
261 
HasPreview() const262 bool PrintJob::HasPreview() const
263 {
264     return hasPreview_;
265 }
266 
GetPreview(PrintPreviewAttribute & previewAttr) const267 void PrintJob::GetPreview(PrintPreviewAttribute &previewAttr) const
268 {
269     previewAttr = preview_;
270 }
271 
HasOption() const272 bool PrintJob::HasOption() const
273 {
274     return hasOption_;
275 }
276 
GetOption() const277 const std::string &PrintJob::GetOption() const
278 {
279     return option_;
280 }
281 
ReadParcelFD(Parcel & parcel)282 void PrintJob::ReadParcelFD(Parcel &parcel)
283 {
284     uint32_t fdSize = parcel.ReadUint32();
285     fdList_.clear();
286 
287     CHECK_IS_EXCEED_PRINT_RANGE_VOID(fdSize);
288     auto msgParcel = static_cast<MessageParcel*>(&parcel);
289     for (uint32_t index = 0; index < fdSize; index++) {
290         auto fd = msgParcel->ReadFileDescriptor();
291         PRINT_HILOGD("fd[%{public}d] = %{public}d", index, fd);
292         fdList_.emplace_back(fd);
293     }
294 }
295 
ReadFromParcel(Parcel & parcel)296 void PrintJob::ReadFromParcel(Parcel &parcel)
297 {
298     if (parcel.GetReadableBytes() == 0) {
299         PRINT_HILOGE("no data in parcel");
300         return;
301     }
302     ReadParcelFD(parcel);
303     SetJobId(parcel.ReadString());
304     SetPrinterId(parcel.ReadString());
305     SetJobState(parcel.ReadUint32());
306     SetSubState(parcel.ReadUint32());
307     SetCopyNumber(parcel.ReadUint32());
308     auto rangePtr = PrintRange::Unmarshalling(parcel);
309     if (rangePtr != nullptr) {
310         SetPageRange(*rangePtr);
311     }
312     SetIsSequential(parcel.ReadBool());
313     auto pageSizePtr = PrintPageSize::Unmarshalling(parcel);
314     if (pageSizePtr != nullptr) {
315         SetPageSize(*pageSizePtr);
316     }
317     SetIsLandscape(parcel.ReadBool());
318     SetColorMode(parcel.ReadUint32());
319     SetDuplexMode(parcel.ReadUint32());
320     hasMargin_ = parcel.ReadBool();
321     if (hasMargin_) {
322         auto marginPtr = PrintMargin::Unmarshalling(parcel);
323         if (marginPtr != nullptr) {
324             margin_ = *marginPtr;
325         }
326     }
327     hasPreview_ = parcel.ReadBool();
328     if (hasPreview_) {
329         auto previewPtr = PrintPreviewAttribute::Unmarshalling(parcel);
330         if (previewPtr != nullptr) {
331             preview_ = *previewPtr;
332         }
333     }
334     hasOption_ = parcel.ReadBool();
335     if (hasOption_) {
336         SetOption(parcel.ReadString());
337     }
338 }
339 
Marshalling(Parcel & parcel) const340 bool PrintJob::Marshalling(Parcel &parcel) const
341 {
342     parcel.WriteUint32(fdList_.size());
343     auto msgParcel = static_cast<MessageParcel*>(&parcel);
344     if (msgParcel != nullptr) {
345         for (auto fd : fdList_) {
346             msgParcel->WriteFileDescriptor(fd);
347         }
348     }
349 
350     parcel.WriteString(GetJobId());
351     parcel.WriteString(GetPrinterId());
352     parcel.WriteUint32(GetJobState());
353     parcel.WriteUint32(GetSubState());
354     parcel.WriteUint32(GetCopyNumber());
355     pageRange_.Marshalling(parcel);
356     parcel.WriteBool(GetIsSequential());
357     return MarshallingParam(parcel);
358 }
359 
MarshallingParam(Parcel & parcel) const360 bool PrintJob::MarshallingParam(Parcel &parcel) const
361 {
362     pageSize_.Marshalling(parcel);
363     parcel.WriteBool(GetIsLandscape());
364     parcel.WriteUint32(GetColorMode());
365     parcel.WriteUint32(GetDuplexMode());
366 
367     parcel.WriteBool(hasMargin_);
368     if (hasMargin_) {
369         margin_.Marshalling(parcel);
370     }
371 
372     parcel.WriteBool(hasPreview_);
373     if (hasPreview_) {
374         preview_.Marshalling(parcel);
375     }
376 
377     parcel.WriteBool(hasOption_);
378     if (hasOption_) {
379         parcel.WriteString(GetOption());
380     }
381 
382     return true;
383 }
384 
Unmarshalling(Parcel & parcel)385 std::shared_ptr<PrintJob> PrintJob::Unmarshalling(Parcel &parcel)
386 {
387     auto nativeObj = std::make_shared<PrintJob>();
388     if (nativeObj == nullptr) {
389         PRINT_HILOGE("nativeObj is nullptr");
390         return nullptr;
391     }
392     nativeObj->ReadFromParcel(parcel);
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