diff --git a/tests/src/components/chat/ChatModel.cpp b/tests/src/components/chat/ChatModel.cpp index 7cd5e95ba..a4f1ab6c8 100644 --- a/tests/src/components/chat/ChatModel.cpp +++ b/tests/src/components/chat/ChatModel.cpp @@ -72,21 +72,15 @@ private: // ----------------------------------------------------------------------------- ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { - QObject::connect( - this, &ChatModel::allEntriesRemoved, - CoreManager::getInstance()->getSipAddressesModel(), &SipAddressesModel::handleAllHistoryEntriesRemoved - ); - m_core_handlers = CoreManager::getInstance()->getHandlers(); m_message_handlers = make_shared(this); + CoreManager::getInstance()->getSipAddressesModel()->connectToChatModel(this); + QObject::connect( &(*m_core_handlers), &CoreHandlers::receivedMessage, - this, [this]( - const shared_ptr &room, - const shared_ptr &message - ) { - if (m_chat_room == room) { + this, [this](const shared_ptr &message) { + if (m_chat_room == message->getChatRoom()) { insertMessageAtEnd(message); m_chat_room->markAsRead(); } @@ -112,7 +106,7 @@ int ChatModel::rowCount (const QModelIndex &) const { QVariant ChatModel::data (const QModelIndex &index, int role) const { int row = index.row(); - if (row < 0 || row >= m_entries.count()) + if (!index.isValid() || row < 0 || row >= m_entries.count()) return QVariant(); switch (role) { @@ -132,7 +126,7 @@ bool ChatModel::removeRow (int row, const QModelIndex &) { bool ChatModel::removeRows (int row, int count, const QModelIndex &parent) { int limit = row + count - 1; - if (row < 0 || count < 0 || limit >= m_entries.count()) + if (!parent.isValid() || row < 0 || count < 0 || limit >= m_entries.count()) return false; beginRemoveRows(parent, row, limit); @@ -252,6 +246,8 @@ void ChatModel::sendMessage (const QString &message) { _message->setListener(m_message_handlers); m_chat_room->sendMessage(_message); insertMessageAtEnd(_message); + + emit messageSent(_message); } // ----------------------------------------------------------------------------- @@ -261,7 +257,7 @@ void ChatModel::fillMessageEntry ( const shared_ptr &message ) { dest["type"] = EntryType::MessageEntry; - dest["timestamp"] = QDateTime::fromMSecsSinceEpoch(static_cast(message->getTime()) * 1000); + dest["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); dest["content"] = ::Utils::linphoneStringToQString(message->getText()); dest["isOutgoing"] = message->isOutgoing(); dest["status"] = message->getState(); @@ -271,7 +267,7 @@ void ChatModel::fillCallStartEntry ( QVariantMap &dest, const shared_ptr &call_log ) { - QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(static_cast(call_log->getStartDate()) * 1000); + QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(call_log->getStartDate() * 1000); dest["type"] = EntryType::CallEntry; dest["timestamp"] = timestamp; @@ -284,9 +280,7 @@ void ChatModel::fillCallEndEntry ( QVariantMap &dest, const shared_ptr &call_log ) { - QDateTime timestamp = QDateTime::fromMSecsSinceEpoch( - static_cast(call_log->getStartDate() + call_log->getDuration()) * 1000 - ); + QDateTime timestamp = QDateTime::fromMSecsSinceEpoch((call_log->getStartDate() + call_log->getDuration()) * 1000); dest["type"] = EntryType::CallEntry; dest["timestamp"] = timestamp; diff --git a/tests/src/components/chat/ChatModel.hpp b/tests/src/components/chat/ChatModel.hpp index 5d4d36606..189bf884d 100644 --- a/tests/src/components/chat/ChatModel.hpp +++ b/tests/src/components/chat/ChatModel.hpp @@ -72,6 +72,8 @@ signals: void sipAddressChanged (const QString &sip_address); void allEntriesRemoved (); + void messageSent (std::shared_ptr &message); + private: void fillMessageEntry ( QVariantMap &dest, diff --git a/tests/src/components/core/CoreHandlers.cpp b/tests/src/components/core/CoreHandlers.cpp index e57ba2104..33e19df5c 100644 --- a/tests/src/components/core/CoreHandlers.cpp +++ b/tests/src/components/core/CoreHandlers.cpp @@ -31,7 +31,7 @@ void CoreHandlers::onMessageReceived ( const shared_ptr &room, const shared_ptr &message ) { - emit receivedMessage(room, message); + emit receivedMessage(message); const App *app = App::getInstance(); if (!app->hasFocus()) diff --git a/tests/src/components/core/CoreHandlers.hpp b/tests/src/components/core/CoreHandlers.hpp index 3a6817017..b17106350 100644 --- a/tests/src/components/core/CoreHandlers.hpp +++ b/tests/src/components/core/CoreHandlers.hpp @@ -32,10 +32,7 @@ public: ) override; signals: - void receivedMessage ( - const std::shared_ptr &room, - const std::shared_ptr &message - ); + void receivedMessage (const std::shared_ptr &message); }; #endif // CORE_HANDLERS_H_ diff --git a/tests/src/components/sip-addresses/SipAddressesModel.cpp b/tests/src/components/sip-addresses/SipAddressesModel.cpp index f165b3852..a5117c50a 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.cpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.cpp @@ -20,7 +20,13 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare QObject::connect(contacts, &ContactsListModel::contactRemoved, this, &SipAddressesModel::handleContactRemoved); m_handlers = CoreManager::getInstance()->getHandlers(); - QObject::connect(&(*m_handlers), &CoreHandlers::receivedMessage, this, &SipAddressesModel::handleReceivedMessage); + QObject::connect( + &(*m_handlers), &CoreHandlers::receivedMessage, + this, [this](const std::shared_ptr &message) { + const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asStringUriOnly()); + addOrUpdateSipAddress(sip_address, nullptr, message); + } + ); QObject::connect( contacts, &ContactsListModel::sipAddressAdded, this, [this](ContactModel *contact, const QString &sip_address) { @@ -72,6 +78,41 @@ 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; + } + + 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)); + } + ); + + QObject::connect( + chat_model, &ChatModel::messageSent, + this, [this](const std::shared_ptr &message) { + addOrUpdateSipAddress( + ::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), nullptr, message + ); + }); +} + // ----------------------------------------------------------------------------- ContactModel *SipAddressesModel::mapSipAddressToContact (const QString &sip_address) const { @@ -102,38 +143,6 @@ ContactObserver *SipAddressesModel::getContactObserver (const QString &sip_addre // ----------------------------------------------------------------------------- -void SipAddressesModel::handleAllHistoryEntriesRemoved () { - QObject *sender = QObject::sender(); - if (!sender) - return; - - ChatModel *chat_model = qobject_cast(sender); - if (!chat_model) - return; - - 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; - } - - 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)); -} - -// ----------------------------------------------------------------------------- - QString SipAddressesModel::interpretUrl (const QString &sip_address) { shared_ptr l_address = CoreManager::getInstance()->getCore()->interpretUrl( ::Utils::qStringToLinphoneString(sip_address) @@ -179,15 +188,11 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) { removeContactOfSipAddress(sip_address.toString()); } -void SipAddressesModel::handleReceivedMessage ( - const shared_ptr &, - const shared_ptr &message +void SipAddressesModel::addOrUpdateSipAddress ( + const QString &sip_address, + ContactModel *contact, + const std::shared_ptr &message ) { - const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asStringUriOnly()); - addOrUpdateSipAddress(sip_address, nullptr, static_cast(message->getTime())); -} - -void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact, qint64 timestamp) { auto it = m_sip_addresses.find(sip_address); if (it != m_sip_addresses.end()) { if (contact) { @@ -195,8 +200,8 @@ void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, Conta updateObservers(sip_address, contact); } - if (timestamp) - (*it)["timestamp"] = QDateTime::fromMSecsSinceEpoch(timestamp * 1000); + if (message) + (*it)["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); int row = m_refs.indexOf(&(*it)); Q_ASSERT(row != -1); @@ -213,8 +218,8 @@ void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, Conta updateObservers(sip_address, contact); } - if (timestamp) - map["timestamp"] = QDateTime::fromMSecsSinceEpoch(timestamp * 1000); + if (message) + map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); int row = m_refs.count(); @@ -267,7 +272,7 @@ void SipAddressesModel::initSipAddresses () { QVariantMap map; map["sipAddress"] = sip_address; - map["timestamp"] = QDateTime::fromMSecsSinceEpoch(static_cast(history.back()->getTime()) * 1000); + map["timestamp"] = QDateTime::fromMSecsSinceEpoch(history.back()->getTime() * 1000); m_sip_addresses[sip_address] = map; } @@ -287,9 +292,7 @@ void SipAddressesModel::initSipAddresses () { QVariantMap map; map["sipAddress"] = sip_address; - map["timestamp"] = QDateTime::fromMSecsSinceEpoch( - static_cast(call_log->getStartDate() + call_log->getDuration()) * 1000 - ); + map["timestamp"] = QDateTime::fromMSecsSinceEpoch((call_log->getStartDate() + call_log->getDuration()) * 1000); auto it = m_sip_addresses.find(sip_address); if (it == m_sip_addresses.end() || map["timestamp"] > (*it)["timestamp"]) diff --git a/tests/src/components/sip-addresses/SipAddressesModel.hpp b/tests/src/components/sip-addresses/SipAddressesModel.hpp index 6f2e14c39..cd8eeb95f 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.hpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.hpp @@ -3,6 +3,7 @@ #include +#include "../chat/ChatModel.hpp" #include "../contact/ContactModel.hpp" #include "../contact/ContactObserver.hpp" @@ -22,11 +23,11 @@ public: QHash roleNames () const override; QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const override; + void connectToChatModel (ChatModel *chat_model); + Q_INVOKABLE ContactModel *mapSipAddressToContact (const QString &sip_address) const; Q_INVOKABLE ContactObserver *getContactObserver (const QString &sip_address); - Q_INVOKABLE void handleAllHistoryEntriesRemoved (); - Q_INVOKABLE QString interpretUrl (const QString &sip_address); private: @@ -36,12 +37,11 @@ private: void handleContactAdded (ContactModel *contact); void handleContactRemoved (const ContactModel *contact); - void handleReceivedMessage ( - const std::shared_ptr &room, - const std::shared_ptr &message + void addOrUpdateSipAddress ( + const QString &sip_address, + ContactModel *contact = nullptr, + const std::shared_ptr &message = std::shared_ptr() ); - - void addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact = nullptr, qint64 timestamp = 0); void removeContactOfSipAddress (const QString &sip_address); void initSipAddresses ();