From 584cd59f930431cd017bfe2322038a0d337ccc70 Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Wed, 5 May 2021 19:58:34 +0300 Subject: [PATCH] refactor: move ActionHandler::commands to CommandModel::commands --- src/actionshandler.cpp | 66 ------------------------------------- src/actionshandler.h | 5 +-- src/commandmodel.cpp | 74 ++++++++++++++++++++++++++++++++++++++++-- src/commandmodel.h | 27 ++++----------- 4 files changed, 78 insertions(+), 94 deletions(-) diff --git a/src/actionshandler.cpp b/src/actionshandler.cpp index a1c453341..a65a75d74 100644 --- a/src/actionshandler.cpp +++ b/src/actionshandler.cpp @@ -57,72 +57,6 @@ void ActionsHandler::setConnection(Connection *connection) Q_EMIT connectionChanged(); } -QVariantList ActionsHandler::commands() -{ - QVariantList commands; - - // Messages commands - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/me "), - QStringLiteral(""), - i18n("Displays action")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/shrug "), - QStringLiteral(""), - i18n("Prepends ¯\\_(ツ)_/¯ to a plain-text message")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/lenny "), - QStringLiteral(""), - i18n("Prepends ( ͡° ͜ʖ ͡°) to a plain-text message")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/plain "), - QStringLiteral(""), - i18n("Sends a message as plain text, without interpreting it as markdown")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/html "), - QStringLiteral(""), - i18n("Sends a message as html, without interpreting it as markdown")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/rainbow "), - QStringLiteral(""), - i18n("Sends the given message coloured as a rainbow")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/rainbowme "), - QStringLiteral(""), - i18n("Sends the given emote coloured as a rainbow")})); - - - // Actions commands - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/join "), QStringLiteral(""), - i18n("Joins room with given address")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/part "), - QStringLiteral("[]"), - i18n("Leave room")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/invite "), - QStringLiteral(""), - i18n("Invites user with given id to current room")})); - - commands.append(QVariant::fromValue(Command{ - QStringLiteral("/react "), - QStringLiteral(""), - i18n("React to this message with a text")})); - - // TODO more see elements /help action - - return commands; -} - void ActionsHandler::postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId, diff --git a/src/actionshandler.h b/src/actionshandler.h index 0e36cc60c..acd4af9e5 100644 --- a/src/actionshandler.h +++ b/src/actionshandler.h @@ -16,9 +16,6 @@ class ActionsHandler : public QObject { Q_OBJECT - /// \brief List of command definition. Useful for building an autocompletion - /// engine or an help dialog. - Q_PROPERTY(QVariantList commands READ commands CONSTANT) /// \brief The connection that will handle sending the message. Q_PROPERTY(Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged) @@ -36,7 +33,7 @@ public: explicit ActionsHandler(QObject *parent = nullptr); ~ActionsHandler(); - static QVariantList commands(); + [[nodiscard]] Connection *connection() const; void setConnection(Connection *connection); diff --git a/src/commandmodel.cpp b/src/commandmodel.cpp index c4951a1fe..bb34146a4 100644 --- a/src/commandmodel.cpp +++ b/src/commandmodel.cpp @@ -1,17 +1,17 @@ // SPDX-FileCopyrightText: 2021 Srevin Saju // SPDX-License-Identifier: GPL-3.0-or-later -#include +#include -#include "commandmodel.h" #include "actionshandler.h" +#include "commandmodel.h" QVariantList CommandModel::filterModel(const QString &filter) { QVariantList result; - for (const QVariant &e : ActionsHandler::commands()) { + for (const QVariant &e : CommandModel::commands()) { auto command = qvariant_cast(e); if (command.command.startsWith(filter)) { result.append(e); @@ -23,3 +23,71 @@ QVariantList CommandModel::filterModel(const QString &filter) return result; } + + +QVariantList CommandModel::commands() +{ + QVariantList commands; + + // Messages commands + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/me "), + QStringLiteral(""), + i18n("Displays action")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/shrug "), + QStringLiteral(""), + i18n("Prepends ¯\\_(ツ)_/¯ to a plain-text message")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/lenny "), + QStringLiteral(""), + i18n("Prepends ( ͡° ͜ʖ ͡°) to a plain-text message")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/plain "), + QStringLiteral(""), + i18n("Sends a message as plain text, without interpreting it as markdown")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/html "), + QStringLiteral(""), + i18n("Sends a message as html, without interpreting it as markdown")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/rainbow "), + QStringLiteral(""), + i18n("Sends the given message coloured as a rainbow")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/rainbowme "), + QStringLiteral(""), + i18n("Sends the given emote coloured as a rainbow")})); + + + // Actions commands + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/join "), QStringLiteral(""), + i18n("Joins room with given address")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/part "), + QStringLiteral("[]"), + i18n("Leave room")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/invite "), + QStringLiteral(""), + i18n("Invites user with given id to current room")})); + + commands.append(QVariant::fromValue(Command{ + QStringLiteral("/react "), + QStringLiteral(""), + i18n("React to this message with a text")})); + + // TODO more see elements /help action + + return commands; +} + diff --git a/src/commandmodel.h b/src/commandmodel.h index 91d33deba..3e4ef70f8 100644 --- a/src/commandmodel.h +++ b/src/commandmodel.h @@ -10,30 +10,14 @@ #include struct Command { - Command(QString p, QString a, QString h) - : command(std::move(std::move(p))) - , parameter(std::move(std::move(a))) - , help(std::move(std::move(h))) + Command(const QString &p, const QString &a, const QString &h) + : command(p) + , parameter(a) + , help(h) { } Command() = default; - friend QDataStream &operator<<(QDataStream &arch, const Command &object) - { - arch << object.command; - arch << object.parameter; - arch << object.help; - return arch; - } - - friend QDataStream &operator>>(QDataStream &arch, Command &object) - { - arch >> object.command; - arch >> object.parameter; - arch >> object.help; - return arch; - } - QString command; QString parameter; QString help; @@ -42,6 +26,7 @@ Q_GADGET Q_PROPERTY(QString command MEMBER command) Q_PROPERTY(QString parameter MEMBER parameter) Q_PROPERTY(QString help MEMBER help) + }; Q_DECLARE_METATYPE(Command) @@ -57,5 +42,5 @@ public: } Q_INVOKABLE QVariantList filterModel(const QString &filter); - + static QVariantList commands(); };