2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "appdatapath.hpp"
|
2017-12-28 19:03:52 +01:00
|
|
|
#include "debug/log.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
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
|
|
|
|
2017-12-28 19:03:52 +01: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()
|
2017-12-23 23:24:35 +01:00
|
|
|
: streamlinkPath("/behaviour/streamlink/path", "")
|
2017-09-11 23:35:59 +02:00
|
|
|
, preferredQuality("/behaviour/streamlink/quality", "Choose")
|
2017-09-21 12:15:01 +02:00
|
|
|
, emoteScale(this->settingsItems, "emoteScale", 1.0)
|
|
|
|
, pathHighlightSound(this->settingsItems, "pathHighlightSound", "qrc:/sounds/ping2.wav")
|
|
|
|
, highlightProperties(this->settingsItems, "highlightProperties",
|
|
|
|
QMap<QString, QPair<bool, bool>>())
|
2017-09-23 19:23:10 +02:00
|
|
|
, highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "")
|
2017-12-28 19:03:52 +01:00
|
|
|
, snapshot(nullptr)
|
2017-12-23 23:24:35 +01:00
|
|
|
, settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-19 02:31:06 +01:00
|
|
|
this->wordMaskListener.addSetting(this->showTimestamps);
|
|
|
|
this->wordMaskListener.addSetting(this->showTimestampSeconds);
|
|
|
|
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) {
|
2017-12-17 01:19:56 +01:00
|
|
|
this->updateWordTypeMask(); //
|
2017-12-19 02:31:06 +01:00
|
|
|
};
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void SettingManager::save()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
for (auto &item : this->settingsItems) {
|
2017-07-31 00:57:42 +02:00
|
|
|
if (item.get().getName() != "highlightProperties") {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.setValue(item.get().getName(), item.get().getVariant());
|
2017-07-31 00:37:22 +02:00
|
|
|
} else {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.beginGroup("Highlights");
|
2017-07-31 00:37:22 +02:00
|
|
|
QStringList list = highlightProperties.get().keys();
|
|
|
|
list.removeAll("");
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.remove("");
|
2017-07-31 00:57:42 +02:00
|
|
|
for (auto string : list) {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.beginGroup(string);
|
|
|
|
this->settings.setValue("highlightSound",
|
|
|
|
highlightProperties.get().value(string).first);
|
|
|
|
this->settings.setValue("highlightTask",
|
|
|
|
highlightProperties.get().value(string).second);
|
|
|
|
this->settings.endGroup();
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.endGroup();
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void SettingManager::load()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
for (auto &item : this->settingsItems) {
|
2017-07-31 00:57:42 +02:00
|
|
|
if (item.get().getName() != "highlightProperties") {
|
2017-09-21 12:15:01 +02:00
|
|
|
item.get().setVariant(this->settings.value(item.get().getName()));
|
2017-07-31 00:37:22 +02:00
|
|
|
} else {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.beginGroup("Highlights");
|
|
|
|
QStringList list = this->settings.childGroups();
|
2017-07-31 00:57:42 +02:00
|
|
|
for (auto string : list) {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.beginGroup(string);
|
|
|
|
highlightProperties.insertMap(string,
|
|
|
|
this->settings.value("highlightSound").toBool(),
|
|
|
|
this->settings.value("highlightTask").toBool());
|
|
|
|
this->settings.endGroup();
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-09-21 12:15:01 +02:00
|
|
|
this->settings.endGroup();
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
Word::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;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
QSettings &SettingManager::getQSettings()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->settings;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void SettingManager::updateWordTypeMask()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 20:53:43 +02:00
|
|
|
uint32_t newMaskUint = Word::Text;
|
|
|
|
|
|
|
|
if (this->showTimestamps) {
|
|
|
|
if (this->showTimestampSeconds) {
|
|
|
|
newMaskUint |= Word::TimestampWithSeconds;
|
|
|
|
} else {
|
|
|
|
newMaskUint |= Word::TimestampNoSeconds;
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-17 01:19:56 +01:00
|
|
|
newMaskUint |= enableTwitchEmotes ? Word::TwitchEmoteImage : Word::TwitchEmoteText;
|
|
|
|
newMaskUint |= enableFfzEmotes ? Word::FfzEmoteImage : Word::FfzEmoteText;
|
|
|
|
newMaskUint |= enableBttvEmotes ? Word::BttvEmoteImage : Word::BttvEmoteText;
|
2017-06-11 20:53:43 +02:00
|
|
|
newMaskUint |=
|
2017-12-17 01:19:56 +01:00
|
|
|
(enableBttvEmotes && enableGifAnimations) ? Word::BttvEmoteImage : Word::BttvEmoteText;
|
|
|
|
newMaskUint |= enableEmojis ? Word::EmojiImage : Word::EmojiText;
|
2017-06-11 20:53:43 +02:00
|
|
|
|
|
|
|
newMaskUint |= Word::BitsAmount;
|
2017-12-17 01:19:56 +01:00
|
|
|
newMaskUint |= enableGifAnimations ? Word::BitsAnimated : Word::BitsStatic;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
if (this->showBadges) {
|
|
|
|
newMaskUint |= Word::Badges;
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
newMaskUint |= Word::Username;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
newMaskUint |= Word::AlwaysShow;
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
Word::Flags newMask = static_cast<Word::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
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
debug::Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d));
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void SettingManager::recallSnapshot()
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|
2017-12-31 22:58:35 +01:00
|
|
|
}
|