mirror-chatterino2/src/singletons/Settings.cpp

94 lines
2 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "debug/Log.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Paths.hpp"
#include "singletons/Resources.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
std::vector<std::weak_ptr<pajlada::Settings::ISettingData>> _settings;
void _actuallyRegisterSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting)
{
_settings.push_back(setting);
}
2018-06-28 19:51:07 +02:00
Settings::Settings()
2017-04-12 17:46:44 +02:00
{
qDebug() << "init SettingManager";
}
2018-06-28 19:51:07 +02:00
Settings &Settings::getInstance()
{
2018-06-28 19:51:07 +02:00
static Settings instance;
return instance;
}
2018-01-17 14:14:31 +01:00
2018-06-28 19:51:07 +02:00
void Settings::initialize()
{
2017-04-12 17:46:44 +02:00
}
2018-06-28 19:51:07 +02:00
void Settings::load()
{
2018-07-07 11:41:01 +02:00
QString settingsPath = getPaths()->settingsDirectory + "/settings.json";
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
}
2018-06-28 19:51:07 +02:00
void Settings::saveSnapshot()
2017-04-12 17:46:44 +02:00
{
rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType);
rapidjson::Document::AllocatorType &a = d->GetAllocator();
2017-04-12 17:46:44 +02:00
for (const auto &weakSetting : _settings) {
auto setting = weakSetting.lock();
if (!setting) {
continue;
}
rapidjson::Value key(setting->getPath().c_str(), a);
rapidjson::Value val = setting->marshalInto(*d);
d->AddMember(key.Move(), val.Move(), a);
2017-04-12 17:46:44 +02:00
}
2018-07-06 19:23:47 +02:00
this->snapshot_.reset(d);
2018-06-26 17:06:17 +02:00
Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d));
}
2018-06-28 19:51:07 +02:00
void Settings::restoreSnapshot()
{
2018-07-06 19:23:47 +02:00
if (!this->snapshot_) {
return;
}
2018-07-06 19:23:47 +02:00
const auto &snapshotObject = this->snapshot_->GetObject();
for (const auto &weakSetting : _settings) {
auto setting = weakSetting.lock();
if (!setting) {
2018-06-26 17:06:17 +02:00
Log("Error stage 1 of loading");
continue;
}
const char *path = setting->getPath().c_str();
if (!snapshotObject.HasMember(path)) {
2018-06-26 17:06:17 +02:00
Log("Error stage 2 of loading");
continue;
}
setting->unmarshalValue(snapshotObject[path]);
}
2017-04-12 17:46:44 +02:00
}
2018-06-28 19:51:07 +02:00
Settings *getSettings()
2018-01-17 14:14:31 +01:00
{
2018-06-28 19:51:07 +02:00
return &Settings::getInstance();
2018-01-17 14:14:31 +01:00
}
2018-01-23 21:33:49 +01:00
2017-04-14 17:52:22 +02:00
} // namespace chatterino