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

Implement the -p option

This commit is contained in:
tux3 2015-06-04 14:19:18 +02:00
parent 1ffb2d4a92
commit 42a7efb053
9 changed files with 62 additions and 49 deletions

View File

@ -21,6 +21,8 @@
#include "src/misc/settings.h" #include "src/misc/settings.h"
#include "src/misc/cstring.h" #include "src/misc/cstring.h"
#include "src/historykeeper.h" #include "src/historykeeper.h"
#include "src/nexus.h"
#include "src/profile.h"
#include <tox/tox.h> #include <tox/tox.h>
#include <tox/toxencryptsave.h> #include <tox/toxencryptsave.h>
#include <QApplication> #include <QApplication>
@ -139,7 +141,7 @@ void Core::checkEncryptedHistory()
return; return;
} }
QString a(tr("Please enter the password for the chat history for the %1 profile.", "used in load() when no hist pw set").arg(Settings::getInstance().getCurrentProfile())); QString a(tr("Please enter the password for the chat history for the profile \"%1\".", "used in load() when no hist pw set").arg(Nexus::getProfile()->getName()));
QString b(tr("The previous password is incorrect; please try again:", "used on retries in load()")); QString b(tr("The previous password is incorrect; please try again:", "used on retries in load()"));
QString c(tr("\nDisabling chat history now will leave the encrypted history intact (but not usable); if you later remember the password, you may re-enable encryption from the Privacy tab with the correct password to use the history.", "part of history password dialog")); QString c(tr("\nDisabling chat history now will leave the encrypted history intact (but not usable); if you later remember the password, you may re-enable encryption from the Privacy tab with the correct password to use the history.", "part of history password dialog"));
QString dialogtxt; QString dialogtxt;

View File

@ -15,6 +15,8 @@
#include "historykeeper.h" #include "historykeeper.h"
#include "misc/settings.h" #include "misc/settings.h"
#include "src/core/core.h" #include "src/core/core.h"
#include "src/nexus.h"
#include "src/profile.h"
#include <QSqlError> #include <QSqlError>
#include <QFile> #include <QFile>
@ -74,7 +76,7 @@ bool HistoryKeeper::checkPassword(int encrypted)
return true; return true;
if ((encrypted == 1) || (encrypted == -1 && Settings::getInstance().getEncryptLogs())) if ((encrypted == 1) || (encrypted == -1 && Settings::getInstance().getEncryptLogs()))
return EncryptedDb::check(getHistoryPath(Settings::getInstance().getCurrentProfile(), encrypted)); return EncryptedDb::check(getHistoryPath(Nexus::getProfile()->getName(), encrypted));
return true; return true;
} }

View File

