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

159 lines
4.4 KiB
C++
Raw Normal View History

2015-06-04 01:04:28 +08:00
#include "loginscreen.h"
#include "ui_loginscreen.h"
2015-06-04 05:20:47 +08:00
#include "src/profile.h"
#include "src/profilelocker.h"
#include "src/nexus.h"
2015-06-04 20:19:18 +08:00
#include "src/misc/settings.h"
#include <QMessageBox>
2015-06-04 20:19:18 +08:00
#include <QDebug>
2015-06-04 01:04:28 +08:00
LoginScreen::LoginScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::LoginScreen)
{
ui->setupUi(this);
2015-06-04 01:28:16 +08:00
connect(ui->newProfilePgbtn, &QPushButton::clicked, this, &LoginScreen::onNewProfilePageClicked);
connect(ui->loginPgbtn, &QPushButton::clicked, this, &LoginScreen::onLoginPageClicked);
connect(ui->createAccountButton, &QPushButton::clicked, this, &LoginScreen::onCreateNewProfile);
2015-06-04 16:56:48 +08:00
connect(ui->newUsername, &QLineEdit::returnPressed, this, &LoginScreen::onCreateNewProfile);
connect(ui->newPass, &QLineEdit::returnPressed, this, &LoginScreen::onCreateNewProfile);
connect(ui->newPassConfirm, &QLineEdit::returnPressed, this, &LoginScreen::onCreateNewProfile);
2015-06-04 01:28:16 +08:00
connect(ui->loginButton, &QPushButton::clicked, this, &LoginScreen::onLogin);
connect(ui->loginUsernames, &QComboBox::currentTextChanged, this, &LoginScreen::onLoginUsernameSelected);
2015-06-04 16:56:48 +08:00
connect(ui->loginPassword, &QLineEdit::returnPressed, this, &LoginScreen::onLogin);
2015-06-04 05:20:47 +08:00
reset();
}
LoginScreen::~LoginScreen()
{
delete ui;
}
void LoginScreen::reset()
{
ui->newUsername->clear();
ui->newPass->clear();
ui->loginPassword->clear();
ui->loginUsernames->clear();
Profile::scanProfiles();
2015-06-04 20:19:18 +08:00
QString lastUsed = Settings::getInstance().getCurrentProfile();
qDebug() << "Last used is "<<lastUsed;
2015-06-04 05:20:47 +08:00
QVector<QString> profiles = Profile::getProfiles();
for (QString profile : profiles)
2015-06-04 20:19:18 +08:00
{
2015-06-04 05:20:47 +08:00
ui->loginUsernames->addItem(profile);
2015-06-04 20:19:18 +08:00
if (profile == lastUsed)
ui->loginUsernames->setCurrentIndex(ui->loginUsernames->count()-1);
}
2015-06-04 05:20:47 +08:00
if (profiles.isEmpty())
ui->stackedWidget->setCurrentIndex(0);
else
ui->stackedWidget->setCurrentIndex(1);
2015-06-04 01:04:28 +08:00
}
2015-06-04 01:28:16 +08:00
void LoginScreen::onNewProfilePageClicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
void LoginScreen::onLoginPageClicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void LoginScreen::onCreateNewProfile()
{
QString name = ui->newUsername->text();
QString pass = ui->newPass->text();
if (name.isEmpty())
{
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("The username must not be empty."));
return;
}
2015-06-04 01:28:16 +08:00
if (pass.size()!=0 && pass.size() < 6)
{
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("The password must be at least 6 characters."));
return;
}
if (ui->newPassConfirm->text() != pass)
{
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("The passwords are different."));
return;
}
if (Profile::profileExists(name))
{
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("This profile already exists."));
return;
}
Profile* profile = Profile::createProfile(name, pass);
if (!profile)
{
// Unknown error
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("Couldn't create a new profile."));
return;
}
Nexus& nexus = Nexus::getInstance();
nexus.setProfile(profile);
nexus.showMainGUI();
2015-06-04 01:28:16 +08:00
}
void LoginScreen::onLoginUsernameSelected(const QString &name)
{
if (name.isEmpty())
return;
ui->loginPassword->clear();
if (Profile::isEncrypted(name))
{
ui->loginPasswordLabel->show();
ui->loginPassword->show();
}
else
{
ui->loginPasswordLabel->hide();
ui->loginPassword->hide();
}
}
2015-06-04 01:28:16 +08:00
void LoginScreen::onLogin()
{
QString name = ui->loginUsernames->currentText();
QString pass = ui->loginPassword->text();
if (!ProfileLocker::isLockable(name))
{
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("This profile is already in use."));
return;
}
Profile* profile = Profile::loadProfile(name, pass);
if (!profile)
{
// Unknown error
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Couldn't load this profile."));
return;
}
2015-06-04 20:19:18 +08:00
if (!profile->checkPassword())
2015-06-04 08:43:07 +08:00
{
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Wrong password."));
delete profile;
return;
}
Nexus& nexus = Nexus::getInstance();
2015-06-04 01:28:16 +08:00
nexus.setProfile(profile);
nexus.showMainGUI();
2015-06-04 01:28:16 +08:00
}