mirror-chatterino2/src/singletons/Settings.cpp

146 lines
3.5 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Paths.hpp"
#include "singletons/Resources.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include "util/PersistSignalVector.hpp"
#include "util/WindowsHelper.hpp"
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
ConcurrentSettings *concurrentInstance_{};
ConcurrentSettings::ConcurrentSettings()
2020-02-23 23:07:28 +01:00
// NOTE: these do not get deleted
: highlightedMessages(*new SignalVector<HighlightPhrase>())
, highlightedUsers(*new SignalVector<HighlightPhrase>())
, blacklistedUsers(*new SignalVector<HighlightBlacklistUser>())
, ignoredMessages(*new SignalVector<IgnorePhrase>())
2020-02-23 23:07:28 +01:00
, mutedChannels(*new SignalVector<QString>())
, filterRecords(*new SignalVector<FilterRecordPtr>())
2020-02-23 23:07:28 +01:00
, moderationActions(*new SignalVector<ModerationAction>)
{
2020-02-23 23:07:28 +01:00
persist(this->highlightedMessages, "/highlighting/highlights");
persist(this->blacklistedUsers, "/highlighting/blacklist");
persist(this->highlightedUsers, "/highlighting/users");
persist(this->ignoredMessages, "/ignore/phrases");
persist(this->mutedChannels, "/pings/muted");
persist(this->filterRecords, "/filtering/filters");
2020-02-23 23:07:28 +01:00
// tagged users?
persist(this->moderationActions, "/moderation/actions");
}
bool ConcurrentSettings::isHighlightedUser(const QString &username)
{
2020-08-22 11:45:18 +02:00
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;
}
2020-02-23 23:07:28 +01:00
bool ConcurrentSettings::isMutedChannel(const QString &channelName)
{
2020-08-22 11:45:18 +02:00
auto items = this->mutedChannels.readOnly();
for (const auto &channel : *items)
2020-02-23 23:07:28 +01:00
{
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;
2018-08-02 14:23:27 +02:00
2018-11-25 15:02:48 +01:00
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
2017-04-12 17:46:44 +02:00
}
Settings &Settings::instance()
{
return *instance_;
}
2018-06-28 19:51:07 +02:00
Settings *getSettings()
2018-01-17 14:14:31 +01:00
{
return &Settings::instance();
2018-01-17 14:14:31 +01:00
}
2018-01-23 21:33:49 +01:00
2017-04-14 17:52:22 +02:00
} // namespace chatterino