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) {
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<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 {
......
......@@ -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<QVariantMap, std::shared_ptr<void> > ChatEntryData;
......@@ -83,6 +80,8 @@ private:
QList<ChatEntryData> m_entries;
std::shared_ptr<linphone::ChatRoom> m_chat_room;
std::shared_ptr<CoreHandlers> m_handlers;
};
#endif // CHAT_MODEL_H_
......@@ -31,8 +31,7 @@ void CoreHandlers::onMessageReceived (
const shared_ptr<linphone::ChatRoom> &room,
const shared_ptr<linphone::ChatMessage> &message
) {
CoreManager *core = CoreManager::getInstance();
core->getSipAddressesModel()->handleReceivedMessage(room, message);
emit receivedMessage(room, message);
const App *app = App::getInstance();
if (!app->hasFocus())
......
......@@ -2,10 +2,15 @@
#define CORE_HANDLERS_H_
#include <linphone++/linphone.hh>
#include <QObject>
// =============================================================================
class CoreHandlers : public linphone::CoreListener {
class CoreHandlers :
public QObject,
public linphone::CoreListener {
Q_OBJECT;
public:
void onAuthenticationRequested (
const std::shared_ptr<linphone::Core> &core,
......@@ -25,6 +30,12 @@ public:
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
) override;
signals:
void receivedMessage (
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
);
};
#endif // CORE_HANDLERS_H_
#include <QTimer>
#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();
}
......
......@@ -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<linphone::Core> getCore () {
return m_core;
}
std::shared_ptr<CoreHandlers> 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<linphone::Core> m_core;
std::shared_ptr<linphone::CoreListener> m_handlers;
std::shared_ptr<CoreHandlers> m_handlers;
ContactsListModel *m_contacts_list_model;
SipAddressesModel *m_sip_addresses_model;
......
......@@ -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<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) {
......@@ -174,6 +169,14 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) {
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) {
auto it = m_sip_addresses.find(sip_address);
if (it != m_sip_addresses.end()) {
......
......@@ -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<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &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<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &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<const QVariantMap *> m_refs;
QMultiHash<QString, ContactObserver *> m_observers;
std::shared_ptr<CoreHandlers> m_handlers;
};
#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