From 8576147aa0fdda7d90e77b07c5c37ebb45df326e Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 21 Feb 2017 15:13:12 +0100 Subject: [PATCH] feat(src/components/chat/ChatModel): supports terminated calls --- .../src/components/chat/ChatModel.cpp | 90 +++++++++++-------- .../src/components/chat/ChatModel.hpp | 1 + 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/linphone-desktop/src/components/chat/ChatModel.cpp b/linphone-desktop/src/components/chat/ChatModel.cpp index e2fa8a0e7..e6d8c86c0 100644 --- a/linphone-desktop/src/components/chat/ChatModel.cpp +++ b/linphone-desktop/src/components/chat/ChatModel.cpp @@ -169,10 +169,12 @@ private: // ----------------------------------------------------------------------------- ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { - m_core_handlers = CoreManager::getInstance()->getHandlers(); + CoreManager *core = CoreManager::getInstance(); + + m_core_handlers = core->getHandlers(); m_message_handlers = make_shared(this); - CoreManager::getInstance()->getSipAddressesModel()->connectToChatModel(this); + core->getSipAddressesModel()->connectToChatModel(this); QObject::connect( &(*m_core_handlers), &CoreHandlers::messageReceived, @@ -185,6 +187,14 @@ ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { } } ); + + QObject::connect( + &(*m_core_handlers), &CoreHandlers::callStateChanged, + this, [this](const std::shared_ptr &call, linphone::CallState state) { + if (state == linphone::CallStateEnd || state == linphone::CallStateError) + insertCall(call->getCallLog()); + } + ); } ChatModel::~ChatModel () { @@ -282,39 +292,8 @@ void ChatModel::setSipAddress (const QString &sip_address) { } // Get calls. - auto insert_entry = [this]( - const ChatEntryData &pair, - const QList::iterator *start = NULL - ) { - auto it = lower_bound( - start ? *start : m_entries.begin(), m_entries.end(), pair, - [](const ChatEntryData &a, const ChatEntryData &b) { - return a.first["timestamp"] < b.first["timestamp"]; - } - ); - - return m_entries.insert(it, pair); - }; - - for (auto &call_log : core->getCallHistoryForAddress(m_chat_room->getPeerAddress())) { - linphone::CallStatus status = call_log->getStatus(); - - // Ignore aborted calls. - if (status == linphone::CallStatusAborted) - continue; - - // Add start call. - QVariantMap start; - fillCallStartEntry(start, call_log); - auto it = insert_entry(qMakePair(start, static_pointer_cast(call_log))); - - // Add end call. (if necessary) - if (status == linphone::CallStatusSuccess) { - QVariantMap end; - fillCallEndEntry(end, call_log); - insert_entry(qMakePair(end, static_pointer_cast(call_log)), &it); - } - } + for (auto &call_log : core->getCallHistoryForAddress(m_chat_room->getPeerAddress())) + insertCall(call_log); endResetModel(); @@ -379,6 +358,7 @@ void ChatModel::resendMessage (int id) { switch (map["status"].toInt()) { case MessageStatusFileTransferError: case MessageStatusNotDelivered: { + // TODO: Do not duplicate me! Use a linphone core function in the future. shared_ptr message = static_pointer_cast(entry.second); shared_ptr message2 = message->clone(); @@ -544,6 +524,46 @@ void ChatModel::removeEntry (ChatEntryData &pair) { } } +void ChatModel::insertCall (const shared_ptr &call_log) { + auto insert_entry = [this]( + const ChatEntryData &pair, + const QList::iterator *start = NULL + ) { + auto it = lower_bound( + start ? *start : m_entries.begin(), m_entries.end(), pair, + [](const ChatEntryData &a, const ChatEntryData &b) { + return a.first["timestamp"] < b.first["timestamp"]; + } + ); + + int row = static_cast(distance(m_entries.begin(), it)); + + beginInsertRows(QModelIndex(), row, row); + it = m_entries.insert(it, pair); + endInsertRows(); + + return it; + }; + + linphone::CallStatus status = call_log->getStatus(); + + // Ignore aborted calls. + if (status == linphone::CallStatusAborted) + return; + + // Add start call. + QVariantMap start; + fillCallStartEntry(start, call_log); + auto it = insert_entry(qMakePair(start, static_pointer_cast(call_log))); + + // Add end call. (if necessary) + if (status == linphone::CallStatusSuccess) { + QVariantMap end; + fillCallEndEntry(end, call_log); + insert_entry(qMakePair(end, static_pointer_cast(call_log)), &it); + } +} + void ChatModel::insertMessageAtEnd (const shared_ptr &message) { int row = m_entries.count(); diff --git a/linphone-desktop/src/components/chat/ChatModel.hpp b/linphone-desktop/src/components/chat/ChatModel.hpp index c34602818..e67a34fe8 100644 --- a/linphone-desktop/src/components/chat/ChatModel.hpp +++ b/linphone-desktop/src/components/chat/ChatModel.hpp @@ -117,6 +117,7 @@ private: void removeEntry (ChatEntryData &pair); + void insertCall (const std::shared_ptr &call_log); void insertMessageAtEnd (const std::shared_ptr &message); void resetMessagesCount ();