diff --git a/src/components/chat/ChatModel.cpp b/src/components/chat/ChatModel.cpp index d50b4e13b..e55de088b 100644 --- a/src/components/chat/ChatModel.cpp +++ b/src/components/chat/ChatModel.cpp @@ -140,9 +140,9 @@ public: private: QList::iterator findMessageEntry (const shared_ptr &message) { - return find_if(mChatModel->mEntries.begin(), mChatModel->mEntries.end(), [&message](const ChatEntryData &pair) { - return pair.second == message; - }); + return find_if(mChatModel->mEntries.begin(), mChatModel->mEntries.end(), [&message](const ChatEntryData &entry) { + return entry.second == message; + }); } void signalDataChanged (const QList::iterator &it) { @@ -210,9 +210,9 @@ private: // ----------------------------------------------------------------------------- ChatModel::ChatModel (const QString &sipAddress) { - CoreManager *core = CoreManager::getInstance(); + CoreManager *coreManager = CoreManager::getInstance(); - mCoreHandlers = core->getHandlers(); + mCoreHandlers = coreManager->getHandlers(); mMessageHandlers = make_shared(this); setSipAddress(sipAddress); @@ -298,6 +298,7 @@ void ChatModel::setSipAddress (const QString &sipAddress) { handleIsComposingChanged(mChatRoom); // Get messages. + mEntries.clear(); for (auto &message : mChatRoom->getHistory(0)) { QVariantMap map; @@ -446,11 +447,11 @@ void ChatModel::downloadFile (int id) { bool soFarSoGood; const QString safeFilePath = Utils::getSafeFilePath( - QStringLiteral("%1%2") + QStringLiteral("%1%2") .arg(CoreManager::getInstance()->getSettingsModel()->getDownloadFolder()) .arg(entry.first["fileName"].toString()), - &soFarSoGood - ); + &soFarSoGood + ); if (!soFarSoGood) { qWarning() << QStringLiteral("Unable to create safe file path for: %1.").arg(id); @@ -561,34 +562,34 @@ void ChatModel::fillCallEndEntry (QVariantMap &dest, const shared_ptr message = static_pointer_cast(pair.second); + shared_ptr message = static_pointer_cast(entry.second); removeFileMessageThumbnail(message); mChatRoom->deleteMessage(message); break; } case ChatModel::CallEntry: { - if (pair.first["status"].toInt() == linphone::CallStatusSuccess) { + if (entry.first["status"].toInt() == linphone::CallStatusSuccess) { // WARNING: Unable to remove symmetric call here. (start/end) // We are between `beginRemoveRows` and `endRemoveRows`. // A solution is to schedule a `removeEntry` call in the Qt main loop. - shared_ptr linphonePtr = pair.second; + shared_ptr linphonePtr = entry.second; QTimer::singleShot(0, this, [this, linphonePtr]() { - auto it = find_if(mEntries.begin(), mEntries.end(), [linphonePtr](const ChatEntryData &pair) { - return pair.second == linphonePtr; - }); - - if (it != mEntries.end()) - removeEntry(int(distance(mEntries.begin(), it))); + auto it = find_if(mEntries.begin(), mEntries.end(), [linphonePtr](const ChatEntryData &entry) { + return entry.second == linphonePtr; }); + + if (it != mEntries.end()) + removeEntry(int(distance(mEntries.begin(), it))); + }); } - CoreManager::getInstance()->getCore()->removeCallLog(static_pointer_cast(pair.second)); + CoreManager::getInstance()->getCore()->removeCallLog(static_pointer_cast(entry.second)); break; } @@ -616,17 +617,17 @@ void ChatModel::insertCall (const shared_ptr &callLog) { } auto insertEntry = [this]( - const ChatEntryData &pair, + const ChatEntryData &entry, const QList::iterator *start = NULL ) { - auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), pair, [](const ChatEntryData &a, const ChatEntryData &b) { + auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), entry, [](const ChatEntryData &a, const ChatEntryData &b) { return a.first["timestamp"] < b.first["timestamp"]; }); int row = int(distance(mEntries.begin(), it)); beginInsertRows(QModelIndex(), row, row); - it = mEntries.insert(it, pair); + it = mEntries.insert(it, entry); endInsertRows(); return it; diff --git a/src/components/chat/ChatModel.hpp b/src/components/chat/ChatModel.hpp index fef931f2f..dd1b1a8d9 100644 --- a/src/components/chat/ChatModel.hpp +++ b/src/components/chat/ChatModel.hpp @@ -127,7 +127,7 @@ private: void fillCallStartEntry (QVariantMap &dest, const std::shared_ptr &callLog); void fillCallEndEntry (QVariantMap &dest, const std::shared_ptr &callLog); - void removeEntry (ChatEntryData &pair); + void removeEntry (ChatEntryData &entry); void insertCall (const std::shared_ptr &callLog); void insertMessageAtEnd (const std::shared_ptr &message); diff --git a/src/components/sip-addresses/SipAddressesModel.cpp b/src/components/sip-addresses/SipAddressesModel.cpp index 338becf3e..7a3d057ac 100644 --- a/src/components/sip-addresses/SipAddressesModel.cpp +++ b/src/components/sip-addresses/SipAddressesModel.cpp @@ -122,12 +122,11 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip } mObservers.insert(cleanedSipAddress, model); - QObject::connect( - model, &SipAddressObserver::destroyed, this, [this, model, cleanedSipAddress]() { - // Do not use `model` methods here. `model` is partially destroyed here! - if (mObservers.remove(cleanedSipAddress, model) == 0) - qWarning() << QStringLiteral("Unable to remove sip address `%1` from observers.").arg(cleanedSipAddress); - }); + QObject::connect(model, &SipAddressObserver::destroyed, this, [this, model, cleanedSipAddress]() { + // Do not use `model` methods here. `model` is partially destroyed here! + if (mObservers.remove(cleanedSipAddress, model) == 0) + qWarning() << QStringLiteral("Unable to remove sip address `%1` from observers.").arg(cleanedSipAddress); + }); return model; } @@ -136,8 +135,8 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip QString SipAddressesModel::getTransportFromSipAddress (const QString &sipAddress) const { const shared_ptr address = linphone::Factory::get()->createAddress( - Utils::appStringToCoreString(sipAddress) - ); + Utils::appStringToCoreString(sipAddress) + ); if (!address) return QString(""); @@ -490,14 +489,20 @@ void SipAddressesModel::removeContactOfSipAddress (const QString &sipAddress) { removeRow(row); } -void SipAddressesModel::initSipAddresses () { - shared_ptr core = CoreManager::getInstance()->getCore(); +// ----------------------------------------------------------------------------- - // Get sip addresses from chatrooms. - for (const auto &chatRoom : core->getChatRooms()) { +void SipAddressesModel::initSipAddresses () { + initSipAddressesFromChat(); + initSipAddressesFromCalls(); + initRefs(); + initSipAddressesFromContacts(); +} + +void SipAddressesModel::initSipAddressesFromChat () { + for (const auto &chatRoom : CoreManager::getInstance()->getCore()->getChatRooms()) { list> history = chatRoom->getHistory(0); - if (history.size() == 0) + if (history.empty()) continue; QString sipAddress = Utils::coreStringToAppString(chatRoom->getPeerAddress()->asStringUriOnly()); @@ -507,12 +512,14 @@ void SipAddressesModel::initSipAddresses () { map["timestamp"] = QDateTime::fromMSecsSinceEpoch(history.back()->getTime() * 1000); map["unreadMessagesCount"] = chatRoom->getUnreadMessagesCount(); + qInfo() << QStringLiteral("Add sip address: `%1`.").arg(map["sipAddress"].toString()); mSipAddresses[sipAddress] = map; } +} - // Get sip addresses from calls. +void SipAddressesModel::initSipAddressesFromCalls () { QSet addressDone; - for (const auto &callLog : core->getCallLogs()) { + for (const auto &callLog : CoreManager::getInstance()->getCore()->getCallLogs()) { const QString sipAddress = Utils::coreStringToAppString(callLog->getRemoteAddress()->asStringUriOnly()); if (addressDone.contains(sipAddress)) @@ -535,17 +542,18 @@ void SipAddressesModel::initSipAddresses () { if (it == mSipAddresses.end() || map["timestamp"] > (*it)["timestamp"]) mSipAddresses[sipAddress] = map; } +} - for (const auto &map : mSipAddresses) { - qInfo() << QStringLiteral("Add sip address: `%1`.").arg(map["sipAddress"].toString()); - mRefs << ↦ - } - - // Get sip addresses from contacts. +void SipAddressesModel::initSipAddressesFromContacts () { for (auto &contact : CoreManager::getInstance()->getContactsListModel()->mList) handleContactAdded(contact); } +void SipAddressesModel::initRefs () { + for (const auto &map : mSipAddresses) + mRefs << ↦ +} + // ----------------------------------------------------------------------------- void SipAddressesModel::updateObservers (const QString &sipAddress, ContactModel *contact) { diff --git a/src/components/sip-addresses/SipAddressesModel.hpp b/src/components/sip-addresses/SipAddressesModel.hpp index 12853d7e5..6cec69cba 100644 --- a/src/components/sip-addresses/SipAddressesModel.hpp +++ b/src/components/sip-addresses/SipAddressesModel.hpp @@ -109,6 +109,12 @@ private: void initSipAddresses (); + void initSipAddressesFromChat (); + void initSipAddressesFromCalls (); + void initSipAddressesFromContacts (); + + void initRefs (); + void updateObservers (const QString &sipAddress, ContactModel *contact); void updateObservers (const QString &sipAddress, const Presence::PresenceStatus &presenceStatus); void updateObservers (const QString &sipAddress, int messagesCount);