From 55ae5858b58412f23a30dc078848e61eea709aa2 Mon Sep 17 00:00:00 2001 From: blue Date: Sun, 19 Apr 2020 16:13:15 +0300 Subject: [PATCH] some workaround about disconnection segfault --- core/account.cpp | 64 ++++++++++++++++++++++++++++++--------------- core/account.h | 1 + core/rosteritem.cpp | 10 +++++++ core/rosteritem.h | 1 + 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 9ef85ee..90c5b42 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -219,6 +219,8 @@ void Core::Account::onClientConnected() void Core::Account::onClientDisconnected() { + cancelHistoryRequests(); + pendingVCardRequests.clear(); clearConferences(); if (state != Shared::ConnectionState::disconnected) { if (reconnectTimes > 0) { @@ -854,18 +856,20 @@ void Core::Account::onMamMessageReceived(const QString& queryId, const QXmppMess { if (msg.id().size() > 0 && (msg.body().size() > 0 || msg.outOfBandUrl().size() > 0)) { std::map::const_iterator itr = achiveQueries.find(queryId); - QString jid = itr->second; - RosterItem* item = getRosterItem(jid); - - Shared::Message sMsg(static_cast(msg.type())); - initializeMessage(sMsg, msg, false, true, true); - sMsg.setState(Shared::Message::State::sent); - - QString oId = msg.replaceId(); - if (oId.size() > 0) { - item->correctMessageInArchive(oId, sMsg); - } else { - item->addMessageToArchive(sMsg); + if (itr != achiveQueries.end()) { + QString jid = itr->second; + RosterItem* item = getRosterItem(jid); + + Shared::Message sMsg(static_cast(msg.type())); + initializeMessage(sMsg, msg, false, true, true); + sMsg.setState(Shared::Message::State::sent); + + QString oId = msg.replaceId(); + if (oId.size() > 0) { + item->correctMessageInArchive(oId, sMsg); + } else { + item->addMessageToArchive(sMsg); + } } } @@ -896,10 +900,15 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString& if (contact == 0) { qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the contact with such id wasn't found, skipping"; - emit responseArchive(contact->jid, std::list()); + emit responseArchive(jid, std::list()); return; } + if (state != Shared::ConnectionState::connected) { + qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the account is not online, skipping"; + emit responseArchive(contact->jid, std::list()); + } + if (contact->getArchiveState() == RosterItem::empty && before.size() == 0) { QXmppMessage msg(getFullJid(), jid, "", ""); QString last = Shared::generateUUID(); @@ -952,14 +961,16 @@ void Core::Account::onContactNeedHistory(const QString& before, const QString& a void Core::Account::onMamResultsReceived(const QString& queryId, const QXmppResultSetReply& resultSetReply, bool complete) { std::map::const_iterator itr = achiveQueries.find(queryId); - QString jid = itr->second; - achiveQueries.erase(itr); - - RosterItem* ri = getRosterItem(jid); - - if (ri != 0) { - qDebug() << "Flushing messages for" << jid; - ri->flushMessagesToArchive(complete, resultSetReply.first(), resultSetReply.last()); + if (itr != achiveQueries.end()) { + QString jid = itr->second; + achiveQueries.erase(itr); + + RosterItem* ri = getRosterItem(jid); + + if (ri != 0) { + qDebug() << "Flushing messages for" << jid; + ri->flushMessagesToArchive(complete, resultSetReply.first(), resultSetReply.last()); + } } } @@ -1743,3 +1754,14 @@ void Core::Account::onReceiptReceived(const QString& jid, const QString& id) } } +void Core::Account::cancelHistoryRequests() +{ + for (const std::pair& pair : conferences) { + pair.second->clearArchiveRequests(); + } + for (const std::pair& pair : contacts) { + pair.second->clearArchiveRequests(); + } + achiveQueries.clear(); +} + diff --git a/core/account.h b/core/account.h index 386835c..b706916 100644 --- a/core/account.h +++ b/core/account.h @@ -236,6 +236,7 @@ private: void logMessage(const QXmppMessage& msg, const QString& reason = "Message wasn't handled: "); void storeConferences(); void clearConferences(); + void cancelHistoryRequests(); void sendMessageWithLocalUploadedFile(Shared::Message msg, const QString& url); RosterItem* getRosterItem(const QString& jid); }; diff --git a/core/rosteritem.cpp b/core/rosteritem.cpp index c25b339..8260eec 100644 --- a/core/rosteritem.cpp +++ b/core/rosteritem.cpp @@ -518,3 +518,13 @@ Shared::VCard Core::RosterItem::handleResponseVCard(const QXmppVCardIq& card, co return vCard; } +void Core::RosterItem::clearArchiveRequests() +{ + syncronizing = false; + requestedCount = 0; + requestedBefore = ""; + hisoryCache.clear(); + responseCache.clear(); + appendCache.clear(); + requestCache.clear(); +} diff --git a/core/rosteritem.h b/core/rosteritem.h index 47470b1..cecd2e4 100644 --- a/core/rosteritem.h +++ b/core/rosteritem.h @@ -76,6 +76,7 @@ public: virtual void handlePresence(const QXmppPresence& pres) = 0; bool changeMessage(const QString& id, const QMap& data); + void clearArchiveRequests(); signals: void nameChanged(const QString& name);