ignorephrase format

This commit is contained in:
hemirt 2018-09-30 13:39:05 +02:00 committed by pajlada
parent 6ca4f661a7
commit e7f74aa8ca

View file

@ -20,14 +20,14 @@ class IgnorePhrase
public: public:
bool operator==(const IgnorePhrase &other) const bool operator==(const IgnorePhrase &other) const
{ {
return std::tie(this->pattern_, this->isRegex_, this->isBlock_, this->replace_, return std::tie(this->pattern_, this->isRegex_, this->isBlock_,
this->isCaseSensitive_) == std::tie(other.pattern_, other.isRegex_, this->replace_, this->isCaseSensitive_) ==
other.isBlock_, other.replace_, std::tie(other.pattern_, other.isRegex_, other.isBlock_,
other.isCaseSensitive_); other.replace_, other.isCaseSensitive_);
} }
IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock, const QString &replace, IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock,
bool isCaseSensitive) const QString &replace, bool isCaseSensitive)
: pattern_(pattern) : pattern_(pattern)
, isRegex_(isRegex) , isRegex_(isRegex)
, regex_(pattern) , regex_(pattern)
@ -36,10 +36,12 @@ public:
, isCaseSensitive_(isCaseSensitive) , isCaseSensitive_(isCaseSensitive)
{ {
if (this->isCaseSensitive_) { if (this->isCaseSensitive_) {
regex_.setPatternOptions(QRegularExpression::UseUnicodePropertiesOption); regex_.setPatternOptions(
QRegularExpression::UseUnicodePropertiesOption);
} else { } else {
regex_.setPatternOptions(QRegularExpression::CaseInsensitiveOption | regex_.setPatternOptions(
QRegularExpression::UseUnicodePropertiesOption); QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption);
} }
} }
@ -61,8 +63,10 @@ public:
bool isMatch(const QString &subject) const bool isMatch(const QString &subject) const
{ {
return !this->pattern_.isEmpty() && return !this->pattern_.isEmpty() &&
(this->isRegex() ? (this->regex_.isValid() && this->regex_.match(subject).hasMatch()) (this->isRegex() ? (this->regex_.isValid() &&
: subject.contains(this->pattern_, this->caseSensitivity())); this->regex_.match(subject).hasMatch())
: subject.contains(this->pattern_,
this->caseSensitivity()));
} }
const QRegularExpression &getRegex() const const QRegularExpression &getRegex() const
@ -98,11 +102,13 @@ public:
bool containsEmote() const bool containsEmote() const
{ {
if (!this->emotesChecked_) { if (!this->emotesChecked_) {
const auto &accvec = getApp()->accounts->twitch.accounts.getVector(); const auto &accvec =
getApp()->accounts->twitch.accounts.getVector();
for (const auto &acc : accvec) { for (const auto &acc : accvec) {
const auto &accemotes = *acc->accessEmotes(); const auto &accemotes = *acc->accessEmotes();
for (const auto &emote : accemotes.emotes) { for (const auto &emote : accemotes.emotes) {
if (this->replace_.contains(emote.first.string, Qt::CaseSensitive)) { if (this->replace_.contains(emote.first.string,
Qt::CaseSensitive)) {
this->emotes_.emplace(emote.first, emote.second); this->emotes_.emplace(emote.first, emote.second);
} }
} }
@ -127,47 +133,50 @@ private:
namespace pajlada { namespace pajlada {
namespace Settings { namespace Settings {
template <> template <>
struct Serialize<chatterino::IgnorePhrase> { struct Serialize<chatterino::IgnorePhrase> {
static rapidjson::Value get(const chatterino::IgnorePhrase &value, static rapidjson::Value get(const chatterino::IgnorePhrase &value,
rapidjson::Document::AllocatorType &a) rapidjson::Document::AllocatorType &a)
{ {
rapidjson::Value ret(rapidjson::kObjectType); rapidjson::Value ret(rapidjson::kObjectType);
AddMember(ret, "pattern", value.getPattern(), a); AddMember(ret, "pattern", value.getPattern(), a);
AddMember(ret, "regex", value.isRegex(), a); AddMember(ret, "regex", value.isRegex(), a);
AddMember(ret, "isBlock", value.isBlock(), a); AddMember(ret, "isBlock", value.isBlock(), a);
AddMember(ret, "replaceWith", value.getReplace(), a); AddMember(ret, "replaceWith", value.getReplace(), a);
AddMember(ret, "caseSensitive", value.isCaseSensitive(), a); AddMember(ret, "caseSensitive", value.isCaseSensitive(), a);
return ret; return ret;
}
};
template <>
struct Deserialize<chatterino::IgnorePhrase> {
static chatterino::IgnorePhrase get(const rapidjson::Value &value)
{
if (!value.IsObject()) {
return chatterino::IgnorePhrase(
QString(), false, false,
::chatterino::getSettings()->ignoredPhraseReplace.getValue(), true);
} }
};
QString _pattern; template <>
bool _isRegex = false; struct Deserialize<chatterino::IgnorePhrase> {
bool _isBlock = false; static chatterino::IgnorePhrase get(const rapidjson::Value &value)
QString _replace; {
bool _caseSens = true; if (!value.IsObject()) {
return chatterino::IgnorePhrase(
QString(), false, false,
::chatterino::getSettings()
->ignoredPhraseReplace.getValue(),
true);
}
chatterino::rj::getSafe(value, "pattern", _pattern); QString _pattern;
chatterino::rj::getSafe(value, "regex", _isRegex); bool _isRegex = false;
chatterino::rj::getSafe(value, "isBlock", _isBlock); bool _isBlock = false;
chatterino::rj::getSafe(value, "replaceWith", _replace); QString _replace;
chatterino::rj::getSafe(value, "caseSensitive", _caseSens); bool _caseSens = true;
return chatterino::IgnorePhrase(_pattern, _isRegex, _isBlock, _replace, _caseSens); chatterino::rj::getSafe(value, "pattern", _pattern);
} chatterino::rj::getSafe(value, "regex", _isRegex);
}; chatterino::rj::getSafe(value, "isBlock", _isBlock);
chatterino::rj::getSafe(value, "replaceWith", _replace);
chatterino::rj::getSafe(value, "caseSensitive", _caseSens);
return chatterino::IgnorePhrase(_pattern, _isRegex, _isBlock,
_replace, _caseSens);
}
};
} // namespace Settings } // namespace Settings
} // namespace pajlada } // namespace pajlada