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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Nicoletti <dantti12@gmail.com>
0003     SPDX-FileCopyrightText: 2023 Mike Noe <noeerover@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef DEVICES_MODEL_H
0009 #define DEVICES_MODEL_H
0010 
0011 #include <QDBusMessage>
0012 #include <QRegularExpression>
0013 #include <QStandardItemModel>
0014 
0015 #include <qqmlregistration.h>
0016 
0017 #include <KCupsRequest.h>
0018 #include <kcupslib_export.h>
0019 
0020 typedef QMap<QString, QString> MapSS;
0021 typedef QMap<QString, MapSS> MapSMapSS;
0022 
0023 class KCupsRequest;
0024 
0025 class KCUPSLIB_EXPORT DevicesModel : public QStandardItemModel
0026 {
0027     Q_OBJECT
0028     QML_ELEMENT
0029 
0030 public:
0031     enum Role {
0032         DeviceClass = Qt::UserRole + 2,
0033         DeviceId,
0034         DeviceInfo,
0035         DeviceMakeAndModel,
0036         DeviceUri,
0037         DeviceUris,
0038         DeviceLocation,
0039         DeviceDescription,
0040         DeviceCategory
0041     };
0042     Q_ENUM(Role)
0043 
0044     enum Kind { Other, Local, Networked, OtherNetworked };
0045     Q_ENUM(Kind)
0046 
0047     explicit DevicesModel(QObject *parent = nullptr);
0048     virtual QHash<int, QByteArray> roleNames() const override;
0049 
0050 Q_SIGNALS:
0051     void loaded();
0052     void parentAdded(const QModelIndex &index);
0053     void errorMessage(const QString &message);
0054 
0055 public Q_SLOTS:
0056     void update();
0057     void insertDevice(const QString &device_class,
0058                       const QString &device_id,
0059                       const QString &device_info,
0060                       const QString &device_make_and_model,
0061                       const QString &device_uri,
0062                       const QString &device_location,
0063                       const QStringList &grouped_uris = QStringList());
0064 
0065     void insertDevice(const QString &device_class,
0066                       const QString &device_id,
0067                       const QString &device_info,
0068                       const QString &device_make_and_model,
0069                       const QString &device_uri,
0070                       const QString &device_location,
0071                       const KCupsPrinters &grouped_printers);
0072 
0073     QString uriDevice(const QString &uri) const;
0074 
0075 private Q_SLOTS:
0076     QStandardItem *createItem(const QString &device_class,
0077                               const QString &device_id,
0078                               const QString &device_info,
0079                               const QString &device_make_and_model,
0080                               const QString &device_uri,
0081                               const QString &device_location,
0082                               bool grouped);
0083 
0084     void gotDevice(const QString &device_class,
0085                    const QString &device_id,
0086                    const QString &device_info,
0087                    const QString &device_make_and_model,
0088                    const QString &device_uri,
0089                    const QString &device_location);
0090 
0091     void finished();
0092     void getGroupedDevicesSuccess(const QDBusMessage &message);
0093     void getGroupedDevicesFailed(const QDBusError &error, const QDBusMessage &message);
0094     void groupedDevicesFallback();
0095 
0096 private:
0097     QStandardItem *findCreateCategory(const QString &category, Kind kind);
0098     QString deviceDescription(const QString &uri) const;
0099 
0100     KCupsRequest *m_request = nullptr;
0101     MapSMapSS m_mappedDevices;
0102     QRegularExpression m_rx;
0103     QStringList m_blacklistedURIs;
0104     QHash<int, QByteArray> m_roles;
0105 };
0106 
0107 Q_DECLARE_METATYPE(MapSS)
0108 Q_DECLARE_METATYPE(MapSMapSS)
0109 
0110 #endif