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

Allow to load/create from login screen

Saving is not implemented, so bad things will happen if you let qTox save those profiles
This commit is contained in:
tux3 2015-06-04 02:10:06 +02:00
parent 032c561e62
commit bf29d4a6d5
6 changed files with 114 additions and 20 deletions

View File

@ -1282,9 +1282,3 @@ void Settings::createPersonal(QString basename)
ps.beginGroup("Privacy");
ps.endGroup();
}
bool Settings::profileExists(QString basename)
{
QString path = getSettingsDirPath() + QDir::separator() + basename;
return QFile::exists(path+".tox") && QFile::exists(path+".ini");
}

View File

@ -38,7 +38,6 @@ public:
QString askProfiles();
void createPersonal(QString basename); ///< Write a default personnal settings file for a profile
bool profileExists(QString basename); ///< Return true if the given profile (tox AND ini) exists
void executeSettingsDialog(QWidget* parent);

View File

@ -16,6 +16,7 @@
#include <src/widget/androidgui.h>
#else
#include <src/widget/widget.h>
#include <QDesktopWidget>
#endif
static Nexus* nexus{nullptr};
@ -65,6 +66,9 @@ void Nexus::showLogin()
{
((QApplication*)qApp)->setQuitOnLastWindowClosed(true);
loginScreen->reset();
#ifndef Q_OS_ANDROID
loginScreen->move(QApplication::desktop()->screen()->rect().center() - loginScreen->rect().center());
#endif
loginScreen->show();
}

View File

@ -11,8 +11,8 @@
QVector<QString> Profile::profiles;
Profile::Profile(QString name, QString password)
: name{name}, password{password}
Profile::Profile(QString name, QString password, bool isNewProfile)
: name{name}, password{password}, isNewProfile{isNewProfile}
{
coreThread = new QThread();
coreThread->setObjectName("qTox Core");
@ -25,7 +25,8 @@ Profile* Profile::loadProfile(QString name, QString password)
{
if (ProfileLocker::hasLock())
{
qCritical() << "Tried to load profile "<<name<<", but another profile is already locked!";
return nullptr;
}
if (!ProfileLocker::lock(name))
@ -34,7 +35,31 @@ Profile* Profile::loadProfile(QString name, QString password)
return nullptr;
}
return new Profile(name, password);
return new Profile(name, password, false);
}
Profile* Profile::createProfile(QString name, QString password)
{
if (ProfileLocker::hasLock())
{
qCritical() << "Tried to create profile "<<name<<", but another profile is already locked!";
return nullptr;
}
if (profileExists(name))
{
qCritical() << "Tried to create profile "<<name<<", but it already exists!";
return nullptr;
}
if (!ProfileLocker::lock(name))
{
qWarning() << "Failed to lock profile "<<name;
return nullptr;
}
Settings::getInstance().createPersonal(name);
return new Profile(name, password, true);
}
Profile::~Profile()
@ -72,9 +97,8 @@ void Profile::scanProfiles()
void Profile::importProfile(QString name)
{
Settings& s = Settings::getInstance();
assert(!s.profileExists(name));
s.createPersonal(name);
assert(!profileExists(name));
Settings::getInstance().createPersonal(name);
}
QVector<QString> Profile::getProfiles()
@ -95,8 +119,14 @@ void Profile::startCore()
QByteArray Profile::loadToxSave()
{
QByteArray data;
QString path = Settings::getSettingsDirPath() + QDir::separator() + name;
if (isNewProfile)
{
qDebug() << "Loading empty data for new profile";
return data;
}
QString path = Settings::getSettingsDirPath() + QDir::separator() + name + ".tox";
QFile saveFile(path);
qint64 fileSize;
qDebug() << "Loading tox save "<<path;
@ -148,6 +178,12 @@ fail:
return data;
}
bool Profile::profileExists(QString name)
{
QString path = Settings::getSettingsDirPath() + QDir::separator() + name;
return QFile::exists(path+".tox") && QFile::exists(path+".ini");
}
bool Profile::isProfileEncrypted(QString name)
{
uint8_t data[encryptHeaderSize] = {0};

View File

@ -14,7 +14,11 @@ class Profile
public:
/// Locks and loads an existing profile and create the associate Core* instance
/// Returns a nullptr on error, for example if the profile is already in use
Profile* loadProfile(QString name, QString password);
static Profile* loadProfile(QString name, QString password);
/// Creates a new profile and the associated Core* instance
/// If password is not empty, the profile will be encrypted
/// Returns a nullptr on error, for example if the profile already exists
static Profile* createProfile(QString name, QString password);
~Profile();
Core* getCore();
@ -26,11 +30,11 @@ public:
static void scanProfiles();
static QVector<QString> getProfiles();
/// Checks whether a profile is encrypted. Return false on error.
static bool isProfileEncrypted(QString name);
static bool profileExists(QString name);
static bool isProfileEncrypted(QString name); ///< Returns false on error.
private:
Profile(QString name, QString password);
Profile(QString name, QString password, bool isNewProfile);
/// Lists all the files in the config dir with a given extension
/// Pass the raw extension, e.g. "jpeg" not ".jpeg".
static QVector<QString> getFilesByExt(QString extension);
@ -43,6 +47,7 @@ private:
QThread* coreThread;
QString name, password;
static QVector<QString> profiles;
bool isNewProfile;
/// How much data we need to read to check if the file is encrypted
/// Must be >= TOX_ENC_SAVE_MAGIC_LENGTH (8), which isn't publicly defined
static constexpr int encryptHeaderSize = 8;

View File

@ -1,6 +1,9 @@
#include "loginscreen.h"
#include "ui_loginscreen.h"
#include "src/profile.h"
#include "src/profilelocker.h"
#include "src/nexus.h"
#include <QMessageBox>
LoginScreen::LoginScreen(QWidget *parent) :
QWidget(parent),
@ -51,8 +54,40 @@ void LoginScreen::onLoginPageClicked()
}
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;
}
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();
}
void LoginScreen::onLoginUsernameSelected(const QString &name)
@ -60,6 +95,7 @@ void LoginScreen::onLoginUsernameSelected(const QString &name)
if (name.isEmpty())
return;
ui->loginPassword->clear();
if (Profile::isProfileEncrypted(name))
{
ui->loginPasswordLabel->show();
@ -74,5 +110,25 @@ void LoginScreen::onLoginUsernameSelected(const QString &name)
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;
}
Nexus& nexus = Nexus::getInstance();
nexus.setProfile(profile);
nexus.showMainGUI();
}