@ -20,6 +20,7 @@
#include "src/widget/toxuri.h" #include "src/widget/toxuri.h"
#include "src/widget/toxsave.h" #include "src/widget/toxsave.h"
#include "src/autoupdate.h" #include "src/autoupdate.h"
#include "src/profile.h"
#include "src/profilelocker.h" #include "src/profilelocker.h"
#include "src/widget/loginscreen.h" #include "src/widget/loginscreen.h"
#include <QApplication> #include <QApplication>
@ -113,15 +114,28 @@ int main(int argc, char *argv[])
if (parser.isSet("p")) if (parser.isSet("p"))
{ {
QString profile = parser.value("p"); QString profileName = parser.value("p");
if (QDir(Settings::getSettingsDirPath()).exists(profile + ".tox")) if (QDir(Settings::getSettingsDirPath()).exists(profileName + ".tox"))
{ {
qDebug() << "Setting profile to" << profile; qDebug() << "Setting profile to" << profileName;
Settings::getInstance().switchProfile(profile); if (Profile::isProfileEncrypted(profileName))
{
Settings::getInstance().setCurrentProfile(profileName);
} }
else else
{ {
qCritical() << "-p profile" << profile + ".tox" << "doesn't exist"; Profile* profile = Profile::loadProfile(profileName);
if (!profile)
{
qCritical() << "-p profile" << profileName + ".tox" << " couldn't be loaded";
return EXIT_FAILURE;
}
Nexus::getInstance().setProfile(profile);
}
}
else
{
qCritical() << "-p profile" << profileName + ".tox" << "doesn't exist";
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }

View File

@ -40,8 +40,7 @@
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) true #define SHOW_SYSTEM_TRAY_DEFAULT (bool) true
const QString Settings::OLDFILENAME = "settings.ini"; const QString Settings::globalSettingsFile = "qtox.ini";
const QString Settings::FILENAME = "qtox.ini";
Settings* Settings::settings{nullptr}; Settings* Settings::settings{nullptr};
bool Settings::makeToxPortable{false}; bool Settings::makeToxPortable{false};
@ -59,38 +58,17 @@ Settings& Settings::getInstance()
return *settings; return *settings;
} }
void Settings::switchProfile(const QString& profile)
{
// Saves current profile as main profile if this instance is main instance
setCurrentProfile(profile);
save(false);
// If this instance is not main instance previous save did not happen therefore
// we manually set profile again and load profile settings
setCurrentProfile(profile);
loaded = false;
load();
}
void Settings::load() void Settings::load()
{ {
if (loaded) if (loaded)
return; return;
createSettingsDir();
QDir dir(getSettingsDirPath()); QDir dir(getSettingsDirPath());
if (!dir.exists())
dir.mkpath(".");
if (QFile(FILENAME).exists()) if (QFile(globalSettingsFile).exists())
{ {
QSettings ps(FILENAME, QSettings::IniFormat); QSettings ps(globalSettingsFile, QSettings::IniFormat);
ps.beginGroup("General");
makeToxPortable = ps.value("makeToxPortable", false).toBool();
ps.endGroup();
}
else if (QFile(OLDFILENAME).exists())
{
QSettings ps(OLDFILENAME, QSettings::IniFormat);
ps.beginGroup("General"); ps.beginGroup("General");
makeToxPortable = ps.value("makeToxPortable", false).toBool(); makeToxPortable = ps.value("makeToxPortable", false).toBool();
ps.endGroup(); ps.endGroup();
@ -100,16 +78,13 @@ void Settings::load()
makeToxPortable = false; makeToxPortable = false;
} }
QString filePath = dir.filePath(FILENAME); QString filePath = dir.filePath(globalSettingsFile);
//if no settings file exist -- use the default one // If no settings file exist -- use the default one
if (!QFile(filePath).exists()) if (!QFile(filePath).exists())
{
if (!QFile(filePath = dir.filePath(OLDFILENAME)).exists())
{ {
qDebug() << "No settings file found, using defaults"; qDebug() << "No settings file found, using defaults";
filePath = ":/conf/" + FILENAME; filePath = ":/conf/" + globalSettingsFile;
}
} }
qDebug() << "Loading settings from " + filePath; qDebug() << "Loading settings from " + filePath;
@ -282,7 +257,7 @@ void Settings::load()
void Settings::save(bool writePersonal) void Settings::save(bool writePersonal)
{ {
QString filePath = QDir(getSettingsDirPath()).filePath(FILENAME); QString filePath = QDir(getSettingsDirPath()).filePath(globalSettingsFile);
save(filePath, writePersonal); save(filePath, writePersonal);
} }
@ -533,7 +508,7 @@ bool Settings::getMakeToxPortable() const
void Settings::setMakeToxPortable(bool newValue) void Settings::setMakeToxPortable(bool newValue)
{ {
makeToxPortable = newValue; makeToxPortable = newValue;
save(FILENAME); // Commit to the portable file that we don't want to use it save(globalSettingsFile); // Commit to the portable file that we don't want to use it
if (!newValue) // Update the new file right now if not already done if (!newValue) // Update the new file right now if not already done
save(); save();
} }

View File

@ -32,7 +32,6 @@ class Settings : public QObject
public: public:
~Settings() = default; ~Settings() = default;
static Settings& getInstance(); static Settings& getInstance();
void switchProfile(const QString& profile);
void createSettingsDir(); ///< Creates a path to the settings dir, if it doesn't already exist void createSettingsDir(); ///< Creates a path to the settings dir, if it doesn't already exist
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
@ -267,7 +266,7 @@ private:
void saveGlobal(QString path); void saveGlobal(QString path);
void savePersonal(QString path); void savePersonal(QString path);
static const QString FILENAME; static const QString globalSettingsFile;
static const QString OLDFILENAME; static const QString OLDFILENAME;
bool loaded; bool loaded;

View File

@ -62,8 +62,11 @@ void Nexus::start()
qRegisterMetaType<Core::PasswordType>("Core::PasswordType"); qRegisterMetaType<Core::PasswordType>("Core::PasswordType");
qRegisterMetaType<std::shared_ptr<VideoFrame>>("std::shared_ptr<VideoFrame>"); qRegisterMetaType<std::shared_ptr<VideoFrame>>("std::shared_ptr<VideoFrame>");
// Create and show login screen
loginScreen = new LoginScreen(); loginScreen = new LoginScreen();
if (profile)
showMainGUI();
else
showLogin(); showLogin();
} }

