1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Create interfaces for ContentDialog and ContenDialogManager

This commit is contained in:
Diadlo 2019-06-19 12:34:03 +03:00
parent c2bdcdf6c2
commit ac36ed8180
10 changed files with 121 additions and 43 deletions

View File

@ -336,6 +336,7 @@ set(${PROJECT_NAME}_SOURCES
src/model/profile/iprofileinfo.h src/model/profile/iprofileinfo.h
src/model/profile/profileinfo.cpp src/model/profile/profileinfo.cpp
src/model/profile/profileinfo.h src/model/profile/profileinfo.h
src/model/dialogs/idialogs.h
src/net/bootstrapnodeupdater.cpp src/net/bootstrapnodeupdater.cpp
src/net/bootstrapnodeupdater.h src/net/bootstrapnodeupdater.h
src/net/avatarbroadcaster.cpp src/net/avatarbroadcaster.cpp

View File

@ -0,0 +1,39 @@
/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre 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 3 of the License, or
(at your option) any later version.
qTox 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 qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef I_DIALOGS_H
#define I_DIALOGS_H
class ContactId;
class GroupId;
class ToxPk;
class IDialogs
{
public:
virtual bool hasContact(const ContactId& contactId) const = 0;
virtual bool isContactActive(const ContactId& contactId) const = 0;
virtual void removeFriend(const ToxPk& friendPk) = 0;
virtual void removeGroup(const GroupId& groupId) = 0;
virtual int chatroomCount() const = 0;
};
#endif // I_DIALOGS_H

View File

@ -0,0 +1,35 @@
/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre 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 3 of the License, or
(at your option) any later version.
qTox 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 qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef I_DIALOGS_MANAGER
#define I_DIALOGS_MANAGER
#include "idialogs.h"
class GroupId;
class ToxPk;
class IDialogsManager
{
public:
virtual IDialogs* getFriendDialogs(const ToxPk& friendPk) const = 0;
virtual IDialogs* getGroupDialogs(const GroupId& groupId) const = 0;
};
#endif // I_DIALOGS_MANAGER

View File

@ -202,7 +202,7 @@ void ContentDialog::removeFriend(const ToxPk& friendPk)
chatroomWidget->deleteLater(); chatroomWidget->deleteLater();
if (chatroomWidgetCount() == 0) { if (chatroomCount() == 0) {
contentLayout->clear(); contentLayout->clear();
activeChatroomWidget = nullptr; activeChatroomWidget = nullptr;
deleteLater(); deleteLater();
@ -226,7 +226,7 @@ void ContentDialog::removeGroup(const GroupId& groupId)
groupLayout.removeSortedWidget(chatroomWidget); groupLayout.removeSortedWidget(chatroomWidget);
chatroomWidget->deleteLater(); chatroomWidget->deleteLater();
if (chatroomWidgetCount() == 0) { if (chatroomCount() == 0) {
contentLayout->clear(); contentLayout->clear();
activeChatroomWidget = nullptr; activeChatroomWidget = nullptr;
deleteLater(); deleteLater();
@ -246,7 +246,7 @@ void ContentDialog::closeIfEmpty()
} }
} }
int ContentDialog::chatroomWidgetCount() const int ContentDialog::chatroomCount() const
{ {
return friendLayout->friendTotalCount() + groupLayout.getLayout()->count(); return friendLayout->friendTotalCount() + groupLayout.getLayout()->count();
} }
@ -469,7 +469,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
ToxPk friendId = contact->getPublicKey(); ToxPk friendId = contact->getPublicKey();
// If friend is already in a dialog then you can't drop friend where it already is. // If friend is already in a dialog then you can't drop friend where it already is.
if (!hasContactWidget(friendId)) { if (!hasContact(friendId)) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
} else if (group) { } else if (group) {
@ -480,7 +480,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return; return;
} }
if (!hasContactWidget(groupId)) { if (!hasContact(groupId)) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
} }
@ -586,11 +586,6 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
updateTitleAndStatusIcon(); updateTitleAndStatusIcon();
} }
bool ContentDialog::containsContact(const ContactId& contactId) const
{
return contactWidgets.contains(contactId);
}
void ContentDialog::updateFriendStatus(const ToxPk& friendPk, Status::Status status) void ContentDialog::updateFriendStatus(const ToxPk& friendPk, Status::Status status)
{ {
auto widget = qobject_cast<FriendWidget*>(contactWidgets.value(friendPk)); auto widget = qobject_cast<FriendWidget*>(contactWidgets.value(friendPk));
@ -605,7 +600,7 @@ void ContentDialog::updateContactStatusLight(const ContactId& contactId)
} }
} }
bool ContentDialog::isContactWidgetActive(const ContactId& contactId) bool ContentDialog::isContactActive(const ContactId& contactId) const
{ {
auto widget = contactWidgets.value(contactId); auto widget = contactWidgets.value(contactId);
if (widget == nullptr) { if (widget == nullptr) {
@ -674,7 +669,7 @@ void ContentDialog::saveSplitterState()
Settings::getInstance().setDialogSplitterState(splitter->saveState()); Settings::getInstance().setDialogSplitterState(splitter->saveState());
} }
bool ContentDialog::hasContactWidget(const ContactId& contactId) const bool ContentDialog::hasContact(const ContactId& contactId) const
{ {
return contactWidgets.contains(contactId); return contactWidgets.contains(contactId);
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright © 2015-2018 by The qTox Project Contributors Copyright © 2015-2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox. This file is part of qTox, a Qt-based graphical interface for Tox.
@ -20,6 +20,7 @@
#ifndef CONTENTDIALOG_H #ifndef CONTENTDIALOG_H
#define CONTENTDIALOG_H #define CONTENTDIALOG_H
#include "src/model/dialogs/idialogs.h"
#include "src/widget/genericchatitemlayout.h" #include "src/widget/genericchatitemlayout.h"
#include "src/widget/tool/activatedialog.h" #include "src/widget/tool/activatedialog.h"
#include "src/model/status.h" #include "src/model/status.h"
@ -27,14 +28,10 @@
#include "src/core/toxpk.h" #include "src/core/toxpk.h"
#include <memory> #include <memory>
#include <tuple>
template <typename K, typename V> template <typename K, typename V>
class QHash; class QHash;
template <typename T>
class QSet;
class ContentDialog;
class ContentLayout; class ContentLayout;
class Friend; class Friend;
class FriendChatroom; class FriendChatroom;
@ -47,11 +44,8 @@ class GroupChatroom;
class GroupWidget; class GroupWidget;
class QCloseEvent; class QCloseEvent;
class QSplitter; class QSplitter;
class QVBoxLayout;
using ContactInfo = std::tuple<ContentDialog*, GenericChatroomWidget*>; class ContentDialog : public ActivateDialog, public IDialogs
class ContentDialog : public ActivateDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -60,9 +54,9 @@ public:
FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form); FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form); GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
void removeFriend(const ToxPk& friendPk); void removeFriend(const ToxPk& friendPk) override;
void removeGroup(const GroupId& groupId); void removeGroup(const GroupId& groupId) override;
int chatroomWidgetCount() const; int chatroomCount() const override;
void ensureSplitterVisible(); void ensureSplitterVisible();
void updateTitleAndStatusIcon(); void updateTitleAndStatusIcon();
@ -73,12 +67,12 @@ public:
void addFriendWidget(FriendWidget* widget, Status::Status status); void addFriendWidget(FriendWidget* widget, Status::Status status);
bool isActiveWidget(GenericChatroomWidget* widget); bool isActiveWidget(GenericChatroomWidget* widget);
bool hasContactWidget(const ContactId& contactId) const; bool hasContact(const ContactId& contactId) const override;
bool isContactActive(const ContactId& contactId) const override;
void focusContact(const ContactId& friendPk); void focusContact(const ContactId& friendPk);
bool containsContact(const ContactId& friendPk) const;
void updateFriendStatus(const ToxPk& friendPk, Status::Status status); void updateFriendStatus(const ToxPk& friendPk, Status::Status status);
void updateContactStatusLight(const ContactId& contactId); void updateContactStatusLight(const ContactId& contactId);
bool isContactWidgetActive(const ContactId& contactId);
void setStatusMessage(const ToxPk& friendPk, const QString& message); void setStatusMessage(const ToxPk& friendPk, const QString& message);

View File

@ -37,7 +37,7 @@ bool ContentDialogManager::contactWidgetExists(const ContactId& contactId)
return false; return false;
} }
return dialog->containsContact(contactId); return dialog->hasContact(contactId);
} }
FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog, FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
@ -109,7 +109,7 @@ void ContentDialogManager::updateFriendStatus(const ToxPk& friendPk)
} }
dialog->updateContactStatusLight(friendPk); dialog->updateContactStatusLight(friendPk);
if (dialog->isContactWidgetActive(friendPk)) { if (dialog->isContactActive(friendPk)) {
dialog->updateTitleAndStatusIcon(); dialog->updateTitleAndStatusIcon();
} }
@ -125,19 +125,19 @@ void ContentDialogManager::updateGroupStatus(const GroupId& groupId)
} }
dialog->updateContactStatusLight(groupId); dialog->updateContactStatusLight(groupId);
if (dialog->isContactWidgetActive(groupId)) { if (dialog->isContactActive(groupId)) {
dialog->updateTitleAndStatusIcon(); dialog->updateTitleAndStatusIcon();
} }
} }
bool ContentDialogManager::isContactWidgetActive(const ContactId& contactId) bool ContentDialogManager::isContactActive(const ContactId& contactId)
{ {
const auto dialog = contactDialogs.value(contactId); const auto dialog = contactDialogs.value(contactId);
if (dialog == nullptr) { if (dialog == nullptr) {
return false; return false;
} }
return dialog->isContactWidgetActive(contactId); return dialog->isContactActive(contactId);
} }
ContentDialog* ContentDialogManager::getFriendDialog(const ToxPk& friendPk) const ContentDialog* ContentDialogManager::getFriendDialog(const ToxPk& friendPk) const
@ -181,3 +181,13 @@ void ContentDialogManager::onDialogClose()
removeDialog(dialog, contactDialogs); removeDialog(dialog, contactDialogs);
} }
IDialogs* ContentDialogManager::getFriendDialogs(const ToxPk& friendPk) const
{
return getFriendDialog(friendPk);
}
IDialogs* ContentDialogManager::getGroupDialogs(const GroupId& groupId) const
{
return getGroupDialog(groupId);
}

