diff --git a/src/components/chat/ChatModel.cpp b/src/components/chat/ChatModel.cpp index b7dc7075d..68c41f894 100644 --- a/src/components/chat/ChatModel.cpp +++ b/src/components/chat/ChatModel.cpp @@ -277,6 +277,8 @@ bool ChatModel::removeRows (int row, int count, const QModelIndex &parent) { if (mEntries.count() == 0) emit allEntriesRemoved(); + else if (limit == mEntries.count()) + emit lastEntryRemoved(); return true; } diff --git a/src/components/chat/ChatModel.hpp b/src/components/chat/ChatModel.hpp index 0d36dcc6d..fef931f2f 100644 --- a/src/components/chat/ChatModel.hpp +++ b/src/components/chat/ChatModel.hpp @@ -109,6 +109,7 @@ signals: bool isRemoteComposingChanged (bool status); void allEntriesRemoved (); + void lastEntryRemoved (); void messageSent (const std::shared_ptr &message); void messageReceived (const std::shared_ptr &message); diff --git a/src/components/sip-addresses/SipAddressesModel.cpp b/src/components/sip-addresses/SipAddressesModel.cpp index cf17d06a1..338becf3e 100644 --- a/src/components/sip-addresses/SipAddressesModel.cpp +++ b/src/components/sip-addresses/SipAddressesModel.cpp @@ -238,14 +238,16 @@ void SipAddressesModel::handleChatModelCreated (const shared_ptr &cha ChatModel *ptr = chatModel.get(); QObject::connect(ptr, &ChatModel::allEntriesRemoved, this, [this, ptr] { - handleAllEntriesRemoved(ptr->getSipAddress()); + handleAllEntriesRemoved(ptr); + }); + QObject::connect(ptr, &ChatModel::lastEntryRemoved, this, [this, ptr] { + handleLastEntryRemoved(ptr); + }); + QObject::connect(ptr, &ChatModel::messagesCountReset, this, [this, ptr] { + handleMessagesCountReset(ptr); }); QObject::connect(ptr, &ChatModel::messageSent, this, &SipAddressesModel::handleMessageSent); - - QObject::connect(ptr, &ChatModel::messagesCountReset, this, [this, ptr] { - handleMessagesCountReset(ptr->getSipAddress()); - }); } void SipAddressesModel::handleContactAdded (ContactModel *contact) { @@ -331,12 +333,10 @@ void SipAddressesModel::handlePresenceReceived ( updateObservers(sipAddress, status); } -void SipAddressesModel::handleAllEntriesRemoved (const QString &sipAddress) { - auto it = mSipAddresses.find(sipAddress); - if (it == mSipAddresses.end()) { - qWarning() << QStringLiteral("Unable to find sip address: `%1`.").arg(sipAddress); +void SipAddressesModel::handleAllEntriesRemoved (ChatModel *chatModel) { + auto it = mSipAddresses.find(chatModel->getSipAddress()); + if (it == mSipAddresses.end()) return; - } int row = mRefs.indexOf(&(*it)); Q_ASSERT(row != -1); @@ -347,19 +347,31 @@ void SipAddressesModel::handleAllEntriesRemoved (const QString &sipAddress) { return; } - // Signal changes. it->remove("timestamp"); emit dataChanged(index(row, 0), index(row, 0)); } -void SipAddressesModel::handleMessageSent (const shared_ptr &message) { - addOrUpdateSipAddress( - Utils::coreStringToAppString(message->getToAddress()->asStringUriOnly()), - message - ); +void SipAddressesModel::handleLastEntryRemoved (ChatModel *chatModel) { + auto it = mSipAddresses.find(chatModel->getSipAddress()); + if (it == mSipAddresses.end()) + return; + + int row = mRefs.indexOf(&(*it)); + Q_ASSERT(row != -1); + + Q_ASSERT(chatModel->rowCount() > 0); + const QVariantMap map = chatModel->data( + chatModel->index(chatModel->rowCount() - 1, 0), + ChatModel::ChatEntry + ).toMap(); + + // Update the timestamp with the new last chat message timestamp. + (*it)["timestamp"] = map["timestamp"]; + emit dataChanged(index(row, 0), index(row, 0)); } -void SipAddressesModel::handleMessagesCountReset (const QString &sipAddress) { +void SipAddressesModel::handleMessagesCountReset (ChatModel *chatModel) { + const QString &sipAddress = chatModel->getSipAddress(); auto it = mSipAddresses.find(sipAddress); if (it != mSipAddresses.end()) { (*it)["unreadMessagesCount"] = 0; @@ -372,6 +384,13 @@ void SipAddressesModel::handleMessagesCountReset (const QString &sipAddress) { updateObservers(sipAddress, 0); } +void SipAddressesModel::handleMessageSent (const shared_ptr &message) { + addOrUpdateSipAddress( + Utils::coreStringToAppString(message->getToAddress()->asStringUriOnly()), + message + ); +} + void SipAddressesModel::handlerIsComposingChanged (const shared_ptr &chatRoom) { auto it = mSipAddresses.find(Utils::coreStringToAppString(chatRoom->getPeerAddress()->asStringUriOnly())); if (it != mSipAddresses.end()) { diff --git a/src/components/sip-addresses/SipAddressesModel.hpp b/src/components/sip-addresses/SipAddressesModel.hpp index a12cf0ea0..12853d7e5 100644 --- a/src/components/sip-addresses/SipAddressesModel.hpp +++ b/src/components/sip-addresses/SipAddressesModel.hpp @@ -84,9 +84,11 @@ private: void handleCallStateChanged (const std::shared_ptr &call, linphone::CallState state); void handlePresenceReceived (const QString &sipAddress, const std::shared_ptr &presenceModel); - void handleAllEntriesRemoved (const QString &sipAddress); + void handleAllEntriesRemoved (ChatModel *chatModel); + void handleLastEntryRemoved (ChatModel *chatModel); + void handleMessagesCountReset (ChatModel *chatModel); + void handleMessageSent (const std::shared_ptr &message); - void handleMessagesCountReset (const QString &sipAddress); void handlerIsComposingChanged (const std::shared_ptr &chatRoom);