From e6a11b2ad8c4ed84b14a295fdca102b8ff41c204 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Thu, 28 Mar 2024 22:28:04 +0100 Subject: [PATCH] Make various models more robust against deleted rooms --- src/actionshandler.cpp | 1 + src/actionshandler.h | 2 +- src/models/completionmodel.cpp | 4 +++- src/models/completionmodel.h | 2 +- src/models/messagecontentmodel.h | 2 +- src/models/reactionmodel.cpp | 2 +- src/models/reactionmodel.h | 4 ++-- src/models/searchmodel.h | 2 +- src/models/statekeysmodel.cpp | 9 ++++++--- src/models/statekeysmodel.h | 2 +- src/models/stickermodel.cpp | 8 +++++++- src/models/stickermodel.h | 2 +- 12 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/actionshandler.cpp b/src/actionshandler.cpp index 61ae4d313..8fd3cf535 100644 --- a/src/actionshandler.cpp +++ b/src/actionshandler.cpp @@ -80,6 +80,7 @@ QString ActionsHandler::handleMentions(QString handledText, QList *ment void ActionsHandler::handleMessage(const QString &text, QString handledText, ChatBarCache *chatBarCache) { + Q_ASSERT(m_room); if (NeoChatConfig::allowQuickEdit()) { QRegularExpression sed(QStringLiteral("^s/([^/]*)/([^/]*)(/g)?$")); auto match = sed.match(text); diff --git a/src/actionshandler.h b/src/actionshandler.h index f3a6786bc..451abb87f 100644 --- a/src/actionshandler.h +++ b/src/actionshandler.h @@ -58,7 +58,7 @@ public Q_SLOTS: void handleMessageEvent(ChatBarCache *chatBarCache); private: - NeoChatRoom *m_room = nullptr; + QPointer m_room; void checkEffects(const QString &text); QString handleMentions(QString handledText, QList *mentions); diff --git a/src/models/completionmodel.cpp b/src/models/completionmodel.cpp index 1f0443ae5..789545ef6 100644 --- a/src/models/completionmodel.cpp +++ b/src/models/completionmodel.cpp @@ -91,7 +91,9 @@ QVariant CompletionModel::data(const QModelIndex &index, int role) const if (mediaId.isEmpty()) { return QVariant(); } - return m_room->connection()->makeMediaUrl(QUrl(QStringLiteral("mxc://%1").arg(mediaId))); + if (m_room) { + return m_room->connection()->makeMediaUrl(QUrl(QStringLiteral("mxc://%1").arg(mediaId))); + } } } if (m_autoCompletionType == Emoji) { diff --git a/src/models/completionmodel.h b/src/models/completionmodel.h index b6f5e18aa..6f52f1c30 100644 --- a/src/models/completionmodel.h +++ b/src/models/completionmodel.h @@ -118,7 +118,7 @@ private: QString m_text; QString m_fullText; CompletionProxyModel *m_filterModel; - NeoChatRoom *m_room = nullptr; + QPointer m_room; AutoCompletionType m_autoCompletionType = None; void updateCompletion(); diff --git a/src/models/messagecontentmodel.h b/src/models/messagecontentmodel.h index 0e7da0818..6561d1e4a 100644 --- a/src/models/messagecontentmodel.h +++ b/src/models/messagecontentmodel.h @@ -87,7 +87,7 @@ public: [[nodiscard]] QHash roleNames() const override; private: - NeoChatRoom *m_room = nullptr; + QPointer m_room; const Quotient::RoomEvent *m_event = nullptr; QList m_components; diff --git a/src/models/reactionmodel.cpp b/src/models/reactionmodel.cpp index e3f4887b7..157dc3d03 100644 --- a/src/models/reactionmodel.cpp +++ b/src/models/reactionmodel.cpp @@ -16,7 +16,7 @@ #include -ReactionModel::ReactionModel(const Quotient::RoomMessageEvent *event, const NeoChatRoom *room) +ReactionModel::ReactionModel(const Quotient::RoomMessageEvent *event, NeoChatRoom *room) : QAbstractListModel(nullptr) , m_room(room) , m_event(event) diff --git a/src/models/reactionmodel.h b/src/models/reactionmodel.h index a80bfa467..5783cc5d1 100644 --- a/src/models/reactionmodel.h +++ b/src/models/reactionmodel.h @@ -44,7 +44,7 @@ public: HasLocalUser, /**< Whether the local user is in the list of authors. */ }; - explicit ReactionModel(const Quotient::RoomMessageEvent *event, const NeoChatRoom *room); + explicit ReactionModel(const Quotient::RoomMessageEvent *event, NeoChatRoom *room); /** * @brief Get the given role value at the given index. @@ -68,7 +68,7 @@ public: [[nodiscard]] QHash roleNames() const override; private: - const NeoChatRoom *m_room; + QPointer m_room; const Quotient::RoomMessageEvent *m_event; QList m_reactions; QMap m_shortcodes; diff --git a/src/models/searchmodel.h b/src/models/searchmodel.h index fd4bb7580..9a2d93141 100644 --- a/src/models/searchmodel.h +++ b/src/models/searchmodel.h @@ -121,7 +121,7 @@ private: void setSearching(bool searching); QString m_searchText; - NeoChatRoom *m_room = nullptr; + QPointer m_room; Quotient::Omittable m_result = Quotient::none; Quotient::SearchJob *m_job = nullptr; bool m_searching = false; diff --git a/src/models/statekeysmodel.cpp b/src/models/statekeysmodel.cpp index 65d520d79..fd1b97958 100644 --- a/src/models/statekeysmodel.cpp +++ b/src/models/statekeysmodel.cpp @@ -55,11 +55,14 @@ void StateKeysModel::setRoom(NeoChatRoom *room) m_room = room; Q_EMIT roomChanged(); - loadState(); - connect(room, &NeoChatRoom::changed, this, [this] { + if (room) { loadState(); - }); + + connect(room, &NeoChatRoom::changed, this, [this] { + loadState(); + }); + } } QString StateKeysModel::eventType() const diff --git a/src/models/statekeysmodel.h b/src/models/statekeysmodel.h index a666ab2aa..9de91a433 100644 --- a/src/models/statekeysmodel.h +++ b/src/models/statekeysmodel.h @@ -76,7 +76,7 @@ Q_SIGNALS: void eventTypeChanged(); private: - NeoChatRoom *m_room = nullptr; + QPointer m_room; QString m_eventType; QVector m_stateKeys; void loadState(); diff --git a/src/models/stickermodel.cpp b/src/models/stickermodel.cpp index a9579ff9b..6ea86f471 100644 --- a/src/models/stickermodel.cpp +++ b/src/models/stickermodel.cpp @@ -22,7 +22,9 @@ QVariant StickerModel::data(const QModelIndex &index, int role) const const auto &row = index.row(); const auto &image = m_images[row]; if (role == UrlRole) { - return m_room->connection()->makeMediaUrl(image.url); + if (m_room) { + return m_room->connection()->makeMediaUrl(image.url); + } } if (role == BodyRole) { if (image.body) { @@ -108,6 +110,10 @@ void StickerModel::setRoom(NeoChatRoom *room) void StickerModel::postSticker(int index) { + if (!m_room) { + qWarning() << "No room"; + } + const auto &image = m_images[index]; const auto &body = image.body ? *image.body : image.shortcode; QJsonObject infoJson; diff --git a/src/models/stickermodel.h b/src/models/stickermodel.h index 8cc85c7b6..7492997e4 100644 --- a/src/models/stickermodel.h +++ b/src/models/stickermodel.h @@ -101,6 +101,6 @@ private: ImagePacksModel *m_model = nullptr; int m_index = 0; QList m_images; - NeoChatRoom *m_room; + QPointer m_room; void reloadImages(); };