File indexing completed on 2024-12-01 13:08:31

0001 // SPDX-FileCopyrightText: Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QPointer>
0008 
0009 #include <Quotient/csapi/definitions/client_device.h>
0010 
0011 namespace Quotient
0012 {
0013 class Connection;
0014 }
0015 
0016 /**
0017  * @class DevicesModel
0018  *
0019  * This class defines the model for managing the devices of the local user.
0020  *
0021  * A device is any session where the local user is logged into a client. This means
0022  * the same physical device can have multiple sessions for example if the user uses
0023  * multiple clients on the same machine.
0024  */
0025 class DevicesModel : public QAbstractListModel
0026 {
0027     Q_OBJECT
0028 
0029     /**
0030      * @brief The current connection that the model is getting its devices from.
0031      */
0032     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged REQUIRED)
0033 
0034 public:
0035     /**
0036      * @brief Defines the model roles.
0037      */
0038     enum Roles {
0039         Id, /**< The device ID. */
0040         DisplayName, /**< Display name set by the user for this device. */
0041         LastIp, /**< The IP address where this device was last seen. */
0042         LastTimestamp, /**< The timestamp when this devices was last seen. */
0043         TimestampString, /**< String for the timestamp when this devices was last seen. */
0044         Type, /**< The category to sort this device into. */
0045     };
0046     Q_ENUM(Roles)
0047 
0048     enum DeviceType {
0049         This,
0050         Verified,
0051         Unverified,
0052         Unencrypted,
0053     };
0054     Q_ENUM(DeviceType);
0055 
0056     /**
0057      * @brief Get the given role value at the given index.
0058      *
0059      * @sa QAbstractItemModel::data
0060      */
0061     QVariant data(const QModelIndex &index, int role) const override;
0062 
0063     /**
0064      * @brief Number of rows in the model.
0065      *
0066      * @sa  QAbstractItemModel::rowCount
0067      */
0068     int rowCount(const QModelIndex &parent) const override;
0069 
0070     /**
0071      * @brief Returns a mapping from Role enum values to role names.
0072      *
0073      * @sa Roles, QAbstractItemModel::roleNames()
0074      */
0075     QHash<int, QByteArray> roleNames() const override;
0076 
0077     /**
0078      * @brief Logout the device with the given id.
0079      */
0080     Q_INVOKABLE void logout(const QString &deviceId, const QString &password);
0081 
0082     /**
0083      * @brief Set the display name of the device with the given id.
0084      */
0085     Q_INVOKABLE void setName(const QString &deviceId, const QString &name);
0086 
0087     explicit DevicesModel(QObject *parent = nullptr);
0088 
0089 
0090     [[nodiscard]] Quotient::Connection *connection() const;
0091     void setConnection(Quotient::Connection *connection);
0092 
0093 Q_SIGNALS:
0094     void connectionChanged();
0095     void countChanged();
0096 
0097 private:
0098     void fetchDevices();
0099     QVector<Quotient::Device> m_devices;
0100     QPointer<Quotient::Connection> m_connection;
0101 };