File indexing completed on 2024-11-03 10:41:26

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