moved some Settings from Application to Settings

This commit is contained in:
fourtf 2020-02-23 22:15:13 +01:00
parent f8a9850151
commit 5ad427bd61
11 changed files with 102 additions and 77 deletions

View file

@ -5,8 +5,6 @@
#include "common/Args.hpp" #include "common/Args.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp" #include "controllers/commands/CommandController.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/ignores/IgnoreController.hpp" #include "controllers/ignores/IgnoreController.hpp"
#include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/moderationactions/ModerationActions.hpp"
#include "controllers/notifications/NotificationController.hpp" #include "controllers/notifications/NotificationController.hpp"
@ -31,7 +29,6 @@
#include "singletons/Updates.hpp" #include "singletons/Updates.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/IsBigEndian.hpp" #include "util/IsBigEndian.hpp"
#include "util/PersistSignalVector.hpp"
#include "util/PostToThread.hpp" #include "util/PostToThread.hpp"
#include "widgets/Notebook.hpp" #include "widgets/Notebook.hpp"
#include "widgets/Window.hpp" #include "widgets/Window.hpp"
@ -48,11 +45,7 @@ Application *Application::instance = nullptr;
// to each other // to each other
Application::Application(Settings &_settings, Paths &_paths) Application::Application(Settings &_settings, Paths &_paths)
: highlightedMessages(*new SignalVector<HighlightPhrase>()) : themes(&this->emplace<Theme>())
, highlightedUsers(*new SignalVector<HighlightPhrase>())
, blacklistedUsers(*new SignalVector<HighlightBlacklistUser>())
, themes(&this->emplace<Theme>())
, fonts(&this->emplace<Fonts>()) , fonts(&this->emplace<Fonts>())
, emotes(&this->emplace<Emotes>()) , emotes(&this->emplace<Emotes>())
, windows(&this->emplace<WindowManager>()) , windows(&this->emplace<WindowManager>())
@ -62,7 +55,6 @@ Application::Application(Settings &_settings, Paths &_paths)
, commands(&this->emplace<CommandController>()) , commands(&this->emplace<CommandController>())
, notifications(&this->emplace<NotificationController>()) , notifications(&this->emplace<NotificationController>())
, pings(&this->emplace<MutedChannelController>()) , pings(&this->emplace<MutedChannelController>())
, ignores(&this->emplace<IgnoreController>())
, taggedUsers(&this->emplace<TaggedUsersController>()) , taggedUsers(&this->emplace<TaggedUsersController>())
, moderationActions(&this->emplace<ModerationActions>()) , moderationActions(&this->emplace<ModerationActions>())
, twitch2(&this->emplace<TwitchIrcServer>()) , twitch2(&this->emplace<TwitchIrcServer>())
@ -71,10 +63,6 @@ Application::Application(Settings &_settings, Paths &_paths)
{ {
this->instance = this; this->instance = this;
persist(this->highlightedMessages, "/highlighting/highlights");
persist(this->blacklistedUsers, "/highlighting/blacklist");
persist(this->highlightedUsers, "/highlighting/users");
this->fonts->fontChanged.connect( this->fonts->fontChanged.connect(
[this]() { this->windows->layoutChannelViews(); }); [this]() { this->windows->layoutChannelViews(); });
@ -329,28 +317,4 @@ Application *getApp()
return Application::instance; return Application::instance;
} }
bool Application::isHighlightedUser(const QString &username)
{
for (const auto &highlightedUser : this->highlightedUsers)
{
if (highlightedUser.isMatch(username))
return true;
}
return false;
}
bool Application::isBlacklistedUser(const QString &username)
{
auto items = this->blacklistedUsers.readOnly();
for (const auto &blacklistedUser : *items)
{
if (blacklistedUser.isMatch(username))
return true;
}
return false;
}
} // namespace chatterino } // namespace chatterino

View file

