From 11f263ffc37e282625ac14e5b6c6bee99287976a Mon Sep 17 00:00:00 2001 From: sudden6 Date: Mon, 8 Jan 2018 19:49:06 +0100 Subject: [PATCH] refactor(nexus): make loginscreen independent of nexus --- src/loginscreen.ui | 2 +- src/main.cpp | 25 ++++++++++++------- src/nexus.cpp | 49 +++++++++++--------------------------- src/nexus.h | 6 ----- src/widget/loginscreen.cpp | 27 ++++++++++----------- src/widget/loginscreen.h | 10 +++++--- src/widget/widget.cpp | 2 +- 7 files changed, 51 insertions(+), 70 deletions(-) diff --git a/src/loginscreen.ui b/src/loginscreen.ui index 885bb78d5..bd4418023 100644 --- a/src/loginscreen.ui +++ b/src/loginscreen.ui @@ -1,7 +1,7 @@ LoginScreen - + 0 diff --git a/src/main.cpp b/src/main.cpp index 8beb03c87..fcd1af9a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -323,18 +323,25 @@ int main(int argc, char* argv[]) } } + Profile* profile = nullptr; + // Autologin - if (autoLogin) { - if (Profile::exists(profileName)) { - if (!Profile::isEncrypted(profileName)) { - Profile* profile = Profile::loadProfile(profileName); - if (profile) - Nexus::getInstance().setProfile(profile); - } - Settings::getInstance().setCurrentProfile(profileName); - } + if (autoLogin && Profile::exists(profileName) && + !Profile::isEncrypted(profileName)) { + profile = Profile::loadProfile(profileName); + } else { + LoginScreen loginScreen{}; + loginScreen.exec(); + profile = loginScreen.getProfile(); } + if (!profile) { + return EXIT_FAILURE; + } + + Nexus::getInstance().setProfile(profile); + Settings::getInstance().setCurrentProfile(profileName); + Nexus& nexus = Nexus::getInstance(); nexus.start(); diff --git a/src/nexus.cpp b/src/nexus.cpp index 544809333..53b048fc7 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -60,9 +60,7 @@ Nexus::Nexus(QObject* parent) : QObject(parent) , profile{nullptr} , widget{nullptr} - , loginScreen{nullptr} , running{true} - , quitOnLastWindowClosed{true} { } @@ -70,8 +68,6 @@ Nexus::~Nexus() { delete widget; widget = nullptr; - delete loginScreen; - loginScreen = nullptr; delete profile; profile = nullptr; Settings::getInstance().saveGlobal(); @@ -109,17 +105,8 @@ void Nexus::start() qRegisterMetaType("ToxId"); qRegisterMetaType("GroupInvite"); - loginScreen = new LoginScreen(); - - // We need this LastWindowClosed dance because the LoginScreen may be shown - // and closed in a processEvents() loop before the start of the real - // exec() event loop, meaning we wouldn't receive the onLastWindowClosed, - // and so we wouldn't have a chance to tell the processEvents() loop to quit. - qApp->setQuitOnLastWindowClosed(false); - connect(qApp, &QApplication::lastWindowClosed, this, &Nexus::onLastWindowClosed); - connect(loginScreen, &LoginScreen::closed, this, &Nexus::onLastWindowClosed); - #ifdef Q_OS_MAC + // TODO: still needed? globalMenuBar = new QMenuBar(0); dockMenu = new QMenu(globalMenuBar); @@ -150,15 +137,9 @@ void Nexus::start() windowMapper = new QSignalMapper(this); connect(windowMapper, SIGNAL(mapped(QObject*)), this, SLOT(onOpenWindow(QObject*))); - connect(loginScreen, &LoginScreen::windowStateChanged, this, &Nexus::onWindowStateChanged); - retranslateUi(); #endif - - if (profile) - showMainGUI(); - else - showLogin(); + showMainGUI(); } /** @@ -172,20 +153,24 @@ void Nexus::showLogin() delete profile; profile = nullptr; - loginScreen->reset(); - loginScreen->move(QApplication::desktop()->screen()->rect().center() - - loginScreen->rect().center()); - loginScreen->show(); - quitOnLastWindowClosed = true; + LoginScreen loginScreen; + loginScreen.exec(); + + profile = loginScreen.getProfile(); + + if (profile) { + Nexus::getInstance().setProfile(profile); + Settings::getInstance().setCurrentProfile(profile->getName()); + showMainGUI(); + } else { + quit(); + } } void Nexus::showMainGUI() { assert(profile); - quitOnLastWindowClosed = false; - loginScreen->close(); - // Create GUI widget = Widget::getInstance(); @@ -338,12 +323,6 @@ bool Nexus::tryRemoveFile(const QString& filepath) return writable; } -void Nexus::onLastWindowClosed() -{ - if (quitOnLastWindowClosed) - quit(); -} - #ifdef Q_OS_MAC void Nexus::retranslateUi() { diff --git a/src/nexus.h b/src/nexus.h index 62917f032..5ceabe93d 100644 --- a/src/nexus.h +++ b/src/nexus.h @@ -25,7 +25,6 @@ class Widget; class Profile; -class LoginScreen; class Core; #ifdef Q_OS_MAC @@ -85,9 +84,6 @@ private: QActionGroup* windowActions = nullptr; #endif -private slots: - void onLastWindowClosed(); - private: explicit Nexus(QObject* parent = 0); ~Nexus(); @@ -95,9 +91,7 @@ private: private: Profile* profile; Widget* widget; - LoginScreen* loginScreen; bool running; - bool quitOnLastWindowClosed; }; #endif // NEXUS_H diff --git a/src/widget/loginscreen.cpp b/src/widget/loginscreen.cpp index d6df9862e..705f79f46 100644 --- a/src/widget/loginscreen.cpp +++ b/src/widget/loginscreen.cpp @@ -20,7 +20,6 @@ #include "loginscreen.h" #include "ui_loginscreen.h" -#include "src/nexus.h" #include "src/persistence/profile.h" #include "src/persistence/profilelocker.h" #include "src/persistence/settings.h" @@ -29,11 +28,12 @@ #include "src/widget/tool/profileimporter.h" #include "src/widget/translator.h" #include +#include #include #include LoginScreen::LoginScreen(QWidget* parent) - : QWidget(parent) + : QDialog(parent) , ui(new Ui::LoginScreen) , quitShortcut{QKeySequence(Qt::CTRL + Qt::Key_Q), this} { @@ -105,6 +105,11 @@ void LoginScreen::reset() ui->autoLoginCB->blockSignals(false); } +Profile *LoginScreen::getProfile() const +{ + return profile; +} + bool LoginScreen::event(QEvent* event) { switch (event->type()) { @@ -118,7 +123,6 @@ bool LoginScreen::event(QEvent* event) break; } - return QWidget::event(event); } @@ -167,19 +171,16 @@ void LoginScreen::onCreateNewProfile() return; } - Profile* profile = Profile::createProfile(name, pass); + profile = Profile::createProfile(name, pass); if (!profile) { // Unknown error QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("Unknown error: Couldn't create a new profile.\nIf you " "encountered this error, please report it.")); + done(-1); return; } - - Nexus& nexus = Nexus::getInstance(); - - nexus.setProfile(profile); - nexus.showMainGUI(); + done(0); } void LoginScreen::onLoginUsernameSelected(const QString& name) @@ -223,7 +224,7 @@ void LoginScreen::onLogin() return; } - Profile* profile = Profile::loadProfile(name, pass); + profile = Profile::loadProfile(name, pass); if (!profile) { if (!ProfileLocker::isLockable(name)) { QMessageBox::critical(this, tr("Couldn't load this profile"), @@ -236,11 +237,7 @@ void LoginScreen::onLogin() return; } } - - Nexus& nexus = Nexus::getInstance(); - - nexus.setProfile(profile); - nexus.showMainGUI(); + done(0); } void LoginScreen::onPasswordEdited() diff --git a/src/widget/loginscreen.h b/src/widget/loginscreen.h index 6e309c11b..5560613b4 100644 --- a/src/widget/loginscreen.h +++ b/src/widget/loginscreen.h @@ -23,20 +23,23 @@ #include #include -#include +#include + +class Profile; namespace Ui { class LoginScreen; } -class LoginScreen : public QWidget +class LoginScreen : public QDialog { Q_OBJECT public: - explicit LoginScreen(QWidget* parent = 0); + explicit LoginScreen(QWidget* parent = nullptr); ~LoginScreen(); void reset(); + Profile* getProfile() const; bool event(QEvent* event) final override; @@ -68,6 +71,7 @@ private: private: Ui::LoginScreen* ui; QShortcut quitShortcut; + Profile* profile{nullptr}; }; #endif // LOGINSCREEN_H diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 667a4027d..75fcd8cc1 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -963,7 +963,7 @@ void Widget::onStopNotification() void Widget::onRejectCall(uint32_t friendId) { - CoreAV* av = Core::getInstance()->getAv(); + CoreAV* const av = Core::getInstance()->getAv(); av->cancelCall(friendId); }