File indexing completed on 2024-04-28 04:59:31

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