• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2023 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// @ts-nocheck
17import {
18  Constants,
19  AppCommonEvent,
20  AppStorageKeyName
21} from '@ohos/common';
22import { PrintJob, PrintJobState } from '@ohos/common';
23import AppStorageHelper from '../Common/Adapter/AppStorageHelper';
24import { Log } from '@ohos/common';
25import emitter from '@ohos.events.emitter';
26
27const TAG = '[PrintJobModel]:';
28
29/**
30 * PrintJobModel
31 */
32
33/**
34 * Print Job model
35 */
36export class PrintJobModel {
37  public  mPrintJobs: Array<PrintJob> = [];
38
39  /**
40   * reset
41   */
42  public reset() {
43    this.mPrintJobs = [];
44    AppStorageHelper.setValue<Array<PrintJob>>(this.mPrintJobs, AppStorageKeyName.JOB_QUEUE_NAME);
45  }
46
47  /**
48   * remove printJob
49   *
50   * @param jobId print Job id
51   */
52  public removePrintJob(jobId: string) {
53    this.mPrintJobs = this.mPrintJobs.filter((printJob) => printJob.jobId !== jobId);
54    Log.info(TAG, 'remove printJob, jobId = ' + JSON.stringify(jobId));
55    AppStorageHelper.setValue<Array<PrintJob>>(this.mPrintJobs, AppStorageKeyName.JOB_QUEUE_NAME);
56  }
57
58  /**
59   * add printJob
60   *
61   * @param printJob PrintJob
62   * @return true for added
63   */
64  public addPrintJob(printJob: PrintJob): boolean {
65    for (let index = 0; index < this.mPrintJobs.length; index++) {
66      if (this.mPrintJobs[index].jobId === printJob.jobId) {
67        Log.info(TAG, 'printJob is already exit, printJob = ' + printJob.jobId);
68        return false;
69      }
70    }
71    this.mPrintJobs.push(printJob)
72    Log.info(TAG, 'add PrintJob, jobId = ' + JSON.stringify(printJob.jobId));
73    AppStorageHelper.setValue<Array<PrintJob>>(this.mPrintJobs, AppStorageKeyName.JOB_QUEUE_NAME);
74    return true;
75  }
76
77  /**
78   * update printJob
79   *
80   * @param index array mPrintJobs index
81   * @param index job PrintJob
82   * @return true for update
83   */
84  public updatePrintJob(index: number, job: PrintJob): void {
85    Log.info(TAG, 'update job info, jobId =' + JSON.stringify(job.jobId));
86    if ((index < 0 || index >= (this.mPrintJobs.length)) || job === undefined) {
87      Log.error(TAG, 'invalid param, return');
88      return;
89    }
90    this.mPrintJobs[index].jobFiles = job.jobFiles;
91    this.mPrintJobs[index].fdList = job.fdList;
92    this.mPrintJobs[index].printerId = job.printerId;
93    this.mPrintJobs[index].jobState = job.jobState;
94    this.mPrintJobs[index].jobSubState = job.jobSubState;
95    this.mPrintJobs[index].copyNumber = job.copyNumber;
96    this.mPrintJobs[index].pageRange = job.pageRange;
97    this.mPrintJobs[index].isSequential = job.isSequential;
98    this.mPrintJobs[index].pageSize = job.pageSize;
99    this.mPrintJobs[index].isLandscape = job.isLandscape;
100    this.mPrintJobs[index].colorMode = job.colorMode;
101    this.mPrintJobs[index].duplexMode = job.duplexMode;
102    this.mPrintJobs[index].margin = job.margin;
103    this.mPrintJobs[index].preview = job.preview;
104    this.mPrintJobs[index].option = job.option;
105    AppStorageHelper.setValue<Array<PrintJob>>(this.mPrintJobs, AppStorageKeyName.JOB_QUEUE_NAME);
106  }
107
108  /**
109   * printJob state change
110   *
111   * @param jobId  job id
112   * @param state job state
113   * @param subState job subState
114   * @return true for change
115   */
116  public printJobStateChange(jobId: string, state: number, subState: number): boolean {
117    Log.info(TAG, 'printJobStateChange, jobId = ' + JSON.stringify(jobId));
118    Log.info(TAG, 'printJobStateChange, PrintJobs.length = ' + this.mPrintJobs.length);
119    for (let index = 0; index < this.mPrintJobs.length; index++) {
120      if (this.mPrintJobs[index].jobId === jobId) {
121        Log.info(TAG, 'printJobStateChanging, jobId = ' + this.mPrintJobs[index].jobId);
122        this.mPrintJobs[index].jobState = state;
123        this.mPrintJobs[index].jobSubState = subState;
124        AppStorageHelper.setValue<Array<PrintJob>>(this.mPrintJobs, AppStorageKeyName.JOB_QUEUE_NAME);
125        if (state === PrintJobState.PRINT_JOB_COMPLETED) {
126          setTimeout(() => {
127            Log.info(TAG, 'show completed job time out, jobId = ' + JSON.stringify(jobId));
128            this.removePrintJob(jobId);
129            if (this.mPrintJobs.length === 0) {
130              let innerEvent = {
131                eventId: AppCommonEvent.TERMINATE_JOB_MANAGER_ABILITY_EVENT,
132                priority: emitter.EventPriority.HIGH
133              }
134              emitter.emit(innerEvent);
135            } else {
136            }
137          }, Constants.SHOW_JOB_COMPLETED_TIMEOUT);
138        }
139      }
140    }
141    return true
142  }
143}
144