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 ***************************************************************************/
25 #include <QStringList>
27 #include "expressionmatch.h"
31 * Nickname matcher with automatic caching for performance
33 class NickHighlightMatcher
36 /// Nickname highlighting mode
37 enum class HighlightNickType {
38 NoNick = 0x00, ///< Don't match any nickname
39 CurrentNick = 0x01, ///< Match the current nickname
40 AllNicks = 0x02 ///< Match all configured nicknames in the chosen identity
42 // NOTE: Keep this in sync with HighlightRuleManager::HighlightNickType and
43 // NotificationSettings::HighlightNickType!
46 * Construct an empty NicknameMatcher
48 NickHighlightMatcher() {}
51 * Construct a configured NicknameMatcher
53 * @param highlightMode Nickname highlighting mode
54 * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
56 NickHighlightMatcher(HighlightNickType highlightMode, bool isCaseSensitive)
57 : _highlightMode(highlightMode),
58 _isCaseSensitive(isCaseSensitive) {}
61 * Gets the nickname highlighting policy
63 * @return HighlightNickType for the given network
65 inline HighlightNickType highlightMode() const { return _highlightMode; }
68 * Sets the nickname highlighting policy
70 * @param highlightMode Nickname highlighting mode
72 void setHighlightMode(HighlightNickType highlightMode) {
73 if (_highlightMode != highlightMode) {
74 _highlightMode = highlightMode;
75 invalidateNickCache();
80 * Gets the nickname case-sensitivity policy
82 * @return True if nickname highlights are case-sensitive, otherwise false
84 inline bool isCaseSensitive() const { return _isCaseSensitive; }
87 * Sets the nickname case-sensitivity policy
89 * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
91 void setCaseSensitive(bool isCaseSensitive) {
92 if (_isCaseSensitive != isCaseSensitive) {
93 _isCaseSensitive = isCaseSensitive;
94 invalidateNickCache();
99 * Checks if the given string matches the specified network's nickname matcher
101 * Updates cache when called if needed.
103 * @param string String to match against
104 * @param netId Network ID of source network
105 * @param currentNick Current nickname
106 * @param identityNicks All nicknames configured for the current identity
107 * @return True if match found, otherwise false
109 bool match(const QString &string, const NetworkId &netId, const QString ¤tNick,
110 const QStringList &identityNicks) const;
114 * Removes the specified network ID from the cache
116 * @param netId Network ID of source network
118 void removeNetwork(const NetworkId &netId) {
119 // Remove the network from the cache list
120 if (_nickMatchCache.remove(netId) > 0) {
121 qDebug() << "Cleared nickname matching cache for removed network ID" << netId;
126 struct NickMatchCache {
127 // These represent internal cache and should be safe to mutate in 'const' functions
128 QString nickCurrent = {}; ///< Last cached current nick
129 QStringList identityNicks = {}; ///< Last cached identity nicks
130 ExpressionMatch matcher = {}; ///< Expression match cache for nicks
134 * Update internal cache of nickname matching if needed
136 * @param netId Network ID of source network
137 * @param currentNick Current nickname
138 * @param identityNicks All nicknames configured for the current identity
140 void determineExpressions(const NetworkId &netId, const QString ¤tNick,
141 const QStringList &identityNicks) const;
144 * Invalidate all nickname match caches
146 * Use this after changing global configuration.
148 inline void invalidateNickCache() {
149 // Mark all as invalid
150 if (_nickMatchCache.size() > 0) {
151 _nickMatchCache.clear();
152 qDebug() << "Cleared all nickname matching cache (settings changed)";
156 // Global nickname configuration
157 /// Nickname highlighting mode
158 HighlightNickType _highlightMode = HighlightNickType::CurrentNick;
159 bool _isCaseSensitive = false; ///< If true, match nicknames with exact case
161 // These represent internal cache and should be safe to mutate in 'const' functions
162 mutable QHash<NetworkId, NickMatchCache> _nickMatchCache; ///< Per-network nick matching cache