From ff733ab3066ce8c0c3f4994c4940b9fd45672d24 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 11 Jan 2017 11:53:56 +0100 Subject: [PATCH] feat(src/components/chat/ChatModel): mark messages as read when it is necessary --- tests/src/components/chat/ChatModel.cpp | 17 ++++++++++++++--- tests/src/components/chat/ChatModel.hpp | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/src/components/chat/ChatModel.cpp b/tests/src/components/chat/ChatModel.cpp index e945cc2b3..7cd5e95ba 100644 --- a/tests/src/components/chat/ChatModel.cpp +++ b/tests/src/components/chat/ChatModel.cpp @@ -14,13 +14,13 @@ using namespace std; // ============================================================================= class ChatModel::MessageHandlers : public linphone::ChatMessageListener { + friend class ChatModel; + public: MessageHandlers (ChatModel *chat_model) : m_chat_model(chat_model) {} ~MessageHandlers () = default; -private: - void onFileTransferRecv ( const shared_ptr &message, const shared_ptr &content, @@ -48,6 +48,9 @@ private: } void onMsgStateChanged (const shared_ptr &message, linphone::ChatMessageState state) override { + if (!m_chat_model) + return; + ChatModel &chat = *m_chat_model; auto it = find_if(chat.m_entries.begin(), chat.m_entries.end(), [&message](const ChatEntryData &pair) { @@ -62,6 +65,7 @@ private: emit chat.dataChanged(chat.index(row, 0), chat.index(row, 0)); } +private: ChatModel *m_chat_model; }; @@ -82,12 +86,18 @@ ChatModel::ChatModel (QObject *parent) : QAbstractListModel(parent) { const shared_ptr &room, const shared_ptr &message ) { - if (m_chat_room == room) + if (m_chat_room == room) { insertMessageAtEnd(message); + m_chat_room->markAsRead(); + } } ); } +ChatModel::~ChatModel () { + m_message_handlers->m_chat_model = nullptr; +} + QHash ChatModel::roleNames () const { QHash roles; roles[Roles::ChatEntry] = "$chatEntry"; @@ -162,6 +172,7 @@ void ChatModel::setSipAddress (const QString &sip_address) { string std_sip_address = ::Utils::qStringToLinphoneString(sip_address); m_chat_room = core->getChatRoomFromUri(std_sip_address); + m_chat_room->markAsRead(); // Get messages. for (auto &message : m_chat_room->getHistory(0)) { diff --git a/tests/src/components/chat/ChatModel.hpp b/tests/src/components/chat/ChatModel.hpp index e89a61de3..5d4d36606 100644 --- a/tests/src/components/chat/ChatModel.hpp +++ b/tests/src/components/chat/ChatModel.hpp @@ -50,7 +50,7 @@ public: Q_ENUM(MessageStatus); ChatModel (QObject *parent = Q_NULLPTR); - ~ChatModel () = default; + ~ChatModel (); int rowCount (const QModelIndex &index = QModelIndex()) const override;