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

refactor(profile): remove some uses of Settings::getInstance()

This commit is contained in:
sudden6 2020-08-28 01:33:11 +02:00
parent 4fab6faea6
commit db205bea57
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
2 changed files with 27 additions and 23 deletions

View File

@ -293,12 +293,13 @@ void Profile::initCore(const QByteArray& toxsave, Settings& s, bool isNewProfile
avatarBroadcaster = std::unique_ptr<AvatarBroadcaster>(new AvatarBroadcaster(*core)); avatarBroadcaster = std::unique_ptr<AvatarBroadcaster>(new AvatarBroadcaster(*core));
} }
Profile::Profile(const QString& name, std::unique_ptr<ToxEncrypt> passkey, Paths& paths_) Profile::Profile(const QString& name, std::unique_ptr<ToxEncrypt> passkey, Paths& paths_, Settings& settings_)
: name{name} : name{name}
, passkey{std::move(passkey)} , passkey{std::move(passkey)}
, isRemoved{false} , isRemoved{false}
, encrypted{this->passkey != nullptr} , encrypted{this->passkey != nullptr}
, paths{paths_} , paths{paths_}
, settings{settings_}
{} {}
/** /**
@ -324,14 +325,15 @@ Profile* Profile::loadProfile(const QString& name, const QString& password, Sett
LoadToxDataError error; LoadToxDataError error;
QByteArray toxsave = QByteArray(); QByteArray toxsave = QByteArray();
QString path = settings.getPaths().getSettingsDirPath() + name + ".tox"; Paths& paths = settings.getPaths();
QString path = paths.getSettingsDirPath() + name + ".tox";
std::unique_ptr<ToxEncrypt> tmpKey = loadToxData(password, path, toxsave, error); std::unique_ptr<ToxEncrypt> tmpKey = loadToxData(password, path, toxsave, error);
if (logLoadToxDataError(error, path)) { if (logLoadToxDataError(error, path)) {
ProfileLocker::unlock(); ProfileLocker::unlock();
return nullptr; 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 // Core settings are saved per profile, need to load them before starting Core
settings.updateProfileData(p, parser); settings.updateProfileData(p, parser);
@ -354,7 +356,8 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se
const QCommandLineParser* parser) const QCommandLineParser* parser)
{ {
CreateToxDataError error; CreateToxDataError error;
QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name + ".tox"; Paths& paths = settings.getPaths();
QString path = paths.getSettingsDirPath() + name + ".tox";
std::unique_ptr<ToxEncrypt> tmpKey = createToxData(name, password, path, error); std::unique_ptr<ToxEncrypt> tmpKey = createToxData(name, password, path, error);
if (logCreateToxDataError(error, name)) { if (logCreateToxDataError(error, name)) {
@ -362,7 +365,7 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se
} }
settings.createPersonal(name); 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); settings.updateProfileData(p, parser);
p->initCore(QByteArray(), settings, /*isNewProfile*/ true); p->initCore(QByteArray(), settings, /*isNewProfile*/ true);
@ -377,8 +380,8 @@ Profile::~Profile()
} }
onSaveToxSave(); onSaveToxSave();
Settings::getInstance().savePersonal(this); settings.savePersonal(this);
Settings::getInstance().sync(); settings.sync();
ProfileLocker::assertLock(); ProfileLocker::assertLock();
assert(ProfileLocker::getCurLockName() == name); assert(ProfileLocker::getCurLockName() == name);
ProfileLocker::unlock(); ProfileLocker::unlock();
@ -488,7 +491,7 @@ bool Profile::saveToxSave(QByteArray data)
ProfileLocker::assertLock(); ProfileLocker::assertLock();
assert(ProfileLocker::getCurLockName() == name); assert(ProfileLocker::getCurLockName() == name);
QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name + ".tox"; QString path = paths.getSettingsDirPath() + name + ".tox";
qDebug() << "Saving tox save to " << path; qDebug() << "Saving tox save to " << path;
QSaveFile saveFile(path); QSaveFile saveFile(path);
if (!saveFile.open(QIODevice::WriteOnly)) { if (!saveFile.open(QIODevice::WriteOnly)) {
@ -530,7 +533,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted)
{ {
const QString ownerStr = owner.toString(); const QString ownerStr = owner.toString();
if (!encrypted || forceUnencrypted) { if (!encrypted || forceUnencrypted) {
return Settings::getInstance().getPaths().getSettingsDirPath() + "avatars/" + ownerStr + ".png"; return paths.getSettingsDirPath() + "avatars/" + ownerStr + ".png";
} }
QByteArray idData = ownerStr.toUtf8(); QByteArray idData = ownerStr.toUtf8();
@ -544,7 +547,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted)
QByteArray hash(hashSize, 0); QByteArray hash(hashSize, 0);
crypto_generichash(reinterpret_cast<uint8_t*>(hash.data()), hashSize, reinterpret_cast<uint8_t*>(idData.data()), idData.size(), crypto_generichash(reinterpret_cast<uint8_t*>(hash.data()), hashSize, reinterpret_cast<uint8_t*>(idData.data()), idData.size(),
reinterpret_cast<uint8_t*>(pubkeyData.data()), pubkeyData.size()); reinterpret_cast<uint8_t*>(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 Profile::loadAvatar(const ToxPk& owner)
{ {
QPixmap pic; QPixmap pic;
if (Settings::getInstance().getShowIdenticons()) { if (settings.getShowIdenticons()) {
const QByteArray avatarData = loadAvatarData(owner); const QByteArray avatarData = loadAvatarData(owner);
if (avatarData.isEmpty()) { if (avatarData.isEmpty()) {
@ -652,7 +655,7 @@ void Profile::setAvatar(QByteArray pic)
pixmap.loadFromData(pic); pixmap.loadFromData(pic);
avatarData = pic; avatarData = pic;
} else { } else {
if (Settings::getInstance().getShowIdenticons()) { if (settings.getShowIdenticons()) {
const QImage identicon = Identicon(selfPk.getByteArray()).toImage(32); const QImage identicon = Identicon(selfPk.getByteArray()).toImage(32);
pixmap = QPixmap::fromImage(identicon); pixmap = QPixmap::fromImage(identicon);
@ -682,7 +685,7 @@ void Profile::setFriendAvatar(const ToxPk& owner, QByteArray pic)
pixmap.loadFromData(pic); pixmap.loadFromData(pic);
avatarData = pic; avatarData = pic;
emit friendAvatarSet(owner, pixmap); emit friendAvatarSet(owner, pixmap);
} else if (Settings::getInstance().getShowIdenticons()) { } else if (settings.getShowIdenticons()) {
const QImage identicon = Identicon(owner.getByteArray()).toImage(32); const QImage identicon = Identicon(owner.getByteArray()).toImage(32);
pixmap = QPixmap::fromImage(identicon); pixmap = QPixmap::fromImage(identicon);
emit friendAvatarSet(owner, pixmap); 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; const QByteArray& pic = needEncrypt ? passkey->encrypt(avatar) : avatar;
QString path = avatarPath(owner); QString path = avatarPath(owner);
QDir(Settings::getInstance().getPaths().getSettingsDirPath()).mkdir("avatars"); QDir(paths.getSettingsDirPath()).mkdir("avatars");
if (pic.isEmpty()) { if (pic.isEmpty()) {
QFile::remove(path); QFile::remove(path);
} else { } else {
@ -773,7 +776,7 @@ void Profile::removeFriendAvatar(const ToxPk& owner)
*/ */
bool Profile::isHistoryEnabled() bool Profile::isHistoryEnabled()
{ {
return Settings::getInstance().getEnableLogging() && history; return settings.getEnableLogging() && history;
} }
/** /**
@ -857,7 +860,7 @@ QStringList Profile::remove()
i--; i--;
} }
} }
QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name; QString path = paths.getSettingsDirPath() + name;
ProfileLocker::unlock(); ProfileLocker::unlock();
QFile profileMain{path + ".tox"}; QFile profileMain{path + ".tox"};
@ -893,8 +896,8 @@ QStringList Profile::remove()
*/ */
bool Profile::rename(QString newName) bool Profile::rename(QString newName)
{ {
QString path = Settings::getInstance().getPaths().getSettingsDirPath() + name, QString path = paths.getSettingsDirPath() + name,
newPath = Settings::getInstance().getPaths().getSettingsDirPath() + newName; newPath = paths.getSettingsDirPath() + newName;
if (!ProfileLocker::lock(newName)) { if (!ProfileLocker::lock(newName)) {
return false; return false;
@ -906,11 +909,11 @@ bool Profile::rename(QString newName)
database->rename(newName); database->rename(newName);
} }
bool resetAutorun = Settings::getInstance().getAutorun(); bool resetAutorun = settings.getAutorun();
Settings::getInstance().setAutorun(false); settings.setAutorun(false);
Settings::getInstance().setCurrentProfile(newName); settings.setCurrentProfile(newName);
if (resetAutorun) { 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; name = newName;

View File

@ -104,7 +104,7 @@ private slots:
void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash); void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash);
private: private:
Profile(const QString& name, std::unique_ptr<ToxEncrypt> passkey, Paths& paths); Profile(const QString& name, std::unique_ptr<ToxEncrypt> passkey, Paths& paths, Settings &settings_);
static QStringList getFilesByExt(QString extension); static QStringList getFilesByExt(QString extension);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
bool saveToxSave(QByteArray data); bool saveToxSave(QByteArray data);
@ -123,4 +123,5 @@ private:
static QStringList profiles; static QStringList profiles;
std::unique_ptr<BootstrapNodeUpdater> bootstrapNodes; std::unique_ptr<BootstrapNodeUpdater> bootstrapNodes;
Paths& paths; Paths& paths;
Settings& settings;
}; };