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"
28 #include "nickhighlightmatcher.h"
30 class QtUiMessageProcessor : public AbstractMessageProcessor
40 QtUiMessageProcessor(QObject *parent);
42 inline bool isProcessing() const { return _processing; }
43 inline Mode processMode() const { return _processMode; }
48 void process(Message &msg);
49 void process(QList<Message> &msgs);
52 * Network removed from system
54 * Handles cleaning up cache from stale networks.
56 * @param id Network ID of removed network
58 inline void networkRemoved(NetworkId id) {
59 // Clean up nickname matching cache
60 _nickMatcher.removeNetwork(id);
64 void processNextMessage();
65 void nicksCaseSensitiveChanged(const QVariant &variant);
66 void highlightListChanged(const QVariant &variant);
67 void highlightNickChanged(const QVariant &variant);
71 * Individual highlight rule (legacy client-side version)
73 class LegacyHighlightRule
77 * Construct an empty highlight rule
79 LegacyHighlightRule() {}
82 * Construct a highlight rule with the given parameters
84 * @param contents String representing a message contents expression to match
85 * @param isRegEx True if regular expression, otherwise false
86 * @param isCaseSensitive True if case sensitive, otherwise false
87 * @param isEnabled True if enabled, otherwise false
88 * @param chanName String representing a channel name expression to match
90 LegacyHighlightRule(QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
92 : _contents(contents), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
93 _isEnabled(isEnabled), _chanName(chanName)
96 // Cache expression matches on construction
98 // This provides immediate feedback on errors when loading the rule. If profiling shows
99 // this as a performance bottleneck, this can be removed in deference to caching on
102 // Inversely, if needed for validity checks, caching can be done on every update below
103 // instead of on first use.
104 determineExpressions();
108 * Gets the message contents this rule matches
110 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
112 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
113 * Duplicate entries are not allowed.
115 * @return String representing a phrase or expression to match
117 inline QString contents() const {
121 * Sets the message contents this rule matches
123 * @param contents String representing a phrase or expression to match
125 inline void setContents(const QString &contents) {
126 _contents = contents;
127 _cacheInvalid = true;
131 * Gets if this is a regular expression rule
133 * @return True if regular expression, otherwise false
135 inline bool isRegEx() const {
139 * Sets if this rule is a regular expression rule
141 * @param isRegEx True if regular expression, otherwise false
143 inline void setIsRegEx(bool isRegEx) {
145 _cacheInvalid = true;
149 * Gets if this rule is case sensitive
151 * @return True if case sensitive, otherwise false
153 inline bool isCaseSensitive() const {
154 return _isCaseSensitive;
157 * Sets if this rule is case sensitive
159 * @param isCaseSensitive True if case sensitive, otherwise false
161 inline void setIsCaseSensitive(bool isCaseSensitive) {
162 _isCaseSensitive = isCaseSensitive;
163 _cacheInvalid = true;
167 * Gets if this rule is enabled and active
169 * @return True if enabled, otherwise false
171 inline bool isEnabled() const {
175 * Sets if this rule is enabled and active
177 * @param isEnabled True if enabled, otherwise false
179 inline void setIsEnabled(bool isEnabled) {
180 _isEnabled = isEnabled;
184 * Gets the channel name this rule matches
186 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
188 * @return String representing a phrase or expression to match
190 inline QString chanName() const {
194 * Sets the channel name this rule matches
196 * @param chanName String representing a phrase or expression to match
198 inline void setChanName(const QString &chanName) {
199 _chanName = chanName;
200 _cacheInvalid = true;
204 * Gets the expression matcher for the message contents, caching if needed
206 * @return Expression matcher to compare with message contents
208 inline ExpressionMatch contentsMatcher() const {
210 determineExpressions();
212 return _contentsMatch;
216 * Gets the expression matcher for the channel name, caching if needed
218 * @return Expression matcher to compare with channel name
220 inline ExpressionMatch chanNameMatcher() const {
222 determineExpressions();
224 return _chanNameMatch;
227 bool operator!=(const LegacyHighlightRule &other) const;
231 * Update internal cache of expression matching if needed
233 void determineExpressions() const;
235 QString _contents = {};
236 bool _isRegEx = false;
237 bool _isCaseSensitive = false;
238 bool _isEnabled = true;
239 QString _chanName = {};
241 // These represent internal cache and should be safe to mutate in 'const' functions
242 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
243 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
244 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
245 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
248 using LegacyHighlightRuleList = QList<LegacyHighlightRule>;
250 void checkForHighlight(Message &msg);
251 void startProcessing();
253 using HighlightNickType = NotificationSettings::HighlightNickType;
255 LegacyHighlightRuleList _highlightRuleList; ///< Custom highlight rule list
256 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
258 /// Nickname highlighting mode
259 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
260 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case
262 QList<QList<Message> > _processQueue;
263 QList<Message> _currentBatch;
264 QTimer _processTimer;