diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f470b4f7..0f5533146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ set(SOURCES src/components/contacts/ContactsListProxyModel.cpp src/components/core/CoreHandlers.cpp src/components/core/CoreManager.cpp + src/components/core/MessagesCountNotifier.cpp src/components/notifier/Notifier.cpp src/components/other/colors/Colors.cpp src/components/other/clipboard/Clipboard.cpp @@ -181,6 +182,7 @@ set(HEADERS src/components/contacts/ContactsListProxyModel.hpp src/components/core/CoreHandlers.hpp src/components/core/CoreManager.hpp + src/components/core/MessagesCountNotifier.hpp src/components/notifier/Notifier.hpp src/components/other/colors/Colors.hpp src/components/other/clipboard/Clipboard.hpp diff --git a/src/components/chat/ChatModel.cpp b/src/components/chat/ChatModel.cpp index 01a17723c..0225198a6 100644 --- a/src/components/chat/ChatModel.cpp +++ b/src/components/chat/ChatModel.cpp @@ -272,9 +272,6 @@ void ChatModel::setSipAddress (const QString &sipAddress) { handleIsComposingChanged(mChatRoom); - if (mChatRoom->getUnreadMessagesCount() > 0) - resetMessagesCount(); - // Get messages. for (auto &message : mChatRoom->getHistory(0)) { QVariantMap map; @@ -461,6 +458,13 @@ void ChatModel::compose () { mChatRoom->compose(); } +void ChatModel::resetMessagesCount () { + if (mChatRoom->getUnreadMessagesCount() > 0) { + mChatRoom->markAsRead(); + emit messagesCountReset(); + } +} + // ----------------------------------------------------------------------------- const ChatModel::ChatEntryData ChatModel::getFileMessageEntry (int id) { @@ -617,11 +621,6 @@ void ChatModel::insertMessageAtEnd (const shared_ptr &mes endInsertRows(); } -void ChatModel::resetMessagesCount () { - mChatRoom->markAsRead(); - emit messagesCountReset(); -} - // ----------------------------------------------------------------------------- void ChatModel::handleCallStateChanged (const shared_ptr &call, linphone::CallState state) { @@ -645,8 +644,6 @@ void ChatModel::handleIsComposingChanged (const shared_ptr & void ChatModel::handleMessageReceived (const shared_ptr &message) { if (mChatRoom == message->getChatRoom()) { insertMessageAtEnd(message); - resetMessagesCount(); - emit messageReceived(message); } } diff --git a/src/components/chat/ChatModel.hpp b/src/components/chat/ChatModel.hpp index 02a143b26..d09c2b2c6 100644 --- a/src/components/chat/ChatModel.hpp +++ b/src/components/chat/ChatModel.hpp @@ -106,6 +106,8 @@ public: void compose (); + void resetMessagesCount (); + signals: bool isRemoteComposingChanged (bool status); @@ -132,8 +134,6 @@ private: void insertCall (const std::shared_ptr &callLog); void insertMessageAtEnd (const std::shared_ptr &message); - void resetMessagesCount (); - void handleCallStateChanged (const std::shared_ptr &call, linphone::CallState state); void handleIsComposingChanged (const std::shared_ptr &chatRoom); void handleMessageReceived (const std::shared_ptr &message); diff --git a/src/components/chat/ChatProxyModel.cpp b/src/components/chat/ChatProxyModel.cpp index 743837636..83db8b545 100644 --- a/src/components/chat/ChatProxyModel.cpp +++ b/src/components/chat/ChatProxyModel.cpp @@ -159,6 +159,8 @@ void ChatProxyModel::setSipAddress (const QString &sipAddress) { } mChatModel = CoreManager::getInstance()->getChatModelFromSipAddress(sipAddress); + mChatModel->resetMessagesCount(); + if (mChatModel) { ChatModel *chatModel = mChatModel.get(); QObject::connect(chatModel, &ChatModel::isRemoteComposingChanged, this, &ChatProxyModel::handleIsRemoteComposingChanged); @@ -181,6 +183,7 @@ void ChatProxyModel::handleIsRemoteComposingChanged (bool status) { void ChatProxyModel::handleMessageReceived (const shared_ptr &) { mMaxDisplayedEntries++; + mChatModel->resetMessagesCount(); } void ChatProxyModel::handleMessageSent (const shared_ptr &) { diff --git a/src/components/core/CoreManager.cpp b/src/components/core/CoreManager.cpp index 2ceca3cb7..1774c3584 100644 --- a/src/components/core/CoreManager.cpp +++ b/src/components/core/CoreManager.cpp @@ -27,6 +27,7 @@ #include "../../app/paths/Paths.hpp" #include "../../utils/Utils.hpp" +#include "MessagesCountNotifier.hpp" #include "CoreManager.hpp" @@ -54,6 +55,8 @@ CoreManager::CoreManager (QObject *parent, const QString &configPath) : CoreHandlers *coreHandlers = mHandlers.get(); QObject::connect(coreHandlers, &CoreHandlers::coreStarted, this, [] { + new MessagesCountNotifier(mInstance); + mInstance->mCallsListModel = new CallsListModel(mInstance); mInstance->mContactsListModel = new ContactsListModel(mInstance); mInstance->mSipAddressesModel = new SipAddressesModel(mInstance); diff --git a/src/components/core/CoreManager.hpp b/src/components/core/CoreManager.hpp index f43086954..2de5786eb 100644 --- a/src/components/core/CoreManager.hpp +++ b/src/components/core/CoreManager.hpp @@ -127,7 +127,7 @@ signals: void coreCreated (); void coreStarted (); - void chatModelCreated (const std::shared_ptr chatModel); + void chatModelCreated (const std::shared_ptr &chatModel); void logsUploaded (const QString &url); diff --git a/src/components/core/MessagesCountNotifier.cpp b/src/components/core/MessagesCountNotifier.cpp new file mode 100644 index 000000000..cbbb5e50e --- /dev/null +++ b/src/components/core/MessagesCountNotifier.cpp @@ -0,0 +1,79 @@ +/* + * MessagesCountNotifier.cpp + * Copyright (C) 2017 Belledonne Communications, Grenoble, France + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Created on: June 29, 2017 + * Author: Ronan Abhamon + */ + +#include "../core/CoreManager.hpp" + +#include "MessagesCountNotifier.hpp" + +using namespace std; + +// ============================================================================= + +MessagesCountNotifier::MessagesCountNotifier (QObject *parent) : QObject(parent) { + CoreManager *coreManager = CoreManager::getInstance(); + QObject::connect( + coreManager, &CoreManager::chatModelCreated, + this, &MessagesCountNotifier::handleChatModelCreated + ); + QObject::connect( + coreManager->getHandlers().get(), &CoreHandlers::messageReceived, + this, &MessagesCountNotifier::handleMessageReceived + ); + + updateUnreadMessagesCount(); +} + +// ----------------------------------------------------------------------------- + +void MessagesCountNotifier::updateUnreadMessagesCount () { + mUnreadMessagesCount = 0; + for (const auto &chatRoom : CoreManager::getInstance()->getCore()->getChatRooms()) + mUnreadMessagesCount += chatRoom->getUnreadMessagesCount(); + + notifyUnreadMessagesCount(); +} + +void MessagesCountNotifier::notifyUnreadMessagesCount () { + qInfo() << QStringLiteral("Notify unread messages count: %1.").arg(mUnreadMessagesCount); + + #ifdef Q_OS_LINUX + // TODO. + #elif Q_OS_MACOS + // TODO. + #elif Q_OS_WIN + // TODO. + #endif // ifdef Q_OS_LINUX +} + +// ----------------------------------------------------------------------------- + +void MessagesCountNotifier::handleChatModelCreated (const shared_ptr &chatModel) { + QObject::connect( + chatModel.get(), &ChatModel::messagesCountReset, + this, &MessagesCountNotifier::updateUnreadMessagesCount + ); +} + +void MessagesCountNotifier::handleMessageReceived (const shared_ptr &) { + mUnreadMessagesCount++; + notifyUnreadMessagesCount(); +} diff --git a/src/components/core/MessagesCountNotifier.hpp b/src/components/core/MessagesCountNotifier.hpp new file mode 100644 index 000000000..0677bbe20 --- /dev/null +++ b/src/components/core/MessagesCountNotifier.hpp @@ -0,0 +1,50 @@ +/* + * MessagesCountNotifier.hpp + * Copyright (C) 2017 Belledonne Communications, Grenoble, France + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Created on: June 29, 2017 + * Author: Ronan Abhamon + */ + +#include + +#include + +// ============================================================================= + +namespace linphone { + class ChatMessage; +} + +class ChatModel; + +class MessagesCountNotifier : public QObject { + Q_OBJECT; + +public: + MessagesCountNotifier (QObject *parent = Q_NULLPTR); + ~MessagesCountNotifier () = default; + +private: + void updateUnreadMessagesCount (); + void notifyUnreadMessagesCount (); + + void handleChatModelCreated (const std::shared_ptr &chatModel); + void handleMessageReceived (const std::shared_ptr &message); + + int mUnreadMessagesCount = 0; +};