2018-06-26 14:09:39 +02:00
|
|
|
#include "singletons/SettingsManager.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "singletons/PathManager.hpp"
|
|
|
|
#include "singletons/ResourceManager.hpp"
|
|
|
|
#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
|
|
|
|
2017-12-28 19:03:52 +01:00
|
|
|
std::vector<std::weak_ptr<pajlada::Settings::ISettingData>> _settings;
|
|
|
|
|
2018-04-14 21:59:51 +02:00
|
|
|
void _actuallyRegisterSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting)
|
2017-12-28 19:03:52 +01:00
|
|
|
{
|
|
|
|
_settings.push_back(setting);
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
SettingManager::SettingManager()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-04-26 18:10:26 +02:00
|
|
|
qDebug() << "init SettingManager";
|
2018-06-28 19:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SettingManager &SettingManager::getInstance()
|
|
|
|
{
|
|
|
|
static SettingManager instance;
|
2018-04-26 18:10:26 +02:00
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
return instance;
|
2018-04-28 15:20:18 +02:00
|
|
|
}
|
2018-01-17 14:14:31 +01:00
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
void SettingManager::initialize()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
this->timestampFormat.connect([](auto, auto) {
|
|
|
|
auto app = getApp();
|
2018-06-04 16:10:54 +02:00
|
|
|
app->windows->layoutChannelViews();
|
2018-04-27 22:11:19 +02:00
|
|
|
});
|
2018-05-23 13:31:55 +02:00
|
|
|
|
2018-06-04 16:10:54 +02:00
|
|
|
this->emoteScale.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
|
|
|
|
|
|
|
|
this->timestampFormat.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
|
|
|
|
this->alternateMessageBackground.connect(
|
|
|
|
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
|
2018-06-24 18:32:00 +02:00
|
|
|
this->separateMessages.connect(
|
2018-06-04 16:10:54 +02:00
|
|
|
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
|
2018-06-04 16:34:47 +02:00
|
|
|
this->collpseMessagesMinLines.connect(
|
2018-06-04 16:10:54 +02:00
|
|
|
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
void SettingManager::load()
|
2018-01-04 01:52:37 +01:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-06-21 13:02:34 +02:00
|
|
|
QString settingsPath = app->paths->settingsDirectory + "/settings.json";
|
2018-01-05 01:55:21 +01:00
|
|
|
|
|
|
|
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
|
2018-01-04 01:52:37 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void SettingManager::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
|
|
|
|
2017-12-28 19:03:52 +01:00
|
|
|
for (const auto &weakSetting : _settings) {
|
|
|
|
auto setting = weakSetting.lock();
|
|
|
|
if (!setting) {
|
|
|
|
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);
|
|
|
|
rapidjson::Value val = setting->marshalInto(*d);
|
|
|
|
d->AddMember(key.Move(), val.Move(), a);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 19:03:52 +01:00
|
|
|
this->snapshot.reset(d);
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d));
|
2017-12-28 19:03:52 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void SettingManager::restoreSnapshot()
|
2017-12-28 19:03:52 +01:00
|
|
|
{
|
|
|
|
if (!this->snapshot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2017-12-28 19:03:52 +01:00
|
|
|
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");
|
2017-12-28 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
setting->unmarshalValue(snapshotObject[path]);
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
SettingManager *getSettings()
|
2018-01-17 14:14:31 +01:00
|
|
|
{
|
2018-06-28 19:38:57 +02:00
|
|
|
return &SettingManager::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
|