2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Settings.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
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
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
std::vector<std::weak_ptr<pajlada::Settings::SettingData>> _settings;
|
2017-12-28 19:03:52 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Settings *Settings::instance = nullptr;
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void _actuallyRegisterSetting(
|
2018-11-03 13:00:07 +01:00
|
|
|
std::weak_ptr<pajlada::Settings::SettingData> setting)
|
2017-12-28 19:03:52 +01:00
|
|
|
{
|
|
|
|
_settings.push_back(setting);
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Settings::Settings(Paths &paths)
|
2018-06-28 19:38:57 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
instance = this;
|
2018-04-26 18:10:26 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
QString settingsPath = paths.settingsDirectory + "/settings.json";
|
2018-01-17 14:14:31 +01:00
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
// get global instance of the settings library
|
|
|
|
auto settingsInstance = pajlada::Settings::SettingManager::getInstance();
|
|
|
|
|
|
|
|
settingsInstance->load(qPrintable(settingsPath));
|
|
|
|
|
|
|
|
settingsInstance->setBackupEnabled(true);
|
|
|
|
settingsInstance->setBackupSlots(9);
|
|
|
|
settingsInstance->saveMethod =
|
|
|
|
pajlada::Settings::SettingManager::SaveMethod::SaveOnExit;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Settings &Settings::getInstance()
|
2018-01-04 01:52:37 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return *instance;
|
2018-01-04 01:52:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
void Settings::saveSnapshot()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType);
|
|
|
|
rapidjson::Document::AllocatorType &a = d->GetAllocator();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &weakSetting : _settings)
|
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
auto setting = weakSetting.lock();
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!setting)
|
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
continue;
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-12-28 19:03:52 +01:00
|
|
|
|
|
|
|
rapidjson::Value key(setting->getPath().c_str(), a);
|
2018-11-03 13:00:07 +01:00
|
|
|
auto curVal = setting->unmarshalJSON();
|
|
|
|
if (curVal == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value val;
|
|
|
|
val.CopyFrom(*curVal, a);
|
2017-12-28 19:03:52 +01:00
|
|
|
d->AddMember(key.Move(), val.Move(), a);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
// log("Snapshot state: {}", rj::stringify(*d));
|
2017-12-28 19:03:52 +01:00
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
this->snapshot_.reset(d);
|
2017-12-28 19:03:52 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
void Settings::restoreSnapshot()
|
2017-12-28 19:03:52 +01:00
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->snapshot_)
|
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
const auto &snapshot = *(this->snapshot_.get());
|
|
|
|
|
|
|
|
if (!snapshot.IsObject())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-12-28 19:03:52 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &weakSetting : _settings)
|
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
auto setting = weakSetting.lock();
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!setting)
|
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *path = setting->getPath().c_str();
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
if (!snapshot.HasMember(path))
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2017-12-28 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
setting->marshalJSON(snapshot[path]);
|
2017-12-28 19:03:52 +01:00
|
|
|
}
|
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
|