Skip to content

Commit

Permalink
refactor(Nexus): Remove Nexus::getDesktopGUI
Browse files Browse the repository at this point in the history
Remove ProfileImporter's dependence on Widget, on path towards removing
Nexus singleton.
  • Loading branch information
anthonybilinski committed May 27, 2022
1 parent d2fe222 commit 665a6c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/appmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

class MessageBoxManager;
class Settings;
class ToxSave;
class IPC;
class QApplication;
class ToxURIDialog;
Expand All @@ -47,6 +46,5 @@ private slots:
std::unique_ptr<MessageBoxManager> messageBoxManager;
std::unique_ptr<Settings> settings;
std::unique_ptr<IPC> ipc;
std::unique_ptr<ToxSave> toxSave;
std::unique_ptr<ToxURIDialog> uriDialog;
};
19 changes: 8 additions & 11 deletions src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -355,9 +346,15 @@ void Nexus::setIpc(IPC* ipc_)
ipc = ipc_;
}

void Nexus::registerActivate()
void Nexus::registerIpcHandlers()
{
widget->registerIpcHandlers();
}

bool Nexus::handleToxSave(const QString& path)
{
widget->registerActivate();
assert(widget);
return widget->handleToxSave(path);
}

#ifdef Q_OS_MAC
Expand Down
5 changes: 2 additions & 3 deletions src/nexus.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ class Nexus : public QObject
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:
Expand Down
5 changes: 3 additions & 2 deletions src/persistence/toxsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion src/persistence/toxsave.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ 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);

private:
Settings& settings;
IPC& ipc;
QWidget* parent;
};
11 changes: 10 additions & 1 deletion src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -99,6 +100,7 @@ bool tryRemoveFile(const QString& filepath)
}

const QString activateHandlerKey("activate");
const QString saveHandlerKey("save");

} // namespace

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
5 changes: 4 additions & 1 deletion src/widget/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ContentDialogManager;
class FriendList;
class GroupList;
class IPC;
class ToxSave;

class Widget final : public QMainWindow
{
Expand Down Expand Up @@ -154,8 +155,9 @@ class Widget final : public QMainWindow
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();
Expand Down Expand Up @@ -400,4 +402,5 @@ private slots:
std::unique_ptr<GroupList> groupList;
std::unique_ptr<ContentDialogManager> contentDialogManager;
IPC& ipc;
std::unique_ptr<ToxSave> toxSave;
};

0 comments on commit 665a6c4

Please sign in to comment.