@ -32,9 +32,6 @@ class Fonts;
class Toasts; class Toasts;
class ChatterinoBadges; class ChatterinoBadges;
class HighlightPhrase;
class HighlightBlacklistUser;
class Application class Application
{ {
std::vector<std::unique_ptr<Singleton>> singletons_; std::vector<std::unique_ptr<Singleton>> singletons_;
@ -54,15 +51,6 @@ public:
friend void test(); friend void test();
// clang-format off
SignalVector<HighlightPhrase> &highlightedMessages;
SignalVector<HighlightPhrase> &highlightedUsers;
SignalVector<HighlightBlacklistUser> &blacklistedUsers;
// clang-format on
bool isHighlightedUser(const QString &username);
bool isBlacklistedUser(const QString &username);
Theme *const themes{}; Theme *const themes{};
Fonts *const fonts{}; Fonts *const fonts{};
Emotes *const emotes{}; Emotes *const emotes{};
@ -73,7 +61,6 @@ public:
CommandController *const commands{}; CommandController *const commands{};
NotificationController *const notifications{}; NotificationController *const notifications{};
MutedChannelController *const pings{}; MutedChannelController *const pings{};
IgnoreController *const ignores{};
TaggedUsersController *const taggedUsers{}; TaggedUsersController *const taggedUsers{};
ModerationActions *const moderationActions{}; ModerationActions *const moderationActions{};
TwitchIrcServer *const twitch2{}; TwitchIrcServer *const twitch2{};

View file

@ -12,8 +12,6 @@ void IgnoreController::initialize(Settings &, Paths &)
{ {
assert(!this->initialized_); assert(!this->initialized_);
this->initialized_ = true; this->initialized_ = true;
persist(this->phrases, "/ignore/phrases");
} }
} // namespace chatterino } // namespace chatterino

View file

@ -19,8 +19,6 @@ class IgnoreController final : public Singleton
public: public:
virtual void initialize(Settings &settings, Paths &paths) override; virtual void initialize(Settings &settings, Paths &paths) override;
SignalVector<IgnorePhrase> phrases;
private: private:
bool initialized_ = false; bool initialized_ = false;
}; };

View file

@ -37,12 +37,12 @@ QSet<QColor> ColorProvider::recentColors() const
* Currently, only colors used in highlight phrases are considered. This * Currently, only colors used in highlight phrases are considered. This
* may change at any point in the future. * may change at any point in the future.
*/ */
for (auto phrase : getApp()->highlightedMessages) for (auto phrase : getSettings()->highlightedMessages)
{ {
retVal.insert(*phrase.getColor()); retVal.insert(*phrase.getColor());
} }
for (auto userHl : getApp()->highlightedUsers) for (auto userHl : getSettings()->highlightedUsers)
{ {
retVal.insert(*userHl.getColor()); retVal.insert(*userHl.getColor());
} }

View file

