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

0001 /*
0002     SPDX-FileCopyrightText: 2010-2018 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef PRINTER_MODEL_H
0008 #define PRINTER_MODEL_H
0009 
0010 #include <QStandardItemModel>
0011 #include <QTimer>
0012 #include <qqmlregistration.h>
0013 
0014 #include <KCupsPrinter.h>
0015 #include <kcupslib_export.h>
0016 
0017 class KCupsRequest;
0018 class KCUPSLIB_EXPORT PrinterModel : public QStandardItemModel
0019 {
0020     Q_OBJECT
0021     QML_ELEMENT
0022 
0023     Q_PROPERTY(int count READ count NOTIFY countChanged)
0024     Q_PROPERTY(bool serverUnavailable READ serverUnavailable NOTIFY serverUnavailableChanged)
0025     /**
0026      * Whether or not to actually display the location of the printer
0027      *
0028      * Only show the location if there is more than one printer
0029      * and at least two distinct locations exist.  If there is only one
0030      * printer or 2 or more printers have the same location, this will be false
0031      */
0032     Q_PROPERTY(bool displayLocationHint READ displayLocationHint NOTIFY displayLocationHintChanged)
0033     /**
0034      * true if model only contains printers (not classes)
0035      */
0036     Q_PROPERTY(bool printersOnly READ printersOnly NOTIFY countChanged)
0037 
0038 public:
0039     enum Role {
0040         DestStatus = Qt::UserRole,
0041         DestState,
0042         DestName,
0043         DestIsDefault,
0044         DestIsShared,
0045         DestIsAcceptingJobs,
0046         DestIsPaused,
0047         DestIsClass,
0048         DestLocation,
0049         DestDescription,
0050         DestKind,
0051         DestType,
0052         DestCommands,
0053         DestMarkerChangeTime,
0054         DestMarkers,
0055         DestIconName,
0056         DestRemote,
0057         DestUri,
0058         DestUriSupported,
0059         DestMemberNames
0060     };
0061     Q_ENUM(Role)
0062 
0063     enum JobAction { Cancel, Hold, Release, Move };
0064     Q_ENUM(JobAction)
0065 
0066     explicit PrinterModel(QObject *parent = nullptr);
0067 
0068     Qt::ItemFlags flags(const QModelIndex &index) const override;
0069     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0070     int count() const;
0071     bool serverUnavailable() const;
0072 
0073     virtual QHash<int, QByteArray> roleNames() const override;
0074 
0075     Q_INVOKABLE void pausePrinter(const QString &printerName);
0076     Q_INVOKABLE void resumePrinter(const QString &printerName);
0077     Q_INVOKABLE void rejectJobs(const QString &printerName);
0078     Q_INVOKABLE void acceptJobs(const QString &printerName);
0079 
0080     bool displayLocationHint() const;
0081 
0082 public slots:
0083     void update();
0084     void getDestsFinished(KCupsRequest *request);
0085     void slotCountChanged();
0086 
0087 signals:
0088     void countChanged(int count);
0089     void serverUnavailableChanged(bool unavailable);
0090     void error(int lastError, const QString &errorTitle, const QString &errorMsg);
0091     void displayLocationHintChanged();
0092 
0093 private slots:
0094     void insertUpdatePrinterName(const QString &printerName);
0095     void insertUpdatePrinter(const QString &text,
0096                              const QString &printerUri,
0097                              const QString &printerName,
0098                              uint printerState,
0099                              const QString &printerStateReasons,
0100                              bool printerIsAcceptingJobs);
0101     void insertUpdatePrinterFinished(KCupsRequest *request);
0102     void printerRemovedName(const QString &printerName);
0103     void printerRemoved(const QString &text,
0104                         const QString &printerUri,
0105                         const QString &printerName,
0106                         uint printerState,
0107                         const QString &printerStateReasons,
0108                         bool printerIsAcceptingJobs);
0109     void printerStateChanged(const QString &text,
0110                              const QString &printerUri,
0111                              const QString &printerName,
0112                              uint printerState,
0113                              const QString &printerStateReasons,
0114                              bool printerIsAcceptingJobs);
0115     void printerStopped(const QString &text,
0116                         const QString &printerUri,
0117                         const QString &printerName,
0118                         uint printerState,
0119                         const QString &printerStateReasons,
0120                         bool printerIsAcceptingJobs);
0121     void printerRestarted(const QString &text,
0122                           const QString &printerUri,
0123                           const QString &printerName,
0124                           uint printerState,
0125                           const QString &printerStateReasons,
0126                           bool printerIsAcceptingJobs);
0127     void printerShutdown(const QString &text,
0128                          const QString &printerUri,
0129                          const QString &printerName,
0130                          uint printerState,
0131                          const QString &printerStateReasons,
0132                          bool printerIsAcceptingJobs);
0133     void printerModified(const QString &text,
0134                          const QString &printerUri,
0135                          const QString &printerName,
0136                          uint printerState,
0137                          const QString &printerStateReasons,
0138                          bool printerIsAcceptingJobs);
0139     void serverChanged(const QString &text);
0140 
0141 private:
0142     WId m_parentId;
0143     QHash<int, QByteArray> m_roles;
0144     bool m_unavailable = true;
0145     bool m_displayLocationHint = true;
0146 
0147     void setDisplayLocationHint();
0148     int destRow(const QString &destName);
0149     void insertDest(int pos, const KCupsPrinter &printer);
0150     void updateDest(QStandardItem *item, const KCupsPrinter &printer);
0151 
0152     QString destStatus(KCupsPrinter::Status state, const QString &message, bool isAcceptingJobs) const;
0153     bool printersOnly() const;
0154 };
0155 
0156 #endif // PRINTER_MODEL_H