Browse Source

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.
reviewable/pr6596/r2
Anthony Bilinski 3 years ago
parent
commit
6c6620ffc1
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 2
      src/persistence/profile.cpp
  2. 26
      src/persistence/settings.cpp
  3. 4
      src/persistence/settings.h

2
src/persistence/profile.cpp

@ -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);

26
src/persistence/settings.cpp

@ -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();
@ -745,27 +746,18 @@ void Settings::saveGlobal()
s.endGroup(); s.endGroup();
} }
/**
* @brief Asynchronous, saves the current profile.
*/
void Settings::savePersonal()
{
savePersonal(Nexus::getProfile());
}
/** /**
* @brief Asynchronous, saves the profile. * @brief Asynchronous, saves the profile.
* @param profile Profile to save.
*/ */
void Settings::savePersonal(Profile* profile) void Settings::savePersonal()
{ {
if (!profile) { if (!loadedProfile) {
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)

4
src/persistence/settings.h

@ -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;
}; };

Loading…
Cancel
Save