From f0a4652c6261835ff1369bf1bd60cff02a8b5c10 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 4 Jan 2017 10:36:06 +0100 Subject: [PATCH] feat(app): handle properly new messages --- tests/src/components/chat/ChatModel.cpp | 21 +++++++++++++++++++ tests/src/components/chat/ChatModel.hpp | 11 +++++----- tests/src/components/core/CoreHandlers.cpp | 3 +-- tests/src/components/core/CoreHandlers.hpp | 13 +++++++++++- tests/src/components/core/CoreManager.cpp | 5 ----- tests/src/components/core/CoreManager.hpp | 17 ++++++++++++--- .../sip-addresses/SipAddressesModel.cpp | 19 ++++++++++------- .../sip-addresses/SipAddressesModel.hpp | 14 ++++++++----- 8 files changed, 73 insertions(+), 30 deletions(-) diff --git a/tests/src/components/chat/ChatModel.cpp b/tests/src/components/chat/ChatModel.cpp index 9c85fb1ae..f0fb8ac07 100644 --- a/tests/src/components/chat/ChatModel.cpp +++ b/tests/src/components/chat/ChatModel.cpp @@ -18,6 +18,27 @@ ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { this, &ChatModel::allEntriesRemoved, CoreManager::getInstance()->getSipAddressesModel(), &SipAddressesModel::handleAllHistoryEntriesRemoved ); + + m_handlers = CoreManager::getInstance()->getHandlers(); + QObject::connect( + &(*m_handlers), &CoreHandlers::receivedMessage, + this, [this]( + const std::shared_ptr &room, + const std::shared_ptr &message + ) { + if (m_chat_room == room) { + int row = rowCount(); + + beginInsertRows(QModelIndex(), row, row); + + QVariantMap map; + fillMessageEntry(map, message); + m_entries << qMakePair(map, static_pointer_cast(message)); + + endInsertRows(); + } + } + ); } QHash ChatModel::roleNames () const { diff --git a/tests/src/components/chat/ChatModel.hpp b/tests/src/components/chat/ChatModel.hpp index 26b0a0e86..ea9d2740b 100644 --- a/tests/src/components/chat/ChatModel.hpp +++ b/tests/src/components/chat/ChatModel.hpp @@ -8,15 +8,12 @@ // Fetch all N messages of a ChatRoom. // ============================================================================= +class CoreHandlers; + class ChatModel : public QAbstractListModel { Q_OBJECT; - Q_PROPERTY( - QString sipAddress - READ getSipAddress - WRITE setSipAddress - NOTIFY sipAddressChanged - ); + Q_PROPERTY(QString sipAddress READ getSipAddress WRITE setSipAddress NOTIFY sipAddressChanged); public: typedef QPair > ChatEntryData; @@ -83,6 +80,8 @@ private: QList m_entries; std::shared_ptr m_chat_room; + + std::shared_ptr m_handlers; }; #endif // CHAT_MODEL_H_ diff --git a/tests/src/components/core/CoreHandlers.cpp b/tests/src/components/core/CoreHandlers.cpp index 7ae767c58..e57ba2104 100644 --- a/tests/src/components/core/CoreHandlers.cpp +++ b/tests/src/components/core/CoreHandlers.cpp @@ -31,8 +31,7 @@ void CoreHandlers::onMessageReceived ( const shared_ptr &room, const shared_ptr &message ) { - CoreManager *core = CoreManager::getInstance(); - core->getSipAddressesModel()->handleReceivedMessage(room, message); + emit receivedMessage(room, 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 f167a9f17..3a6817017 100644 --- a/tests/src/components/core/CoreHandlers.hpp +++ b/tests/src/components/core/CoreHandlers.hpp @@ -2,10 +2,15 @@ #define CORE_HANDLERS_H_ #include +#include // ============================================================================= -class CoreHandlers : public linphone::CoreListener { +class CoreHandlers : + public QObject, + public linphone::CoreListener { + Q_OBJECT; + public: void onAuthenticationRequested ( const std::shared_ptr &core, @@ -25,6 +30,12 @@ public: const std::shared_ptr &room, const std::shared_ptr &message ) override; + +signals: + void receivedMessage ( + const std::shared_ptr &room, + const std::shared_ptr &message + ); }; #endif // CORE_HANDLERS_H_ diff --git a/tests/src/components/core/CoreManager.cpp b/tests/src/components/core/CoreManager.cpp index 77d639475..4f2c50720 100644 --- a/tests/src/components/core/CoreManager.cpp +++ b/tests/src/components/core/CoreManager.cpp @@ -1,7 +1,6 @@ #include #include "../../app/Database.hpp" -#include "CoreHandlers.hpp" #include "CoreManager.hpp" @@ -19,10 +18,6 @@ CoreManager::CoreManager (QObject *parent) : QObject(parent), m_handlers(make_sh setDatabasesPaths(); } -CoreManager::~CoreManager () { - delete m_cbs_timer; -} - void CoreManager::enableHandlers () { m_cbs_timer->start(); } diff --git a/tests/src/components/core/CoreManager.hpp b/tests/src/components/core/CoreManager.hpp index 5f12389f6..ef81957c1 100644 --- a/tests/src/components/core/CoreManager.hpp +++ b/tests/src/components/core/CoreManager.hpp @@ -3,6 +3,7 @@ #include "../contacts/ContactsListModel.hpp" #include "../sip-addresses/SipAddressesModel.hpp" +#include "CoreHandlers.hpp" // ============================================================================= @@ -12,12 +13,18 @@ class CoreManager : public QObject { Q_OBJECT; public: - ~CoreManager (); + ~CoreManager () = default; + + void enableHandlers (); std::shared_ptr getCore () { return m_core; } + std::shared_ptr getHandlers () { + return m_handlers; + } + // --------------------------------------------------------------------------- // Singleton models. // --------------------------------------------------------------------------- @@ -30,7 +37,9 @@ public: return m_sip_addresses_model; } - void enableHandlers (); + // --------------------------------------------------------------------------- + // Initialization. + // --------------------------------------------------------------------------- static void init (); @@ -38,6 +47,8 @@ public: return m_instance; } + // --------------------------------------------------------------------------- + // Must be used in a qml scene. // Warning: The ownership of `VcardModel` is `QQmlEngine::JavaScriptOwnership` by default. Q_INVOKABLE VcardModel *createDetachedVcardModel (); @@ -48,7 +59,7 @@ private: void setDatabasesPaths (); std::shared_ptr m_core; - std::shared_ptr m_handlers; + std::shared_ptr m_handlers; ContactsListModel *m_contacts_list_model; SipAddressesModel *m_sip_addresses_model; diff --git a/tests/src/components/sip-addresses/SipAddressesModel.cpp b/tests/src/components/sip-addresses/SipAddressesModel.cpp index 9e16265ef..4c434114c 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.cpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.cpp @@ -19,6 +19,9 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded); QObject::connect(contacts, &ContactsListModel::contactRemoved, this, &SipAddressesModel::handleContactRemoved); + m_handlers = CoreManager::getInstance()->getHandlers(); + QObject::connect(&(*m_handlers), &CoreHandlers::receivedMessage, this, &SipAddressesModel::handleReceivedMessage); + QObject::connect( contacts, &ContactsListModel::sipAddressAdded, this, [this](ContactModel *contact, const QString &sip_address) { // TODO: Avoid the limitation of one contact by sip address. @@ -129,14 +132,6 @@ void SipAddressesModel::handleAllHistoryEntriesRemoved () { emit dataChanged(index(row, 0), index(row, 0)); } -void SipAddressesModel::handleReceivedMessage ( - const shared_ptr &room, - const shared_ptr &message -) { - const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asString()); - addOrUpdateSipAddress(sip_address, nullptr, static_cast(message->getTime())); -} - // ----------------------------------------------------------------------------- bool SipAddressesModel::removeRow (int row, const QModelIndex &parent) { @@ -174,6 +169,14 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) { removeContactOfSipAddress(sip_address.toString()); } +void SipAddressesModel::handleReceivedMessage ( + const shared_ptr &, + const shared_ptr &message +) { + const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asString()); + 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()) { diff --git a/tests/src/components/sip-addresses/SipAddressesModel.hpp b/tests/src/components/sip-addresses/SipAddressesModel.hpp index 8fc86aa31..6004c6561 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.hpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.hpp @@ -8,6 +8,8 @@ // ============================================================================= +class CoreHandlers; + class SipAddressesModel : public QAbstractListModel { Q_OBJECT; @@ -25,11 +27,6 @@ public: Q_INVOKABLE void handleAllHistoryEntriesRemoved (); - void handleReceivedMessage ( - const std::shared_ptr &room, - const std::shared_ptr &message - ); - private: bool removeRow (int row, const QModelIndex &parent = QModelIndex()); bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override; @@ -37,6 +34,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, qint64 timestamp = 0); void removeContactOfSipAddress (const QString &sip_address); @@ -48,6 +50,8 @@ private: QList m_refs; QMultiHash m_observers; + + std::shared_ptr m_handlers; }; #endif // SIP_ADDRESSES_MODEL_H_