File indexing completed on 2024-04-21 04:56:48

0001 /**
0002  * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef DEVICESMODEL_H
0008 #define DEVICESMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QList>
0012 #include <QPixmap>
0013 
0014 #include "kdeconnectinterfaces_export.h"
0015 
0016 class QDBusPendingCallWatcher;
0017 class DaemonDbusInterface;
0018 class DeviceDbusInterface;
0019 
0020 class KDECONNECTINTERFACES_EXPORT DevicesModel : public QAbstractListModel
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(int displayFilter READ displayFilter WRITE setDisplayFilter NOTIFY displayFilterChanged)
0024     Q_PROPERTY(int count READ rowCount NOTIFY rowsChanged)
0025 
0026 public:
0027     enum ModelRoles {
0028         NameModelRole = Qt::DisplayRole,
0029         IconModelRole = Qt::DecorationRole,
0030         StatusModelRole = Qt::InitialSortOrderRole,
0031         IdModelRole = Qt::UserRole,
0032         IconNameRole,
0033         DeviceRole
0034     };
0035     Q_ENUM(ModelRoles)
0036 
0037     // A device is always paired or reachable or both
0038     // You can combine the Paired and Reachable flags
0039     enum StatusFilterFlag {
0040         NoFilter = 0x00,
0041         Paired = 0x01, // show device only if it's paired
0042         Reachable = 0x02 // show device only if it's reachable
0043     };
0044     Q_DECLARE_FLAGS(StatusFilterFlags, StatusFilterFlag)
0045     Q_FLAGS(StatusFilterFlags)
0046     Q_ENUM(StatusFilterFlag)
0047 
0048     explicit DevicesModel(QObject *parent = nullptr);
0049     ~DevicesModel() override;
0050 
0051     void setDisplayFilter(int flags);
0052     int displayFilter() const;
0053 
0054     QVariant data(const QModelIndex &index, int role) const override;
0055     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0056 
0057     Q_SCRIPTABLE DeviceDbusInterface *getDevice(int row) const;
0058     QHash<int, QByteArray> roleNames() const override;
0059     Q_SCRIPTABLE int rowForDevice(const QString &id) const;
0060 
0061 private Q_SLOTS:
0062     void deviceAdded(const QString &id);
0063     void deviceRemoved(const QString &id);
0064     void deviceUpdated(const QString &id);
0065     void refreshDeviceList();
0066     void receivedDeviceList(QDBusPendingCallWatcher *watcher);
0067 
0068 Q_SIGNALS:
0069     void rowsChanged();
0070     void displayFilterChanged(int value);
0071 
0072 private:
0073     void clearDevices();
0074     void appendDevice(DeviceDbusInterface *dev);
0075     bool passesFilter(DeviceDbusInterface *dev) const;
0076 
0077     DaemonDbusInterface *m_dbusInterface;
0078     QVector<DeviceDbusInterface *> m_deviceList;
0079     StatusFilterFlag m_displayFilter;
0080 };
0081 
0082 // Q_DECLARE_OPERATORS_FOR_FLAGS(DevicesModel::StatusFilterFlag)
0083 
0084 #endif // DEVICESMODEL_H