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));
}
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}
, 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<ToxEncrypt> 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<ToxEncrypt> 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<uint8_t*>(hash.data()), hashSize, reinterpret_cast<uint8_t*>(idData.data()), idData.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 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;

View File

@ -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<ToxEncrypt> passkey, Paths& paths);
Profile(const QString& name, std::unique_ptr<ToxEncrypt> 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<BootstrapNodeUpdater> bootstrapNodes;
Paths& paths;
Settings& settings;
};