From 8327b4369e6028877015a366888fa872e5a499b9 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 6 Apr 2025 12:59:07 +0100 Subject: [PATCH] Rework so that ActionsModel no longer depends on NeoChatConfig or RoomManager --- src/controller.cpp | 6 ++++++ src/models/actionsmodel.cpp | 23 ++++++++++++++--------- src/models/actionsmodel.h | 18 ++++++++++++++++++ src/roommanager.cpp | 5 +++++ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index afe9097e7..78f4f4069 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -18,6 +18,7 @@ #include #include +#include "models/actionsmodel.h" #include "neochatconfig.h" #include "neochatconnection.h" #include "neochatroom.h" @@ -62,6 +63,11 @@ Controller::Controller(QObject *parent) Q_EMIT globalUrlPreviewDefaultChanged(); }); + ActionsModel::setAllowQuickEdit(NeoChatConfig::allowQuickEdit()); + connect(NeoChatConfig::self(), &NeoChatConfig::AllowQuickEditChanged, this, []() { + ActionsModel::setAllowQuickEdit(NeoChatConfig::allowQuickEdit()); + }); + ProxyController::instance().setApplicationProxy(); #ifndef Q_OS_ANDROID diff --git a/src/models/actionsmodel.cpp b/src/models/actionsmodel.cpp index 877387031..02e16f71d 100644 --- a/src/models/actionsmodel.cpp +++ b/src/models/actionsmodel.cpp @@ -5,10 +5,8 @@ #include "chatbarcache.h" #include "enums/messagetype.h" -#include "neochatconfig.h" #include "neochatconnection.h" #include "neochatroom.h" -#include "roommanager.h" #include #include #include @@ -20,6 +18,8 @@ using Action = ActionsModel::Action; using namespace Quotient; using namespace Qt::StringLiterals; +bool ActionsModel::m_allowQuickEdit = false; + QStringList rainbowColors{"#ff2b00"_L1, "#ff5500"_L1, "#ff8000"_L1, "#ffaa00"_L1, "#ffd500"_L1, "#ffff00"_L1, "#d4ff00"_L1, "#aaff00"_L1, "#80ff00"_L1, "#55ff00"_L1, "#2bff00"_L1, "#00ff00"_L1, "#00ff2b"_L1, "#00ff55"_L1, "#00ff80"_L1, "#00ffaa"_L1, "#00ffd5"_L1, "#00ffff"_L1, "#00d4ff"_L1, "#00aaff"_L1, "#007fff"_L1, "#0055ff"_L1, "#002bff"_L1, "#0000ff"_L1, "#2a00ff"_L1, "#5500ff"_L1, "#7f00ff"_L1, @@ -226,11 +226,11 @@ QList actions{ } auto targetRoom = text.startsWith(QLatin1Char('!')) ? room->connection()->room(text) : room->connection()->roomByAlias(text); if (targetRoom) { - RoomManager::instance().resolveResource(targetRoom->id()); + ActionsModel::instance().resolveResource(targetRoom->id()); return QString(); } Q_EMIT room->showMessage(MessageType::Information, i18nc("Joining room .", "Joining room %1.", text)); - RoomManager::instance().resolveResource(text, "join"_L1); + ActionsModel::instance().resolveResource(text, "join"_L1); return QString(); }, std::nullopt, @@ -251,16 +251,16 @@ QList actions{ } auto targetRoom = text.startsWith(QLatin1Char('!')) ? room->connection()->room(text) : room->connection()->roomByAlias(text); if (targetRoom) { - RoomManager::instance().resolveResource(targetRoom->id()); + ActionsModel::instance().resolveResource(targetRoom->id()); return QString(); } Q_EMIT room->showMessage(MessageType::Information, i18nc("Knocking room .", "Knocking room %1.", text)); auto connection = dynamic_cast(room->connection()); const auto knownServer = roomName.mid(roomName.indexOf(":"_L1) + 1); if (parts.length() >= 2) { - RoomManager::instance().knockRoom(connection, roomName, parts[1], QStringList{knownServer}); + ActionsModel::instance().knockRoom(connection, roomName, parts[1], QStringList{knownServer}); } else { - RoomManager::instance().knockRoom(connection, roomName, QString(), QStringList{knownServer}); + ActionsModel::instance().knockRoom(connection, roomName, QString(), QStringList{knownServer}); } return QString(); }, @@ -283,7 +283,7 @@ QList actions{ return QString(); } Q_EMIT room->showMessage(MessageType::Information, i18nc("Joining room .", "Joining room %1.", text)); - RoomManager::instance().resolveResource(text, "join"_L1); + ActionsModel::instance().resolveResource(text, "join"_L1); return QString(); }, std::nullopt, @@ -553,7 +553,7 @@ bool ActionsModel::handleQuickEditAction(NeoChatRoom *room, const QString &messa return false; } - if (NeoChatConfig::allowQuickEdit()) { + if (m_allowQuickEdit) { QRegularExpression sed(u"^s/([^/]*)/([^/]*)(/g)?$"_s); auto match = sed.match(messageText); if (match.hasMatch()) { @@ -618,3 +618,8 @@ std::pair, std::optional class ChatBarCache; +class NeoChatConnection; class NeoChatRoom; /** @@ -20,6 +21,8 @@ class NeoChatRoom; */ class ActionsModel : public QAbstractListModel { + Q_OBJECT + public: /** * @brief Definition of an action. @@ -97,6 +100,21 @@ public: */ static std::pair, std::optional> handleAction(NeoChatRoom *room, ChatBarCache *chatBarCache); + static void setAllowQuickEdit(bool allow); + +Q_SIGNALS: + /** + * @brief Request a resource is resolved. + */ + void resolveResource(const QString &idOrUri, const QString &action = {}); + + /** + * @brief Request a room Knock. + */ + void knockRoom(NeoChatConnection *account, const QString &roomAliasOrId, const QString &reason, const QStringList &viaServers); + private: ActionsModel() = default; + + static bool m_allowQuickEdit; }; diff --git a/src/roommanager.cpp b/src/roommanager.cpp index adb48d882..f150d65a9 100644 --- a/src/roommanager.cpp +++ b/src/roommanager.cpp @@ -7,6 +7,7 @@ #include "chatbarcache.h" #include "controller.h" #include "eventhandler.h" +#include "models/actionsmodel.h" #include "neochatconfig.h" #include "neochatconnection.h" #include "neochatroom.h" @@ -67,6 +68,10 @@ RoomManager::RoomManager(QObject *parent) m_roomTreeModel->setConnection(m_connection); }); connect(m_sortFilterSpaceListModel, &SortFilterSpaceListModel::layoutChanged, m_sortFilterRoomTreeModel, &SortFilterRoomTreeModel::invalidate); + connect(&ActionsModel::instance(), &ActionsModel::resolveResource, this, [this](const QString &idOrUri, const QString &action) { + resolveResource(idOrUri, action); + }); + connect(&ActionsModel::instance(), &ActionsModel::knockRoom, this, &RoomManager::knockRoom); } RoomManager::~RoomManager()