Commit bd53a150 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(app): many fixes & refactoring

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