From f8a98501514e4d7dde415e6c3940037c64a309a7 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 23 Feb 2020 21:18:40 +0100 Subject: [PATCH] absorbed HighlightController into Application --- chatterino.pro | 2 - src/Application.cpp | 40 +++++++++++++++++-- src/Application.hpp | 14 ++++++- .../highlights/HighlightController.cpp | 40 ------------------- .../highlights/HighlightController.hpp | 26 ------------ src/providers/colors/ColorProvider.cpp | 5 +-- src/providers/twitch/IrcMessageHandler.cpp | 1 - src/providers/twitch/TwitchIrcServer.cpp | 1 - src/providers/twitch/TwitchMessageBuilder.cpp | 7 ++-- src/widgets/dialogs/UserInfoPopup.cpp | 15 ++++--- .../settingspages/HighlightingPage.cpp | 22 +++++----- 11 files changed, 70 insertions(+), 103 deletions(-) delete mode 100644 src/controllers/highlights/HighlightController.cpp delete mode 100644 src/controllers/highlights/HighlightController.hpp diff --git a/chatterino.pro b/chatterino.pro index f663b52cf..d19b4fb83 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -131,7 +131,6 @@ SOURCES += \ src/controllers/commands/CommandController.cpp \ src/controllers/commands/CommandModel.cpp \ src/controllers/highlights/HighlightBlacklistModel.cpp \ - src/controllers/highlights/HighlightController.cpp \ src/controllers/highlights/HighlightModel.cpp \ src/controllers/highlights/HighlightPhrase.cpp \ src/controllers/highlights/UserHighlightModel.cpp \ @@ -326,7 +325,6 @@ HEADERS += \ src/controllers/commands/CommandModel.hpp \ src/controllers/highlights/HighlightBlacklistModel.hpp \ src/controllers/highlights/HighlightBlacklistUser.hpp \ - src/controllers/highlights/HighlightController.hpp \ src/controllers/highlights/HighlightModel.hpp \ src/controllers/highlights/HighlightPhrase.hpp \ src/controllers/highlights/UserHighlightModel.hpp \ diff --git a/src/Application.cpp b/src/Application.cpp index da891ab89..4e0ae212e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -5,7 +5,8 @@ #include "common/Args.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/commands/CommandController.hpp" -#include "controllers/highlights/HighlightController.hpp" +#include "controllers/highlights/HighlightBlacklistUser.hpp" +#include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/ignores/IgnoreController.hpp" #include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/notifications/NotificationController.hpp" @@ -30,6 +31,7 @@ #include "singletons/Updates.hpp" #include "singletons/WindowManager.hpp" #include "util/IsBigEndian.hpp" +#include "util/PersistSignalVector.hpp" #include "util/PostToThread.hpp" #include "widgets/Notebook.hpp" #include "widgets/Window.hpp" @@ -46,7 +48,11 @@ Application *Application::instance = nullptr; // to each other Application::Application(Settings &_settings, Paths &_paths) - : themes(&this->emplace()) + : highlightedMessages(*new SignalVector()) + , highlightedUsers(*new SignalVector()) + , blacklistedUsers(*new SignalVector()) + + , themes(&this->emplace()) , fonts(&this->emplace()) , emotes(&this->emplace()) , windows(&this->emplace()) @@ -54,7 +60,6 @@ Application::Application(Settings &_settings, Paths &_paths) , accounts(&this->emplace()) , commands(&this->emplace()) - , highlights(&this->emplace()) , notifications(&this->emplace()) , pings(&this->emplace()) , ignores(&this->emplace()) @@ -63,10 +68,13 @@ Application::Application(Settings &_settings, Paths &_paths) , twitch2(&this->emplace()) , chatterinoBadges(&this->emplace()) , logging(&this->emplace()) - { this->instance = this; + persist(this->highlightedMessages, "/highlighting/highlights"); + persist(this->blacklistedUsers, "/highlighting/blacklist"); + persist(this->highlightedUsers, "/highlighting/users"); + this->fonts->fontChanged.connect( [this]() { this->windows->layoutChannelViews(); }); @@ -321,4 +329,28 @@ Application *getApp() 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 diff --git a/src/Application.hpp b/src/Application.hpp index 87489fdff..966e21ef5 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -3,6 +3,7 @@ #include #include +#include "common/SignalVector.hpp" #include "common/Singleton.hpp" #include "singletons/NativeMessaging.hpp" @@ -31,6 +32,9 @@ class Fonts; class Toasts; class ChatterinoBadges; +class HighlightPhrase; +class HighlightBlacklistUser; + class Application { std::vector> singletons_; @@ -50,6 +54,15 @@ public: friend void test(); + // clang-format off + SignalVector &highlightedMessages; + SignalVector &highlightedUsers; + SignalVector &blacklistedUsers; + // clang-format on + + bool isHighlightedUser(const QString &username); + bool isBlacklistedUser(const QString &username); + Theme *const themes{}; Fonts *const fonts{}; Emotes *const emotes{}; @@ -58,7 +71,6 @@ public: AccountController *const accounts{}; CommandController *const commands{}; - HighlightController *const highlights{}; NotificationController *const notifications{}; MutedChannelController *const pings{}; IgnoreController *const ignores{}; diff --git a/src/controllers/highlights/HighlightController.cpp b/src/controllers/highlights/HighlightController.cpp deleted file mode 100644 index d81ea74e7..000000000 --- a/src/controllers/highlights/HighlightController.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "HighlightController.hpp" - -#include "Application.hpp" -#include "util/PersistSignalVector.hpp" - -namespace chatterino { - -void HighlightController::initialize(Settings &settings, Paths &paths) -{ - assert(!this->initialized_); - this->initialized_ = true; - - persist(this->phrases, "/highlighting/highlights"); - persist(this->blacklistedUsers, "/highlighting/blacklist"); - persist(this->highlightedUsers, "/highlighting/users"); -} - -bool HighlightController::isHighlightedUser(const QString &username) -{ - for (const auto &highlightedUser : this->highlightedUsers) - { - if (highlightedUser.isMatch(username)) - return true; - } - - return false; -} - -bool HighlightController::blacklistContains(const QString &username) -{ - for (const auto &blacklistedUser : *this->blacklistedUsers.readOnly()) - { - if (blacklistedUser.isMatch(username)) - return true; - } - - return false; -} - -} // namespace chatterino diff --git a/src/controllers/highlights/HighlightController.hpp b/src/controllers/highlights/HighlightController.hpp deleted file mode 100644 index 6741e1dbd..000000000 --- a/src/controllers/highlights/HighlightController.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "common/SignalVector.hpp" -#include "common/Singleton.hpp" -#include "controllers/highlights/HighlightBlacklistUser.hpp" -#include "controllers/highlights/HighlightPhrase.hpp" - -namespace chatterino { - -class HighlightController final : public Singleton -{ -public: - virtual void initialize(Settings &settings, Paths &paths) override; - - SignalVector phrases; - SignalVector blacklistedUsers; - SignalVector highlightedUsers; - - bool isHighlightedUser(const QString &username); - bool blacklistContains(const QString &username); - -private: - bool initialized_ = false; -}; - -} // namespace chatterino diff --git a/src/providers/colors/ColorProvider.cpp b/src/providers/colors/ColorProvider.cpp index 492689aa0..ce61a69f6 100644 --- a/src/providers/colors/ColorProvider.cpp +++ b/src/providers/colors/ColorProvider.cpp @@ -1,6 +1,5 @@ #include "providers/colors/ColorProvider.hpp" -#include "controllers/highlights/HighlightController.hpp" #include "singletons/Theme.hpp" namespace chatterino { @@ -38,12 +37,12 @@ QSet ColorProvider::recentColors() const * Currently, only colors used in highlight phrases are considered. This * may change at any point in the future. */ - for (auto phrase : getApp()->highlights->phrases) + for (auto phrase : getApp()->highlightedMessages) { retVal.insert(*phrase.getColor()); } - for (auto userHl : getApp()->highlights->highlightedUsers) + for (auto userHl : getApp()->highlightedUsers) { retVal.insert(*userHl.getColor()); } diff --git a/src/providers/twitch/IrcMessageHandler.cpp b/src/providers/twitch/IrcMessageHandler.cpp index 5b022bc1e..60d5a5c5a 100644 --- a/src/providers/twitch/IrcMessageHandler.cpp +++ b/src/providers/twitch/IrcMessageHandler.cpp @@ -2,7 +2,6 @@ #include "Application.hpp" #include "controllers/accounts/AccountController.hpp" -#include "controllers/highlights/HighlightController.hpp" #include "messages/LimitedQueue.hpp" #include "messages/Message.hpp" #include "providers/twitch/TwitchAccountManager.hpp" diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index d13850b46..7ed568c8f 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -7,7 +7,6 @@ #include "common/Common.hpp" #include "common/Env.hpp" #include "controllers/accounts/AccountController.hpp" -#include "controllers/highlights/HighlightController.hpp" #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" #include "providers/twitch/IrcMessageHandler.hpp" diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 09e5f675b..8902512d1 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -2,7 +2,6 @@ #include "Application.hpp" #include "controllers/accounts/AccountController.hpp" -#include "controllers/highlights/HighlightController.hpp" #include "controllers/ignores/IgnoreController.hpp" #include "controllers/pings/MutedChannelController.hpp" #include "messages/Message.hpp" @@ -1018,7 +1017,7 @@ void TwitchMessageBuilder::parseHighlights() QString currentUsername = currentUser->getUserName(); - if (app->highlights->blacklistContains(this->ircMessage->nick())) + if (app->isBlacklistedUser(this->ircMessage->nick())) { // Do nothing. We ignore highlights from this user. return; @@ -1058,7 +1057,7 @@ void TwitchMessageBuilder::parseHighlights() } // Highlight because of sender - auto userHighlights = app->highlights->highlightedUsers.readOnly(); + auto userHighlights = app->highlightedUsers.readOnly(); for (const HighlightPhrase &userHighlight : *userHighlights) { if (!userHighlight.isMatch(this->ircMessage->nick())) @@ -1110,7 +1109,7 @@ void TwitchMessageBuilder::parseHighlights() // TODO: This vector should only be rebuilt upon highlights being changed // fourtf: should be implemented in the HighlightsController std::vector activeHighlights = - app->highlights->phrases.cloneVector(); + app->highlightedMessages.cloneVector(); if (getSettings()->enableSelfHighlight && currentUsername.size() > 0) { diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index e54a4373a..d60414488 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -4,7 +4,7 @@ #include "common/Channel.hpp" #include "common/NetworkRequest.hpp" #include "controllers/accounts/AccountController.hpp" -#include "controllers/highlights/HighlightController.hpp" +#include "controllers/highlights/HighlightBlacklistUser.hpp" #include "providers/twitch/PartialTwitchUser.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "singletons/Resources.hpp" @@ -336,24 +336,23 @@ void UserInfoPopup::installEvents() if (checked) { - getApp()->highlights->blacklistedUsers.insert( + getApp()->blacklistedUsers.insert( HighlightBlacklistUser{this->userName_, false}); this->ui_.ignoreHighlights->setEnabled(true); } else { - const auto &vector = - getApp()->highlights->blacklistedUsers.raw(); + const auto &vector = getApp()->blacklistedUsers.raw(); for (int i = 0; i < vector.size(); i++) { if (this->userName_ == vector[i].getPattern()) { - getApp()->highlights->blacklistedUsers.removeAt(i); + getApp()->blacklistedUsers.removeAt(i); i--; } } - if (getApp()->highlights->blacklistContains(this->userName_)) + if (getApp()->isBlacklistedUser(this->userName_)) { this->ui_.ignoreHighlights->setToolTip( "Name matched by regex"); @@ -456,7 +455,7 @@ void UserInfoPopup::updateUserData() // get ignoreHighlights state bool isIgnoringHighlights = false; - const auto &vector = getApp()->highlights->blacklistedUsers.raw(); + const auto &vector = getApp()->blacklistedUsers.raw(); for (int i = 0; i < vector.size(); i++) { if (this->userName_ == vector[i].getPattern()) @@ -465,7 +464,7 @@ void UserInfoPopup::updateUserData() break; } } - if (getApp()->highlights->blacklistContains(this->userName_) && + if (getApp()->isBlacklistedUser(this->userName_) && !isIgnoringHighlights) { this->ui_.ignoreHighlights->setToolTip("Name matched by regex"); diff --git a/src/widgets/settingspages/HighlightingPage.cpp b/src/widgets/settingspages/HighlightingPage.cpp index fb2d7da01..5013bc30e 100644 --- a/src/widgets/settingspages/HighlightingPage.cpp +++ b/src/widgets/settingspages/HighlightingPage.cpp @@ -2,7 +2,6 @@ #include "Application.hpp" #include "controllers/highlights/HighlightBlacklistModel.hpp" -#include "controllers/highlights/HighlightController.hpp" #include "controllers/highlights/HighlightModel.hpp" #include "controllers/highlights/UserHighlightModel.hpp" #include "singletons/Settings.hpp" @@ -53,7 +52,7 @@ HighlightingPage::HighlightingPage() highlights .emplace( (new HighlightModel(nullptr)) - ->initialized(&app->highlights->phrases)) + ->initialized(&app->highlightedMessages)) .getElement(); view->addRegexHelpLink(); view->setTitles({"Pattern", "Flash\ntaskbar", "Play\nsound", @@ -72,7 +71,7 @@ HighlightingPage::HighlightingPage() }); view->addButtonPressed.connect([] { - getApp()->highlights->phrases.append(HighlightPhrase{ + getApp()->highlightedMessages.append(HighlightPhrase{ "my phrase", true, false, false, false, "", *ColorProvider::instance().color( ColorType::SelfHighlight)}); @@ -95,8 +94,7 @@ HighlightingPage::HighlightingPage() pingUsers .emplace( (new UserHighlightModel(nullptr)) - ->initialized( - &app->highlights->highlightedUsers)) + ->initialized(&app->highlightedUsers)) .getElement(); view->addRegexHelpLink(); @@ -120,11 +118,10 @@ HighlightingPage::HighlightingPage() }); view->addButtonPressed.connect([] { - getApp()->highlights->highlightedUsers.append( - HighlightPhrase{"highlighted user", true, false, false, - false, "", - *ColorProvider::instance().color( - ColorType::SelfHighlight)}); + getApp()->highlightedUsers.append(HighlightPhrase{ + "highlighted user", true, false, false, false, "", + *ColorProvider::instance().color( + ColorType::SelfHighlight)}); }); QObject::connect(view->getTableView(), &QTableView::clicked, @@ -143,8 +140,7 @@ HighlightingPage::HighlightingPage() disabledUsers .emplace( (new HighlightBlacklistModel(nullptr)) - ->initialized( - &app->highlights->blacklistedUsers)) + ->initialized(&app->blacklistedUsers)) .getElement(); view->addRegexHelpLink(); @@ -162,7 +158,7 @@ HighlightingPage::HighlightingPage() }); view->addButtonPressed.connect([] { - getApp()->highlights->blacklistedUsers.append( + getApp()->blacklistedUsers.append( HighlightBlacklistUser{"blacklisted user", false}); }); }