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 inline void networkRemoved(NetworkId id) {
60 // Clean up nickname matching cache
61 _nickMatcher.removeNetwork(id);
65 void processNextMessage();
66 void nicksCaseSensitiveChanged(const QVariant &variant);
67 void highlightListChanged(const QVariant &variant);
68 void highlightNickChanged(const QVariant &variant);
72 * Individual highlight rule (legacy client-side version)
74 class LegacyHighlightRule
78 * Construct an empty highlight rule
80 LegacyHighlightRule() = default;
83 * Construct a highlight rule with the given parameters
85 * @param contents String representing a message contents expression to match
86 * @param isRegEx True if regular expression, otherwise false
87 * @param isCaseSensitive True if case sensitive, otherwise false
88 * @param isEnabled True if enabled, otherwise false
89 * @param chanName String representing a channel name expression to match
91 LegacyHighlightRule(QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
93 : _contents(std::move(contents)), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
94 _isEnabled(isEnabled), _chanName(std::move(chanName))
97 // Cache expression matches on construction
99 // This provides immediate feedback on errors when loading the rule. If profiling shows
100 // this as a performance bottleneck, this can be removed in deference to caching on
103 // Inversely, if needed for validity checks, caching can be done on every update below
104 // instead of on first use.
105 determineExpressions();
109 * Gets the message contents this rule matches
111 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
113 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
114 * Duplicate entries are not allowed.
116 * @return String representing a phrase or expression to match
118 inline QString contents() const {
122 * Sets the message contents this rule matches
124 * @param contents String representing a phrase or expression to match
126 inline void setContents(const QString &contents) {
127 _contents = contents;
128 _cacheInvalid = true;
132 * Gets if this is a regular expression rule
134 * @return True if regular expression, otherwise false
136 inline bool isRegEx() const {
140 * Sets if this rule is a regular expression rule
142 * @param isRegEx True if regular expression, otherwise false
144 inline void setIsRegEx(bool isRegEx) {
146 _cacheInvalid = true;
150 * Gets if this rule is case sensitive
152 * @return True if case sensitive, otherwise false
154 inline bool isCaseSensitive() const {
155 return _isCaseSensitive;
158 * Sets if this rule is case sensitive
160 * @param isCaseSensitive True if case sensitive, otherwise false
162 inline void setIsCaseSensitive(bool isCaseSensitive) {
163 _isCaseSensitive = isCaseSensitive;
164 _cacheInvalid = true;
168 * Gets if this rule is enabled and active
170 * @return True if enabled, otherwise false
172 inline bool isEnabled() const {
176 * Sets if this rule is enabled and active
178 * @param isEnabled True if enabled, otherwise false
180 inline void setIsEnabled(bool isEnabled) {
181 _isEnabled = isEnabled;
185 * Gets the channel name this rule matches
187 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
189 * @return String representing a phrase or expression to match
191 inline QString chanName() const {
195 * Sets the channel name this rule matches
197 * @param chanName String representing a phrase or expression to match
199 inline void setChanName(const QString &chanName) {
200 _chanName = chanName;
201 _cacheInvalid = true;
205 * Gets the expression matcher for the message contents, caching if needed
207 * @return Expression matcher to compare with message contents
209 inline ExpressionMatch contentsMatcher() const {
211 determineExpressions();
213 return _contentsMatch;
217 * Gets the expression matcher for the channel name, caching if needed
219 * @return Expression matcher to compare with channel name
221 inline ExpressionMatch chanNameMatcher() const {
223 determineExpressions();
225 return _chanNameMatch;
228 bool operator!=(const LegacyHighlightRule &other) const;
232 * Update internal cache of expression matching if needed
234 void determineExpressions() const;
236 QString _contents = {};
237 bool _isRegEx = false;
238 bool _isCaseSensitive = false;
239 bool _isEnabled = true;
240 QString _chanName = {};
242 // These represent internal cache and should be safe to mutate in 'const' functions
243 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
244 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
245 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
246 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
249 using LegacyHighlightRuleList = QList<LegacyHighlightRule>;
251 void checkForHighlight(Message &msg);
252 void startProcessing();
254 using HighlightNickType = NotificationSettings::HighlightNickType;
256 LegacyHighlightRuleList _highlightRuleList; ///< Custom highlight rule list
257 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
259 /// Nickname highlighting mode
260 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
261 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case
263 QList<QList<Message> > _processQueue;
264 QList<Message> _currentBatch;
265 QTimer _processTimer;