diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 235c81668..bf17a841b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -194,6 +194,7 @@ add_library(neochat STATIC neochatroommember.h models/threadmodel.cpp models/threadmodel.h + enums/messagetype.h ) set_source_files_properties(qml/OsmLocationPlugin.qml PROPERTIES diff --git a/src/controller.h b/src/controller.h index 3865e2e58..e5e41afc8 100644 --- a/src/controller.h +++ b/src/controller.h @@ -53,16 +53,6 @@ class Controller : public QObject Q_PROPERTY(bool csSupported READ csSupported CONSTANT) public: - /** - * @brief Define the types on inline messages that can be shown. - */ - enum MessageType { - Positive, /**< Positive message, typically green. */ - Info, /**< Info message, typically highlight color. */ - Error, /**< Error message, typically red. */ - }; - Q_ENUM(MessageType) - static Controller &instance(); static Controller *create(QQmlEngine *engine, QJSEngine *) { @@ -144,5 +134,4 @@ Q_SIGNALS: void connectionDropped(NeoChatConnection *connection); void activeConnectionChanged(NeoChatConnection *connection); void accountsLoadingChanged(); - void showMessage(MessageType messageType, const QString &message); }; diff --git a/src/enums/messagetype.h b/src/enums/messagetype.h new file mode 100644 index 000000000..72c8e93f3 --- /dev/null +++ b/src/enums/messagetype.h @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +#pragma once + +#include +#include + +/** + * @class MessageType + * + * This class is designed to define the MessageType enumeration. + */ +class MessageType : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_UNCREATABLE("") + +public: + /** + * @brief The types of messages that can be shown. + */ + enum Type { + Information = 0, /**< Info message, typically highlight color. */ + Positive, /**< Positive message, typically green. */ + Warning, /**< Warning message, typically amber. */ + Error, /**< Error message, typically red. */ + }; + Q_ENUM(Type); +}; diff --git a/src/models/actionsmodel.cpp b/src/models/actionsmodel.cpp index e6ee1b74d..251d9c591 100644 --- a/src/models/actionsmodel.cpp +++ b/src/models/actionsmodel.cpp @@ -4,7 +4,7 @@ #include "actionsmodel.h" #include "chatbarcache.h" -#include "controller.h" +#include "enums/messagetype.h" #include "neochatconnection.h" #include "neochatroom.h" #include "roommanager.h" @@ -24,15 +24,14 @@ QStringList rainbowColors{"#ff2b00"_ls, "#ff5500"_ls, "#ff8000"_ls, "#ffaa00"_ls auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache *) { if (text.isEmpty()) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18n("Leaving this room.")); + Q_EMIT room->showMessage(MessageType::Information, i18n("Leaving this room.")); room->connection()->leaveRoom(room); } else { QRegularExpression roomRegex(QStringLiteral(R"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)")); auto regexMatch = roomRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, - i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); + Q_EMIT room->showMessage(MessageType::Error, + i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); return QString(); } auto leaving = room->connection()->room(text); @@ -40,10 +39,10 @@ auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache * leaving = room->connection()->roomByAlias(text); } if (leaving) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("Leaving room .", "Leaving room %1.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc("Leaving room .", "Leaving room %1.", text)); room->connection()->leaveRoom(leaving); } else { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("Room not found", "Room %1 not found.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc("Room not found", "Room %1 not found.", text)); } } return QString(); @@ -51,7 +50,7 @@ auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache * auto roomNickLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache *) { if (text.isEmpty()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("No new nickname provided, no changes will happen.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("No new nickname provided, no changes will happen.")); } else { room->connection()->user()->rename(text, room); } @@ -193,31 +192,29 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + Q_EMIT room->showMessage(MessageType::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); return QString(); } const RoomMemberEvent *roomMemberEvent = room->currentState().get(text); if (roomMemberEvent && roomMemberEvent->membership() == Membership::Invite) { - Q_EMIT Controller::instance().showMessage(Controller::Info, - i18nc(" is already invited to this room.", "%1 is already invited to this room.", text)); + Q_EMIT room->showMessage(MessageType::Information, + i18nc(" is already invited to this room.", "%1 is already invited to this room.", text)); return QString(); } if (roomMemberEvent && roomMemberEvent->membership() == Membership::Ban) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc(" is banned from this room.", "%1 is banned from this room.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc(" is banned from this room.", "%1 is banned from this room.", text)); return QString(); } if (room->localMember().id() == text) { - Q_EMIT Controller::instance().showMessage(Controller::Positive, i18n("You are already in this room.")); + Q_EMIT room->showMessage(MessageType::Positive, i18n("You are already in this room.")); return QString(); } if (room->joinedMemberIds().contains(text)) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc(" is already in this room.", "%1 is already in this room.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc(" is already in this room.", "%1 is already in this room.", text)); return QString(); } room->inviteToRoom(text); - Q_EMIT Controller::instance().showMessage(Controller::Positive, - i18nc(" was invited into this room", "%1 was invited into this room", text)); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was invited into this room", "%1 was invited into this room", text)); return QString(); }, false, @@ -231,9 +228,8 @@ QList actions{ QRegularExpression roomRegex(QStringLiteral(R"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)")); auto regexMatch = roomRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, - i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); + Q_EMIT room->showMessage(MessageType::Error, + i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); return QString(); } auto targetRoom = text.startsWith(QLatin1Char('!')) ? room->connection()->room(text) : room->connection()->roomByAlias(text); @@ -241,7 +237,7 @@ QList actions{ RoomManager::instance().resolveResource(targetRoom->id()); return QString(); } - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("Joining room .", "Joining room %1.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc("Joining room .", "Joining room %1.", text)); RoomManager::instance().resolveResource(text, "join"_ls); return QString(); }, @@ -258,9 +254,8 @@ QList actions{ QRegularExpression roomRegex(QStringLiteral(R"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)")); auto regexMatch = roomRegex.match(roomName); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, - i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); + Q_EMIT room->showMessage(MessageType::Error, + i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); return QString(); } auto targetRoom = text.startsWith(QLatin1Char('!')) ? room->connection()->room(text) : room->connection()->roomByAlias(text); @@ -268,7 +263,7 @@ QList actions{ RoomManager::instance().resolveResource(targetRoom->id()); return QString(); } - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("Knocking room .", "Knocking room %1.", text)); + 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(":"_ls) + 1); if (parts.length() >= 2) { @@ -289,16 +284,15 @@ QList actions{ QRegularExpression roomRegex(QStringLiteral(R"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)")); auto regexMatch = roomRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, - i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); + Q_EMIT room->showMessage(MessageType::Error, + i18nc("'' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); return QString(); } if (room->connection()->room(text) || room->connection()->roomByAlias(text)) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("You are already in room .", "You are already in room %1.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc("You are already in room .", "You are already in room %1.", text)); return QString(); } - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc("Joining room .", "Joining room %1.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc("Joining room .", "Joining room %1.", text)); RoomManager::instance().resolveResource(text, "join"_ls); return QString(); }, @@ -327,7 +321,7 @@ QList actions{ QStringLiteral("nick"), [](const QString &text, NeoChatRoom *room, ChatBarCache *) { if (text.isEmpty()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("No new nickname provided, no changes will happen.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("No new nickname provided, no changes will happen.")); } else { room->connection()->user()->rename(text); } @@ -361,16 +355,15 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + Q_EMIT room->showMessage(MessageType::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); return QString(); } if (room->connection()->ignoredUsers().contains(text)) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc(" is already ignored.", "%1 is already ignored.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc(" is already ignored.", "%1 is already ignored.", text)); return QString(); } room->connection()->addToIgnoredUsers(text); - Q_EMIT Controller::instance().showMessage(Controller::Positive, i18nc(" is now ignored", "%1 is now ignored.", text)); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" is now ignored", "%1 is now ignored.", text)); return QString(); }, @@ -386,16 +379,15 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + Q_EMIT room->showMessage(MessageType::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); return QString(); } if (!room->connection()->ignoredUsers().contains(text)) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18nc(" is not ignored.", "%1 is not ignored.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc(" is not ignored.", "%1 is not ignored.", text)); return QString(); } room->connection()->removeFromIgnoredUsers(text); - Q_EMIT Controller::instance().showMessage(Controller::Positive, i18nc(" is no longer ignored.", "%1 is no longer ignored.", text)); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" is no longer ignored.", "%1 is no longer ignored.", text)); return QString(); }, false, @@ -431,14 +423,13 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(parts[0]); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + Q_EMIT room->showMessage(MessageType::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); return QString(); } auto state = room->currentState().get(parts[0]); if (state && state->membership() == Membership::Ban) { - Q_EMIT Controller::instance().showMessage(Controller::Info, - i18nc(" is already banned from this room.", "%1 is already banned from this room.", text)); + Q_EMIT room->showMessage(MessageType::Information, + i18nc(" is already banned from this room.", "%1 is already banned from this room.", text)); return QString(); } auto plEvent = room->currentState().get(); @@ -446,18 +437,17 @@ QList actions{ return QString(); } if (plEvent->ban() > plEvent->powerLevelForUser(room->localMember().id())) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("You are not allowed to ban users from this room.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("You are not allowed to ban users from this room.")); return QString(); } if (plEvent->powerLevelForUser(room->localMember().id()) <= plEvent->powerLevelForUser(parts[0])) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, + Q_EMIT room->showMessage( + MessageType::Error, i18nc("You are not allowed to ban from this room.", "You are not allowed to ban %1 from this room.", parts[0])); return QString(); } room->ban(parts[0], parts.size() > 1 ? parts.mid(1).join(QLatin1Char(' ')) : QString()); - Q_EMIT Controller::instance().showMessage(Controller::Positive, - i18nc(" was banned from this room.", "%1 was banned from this room.", parts[0])); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was banned from this room.", "%1 was banned from this room.", parts[0])); return QString(); }, false, @@ -472,8 +462,7 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(text); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); + Q_EMIT room->showMessage(MessageType::Error, i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", text)); return QString(); } auto plEvent = room->currentState().get(); @@ -481,18 +470,16 @@ QList actions{ return QString(); } if (plEvent->ban() > plEvent->powerLevelForUser(room->localMember().id())) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("You are not allowed to unban users from this room.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("You are not allowed to unban users from this room.")); return QString(); } auto state = room->currentState().get(text); if (state && state->membership() != Membership::Ban) { - Q_EMIT Controller::instance().showMessage(Controller::Info, - i18nc(" is not banned from this room.", "%1 is not banned from this room.", text)); + Q_EMIT room->showMessage(MessageType::Information, i18nc(" is not banned from this room.", "%1 is not banned from this room.", text)); return QString(); } room->unban(text); - Q_EMIT Controller::instance().showMessage(Controller::Positive, - i18nc(" was unbanned from this room.", "%1 was unbanned from this room.", text)); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was unbanned from this room.", "%1 was unbanned from this room.", text)); return QString(); }, @@ -509,16 +496,16 @@ QList actions{ QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))")); auto regexMatch = mxidRegex.match(parts[0]); if (!regexMatch.hasMatch()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, - i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", parts[0])); + Q_EMIT room->showMessage(MessageType::Error, + i18nc("'' does not look like a matrix id.", "'%1' does not look like a matrix id.", parts[0])); return QString(); } if (parts[0] == room->localMember().id()) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("You cannot kick yourself from the room.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("You cannot kick yourself from the room.")); return QString(); } if (!room->isMember(parts[0])) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18nc(" is not in this room", "%1 is not in this room.", parts[0])); + Q_EMIT room->showMessage(MessageType::Error, i18nc(" is not in this room", "%1 is not in this room.", parts[0])); return QString(); } auto plEvent = room->currentState().get(); @@ -527,18 +514,17 @@ QList actions{ } auto kick = plEvent->kick(); if (plEvent->powerLevelForUser(room->localMember().id()) < kick) { - Q_EMIT Controller::instance().showMessage(Controller::Error, i18n("You are not allowed to kick users from this room.")); + Q_EMIT room->showMessage(MessageType::Error, i18n("You are not allowed to kick users from this room.")); return QString(); } if (plEvent->powerLevelForUser(room->localMember().id()) <= plEvent->powerLevelForUser(parts[0])) { - Q_EMIT Controller::instance().showMessage( - Controller::Error, + Q_EMIT room->showMessage( + MessageType::Error, i18nc("You are not allowed to kick from this room", "You are not allowed to kick %1 from this room.", parts[0])); return QString(); } room->kickMember(parts[0], parts.size() > 1 ? parts.mid(1).join(QLatin1Char(' ')) : QString()); - Q_EMIT Controller::instance().showMessage(Controller::Positive, - i18nc(" was kicked from this room.", "%1 was kicked from this room.", parts[0])); + Q_EMIT room->showMessage(MessageType::Positive, i18nc(" was kicked from this room.", "%1 was kicked from this room.", parts[0])); return QString(); }, false, diff --git a/src/neochatconnection.cpp b/src/neochatconnection.cpp index fb4e48a19..8b2cf9a83 100644 --- a/src/neochatconnection.cpp +++ b/src/neochatconnection.cpp @@ -83,7 +83,8 @@ void NeoChatConnection::connectSignals() }); connect(this, &NeoChatConnection::requestFailed, this, [](BaseJob *job) { if (dynamic_cast(job) && job->jsonData()["errcode"_ls].toString() == "M_TOO_LARGE"_ls) { - RoomManager::instance().warning(i18n("File too large to download."), i18n("Contact your matrix server administrator for support.")); + RoomManager::instance().showMessage(MessageType::Warning, + i18n("File too large to download.
Contact your matrix server administrator for support.")); } }); connect(this, &NeoChatConnection::directChatsListChanged, this, [this](DirectChatsMap additions, DirectChatsMap removals) { diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 25bfd9f31..39e0a1058 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -36,7 +36,6 @@ #include "chatbarcache.h" #include "clipboard.h" -#include "controller.h" #include "eventhandler.h" #include "events/joinrulesevent.h" #include "events/pollevent.h" @@ -1450,9 +1449,9 @@ void NeoChatRoom::updatePushNotificationState(QString type) void NeoChatRoom::reportEvent(const QString &eventId, const QString &reason) { auto job = connection()->callApi(id(), eventId, -50, reason); - connect(job, &BaseJob::finished, this, [job]() { + connect(job, &BaseJob::finished, this, [this, job]() { if (job->error() == BaseJob::Success) { - Q_EMIT Controller::instance().showMessage(Controller::Positive, i18n("Report sent successfully.")); + Q_EMIT showMessage(MessageType::Positive, i18n("Report sent successfully.")); } }); } diff --git a/src/neochatroom.h b/src/neochatroom.h index 88b528e95..58b8cfd88 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -12,6 +12,7 @@ #include #include +#include "enums/messagetype.h" #include "enums/pushrule.h" #include "neochatroommember.h" #include "pollhandler.h" @@ -658,6 +659,11 @@ Q_SIGNALS: void extraEventLoaded(const QString &eventId); void extraEventNotFound(const QString &eventId); + /** + * @brief Request a message be shown to the user of the given type. + */ + void showMessage(MessageType::Type messageType, const QString &message); + public Q_SLOTS: /** * @brief Upload a file to the matrix server and post the file to the room. diff --git a/src/qml/RoomPage.qml b/src/qml/RoomPage.qml index 16bf96010..e46109af3 100644 --- a/src/qml/RoomPage.qml +++ b/src/qml/RoomPage.qml @@ -199,10 +199,6 @@ Kirigami.Page { } } - function onWarning(title, message) { - root.warning(title, message); - } - function onGoToEvent(eventId) { (timelineViewLoader.item as TimelineView).goToEvent(eventId); } @@ -241,22 +237,13 @@ Kirigami.Page { } Connections { - target: Controller + target: RoomManager + function onShowMessage(messageType, message) { banner.text = message; - banner.type = messageType === ActionsHandler.Error ? Kirigami.MessageType.Error : messageType === ActionsHandler.Positive ? Kirigami.MessageType.Positive : Kirigami.MessageType.Information; + banner.type = messageType; banner.visible = true; } - } - - function warning(title, message) { - banner.text = `${title}
${message}`; - banner.type = Kirigami.MessageType.Warning; - banner.visible = true; - } - - Connections { - target: RoomManager function onShowEventSource(eventId) { applicationWindow().pageStack.pushDialogLayer(Qt.createComponent('org.kde.neochat', 'MessageSourceSheet'), { diff --git a/src/roommanager.cpp b/src/roommanager.cpp index f9dab98f6..212dabe5b 100644 --- a/src/roommanager.cpp +++ b/src/roommanager.cpp @@ -134,7 +134,7 @@ void RoomManager::resolveResource(const QString &idOrUri, const QString &action) { Uri uri{idOrUri}; if (!uri.isValid()) { - Q_EMIT warning(i18n("Malformed or empty Matrix id"), i18n("%1 is not a correct Matrix identifier", idOrUri)); + Q_EMIT showMessage(MessageType::Warning, i18n("Malformed or empty Matrix id
%1 is not a correct Matrix identifier", idOrUri)); return; } @@ -351,7 +351,7 @@ void RoomManager::joinRoom(Quotient::Connection *account, const QString &roomAli }, Qt::SingleShotConnection); } else { - Q_EMIT warning(i18n("Failed to join room"), finish->errorString()); + Q_EMIT showMessage(MessageType::Warning, i18n("Failed to join room
%1", finish->errorString())); } }, Qt::SingleShotConnection); @@ -386,12 +386,12 @@ void RoomManager::knockRoom(NeoChatConnection *account, const QString &roomAlias account, &NeoChatConnection::newRoom, this, - [](Quotient::Room *room) { - Q_EMIT Controller::instance().showMessage(Controller::Info, i18n("You requested to join '%1'", room -> name())); + [this](Quotient::Room *room) { + Q_EMIT showMessage(MessageType::Information, i18n("You requested to join '%1'", room->name())); }, Qt::SingleShotConnection); } else { - Q_EMIT warning(i18n("Failed to request joining room"), job->errorString()); + Q_EMIT showMessage(MessageType::Warning, i18n("Failed to request joining room
%1", job->errorString())); } }, Qt::SingleShotConnection); @@ -466,11 +466,20 @@ void RoomManager::setCurrentSpace(const QString &spaceId, bool setRoom) void RoomManager::setCurrentRoom(const QString &roomId) { + if (m_currentRoom != nullptr) { + m_currentRoom->disconnect(this); + } + if (roomId.isEmpty()) { m_currentRoom = nullptr; } else { m_currentRoom = dynamic_cast(m_connection->room(roomId)); } + + if (m_currentRoom != nullptr) { + connect(m_currentRoom, &NeoChatRoom::showMessage, this, &RoomManager::showMessage); + } + Q_EMIT currentRoomChanged(); if (m_connection) { m_lastRoomConfig.writeEntry(m_connection->userId(), roomId); diff --git a/src/roommanager.h b/src/roommanager.h index 9c521b3e0..465e7d04d 100644 --- a/src/roommanager.h +++ b/src/roommanager.h @@ -13,6 +13,7 @@ #include "chatdocumenthandler.h" #include "enums/messagecomponenttype.h" +#include "enums/messagetype.h" #include "models/mediamessagefiltermodel.h" #include "models/messagefiltermodel.h" #include "models/roomlistmodel.h" @@ -328,9 +329,9 @@ Q_SIGNALS: void askDirectChatConfirmation(const Quotient::User *user); /** - * @brief Displays warning to the user. + * @brief Request a message be shown to the user of the given type. */ - void warning(const QString &title, const QString &message); + void showMessage(MessageType::Type messageType, const QString &message); void chatDocumentHandlerChanged();