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_
27 #include "abstractmessageprocessor.h"
28 #include "expressionmatch.h"
29 #include "nickhighlightmatcher.h"
31 class QtUiMessageProcessor : public AbstractMessageProcessor
41 QtUiMessageProcessor(QObject *parent);
43 inline bool isProcessing() const { return _processing; }
44 inline Mode processMode() const { return _processMode; }
46 void reset() override;
49 void process(Message &msg) override;
50 void process(QList<Message> &msgs) override;
53 * Network removed from system
55 * Handles cleaning up cache from stale networks.
57 * @param id Network ID of removed network
59 void networkRemoved(NetworkId id) override;
62 void processNextMessage();
63 void nicksCaseSensitiveChanged(const QVariant &variant);
64 void highlightListChanged(const QVariant &variant);
65 void highlightNickChanged(const QVariant &variant);
69 * Individual highlight rule (legacy client-side version)
71 class LegacyHighlightRule
75 * Construct an empty highlight rule
77 LegacyHighlightRule() = default;
80 * Construct a highlight rule with the given parameters
82 * @param contents String representing a message contents expression to match
83 * @param isRegEx True if regular expression, otherwise false
84 * @param isCaseSensitive True if case sensitive, otherwise false
85 * @param isEnabled True if enabled, otherwise false
86 * @param chanName String representing a channel name expression to match
88 LegacyHighlightRule(QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
90 : _contents(std::move(contents)), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
91 _isEnabled(isEnabled), _chanName(std::move(chanName))
94 // Cache expression matches on construction
96 // This provides immediate feedback on errors when loading the rule. If profiling shows
97 // this as a performance bottleneck, this can be removed in deference to caching on
100 // Inversely, if needed for validity checks, caching can be done on every update below
101 // instead of on first use.
102 determineExpressions();
106 * Gets the message contents this rule matches
108 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
110 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
111 * Duplicate entries are not allowed.
113 * @return String representing a phrase or expression to match
115 inline QString contents() const {
119 * Sets the message contents this rule matches
121 * @param contents String representing a phrase or expression to match
123 inline void setContents(const QString &contents) {
124 _contents = contents;
125 _cacheInvalid = true;
129 * Gets if this is a regular expression rule
131 * @return True if regular expression, otherwise false
133 inline bool isRegEx() const {
137 * Sets if this rule is a regular expression rule
139 * @param isRegEx True if regular expression, otherwise false
141 inline void setIsRegEx(bool isRegEx) {
143 _cacheInvalid = true;
147 * Gets if this rule is case sensitive
149 * @return True if case sensitive, otherwise false
151 inline bool isCaseSensitive() const {
152 return _isCaseSensitive;
155 * Sets if this rule is case sensitive
157 * @param isCaseSensitive True if case sensitive, otherwise false
159 inline void setIsCaseSensitive(bool isCaseSensitive) {
160 _isCaseSensitive = isCaseSensitive;
161 _cacheInvalid = true;
165 * Gets if this rule is enabled and active
167 * @return True if enabled, otherwise false
169 inline bool isEnabled() const {
173 * Sets if this rule is enabled and active
175 * @param isEnabled True if enabled, otherwise false
177 inline void setIsEnabled(bool isEnabled) {
178 _isEnabled = isEnabled;
182 * Gets the channel name this rule matches
184 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
186 * @return String representing a phrase or expression to match
188 inline QString chanName() const {
192 * Sets the channel name this rule matches
194 * @param chanName String representing a phrase or expression to match
196 inline void setChanName(const QString &chanName) {
197 _chanName = chanName;
198 _cacheInvalid = true;
202 * Gets the expression matcher for the message contents, caching if needed
204 * @return Expression matcher to compare with message contents
206 inline ExpressionMatch contentsMatcher() const {
208 determineExpressions();
210 return _contentsMatch;
214 * Gets the expression matcher for the channel name, caching if needed
216 * @return Expression matcher to compare with channel name
218 inline ExpressionMatch chanNameMatcher() const {
220 determineExpressions();
222 return _chanNameMatch;
225 bool operator!=(const LegacyHighlightRule &other) const;
229 * Update internal cache of expression matching if needed
231 void determineExpressions() const;
233 QString _contents = {};
234 bool _isRegEx = false;
235 bool _isCaseSensitive = false;
236 bool _isEnabled = true;
237 QString _chanName = {};
239 // These represent internal cache and should be safe to mutate in 'const' functions
240 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
241 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
242 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
243 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
246 using LegacyHighlightRuleList = QList<LegacyHighlightRule>;
248 void checkForHighlight(Message &msg);
249 void startProcessing();
251 using HighlightNickType = NotificationSettings::HighlightNickType;
253 LegacyHighlightRuleList _highlightRuleList; ///< Custom highlight rule list
254 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
256 /// Nickname highlighting mode
257 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
258 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case
260 QList<QList<Message> > _processQueue;
261 QList<Message> _currentBatch;
262 QTimer _processTimer;