Commit d48a24c9 authored by Rafał Malinowski's avatar Rafał Malinowski

gui: extracted common code into new class ChatWidgetMessageHandler

Signed-off-by: Rafał Malinowski's avatarRafał Malinowski <rafal.przemyslaw.malinowski@gmail.com>
parent e5cb6512
......@@ -57,6 +57,7 @@
#include "gui/widgets/chat-top-bar-widget-factory-repository.h"
#include "gui/widgets/chat-widget/chat-widget-factory.h"
#include "gui/widgets/chat-widget/chat-widget-manager.h"
#include "gui/widgets/chat-widget/chat-widget-message-handler.h"
#include "gui/widgets/chat-widget/chat-widget-repository.h"
#include "gui/windows/buddy-data-window-repository.h"
#include "gui/windows/chat-data-window-repository.h"
......@@ -584,6 +585,10 @@ void Core::runServices()
CurrentChatWidgetRepository = new ChatWidgetRepository(this);
CurrentChatWidgetRepository->setChatWidgetFactory(CurrentChatWidgetFactory);
CurrentChatWidgetMessageHandler = new ChatWidgetMessageHandler(this);
CurrentChatWidgetMessageHandler->setChatWidgetRepository(CurrentChatWidgetRepository);
CurrentChatWidgetMessageHandler->setUnreadMessageRepository(CurrentUnreadMessageRepository);
CurrentChatWindowFactory = new ChatWindowFactory(this);
CurrentChatWindowFactory->setUnreadMessageRepository(CurrentUnreadMessageRepository);
......
......@@ -57,6 +57,7 @@ class ChatDataWindowRepository;
class ChatImageRequestService;
class ChatTopBarWidgetFactoryRepository;
class ChatWidgetFactory;
class ChatWidgetMessageHandler;
class ChatWidgetRepository;
class ChatWindowFactory;
class ChatWindowStorage;
......@@ -106,6 +107,7 @@ class KADUAPI Core : public QObject, private AccountsAwareObject, public Configu
UnreadMessageRepository *CurrentUnreadMessageRepository;
RosterNotifier *CurrentRosterNotifier;
ChatWidgetFactory *CurrentChatWidgetFactory;
ChatWidgetMessageHandler *CurrentChatWidgetMessageHandler;
ChatWidgetRepository *CurrentChatWidgetRepository;
ChatWindowFactory *CurrentChatWindowFactory;
ChatWindowStorage *CurrentChatWindowStorage;
......
......@@ -25,6 +25,7 @@ set (gui_SRCS
widgets/chat-widget/chat-widget-actions.cpp
widgets/chat-widget/chat-widget-factory.cpp
widgets/chat-widget/chat-widget-manager.cpp
widgets/chat-widget/chat-widget-message-handler.cpp
widgets/chat-widget/chat-widget-repository.cpp
widgets/group-tab-bar/group-filter-tab-data.cpp
......
......@@ -61,7 +61,7 @@ void RecentChatsAction::openRecentChats(QAction *action)
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(action->data().value<Chat>(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
#include "moc_recent-chats-action.cpp"
......@@ -489,7 +489,7 @@ void ChatWidgetActions::openChatActionActivated(QAction *sender, bool toggled)
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(action->context()->chat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
kdebugf2();
}
......
......@@ -43,7 +43,7 @@ public:
* Activates given chat widget in this ChatWidgetContainer. This container window will be
* activated if neccessary.
*/
virtual void activateChatWidget(ChatWidget *chatWidget) = 0;
virtual void tryActivateChatWidget(ChatWidget *chatWidget) = 0;
/**
* @short Send alert about given chat widget in this ChatWidgetContainer.
......
/*
* %kadu copyright begin%
* Copyright 2013 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "chat-widget-message-handler.h"
#include "gui/widgets/chat-widget/chat-widget.h"
#include "gui/widgets/chat-widget/chat-widget-repository.h"
#include "message/unread-message-repository.h"
ChatWidgetMessageHandler::ChatWidgetMessageHandler(QObject *parent) :
QObject(parent)
{
}
ChatWidgetMessageHandler::~ChatWidgetMessageHandler()
{
}
void ChatWidgetMessageHandler::setChatWidgetRepository(ChatWidgetRepository *chatWidgetRepository)
{
m_chatWidgetRepository = chatWidgetRepository;
if (!m_chatWidgetRepository)
return;
connect(m_chatWidgetRepository.data(), SIGNAL(chatWidgetCreated(ChatWidget*)), this, SLOT(chatWidgetCreated(ChatWidget*)));
connect(m_chatWidgetRepository.data(), SIGNAL(chatWidgetDestroyed(ChatWidget*)), this, SLOT(chatWidgetDestroyed(ChatWidget*)));
foreach (auto chatWidget, m_chatWidgetRepository.data()->widgets())
chatWidgetCreated(chatWidget);
}
void ChatWidgetMessageHandler::setUnreadMessageRepository(UnreadMessageRepository *unreadMessageRepository)
{
m_unreadMessageRepository = unreadMessageRepository;
}
void ChatWidgetMessageHandler::chatWidgetCreated(ChatWidget *chatWidget)
{
connect(chatWidget, SIGNAL(activated(ChatWidget*)), this, SLOT(chatWidgetActivated(ChatWidget*)));
}
void ChatWidgetMessageHandler::chatWidgetDestroyed(ChatWidget *chatWidget)
{
disconnect(chatWidget, SIGNAL(activated(ChatWidget*)), this, SLOT(chatWidgetActivated(ChatWidget*)));
}
void ChatWidgetMessageHandler::chatWidgetActivated(ChatWidget *chatWidget)
{
if (!m_unreadMessageRepository)
return;
auto chat = chatWidget->chat();
auto messages = m_unreadMessageRepository.data()->unreadMessagesForChat(chat);
m_unreadMessageRepository.data()->markMessagesAsRead(messages);
}
/*
* %kadu copyright begin%
* Copyright 2013 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QtCore/QObject>
#include <QtCore/QWeakPointer>
class ChatWidget;
class ChatWidgetRepository;
class UnreadMessageRepository;
class ChatWidgetMessageHandler : public QObject
{
Q_OBJECT
public:
explicit ChatWidgetMessageHandler(QObject *parent = nullptr);
virtual ~ChatWidgetMessageHandler();
void setChatWidgetRepository(ChatWidgetRepository *chatWidgetRepository);
void setUnreadMessageRepository(UnreadMessageRepository *unreadMessageRepository);
private:
QWeakPointer<ChatWidgetRepository> m_chatWidgetRepository;
QWeakPointer<UnreadMessageRepository> m_unreadMessageRepository;
private slots:
void chatWidgetCreated(ChatWidget *chatWidget);
void chatWidgetDestroyed(ChatWidget *chatWidget);
void chatWidgetActivated(ChatWidget *chatWidget);
};
......@@ -682,10 +682,15 @@ Protocol *ChatWidget::currentProtocol() const
return CurrentChat.chatAccount().protocolHandler();
}
void ChatWidget::activate()
void ChatWidget::tryActivate()
{
if (Container)
Container->activateChatWidget(this);
Container->tryActivateChatWidget(this);
}
void ChatWidget::markActive()
{
emit activated(this);
}
bool ChatWidget::isActive()
......
......@@ -170,7 +170,8 @@ public slots:
void colorSelectorAboutToClose();
void clearChatWindow();
void activate();
void tryActivate();
void markActive();
/**
* @author Rafal 'Vogel' Malinowski
......@@ -189,6 +190,8 @@ signals:
void widgetDestroyed(Chat chat);
void widgetDestroyed(ChatWidget *widget);
void activated(ChatWidget *chatWidget);
void closed();
};
......
......@@ -279,7 +279,7 @@ void AddConferenceWindow::start()
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(computeChat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
QDialog::accept();
}
......
......@@ -230,7 +230,7 @@ void AddRoomChatWindow::start()
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(computeChat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
ChatDetailsRoom *details = qobject_cast<ChatDetailsRoom *>(chat.details());
Q_ASSERT(details);
......
......@@ -236,12 +236,7 @@ void ChatWindow::changeEvent(QEvent *event)
kdebugf();
if (_isActiveWindow(this))
{
if (m_unreadMessageRepository)
{
auto messages = m_unreadMessageRepository.data()->unreadMessagesForChat(m_chatWidget->chat());
m_unreadMessageRepository.data()->markMessagesAsRead(messages);
}
m_chatWidget->markActive();
setWindowTitle(m_chatWidget->title());
m_titleTimer->stop();
}
......@@ -256,7 +251,7 @@ void ChatWindow::setWindowTitle(QString title)
QWidget::setWindowTitle(title.replace(QLatin1String("[*]"), QLatin1String("[*][*]")));
}
void ChatWindow::activateChatWidget(ChatWidget *chatWidget)
void ChatWindow::tryActivateChatWidget(ChatWidget *chatWidget)
{
Q_UNUSED(chatWidget)
Q_ASSERT(chatWidget == m_chatWidget);
......@@ -272,11 +267,7 @@ void ChatWindow::alertChatWidget(ChatWidget *chatWidget)
if (isChatWidgetActive(chatWidget))
{
if (m_unreadMessageRepository)
{
auto messages = m_unreadMessageRepository.data()->unreadMessagesForChat(m_chatWidget->chat());
m_unreadMessageRepository.data()->markMessagesAsRead(messages);
}
m_chatWidget->markActive();
return;
}
......
......@@ -54,7 +54,7 @@ public:
Chat chat() const;
ChatWidget * chatWidget() const { return m_chatWidget; }
virtual void activateChatWidget(ChatWidget *chatWidget) override;
virtual void tryActivateChatWidget(ChatWidget *chatWidget) override;
virtual void alertChatWidget(ChatWidget *chatWidget) override;
virtual void closeChatWidget(ChatWidget *chatWidget) override;
virtual bool isChatWidgetActive(ChatWidget *chatWidget) override;
......
......@@ -299,7 +299,7 @@ void KaduWindow::talkableActivatedSlot(const Talkable &talkable)
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
return;
}
......
......@@ -227,7 +227,7 @@ void OpenChatWith::itemActivated(int index)
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
deleteLater();
}
......
......@@ -340,7 +340,7 @@ void SearchWindow::chatFound()
: ChatTypeContactSet::findChat(contacts, ActionCreateAndAdd);
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
}
......
......@@ -41,7 +41,7 @@ void ChatNotification::openChat()
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
void ChatNotification::callbackAccept()
......
......@@ -402,7 +402,7 @@ void DockingManager::openUnreadMessages()
const Message &message = Core::instance()->unreadMessageRepository()->unreadMessage();
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(message.messageChat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
void DockingManager::trayMousePressEvent(QMouseEvent * e)
......
......@@ -74,7 +74,7 @@ void GaduUrlHandler::openUrl(const QByteArray &url, bool disableMenu)
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
return;
}
}
......@@ -113,7 +113,7 @@ void GaduUrlHandler::accountSelected(QAction *action)
const Chat &chat = ChatTypeContact::findChat(contact, ActionCreateAndAdd);
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
#include "moc_gadu-url-handler.cpp"
......@@ -378,7 +378,7 @@ void HintManager::openChat(Hint *hint)
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(hint->chat(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
deleteHintAndUpdate(hint);
......
......@@ -93,7 +93,7 @@ void JabberUrlHandler::openUrl(const QByteArray &url, bool disableMenu)
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
return;
}
}
......@@ -132,7 +132,7 @@ void JabberUrlHandler::accountSelected(QAction *action)
const Chat &chat = ChatTypeContact::findChat(contact, ActionCreateAndAdd);
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
#include "moc_jabber-url-handler.cpp"
......@@ -136,7 +136,7 @@ void Qt4Notify::messageClicked()
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
NotifierConfigurationWidget *Qt4Notify::createConfigurationWidget(QWidget *parent)
......
......@@ -180,8 +180,7 @@ void SingleWindow::changeEvent(QEvent *event)
ChatWidget *chatWidget = static_cast<ChatWidget *>(m_tabs->currentWidget());
if (chatWidget && _isActiveWindow(this))
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
updateTabIcon(chatWidget);
updateTabName(chatWidget);
}
......@@ -305,7 +304,7 @@ void SingleWindow::resizeEvent(QResizeEvent *event)
m_split->resize(newSize);
}
void SingleWindow::activateChatWidget(ChatWidget *chatWidget)
void SingleWindow::tryActivateChatWidget(ChatWidget *chatWidget)
{
int index = m_tabs->indexOf(chatWidget);
if (index < 0)
......@@ -323,8 +322,7 @@ void SingleWindow::alertChatWidget(ChatWidget *chatWidget)
if (isChatWidgetActive(chatWidget))
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
return;
}
......@@ -353,10 +351,7 @@ void SingleWindow::onTabChange(int index)
return;
ChatWidget *chatWidget = (ChatWidget *)m_tabs->widget(index);
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
updateTabIcon(chatWidget);
updateTabName(chatWidget);
}
......
......@@ -28,7 +28,7 @@ public:
virtual void changeEvent(QEvent *event);
virtual void activateChatWidget(ChatWidget *chatWidget);
virtual void tryActivateChatWidget(ChatWidget *chatWidget);
virtual void alertChatWidget(ChatWidget *chatWidget);
virtual void closeChatWidget(ChatWidget *chatWidget);
virtual bool isChatWidgetActive(ChatWidget *chatWidget);
......
......@@ -190,7 +190,7 @@ void TabWidget::tabsMenuSelected(QAction *action)
tabBar()->setCurrentIndex(action->data().toInt());
}
void TabWidget::activateChatWidget(ChatWidget *chatWidget)
void TabWidget::tryActivateChatWidget(ChatWidget *chatWidget)
{
int index = indexOf(chatWidget);
if (index < 0)
......@@ -208,8 +208,7 @@ void TabWidget::alertChatWidget(ChatWidget *chatWidget)
if (isChatWidgetActive(chatWidget))
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
return;
}
......@@ -429,10 +428,7 @@ void TabWidget::changeEvent(QEvent *event)
kdebugf();
ChatWidget *chatWidget = static_cast<ChatWidget *>(currentWidget());
if (chatWidget && _isActiveWindow(this))
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
}
chatWidget->markActive();
kdebugf2();
}
}
......@@ -467,7 +463,7 @@ void TabWidget::openRecentChat(QAction *action)
{
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(action->data().value<Chat>(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
}
void TabWidget::deleteTab()
......
......@@ -171,7 +171,7 @@ public:
*/
void configurationUpdated();
virtual void activateChatWidget(ChatWidget *chatWidget);
virtual void tryActivateChatWidget(ChatWidget *chatWidget);
virtual void alertChatWidget(ChatWidget *chatWidget);
virtual bool isChatWidgetActive(ChatWidget *chatWidget);
......
......@@ -283,8 +283,7 @@ void TabsManager::onTabChange(int index)
const Chat &chat = chatWidget->chat();
if (chat.unreadMessagesCount() > 0)
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chat);
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
updateTabName(chatWidget);
removeChatWidgetFromChatWidgetsWithMessage(chatWidget);
......@@ -424,8 +423,7 @@ void TabsManager::onTimer()
{
if (currentChatWidget == chatWidget)
{
auto messages = Core::instance()->unreadMessageRepository()->unreadMessagesForChat(chatWidget->chat());
Core::instance()->unreadMessageRepository()->markMessagesAsRead(messages);
chatWidget->markActive();
removeChatWidgetFromChatWidgetsWithMessage(chatWidget);
}
......@@ -585,7 +583,7 @@ bool TabsManager::detachChat(ChatWidget *chatWidget)
chat.addProperty("tabs:detached", true, CustomProperties::Storable);
ChatWidget * const detachedChatWidget = ChatWidgetManager::instance()->byChat(chat, true);
if (detachedChatWidget)
detachedChatWidget->activate();
detachedChatWidget->tryActivate();
return true;
}
......@@ -788,7 +786,7 @@ void TabsManager::reopenClosedChat()
ChatWidget * const chatWidget = ChatWidgetManager::instance()->byChat(ClosedChats.takeFirst(), true);
if (chatWidget)
chatWidget->activate();
chatWidget->tryActivate();
ReopenClosedTabMenuAction->setEnabled(ClosedChats.isEmpty());
}
......
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