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

refactor(profile): simplify Profile constructor

Simplifies Profile constructor by passing Settings instance by
reference during loadProfile and createNewProfile calls.
This commit is contained in:
jenli669 2019-06-28 10:11:32 +02:00
parent 5a3b720513
commit 00449937d4
No known key found for this signature in database
GPG Key ID: 8267F9F7C2BF7E5E
4 changed files with 25 additions and 23 deletions

View File

@ -341,7 +341,7 @@ int main(int argc, char* argv[])
// TODO (kriby): Shift responsibility of linking views to model objects from nexus
// Further: generate view instances separately (loginScreen, mainGUI, audio)
if (autoLogin && Profile::exists(profileName) && !Profile::isEncrypted(profileName)) {
Profile* profile = Profile::loadProfile(profileName);
Profile* profile = Profile::loadProfile(profileName, QString(), settings);
settings.updateProfileData(profile);
nexus.bootstrapWithProfile(profile);
} else {

View File

@ -292,7 +292,7 @@ Profile* Nexus::getProfile()
*/
void Nexus::onCreateNewProfile(const QString& name, const QString& pass)
{
setProfile(Profile::createProfile(name, pass));
setProfile(Profile::createProfile(name, pass, *settings));
}
/**
@ -300,7 +300,7 @@ void Nexus::onCreateNewProfile(const QString& name, const QString& pass)
*/
void Nexus::onLoadProfile(const QString& name, const QString& pass)
{
setProfile(Profile::loadProfile(name, pass));
setProfile(Profile::loadProfile(name, pass, *settings));
}
/**
* Changes the loaded profile and notifies listeners.

View File

@ -275,20 +275,12 @@ void Profile::initCore(const QByteArray& toxsave, const ICoreSettings& s, bool i
Qt::ConnectionType::QueuedConnection);
}
Profile::Profile(const QString& name, const QString& password, bool isNewProfile,
const QByteArray& toxsave, std::unique_ptr<ToxEncrypt> passkey)
Profile::Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> passkey)
: name{name}
, passkey{std::move(passkey)}
, isRemoved{false}
, encrypted{this->passkey != nullptr}
{
Settings& s = Settings::getInstance();
// TODO(kriby): Move/refactor core initialization to remove settings dependency
// note to self: use slots/signals for this?
initCore(toxsave, s, isNewProfile);
loadDatabase(password);
}
{}
/**
* @brief Locks and loads an existing profile and creates the associate Core* instance.
@ -298,7 +290,7 @@ Profile::Profile(const QString& name, const QString& password, bool isNewProfile
*
* @note If the profile is already in use return nullptr.
*/
Profile* Profile::loadProfile(const QString& name, const QString& password)
Profile* Profile::loadProfile(const QString& name, const QString& password, const Settings& settings)
{
if (ProfileLocker::hasLock()) {
qCritical() << "Tried to load profile " << name << ", but another profile is already locked!";
@ -312,7 +304,7 @@ Profile* Profile::loadProfile(const QString& name, const QString& password)
LoadToxDataError error;
QByteArray toxsave = QByteArray();
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QString path = settings.getSettingsDirPath() + name + ".tox";
std::unique_ptr<ToxEncrypt> tmpKey = loadToxData(password, path, toxsave, error);
if (logLoadToxDataError(error, path)) {
@ -320,7 +312,12 @@ Profile* Profile::loadProfile(const QString& name, const QString& password)
return nullptr;
}
return new Profile(name, password, false, toxsave, std::move(tmpKey));
Profile* p = new Profile(name, password, std::move(tmpKey));
p->initCore(toxsave, settings, /*isNewProfile*/ false);
p->loadDatabase(password);
return p;
}
/**
@ -331,7 +328,8 @@ Profile* Profile::loadProfile(const QString& name, const QString& password)
*
* @note If the profile is already in use return nullptr.
*/
Profile* Profile::createProfile(const QString& userName, const QString& password)
Profile* Profile::createProfile(const QString& userName, const QString& password,
const Settings& settings)
{
CreateToxDataError error;
QString path = Settings::getInstance().getSettingsDirPath() + userName + ".tox";
@ -341,8 +339,10 @@ Profile* Profile::createProfile(const QString& userName, const QString& password
return nullptr;
}
Settings::getInstance().createPersonal(userName);
Profile* p = new Profile(userName, password, true, QByteArray(), std::move(tmpKey));
settings.createPersonal(userName);
Profile* p = new Profile(userName, password, std::move(tmpKey));
p->initCore(QByteArray(), settings, /*isNewProfile*/ true);
p->loadDatabase(password);
return p;
}

View File

@ -34,13 +34,16 @@
#include <QVector>
#include <memory>
class Settings;
class Profile : public QObject
{
Q_OBJECT
public:
static Profile* loadProfile(const QString& name, const QString& password = QString());
static Profile* createProfile(const QString& name, const QString& password);
static Profile* loadProfile(const QString& name, const QString& password, const Settings& settings);
static Profile* createProfile(const QString& name, const QString& password,
const Settings& settings);
~Profile();
Core* getCore();
@ -98,8 +101,7 @@ private slots:
void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash);
private:
Profile(const QString& name, const QString& password, bool newProfile, const QByteArray& toxsave,
std::unique_ptr<ToxEncrypt> passKey);
Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> passkey);
static QStringList getFilesByExt(QString extension);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
bool saveToxSave(QByteArray data);