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:
parent
1ffb2d4a92
commit
42a7efb053
|
@ -21,6 +21,8 @@
|
|||
#include "src/misc/settings.h"
|
||||
#include "src/misc/cstring.h"
|
||||
#include "src/historykeeper.h"
|
||||
#include "src/nexus.h"
|
||||
#include "src/profile.h"
|
||||
#include <tox/tox.h>
|
||||
#include <tox/toxencryptsave.h>
|
||||
#include <QApplication>
|
||||
|
@ -139,7 +141,7 @@ void Core::checkEncryptedHistory()
|
|||
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 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;
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
#include "historykeeper.h"
|
||||
#include "misc/settings.h"
|
||||
#include "src/core/core.h"
|
||||
#include "src/nexus.h"
|
||||
#include "src/profile.h"
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QFile>
|
||||
|
@ -74,7 +76,7 @@ bool HistoryKeeper::checkPassword(int encrypted)
|
|||
return true;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
24
src/main.cpp
24
src/main.cpp
|
@ -20,6 +20,7 @@
|
|||
#include "src/widget/toxuri.h"
|
||||
#include "src/widget/toxsave.h"
|
||||
#include "src/autoupdate.h"
|
||||
#include "src/profile.h"
|
||||
#include "src/profilelocker.h"
|
||||
#include "src/widget/loginscreen.h"
|
||||
#include <QApplication>
|
||||
|
@ -113,15 +114,28 @@ int main(int argc, char *argv[])
|
|||
|
||||
if (parser.isSet("p"))
|
||||
{
|
||||
QString profile = parser.value("p");
|
||||
if (QDir(Settings::getSettingsDirPath()).exists(profile + ".tox"))
|
||||
QString profileName = parser.value("p");
|
||||
if (QDir(Settings::getSettingsDirPath()).exists(profileName + ".tox"))
|
||||
{
|
||||
qDebug() << "Setting profile to" << profile;
|
||||
Settings::getInstance().switchProfile(profile);
|
||||
qDebug() << "Setting profile to" << profileName;
|
||||
if (Profile::isProfileEncrypted(profileName))
|
||||
{
|
||||
Settings::getInstance().setCurrentProfile(profileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
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" << profile + ".tox" << "doesn't exist";
|
||||
qCritical() << "-p profile" << profileName + ".tox" << "doesn't exist";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,8 +40,7 @@
|
|||
|
||||
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) true
|
||||
|
||||
const QString Settings::OLDFILENAME = "settings.ini";
|
||||
const QString Settings::FILENAME = "qtox.ini";
|
||||
const QString Settings::globalSettingsFile = "qtox.ini";
|
||||
Settings* Settings::settings{nullptr};
|
||||
bool Settings::makeToxPortable{false};
|
||||
|
||||
|
@ -59,38 +58,17 @@ Settings& Settings::getInstance()
|
|||
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()
|
||||
{
|
||||
if (loaded)
|
||||
return;
|
||||
|
||||
createSettingsDir();
|
||||
QDir dir(getSettingsDirPath());
|
||||
if (!dir.exists())
|
||||
dir.mkpath(".");
|
||||
|
||||
if (QFile(FILENAME).exists())
|
||||
if (QFile(globalSettingsFile).exists())
|
||||
{
|
||||
QSettings ps(FILENAME, QSettings::IniFormat);
|
||||
ps.beginGroup("General");
|
||||
makeToxPortable = ps.value("makeToxPortable", false).toBool();
|
||||
ps.endGroup();
|
||||
}
|
||||
else if (QFile(OLDFILENAME).exists())
|
||||
{
|
||||
QSettings ps(OLDFILENAME, QSettings::IniFormat);
|
||||
QSettings ps(globalSettingsFile, QSettings::IniFormat);
|
||||
ps.beginGroup("General");
|
||||
makeToxPortable = ps.value("makeToxPortable", false).toBool();
|
||||
ps.endGroup();
|
||||
|
@ -100,16 +78,13 @@ void Settings::load()
|
|||
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 = dir.filePath(OLDFILENAME)).exists())
|
||||
{
|
||||
qDebug() << "No settings file found, using defaults";
|
||||
filePath = ":/conf/" + FILENAME;
|
||||
}
|
||||
qDebug() << "No settings file found, using defaults";
|
||||
filePath = ":/conf/" + globalSettingsFile;
|
||||
}
|
||||
|
||||
qDebug() << "Loading settings from " + filePath;
|
||||
|
@ -282,7 +257,7 @@ void Settings::load()
|
|||
|
||||
void Settings::save(bool writePersonal)
|
||||
{
|
||||
QString filePath = QDir(getSettingsDirPath()).filePath(FILENAME);
|
||||
QString filePath = QDir(getSettingsDirPath()).filePath(globalSettingsFile);
|
||||
save(filePath, writePersonal);
|
||||
}
|
||||
|
||||
|
@ -533,7 +508,7 @@ bool Settings::getMakeToxPortable() const
|
|||
void Settings::setMakeToxPortable(bool 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
|
||||
save();
|
||||
}
|
||||
|
|
|
@ -32,7 +32,6 @@ class Settings : public QObject
|
|||
public:
|
||||
~Settings() = default;
|
||||
static Settings& getInstance();
|
||||
void switchProfile(const QString& profile);
|
||||
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
|
||||
|
@ -267,7 +266,7 @@ private:
|
|||
void saveGlobal(QString path);
|
||||
void savePersonal(QString path);
|
||||
|
||||
static const QString FILENAME;
|
||||
static const QString globalSettingsFile;
|
||||
static const QString OLDFILENAME;
|
||||
|
||||
bool loaded;
|
||||
|
|
|
@ -62,9 +62,12 @@ void Nexus::start()
|
|||
qRegisterMetaType<Core::PasswordType>("Core::PasswordType");
|
||||
qRegisterMetaType<std::shared_ptr<VideoFrame>>("std::shared_ptr<VideoFrame>");
|
||||
|
||||
// Create and show login screen
|
||||
loginScreen = new LoginScreen();
|
||||
showLogin();
|
||||
|
||||
if (profile)
|
||||
showMainGUI();
|
||||
else
|
||||
showLogin();
|
||||
}
|
||||
|
||||
void Nexus::showLogin()
|
||||
|
|
|
@ -16,6 +16,8 @@ Profile::Profile(QString name, QString password, bool isNewProfile)
|
|||
: name{name}, password{password},
|
||||
newProfile{isNewProfile}, isRemoved{false}
|
||||
{
|
||||
Settings::getInstance().setCurrentProfile(name);
|
||||
|
||||
coreThread = new QThread();
|
||||
coreThread->setObjectName("qTox Core");
|
||||
core = new Core(coreThread, *this);
|
||||
|
@ -293,3 +295,11 @@ bool Profile::rename(QString newName)
|
|||
name = newName;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Profile::checkPassword()
|
||||
{
|
||||
if (isRemoved)
|
||||
return false;
|
||||
|
||||
return !loadToxSave().isEmpty();
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ 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
|
||||
static Profile* loadProfile(QString name, QString password);
|
||||
static Profile* loadProfile(QString name, QString password = QString());
|
||||
/// 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
|
||||
|
@ -26,6 +26,7 @@ public:
|
|||
|
||||
void startCore(); ///< Starts the Core thread
|
||||
bool isNewProfile();
|
||||
bool checkPassword(); ///< Checks whether the password is valid
|
||||
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(QByteArray data); ///< Write the .tox save, encrypted if needed. Invalid on deleted profiles.
|
||||
|
|
|
@ -3,7 +3,9 @@
|
|||
#include "src/profile.h"
|
||||
#include "src/profilelocker.h"
|
||||
#include "src/nexus.h"
|
||||
#include "src/misc/settings.h"
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
|
||||
LoginScreen::LoginScreen(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
|
@ -37,9 +39,15 @@ void LoginScreen::reset()
|
|||
|
||||
ui->loginUsernames->clear();
|
||||
Profile::scanProfiles();
|
||||
QString lastUsed = Settings::getInstance().getCurrentProfile();
|
||||
qDebug() << "Last used is "<<lastUsed;
|
||||
QVector<QString> profiles = Profile::getProfiles();
|
||||
for (QString profile : profiles)
|
||||
{
|
||||
ui->loginUsernames->addItem(profile);
|
||||
if (profile == lastUsed)
|
||||
ui->loginUsernames->setCurrentIndex(ui->loginUsernames->count()-1);
|
||||
}
|
||||
|
||||
if (profiles.isEmpty())
|
||||
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."));
|
||||
return;
|
||||
}
|
||||
if (profile->loadToxSave().isEmpty())
|
||||
if (!profile->checkPassword())
|
||||
{
|
||||
// Unknown error
|
||||
QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Wrong password."));
|
||||
delete profile;
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue
Block a user