From 00449937d41b9fc2c67e0ac86c96a830ee8849f1 Mon Sep 17 00:00:00 2001 From: jenli669 Date: Fri, 28 Jun 2019 10:11:32 +0200 Subject: [PATCH] refactor(profile): simplify Profile constructor Simplifies Profile constructor by passing Settings instance by reference during loadProfile and createNewProfile calls. --- src/main.cpp | 2 +- src/nexus.cpp | 4 ++-- src/persistence/profile.cpp | 32 ++++++++++++++++---------------- src/persistence/profile.h | 10 ++++++---- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cf8aca658..919a3de52 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 { diff --git a/src/nexus.cpp b/src/nexus.cpp index ccc66fc45..9c4fba2c1 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -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. diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 055f5ef3a..c4b287073 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -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 passkey) +Profile::Profile(const QString& name, const QString& password, std::unique_ptr 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 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; } diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 0e7fa454b..a251cd9a8 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -34,13 +34,16 @@ #include #include +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 passKey); + Profile(const QString& name, const QString& password, std::unique_ptr passkey); static QStringList getFilesByExt(QString extension); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); bool saveToxSave(QByteArray data);