From b24faabf423e6f4554d6ea52d8aa82cc38c144e0 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Sun, 13 Mar 2022 06:04:24 -0700 Subject: [PATCH] refactor(GUI): Move remaining messsage box functionality to new class * 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 --- CMakeLists.txt | 6 +- doc/coding_standards.md | 1 - src/chatlog/chatmessage.cpp | 10 +- src/chatlog/chatmessage.h | 3 +- src/chatlog/chatwidget.cpp | 7 +- src/chatlog/chatwidget.h | 5 +- src/chatlog/content/filetransferwidget.cpp | 9 +- src/chatlog/content/filetransferwidget.h | 4 +- src/main.cpp | 10 +- src/net/toxuri.cpp | 9 +- src/net/toxuri.h | 4 +- src/nexus.cpp | 16 +- src/nexus.h | 3 + src/persistence/profile.cpp | 20 ++- src/persistence/profile.h | 10 +- src/persistence/settings.cpp | 7 +- src/persistence/settings.h | 4 +- src/persistence/toxsave.cpp | 1 - src/widget/about/aboutfriendform.cpp | 9 +- src/widget/about/aboutfriendform.h | 4 +- src/widget/chatformheader.cpp | 1 - src/widget/circlewidget.cpp | 5 +- src/widget/circlewidget.h | 4 +- src/widget/contentdialog.cpp | 5 +- src/widget/contentdialog.h | 5 +- src/widget/contentlayout.cpp | 1 - src/widget/form/addfriendform.cpp | 14 +- src/widget/form/addfriendform.h | 5 +- src/widget/form/chatform.cpp | 4 +- src/widget/form/chatform.h | 3 +- src/widget/form/filesform.cpp | 10 +- src/widget/form/filesform.h | 6 +- src/widget/form/genericchatform.cpp | 5 +- src/widget/form/genericchatform.h | 3 +- src/widget/form/groupchatform.cpp | 4 +- src/widget/form/groupchatform.h | 4 +- src/widget/form/groupinviteform.h | 2 - src/widget/form/profileform.cpp | 25 +-- src/widget/form/profileform.h | 4 +- src/widget/form/settings/advancedform.cpp | 9 +- src/widget/form/settings/advancedform.h | 4 +- src/widget/form/settings/avform.h | 2 +- src/widget/form/settings/genericsettings.cpp | 1 - src/widget/form/settings/genericsettings.h | 2 +- src/widget/form/settings/privacyform.cpp | 1 - src/widget/form/settingswidget.cpp | 4 +- src/widget/form/settingswidget.h | 3 +- src/widget/friendlistwidget.cpp | 7 +- src/widget/friendlistwidget.h | 5 +- src/widget/friendwidget.cpp | 6 +- src/widget/friendwidget.h | 6 +- src/widget/genericchatitemwidget.cpp | 1 - src/widget/genericchatitemwidget.h | 2 +- src/widget/searchform.cpp | 1 - src/widget/style.cpp | 1 - src/widget/tool/activatedialog.cpp | 1 - src/widget/tool/activatedialog.h | 4 +- src/widget/tool/imessageboxmanager.cpp | 22 +++ src/widget/tool/imessageboxmanager.h | 37 +++++ .../{gui.cpp => tool/messageboxmanager.cpp} | 145 ++++++++---------- .../{gui.h => tool/messageboxmanager.h} | 35 ++--- src/widget/widget.cpp | 63 ++------ src/widget/widget.h | 9 +- translations/ar.ts | 23 +-- translations/be.ts | 23 +-- translations/bg.ts | 23 +-- translations/cs.ts | 23 +-- translations/da.ts | 23 +-- translations/de.ts | 23 +-- translations/el.ts | 23 +-- translations/eo.ts | 23 +-- translations/es.ts | 23 +-- translations/et.ts | 23 +-- translations/fa.ts | 23 +-- translations/fi.ts | 23 +-- translations/fr.ts | 23 +-- translations/gl.ts | 23 +-- translations/he.ts | 23 +-- translations/hr.ts | 23 +-- translations/hu.ts | 23 +-- translations/is.ts | 23 +-- translations/it.ts | 23 +-- translations/ja.ts | 23 +-- translations/kn.ts | 23 +-- translations/ko.ts | 23 +-- translations/lt.ts | 23 +-- translations/lv.ts | 23 +-- translations/mk.ts | 23 +-- translations/nl.ts | 23 +-- translations/nl_BE.ts | 23 +-- translations/no_nb.ts | 23 +-- translations/pl.ts | 24 +-- translations/pt.ts | 23 +-- translations/pt_BR.ts | 23 +-- translations/ro.ts | 23 +-- translations/ru.ts | 23 +-- translations/si.ts | 23 +-- translations/sk.ts | 23 +-- translations/sl.ts | 23 +-- translations/sq.ts | 23 +-- translations/sr.ts | 23 +-- translations/sr_Latn.ts | 23 +-- translations/sv.ts | 23 +-- translations/sw.ts | 23 +-- translations/ta.ts | 23 +-- translations/tr.ts | 23 +-- translations/ug.ts | 23 +-- translations/uk.ts | 23 +-- translations/ur.ts | 23 +-- translations/zh_CN.ts | 23 +-- translations/zh_TW.ts | 23 +-- 111 files changed, 973 insertions(+), 755 deletions(-) create mode 100644 src/widget/tool/imessageboxmanager.cpp create mode 100644 src/widget/tool/imessageboxmanager.h rename src/widget/{gui.cpp => tool/messageboxmanager.cpp} (56%) rename src/widget/{gui.h => tool/messageboxmanager.h} (65%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8709bae37..3c86b9bb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 diff --git a/doc/coding_standards.md b/doc/coding_standards.md index 5aae3d5d2..0acf88be5 100644 --- a/doc/coding_standards.md +++ b/doc/coding_standards.md @@ -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 #include diff --git a/src/chatlog/chatmessage.cpp b/src/chatlog/chatmessage.cpp index a6481e390..1f3c4a706 100644 --- a/src/chatlog/chatmessage.cpp +++ b/src/chatlog/chatmessage.cpp @@ -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)); @@ -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), diff --git a/src/chatlog/chatmessage.h b/src/chatlog/chatmessage.h index ed3ee1684..37034fa92 100644 --- a/src/chatlog/chatmessage.h +++ b/src/chatlog/chatmessage.h @@ -31,6 +31,7 @@ class DocumentCache; class SmileyPack; class Settings; class Style; +class IMessageBoxManager; class ChatMessage : public ChatLine { @@ -65,7 +66,7 @@ public: 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); diff --git a/src/chatlog/chatwidget.cpp b/src/chatlog/chatwidget.cpp index d6748b9bb..f38b281cc 100644 --- a/src/chatlog/chatwidget.cpp +++ b/src/chatlog/chatwidget.cpp @@ -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" @@ -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_) @@ -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); @@ -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(chatMessage->getContent(1)); assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType); diff --git a/src/chatlog/chatwidget.h b/src/chatlog/chatwidget.h index a7c1bcba3..da09c8556 100644 --- a/src/chatlog/chatwidget.h +++ b/src/chatlog/chatwidget.h @@ -37,6 +37,7 @@ class SmileyPack; class Settings; class Style; class ChatLineStorage; +class IMessageBoxManager; static const size_t DEF_NUM_MSG_TO_LOAD = 100; class ChatWidget : public QGraphicsView @@ -44,7 +45,8 @@ 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 chatLines); @@ -220,4 +222,5 @@ private: SmileyPack& smileyPack; Settings& settings; Style& style; + IMessageBoxManager& messageBoxManager; }; diff --git a/src/chatlog/content/filetransferwidget.cpp b/src/chatlog/content/filetransferwidget.cpp index 6bb98dcba..fd517ca70 100644 --- a/src/chatlog/content/filetransferwidget.cpp +++ b/src/chatlog/content/filetransferwidget.cpp @@ -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" @@ -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) @@ -60,6 +60,7 @@ FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile, , active(true) , settings(settings_) , style{style_} + , messageBoxManager{messageBoxManager_} { ui->setupUi(this); @@ -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")); @@ -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)); diff --git a/src/chatlog/content/filetransferwidget.h b/src/chatlog/content/filetransferwidget.h index f27536cc8..217ffe14c 100644 --- a/src/chatlog/content/filetransferwidget.h +++ b/src/chatlog/content/filetransferwidget.h @@ -35,6 +35,7 @@ class QVariantAnimation; class QPushButton; class Settings; class Style; +class IMessageBoxManager; class FileTransferWidget : public QWidget { @@ -42,7 +43,7 @@ class FileTransferWidget : public QWidget 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); @@ -93,4 +94,5 @@ private: ToxFile::FileStatus lastStatus = ToxFile::INITIALIZING; Settings& settings; Style& style; + IMessageBoxManager& messageBoxManager; }; diff --git a/src/main.cpp b/src/main.cpp index a0bbde80a..fc8c6a870 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 #include @@ -57,6 +58,7 @@ QMutex* logBufferMutex = new QMutex(); std::unique_ptr settings; std::unique_ptr toxSave; +std::unique_ptr messageBoxManager; void cleanup() { @@ -236,7 +238,8 @@ int main(int argc, char* argv[]) qWarning() << "Couldn't load font"; } - settings = std::unique_ptr(new Settings()); + messageBoxManager = std::unique_ptr(new MessageBoxManager()); + settings = std::unique_ptr(new Settings(*messageBoxManager)); QString locale = settings->getTranslation(); // We need to init the resources in the translations_library explicitely. @@ -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.")); @@ -429,7 +433,7 @@ int main(int argc, char* argv[]) profile = nexus.getProfile(); } - uriDialog = std::unique_ptr(new ToxURIDialog(nullptr, profile->getCore())); + uriDialog = std::unique_ptr(new ToxURIDialog(nullptr, profile->getCore(), *messageBoxManager)); if (ipc.isAttached()) { // Start to accept Inter-process communication diff --git a/src/net/toxuri.cpp b/src/net/toxuri.cpp index f95e0ab66..97f5d3c9a 100644 --- a/src/net/toxuri.cpp +++ b/src/net/toxuri.cpp @@ -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 #include #include @@ -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; } @@ -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?", diff --git a/src/net/toxuri.h b/src/net/toxuri.h index d51d87234..4108eb081 100644 --- a/src/net/toxuri.h +++ b/src/net/toxuri.h @@ -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); @@ -44,4 +45,5 @@ private: QLabel* friendsLabel; QLineEdit* userIdEdit; Core& core; + IMessageBoxManager& messageBoxManager; }; diff --git a/src/nexus.cpp b/src/nexus.cpp index b077ce8e3..eff23fd4e 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -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 @@ -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 @@ -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 } @@ -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 } /** @@ -356,6 +357,11 @@ CameraSource& Nexus::getCameraSource() return *getInstance().cameraSource; } +void Nexus::setMessageBoxManager(IMessageBoxManager* messageBoxManager_) +{ + messageBoxManager = messageBoxManager_; +} + #ifdef Q_OS_MAC void Nexus::retranslateUi() { diff --git a/src/nexus.h b/src/nexus.h index e53e89239..c96b4b37e 100644 --- a/src/nexus.h +++ b/src/nexus.h @@ -34,6 +34,7 @@ class Core; class QCommandLineParser; class CameraSource; class Style; +class IMessageBoxManager; #ifdef Q_OS_MAC class QMenuBar; @@ -51,6 +52,7 @@ public: void start(); void showMainGUI(); void setSettings(Settings* settings_); + void setMessageBoxManager(IMessageBoxManager* messageBoxManager); void setParser(QCommandLineParser* parser_); static Nexus& getInstance(); static void destroyInstance(); @@ -111,4 +113,5 @@ private: QCommandLineParser* parser = nullptr; std::unique_ptr cameraSource; std::unique_ptr