Commit f0a4652c authored by Ronan Abhamon's avatar Ronan Abhamon

feat(app): handle properly new messages

parent bd1b7e72
...@@ -18,6 +18,27 @@ ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { ...@@ -18,6 +18,27 @@ ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) {
this, &ChatModel::allEntriesRemoved, this, &ChatModel::allEntriesRemoved,
CoreManager::getInstance()->getSipAddressesModel(), &SipAddressesModel::handleAllHistoryEntriesRemoved CoreManager::getInstance()->getSipAddressesModel(), &SipAddressesModel::handleAllHistoryEntriesRemoved
); );
m_handlers = CoreManager::getInstance()->getHandlers();
QObject::connect(
&(*m_handlers), &CoreHandlers::receivedMessage,
this, [this](
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &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<void>(message));
endInsertRows();
}
}
);
} }
QHash<int, QByteArray> ChatModel::roleNames () const { QHash<int, QByteArray> ChatModel::roleNames () const {
......
...@@ -8,15 +8,12 @@ ...@@ -8,15 +8,12 @@
// Fetch all N messages of a ChatRoom. // Fetch all N messages of a ChatRoom.
// ============================================================================= // =============================================================================
class CoreHandlers;
class ChatModel : public QAbstractListModel { class ChatModel : public QAbstractListModel {
Q_OBJECT; Q_OBJECT;
Q_PROPERTY( Q_PROPERTY(QString sipAddress READ getSipAddress WRITE setSipAddress NOTIFY sipAddressChanged);
QString sipAddress
READ getSipAddress
WRITE setSipAddress
NOTIFY sipAddressChanged
);
public: public:
typedef QPair<QVariantMap, std::shared_ptr<void> > ChatEntryData; typedef QPair<QVariantMap, std::shared_ptr<void> > ChatEntryData;
...@@ -83,6 +80,8 @@ private: ...@@ -83,6 +80,8 @@ private:
QList<ChatEntryData> m_entries; QList<ChatEntryData> m_entries;
std::shared_ptr<linphone::ChatRoom> m_chat_room; std::shared_ptr<linphone::ChatRoom> m_chat_room;
std::shared_ptr<CoreHandlers> m_handlers;
}; };
#endif // CHAT_MODEL_H_ #endif // CHAT_MODEL_H_
...@@ -31,8 +31,7 @@ void CoreHandlers::onMessageReceived ( ...@@ -31,8 +31,7 @@ void CoreHandlers::onMessageReceived (
const shared_ptr<linphone::ChatRoom> &room, const shared_ptr<linphone::ChatRoom> &room,
const shared_ptr<linphone::ChatMessage> &message const shared_ptr<linphone::ChatMessage> &message
) { ) {
CoreManager *core = CoreManager::getInstance(); emit receivedMessage(room, message);
core->getSipAddressesModel()->handleReceivedMessage(room, message);
const App *app = App::getInstance(); const App *app = App::getInstance();
if (!app->hasFocus()) if (!app->hasFocus())
......
...@@ -2,10 +2,15 @@ ...@@ -2,10 +2,15 @@
#define CORE_HANDLERS_H_ #define CORE_HANDLERS_H_
#include <linphone++/linphone.hh> #include <linphone++/linphone.hh>
#include <QObject>
// ============================================================================= // =============================================================================
class CoreHandlers : public linphone::CoreListener { class CoreHandlers :
public QObject,
public linphone::CoreListener {
Q_OBJECT;
public: public:
void onAuthenticationRequested ( void onAuthenticationRequested (
const std::shared_ptr<linphone::Core> &core, const std::shared_ptr<linphone::Core> &core,
...@@ -25,6 +30,12 @@ public: ...@@ -25,6 +30,12 @@ public:
const std::shared_ptr<linphone::ChatRoom> &room, const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message const std::shared_ptr<linphone::ChatMessage> &message
) override; ) override;
signals:
void receivedMessage (
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
);
}; };
#endif // CORE_HANDLERS_H_ #endif // CORE_HANDLERS_H_
#include <QTimer> #include <QTimer>
#include "../../app/Database.hpp" #include "../../app/Database.hpp"
#include "CoreHandlers.hpp"
#include "CoreManager.hpp" #include "CoreManager.hpp"
...@@ -19,10 +18,6 @@ CoreManager::CoreManager (QObject *parent) : QObject(parent), m_handlers(make_sh ...@@ -19,10 +18,6 @@ CoreManager::CoreManager (QObject *parent) : QObject(parent), m_handlers(make_sh
setDatabasesPaths(); setDatabasesPaths();
} }
CoreManager::~CoreManager () {
delete m_cbs_timer;
}
void CoreManager::enableHandlers () { void CoreManager::enableHandlers () {
m_cbs_timer->start(); m_cbs_timer->start();
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "../contacts/ContactsListModel.hpp" #include "../contacts/ContactsListModel.hpp"
#include "../sip-addresses/SipAddressesModel.hpp" #include "../sip-addresses/SipAddressesModel.hpp"
#include "CoreHandlers.hpp"
// ============================================================================= // =============================================================================
...@@ -12,12 +13,18 @@ class CoreManager : public QObject { ...@@ -12,12 +13,18 @@ class CoreManager : public QObject {
Q_OBJECT; Q_OBJECT;
public: public:
~CoreManager (); ~CoreManager () = default;
void enableHandlers ();
std::shared_ptr<linphone::Core> getCore () { std::shared_ptr<linphone::Core> getCore () {
return m_core; return m_core;
} }
std::shared_ptr<CoreHandlers> getHandlers () {
return m_handlers;
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Singleton models. // Singleton models.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -30,7 +37,9 @@ public: ...@@ -30,7 +37,9 @@ public:
return m_sip_addresses_model; return m_sip_addresses_model;
} }
void enableHandlers (); // ---------------------------------------------------------------------------
// Initialization.
// ---------------------------------------------------------------------------
static void init (); static void init ();
...@@ -38,6 +47,8 @@ public: ...@@ -38,6 +47,8 @@ public:
return m_instance; return m_instance;
} }
// ---------------------------------------------------------------------------
// Must be used in a qml scene. // Must be used in a qml scene.
// Warning: The ownership of `VcardModel` is `QQmlEngine::JavaScriptOwnership` by default. // Warning: The ownership of `VcardModel` is `QQmlEngine::JavaScriptOwnership` by default.
Q_INVOKABLE VcardModel *createDetachedVcardModel (); Q_INVOKABLE VcardModel *createDetachedVcardModel ();
...@@ -48,7 +59,7 @@ private: ...@@ -48,7 +59,7 @@ private:
void setDatabasesPaths (); void setDatabasesPaths ();
std::shared_ptr<linphone::Core> m_core; std::shared_ptr<linphone::Core> m_core;
std::shared_ptr<linphone::CoreListener> m_handlers; std::shared_ptr<CoreHandlers> m_handlers;
ContactsListModel *m_contacts_list_model; ContactsListModel *m_contacts_list_model;
SipAddressesModel *m_sip_addresses_model; SipAddressesModel *m_sip_addresses_model;
......
...@@ -19,6 +19,9 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare ...@@ -19,6 +19,9 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare
QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded); QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded);
QObject::connect(contacts, &ContactsListModel::contactRemoved, this, &SipAddressesModel::handleContactRemoved); QObject::connect(contacts, &ContactsListModel::contactRemoved, this, &SipAddressesModel::handleContactRemoved);
m_handlers = CoreManager::getInstance()->getHandlers();
QObject::connect(&(*m_handlers), &CoreHandlers::receivedMessage, this, &SipAddressesModel::handleReceivedMessage);
QObject::connect( QObject::connect(
contacts, &ContactsListModel::sipAddressAdded, this, [this](ContactModel *contact, const QString &sip_address) { contacts, &ContactsListModel::sipAddressAdded, this, [this](ContactModel *contact, const QString &sip_address) {
// TODO: Avoid the limitation of one contact by sip address. // TODO: Avoid the limitation of one contact by sip address.
...@@ -129,14 +132,6 @@ void SipAddressesModel::handleAllHistoryEntriesRemoved () { ...@@ -129,14 +132,6 @@ void SipAddressesModel::handleAllHistoryEntriesRemoved () {
emit dataChanged(index(row, 0), index(row, 0)); emit dataChanged(index(row, 0), index(row, 0));
} }
void SipAddressesModel::handleReceivedMessage (
const shared_ptr<linphone::ChatRoom> &room,
const shared_ptr<linphone::ChatMessage> &message
) {
const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asString());
addOrUpdateSipAddress(sip_address, nullptr, static_cast<qint64>(message->getTime()));
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool SipAddressesModel::removeRow (int row, const QModelIndex &parent) { bool SipAddressesModel::removeRow (int row, const QModelIndex &parent) {
...@@ -174,6 +169,14 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) { ...@@ -174,6 +169,14 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) {
removeContactOfSipAddress(sip_address.toString()); removeContactOfSipAddress(sip_address.toString());
} }
void SipAddressesModel::handleReceivedMessage (
const shared_ptr<linphone::ChatRoom> &,
const shared_ptr<linphone::ChatMessage> &message
) {
const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asString());
addOrUpdateSipAddress(sip_address, nullptr, static_cast<qint64>(message->getTime()));
}
void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact, qint64 timestamp) { void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact, qint64 timestamp) {
auto it = m_sip_addresses.find(sip_address); auto it = m_sip_addresses.find(sip_address);
if (it != m_sip_addresses.end()) { if (it != m_sip_addresses.end()) {
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
// ============================================================================= // =============================================================================
class CoreHandlers;
class SipAddressesModel : public QAbstractListModel { class SipAddressesModel : public QAbstractListModel {
Q_OBJECT; Q_OBJECT;
...@@ -25,11 +27,6 @@ public: ...@@ -25,11 +27,6 @@ public:
Q_INVOKABLE void handleAllHistoryEntriesRemoved (); Q_INVOKABLE void handleAllHistoryEntriesRemoved ();
void handleReceivedMessage (
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
);
private: private:
bool removeRow (int row, const QModelIndex &parent = QModelIndex()); bool removeRow (int row, const QModelIndex &parent = QModelIndex());
bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override; bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override;
...@@ -37,6 +34,11 @@ private: ...@@ -37,6 +34,11 @@ private:
void handleContactAdded (ContactModel *contact); void handleContactAdded (ContactModel *contact);
void handleContactRemoved (const ContactModel *contact); void handleContactRemoved (const ContactModel *contact);
void handleReceivedMessage (
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
);
void addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact = nullptr, qint64 timestamp = 0); void addOrUpdateSipAddress (const QString &sip_address, ContactModel *contact = nullptr, qint64 timestamp = 0);
void removeContactOfSipAddress (const QString &sip_address); void removeContactOfSipAddress (const QString &sip_address);
...@@ -48,6 +50,8 @@ private: ...@@ -48,6 +50,8 @@ private:
QList<const QVariantMap *> m_refs; QList<const QVariantMap *> m_refs;
QMultiHash<QString, ContactObserver *> m_observers; QMultiHash<QString, ContactObserver *> m_observers;
std::shared_ptr<CoreHandlers> m_handlers;
}; };
#endif // SIP_ADDRESSES_MODEL_H_ #endif // SIP_ADDRESSES_MODEL_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment