Commit bd53a150 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(app): many fixes & refactoring

parent ff733ab3
......@@ -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<MessageHandlers>(this);
CoreManager::getInstance()->getSipAddressesModel()->connectToChatModel(this);
QObject::connect(
&(*m_core_handlers), &CoreHandlers::receivedMessage,
this, [this](
const shared_ptr<linphone::ChatRoom> &room,
const shared_ptr<linphone::ChatMessage> &message
) {
if (m_chat_room == room) {
this, [this](const shared_ptr<linphone::ChatMessage> &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<linphone::ChatMessage> &message
) {
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["isOutgoing"] = message->isOutgoing();
dest["status"] = message->getState();
......@@ -271,7 +267,7 @@ void ChatModel::fillCallStartEntry (
QVariantMap &dest,
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["timestamp"] = timestamp;
......@@ -284,9 +280,7 @@ void ChatModel::fillCallEndEntry (
QVariantMap &dest,
const shared_ptr<linphone::CallLog> &call_log
) {
QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(
static_cast<qint64>(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;
......
......@@ -72,6 +72,8 @@ signals:
void sipAddressChanged (const QString &sip_address);
void allEntriesRemoved ();
void messageSent (std::shared_ptr<linphone::ChatMessage> &message);
private:
void fillMessageEntry (
QVariantMap &dest,
......
......@@ -31,7 +31,7 @@ void CoreHandlers::onMessageReceived (
const shared_ptr<linphone::ChatRoom> &room,
const shared_ptr<linphone::ChatMessage> &message
) {
emit receivedMessage(room, message);
emit receivedMessage(message);
const App *app = App::getInstance();
if (!app->hasFocus())
......
......@@ -32,10 +32,7 @@ public:
) override;
signals:
void receivedMessage (
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
);
void receivedMessage (const std::shared_ptr<linphone::ChatMessage> &message);
};
#endif // CORE_HANDLERS_H_
......@@ -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<linphone::ChatMessage> &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<linphone::ChatMessage> &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<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) {
shared_ptr<linphone::Address> 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<linphone::ChatRoom> &,
const shared_ptr<linphone::ChatMessage> &message
void SipAddressesModel::addOrUpdateSipAddress (
const QString &sip_address,
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);
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<qint64>(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<qint64>(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"])
......
......@@ -3,6 +3,7 @@
#include <QAbstractListModel>
#include "../chat/ChatModel.hpp"
#include "../contact/ContactModel.hpp"
#include "../contact/ContactObserver.hpp"
......@@ -22,11 +23,11 @@ public:
QHash<int, QByteArray> 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<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
void addOrUpdateSipAddress (
const QString &sip_address,
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 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