@ -169,7 +169,7 @@ bool TwitchMessageBuilder::isIgnored() const
auto app = getApp(); auto app = getApp();
// TODO(pajlada): Do we need to check if the phrase is valid first? // TODO(pajlada): Do we need to check if the phrase is valid first?
auto phrases = app->ignores->phrases.readOnly(); auto phrases = getCSettings().ignoredMessages.readOnly();
for (const auto &phrase : *phrases) for (const auto &phrase : *phrases)
{ {
if (phrase.isBlock() && phrase.isMatch(this->originalMessage_)) if (phrase.isBlock() && phrase.isMatch(this->originalMessage_))
@ -764,8 +764,7 @@ void TwitchMessageBuilder::appendUsername()
void TwitchMessageBuilder::runIgnoreReplaces( void TwitchMessageBuilder::runIgnoreReplaces(
std::vector<std::tuple<int, EmotePtr, EmoteName>> &twitchEmotes) std::vector<std::tuple<int, EmotePtr, EmoteName>> &twitchEmotes)
{ {
auto app = getApp(); auto phrases = getCSettings().ignoredMessages.readOnly();
auto phrases = app->ignores->phrases.readOnly();
auto removeEmotesInRange = auto removeEmotesInRange =
[](int pos, int len, [](int pos, int len,
std::vector<std::tuple<int, EmotePtr, EmoteName>> std::vector<std::tuple<int, EmotePtr, EmoteName>>
@ -1017,7 +1016,7 @@ void TwitchMessageBuilder::parseHighlights()
QString currentUsername = currentUser->getUserName(); QString currentUsername = currentUser->getUserName();
if (app->isBlacklistedUser(this->ircMessage->nick())) if (getCSettings().isBlacklistedUser(this->ircMessage->nick()))
{ {
// Do nothing. We ignore highlights from this user. // Do nothing. We ignore highlights from this user.
return; return;
@ -1057,7 +1056,7 @@ void TwitchMessageBuilder::parseHighlights()
} }
// Highlight because of sender // Highlight because of sender
auto userHighlights = app->highlightedUsers.readOnly(); auto userHighlights = getCSettings().highlightedUsers.readOnly();
for (const HighlightPhrase &userHighlight : *userHighlights) for (const HighlightPhrase &userHighlight : *userHighlights)
{ {
if (!userHighlight.isMatch(this->ircMessage->nick())) if (!userHighlight.isMatch(this->ircMessage->nick()))
@ -1109,7 +1108,7 @@ void TwitchMessageBuilder::parseHighlights()
// TODO: This vector should only be rebuilt upon highlights being changed // TODO: This vector should only be rebuilt upon highlights being changed
// fourtf: should be implemented in the HighlightsController // fourtf: should be implemented in the HighlightsController
std::vector<HighlightPhrase> activeHighlights = std::vector<HighlightPhrase> activeHighlights =
app->highlightedMessages.cloneVector(); getSettings()->highlightedMessages.cloneVector();
if (getSettings()->enableSelfHighlight && currentUsername.size() > 0) if (getSettings()->enableSelfHighlight && currentUsername.size() > 0)
{ {

View file

@ -1,19 +1,71 @@
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/PersistSignalVector.hpp"
#include "util/WindowsHelper.hpp" #include "util/WindowsHelper.hpp"
namespace chatterino { namespace chatterino {
ConcurrentSettings *concurrentInstance_{};
ConcurrentSettings::ConcurrentSettings()
: highlightedMessages(*new SignalVector<HighlightPhrase>())
, highlightedUsers(*new SignalVector<HighlightPhrase>())
, blacklistedUsers(*new SignalVector<HighlightBlacklistUser>())
, ignoredMessages(*new SignalVector<IgnorePhrase>())
{
}
bool ConcurrentSettings::isHighlightedUser(const QString &username)
{
for (const auto &highlightedUser : this->highlightedUsers)
{
if (highlightedUser.isMatch(username))
return true;
}
return false;
}
bool ConcurrentSettings::isBlacklistedUser(const QString &username)
{
auto items = this->blacklistedUsers.readOnly();
for (const auto &blacklistedUser : *items)
{
if (blacklistedUser.isMatch(username))
return true;
}
return false;
}
ConcurrentSettings &getCSettings()
{
// `concurrentInstance_` gets assigned in Settings ctor.
assert(concurrentInstance_);
return *concurrentInstance_;
}
Settings *Settings::instance_ = nullptr; Settings *Settings::instance_ = nullptr;
Settings::Settings(const QString &settingsDirectory) Settings::Settings(const QString &settingsDirectory)
: ABSettings(settingsDirectory) : ABSettings(settingsDirectory)
{ {
instance_ = this; instance_ = this;
concurrentInstance_ = this;
persist(this->highlightedMessages, "/highlighting/highlights");
persist(this->blacklistedUsers, "/highlighting/blacklist");
persist(this->highlightedUsers, "/highlighting/users");
persist(this->ignoredMessages, "/ignore/phrases");
#ifdef USEWINSDK #ifdef USEWINSDK
this->autorun = isRegisteredForStartup(); this->autorun = isRegisteredForStartup();

View file

@ -5,13 +5,38 @@
#include "BaseSettings.hpp" #include "BaseSettings.hpp"
#include "common/Channel.hpp" #include "common/Channel.hpp"
#include "common/SignalVector.hpp"
#include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/moderationactions/ModerationAction.hpp" #include "controllers/moderationactions/ModerationAction.hpp"
#include "singletons/Toasts.hpp" #include "singletons/Toasts.hpp"
namespace chatterino { namespace chatterino {
class Settings : public ABSettings class HighlightPhrase;
class HighlightBlacklistUser;
class IgnorePhrase;
// Settings which are availlable for reading on all threads.
class ConcurrentSettings
{
public:
ConcurrentSettings();
// clang-format off
SignalVector<HighlightPhrase> &highlightedMessages;
SignalVector<HighlightPhrase> &highlightedUsers;
SignalVector<HighlightBlacklistUser> &blacklistedUsers;
SignalVector<IgnorePhrase> &ignoredMessages;
// clang-format on
bool isHighlightedUser(const QString &username);
bool isBlacklistedUser(const QString &username);
};
ConcurrentSettings &getCSettings();
// These settings are still accessed concurrently in the code but it is bad practice.
class Settings : public ABSettings, public ConcurrentSettings
{ {
static Settings *instance_; static Settings *instance_;

View file

@ -8,6 +8,7 @@
#include "providers/twitch/PartialTwitchUser.hpp" #include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
#include "util/PostToThread.hpp" #include "util/PostToThread.hpp"
#include "widgets/Label.hpp" #include "widgets/Label.hpp"
@ -336,23 +337,23 @@ void UserInfoPopup::installEvents()
if (checked) if (checked)
{ {
getApp()->blacklistedUsers.insert( getSettings()->blacklistedUsers.insert(
HighlightBlacklistUser{this->userName_, false}); HighlightBlacklistUser{this->userName_, false});
this->ui_.ignoreHighlights->setEnabled(true); this->ui_.ignoreHighlights->setEnabled(true);
} }
else else
{ {
const auto &vector = getApp()->blacklistedUsers.raw(); const auto &vector = getSettings()->blacklistedUsers.raw();
for (int i = 0; i < vector.size(); i++) for (int i = 0; i < vector.size(); i++)
{ {
if (this->userName_ == vector[i].getPattern()) if (this->userName_ == vector[i].getPattern())
{ {
getApp()->blacklistedUsers.removeAt(i); getSettings()->blacklistedUsers.removeAt(i);
i--; i--;
} }
} }
if (getApp()->isBlacklistedUser(this->userName_)) if (getSettings()->isBlacklistedUser(this->userName_))
{ {
this->ui_.ignoreHighlights->setToolTip( this->ui_.ignoreHighlights->setToolTip(
"Name matched by regex"); "Name matched by regex");
@ -455,7 +456,7 @@ void UserInfoPopup::updateUserData()
// get ignoreHighlights state // get ignoreHighlights state
bool isIgnoringHighlights = false; bool isIgnoringHighlights = false;
const auto &vector = getApp()->blacklistedUsers.raw(); const auto &vector = getSettings()->blacklistedUsers.raw();
for (int i = 0; i < vector.size(); i++) for (int i = 0; i < vector.size(); i++)
{ {
if (this->userName_ == vector[i].getPattern()) if (this->userName_ == vector[i].getPattern())
@ -464,7 +465,7 @@ void UserInfoPopup::updateUserData()
break; break;
} }
} }
if (getApp()->isBlacklistedUser(this->userName_) && if (getSettings()->isBlacklistedUser(this->userName_) &&
!isIgnoringHighlights) !isIgnoringHighlights)
{ {
this->ui_.ignoreHighlights->setToolTip("Name matched by regex"); this->ui_.ignoreHighlights->setToolTip("Name matched by regex");

View file

@ -52,7 +52,8 @@ HighlightingPage::HighlightingPage()
highlights highlights
.emplace<EditableModelView>( .emplace<EditableModelView>(
(new HighlightModel(nullptr)) (new HighlightModel(nullptr))
->initialized(&app->highlightedMessages)) ->initialized(
&getSettings()->highlightedMessages))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
view->setTitles({"Pattern", "Flash\ntaskbar", "Play\nsound", view->setTitles({"Pattern", "Flash\ntaskbar", "Play\nsound",
@ -71,7 +72,7 @@ HighlightingPage::HighlightingPage()
}); });
view->addButtonPressed.connect([] { view->addButtonPressed.connect([] {
getApp()->highlightedMessages.append(HighlightPhrase{ getSettings()->highlightedMessages.append(HighlightPhrase{
"my phrase", true, false, false, false, "", "my phrase", true, false, false, false, "",
*ColorProvider::instance().color( *ColorProvider::instance().color(
ColorType::SelfHighlight)}); ColorType::SelfHighlight)});
@ -94,7 +95,7 @@ HighlightingPage::HighlightingPage()
pingUsers pingUsers
.emplace<EditableModelView>( .emplace<EditableModelView>(
(new UserHighlightModel(nullptr)) (new UserHighlightModel(nullptr))
->initialized(&app->highlightedUsers)) ->initialized(&getSettings()->highlightedUsers))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
@ -118,7 +119,7 @@ HighlightingPage::HighlightingPage()
}); });
view->addButtonPressed.connect([] { view->addButtonPressed.connect([] {
getApp()->highlightedUsers.append(HighlightPhrase{ getSettings()->highlightedUsers.append(HighlightPhrase{
"highlighted user", true, false, false, false, "", "highlighted user", true, false, false, false, "",
*ColorProvider::instance().color( *ColorProvider::instance().color(
ColorType::SelfHighlight)}); ColorType::SelfHighlight)});
@ -140,7 +141,7 @@ HighlightingPage::HighlightingPage()
disabledUsers disabledUsers
.emplace<EditableModelView>( .emplace<EditableModelView>(
(new HighlightBlacklistModel(nullptr)) (new HighlightBlacklistModel(nullptr))
->initialized(&app->blacklistedUsers)) ->initialized(&getSettings()->blacklistedUsers))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
@ -158,7 +159,7 @@ HighlightingPage::HighlightingPage()
}); });
view->addButtonPressed.connect([] { view->addButtonPressed.connect([] {
getApp()->blacklistedUsers.append( getSettings()->blacklistedUsers.append(
HighlightBlacklistUser{"blacklisted user", false}); HighlightBlacklistUser{"blacklisted user", false});
}); });
} }

View file

@ -46,7 +46,7 @@ void addPhrasesTab(LayoutCreator<QVBoxLayout> layout)
layout layout
.emplace<EditableModelView>( .emplace<EditableModelView>(
(new IgnoreModel(nullptr)) (new IgnoreModel(nullptr))
->initialized(&getApp()->ignores->phrases)) ->initialized(&getSettings()->ignoredMessages))
.getElement(); .getElement();
view->setTitles( view->setTitles(
{"Pattern", "Regex", "Case Sensitive", "Block", "Replacement"}); {"Pattern", "Regex", "Case Sensitive", "Block", "Replacement"});
@ -62,7 +62,7 @@ void addPhrasesTab(LayoutCreator<QVBoxLayout> layout)
}); });
view->addButtonPressed.connect([] { view->addButtonPressed.connect([] {
getApp()->ignores->phrases.append( getSettings()->ignoredMessages.append(
IgnorePhrase{"my pattern", false, false, IgnorePhrase{"my pattern", false, false,
getSettings()->ignoredPhraseReplace.getValue(), true}); getSettings()->ignoredPhraseReplace.getValue(), true});
}); });