File indexing completed on 2025-01-05 05:09:29

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef JOB_MODEL_H
0008 #define JOB_MODEL_H
0009 
0010 #include <QStandardItemModel>
0011 #include <qqmlregistration.h>
0012 
0013 #include <cups/cups.h>
0014 #include <kcupslib_export.h>
0015 
0016 class KCupsJob;
0017 class KCupsRequest;
0018 
0019 class KCUPSLIB_EXPORT JobModel : public QStandardItemModel
0020 {
0021     Q_OBJECT
0022     QML_ELEMENT
0023 
0024 public:
0025     enum Role {
0026         RoleJobId = Qt::UserRole + 2,
0027         RoleJobState,
0028         RoleJobName,
0029         RoleJobPages,
0030         RoleJobSize,
0031         RoleJobOwner,
0032         RoleJobCreatedAt,
0033         RoleJobIconName,
0034         RoleJobCancelEnabled,
0035         RoleJobHoldEnabled,
0036         RoleJobReleaseEnabled,
0037         RoleJobRestartEnabled,
0038         RoleJobPrinter,
0039         RoleJobOriginatingHostName,
0040         RoleJobAuthenticationRequired
0041     };
0042     Q_ENUM(Role)
0043 
0044     enum JobAction { Cancel, Hold, Release, Move, Reprint };
0045     Q_ENUM(JobAction)
0046 
0047     enum WhichJobs { WhichAll, WhichActive, WhichCompleted };
0048     Q_ENUM(WhichJobs)
0049 
0050     enum Columns {
0051         ColStatus = 0,
0052         ColName,
0053         ColUser,
0054         ColCreated,
0055         ColCompleted,
0056         ColPages,
0057         ColProcessed,
0058         ColSize,
0059         ColStatusMessage,
0060         ColPrinter,
0061         ColFromHost,
0062         LastColumn
0063     };
0064 
0065     explicit JobModel(QObject *parent = nullptr);
0066     void setParentWId(WId parentId);
0067     Q_INVOKABLE void init(const QString &destName = QString());
0068 
0069     Q_INVOKABLE void hold(const QString &printerName, int jobId);
0070     Q_INVOKABLE void release(const QString &printerName, int jobId);
0071     Q_INVOKABLE void cancel(const QString &printerName, int jobId);
0072     Q_INVOKABLE void move(const QString &printerName, int jobId, const QString &toPrinterName);
0073 
0074     QString processingJob() const;
0075 
0076     Qt::ItemFlags flags(const QModelIndex &index) const override;
0077     QStringList mimeTypes() const override;
0078     Qt::DropActions supportedDropActions() const override;
0079     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0080     bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
0081     virtual QHash<int, QByteArray> roleNames() const override;
0082 
0083     Q_INVOKABLE void setWhichJobs(WhichJobs whichjobs);
0084     KCupsRequest *modifyJob(int row, JobAction action, const QString &newDestName = QString(), const QModelIndex &parent = QModelIndex());
0085 
0086 private slots:
0087     void getJobs();
0088     void getJobFinished(KCupsRequest *request);
0089 
0090     void jobCompleted(const QString &text,
0091                       const QString &printerUri,
0092                       const QString &printerName,
0093                       uint printerState,
0094                       const QString &printerStateReasons,
0095                       bool printerIsAcceptingJobs,
0096                       uint jobId,
0097                       uint jobState,
0098                       const QString &jobStateReasons,
0099                       const QString &jobName,
0100                       uint jobImpressionsCompleted);
0101     void insertUpdateJob(const QString &text,
0102                          const QString &printerUri,
0103                          const QString &printerName,
0104                          uint printerState,
0105                          const QString &printerStateReasons,
0106                          bool printerIsAcceptingJobs,
0107                          uint jobId,
0108                          uint jobState,
0109                          const QString &jobStateReasons,
0110                          const QString &jobName,
0111                          uint jobImpressionsCompleted);
0112 
0113 private:
0114     int jobRow(int jobId);
0115     void insertJob(int pos, const KCupsJob &job);
0116     void updateJob(int pos, const KCupsJob &job);
0117     QString jobStatus(ipp_jstate_e job_state);
0118     void clear();
0119 
0120     KCupsRequest *m_jobRequest = nullptr;
0121     QString m_destName;
0122     QString m_processingJob;
0123     QHash<int, QByteArray> m_roles;
0124     int m_whichjobs = CUPS_WHICHJOBS_ACTIVE;
0125     WId m_parentId = 0;
0126 };
0127 
0128 #endif // JOB_MODEL_H