File indexing completed on 2024-12-08 04:32:43

0001 // SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "events/imagepackevent.h"
0007 
0008 #include <QAbstractListModel>
0009 #include <QCoroTask>
0010 #include <QList>
0011 #include <QObject>
0012 #include <QPointer>
0013 #include <QQmlEngine>
0014 
0015 #include <Quotient/connection.h>
0016 
0017 /**
0018  * @class AccountEmoticonModel
0019  *
0020  * This class defines the model for visualising the account stickers and emojis.
0021  *
0022  * This is based upon the im.ponies.user_emotes spec (MSC2545).
0023  */
0024 class AccountEmoticonModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027     QML_ELEMENT
0028 
0029     /**
0030      * @brief The connection to get emoticons from.
0031      */
0032     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0033 
0034 public:
0035     enum Roles {
0036         UrlRole = Qt::UserRole + 1, /**< The URL for the emoticon. */
0037         ShortCodeRole, /**< The shortcode for the emoticon. */
0038         BodyRole, //**< A textual description of the emoticon */
0039         IsStickerRole, //**< Whether this emoticon is a sticker */
0040         IsEmojiRole, //**< Whether this emoticon is an emoji */
0041     };
0042 
0043     explicit AccountEmoticonModel(QObject *parent = nullptr);
0044 
0045     /**
0046      * @brief Number of rows in the model.
0047      *
0048      * @sa QAbstractItemModel::rowCount
0049      */
0050     [[nodiscard]] int rowCount(const QModelIndex &index) const override;
0051 
0052     /**
0053      * @brief Get the given role value at the given index.
0054      *
0055      * @sa QAbstractItemModel::data
0056      */
0057     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0058 
0059     /**
0060      * @brief Returns a mapping from Role enum values to role names.
0061      *
0062      * @sa Roles, QAbstractItemModel::roleNames()
0063      */
0064     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0065 
0066     [[nodiscard]] Quotient::Connection *connection() const;
0067     void setConnection(Quotient::Connection *connection);
0068 
0069     /**
0070      * @brief Deletes the emoticon at the given index.
0071      */
0072     Q_INVOKABLE void deleteEmoticon(int index);
0073 
0074     /**
0075      * @brief Changes the description for the emoticon at the given index.
0076      */
0077     Q_INVOKABLE void setEmoticonBody(int index, const QString &text);
0078 
0079     /**
0080      * @brief Changes the shortcode for the emoticon at the given index.
0081      */
0082     Q_INVOKABLE void setEmoticonShortcode(int index, const QString &shortCode);
0083 
0084     /**
0085      * @brief Changes the image for the emoticon at the given index.
0086      */
0087     Q_INVOKABLE void setEmoticonImage(int index, const QUrl &source);
0088 
0089     /**
0090      * @brief Add an emoticon with the given parameters.
0091      */
0092     Q_INVOKABLE void addEmoticon(const QUrl &source, const QString &shortcode, const QString &description, const QString &type);
0093 
0094 Q_SIGNALS:
0095     void connectionChanged();
0096 
0097 private:
0098     std::optional<Quotient::ImagePackEventContent> m_images;
0099     QPointer<Quotient::Connection> m_connection;
0100     QCoro::Task<void> doSetEmoticonImage(int index, QUrl source);
0101     QCoro::Task<void> doAddEmoticon(QUrl source, QString shortcode, QString description, QString type);
0102 
0103     void reloadEmoticons();
0104 };