Chatterino
IgnorePhrase.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Application.hpp"
6 
9 
10 #include <QRegularExpression>
11 #include <QString>
12 #include <pajlada/serialize.hpp>
13 
14 #include <memory>
15 
16 namespace chatterino {
17 
19 {
20 public:
21  bool operator==(const IgnorePhrase &other) const
22  {
23  return std::tie(this->pattern_, this->isRegex_, this->isBlock_,
24  this->replace_, this->isCaseSensitive_) ==
25  std::tie(other.pattern_, other.isRegex_, other.isBlock_,
26  other.replace_, other.isCaseSensitive_);
27  }
28 
29  IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock,
30  const QString &replace, bool isCaseSensitive)
31  : pattern_(pattern)
32  , isRegex_(isRegex)
33  , regex_(pattern)
34  , isBlock_(isBlock)
35  , replace_(replace)
36  , isCaseSensitive_(isCaseSensitive)
37  {
38  if (this->isCaseSensitive_)
39  {
40  regex_.setPatternOptions(
41  QRegularExpression::UseUnicodePropertiesOption);
42  }
43  else
44  {
45  regex_.setPatternOptions(
46  QRegularExpression::CaseInsensitiveOption |
47  QRegularExpression::UseUnicodePropertiesOption);
48  }
49  }
50 
51  const QString &getPattern() const
52  {
53  return this->pattern_;
54  }
55 
56  bool isRegex() const
57  {
58  return this->isRegex_;
59  }
60 
61  bool isRegexValid() const
62  {
63  return this->regex_.isValid();
64  }
65 
66  bool isMatch(const QString &subject) const
67  {
68  return !this->pattern_.isEmpty() &&
69  (this->isRegex() ? (this->regex_.isValid() &&
70  this->regex_.match(subject).hasMatch())
71  : subject.contains(this->pattern_,
72  this->caseSensitivity()));
73  }
74 
75  const QRegularExpression &getRegex() const
76  {
77  return this->regex_;
78  }
79 
80  bool isBlock() const
81  {
82  return this->isBlock_;
83  }
84 
85  const QString &getReplace() const
86  {
87  return this->replace_;
88  }
89 
90  bool isCaseSensitive() const
91  {
92  return this->isCaseSensitive_;
93  }
94 
95  Qt::CaseSensitivity caseSensitivity() const
96  {
97  return this->isCaseSensitive_ ? Qt::CaseSensitive : Qt::CaseInsensitive;
98  }
99 
100  const std::unordered_map<EmoteName, EmotePtr> &getEmotes() const
101  {
102  return this->emotes_;
103  }
104 
105  bool containsEmote() const
106  {
107  if (!this->emotesChecked_)
108  {
109  const auto &accvec = getApp()->accounts->twitch.accounts;
110  for (const auto &acc : accvec)
111  {
112  const auto &accemotes = *acc->accessEmotes();
113  for (const auto &emote : accemotes.emotes)
114  {
115  if (this->replace_.contains(emote.first.string,
116  Qt::CaseSensitive))
117  {
118  this->emotes_.emplace(emote.first, emote.second);
119  }
120  }
121  }
122  this->emotesChecked_ = true;
123  }
124  return !this->emotes_.empty();
125  }
126 
127 private:
128  QString pattern_;
129  bool isRegex_;
130  QRegularExpression regex_;
131  bool isBlock_;
132  QString replace_;
133  bool isCaseSensitive_;
134  mutable std::unordered_map<EmoteName, EmotePtr> emotes_;
135  mutable bool emotesChecked_{false};
136 };
137 } // namespace chatterino
138 
139 namespace pajlada {
140 
141 template <>
142 struct Serialize<chatterino::IgnorePhrase> {
143  static rapidjson::Value get(const chatterino::IgnorePhrase &value,
144  rapidjson::Document::AllocatorType &a)
145  {
146  rapidjson::Value ret(rapidjson::kObjectType);
147 
148  chatterino::rj::set(ret, "pattern", value.getPattern(), a);
149  chatterino::rj::set(ret, "regex", value.isRegex(), a);
150  chatterino::rj::set(ret, "isBlock", value.isBlock(), a);
151  chatterino::rj::set(ret, "replaceWith", value.getReplace(), a);
152  chatterino::rj::set(ret, "caseSensitive", value.isCaseSensitive(), a);
153 
154  return ret;
155  }
156 };
157 
158 template <>
159 struct Deserialize<chatterino::IgnorePhrase> {
160  static chatterino::IgnorePhrase get(const rapidjson::Value &value,
161  bool *error = nullptr)
162  {
163  if (!value.IsObject())
164  {
165  PAJLADA_REPORT_ERROR(error)
167  QString(), false, false,
168  ::chatterino::getSettings()->ignoredPhraseReplace.getValue(),
169  true);
170  }
171 
172  QString _pattern;
173  bool _isRegex = false;
174  bool _isBlock = false;
175  QString _replace;
176  bool _caseSens = true;
177 
178  chatterino::rj::getSafe(value, "pattern", _pattern);
179  chatterino::rj::getSafe(value, "regex", _isRegex);
180  chatterino::rj::getSafe(value, "isBlock", _isBlock);
181  chatterino::rj::getSafe(value, "replaceWith", _replace);
182  chatterino::rj::getSafe(value, "caseSensitive", _caseSens);
183 
184  return chatterino::IgnorePhrase(_pattern, _isRegex, _isBlock, _replace,
185  _caseSens);
186  }
187 };
188 
189 } // namespace pajlada
Application * getApp()
Definition: Application.cpp:623
EmotePtr emote
Definition: InputCompletionPopup.cpp:20
bool isBlock() const
Definition: IgnorePhrase.hpp:80
bool isCaseSensitive() const
Definition: IgnorePhrase.hpp:90
const QRegularExpression & getRegex() const
Definition: IgnorePhrase.hpp:75
bool containsEmote() const
Definition: IgnorePhrase.hpp:105
Definition: Application.cpp:48
const QString & getReplace() const
Definition: IgnorePhrase.hpp:85
bool isRegexValid() const
Definition: IgnorePhrase.hpp:61
Qt::CaseSensitivity caseSensitivity() const
Definition: IgnorePhrase.hpp:95
Definition: IgnorePhrase.hpp:18
TwitchAccountManager twitch
Definition: AccountController.hpp:27
void set(rapidjson::Value &obj, const char *key, const Type &value, rapidjson::Document::AllocatorType &a)
Definition: RapidjsonHelpers.hpp:22
const std::unordered_map< EmoteName, EmotePtr > & getEmotes() const
Definition: IgnorePhrase.hpp:100
Definition: Command.hpp:25
bool isMatch(const QString &subject) const
Definition: IgnorePhrase.hpp:66
bool getSafe(const rapidjson::Value &obj, const char *key, Type &out)
Definition: RapidjsonHelpers.hpp:73
Settings * getSettings()
Definition: BaseSettings.cpp:110
IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock, const QString &replace, bool isCaseSensitive)
Definition: IgnorePhrase.hpp:29
bool isRegex() const
Definition: IgnorePhrase.hpp:56
bool operator==(const IgnorePhrase &other) const
Definition: IgnorePhrase.hpp:21
AccountController *const accounts
Definition: Application.hpp:83
SignalVector< std::shared_ptr< TwitchAccount > > accounts
Definition: TwitchAccountManager.hpp:57
const QString & getPattern() const
Definition: IgnorePhrase.hpp:51