Skip to content

Commit

Permalink
refactor(GUI): Move remaining messsage box functionality to new class
Browse files Browse the repository at this point in the history
* Pass MessageBoxManager instantiation around instead of relying on a singleton
* Mock MessageBoxManager for unit tests when needed, since they don't have a
  QApplication which is required for creating QWidgets
* Remove GUI class, which didn't have a clear purpose
  • Loading branch information
anthonybilinski committed Mar 24, 2022
1 parent 3bd8545 commit b24faab
Show file tree
Hide file tree
Showing 111 changed files with 973 additions and 755 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ set(${PROJECT_NAME}_SOURCES
src/widget/genericchatroomwidget.h
src/widget/groupwidget.cpp
src/widget/groupwidget.h
src/widget/gui.cpp
src/widget/gui.h
src/widget/loginscreen.cpp
src/widget/loginscreen.h
src/widget/maskablepixmapwidget.cpp
Expand All @@ -470,6 +468,10 @@ set(${PROJECT_NAME}_SOURCES
src/widget/tool/activatedialog.h
src/widget/tool/adjustingscrollarea.cpp
src/widget/tool/adjustingscrollarea.h
src/widget/tool/imessageboxmanager.h
src/widget/tool/imessageboxmanager.cpp
src/widget/tool/messageboxmanager.cpp
src/widget/tool/messageboxmanager.h
src/widget/tool/callconfirmwidget.cpp
src/widget/tool/callconfirmwidget.h
src/widget/tool/chattextedit.cpp
Expand Down
1 change: 0 additions & 1 deletion doc/coding_standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ The following example demonstrates the above include rules:
#include "persistence/profile.h"
#include "persistence/profilelocker.h"
#include "persistence/settings.h"
#include "widget/gui.h"

#include <QCoreApplication>
#include <QThread>
Expand Down
10 changes: 5 additions & 5 deletions src/chatlog/chatmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
return msg;
}

ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile,
ToxFile file, bool isMe, const QDateTime& date,
DocumentCache& documentCache,
Settings& settings, Style& style)
ChatMessage::Ptr ChatMessage::createFileTransferMessage(
const QString& sender, CoreFile& coreFile, ToxFile file, bool isMe,
const QDateTime& date, DocumentCache& documentCache, Settings& settings,
Style& style, IMessageBoxManager& messageBoxManager)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache, settings, style));

Expand All @@ -189,7 +189,7 @@ ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, C
msg->addColumn(new Text(documentCache, settings, style, sender, authorFont, true),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(
nullptr, coreFile, file, settings, style), 320, 0.6f),
nullptr, coreFile, file, settings, style, messageBoxManager), 320, 0.6f),
ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Timestamp(date, settings.getTimestampFormat(), baseFont,
documentCache, settings, style),
Expand Down
3 changes: 2 additions & 1 deletion src/chatlog/chatmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DocumentCache;
class SmileyPack;
class Settings;
class Style;
class IMessageBoxManager;

class ChatMessage : public ChatLine
{
Expand Down Expand Up @@ -65,7 +66,7 @@ class ChatMessage : public ChatLine
Style& style);
static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile,
ToxFile file, bool isMe, const QDateTime& date,
DocumentCache& documentCache, Settings& settings, Style& style);
DocumentCache& documentCache, Settings& settings, Style& style, IMessageBoxManager& messageBoxManager);
static ChatMessage::Ptr createTypingNotification(DocumentCache& documentCache, Settings& settings, Style& style);
static ChatMessage::Ptr createBusyNotification(DocumentCache& documentCache, Settings& settings, Style& style);