View File

@ -16,6 +16,8 @@ Profile::Profile(QString name, QString password, bool isNewProfile)
: name{name}, password{password}, : name{name}, password{password},
newProfile{isNewProfile}, isRemoved{false} newProfile{isNewProfile}, isRemoved{false}
{ {
Settings::getInstance().setCurrentProfile(name);
coreThread = new QThread(); coreThread = new QThread();
coreThread->setObjectName("qTox Core"); coreThread->setObjectName("qTox Core");
core = new Core(coreThread, *this); core = new Core(coreThread, *this);
@ -293,3 +295,11 @@ bool Profile::rename(QString newName)
name = newName; name = newName;
return true; return true;
} }
bool Profile::checkPassword()
{
if (isRemoved)
return false;
return !loadToxSave().isEmpty();
}

View File

@ -14,7 +14,7 @@ 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
static Profile* loadProfile(QString name, QString password); static Profile* loadProfile(QString name, QString password = QString());
/// Creates a new profile and the associated Core* instance /// Creates a new profile and the associated Core* instance
/// If password is not empty, the profile will be encrypted /// If password is not empty, the profile will be encrypted
/// Returns a nullptr on error, for example if the profile already exists /// Returns a nullptr on error, for example if the profile already exists
@ -26,6 +26,7 @@ public:
void startCore(); ///< Starts the Core thread void startCore(); ///< Starts the Core thread
bool isNewProfile(); bool isNewProfile();
bool checkPassword(); ///< Checks whether the password is valid
QByteArray loadToxSave(); ///< Loads the profile's .tox save from file, unencrypted QByteArray loadToxSave(); ///< Loads the profile's .tox save from file, unencrypted
void saveToxSave(); ///< Saves the profile's .tox save, encrypted if needed. Invalid on deleted profiles. void saveToxSave(); ///< Saves the profile's .tox save, encrypted if needed. Invalid on deleted profiles.
void saveToxSave(QByteArray data); ///< Write the .tox save, encrypted if needed. Invalid on deleted profiles. void saveToxSave(QByteArray data); ///< Write the .tox save, encrypted if needed. Invalid on deleted profiles.

View File

@ -3,7 +3,9 @@
#include "src/profile.h" #include "src/profile.h"
#include "src/profilelocker.h" #include "src/profilelocker.h"
#include "src/nexus.h" #include "src/nexus.h"
#include "src/misc/settings.h"
#include <QMessageBox> #include <QMessageBox>
#include <QDebug>
LoginScreen::LoginScreen(QWidget *parent) : LoginScreen::LoginScreen(QWidget *parent) :
QWidget(parent), QWidget(parent),
@ -37,9 +39,15 @@ void LoginScreen::reset()
ui->loginUsernames->clear(); ui->loginUsernames->clear();
Profile::scanProfiles(); Profile::scanProfiles();
QString lastUsed = Settings::getInstance().getCurrentProfile();
qDebug() << "Last used is "<<lastUsed;
QVector<QString> profiles = Profile::getProfiles(); QVector<QString> profiles = Profile::getProfiles();
for (QString profile : profiles) for (QString profile : profiles)
{
ui->loginUsernames->addItem(profile); ui->loginUsernames->addItem(profile);
if (profile == lastUsed)
ui->loginUsernames->setCurrentIndex(ui->loginUsernames->count()-1);
}
if (profiles.isEmpty()) if (profiles.isEmpty())
ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget->setCurrentIndex(0);
@ -130,9 +138,8 @@ void LoginScreen::onLogin()
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Couldn't load this profile.")); QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Couldn't load this profile."));
return; return;
} }
if (profile->loadToxSave().isEmpty()) if (!profile->checkPassword())
{ {
// Unknown error
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Wrong password.")); QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Wrong password."));
delete profile; delete profile;
return; return;