mirror-chatterino2/src/singletons/Settings.cpp
askepticaldreamer 4c782ce90c
Add feature to select channels to log (#4302)
* Add checkbox for custom logging and table with channels to log on Logs page

* Add checkbox to enable and disable logging per channel

* Return from addMessage before logging if custom logging enabled and channel does not have logging enabled

* Use clang-format to fix formatting

* Add CHANGELOG.md entry

* Resolve PR comments

* Remove toggle for channels so any channel listed will be logged

* Move Only log channels listed below checkbox to just above table

* Fix formatting

* Re-order changelog

* ChannelLog constructor: Copy & move instead of const ref & copy

* ChannelLog::createEmpty: Curly brace initialize instead of repeating
name

* ChannelLog toString & createEmpty: nodiscard

* Use COUNT paradigm in model column

* Remove ChanneLoggingModel source file comments

* Use Column::Channel in getRowFromItem

* Rename `getItemFromRow` parameter and mark it as unused

* Curly brace initialize ChannelLog

* private & friend class the model

* Filter out channels to log using a set instead of iterating over a vector every time a message comes in

* Rename `ChannelLog::channel` member to `ChannelLog::channelName`

Also made it private

* mini comment on ChannelLog

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
2023-01-15 11:47:22 +00:00

151 lines
3.9 KiB
C++

#include "singletons/Settings.hpp"
#include "controllers/filters/FilterRecord.hpp"
#include "controllers/highlights/HighlightBadge.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "controllers/moderationactions/ModerationAction.hpp"
#include "controllers/nicknames/Nickname.hpp"
#include "util/PersistSignalVector.hpp"
#include "util/WindowsHelper.hpp"
namespace chatterino {
ConcurrentSettings *concurrentInstance_{};
ConcurrentSettings::ConcurrentSettings()
// NOTE: these do not get deleted
: highlightedMessages(*new SignalVector<HighlightPhrase>())
, highlightedUsers(*new SignalVector<HighlightPhrase>())
, highlightedBadges(*new SignalVector<HighlightBadge>())
, blacklistedUsers(*new SignalVector<HighlightBlacklistUser>())
, ignoredMessages(*new SignalVector<IgnorePhrase>())
, mutedChannels(*new SignalVector<QString>())
, filterRecords(*new SignalVector<FilterRecordPtr>())
, nicknames(*new SignalVector<Nickname>())
, moderationActions(*new SignalVector<ModerationAction>)
, loggedChannels(*new SignalVector<ChannelLog>)
{
persist(this->highlightedMessages, "/highlighting/highlights");
persist(this->blacklistedUsers, "/highlighting/blacklist");
persist(this->highlightedBadges, "/highlighting/badges");
persist(this->highlightedUsers, "/highlighting/users");
persist(this->ignoredMessages, "/ignore/phrases");
persist(this->mutedChannels, "/pings/muted");
persist(this->filterRecords, "/filtering/filters");
persist(this->nicknames, "/nicknames");
// tagged users?
persist(this->moderationActions, "/moderation/actions");
persist(this->loggedChannels, "/logging/channels");
}
bool ConcurrentSettings::isHighlightedUser(const QString &username)
{
auto items = this->highlightedUsers.readOnly();
for (const auto &highlightedUser : *items)
{
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;
}
bool ConcurrentSettings::isMutedChannel(const QString &channelName)
{
auto items = this->mutedChannels.readOnly();
for (const auto &channel : *items)
{
if (channelName.toLower() == channel.toLower())
{
return true;
}
}
return false;
}
void ConcurrentSettings::mute(const QString &channelName)
{
mutedChannels.append(channelName);
}
void ConcurrentSettings::unmute(const QString &channelName)
{
for (std::vector<int>::size_type i = 0; i != mutedChannels.raw().size();
i++)
{
if (mutedChannels.raw()[i].toLower() == channelName.toLower())
{
mutedChannels.removeAt(i);
i--;
}
}
}
bool ConcurrentSettings::toggleMutedChannel(const QString &channelName)
{
if (this->isMutedChannel(channelName))
{
unmute(channelName);
return false;
}
else
{
mute(channelName);
return true;
}
}
ConcurrentSettings &getCSettings()
{
// `concurrentInstance_` gets assigned in Settings ctor.
assert(concurrentInstance_);
return *concurrentInstance_;
}
Settings *Settings::instance_ = nullptr;
Settings::Settings(const QString &settingsDirectory)
: ABSettings(settingsDirectory)
{
instance_ = this;
concurrentInstance_ = this;
#ifdef USEWINSDK
this->autorun = isRegisteredForStartup();
this->autorun.connect(
[](bool autorun) {
setRegisteredForStartup(autorun);
},
false);
#endif
}
Settings &Settings::instance()
{
return *instance_;
}
Settings *getSettings()
{
return &Settings::instance();
}
} // namespace chatterino