From aea9eea8a4e2f95a7175954183959c1070bcc315 Mon Sep 17 00:00:00 2001 From: Dubslow Date: Tue, 2 Dec 2014 18:26:17 -0600 Subject: [PATCH] move friend information to a separate ini file; fixes #594 --- src/core.cpp | 13 +++++-- src/misc/settings.cpp | 88 +++++++++++++++++++++++++++---------------- src/misc/settings.h | 7 +++- 3 files changed, 70 insertions(+), 38 deletions(-) diff --git a/src/core.cpp b/src/core.cpp index 5268e4285..8cb27e8a8 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -1370,12 +1370,13 @@ void Core::switchConfiguration(const QString& profile) qDebug() << "Core: creating new Id"; else qDebug() << "Core: switching from" << Settings::getInstance().getCurrentProfile() << "to" << profile; - + saveConfiguration(); + + ready = false; clearPassword(ptMain); clearPassword(ptHistory); - ready = false; toxTimer->stop(); Widget::getInstance()->setEnabledThreadsafe(false); if (tox) { @@ -1384,6 +1385,7 @@ void Core::switchConfiguration(const QString& profile) tox_kill(tox); tox = nullptr; } + emit selfAvatarChanged(QPixmap(":/img/contact_dark.png")); emit blockingClearContacts(); // we need this to block, but signals are required for thread safety @@ -1391,7 +1393,12 @@ void Core::switchConfiguration(const QString& profile) loadPath = ""; else loadPath = QDir(Settings::getSettingsDirPath()).filePath(profile + TOX_EXT); - Settings::getInstance().setCurrentProfile(profile); + + // the new profile needs to be set before resetting the settings, so that + // we don't load the old profile's profile.ini + Settings::getInstance().setCurrentProfile(profile); + Settings::getInstance().save(false); // save new profile, but don't write old profile info to newprofile.ini + Settings::resetInstance(); HistoryKeeper::getInstance()->resetInstance(); start(); diff --git a/src/misc/settings.cpp b/src/misc/settings.cpp index b40e4f2d1..c5e13b1e7 100644 --- a/src/misc/settings.cpp +++ b/src/misc/settings.cpp @@ -38,6 +38,7 @@ const QString Settings::OLDFILENAME = "settings.ini"; const QString Settings::FILENAME = "qtox.ini"; +Settings* Settings::settings{nullptr}; bool Settings::makeToxPortable{false}; Settings::Settings() : @@ -48,12 +49,20 @@ Settings::Settings() : Settings& Settings::getInstance() { - static Settings* settings{nullptr}; if (!settings) settings = new Settings(); return *settings; } +void Settings::resetInstance() +{ + if (settings) + { + delete settings; + settings = nullptr; + } +} + void Settings::load() { if (loaded) @@ -113,21 +122,6 @@ void Settings::load() useCustomDhtList=false; s.endGroup(); - friendLst.clear(); - s.beginGroup("Friends"); - int size = s.beginReadArray("Friend"); - for (int i = 0; i < size; i ++) - { - s.setArrayIndex(i); - friendProp fp; - fp.addr = s.value("addr").toString(); - fp.alias = s.value("alias").toString(); - fp.autoAcceptDir = s.value("autoAcceptDir").toString(); - friendLst[ToxID::fromString(fp.addr).publicKey] = fp; - } - s.endArray(); - s.endGroup(); - s.beginGroup("General"); enableIPv6 = s.value("enableIPv6", true).toBool(); translation = s.value("translation", "en").toString(); @@ -227,15 +221,39 @@ void Settings::load() } loaded = true; + + if (currentProfile.isEmpty()) // new profile in Core::switchConfiguration + return; + + // load from a profile specific friend data list if possible + QString tmp = dir.filePath(currentProfile + ".ini"); + if (QFile(tmp).exists()) + filePath = tmp; + + QSettings fs(filePath, QSettings::IniFormat); + friendLst.clear(); + fs.beginGroup("Friends"); + int size = fs.beginReadArray("Friend"); + for (int i = 0; i < size; i ++) + { + fs.setArrayIndex(i); + friendProp fp; + fp.addr = fs.value("addr").toString(); + fp.alias = fs.value("alias").toString(); + fp.autoAcceptDir = fs.value("autoAcceptDir").toString(); + friendLst[ToxID::fromString(fp.addr).publicKey] = fp; + } + fs.endArray(); + fs.endGroup(); } -void Settings::save() +void Settings::save(bool writeFriends) { QString filePath = QDir(getSettingsDirPath()).filePath(FILENAME); - save(filePath); + save(filePath, writeFriends); } -void Settings::save(QString path) +void Settings::save(QString path, bool writeFriends) { qDebug() << "Settings: Saving in "<