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

refactor(Settings): Save loaded profile automatically

It's invalid to save a different profile than was loaded, so no need to
require the profile to be passed in again on save.

Removes dependence on Nexus singleton for Nexus::getProfile.
This commit is contained in:
Anthony Bilinski 2022-03-28 04:05:38 -07:00
parent a1f7c71feb
commit 6c6620ffc1
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
3 changed files with 12 additions and 20 deletions

View File

@ -387,7 +387,7 @@ Profile::~Profile()
} }
onSaveToxSave(); onSaveToxSave();
settings.savePersonal(this); settings.savePersonal();
settings.sync(); settings.sync();
ProfileLocker::assertLock(paths); ProfileLocker::assertLock(paths);
assert(ProfileLocker::getCurLockName() == name); assert(ProfileLocker::getCurLockName() == name);

View File

@ -278,7 +278,7 @@ void Settings::updateProfileData(Profile* profile, const QCommandLineParser* par
} }
setCurrentProfile(profile->getName()); setCurrentProfile(profile->getName());
saveGlobal(); saveGlobal();
loadPersonal(profile->getName(), profile->getPasskey(), newProfile); loadPersonal(*profile, newProfile);
if (parser) { if (parser) {
applyCommandLineOptions(*parser); applyCommandLineOptions(*parser);
} }
@ -467,22 +467,23 @@ bool Settings::applyCommandLineOptions(const QCommandLineParser& parser)
return true; return true;
} }
void Settings::loadPersonal(QString profileName, const ToxEncrypt* passKey, bool newProfile) void Settings::loadPersonal(const Profile& profile, bool newProfile)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
loadedProfile = &profile;
QDir dir(paths.getSettingsDirPath()); QDir dir(paths.getSettingsDirPath());
QString filePath = dir.filePath(globalSettingsFile); QString filePath = dir.filePath(globalSettingsFile);
// load from a profile specific friend data list if possible // load from a profile specific friend data list if possible
QString tmp = dir.filePath(profileName + ".ini"); QString tmp = dir.filePath(profile.getName() + ".ini");
if (QFile(tmp).exists()) { // otherwise, filePath remains the global file if (QFile(tmp).exists()) { // otherwise, filePath remains the global file
filePath = tmp; filePath = tmp;
} }
qDebug() << "Loading personal settings from" << filePath; qDebug() << "Loading personal settings from" << filePath;
SettingsSerializer ps(filePath, passKey); SettingsSerializer ps(filePath, profile.getPasskey());
ps.load(); ps.load();
friendLst.clear(); friendLst.clear();
@ -746,26 +747,17 @@ void Settings::saveGlobal()
} }
/** /**
* @brief Asynchronous, saves the current profile. * @brief Asynchronous, saves the profile.
*/ */
void Settings::savePersonal() void Settings::savePersonal()
{ {
savePersonal(Nexus::getProfile()); if (!loadedProfile) {
}
/**
* @brief Asynchronous, saves the profile.
* @param profile Profile to save.
*/
void Settings::savePersonal(Profile* profile)
{
if (!profile) {
qDebug() << "Could not save personal settings because there is no active profile"; qDebug() << "Could not save personal settings because there is no active profile";
return; return;
} }
QMetaObject::invokeMethod(this, "savePersonal", QMetaObject::invokeMethod(this, "savePersonal",
Q_ARG(QString, profile->getName()), Q_ARG(QString, loadedProfile->getName()),
Q_ARG(const ToxEncrypt*, profile->getPasskey())); Q_ARG(const ToxEncrypt*, loadedProfile->getPasskey()));
} }
void Settings::savePersonal(QString profileName, const ToxEncrypt* passkey) void Settings::savePersonal(QString profileName, const ToxEncrypt* passkey)

View File

@ -158,10 +158,9 @@ public:
void createPersonal(const QString& basename) const; void createPersonal(const QString& basename) const;
void savePersonal(); void savePersonal();
void savePersonal(Profile* profile);
void loadGlobal(); void loadGlobal();
void loadPersonal(QString profileName, const ToxEncrypt* passKey, bool newProfile); void loadPersonal(const Profile& profile, bool newProfile);
void resetToDefault(); void resetToDefault();
@ -718,4 +717,5 @@ private:
int globalSettingsVersion = 0; int globalSettingsVersion = 0;
int personalSettingsVersion = 0; int personalSettingsVersion = 0;
IMessageBoxManager& messageBoxManager; IMessageBoxManager& messageBoxManager;
const Profile* loadedProfile = nullptr;
}; };