diff --git a/linphone-desktop/src/app/object-builders/AsyncObjectBuilder.cpp b/linphone-desktop/src/app/object-builders/AsyncObjectBuilder.cpp index 5dbb86c29..a6364cdd3 100644 --- a/linphone-desktop/src/app/object-builders/AsyncObjectBuilder.cpp +++ b/linphone-desktop/src/app/object-builders/AsyncObjectBuilder.cpp @@ -102,7 +102,11 @@ void AsyncObjectBuilder::createObject (QQmlEngine *engine, const char *path, Dec qInfo() << QStringLiteral("Start async creation of: `%1`. Component:").arg(path) << m_component; - QObject::connect(m_component, &QQmlComponent::statusChanged, this, &AsyncObjectBuilder::handleComponentCreation); + QObject::connect( + m_component, &QQmlComponent::statusChanged, + this, &AsyncObjectBuilder::handleComponentCreation, + Qt::DirectConnection + ); } QObject *AsyncObjectBuilder::getObject () const { diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressObserver.cpp b/linphone-desktop/src/components/sip-addresses/SipAddressObserver.cpp index 478500e46..66bd54514 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressObserver.cpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressObserver.cpp @@ -43,3 +43,11 @@ void SipAddressObserver::setPresenceStatus (const Presence::PresenceStatus &pres m_presence_status = presence_status; emit presenceStatusChanged(presence_status); } + +void SipAddressObserver::setUnreadMessagesCount (int unread_messages_count) { + if (unread_messages_count == m_unread_messages_count) + return; + + m_unread_messages_count = unread_messages_count; + emit unreadMessagesCountChanged(unread_messages_count); +} diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressObserver.hpp b/linphone-desktop/src/components/sip-addresses/SipAddressObserver.hpp index 731431795..e88b13f95 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressObserver.hpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressObserver.hpp @@ -36,6 +36,7 @@ class SipAddressObserver : public QObject { Q_PROPERTY(ContactModel * contact READ getContact NOTIFY contactChanged); Q_PROPERTY(Presence::PresenceStatus presenceStatus READ getPresenceStatus NOTIFY presenceStatusChanged); + Q_PROPERTY(int unreadMessagesCount READ getUnreadMessagesCount NOTIFY unreadMessagesCountChanged); public: SipAddressObserver (const QString &sip_address); @@ -44,28 +45,42 @@ public: signals: void contactChanged (ContactModel *contact); void presenceStatusChanged (const Presence::PresenceStatus &presenceStatus); + void unreadMessagesCountChanged (int unreadMessagesCount); private: QString getSipAddress () const { return m_sip_address; } + // --------------------------------------------------------------------------- + ContactModel *getContact () const { return m_contact; } void setContact (ContactModel *contact); + // --------------------------------------------------------------------------- + Presence::PresenceStatus getPresenceStatus () const { return m_presence_status; } void setPresenceStatus (const Presence::PresenceStatus &presence_status); + // --------------------------------------------------------------------------- + + int getUnreadMessagesCount () const { + return m_unread_messages_count; + } + + void setUnreadMessagesCount (int unread_messages_count); + QString m_sip_address; ContactModel *m_contact = nullptr; Presence::PresenceStatus m_presence_status = Presence::PresenceStatus::Offline; + int m_unread_messages_count = 0; }; Q_DECLARE_METATYPE(SipAddressObserver *); diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp index cc6f9fe5e..b4f649b0c 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.cpp @@ -76,57 +76,18 @@ QVariant SipAddressesModel::data (const QModelIndex &index, int role) const { return QVariant(); } +// ----------------------------------------------------------------------------- + void SipAddressesModel::connectToChatModel (ChatModel *chat_model) { - QObject::connect( - chat_model, &ChatModel::allEntriesRemoved, - this, [this, chat_model]() { - const QString sip_address = chat_model->getSipAddress(); - auto it = m_sip_addresses.find(sip_address); - if (it == m_sip_addresses.end()) { - qWarning() << QStringLiteral("Unable to found sip address: `%1`.").arg(sip_address); - return; - } + QObject::connect(chat_model, &ChatModel::allEntriesRemoved, this, [this, chat_model] { + handleAllEntriesRemoved(chat_model->getSipAddress()); + }); - int row = m_refs.indexOf(&(*it)); - Q_ASSERT(row != -1); + QObject::connect(chat_model, &ChatModel::messageSent, this, &SipAddressesModel::handleMessageSent); - // No history, no contact => Remove sip address from list. - if (!it->contains("contact")) { - removeRow(row); - return; - } - - // Signal changes. - it->remove("timestamp"); - emit dataChanged(index(row, 0), index(row, 0)); - } - ); - - 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]() { - const QString &sip_address = chat_model->getSipAddress(); - - auto it = m_sip_addresses.find(sip_address); - if (it != m_sip_addresses.end()) { - (*it)["unreadMessagesCount"] = 0; - - int row = m_refs.indexOf(&(*it)); - Q_ASSERT(row != -1); - emit dataChanged(index(row, 0), index(row, 0)); - - return; - } - } - ); + QObject::connect(chat_model, &ChatModel::messagesCountReset, this, [this, chat_model] { + handleMessagesCountReset(chat_model->getSipAddress()); + }); } // ----------------------------------------------------------------------------- @@ -151,6 +112,9 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip model->setPresenceStatus( it->value("presenceStatus", Presence::PresenceStatus::Offline).value() ); + model->setUnreadMessagesCount( + it->value("unreadMessagesCount", 0).toInt() + ); } } @@ -288,6 +252,47 @@ void SipAddressesModel::handlePresenceReceived ( updateObservers(sip_address, status); } +void SipAddressesModel::handleAllEntriesRemoved (const QString &sip_address) { + auto it = m_sip_addresses.find(sip_address); + if (it == m_sip_addresses.end()) { + qWarning() << QStringLiteral("Unable to found sip address: `%1`.").arg(sip_address); + return; + } + + int row = m_refs.indexOf(&(*it)); + Q_ASSERT(row != -1); + + // No history, no contact => Remove sip address from list. + if (!it->contains("contact")) { + removeRow(row); + return; + } + + // Signal changes. + it->remove("timestamp"); + emit dataChanged(index(row, 0), index(row, 0)); +} + +void SipAddressesModel::handleMessageSent (const shared_ptr &message) { + addOrUpdateSipAddress( + ::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), + message + ); +} + +void SipAddressesModel::handleMessagesCountReset (const QString &sip_address) { + auto it = m_sip_addresses.find(sip_address); + if (it != m_sip_addresses.end()) { + (*it)["unreadMessagesCount"] = 0; + + int row = m_refs.indexOf(&(*it)); + Q_ASSERT(row != -1); + emit dataChanged(index(row, 0), index(row, 0)); + } + + updateObservers(sip_address, 0); +} + // ----------------------------------------------------------------------------- void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact) { @@ -304,8 +309,14 @@ void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_pt } void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr &message) { + // FIXME: Bug in the core, count is incremented after this function call. + // So... +1! + int count = message->getChatRoom()->getUnreadMessagesCount() + 1; + map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); - map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount(); + map["unreadMessagesCount"] = count; + + updateObservers(map["sipAddress"].toString(), count); } template @@ -418,6 +429,8 @@ void SipAddressesModel::initSipAddresses () { handleContactAdded(contact); } +// ----------------------------------------------------------------------------- + void SipAddressesModel::updateObservers (const QString &sip_address, ContactModel *contact) { for (auto &observer : m_observers.values(sip_address)) observer->setContact(contact); @@ -427,3 +440,8 @@ void SipAddressesModel::updateObservers (const QString &sip_address, const Prese for (auto &observer : m_observers.values(sip_address)) observer->setPresenceStatus(presence_status); } + +void SipAddressesModel::updateObservers (const QString &sip_address, int messages_count) { + for (auto &observer : m_observers.values(sip_address)) + observer->setUnreadMessagesCount(messages_count); +} diff --git a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp index f872b2f80..f5e1bc2c3 100644 --- a/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp +++ b/linphone-desktop/src/components/sip-addresses/SipAddressesModel.hpp @@ -68,6 +68,10 @@ private: void handleCallStateChanged (const std::shared_ptr &call, linphone::CallState state); void handlePresenceReceived (const QString &sip_address, const shared_ptr &presence_model); + void handleAllEntriesRemoved (const QString &sip_address); + void handleMessageSent (const std::shared_ptr &message); + void handleMessagesCountReset (const QString &sip_address); + // --------------------------------------------------------------------------- // A sip address exists in this list if a contact is linked to it, or a call, or a message. @@ -87,6 +91,7 @@ private: void updateObservers (const QString &sip_address, ContactModel *contact); void updateObservers (const QString &sip_address, const Presence::PresenceStatus &presence_status); + void updateObservers (const QString &sip_address, int messages_count); QHash m_sip_addresses; QList m_refs;