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:
parent
032c561e62
commit
bf29d4a6d5
|
@ -1282,9 +1282,3 @@ void Settings::createPersonal(QString basename)
|
||||||
ps.beginGroup("Privacy");
|
ps.beginGroup("Privacy");
|
||||||
ps.endGroup();
|
ps.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Settings::profileExists(QString basename)
|
|
||||||
{
|
|
||||||
QString path = getSettingsDirPath() + QDir::separator() + basename;
|
|
||||||
return QFile::exists(path+".tox") && QFile::exists(path+".ini");
|
|
||||||
}
|
|
||||||
|
|
|
@ -38,7 +38,6 @@ public:
|
||||||
QString askProfiles();
|
QString askProfiles();
|
||||||
|
|
||||||
void createPersonal(QString basename); ///< Write a default personnal settings file for a profile
|
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);
|
void executeSettingsDialog(QWidget* parent);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include <src/widget/androidgui.h>
|
#include <src/widget/androidgui.h>
|
||||||
#else
|
#else
|
||||||
#include <src/widget/widget.h>
|
#include <src/widget/widget.h>
|
||||||
|
#include <QDesktopWidget>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static Nexus* nexus{nullptr};
|
static Nexus* nexus{nullptr};
|
||||||
|
@ -65,6 +66,9 @@ void Nexus::showLogin()
|
||||||
{
|
{
|
||||||
((QApplication*)qApp)->setQuitOnLastWindowClosed(true);
|
((QApplication*)qApp)->setQuitOnLastWindowClosed(true);
|
||||||
loginScreen->reset();
|
loginScreen->reset();
|
||||||
|
#ifndef Q_OS_ANDROID
|
||||||
|
loginScreen->move(QApplication::desktop()->screen()->rect().center() - loginScreen->rect().center());
|
||||||
|
#endif
|
||||||
loginScreen->show();
|
loginScreen->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@
|
||||||
|
|
||||||
QVector<QString> Profile::profiles;
|
QVector<QString> Profile::profiles;
|
||||||
|
|
||||||
Profile::Profile(QString name, QString password)
|
Profile::Profile(QString name, QString password, bool isNewProfile)
|
||||||
: name{name}, password{password}
|
: name{name}, password{password}, isNewProfile{isNewProfile}
|
||||||
{
|
{
|
||||||
coreThread = new QThread();
|
coreThread = new QThread();
|
||||||
coreThread->setObjectName("qTox Core");
|
coreThread->setObjectName("qTox Core");
|
||||||
|
@ -25,7 +25,8 @@ Profile* Profile::loadProfile(QString name, QString password)
|
||||||
{
|
{
|
||||||
if (ProfileLocker::hasLock())
|
if (ProfileLocker::hasLock())
|
||||||
{
|
{
|
||||||
|
qCritical() << "Tried to load profile "<<name<<", but another profile is already locked!";
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ProfileLocker::lock(name))
|
if (!ProfileLocker::lock(name))
|
||||||
|
@ -34,7 +35,31 @@ Profile* Profile::loadProfile(QString name, QString password)
|
||||||
return nullptr;
|
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()
|
Profile::~Profile()
|
||||||
|
@ -72,9 +97,8 @@ void Profile::scanProfiles()
|
||||||
|
|
||||||
void Profile::importProfile(QString name)
|
void Profile::importProfile(QString name)
|
||||||
{
|
{
|
||||||
Settings& s = Settings::getInstance();
|
assert(!profileExists(name));
|
||||||
assert(!s.profileExists(name));
|
Settings::getInstance().createPersonal(name);
|
||||||
s.createPersonal(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<QString> Profile::getProfiles()
|
QVector<QString> Profile::getProfiles()
|
||||||
|
@ -95,8 +119,14 @@ void Profile::startCore()
|
||||||
QByteArray Profile::loadToxSave()
|
QByteArray Profile::loadToxSave()
|
||||||
{
|
{
|
||||||
QByteArray data;
|
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);
|
QFile saveFile(path);
|
||||||
qint64 fileSize;
|
qint64 fileSize;
|
||||||
qDebug() << "Loading tox save "<<path;
|
qDebug() << "Loading tox save "<<path;
|
||||||
|
@ -148,6 +178,12 @@ fail:
|
||||||
return data;
|
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)
|
bool Profile::isProfileEncrypted(QString name)
|
||||||
{
|
{
|
||||||
uint8_t data[encryptHeaderSize] = {0};
|
uint8_t data[encryptHeaderSize] = {0};
|
||||||
|
|
|
@ -14,7 +14,11 @@ class Profile
|
||||||
public:
|
public:
|
||||||
/// Locks and loads an existing profile and create the associate Core* instance
|
/// 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
|
/// 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();
|
~Profile();
|
||||||
|
|
||||||
Core* getCore();
|
Core* getCore();
|
||||||
|
@ -26,11 +30,11 @@ public:
|
||||||
static void scanProfiles();
|
static void scanProfiles();
|
||||||
static QVector<QString> getProfiles();
|
static QVector<QString> getProfiles();
|
||||||
|
|
||||||
/// Checks whether a profile is encrypted. Return false on error.
|
static bool profileExists(QString name);
|
||||||
static bool isProfileEncrypted(QString name);
|
static bool isProfileEncrypted(QString name); ///< Returns false on error.
|
||||||
|
|
||||||
private:
|
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
|
/// Lists all the files in the config dir with a given extension
|
||||||
/// Pass the raw extension, e.g. "jpeg" not ".jpeg".
|
/// Pass the raw extension, e.g. "jpeg" not ".jpeg".
|
||||||
static QVector<QString> getFilesByExt(QString extension);
|
static QVector<QString> getFilesByExt(QString extension);
|
||||||
|
@ -43,6 +47,7 @@ private:
|
||||||
QThread* coreThread;
|
QThread* coreThread;
|
||||||
QString name, password;
|
QString name, password;
|
||||||
static QVector<QString> profiles;
|
static QVector<QString> profiles;
|
||||||
|
bool isNewProfile;
|
||||||
/// How much data we need to read to check if the file is encrypted
|
/// 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
|
/// Must be >= TOX_ENC_SAVE_MAGIC_LENGTH (8), which isn't publicly defined
|
||||||
static constexpr int encryptHeaderSize = 8;
|
static constexpr int encryptHeaderSize = 8;
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
#include "loginscreen.h"
|
#include "loginscreen.h"
|
||||||
#include "ui_loginscreen.h"
|
#include "ui_loginscreen.h"
|
||||||
#include "src/profile.h"
|
#include "src/profile.h"
|
||||||
|
#include "src/profilelocker.h"
|
||||||
|
#include "src/nexus.h"
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
LoginScreen::LoginScreen(QWidget *parent) :
|
LoginScreen::LoginScreen(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
|
@ -51,8 +54,40 @@ void LoginScreen::onLoginPageClicked()
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoginScreen::onCreateNewProfile()
|
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)
|
void LoginScreen::onLoginUsernameSelected(const QString &name)
|
||||||
|
@ -60,6 +95,7 @@ void LoginScreen::onLoginUsernameSelected(const QString &name)
|
||||||
if (name.isEmpty())
|
if (name.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ui->loginPassword->clear();
|
||||||
if (Profile::isProfileEncrypted(name))
|
if (Profile::isProfileEncrypted(name))
|
||||||
{
|
{
|
||||||
ui->loginPasswordLabel->show();
|
ui->loginPasswordLabel->show();
|
||||||
|
@ -74,5 +110,25 @@ void LoginScreen::onLoginUsernameSelected(const QString &name)
|
||||||
|
|
||||||
void LoginScreen::onLogin()
|
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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user