1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef QTUIMESSAGEPROCESSOR_H_
22 #define QTUIMESSAGEPROCESSOR_H_
26 #include "abstractmessageprocessor.h"
27 #include "expressionmatch.h"
29 class QtUiMessageProcessor : public AbstractMessageProcessor
39 QtUiMessageProcessor(QObject *parent);
41 inline bool isProcessing() const { return _processing; }
42 inline Mode processMode() const { return _processMode; }
47 void process(Message &msg);
48 void process(QList<Message> &msgs);
51 void processNextMessage();
52 void nicksCaseSensitiveChanged(const QVariant &variant);
53 void highlightListChanged(const QVariant &variant);
54 void highlightNickChanged(const QVariant &variant);
58 * Individual highlight rule (legacy client-side version)
60 class LegacyHighlightRule
64 * Construct an empty highlight rule
66 LegacyHighlightRule() {}
69 * Construct a highlight rule with the given parameters
71 * @param contents String representing a message contents expression to match
72 * @param isRegEx True if regular expression, otherwise false
73 * @param isCaseSensitive True if case sensitive, otherwise false
74 * @param isEnabled True if enabled, otherwise false
75 * @param chanName String representing a channel name expression to match
77 LegacyHighlightRule(QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
79 : _contents(contents), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
80 _isEnabled(isEnabled), _chanName(chanName)
83 // Cache expression matches on construction
85 // This provides immediate feedback on errors when loading the rule. If profiling shows
86 // this as a performance bottleneck, this can be removed in deference to caching on
89 // Inversely, if needed for validity checks, caching can be done on every update below
90 // instead of on first use.
91 determineExpressions();
95 * Gets the message contents this rule matches
97 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
99 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
100 * Duplicate entries are not allowed.
102 * @return String representing a phrase or expression to match
104 inline QString contents() const {
108 * Sets the message contents this rule matches
110 * @param contents String representing a phrase or expression to match
112 inline void setContents(const QString &contents) {
113 _contents = contents;
114 _cacheInvalid = true;
118 * Gets if this is a regular expression rule
120 * @return True if regular expression, otherwise false
122 inline bool isRegEx() const {
126 * Sets if this rule is a regular expression rule
128 * @param isRegEx True if regular expression, otherwise false
130 inline void setIsRegEx(bool isRegEx) {
132 _cacheInvalid = true;
136 * Gets if this rule is case sensitive
138 * @return True if case sensitive, otherwise false
140 inline bool isCaseSensitive() const {
141 return _isCaseSensitive;
144 * Sets if this rule is case sensitive
146 * @param isCaseSensitive True if case sensitive, otherwise false
148 inline void setIsCaseSensitive(bool isCaseSensitive) {
149 _isCaseSensitive = isCaseSensitive;
150 _cacheInvalid = true;
154 * Gets if this rule is enabled and active
156 * @return True if enabled, otherwise false
158 inline bool isEnabled() const {
162 * Sets if this rule is enabled and active
164 * @param isEnabled True if enabled, otherwise false
166 inline void setIsEnabled(bool isEnabled) {
167 _isEnabled = isEnabled;
171 * Gets the channel name this rule matches
173 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
175 * @return String representing a phrase or expression to match
177 inline QString chanName() const {
181 * Sets the channel name this rule matches
183 * @param chanName String representing a phrase or expression to match
185 inline void setChanName(const QString &chanName) {
186 _chanName = chanName;
187 _cacheInvalid = true;
191 * Gets the expression matcher for the message contents, caching if needed
193 * @return Expression matcher to compare with message contents
195 inline ExpressionMatch contentsMatcher() const {
197 determineExpressions();
199 return _contentsMatch;
203 * Gets the expression matcher for the channel name, caching if needed
205 * @return Expression matcher to compare with channel name
207 inline ExpressionMatch chanNameMatcher() const {
209 determineExpressions();
211 return _chanNameMatch;
214 bool operator!=(const LegacyHighlightRule &other) const;
218 * Update internal cache of expression matching if needed
220 void determineExpressions() const;
222 QString _contents = {};
223 bool _isRegEx = false;
224 bool _isCaseSensitive = false;
225 bool _isEnabled = true;
226 QString _chanName = {};
228 // These represent internal cache and should be safe to mutate in 'const' functions
229 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
230 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
231 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
232 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
235 using LegacyHighlightRuleList = QList<LegacyHighlightRule>;
237 void checkForHighlight(Message &msg);
238 void startProcessing();
240 using HighlightNickType = NotificationSettings::HighlightNickType;
243 * Update internal cache of expression matching if needed
245 void determineNickExpressions(const QString ¤tNick,
246 const QStringList identityNicks) const;
249 * Check if nickname matching cache is invalid
251 * @param identityNicks
254 bool cacheNickInvalid(const QString ¤tNick, const QStringList identityNicks) const {
255 if (_cacheNickConfigInvalid) return true;
256 if (_cachedNickCurrent != currentNick) return true;
257 if (_cachedIdentityNicks != identityNicks) return true;
260 LegacyHighlightRuleList _highlightRuleList;
261 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
262 bool _nicksCaseSensitive = false;
264 // These represent internal cache and should be safe to mutate in 'const' functions
265 mutable bool _cacheNickConfigInvalid = true; ///< If true, nick match cache needs redone
266 mutable QString _cachedNickCurrent = {}; ///< Last cached current nick
267 mutable QStringList _cachedIdentityNicks = {}; ///< Last cached identity nicks
268 mutable ExpressionMatch _cachedNickMatcher = {}; ///< Expression match cache for nicks
270 QList<QList<Message> > _processQueue;
271 QList<Message> _currentBatch;
272 QTimer _processTimer;