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

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

View File

@ -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 "<<path;
@ -256,20 +274,6 @@ void Settings::save(QString path)
s.endArray();
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.setValue("enableIPv6", enableIPv6);
s.setValue("translation",translation);
@ -337,6 +341,24 @@ void Settings::save(QString path)
s.setValue("inDev", inDev);
s.setValue("outDev", outDev);
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()

View File

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