From 0375ef99a574d75334318734bcda0fe607033c2a Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Fri, 20 Feb 2026 13:18:43 +0100 Subject: [PATCH] Clean up chatbarcache --- autotests/actionstest.cpp | 2 +- autotests/chatbarcachetest.cpp | 35 ------------------- src/libneochat/CMakeLists.txt | 7 ++++ src/libneochat/chatbarcache.cpp | 59 ++++++++++++--------------------- src/libneochat/chatbarcache.h | 5 ++- 5 files changed, 33 insertions(+), 75 deletions(-) diff --git a/autotests/actionstest.cpp b/autotests/actionstest.cpp index 294c45e31..232cf9b2e 100644 --- a/autotests/actionstest.cpp +++ b/autotests/actionstest.cpp @@ -90,7 +90,7 @@ void ActionsTest::testActions() QFETCH(std::optional, resultText); QFETCH(std::optional, type); - auto cache = new ChatBarCache(this); + auto cache = new ChatBarCache(room); cache->cache() += Block::CacheItem{.type = MessageComponentType::Text, .content = QTextDocumentFragment::fromMarkdown(command)}; auto result = ActionsModel::handleAction(room, cache); QCOMPARE(resultText, std::get>(result)); diff --git a/autotests/chatbarcachetest.cpp b/autotests/chatbarcachetest.cpp index a31cd931a..7c8fe6114 100644 --- a/autotests/chatbarcachetest.cpp +++ b/autotests/chatbarcachetest.cpp @@ -37,8 +37,6 @@ private Q_SLOTS: void initTestCase(); void empty(); - void noRoom(); - void badParent(); void reply(); void replyMissingUser(); void edit(); @@ -88,39 +86,6 @@ void ChatBarCacheTest::empty() QCOMPARE(chatBarCache->attachmentPath(), QString()); } -void ChatBarCacheTest::noRoom() -{ - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."); - QScopedPointer chatBarCache(new ChatBarCache()); - chatBarCache->setReplyId(eventId); - - // These should return empty even though a reply ID has been set because the - // ChatBarCache has no parent. - - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."); - QCOMPARE(chatBarCache->relationAuthor(), Quotient::RoomMember()); - - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."); - QCOMPARE(chatBarCache->relationMessage(), QString()); -} - -void ChatBarCacheTest::badParent() -{ - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."); - QScopedPointer badParent(new QObject()); - QScopedPointer chatBarCache(new ChatBarCache(badParent.get())); - chatBarCache->setReplyId(eventId); - - // These should return empty even though a reply ID has been set because the - // ChatBarCache has no parent. - - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."); - QCOMPARE(chatBarCache->relationAuthor(), Quotient::RoomMember()); - - QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."); - QCOMPARE(chatBarCache->relationMessage(), QString()); -} - void ChatBarCacheTest::reply() { QScopedPointer chatBarCache(new ChatBarCache(room)); diff --git a/src/libneochat/CMakeLists.txt b/src/libneochat/CMakeLists.txt index 228edc72d..4579ed095 100644 --- a/src/libneochat/CMakeLists.txt +++ b/src/libneochat/CMakeLists.txt @@ -60,6 +60,13 @@ target_sources(LibNeoChat PRIVATE models/widgetmodel.h ) +ecm_qt_declare_logging_category(LibNeoChat + HEADER "chatbarlogging.h" + IDENTIFIER "ChatBar" + CATEGORY_NAME "org.kde.neochat.chatbar" + DEFAULT_SEVERITY Info +) + if (TARGET KF6::KIOWidgets) target_compile_definitions(LibNeoChat PUBLIC -DHAVE_KIO) endif() diff --git a/src/libneochat/chatbarcache.cpp b/src/libneochat/chatbarcache.cpp index 48c9d5922..f06cee7d8 100644 --- a/src/libneochat/chatbarcache.cpp +++ b/src/libneochat/chatbarcache.cpp @@ -14,20 +14,15 @@ #include "neochatroom.h" #include "texthandler.h" +#include "chatbarlogging.h" + using namespace Qt::StringLiterals; -ChatBarCache::ChatBarCache(QObject *parent) - : QObject(parent) +ChatBarCache::ChatBarCache(NeoChatRoom *room) + : QObject(room) + , m_room(room) { - if (parent == nullptr) { - qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; - return; - } - auto room = dynamic_cast(parent); - if (room == nullptr) { - qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."; - return; - } + Q_ASSERT(room); connect(room, &NeoChatRoom::memberLeft, this, &ChatBarCache::relationAuthorIsPresentChanged); connect(room, &NeoChatRoom::memberJoined, this, &ChatBarCache::relationAuthorIsPresentChanged); connect(this, &ChatBarCache::relationIdChanged, this, &ChatBarCache::relationAuthorIsPresentChanged); @@ -110,24 +105,19 @@ void ChatBarCache::setEditId(const QString &editId) Quotient::RoomMember ChatBarCache::relationAuthor() const { - if (parent() == nullptr) { - qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; - return {}; - } - auto room = dynamic_cast(parent()); - if (room == nullptr) { - qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."; + if (!m_room) { + qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted"; return {}; } if (m_relationId.isEmpty()) { - return room->member(QString()); + return m_room->member({}); } - const auto [event, _] = room->getEvent(m_relationId); + const auto [event, _] = m_room->getEvent(m_relationId); if (event != nullptr) { - return room->member(event->senderId()); + return m_room->member(event->senderId()); } - qWarning() << "Failed to find relation" << m_relationId << "in timeline?"; - return room->member(QString()); + qCWarning(ChatBar) << "Failed to find relation" << m_relationId << "in timeline?"; + return m_room->member(QString()); } bool ChatBarCache::relationAuthorIsPresent() const @@ -137,20 +127,14 @@ bool ChatBarCache::relationAuthorIsPresent() const QString ChatBarCache::relationMessage() const { - if (parent() == nullptr) { - qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; + if (!m_room) { + qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted"; return {}; } if (m_relationId.isEmpty()) { return {}; } - auto room = dynamic_cast(parent()); - if (room == nullptr) { - qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."; - return {}; - } - - if (auto [event, _] = room->getEvent(m_relationId); event != nullptr) { + if (auto [event, _] = m_room->getEvent(m_relationId); event != nullptr) { return EventHandler::markdownBody(event); } return {}; @@ -216,9 +200,8 @@ void ChatBarCache::setSavedText(const QString &savedText) void ChatBarCache::postMessage() { - auto room = dynamic_cast(parent()); - if (room == nullptr) { - qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."; + if (!m_room) { + qCWarning(ChatBar) << "ChatBarCache:" << __FUNCTION__ << "called after room was deleted"; return; } @@ -234,12 +217,12 @@ void ChatBarCache::postMessage() } if (!attachmentPath().isEmpty()) { - room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo); + m_room->uploadFile(QUrl(attachmentPath()), sendText(), relatesTo); clearCache(); return; } - const auto result = ActionsModel::handleAction(room, this); + const auto result = ActionsModel::handleAction(m_room, this); if (!result.second.has_value()) { return; } @@ -254,7 +237,7 @@ void ChatBarCache::postMessage() auto content = std::make_unique(sendText, u"text/html"_s); - room->post(m_cache.toString(), + m_room->post(m_cache.toString(), *std::get>(result), std::move(content), relatesTo); diff --git a/src/libneochat/chatbarcache.h b/src/libneochat/chatbarcache.h index cda6793a5..29746c4d0 100644 --- a/src/libneochat/chatbarcache.h +++ b/src/libneochat/chatbarcache.h @@ -15,6 +15,8 @@ namespace Quotient class RoomMember; } +class NeoChatRoom; + /** * @class ChatBarCache * @@ -128,7 +130,7 @@ public: }; Q_ENUM(RelationType) - explicit ChatBarCache(QObject *parent = nullptr); + explicit ChatBarCache(NeoChatRoom *room); Block::Cache &cache(); QString sendText() const; @@ -186,6 +188,7 @@ Q_SIGNALS: private: Block::Cache m_cache; + QPointer m_room; QString m_relationId = QString(); RelationType m_relationType = RelationType::None;