File indexing completed on 2024-12-01 07:40:33

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