From d0a81f3fe7f4c2709ebf2d7a6ef752bc90a42531 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 23 Feb 2020 20:34:00 +0100 Subject: [PATCH] refactored TaggedUsers and Ping --- chatterino.pro | 8 +-- src/Application.cpp | 4 +- src/Application.hpp | 4 +- .../pings/MutedChannelController.cpp | 57 +++++++++++++++ .../pings/MutedChannelController.hpp | 32 +++++++++ src/controllers/pings/MutedChannelModel.cpp | 28 ++++++++ .../{PingModel.hpp => MutedChannelModel.hpp} | 8 +-- src/controllers/pings/PingController.cpp | 70 ------------------- src/controllers/pings/PingController.hpp | 36 ---------- src/controllers/pings/PingModel.cpp | 28 -------- .../taggedusers/TaggedUsersController.cpp | 8 --- .../taggedusers/TaggedUsersController.hpp | 2 - src/providers/twitch/TwitchMessageBuilder.cpp | 2 +- src/widgets/splits/SplitHeader.cpp | 5 +- 14 files changed, 131 insertions(+), 161 deletions(-) create mode 100644 src/controllers/pings/MutedChannelController.cpp create mode 100644 src/controllers/pings/MutedChannelController.hpp create mode 100644 src/controllers/pings/MutedChannelModel.cpp rename src/controllers/pings/{PingModel.hpp => MutedChannelModel.hpp} (79%) delete mode 100644 src/controllers/pings/PingController.cpp delete mode 100644 src/controllers/pings/PingController.hpp delete mode 100644 src/controllers/pings/PingModel.cpp diff --git a/chatterino.pro b/chatterino.pro index 78a226811..f663b52cf 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -142,8 +142,8 @@ SOURCES += \ src/controllers/moderationactions/ModerationActions.cpp \ src/controllers/notifications/NotificationController.cpp \ src/controllers/notifications/NotificationModel.cpp \ - src/controllers/pings/PingController.cpp \ - src/controllers/pings/PingModel.cpp \ + src/controllers/pings/MutedChannelController.cpp \ + src/controllers/pings/MutedChannelModel.cpp \ src/controllers/taggedusers/TaggedUser.cpp \ src/controllers/taggedusers/TaggedUsersController.cpp \ src/controllers/taggedusers/TaggedUsersModel.cpp \ @@ -338,8 +338,8 @@ HEADERS += \ src/controllers/moderationactions/ModerationActions.hpp \ src/controllers/notifications/NotificationController.hpp \ src/controllers/notifications/NotificationModel.hpp \ - src/controllers/pings/PingController.hpp \ - src/controllers/pings/PingModel.hpp \ + src/controllers/pings/MutedChannelController.hpp \ + src/controllers/pings/MutedChannelModel.hpp \ src/controllers/taggedusers/TaggedUser.hpp \ src/controllers/taggedusers/TaggedUsersController.hpp \ src/controllers/taggedusers/TaggedUsersModel.hpp \ diff --git a/src/Application.cpp b/src/Application.cpp index fe0aab349..da891ab89 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -9,7 +9,7 @@ #include "controllers/ignores/IgnoreController.hpp" #include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/notifications/NotificationController.hpp" -#include "controllers/pings/PingController.hpp" +#include "controllers/pings/MutedChannelController.hpp" #include "controllers/taggedusers/TaggedUsersController.hpp" #include "messages/MessageBuilder.hpp" #include "providers/bttv/BttvEmotes.hpp" @@ -56,7 +56,7 @@ Application::Application(Settings &_settings, Paths &_paths) , commands(&this->emplace()) , highlights(&this->emplace()) , notifications(&this->emplace()) - , pings(&this->emplace()) + , pings(&this->emplace()) , ignores(&this->emplace()) , taggedUsers(&this->emplace()) , moderationActions(&this->emplace()) diff --git a/src/Application.hpp b/src/Application.hpp index fac8ec0f6..87489fdff 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -18,7 +18,7 @@ class TaggedUsersController; class AccountController; class ModerationActions; class NotificationController; -class PingController; +class MutedChannelController; class Theme; class WindowManager; @@ -60,7 +60,7 @@ public: CommandController *const commands{}; HighlightController *const highlights{}; NotificationController *const notifications{}; - PingController *const pings{}; + MutedChannelController *const pings{}; IgnoreController *const ignores{}; TaggedUsersController *const taggedUsers{}; ModerationActions *const moderationActions{}; diff --git a/src/controllers/pings/MutedChannelController.cpp b/src/controllers/pings/MutedChannelController.cpp new file mode 100644 index 000000000..809097ab0 --- /dev/null +++ b/src/controllers/pings/MutedChannelController.cpp @@ -0,0 +1,57 @@ +#include "controllers/pings/MutedChannelController.hpp" +#include "controllers/pings/MutedChannelModel.hpp" +#include "util/PersistSignalVector.hpp" + +namespace chatterino { + +void MutedChannelController::initialize(Settings &settings, Paths &paths) +{ + this->initialized_ = true; + + persist(this->channels, "/pings/muted"); +} + +bool MutedChannelController::isMuted(const QString &channelName) +{ + for (const auto &channel : this->channels) + { + if (channelName.toLower() == channel.toLower()) + { + return true; + } + } + return false; +} + +void MutedChannelController::mute(const QString &channelName) +{ + channels.append(channelName); +} + +void MutedChannelController::unmute(const QString &channelName) +{ + for (std::vector::size_type i = 0; i != channels.raw().size(); i++) + { + if (channels.raw()[i].toLower() == channelName.toLower()) + { + channels.removeAt(i); + i--; + } + } +} + +bool MutedChannelController::toggleMuted(const QString &channelName) +{ + if (this->isMuted(channelName)) + { + unmute(channelName); + return false; + } + else + { + mute(channelName); + return true; + } +} + +} // namespace chatterino diff --git a/src/controllers/pings/MutedChannelController.hpp b/src/controllers/pings/MutedChannelController.hpp new file mode 100644 index 000000000..ff4a6f050 --- /dev/null +++ b/src/controllers/pings/MutedChannelController.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include "common/SignalVector.hpp" +#include "common/Singleton.hpp" +#include "singletons/Settings.hpp" + +namespace chatterino { + +class Settings; +class Paths; + +class MutedChannelModel; + +class MutedChannelController final : public Singleton, private QObject +{ +public: + virtual void initialize(Settings &settings, Paths &paths) override; + + bool isMuted(const QString &channelName); + bool toggleMuted(const QString &channelName); + +private: + void mute(const QString &channelName); + void unmute(const QString &channelName); + bool initialized_ = false; + + SignalVector channels; +}; + +} // namespace chatterino diff --git a/src/controllers/pings/MutedChannelModel.cpp b/src/controllers/pings/MutedChannelModel.cpp new file mode 100644 index 000000000..fc1473506 --- /dev/null +++ b/src/controllers/pings/MutedChannelModel.cpp @@ -0,0 +1,28 @@ +#include "MutedChannelModel.hpp" + +#include "Application.hpp" +#include "singletons/Settings.hpp" +#include "util/StandardItemHelper.hpp" + +namespace chatterino { + +MutedChannelModel::MutedChannelModel(QObject *parent) + : SignalVectorModel(1, parent) +{ +} + +// turn a vector item into a model row +QString MutedChannelModel::getItemFromRow(std::vector &row, + const QString &original) +{ + return QString(row[0]->data(Qt::DisplayRole).toString()); +} + +// turn a model +void MutedChannelModel::getRowFromItem(const QString &item, + std::vector &row) +{ + setStringItem(row[0], item); +} + +} // namespace chatterino diff --git a/src/controllers/pings/PingModel.hpp b/src/controllers/pings/MutedChannelModel.hpp similarity index 79% rename from src/controllers/pings/PingModel.hpp rename to src/controllers/pings/MutedChannelModel.hpp index 137be5e0c..c53b3177c 100644 --- a/src/controllers/pings/PingModel.hpp +++ b/src/controllers/pings/MutedChannelModel.hpp @@ -7,11 +7,11 @@ namespace chatterino { -class PingController; +class MutedChannelController; -class PingModel : public SignalVectorModel +class MutedChannelModel : public SignalVectorModel { - explicit PingModel(QObject *parent); + explicit MutedChannelModel(QObject *parent); protected: // turn a vector item into a model row @@ -21,8 +21,6 @@ protected: // turns a row in the model into a vector item virtual void getRowFromItem(const QString &item, std::vector &row) override; - - friend class PingController; }; } // namespace chatterino diff --git a/src/controllers/pings/PingController.cpp b/src/controllers/pings/PingController.cpp deleted file mode 100644 index 268dfe18a..000000000 --- a/src/controllers/pings/PingController.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "controllers/pings/PingController.hpp" -#include "controllers/pings/PingModel.hpp" - -namespace chatterino { - -void PingController::initialize(Settings &settings, Paths &paths) -{ - this->initialized_ = true; - for (const QString &channelName : this->pingSetting_.getValue()) - { - this->channelVector.append(channelName); - } - - this->channelVector.delayedItemsChanged.connect([this] { // - this->pingSetting_.setValue(this->channelVector.raw()); - }); -} - -PingModel *PingController::createModel(QObject *parent) -{ - PingModel *model = new PingModel(parent); - model->initialize(&this->channelVector); - return model; -} - -bool PingController::isMuted(const QString &channelName) -{ - for (const auto &channel : this->channelVector) - { - if (channelName.toLower() == channel.toLower()) - { - return true; - } - } - return false; -} - -void PingController::muteChannel(const QString &channelName) -{ - channelVector.append(channelName); -} - -void PingController::unmuteChannel(const QString &channelName) -{ - for (std::vector::size_type i = 0; i != channelVector.raw().size(); - i++) - { - if (channelVector.raw()[i].toLower() == channelName.toLower()) - { - channelVector.removeAt(i); - i--; - } - } -} - -bool PingController::toggleMuteChannel(const QString &channelName) -{ - if (this->isMuted(channelName)) - { - unmuteChannel(channelName); - return false; - } - else - { - muteChannel(channelName); - return true; - } -} - -} // namespace chatterino diff --git a/src/controllers/pings/PingController.hpp b/src/controllers/pings/PingController.hpp deleted file mode 100644 index b0b5542b4..000000000 --- a/src/controllers/pings/PingController.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -#include "common/SignalVector.hpp" -#include "common/Singleton.hpp" -#include "singletons/Settings.hpp" - -namespace chatterino { - -class Settings; -class Paths; - -class PingModel; - -class PingController final : public Singleton, private QObject -{ -public: - virtual void initialize(Settings &settings, Paths &paths) override; - - bool isMuted(const QString &channelName); - void muteChannel(const QString &channelName); - void unmuteChannel(const QString &channelName); - bool toggleMuteChannel(const QString &channelName); - - PingModel *createModel(QObject *parent); - -private: - bool initialized_ = false; - - SignalVector channelVector; - - ChatterinoSetting> pingSetting_ = {"/pings/muted"}; -}; - -} // namespace chatterino diff --git a/src/controllers/pings/PingModel.cpp b/src/controllers/pings/PingModel.cpp deleted file mode 100644 index 28098a209..000000000 --- a/src/controllers/pings/PingModel.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "PingModel.hpp" - -#include "Application.hpp" -#include "singletons/Settings.hpp" -#include "util/StandardItemHelper.hpp" - -namespace chatterino { - -PingModel::PingModel(QObject *parent) - : SignalVectorModel(1, parent) -{ -} - -// turn a vector item into a model row -QString PingModel::getItemFromRow(std::vector &row, - const QString &original) -{ - return QString(row[0]->data(Qt::DisplayRole).toString()); -} - -// turn a model -void PingModel::getRowFromItem(const QString &item, - std::vector &row) -{ - setStringItem(row[0], item); -} - -} // namespace chatterino diff --git a/src/controllers/taggedusers/TaggedUsersController.cpp b/src/controllers/taggedusers/TaggedUsersController.cpp index a42265fd4..a81b100c3 100644 --- a/src/controllers/taggedusers/TaggedUsersController.cpp +++ b/src/controllers/taggedusers/TaggedUsersController.cpp @@ -9,12 +9,4 @@ TaggedUsersController::TaggedUsersController() { } -TaggedUsersModel *TaggedUsersController::createModel(QObject *parent) -{ - TaggedUsersModel *model = new TaggedUsersModel(parent); - model->initialize(&this->users); - - return model; -} - } // namespace chatterino diff --git a/src/controllers/taggedusers/TaggedUsersController.hpp b/src/controllers/taggedusers/TaggedUsersController.hpp index ea917195a..dd9c4d897 100644 --- a/src/controllers/taggedusers/TaggedUsersController.hpp +++ b/src/controllers/taggedusers/TaggedUsersController.hpp @@ -15,8 +15,6 @@ public: TaggedUsersController(); SignalVector users; - - TaggedUsersModel *createModel(QObject *parent = nullptr); }; } // namespace chatterino diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 2e1d18b4c..09e5f675b 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -4,7 +4,7 @@ #include "controllers/accounts/AccountController.hpp" #include "controllers/highlights/HighlightController.hpp" #include "controllers/ignores/IgnoreController.hpp" -#include "controllers/pings/PingController.hpp" +#include "controllers/pings/MutedChannelController.hpp" #include "messages/Message.hpp" #include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/twitch/TwitchBadges.hpp" diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index 79153b44c..84a389aae 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -4,7 +4,7 @@ #include "controllers/accounts/AccountController.hpp" #include "controllers/moderationactions/ModerationActions.hpp" #include "controllers/notifications/NotificationController.hpp" -#include "controllers/pings/PingController.hpp" +#include "controllers/pings/MutedChannelController.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchIrcServer.hpp" #include "singletons/Resources.hpp" @@ -343,8 +343,7 @@ std::unique_ptr SplitHeader::createMainMenu() this->split_->getChannel()->getName())); }); action->connect(action, &QAction::triggered, this, [this]() { - getApp()->pings->toggleMuteChannel( - this->split_->getChannel()->getName()); + getApp()->pings->toggleMuted(this->split_->getChannel()->getName()); }); moreMenu->addAction(action);