From 217e2f45b7059569574fc01b2999674fc7634b79 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 21 Feb 2017 14:20:16 +0100 Subject: [PATCH] feat(src/components/sip-addresses/SipAddressesModel): deal with calls --- .../sip-addresses/SipAddressesModel.cpp | 78 +++++++++++-------- .../sip-addresses/SipAddressesModel.hpp | 21 ++--- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp index 94205c0a8..a20eae645 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp @@ -36,6 +36,7 @@ using namespace std; SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) { initSipAddresses(); + ContactsListModel *contacts = CoreManager::getInstance()->getContactsListModel(); QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded); @@ -46,7 +47,7 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare &(*m_core_handlers), &CoreHandlers::messageReceived, this, [this](const shared_ptr &message) { const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asStringUriOnly()); - addOrUpdateSipAddress(sip_address, nullptr, message); + addOrUpdateSipAddress(sip_address, message); } ); @@ -74,6 +75,20 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare removeContactOfSipAddress(sip_address); } ); + + QObject::connect( + &(*CoreManager::getInstance()->getHandlers()), &CoreHandlers::callStateChanged, + this, [this](const std::shared_ptr &call, linphone::CallState state) { + // Ignore aborted calls. + if (call->getCallLog()->getStatus() == linphone::CallStatus::CallStatusAborted) + return; + + if (state == linphone::CallStateEnd || state == linphone::CallStateError) + addOrUpdateSipAddress( + ::Utils::linphoneStringToQString(call->getRemoteAddress()->asStringUriOnly()), call + ); + } + ); } // ----------------------------------------------------------------------------- @@ -126,16 +141,14 @@ void SipAddressesModel::connectToChatModel (ChatModel *chat_model) { } ); - for (auto &signal : { &ChatModel::messageSent, &ChatModel::messageReceived }) { - QObject::connect( - chat_model, signal, - this, [this](const shared_ptr &message) { - addOrUpdateSipAddress( - ::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), nullptr, message - ); - } - ); - } + QObject::connect( + chat_model, &ChatModel::messageSent, + this, [this](const shared_ptr &message) { + addOrUpdateSipAddress( + ::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), message + ); + } + ); QObject::connect( chat_model, &ChatModel::messagesCountReset, this, [this, chat_model]() { @@ -230,30 +243,31 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) { removeContactOfSipAddress(sip_address.toString()); } -void SipAddressesModel::addOrUpdateSipAddress ( - QVariantMap &map, - ContactModel *contact, - const shared_ptr &message -) { - if (contact) { - map["contact"] = QVariant::fromValue(contact); - updateObservers(map["sipAddress"].toString(), contact); - } +// ----------------------------------------------------------------------------- - if (message) { - map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); - map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount(); - } +void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact) { + map["contact"] = QVariant::fromValue(contact); + updateObservers(map["sipAddress"].toString(), contact); } -void SipAddressesModel::addOrUpdateSipAddress ( - const QString &sip_address, - ContactModel *contact, - const shared_ptr &message -) { +void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr &call) { + const shared_ptr call_log = call->getCallLog(); + + map["timestamp"] = call_log->getStatus() == linphone::CallStatus::CallStatusSuccess + ? QDateTime::fromMSecsSinceEpoch((call_log->getStartDate() + call_log->getDuration()) * 1000) + : QDateTime::fromMSecsSinceEpoch(call_log->getStartDate() * 1000); +} + +void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr &message) { + map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); + map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount(); +} + +template +void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, T data) { auto it = m_sip_addresses.find(sip_address); if (it != m_sip_addresses.end()) { - addOrUpdateSipAddress(*it, contact, message); + addOrUpdateSipAddress(*it, data); int row = m_refs.indexOf(&(*it)); Q_ASSERT(row != -1); @@ -264,7 +278,7 @@ void SipAddressesModel::addOrUpdateSipAddress ( QVariantMap map; map["sipAddress"] = sip_address; - addOrUpdateSipAddress(map, contact, message); + addOrUpdateSipAddress(map, data); int row = m_refs.count(); @@ -278,6 +292,8 @@ void SipAddressesModel::addOrUpdateSipAddress ( endInsertRows(); } +// ----------------------------------------------------------------------------- + void SipAddressesModel::removeContactOfSipAddress (const QString &sip_address) { auto it = m_sip_addresses.find(sip_address); if (it == m_sip_addresses.end()) { diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp index 06888caa7..d03ac8287 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp @@ -59,17 +59,18 @@ private: void handleContactAdded (ContactModel *contact); void handleContactRemoved (const ContactModel *contact); - void addOrUpdateSipAddress ( - QVariantMap &map, - ContactModel *contact, - const std::shared_ptr &message - ); + // --------------------------------------------------------------------------- - void addOrUpdateSipAddress ( - const QString &sip_address, - ContactModel *contact = nullptr, - const std::shared_ptr &message = std::shared_ptr() - ); + // A sip address exists in this list if a contact is linked to it, or a call, or a message. + + void addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact); + void addOrUpdateSipAddress (QVariantMap &map, const std::shared_ptr &call); + void addOrUpdateSipAddress (QVariantMap &map, const std::shared_ptr &message); + + template + void addOrUpdateSipAddress (const QString &sip_address, T data); + + // --------------------------------------------------------------------------- void removeContactOfSipAddress (const QString &sip_address);