#include "singletons/Settings.hpp" #include "Application.hpp" #include "debug/Log.hpp" #include "singletons/Paths.hpp" #include "singletons/Resources.hpp" #include "singletons/WindowManager.hpp" namespace chatterino { std::vector> _settings; void _actuallyRegisterSetting(std::weak_ptr setting) { _settings.push_back(setting); } Settings::Settings() { qDebug() << "init SettingManager"; } Settings &Settings::getInstance() { static Settings instance; return instance; } void Settings::initialize() { } void Settings::load() { QString settingsPath = getPaths()->settingsDirectory + "/settings.json"; pajlada::Settings::SettingManager::gLoad(qPrintable(settingsPath)); } void Settings::saveSnapshot() { rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType); rapidjson::Document::AllocatorType &a = d->GetAllocator(); 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); } this->snapshot_.reset(d); Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d)); } void Settings::restoreSnapshot() { if (!this->snapshot_) { return; } const auto &snapshotObject = this->snapshot_->GetObject(); for (const auto &weakSetting : _settings) { auto setting = weakSetting.lock(); if (!setting) { Log("Error stage 1 of loading"); continue; } const char *path = setting->getPath().c_str(); if (!snapshotObject.HasMember(path)) { Log("Error stage 2 of loading"); continue; } setting->unmarshalValue(snapshotObject[path]); } } Settings *getSettings() { return &Settings::getInstance(); } } // namespace chatterino