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

move friend information to a separate ini file; fixes #594

This commit is contained in:
Dubslow 2014-12-02 18:26:17 -06:00
parent 4a14155724
commit aea9eea8a4
No known key found for this signature in database
GPG Key ID: 3DB8E05315C220AA
3 changed files with 70 additions and 38 deletions

View File

@ -1372,10 +1372,11 @@ void Core::switchConfiguration(const QString& profile)
qDebug() << "Core: switching from" << Settings::getInstance().getCurrentProfile() << "to" << profile; qDebug() << "Core: switching from" << Settings::getInstance().getCurrentProfile() << "to" << profile;
saveConfiguration(); saveConfiguration();
ready = false;
clearPassword(ptMain); clearPassword(ptMain);
clearPassword(ptHistory); clearPassword(ptHistory);
ready = false;
toxTimer->stop(); toxTimer->stop();
Widget::getInstance()->setEnabledThreadsafe(false); Widget::getInstance()->setEnabledThreadsafe(false);
if (tox) { if (tox) {
@ -1384,6 +1385,7 @@ void Core::switchConfiguration(const QString& profile)
tox_kill(tox); tox_kill(tox);
tox = nullptr; tox = nullptr;
} }
emit selfAvatarChanged(QPixmap(":/img/contact_dark.png")); emit selfAvatarChanged(QPixmap(":/img/contact_dark.png"));
emit blockingClearContacts(); // we need this to block, but signals are required for thread safety 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 = ""; loadPath = "";
else else
loadPath = QDir(Settings::getSettingsDirPath()).filePath(profile + TOX_EXT); loadPath = QDir(Settings::getSettingsDirPath()).filePath(profile + TOX_EXT);
// 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().setCurrentProfile(profile);
Settings::getInstance().save(false); // save new profile, but don't write old profile info to newprofile.ini
Settings::resetInstance();
HistoryKeeper::getInstance()->resetInstance(); HistoryKeeper::getInstance()->resetInstance();
start(); start();

View File

@ -38,6 +38,7 @@
const QString Settings::OLDFILENAME = "settings.ini"; const QString Settings::OLDFILENAME = "settings.ini";
const QString Settings::FILENAME = "qtox.ini"; const QString Settings::FILENAME = "qtox.ini";
Settings* Settings::settings{nullptr};
bool Settings::makeToxPortable{false}; bool Settings::makeToxPortable{false};
Settings::Settings() : Settings::Settings() :
@ -48,12 +49,20 @@ Settings::Settings() :
Settings& Settings::getInstance() Settings& Settings::getInstance()
{ {
static Settings* settings{nullptr};
if (!settings) if (!settings)
settings = new Settings(); settings = new Settings();
return *settings; return *settings;
} }
void Settings::resetInstance()
{
if (settings)
{
delete settings;
settings = nullptr;
}
}
void Settings::load() void Settings::load()
{ {
if (loaded) if (loaded)
@ -113,21 +122,6 @@ void Settings::load()
useCustomDhtList=false; useCustomDhtList=false;
s.endGroup(); 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"); s.beginGroup("General");
enableIPv6 = s.value("enableIPv6", true).toBool(); enableIPv6 = s.value("enableIPv6", true).toBool();
translation = s.value("translation", "en").toString(); translation = s.value("translation", "en").toString();
@ -227,15 +221,39 @@ void Settings::load()
} }
loaded = true; 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); 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 "<<path; qDebug() << "Settings: Saving in "<<path;
@ -256,20 +274,6 @@ void Settings::save(QString path)
s.endArray(); s.endArray();
s.endGroup(); s.endGroup();
s.beginGroup("Friends");
s.beginWriteArray("Friend", friendLst.size());
int index = 0;
for (auto &frnd : friendLst)
{
s.setArrayIndex(index);
s.setValue("addr", frnd.addr);
s.setValue("alias", frnd.alias);
s.setValue("autoAcceptDir", frnd.autoAcceptDir);
index++;
}
s.endArray();
s.endGroup();
s.beginGroup("General"); s.beginGroup("General");
s.setValue("enableIPv6", enableIPv6); s.setValue("enableIPv6", enableIPv6);
s.setValue("translation",translation); s.setValue("translation",translation);
@ -337,6 +341,24 @@ void Settings::save(QString path)
s.setValue("inDev", inDev); s.setValue("inDev", inDev);
s.setValue("outDev", outDev); s.setValue("outDev", outDev);
s.endGroup(); s.endGroup();
if (!writeFriends || currentProfile.isEmpty()) // Core::switchConfiguration
return;
QSettings fs(QFileInfo(path).dir().filePath(currentProfile + ".ini"), QSettings::IniFormat);
fs.beginGroup("Friends");
fs.beginWriteArray("Friend", friendLst.size());
int index = 0;
for (auto& frnd : friendLst)
{
fs.setArrayIndex(index);
fs.setValue("addr", frnd.addr);
fs.setValue("alias", frnd.alias);
fs.setValue("autoAcceptDir", frnd.autoAcceptDir);
index++;
}
fs.endArray();
fs.endGroup();
} }
QString Settings::getSettingsDirPath() QString Settings::getSettingsDirPath()

View File

@ -29,6 +29,7 @@ class Settings : public QObject
Q_OBJECT Q_OBJECT
public: public:
static Settings& getInstance(); static Settings& getInstance();
static void resetInstance();
~Settings() = default; ~Settings() = default;
void executeSettingsDialog(QWidget* parent); void executeSettingsDialog(QWidget* parent);
@ -218,11 +219,13 @@ public:
void setFauxOfflineMessaging(bool value); void setFauxOfflineMessaging(bool value);
public: public:
void save(); void save(bool writeFriends = true);
void save(QString path); void save(QString path, bool writeFriends = true);
void load(); void load();
private: private:
static Settings* settings;
Settings(); Settings();
Settings(Settings &settings) = delete; Settings(Settings &settings) = delete;
Settings& operator=(const Settings&) = delete; Settings& operator=(const Settings&) = delete;