2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2017-12-28 19:03:52 +01:00
|
|
|
#include "debug/log.hpp"
|
2018-01-05 02:23:49 +01:00
|
|
|
#include "singletons/pathmanager.hpp"
|
2018-01-17 14:14:31 +01:00
|
|
|
#include "singletons/resourcemanager.hpp"
|
2018-01-23 21:56:25 +01:00
|
|
|
#include "singletons/windowmanager.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
|
|
|
|
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-01-17 17:02:34 +01:00
|
|
|
this->wordFlagsListener.addSetting(this->showTimestamps);
|
|
|
|
this->wordFlagsListener.addSetting(this->showBadges);
|
|
|
|
this->wordFlagsListener.addSetting(this->enableBttvEmotes);
|
|
|
|
this->wordFlagsListener.addSetting(this->enableEmojis);
|
|
|
|
this->wordFlagsListener.addSetting(this->enableFfzEmotes);
|
|
|
|
this->wordFlagsListener.addSetting(this->enableTwitchEmotes);
|
|
|
|
this->wordFlagsListener.cb = [this](auto) {
|
2017-12-17 01:19:56 +01:00
|
|
|
this->updateWordTypeMask(); //
|
2017-12-19 02:31:06 +01:00
|
|
|
};
|
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-01-17 14:14:31 +01:00
|
|
|
this->moderationActions.connect([this](auto, auto) { this->updateModerationActions(); });
|
2018-01-23 21:56:25 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->timestampFormat.connect([](auto, auto) {
|
|
|
|
auto app = getApp();
|
|
|
|
app->windows->layoutVisibleChatWidgets();
|
|
|
|
});
|
2018-05-23 13:31:55 +02:00
|
|
|
|
|
|
|
this->emoteScale.connect([](auto, auto) { getApp()->windows->layoutVisibleChatWidgets(); });
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 17:02:34 +01:00
|
|
|
MessageElement::Flags SettingManager::getWordFlags()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-01-17 17:02:34 +01:00
|
|
|
return this->wordFlags;
|
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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
QString settingsPath = app->paths->settingsFolderPath + "/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::updateWordTypeMask()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-01-11 20:16:25 +01:00
|
|
|
uint32_t newMaskUint = MessageElement::Text;
|
2017-06-11 20:53:43 +02:00
|
|
|
|
|
|
|
if (this->showTimestamps) {
|
2018-01-11 20:16:25 +01:00
|
|
|
newMaskUint |= MessageElement::Timestamp;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
newMaskUint |=
|
2018-01-11 20:16:25 +01:00
|
|
|
enableTwitchEmotes ? MessageElement::TwitchEmoteImage : MessageElement::TwitchEmoteText;
|
|
|
|
newMaskUint |= enableFfzEmotes ? MessageElement::FfzEmoteImage : MessageElement::FfzEmoteText;
|
|
|
|
newMaskUint |=
|
|
|
|
enableBttvEmotes ? MessageElement::BttvEmoteImage : MessageElement::BttvEmoteText;
|
|
|
|
newMaskUint |= enableEmojis ? MessageElement::EmojiImage : MessageElement::EmojiText;
|
2017-06-11 20:53:43 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
newMaskUint |= MessageElement::BitsAmount;
|
|
|
|
newMaskUint |= enableGifAnimations ? MessageElement::BitsAnimated : MessageElement::BitsStatic;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
if (this->showBadges) {
|
2018-01-11 20:16:25 +01:00
|
|
|
newMaskUint |= MessageElement::Badges;
|
2017-06-11 20:53:43 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
newMaskUint |= MessageElement::Username;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
newMaskUint |= MessageElement::AlwaysShow;
|
2018-04-18 09:12:29 +02:00
|
|
|
newMaskUint |= MessageElement::Collapsed;
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
MessageElement::Flags newMask = static_cast<MessageElement::Flags>(newMaskUint);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-01-17 17:02:34 +01:00
|
|
|
if (newMask != this->wordFlags) {
|
|
|
|
this->wordFlags = newMask;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
this->wordFlagsChanged.invoke();
|
2017-04-12 17:46:44 +02: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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
std::vector<ModerationAction> SettingManager::getModerationActions() const
|
|
|
|
{
|
|
|
|
return this->_moderationActions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingManager::updateModerationActions()
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
2018-01-17 14:14:31 +01:00
|
|
|
|
|
|
|
this->_moderationActions.clear();
|
|
|
|
|
|
|
|
static QRegularExpression newLineRegex("(\r\n?|\n)+");
|
|
|
|
static QRegularExpression replaceRegex("[!/.]");
|
|
|
|
static QRegularExpression timeoutRegex("^[./]timeout.* (\\d+)");
|
|
|
|
QStringList list = this->moderationActions.getValue().split(newLineRegex);
|
|
|
|
|
|
|
|
int multipleTimeouts = 0;
|
|
|
|
|
|
|
|
for (QString &str : list) {
|
|
|
|
if (timeoutRegex.match(str).hasMatch()) {
|
|
|
|
multipleTimeouts++;
|
|
|
|
if (multipleTimeouts > 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
QString &str = list[i];
|
|
|
|
|
|
|
|
if (str.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto timeoutMatch = timeoutRegex.match(str);
|
|
|
|
|
|
|
|
if (timeoutMatch.hasMatch()) {
|
|
|
|
if (multipleTimeouts > 1) {
|
|
|
|
QString line1;
|
|
|
|
QString line2;
|
|
|
|
|
|
|
|
int amount = timeoutMatch.captured(1).toInt();
|
|
|
|
|
|
|
|
if (amount < 60) {
|
|
|
|
line1 = QString::number(amount);
|
|
|
|
line2 = "s";
|
|
|
|
} else if (amount < 60 * 60) {
|
|
|
|
line1 = QString::number(amount / 60);
|
|
|
|
line2 = "m";
|
|
|
|
} else if (amount < 60 * 60 * 24) {
|
|
|
|
line1 = QString::number(amount / 60 / 60);
|
|
|
|
line2 = "h";
|
|
|
|
} else {
|
|
|
|
line1 = QString::number(amount / 60 / 60 / 24);
|
|
|
|
line2 = "d";
|
|
|
|
}
|
|
|
|
|
|
|
|
this->_moderationActions.emplace_back(line1, line2, str);
|
|
|
|
} else {
|
2018-04-28 15:20:18 +02:00
|
|
|
this->_moderationActions.emplace_back(app->resources->buttonTimeout, str);
|
2018-01-17 14:14:31 +01:00
|
|
|
}
|
|
|
|
} else if (str.startsWith("/ban ")) {
|
2018-04-28 15:20:18 +02:00
|
|
|
this->_moderationActions.emplace_back(app->resources->buttonBan, str);
|
2018-01-17 14:14:31 +01:00
|
|
|
} else {
|
|
|
|
QString xD = str;
|
|
|
|
|
|
|
|
xD.replace(replaceRegex, "");
|
|
|
|
|
|
|
|
this->_moderationActions.emplace_back(xD.mid(0, 2), xD.mid(2, 2), str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 21:33:49 +01:00
|
|
|
|
2018-01-04 01:52:37 +01:00
|
|
|
} // namespace singletons
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|