1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(nexus): make loginscreen independent of nexus

This commit is contained in:
sudden6 2018-01-08 19:49:06 +01:00
parent 09ad16bc44
commit 11f263ffc3
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
7 changed files with 51 additions and 70 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginScreen</class>
<widget class="QWidget" name="LoginScreen">
<widget class="QDialog" name="LoginScreen">
<property name="geometry">
<rect>
<x>0</x>

View File

@ -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();

View File

@ -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>("ToxId");
qRegisterMetaType<GroupInvite>("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()
{

View File

@ -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

View File

@ -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 <QDebug>
#include <QDialog>
#include <QMessageBox>
#include <QToolButton>
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()

View File

@ -23,20 +23,23 @@
#include <QShortcut>
#include <QToolButton>
#include <QWidget>
#include <QDialog>
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

View File

@ -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);
}