From db205bea57b2aa3d76ae1523feac6fac92d72c9e Mon Sep 17 00:00:00 2001 From: sudden6 Date: Fri, 28 Aug 2020 01:33:11 +0200 Subject: [PATCH] refactor(profile): remove some uses of Settings::getInstance() --- src/persistence/profile.cpp | 47 ++++++++++++++++++++----------------- src/persistence/profile.h | 3 ++- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 36d319953..62ea34cab 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -293,12 +293,13 @@ void Profile::initCore(const QByteArray& toxsave, Settings& s, bool isNewProfile avatarBroadcaster = std::unique_ptr(new AvatarBroadcaster(*core)); } -Profile::Profile(const QString& name, std::unique_ptr passkey, Paths& paths_) +Profile::Profile(const QString& name, std::unique_ptr passkey, Paths& paths_, Settings& settings_) : name{name} , passkey{std::move(passkey)} , isRemoved{false} , encrypted{this->passkey != nullptr} , paths{paths_} + , settings{settings_} {} /** @@ -324,14 +325,15 @@ Profile* Profile::loadProfile(const QString& name, const QString& password, Sett LoadToxDataError error; QByteArray toxsave = QByteArray(); - QString path = settings.getPaths().getSettingsDirPath() + name + ".tox"; + Paths& paths = settings.getPaths(); + QString path = paths.getSettingsDirPath() + name + ".tox"; std::unique_ptr tmpKey = loadToxData(password, path, toxsave, error); if (logLoadToxDataError(error, path)) { ProfileLocker::unlock(); return nullptr; } - Profile* p = new Profile(name, std::move(tmpKey), settings.getPaths()); + Profile* p = new Profile(name, std::move(tmpKey), paths, settings); // Core settings are saved per profile, need to load them before starting Core settings.updateProfileData(p, parser); @@ -354,7 +356,8 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se const QCommandLineParser* parser) { CreateToxDataError error; - QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name + ".tox"; + Paths& paths = settings.getPaths(); + QString path = paths.getSettingsDirPath() + name + ".tox"; std::unique_ptr tmpKey = createToxData(name, password, path, error); if (logCreateToxDataError(error, name)) { @@ -362,7 +365,7 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se } settings.createPersonal(name); - Profile* p = new Profile(name, std::move(tmpKey), settings.getPaths()); + Profile* p = new Profile(name, std::move(tmpKey), paths, settings); settings.updateProfileData(p, parser); p->initCore(QByteArray(), settings, /*isNewProfile*/ true); @@ -377,8 +380,8 @@ Profile::~Profile() } onSaveToxSave(); - Settings::getInstance().savePersonal(this); - Settings::getInstance().sync(); + settings.savePersonal(this); + settings.sync(); ProfileLocker::assertLock(); assert(ProfileLocker::getCurLockName() == name); ProfileLocker::unlock(); @@ -488,7 +491,7 @@ bool Profile::saveToxSave(QByteArray data) ProfileLocker::assertLock(); assert(ProfileLocker::getCurLockName() == name); - QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name + ".tox"; + QString path = paths.getSettingsDirPath() + name + ".tox"; qDebug() << "Saving tox save to " << path; QSaveFile saveFile(path); if (!saveFile.open(QIODevice::WriteOnly)) { @@ -530,7 +533,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted) { const QString ownerStr = owner.toString(); if (!encrypted || forceUnencrypted) { - return Settings::getInstance().getPaths().getSettingsDirPath() + "avatars/" + ownerStr + ".png"; + return paths.getSettingsDirPath() + "avatars/" + ownerStr + ".png"; } QByteArray idData = ownerStr.toUtf8(); @@ -544,7 +547,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted) QByteArray hash(hashSize, 0); crypto_generichash(reinterpret_cast(hash.data()), hashSize, reinterpret_cast(idData.data()), idData.size(), reinterpret_cast(pubkeyData.data()), pubkeyData.size()); - return Settings::getInstance().getPaths().getSettingsDirPath() + "avatars/" + hash.toHex().toUpper() + ".png"; + return paths.getSettingsDirPath() + "avatars/" + hash.toHex().toUpper() + ".png"; } /** @@ -564,7 +567,7 @@ QPixmap Profile::loadAvatar() QPixmap Profile::loadAvatar(const ToxPk& owner) { QPixmap pic; - if (Settings::getInstance().getShowIdenticons()) { + if (settings.getShowIdenticons()) { const QByteArray avatarData = loadAvatarData(owner); if (avatarData.isEmpty()) { @@ -652,7 +655,7 @@ void Profile::setAvatar(QByteArray pic) pixmap.loadFromData(pic); avatarData = pic; } else { - if (Settings::getInstance().getShowIdenticons()) { + if (settings.getShowIdenticons()) { const QImage identicon = Identicon(selfPk.getByteArray()).toImage(32); pixmap = QPixmap::fromImage(identicon); @@ -682,7 +685,7 @@ void Profile::setFriendAvatar(const ToxPk& owner, QByteArray pic) pixmap.loadFromData(pic); avatarData = pic; emit friendAvatarSet(owner, pixmap); - } else if (Settings::getInstance().getShowIdenticons()) { + } else if (settings.getShowIdenticons()) { const QImage identicon = Identicon(owner.getByteArray()).toImage(32); pixmap = QPixmap::fromImage(identicon); emit friendAvatarSet(owner, pixmap); @@ -723,7 +726,7 @@ void Profile::saveAvatar(const ToxPk& owner, const QByteArray& avatar) const QByteArray& pic = needEncrypt ? passkey->encrypt(avatar) : avatar; QString path = avatarPath(owner); - QDir(Settings::getInstance().getPaths().getSettingsDirPath()).mkdir("avatars"); + QDir(paths.getSettingsDirPath()).mkdir("avatars"); if (pic.isEmpty()) { QFile::remove(path); } else { @@ -773,7 +776,7 @@ void Profile::removeFriendAvatar(const ToxPk& owner) */ bool Profile::isHistoryEnabled() { - return Settings::getInstance().getEnableLogging() && history; + return settings.getEnableLogging() && history; } /** @@ -857,7 +860,7 @@ QStringList Profile::remove() i--; } } - QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name; + QString path = paths.getSettingsDirPath() + name; ProfileLocker::unlock(); QFile profileMain{path + ".tox"}; @@ -893,8 +896,8 @@ QStringList Profile::remove() */ bool Profile::rename(QString newName) { - QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name, - newPath = Settings::getInstance().getPaths().getSettingsDirPath() + newName; + QString path = paths.getSettingsDirPath() + name, + newPath = paths.getSettingsDirPath() + newName; if (!ProfileLocker::lock(newName)) { return false; @@ -906,11 +909,11 @@ bool Profile::rename(QString newName) database->rename(newName); } - bool resetAutorun = Settings::getInstance().getAutorun(); - Settings::getInstance().setAutorun(false); - Settings::getInstance().setCurrentProfile(newName); + bool resetAutorun = settings.getAutorun(); + settings.setAutorun(false); + settings.setCurrentProfile(newName); if (resetAutorun) { - Settings::getInstance().setAutorun(true); // fixes -p flag in autostart command line + settings.setAutorun(true); // fixes -p flag in autostart command line } name = newName; diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 35a855229..c7e9130d1 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -104,7 +104,7 @@ private slots: void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash); private: - Profile(const QString& name, std::unique_ptr passkey, Paths& paths); + Profile(const QString& name, std::unique_ptr passkey, Paths& paths, Settings &settings_); static QStringList getFilesByExt(QString extension); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); bool saveToxSave(QByteArray data); @@ -123,4 +123,5 @@ private: static QStringList profiles; std::unique_ptr bootstrapNodes; Paths& paths; + Settings& settings; };