File indexing completed on 2024-05-12 16:25:03

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <KLazyLocalizedString>
0007 #include <QAbstractListModel>
0008 #include <Quotient/events/roommessageevent.h>
0009 
0010 class NeoChatRoom;
0011 
0012 /**
0013  * @class ActionsModel
0014  *
0015  * This class defines a model for chat actions.
0016  *
0017  * @note A chat action is a message starting with /, resulting in something other
0018  *       than a normal message being sent (e.g. /me, /join).
0019  */
0020 class ActionsModel : public QAbstractListModel
0021 {
0022 public:
0023     /**
0024      * @brief Definition of an action.
0025      */
0026     struct Action {
0027         QString prefix; /**< The prefix, without '/' and space after the word. */
0028         /**
0029          * @brief The function to execute when the action is triggered.
0030          */
0031         std::function<QString(const QString &, NeoChatRoom *)> handle;
0032         /**
0033          * @brief Whether the action is a message type action.
0034          *
0035          * If this is true, a message action will be sent. If this is false, this
0036          * message does some action on the client and should not be sent as a message.
0037          */
0038         bool messageAction;
0039         /**
0040          * @brief The new message type of a message being sent.
0041          *
0042          * For a non-message action or a message action that outputs the same type
0043          * as its input, it's nullopt.
0044          */
0045         std::optional<Quotient::RoomMessageEvent::MsgType> messageType = std::nullopt;
0046         KLazyLocalizedString parameters; /**< The input parameters expected by the action. */
0047         KLazyLocalizedString description; /**< The description of the action. */
0048     };
0049     static ActionsModel &instance()
0050     {
0051         static ActionsModel _instance;
0052         return _instance;
0053     }
0054 
0055     /**
0056      * @brief Defines the model roles.
0057      */
0058     enum Roles {
0059         Prefix = Qt::DisplayRole, /**< The prefix, without '/' and space after the word. */
0060         Description, /**< The description of the action. */
0061         CompletionType, /**< The completion type (always "action" for this model). */
0062         Parameters, /**< The input parameters expected by the action. */
0063     };
0064     Q_ENUM(Roles)
0065 
0066     /**
0067      * @brief Get the given role value at the given index.
0068      *
0069      * @sa QAbstractItemModel::data
0070      */
0071     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0072 
0073     /**
0074      * @brief Number of rows in the model.
0075      *
0076      * @sa QAbstractItemModel::rowCount
0077      */
0078     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0079 
0080     /**
0081      * @brief Returns a mapping from Role enum values to role names.
0082      *
0083      * @sa EventRoles, QAbstractItemModel::roleNames()
0084      */
0085     QHash<int, QByteArray> roleNames() const override;
0086 
0087     /**
0088      * @brief Return a vector with all supported actions.
0089      */
0090     QVector<Action> &allActions() const;
0091 
0092 private:
0093     ActionsModel() = default;
0094 };