From 665a6c4e3df0ffb2e2be20da3b0c60d9a6c7a0d5 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Mon, 28 Mar 2022 17:37:24 -0700 Subject: [PATCH] refactor(Nexus): Remove Nexus::getDesktopGUI Remove ProfileImporter's dependence on Widget, on path towards removing Nexus singleton. --- src/appmanager.cpp | 6 ++---- src/appmanager.h | 2 -- src/nexus.cpp | 19 ++++++++----------- src/nexus.h | 5 ++--- src/persistence/toxsave.cpp | 5 +++-- src/persistence/toxsave.h | 4 +++- src/widget/widget.cpp | 11 ++++++++++- src/widget/widget.h | 5 ++++- 8 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/appmanager.cpp b/src/appmanager.cpp index 981d551f7..d23e31a34 100644 --- a/src/appmanager.cpp +++ b/src/appmanager.cpp @@ -166,7 +166,6 @@ AppManager::AppManager(int argc, char** argv) , messageBoxManager(new MessageBoxManager(nullptr)) , settings(new Settings(*messageBoxManager)) , ipc(new IPC(settings->getCurrentProfileId())) - , toxSave(new ToxSave(*settings, *ipc)) { } @@ -392,15 +391,14 @@ int AppManager::run() if (ipc->isAttached()) { // Start to accept Inter-process communication ipc->registerEventHandler("uri", &toxURIEventHandler, uriDialog.get()); - ipc->registerEventHandler(ToxSave::eventHandlerKey, &ToxSave::toxSaveEventHandler, toxSave.get()); - nexus.registerActivate(); + nexus.registerIpcHandlers(); } // Event was not handled by already running instance therefore we handle it ourselves if (eventType == "uri") { uriDialog->handleToxURI(firstParam); } else if (eventType == ToxSave::eventHandlerKey) { - toxSave->handleToxSave(firstParam); + nexus.handleToxSave(firstParam); } connect(qapp.get(), &QApplication::aboutToQuit, this, &AppManager::cleanup); diff --git a/src/appmanager.h b/src/appmanager.h index de3134486..885e2797f 100644 --- a/src/appmanager.h +++ b/src/appmanager.h @@ -25,7 +25,6 @@ class MessageBoxManager; class Settings; -class ToxSave; class IPC; class QApplication; class ToxURIDialog; @@ -47,6 +46,5 @@ private: std::unique_ptr messageBoxManager; std::unique_ptr settings; std::unique_ptr ipc; - std::unique_ptr toxSave; std::unique_ptr uriDialog; }; diff --git a/src/nexus.cpp b/src/nexus.cpp index dc2b316a2..c1b419ab3 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -331,15 +331,6 @@ void Nexus::setParser(QCommandLineParser* parser_) parser = parser_; } -/** - * @brief Get desktop GUI widget. - * @return nullptr if not started, desktop widget otherwise. - */ -Widget* Nexus::getDesktopGUI() -{ - return getInstance().widget; -} - CameraSource& Nexus::getCameraSource() { return *getInstance().cameraSource; @@ -355,9 +346,15 @@ void Nexus::setIpc(IPC* ipc_) ipc = ipc_; } -void Nexus::registerActivate() +void Nexus::registerIpcHandlers() { - widget->registerActivate(); + widget->registerIpcHandlers(); +} + +bool Nexus::handleToxSave(const QString& path) +{ + assert(widget); + return widget->handleToxSave(path); } #ifdef Q_OS_MAC diff --git a/src/nexus.h b/src/nexus.h index d94b5b158..ff9d47169 100644 --- a/src/nexus.h +++ b/src/nexus.h @@ -59,10 +59,9 @@ public: static Nexus& getInstance(); static void destroyInstance(); Profile* getProfile(); - static Widget* getDesktopGUI(); static CameraSource& getCameraSource(); - void registerActivate(); - + void registerIpcHandlers(); + bool handleToxSave(const QString& path); #ifdef Q_OS_MAC public: diff --git a/src/persistence/toxsave.cpp b/src/persistence/toxsave.cpp index 82ebcbaff..3b0ab490e 100644 --- a/src/persistence/toxsave.cpp +++ b/src/persistence/toxsave.cpp @@ -28,9 +28,10 @@ const QString ToxSave::eventHandlerKey = QStringLiteral("save"); -ToxSave::ToxSave(Settings& settings_, IPC& ipc_) +ToxSave::ToxSave(Settings& settings_, IPC& ipc_, QWidget* parent_) : settings{settings_} , ipc{ipc_} + , parent{parent_} {} ToxSave::~ToxSave() @@ -58,6 +59,6 @@ bool ToxSave::toxSaveEventHandler(const QByteArray& eventData, void* userData) */ bool ToxSave::handleToxSave(const QString& path) { - ProfileImporter importer(settings, Nexus::getDesktopGUI()); + ProfileImporter importer(settings, parent); return importer.importProfile(path); } diff --git a/src/persistence/toxsave.h b/src/persistence/toxsave.h index 203c5c562..c3eba06d7 100644 --- a/src/persistence/toxsave.h +++ b/src/persistence/toxsave.h @@ -24,12 +24,13 @@ class QByteArray; class Settings; class IPC; class QString; +class QWidget; class ToxSave { public: const static QString eventHandlerKey; - ToxSave(Settings& settings, IPC& ipc); + ToxSave(Settings& settings, IPC& ipc, QWidget* parent); ~ToxSave(); bool handleToxSave(const QString& path); static bool toxSaveEventHandler(const QByteArray& eventData, void* userData); @@ -37,4 +38,5 @@ public: private: Settings& settings; IPC& ipc; + QWidget* parent; }; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 708ccc859..61517fb89 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -81,6 +81,7 @@ #include "src/widget/tool/messageboxmanager.h" #include "tool/removechatdialog.h" #include "src/persistence/smileypack.h" +#include "src/persistence/toxsave.h" #include "src/ipc.h" namespace { @@ -99,6 +100,7 @@ bool tryRemoveFile(const QString& filepath) } const QString activateHandlerKey("activate"); +const QString saveHandlerKey("save"); } // namespace @@ -162,6 +164,7 @@ Widget::Widget(Profile &profile_, IAudioControl& audio_, CameraSource& cameraSou , groupList(new GroupList()) , contentDialogManager(new ContentDialogManager(*friendList)) , ipc{ipc_} + , toxSave(new ToxSave{settings, ipc, this}) { installEventFilter(this); QString locale = settings.getTranslation(); @@ -2757,7 +2760,13 @@ void Widget::formatWindowTitle(const QString& content) } } -void Widget::registerActivate() +void Widget::registerIpcHandlers() { ipc.registerEventHandler(activateHandlerKey, &toxActivateEventHandler, this); + ipc.registerEventHandler(saveHandlerKey, &ToxSave::toxSaveEventHandler, toxSave.get()); +} + +bool Widget::handleToxSave(const QString& path) +{ + return toxSave->handleToxSave(path); } diff --git a/src/widget/widget.h b/src/widget/widget.h index b9e6d9740..94435bbe1 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -91,6 +91,7 @@ class ContentDialogManager; class FriendList; class GroupList; class IPC; +class ToxSave; class Widget final : public QMainWindow { @@ -154,8 +155,9 @@ public: bool groupsVisible() const; void resetIcon(); - void registerActivate(); + void registerIpcHandlers(); static bool toxActivateEventHandler(const QByteArray& data, void* userData); + bool handleToxSave(const QString& path); public slots: void reloadTheme(); @@ -400,4 +402,5 @@ private: std::unique_ptr groupList; std::unique_ptr contentDialogManager; IPC& ipc; + std::unique_ptr toxSave; };