mirror-chatterino2/src/singletons/settingsmanager.cpp

132 lines
3.6 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "singletons/settingsmanager.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-12-31 22:58:35 +01:00
namespace singletons {
2017-04-12 17:46:44 +02:00
std::vector<std::weak_ptr<pajlada::Settings::ISettingData>> _settings;
void _registerSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting)
{
_settings.push_back(setting);
}
2017-12-31 22:58:35 +01:00
SettingManager::SettingManager()
: snapshot(nullptr)
2017-04-12 17:46:44 +02:00
{
this->wordMaskListener.addSetting(this->showTimestamps);
this->wordMaskListener.addSetting(this->showBadges);
this->wordMaskListener.addSetting(this->enableBttvEmotes);
this->wordMaskListener.addSetting(this->enableEmojis);
this->wordMaskListener.addSetting(this->enableFfzEmotes);
this->wordMaskListener.addSetting(this->enableTwitchEmotes);
this->wordMaskListener.cb = [this](auto) {
this->updateWordTypeMask(); //
};
2017-04-12 17:46:44 +02:00
}
MessageElement::Flags SettingManager::getWordTypeMask()
2017-04-12 17:46:44 +02:00
{
2017-09-21 12:15:01 +02:00
return this->wordTypeMask;
2017-04-12 17:46:44 +02:00
}
2017-12-31 22:58:35 +01:00
bool SettingManager::isIgnoredEmote(const QString &)
2017-04-12 17:46:44 +02:00
{
return false;
}
void SettingManager::init()
{
QString settingsPath = PathManager::getInstance().settingsFolderPath + "/settings.json";
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
}
2017-12-31 22:58:35 +01:00
void SettingManager::updateWordTypeMask()
2017-04-12 17:46:44 +02:00
{
uint32_t newMaskUint = MessageElement::Text;
if (this->showTimestamps) {
newMaskUint |= MessageElement::Timestamp;
2017-04-12 17:46:44 +02:00
}
newMaskUint |=
enableTwitchEmotes ? MessageElement::TwitchEmoteImage : MessageElement::TwitchEmoteText;
newMaskUint |= enableFfzEmotes ? MessageElement::FfzEmoteImage : MessageElement::FfzEmoteText;
newMaskUint |=
enableBttvEmotes ? MessageElement::BttvEmoteImage : MessageElement::BttvEmoteText;
newMaskUint |= enableEmojis ? MessageElement::EmojiImage : MessageElement::EmojiText;
newMaskUint |= MessageElement::BitsAmount;
newMaskUint |= enableGifAnimations ? MessageElement::BitsAnimated : MessageElement::BitsStatic;
2017-04-12 17:46:44 +02:00
if (this->showBadges) {
newMaskUint |= MessageElement::Badges;
}
2017-04-12 17:46:44 +02:00
newMaskUint |= MessageElement::Username;
2017-04-12 17:46:44 +02:00
newMaskUint |= MessageElement::AlwaysShow;
MessageElement::Flags newMask = static_cast<MessageElement::Flags>(newMaskUint);
2017-04-12 17:46:44 +02:00
2017-09-21 12:15:01 +02:00
if (newMask != this->wordTypeMask) {
this->wordTypeMask = newMask;
2017-04-12 17:46:44 +02:00
emit wordTypeMaskChanged();
}
}
2017-12-31 22:58:35 +01:00
void SettingManager::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
}
this->snapshot.reset(d);
debug::Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d));
}
2017-12-31 22:58:35 +01:00
void SettingManager::recallSnapshot()
{
if (!this->snapshot) {
return;
}
const auto &snapshotObject = this->snapshot->GetObject();
for (const auto &weakSetting : _settings) {
auto setting = weakSetting.lock();
if (!setting) {
debug::Log("Error stage 1 of loading");
continue;
}
const char *path = setting->getPath().c_str();
if (!snapshotObject.HasMember(path)) {
debug::Log("Error stage 2 of loading");
continue;
}
setting->unmarshalValue(snapshotObject[path]);
}
2017-04-12 17:46:44 +02:00
}
} // namespace singletons
2017-04-14 17:52:22 +02:00
} // namespace chatterino