1 /***************************************************************************
2 * Copyright (C) 2005-2020 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_
28 #include "abstractmessageprocessor.h"
29 #include "expressionmatch.h"
30 #include "nickhighlightmatcher.h"
32 class QtUiMessageProcessor : public AbstractMessageProcessor
43 QtUiMessageProcessor(QObject* parent);
45 inline bool isProcessing() const { return _processing; }
46 inline Mode processMode() const { return _processMode; }
48 void reset() override;
51 void process(Message& msg) override;
52 void process(QList<Message>& msgs) override;
55 * Network removed from system
57 * Handles cleaning up cache from stale networks.
59 * @param id Network ID of removed network
61 void networkRemoved(NetworkId id) override;
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() = default;
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, QString chanName)
91 : _contents(std::move(contents))
93 , _isCaseSensitive(isCaseSensitive)
94 , _isEnabled(isEnabled)
95 , _chanName(std::move(chanName))
98 // Cache expression matches on construction
100 // This provides immediate feedback on errors when loading the rule. If profiling shows
101 // this as a performance bottleneck, this can be removed in deference to caching on
104 // Inversely, if needed for validity checks, caching can be done on every update below
105 // instead of on first use.
106 determineExpressions();
110 * Gets the message contents this rule matches
112 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
114 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
115 * Duplicate entries are not allowed.
117 * @return String representing a phrase or expression to match
119 inline QString contents() const { return _contents; }
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)
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 { return _isRegEx; }
138 * Sets if this rule is a regular expression rule
140 * @param isRegEx True if regular expression, otherwise false
142 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 { 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)
161 _isCaseSensitive = isCaseSensitive;
162 _cacheInvalid = true;
166 * Gets if this rule is enabled and active
168 * @return True if enabled, otherwise false
170 inline bool isEnabled() const { return _isEnabled; }
172 * Sets if this rule is enabled and active
174 * @param isEnabled True if enabled, otherwise false
176 inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
179 * Gets the channel name this rule matches
181 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
183 * @return String representing a phrase or expression to match
185 inline QString chanName() const { return _chanName; }
187 * Sets the channel name this rule matches
189 * @param chanName String representing a phrase or expression to match
191 inline void setChanName(const QString& chanName)
193 _chanName = chanName;
194 _cacheInvalid = true;
198 * Gets the expression matcher for the message contents, caching if needed
200 * @return Expression matcher to compare with message contents
202 inline ExpressionMatch contentsMatcher() const
205 determineExpressions();
207 return _contentsMatch;
211 * Gets the expression matcher for the channel name, caching if needed
213 * @return Expression matcher to compare with channel name
215 inline ExpressionMatch chanNameMatcher() const
218 determineExpressions();
220 return _chanNameMatch;
223 bool operator!=(const LegacyHighlightRule& other) const;
227 * Update internal cache of expression matching if needed
229 void determineExpressions() const;
231 QString _contents = {};
232 bool _isRegEx = false;
233 bool _isCaseSensitive = false;
234 bool _isEnabled = true;
235 QString _chanName = {};
237 // These represent internal cache and should be safe to mutate in 'const' functions
238 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
239 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
240 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
241 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
244 using LegacyHighlightRuleList = QList<LegacyHighlightRule>;
246 void checkForHighlight(Message& msg);
247 void startProcessing();
249 using HighlightNickType = NotificationSettings::HighlightNickType;
251 LegacyHighlightRuleList _highlightRuleList; ///< Custom highlight rule list
252 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
254 /// Nickname highlighting mode
255 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
256 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case
258 QList<QList<Message>> _processQueue;
259 QList<Message> _currentBatch;
260 QTimer _processTimer;