diff --git a/lib/settings b/lib/settings index 89f767a5e..75aca0343 160000 --- a/lib/settings +++ b/lib/settings @@ -1 +1 @@ -Subproject commit 89f767a5ed2ea5724a4bf5b6170365e5d069634a +Subproject commit 75aca034359fc0e2145908beb459267e8be3d7ca diff --git a/src/settingsmanager.cpp b/src/settingsmanager.cpp index 8c9745f0e..b8a7f683a 100644 --- a/src/settingsmanager.cpp +++ b/src/settingsmanager.cpp @@ -1,7 +1,7 @@ #include "settingsmanager.hpp" #include "appdatapath.hpp" +#include "debug/log.hpp" -#include #include #include @@ -9,6 +9,13 @@ using namespace chatterino::messages; namespace chatterino { +std::vector> _settings; + +void _registerSetting(std::weak_ptr setting) +{ + _settings.push_back(setting); +} + SettingsManager::SettingsManager() : streamlinkPath("/behaviour/streamlink/path", "") , preferredQuality("/behaviour/streamlink/quality", "Choose") @@ -17,6 +24,7 @@ SettingsManager::SettingsManager() , highlightProperties(this->settingsItems, "highlightProperties", QMap>()) , highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "") + , snapshot(nullptr) , settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat) { this->wordMaskListener.addSetting(this->showTimestamps); @@ -62,7 +70,6 @@ void SettingsManager::load() } else { this->settings.beginGroup("Highlights"); QStringList list = this->settings.childGroups(); - qDebug() << list.join(","); for (auto string : list) { this->settings.beginGroup(string); highlightProperties.insertMap(string, @@ -129,19 +136,51 @@ void SettingsManager::updateWordTypeMask() } } -SettingsSnapshot SettingsManager::createSnapshot() +void SettingsManager::saveSnapshot() { - SettingsSnapshot snapshot; + rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType); + rapidjson::Document::AllocatorType &a = d->GetAllocator(); - for (auto &item : this->settingsItems) { - if (item.get().getName() != "highlightProperties") { - snapshot.addItem(item, item.get().getVariant()); - } else { - snapshot.mapItems = highlightProperties.get(); + 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); } - return snapshot; + this->snapshot.reset(d); + + debug::Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d)); +} + +void SettingsManager::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]); + } } } // namespace chatterino diff --git a/src/settingsmanager.hpp b/src/settingsmanager.hpp index addca5551..29c311429 100644 --- a/src/settingsmanager.hpp +++ b/src/settingsmanager.hpp @@ -2,7 +2,6 @@ #include "messages/word.hpp" #include "setting.hpp" -#include "settingssnapshot.hpp" #include #include @@ -10,12 +9,76 @@ namespace chatterino { +static void _registerSetting(std::weak_ptr setting); + +template +class ChatterinoSetting : public pajlada::Settings::Setting +{ +public: + ChatterinoSetting(const std::string &_path, const Type &_defaultValue) + : pajlada::Settings::Setting(_path, _defaultValue) + { + _registerSetting(this->data); + } + + void saveRecall(); + + ChatterinoSetting &operator=(const Type &newValue) + { + assert(this->data != nullptr); + + this->setValue(newValue); + + return *this; + } + + template + ChatterinoSetting &operator=(const T2 &newValue) + { + assert(this->data != nullptr); + + this->setValue(newValue); + + return *this; + } + + ChatterinoSetting &operator=(Type &&newValue) noexcept + { + assert(this->data != nullptr); + + this->setValue(std::move(newValue)); + + return *this; + } + + bool operator==(const Type &rhs) const + { + assert(this->data != nullptr); + + return this->getValue() == rhs; + } + + bool operator!=(const Type &rhs) const + { + assert(this->data != nullptr); + + return this->getValue() != rhs; + } + + operator const Type() const + { + assert(this->data != nullptr); + + return this->getValue(); + } +}; + class SettingsManager : public QObject { Q_OBJECT - using BoolSetting = pajlada::Settings::Setting; - using FloatSetting = pajlada::Settings::Setting; + using BoolSetting = ChatterinoSetting; + using FloatSetting = ChatterinoSetting; public: void load(); @@ -24,7 +87,6 @@ public: messages::Word::Flags getWordTypeMask(); bool isIgnoredEmote(const QString &emote); QSettings &getQSettings(); - SettingsSnapshot createSnapshot(); /// Appearance BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true}; @@ -87,10 +149,15 @@ public: } void updateWordTypeMask(); + void saveSnapshot(); + void recallSnapshot(); + signals: void wordTypeMaskChanged(); private: + std::unique_ptr snapshot; + SettingsManager(); QSettings settings; diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 421c02651..37a254632 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -27,7 +27,6 @@ namespace widgets { SettingsDialog::SettingsDialog() : BaseWidget() - , snapshot(SettingsManager::getInstance().createSnapshot()) , usernameDisplayMode( "/appearance/messages/usernameDisplayMode", twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName) @@ -615,6 +614,8 @@ void SettingsDialog::showDialog(PreferredTab preferredTab) void SettingsDialog::refresh() { this->ui.accountSwitchWidget->refresh(); + + SettingsManager::getInstance().saveSnapshot(); } void SettingsDialog::dpiMultiplierChanged(float oldDpi, float newDpi) @@ -683,7 +684,9 @@ QCheckBox *SettingsDialog::createCheckbox(const QString &title, auto checkbox = new QCheckBox(title); // Set checkbox initial state - checkbox->setChecked(setting.getValue()); + setting.connect([checkbox](const bool &value, auto) { + checkbox->setChecked(value); // + }); QObject::connect(checkbox, &QCheckBox::toggled, this, [&setting](bool state) { qDebug() << "update checkbox value"; @@ -751,13 +754,11 @@ void SettingsDialog::okButtonClicked() void SettingsDialog::cancelButtonClicked() { - // TODO: Re-implement the snapshot feature properly - auto &instance = SettingsManager::getInstance(); + auto &settings = SettingsManager::getInstance(); - this->snapshot.apply(); - instance.highlightProperties.set(this->snapshot.mapItems); + settings.recallSnapshot(); - QStringList list = instance.highlightProperties.get().keys(); + QStringList list = settings.highlightProperties.get().keys(); list.removeDuplicates(); while (globalHighlights->count() > 0) { delete globalHighlights->takeItem(0); diff --git a/src/widgets/settingsdialog.hpp b/src/widgets/settingsdialog.hpp index d242a5998..9ac34f0e2 100644 --- a/src/widgets/settingsdialog.hpp +++ b/src/widgets/settingsdialog.hpp @@ -1,7 +1,6 @@ #pragma once #include "settingsmanager.hpp" -#include "settingssnapshot.hpp" #include "widgets/accountswitchwidget.hpp" #include "widgets/helper/settingsdialogtab.hpp" @@ -27,11 +26,13 @@ namespace widgets { class SettingsDialog : public BaseWidget { -public: SettingsDialog(); void select(SettingsDialogTab *tab); + friend class SettingsDialogTab; + +public: enum class PreferredTab { NoPreference, Accounts, @@ -45,7 +46,6 @@ protected: private: void refresh(); - SettingsSnapshot snapshot; std::vector tabs; pajlada::Settings::Setting usernameDisplayMode;