From 1b6788f00514d76d518d3dcd467494ff1e51bd25 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 5 May 2017 14:54:11 +0200 Subject: [PATCH] fix(src/components/sip-addresses/SipAddressesModel): try to map to other contact when a contact of one sip address is removed --- .../components/contacts/ContactsListModel.cpp | 10 +++++ .../components/contacts/ContactsListModel.hpp | 2 + .../src/components/core/CoreManager.cpp | 4 +- .../sip-addresses/SipAddressesModel.cpp | 37 +++++++++++-------- .../sip-addresses/SipAddressesModel.hpp | 3 +- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/linphone-desktop/src/components/contacts/ContactsListModel.cpp b/linphone-desktop/src/components/contacts/ContactsListModel.cpp index 714adc8f9..cc6342cb6 100644 --- a/linphone-desktop/src/components/contacts/ContactsListModel.cpp +++ b/linphone-desktop/src/components/contacts/ContactsListModel.cpp @@ -112,6 +112,16 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren // ----------------------------------------------------------------------------- +ContactModel *ContactsListModel::findContactModelFromSipAddress (const QString &sipAddress) const { + auto it = find_if(mList.begin(), mList.end(), [sipAddress](ContactModel *contactModel) { + return contactModel->getVcardModel()->getSipAddresses().contains(sipAddress); + }); + + return it != mList.end() ? *it : nullptr; +} + +// ----------------------------------------------------------------------------- + ContactModel *ContactsListModel::addContact (VcardModel *vcard) { ContactModel *contact = new ContactModel(this, vcard); App::getInstance()->getEngine()->setObjectOwnership(contact, QQmlEngine::CppOwnership); diff --git a/linphone-desktop/src/components/contacts/ContactsListModel.hpp b/linphone-desktop/src/components/contacts/ContactsListModel.hpp index ca30d9af4..aa492d969 100644 --- a/linphone-desktop/src/components/contacts/ContactsListModel.hpp +++ b/linphone-desktop/src/components/contacts/ContactsListModel.hpp @@ -47,6 +47,8 @@ public: bool removeRow (int row, const QModelIndex &parent = QModelIndex()); bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override; + ContactModel *findContactModelFromSipAddress (const QString &sipAddress) const; + Q_INVOKABLE ContactModel *addContact (VcardModel *vcard); Q_INVOKABLE void removeContact (ContactModel *contact); diff --git a/linphone-desktop/src/components/core/CoreManager.cpp b/linphone-desktop/src/components/core/CoreManager.cpp index 6215d0e43..1a884d701 100644 --- a/linphone-desktop/src/components/core/CoreManager.cpp +++ b/linphone-desktop/src/components/core/CoreManager.cpp @@ -76,7 +76,9 @@ void CoreManager::init (QObject *parent, const QString &configPath) { // ----------------------------------------------------------------------------- VcardModel *CoreManager::createDetachedVcardModel () { - return new VcardModel(linphone::Factory::get()->createVcard()); + VcardModel *vcardModel = new VcardModel(linphone::Factory::get()->createVcard(), false); + qInfo() << QStringLiteral("Create detached vcard:") << vcardModel; + return vcardModel; } void CoreManager::forceRefreshRegisters () { diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp index 1fe5a92d5..038c69e25 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp @@ -47,9 +47,10 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare QObject::connect(contacts, &ContactsListModel::sipAddressAdded, this, &SipAddressesModel::handleSipAddressAdded); QObject::connect(contacts, &ContactsListModel::sipAddressRemoved, this, &SipAddressesModel::handleSipAddressRemoved); - QObject::connect(mCoreHandlers.get(), &CoreHandlers::messageReceived, this, &SipAddressesModel::handleMessageReceived); - QObject::connect(mCoreHandlers.get(), &CoreHandlers::callStateChanged, this, &SipAddressesModel::handleCallStateChanged); - QObject::connect(mCoreHandlers.get(), &CoreHandlers::presenceReceived, this, &SipAddressesModel::handlePresenceReceived); + CoreHandlers *intHandlers = mCoreHandlers.get(); + QObject::connect(intHandlers, &CoreHandlers::messageReceived, this, &SipAddressesModel::handleMessageReceived); + QObject::connect(intHandlers, &CoreHandlers::callStateChanged, this, &SipAddressesModel::handleCallStateChanged); + QObject::connect(intHandlers, &CoreHandlers::presenceReceived, this, &SipAddressesModel::handlePresenceReceived); } // ----------------------------------------------------------------------------- @@ -183,12 +184,10 @@ QString SipAddressesModel::addTransportToSipAddress (const QString &sipAddress, return ::Utils::linphoneStringToQString(address->asString()); } -bool SipAddressesModel::sipAddressIsValid (const QString &sipAddress) const { - shared_ptr address = linphone::Factory::get()->createAddress( - ::Utils::qStringToLinphoneString(sipAddress) - ); - - return !!address; +bool SipAddressesModel::sipAddressIsValid (const QString &sipAddress) { + return !!linphone::Factory::get()->createAddress( + ::Utils::qStringToLinphoneString(sipAddress) + ); } // ----------------------------------------------------------------------------- @@ -347,8 +346,14 @@ void SipAddressesModel::handleMessagesCountReset (const QString &sipAddress) { // ----------------------------------------------------------------------------- void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact) { - map["contact"] = QVariant::fromValue(contact); - updateObservers(map["sipAddress"].toString(), contact); + QString sipAddress = map["sipAddress"].toString(); + + if (contact) + map["contact"] = QVariant::fromValue(contact); + else if (map.remove("contact") == 0) + qWarning() << QStringLiteral("`contact` field is empty on sip address: `%1`.").arg(sipAddress); + + updateObservers(sipAddress, contact); } void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr &call) { @@ -406,16 +411,18 @@ void SipAddressesModel::removeContactOfSipAddress (const QString &sipAddress) { return; } - updateObservers(sipAddress, nullptr); + // Try to map other contact on this sip address. + ContactModel *contactModel = CoreManager::getInstance()->getContactsListModel()->findContactModelFromSipAddress(sipAddress); + updateObservers(sipAddress, contactModel); - if (it->remove("contact") == 0) - qWarning() << QStringLiteral("`contact` field is empty on sip address: `%1`.").arg(sipAddress); + qInfo() << QStringLiteral("Map new contact on sip address: `%1`.").arg(sipAddress) << contactModel; + addOrUpdateSipAddress(*it, contactModel); int row = mRefs.indexOf(&(*it)); Q_ASSERT(row != -1); // History exists, signal changes. - if (it->contains("timestamp")) { + if (it->contains("timestamp") || contactModel) { emit dataChanged(index(row, 0), index(row, 0)); return; } diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp index 79ca7b9b0..94ece33a3 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp @@ -58,7 +58,8 @@ public: Q_INVOKABLE QString getTransportFromSipAddress (const QString &sipAddress) const; Q_INVOKABLE QString addTransportToSipAddress (const QString &sipAddress, const QString &transport) const; - Q_INVOKABLE bool sipAddressIsValid (const QString &sipAddress) const; + + Q_INVOKABLE static bool sipAddressIsValid (const QString &sipAddress); // ---------------------------------------------------------------------------