View File

@ -1,5 +1,5 @@
/* /*
Copyright © 2018 by The qTox Project Contributors Copyright © 2018-2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox. This file is part of qTox, a Qt-based graphical interface for Tox.
@ -23,6 +23,7 @@
#include "src/core/contactid.h" #include "src/core/contactid.h"
#include "src/core/toxpk.h" #include "src/core/toxpk.h"
#include "src/core/groupid.h" #include "src/core/groupid.h"
#include "src/model/dialogs/idialogsmanager.h"
#include "contentdialog.h" #include "contentdialog.h"
#include <QObject> #include <QObject>
@ -30,7 +31,7 @@
/** /**
* @breaf Manage all content dialogs * @breaf Manage all content dialogs
*/ */
class ContentDialogManager : public QObject class ContentDialogManager : public QObject, public IDialogsManager
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -39,10 +40,13 @@ public:
void focusContact(const ContactId& contactId); void focusContact(const ContactId& contactId);
void updateFriendStatus(const ToxPk& friendPk); void updateFriendStatus(const ToxPk& friendPk);
void updateGroupStatus(const GroupId& friendPk); void updateGroupStatus(const GroupId& friendPk);
bool isContactWidgetActive(const ContactId& contactId); bool isContactActive(const ContactId& contactId);
ContentDialog* getFriendDialog(const ToxPk& friendPk) const; ContentDialog* getFriendDialog(const ToxPk& friendPk) const;
ContentDialog* getGroupDialog(const GroupId& friendPk) const; ContentDialog* getGroupDialog(const GroupId& friendPk) const;
IDialogs* getFriendDialogs(const ToxPk& friendPk) const;
IDialogs* getGroupDialogs(const GroupId& groupId) const;
FriendWidget* addFriendToDialog(ContentDialog* dialog, std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form); FriendWidget* addFriendToDialog(ContentDialog* dialog, std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroupToDialog(ContentDialog* dialog, std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form); GroupWidget* addGroupToDialog(ContentDialog* dialog, std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);

View File

@ -110,13 +110,13 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
const auto contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk); const auto contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk);
// TODO: move to model // TODO: move to model
if (!contentDialog || contentDialog->chatroomWidgetCount() > 1) { if (!contentDialog || contentDialog->chatroomCount() > 1) {
const auto openChatWindow = menu.addAction(tr("Open chat in new window")); const auto openChatWindow = menu.addAction(tr("Open chat in new window"));
connect(openChatWindow, &QAction::triggered, [=]() { emit newWindowOpened(this); }); connect(openChatWindow, &QAction::triggered, [=]() { emit newWindowOpened(this); });
} }
// TODO: move to model // TODO: move to model
if (contentDialog && contentDialog->hasContactWidget(friendPk)) { if (contentDialog && contentDialog->hasContact(friendPk)) {
const auto removeChatWindow = menu.addAction(tr("Remove chat from this window")); const auto removeChatWindow = menu.addAction(tr("Remove chat from this window"));
connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow); connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow);
} }
@ -171,7 +171,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
menu.addSeparator(); menu.addSeparator();
// TODO: move to model // TODO: move to model
if (!contentDialog || !contentDialog->hasContactWidget(friendPk)) { if (!contentDialog || !contentDialog->hasContact(friendPk)) {
const auto removeAction = const auto removeAction =
menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist")); menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendPk); }, connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendPk); },

View File

@ -90,13 +90,13 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
// TODO: Move to model // TODO: Move to model
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId); ContentDialog* contentDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
const bool notAlone = contentDialog != nullptr && contentDialog->chatroomWidgetCount() > 1; const bool notAlone = contentDialog != nullptr && contentDialog->chatroomCount() > 1;
if (contentDialog == nullptr || notAlone) { if (contentDialog == nullptr || notAlone) {
openChatWindow = menu.addAction(tr("Open chat in new window")); openChatWindow = menu.addAction(tr("Open chat in new window"));
} }
if (contentDialog && contentDialog->hasContactWidget(groupId)) { if (contentDialog && contentDialog->hasContact(groupId)) {
removeChatWindow = menu.addAction(tr("Remove chat from this window")); removeChatWindow = menu.addAction(tr("Remove chat from this window"));
} }

View File

@ -1353,7 +1353,7 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b
if (contentDialog != nullptr) { if (contentDialog != nullptr) {
currentWindow = contentDialog->window(); currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(friendId); hasActive = ContentDialogManager::getInstance()->isContactActive(friendId);
} else { } else {
if (settings.getSeparateWindow() && settings.getShowWindow()) { if (settings.getSeparateWindow() && settings.getShowWindow()) {
if (settings.getDontGroupWindows()) { if (settings.getDontGroupWindows()) {
@ -1367,7 +1367,7 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b
addFriendDialog(f, contentDialog); addFriendDialog(f, contentDialog);
currentWindow = contentDialog->window(); currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(friendId); hasActive = ContentDialogManager::getInstance()->isContactActive(friendId);
} else { } else {
currentWindow = window(); currentWindow = window();
FriendWidget* widget = friendWidgets[friendId]; FriendWidget* widget = friendWidgets[friendId];
@ -1416,7 +1416,7 @@ bool Widget::newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk,
if (contentDialog != nullptr) { if (contentDialog != nullptr) {
currentWindow = contentDialog->window(); currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(groupId); hasActive = ContentDialogManager::getInstance()->isContactActive(groupId);
} else { } else {
currentWindow = window(); currentWindow = window();
hasActive = widget == activeChatroomWidget; hasActive = widget == activeChatroomWidget;