Expand Down
7 changes: 4 additions & 3 deletions src/chatlog/chatwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "chatmessage.h"
#include "content/filetransferwidget.h"
#include "content/text.h"
#include "src/widget/gui.h"
#include "src/widget/translator.h"
#include "src/widget/style.h"
#include "src/persistence/settings.h"
Expand Down Expand Up @@ -211,7 +210,8 @@ ChatLogIdx clampedAdd(ChatLogIdx idx, int val, IChatLog& chatLog)


ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache& documentCache_,
SmileyPack& smileyPack_, Settings& settings_, Style& style_, QWidget* parent)
SmileyPack& smileyPack_, Settings& settings_, Style& style_,
IMessageBoxManager& messageBoxManager_, QWidget* parent)
: QGraphicsView(parent)
, selectionRectColor{style_.getColor(Style::ColorPalette::SelectText)}
, chatLog(chatLog_)
Expand All @@ -221,6 +221,7 @@ ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache& doc
, smileyPack{smileyPack_}
, settings(settings_)
, style{style_}
, messageBoxManager{messageBoxManager_}
{
// Create the scene
busyScene = new QGraphicsScene(this);
Expand Down Expand Up @@ -1460,7 +1461,7 @@ void ChatWidget::renderFile(QString displayName, ToxFile file, bool isSelf, QDat
CoreFile* coreFile = core.getCoreFile();
assert(coreFile);
chatMessage = ChatMessage::createFileTransferMessage(displayName, *coreFile,
file, isSelf, timestamp, documentCache, settings, style);
file, isSelf, timestamp, documentCache, settings, style, messageBoxManager);
} else {
auto proxy = static_cast<ChatLineContentProxy*>(chatMessage->getContent(1));
assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType);
Expand Down
5 changes: 4 additions & 1 deletion src/chatlog/chatwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ class SmileyPack;
class Settings;
class Style;
class ChatLineStorage;
class IMessageBoxManager;

static const size_t DEF_NUM_MSG_TO_LOAD = 100;
class ChatWidget : public QGraphicsView
{
Q_OBJECT
public:
ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache& documentCache,
SmileyPack& smileyPack, Settings& settings, Style& style, QWidget* parent = nullptr);
SmileyPack& smileyPack, Settings& settings, Style& style,
IMessageBoxManager& messageBoxManager, QWidget* parent = nullptr);
virtual ~ChatWidget();

void insertChatlines(std::map<ChatLogIdx, ChatLine::Ptr> chatLines);
Expand Down Expand Up @@ -220,4 +222,5 @@ private slots:
SmileyPack& smileyPack;
Settings& settings;
Style& style;
IMessageBoxManager& messageBoxManager;
};
9 changes: 5 additions & 4 deletions src/chatlog/content/filetransferwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

#include "src/core/corefile.h"
#include "src/persistence/settings.h"
#include "src/widget/gui.h"
#include "src/widget/style.h"
#include "src/widget/widget.h"
#include "src/widget/tool/imessageboxmanager.h"
#include "src/model/exiftransform.h"
#include "util/display.h"

Expand All @@ -49,7 +49,7 @@
// downloaded to.

FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile,
ToxFile file, Settings& settings_, Style& style_)
ToxFile file, Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager_)
: QWidget(parent)
, coreFile{_coreFile}
, ui(new Ui::FileTransferWidget)
Expand All @@ -60,6 +60,7 @@ FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile,
, active(true)
, settings(settings_)
, style{style_}
, messageBoxManager{messageBoxManager_}
{
ui->setupUi(this);

Expand Down Expand Up @@ -140,7 +141,7 @@ void FileTransferWidget::acceptTransfer(const QString& filepath)

// test if writable
if (!tryRemoveFile(filepath)) {
GUI::showWarning(tr("Location not writable", "Title of permissions popup"),
messageBoxManager.showWarning(tr("Location not writable", "Title of permissions popup"),
tr("You do not have permission to write that location. Choose another, or "
"cancel the save dialog.",
"text of permissions popup"));
Expand Down Expand Up @@ -477,7 +478,7 @@ void FileTransferWidget::handleButton(QPushButton* btn)
}

if (btn->objectName() == "ok" || btn->objectName() == "previewButton") {
Widget::confirmExecutableOpen(QFileInfo(fileInfo.filePath));
messageBoxManager.confirmExecutableOpen(QFileInfo(fileInfo.filePath));
} else if (btn->objectName() == "dir") {
QString dirPath = QFileInfo(fileInfo.filePath).dir().path();
QDesktopServices::openUrl(QUrl::fromLocalFile(dirPath));
Expand Down
4 changes: 3 additions & 1 deletion src/chatlog/content/filetransferwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class QVariantAnimation;
class QPushButton;
class Settings;
class Style;
class IMessageBoxManager;

class FileTransferWidget : public QWidget
{
Q_OBJECT

public:
FileTransferWidget(QWidget* parent, CoreFile& _coreFile, ToxFile file,
Settings& settings, Style& style);
Settings& settings, Style& style, IMessageBoxManager& messageBoxManager);
virtual ~FileTransferWidget();
bool isActive() const;
void onFileTransferUpdate(ToxFile file);
Expand Down Expand Up @@ -93,4 +94,5 @@ private slots:
ToxFile::FileStatus lastStatus = ToxFile::INITIALIZING;
Settings& settings;
Style& style;
IMessageBoxManager& messageBoxManager;
};
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "src/video/camerasource.h"
#include "src/widget/loginscreen.h"
#include "src/widget/translator.h"
#include "src/widget/tool/messageboxmanager.h"
#include "widget/widget.h"
#include <QApplication>
#include <QCommandLineParser>
Expand Down Expand Up @@ -57,6 +58,7 @@ QMutex* logBufferMutex = new QMutex();

std::unique_ptr<Settings> settings;
std::unique_ptr<ToxSave> toxSave;
std::unique_ptr<MessageBoxManager> messageBoxManager;

void cleanup()
{
Expand Down Expand Up @@ -236,7 +238,8 @@ int main(int argc, char* argv[])
qWarning() << "Couldn't load font";
}

settings = std::unique_ptr<Settings>(new Settings());
messageBoxManager = std::unique_ptr<MessageBoxManager>(new MessageBoxManager());
settings = std::unique_ptr<Settings>(new Settings(*messageBoxManager));

QString locale = settings->getTranslation();
// We need to init the resources in the translations_library explicitely.
Expand Down Expand Up @@ -406,13 +409,14 @@ int main(int argc, char* argv[])
// note: Because Settings is shouldering global settings as well as model specific ones it
// cannot be integrated into a central model object yet
nexus.setSettings(settings.get());
nexus.setMessageBoxManager(messageBoxManager.get());
auto& cameraSource = Nexus::getCameraSource();
// Autologin
// TODO (kriby): Shift responsibility of linking views to model objects from nexus
// Further: generate view instances separately (loginScreen, mainGUI, audio)
Profile* profile = nullptr;
if (autoLogin && Profile::exists(profileName, settings->getPaths()) && !Profile::isEncrypted(profileName, settings->getPaths())) {
profile = Profile::loadProfile(profileName, QString(), *settings, &parser, cameraSource);
profile = Profile::loadProfile(profileName, QString(), *settings, &parser, cameraSource, *messageBoxManager);
if (!profile) {
QMessageBox::information(nullptr, QObject::tr("Error"),
QObject::tr("Failed to load profile automatically."));
Expand All @@ -429,7 +433,7 @@ int main(int argc, char* argv[])
profile = nexus.getProfile();
}

uriDialog = std::unique_ptr<ToxURIDialog>(new ToxURIDialog(nullptr, profile->getCore()));
uriDialog = std::unique_ptr<ToxURIDialog>(new ToxURIDialog(nullptr, profile->getCore(), *messageBoxManager));

if (ipc.isAttached()) {
// Start to accept Inter-process communication
Expand Down
9 changes: 5 additions & 4 deletions src/net/toxuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "src/net/toxuri.h"
#include "src/core/core.h"
#include "src/widget/gui.h"
#include "src/widget/tool/imessageboxmanager.h"
#include <QByteArray>
#include <QCoreApplication>
#include <QDialogButtonBox>
Expand Down Expand Up @@ -52,7 +52,7 @@ bool ToxURIDialog::handleToxURI(const QString& toxURI)
}

if (!error.isEmpty()) {
GUI::showWarning(QMessageBox::tr("Couldn't add friend"), error);
messageBoxManager.showWarning(QMessageBox::tr("Couldn't add friend"), error);
return false;
}

Expand All @@ -72,9 +72,10 @@ void ToxURIDialog::setUserId(const QString& userId)
userIdEdit->setText(userId);
}

ToxURIDialog::ToxURIDialog(QWidget* parent, Core& _core)
ToxURIDialog::ToxURIDialog(QWidget* parent, Core& core_, IMessageBoxManager& messageBoxManager_)
: QDialog(parent)
, core{_core}
, core{core_}
, messageBoxManager{messageBoxManager_}
{
const QString defaultMessage =
QObject::tr("%1 here! Tox me maybe?",
Expand Down
4 changes: 3 additions & 1 deletion src/net/toxuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ class QByteArray;
class QLabel;
class QLineEdit;
class QPlainTextEdit;
class IMessageBoxManager;
class ToxURIDialog : public QDialog
{
Q_OBJECT
public:
explicit ToxURIDialog(QWidget* parent, Core& _core);
ToxURIDialog(QWidget* parent, Core& core, IMessageBoxManager& messageBoxManager);
QString getRequestMessage();
bool handleToxURI(const QString& toxURI);

Expand All @@ -44,4 +45,5 @@ class ToxURIDialog : public QDialog
QLabel* friendsLabel;
QLineEdit* userIdEdit;
Core& core;
IMessageBoxManager& messageBoxManager;
};
16 changes: 11 additions & 5 deletions src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#include "src/widget/widget.h"
#include "src/widget/style.h"
#include "video/camerasource.h"
#include "widget/gui.h"
#include "widget/loginscreen.h"
#include "src/widget/tool/messageboxmanager.h"
#include "audio/audio.h"

#include <QApplication>
Expand Down Expand Up @@ -235,11 +235,10 @@ void Nexus::showMainGUI()
assert(profile);

// Create GUI
widget = new Widget(*profile, *audioControl, *cameraSource, *settings, *style);
widget = new Widget(*profile, *audioControl, *cameraSource, *settings, *style, *messageBoxManager);

// Start GUI
widget->init();
GUI::getInstance();

// Zetok protection
// There are small instants on startup during which no
Expand Down Expand Up @@ -308,7 +307,8 @@ Profile* Nexus::getProfile()
*/
void Nexus::onCreateNewProfile(const QString& name, const QString& pass)
{
setProfile(Profile::createProfile(name, pass, *settings, parser, *cameraSource));
setProfile(Profile::createProfile(name, pass, *settings, parser, *cameraSource,
*messageBoxManager));
parser = nullptr; // only apply cmdline proxy settings once
}

Expand All @@ -317,7 +317,8 @@ void Nexus::onCreateNewProfile(const QString& name, const QString& pass)
*/
void Nexus::onLoadProfile(const QString& name, const QString& pass)
{
setProfile(Profile::loadProfile(name, pass, *settings, parser, *cameraSource));
setProfile(Profile::loadProfile(name, pass, *settings, parser, *cameraSource,
*messageBoxManager));
parser = nullptr; // only apply cmdline proxy settings once
}
/**
Expand Down Expand Up @@ -356,6 +357,11 @@ CameraSource& Nexus::getCameraSource()
return *getInstance().cameraSource;
}

void Nexus::setMessageBoxManager(IMessageBoxManager* messageBoxManager_)
{
messageBoxManager = messageBoxManager_;
}

#ifdef Q_OS_MAC
void Nexus::retranslateUi()
{
Expand Down
3 changes: 3 additions & 0 deletions src/nexus.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Core;
class QCommandLineParser;
class CameraSource;
class Style;
class IMessageBoxManager;

#ifdef Q_OS_MAC
class QMenuBar;
Expand All @@ -51,6 +52,7 @@ class Nexus : public QObject
void start();
void showMainGUI();
void setSettings(Settings* settings_);
void setMessageBoxManager(IMessageBoxManager* messageBoxManager);
void setParser(QCommandLineParser* parser_);
static Nexus& getInstance();
static void destroyInstance();
Expand Down Expand Up @@ -111,4 +113,5 @@ public slots:
QCommandLineParser* parser = nullptr;
std::unique_ptr<CameraSource> cameraSource;
std::unique_ptr<Style> style;
IMessageBoxManager* messageBoxManager = nullptr;
};
Loading

0 comments on commit b24faab

Please sign in to comment.