mirror-chatterino2/src/controllers/highlights/HighlightBlacklistUser.hpp

102 lines
2.4 KiB
C++
Raw Normal View History

#pragma once
#include "common/SerializeCustom.hpp"
#include "util/RapidjsonHelpers.hpp"
#include <QRegularExpression>
#include <QString>
#include <pajlada/settings/serialize.hpp>
#include <memory>
namespace chatterino {
class HighlightBlacklistUser
{
QString pattern_;
bool isRegex_;
QRegularExpression regex_;
public:
bool operator==(const HighlightBlacklistUser &other) const
{
return std::tie(this->pattern_, this->isRegex_) == std::tie(other.pattern_, other.isRegex_);
}
HighlightBlacklistUser(const QString &pattern, bool isRegex = false)
: pattern_(pattern)
, isRegex_(isRegex)
, regex_(isRegex ? pattern : "", QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption)
{
}
const QString &getPattern() const
{
return this->pattern_;
}
bool isRegex() const
{
return this->isRegex_;
}
bool isValidRegex() const
{
return this->isRegex() && this->regex_.isValid();
}
bool isMatch(const QString &subject) const
{
if (this->isRegex()) {
if (this->isValidRegex()) {
return this->regex_.match(subject).hasMatch();
}
return false;
}
return subject.toLower() == this->pattern_.toLower();
}
};
} // namespace chatterino
namespace pajlada {
namespace Settings {
template <>
struct Serialize<chatterino::HighlightBlacklistUser> {
static rapidjson::Value get(const chatterino::HighlightBlacklistUser &value,
rapidjson::Document::AllocatorType &a)
{
rapidjson::Value ret(rapidjson::kObjectType);
AddMember(ret, "pattern", value.getPattern(), a);
AddMember(ret, "regex", value.isRegex(), a);
return ret;
}
};
template <>
struct Deserialize<chatterino::HighlightBlacklistUser> {
static chatterino::HighlightBlacklistUser get(const rapidjson::Value &value)
{
QString pattern;
bool isRegex = false;
if (!value.IsObject()) {
return chatterino::HighlightBlacklistUser(pattern, isRegex);
}
chatterino::rj::getSafe(value, "pattern", pattern);
chatterino::rj::getSafe(value, "regex", isRegex);
return chatterino::HighlightBlacklistUser(pattern, isRegex);
}
};
} // namespace Settings
} // namespace pajlada