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 #if QT_VERSION >= 0x050000
38 enum class HighlightNickType {
40 enum HighlightNickType {
42 NoNick = 0x00, ///< Don't match any nickname
43 CurrentNick = 0x01, ///< Match the current nickname
44 AllNicks = 0x02 ///< Match all configured nicknames in the chosen identity
46 // NOTE: Keep this in sync with HighlightRuleManager::HighlightNickType and
47 // NotificationSettings::HighlightNickType!
50 * Construct an empty NicknameMatcher
52 NickHighlightMatcher() {}
55 * Construct a configured NicknameMatcher
57 * @param highlightMode Nickname highlighting mode
58 * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
60 NickHighlightMatcher(HighlightNickType highlightMode, bool isCaseSensitive)
61 : _highlightMode(highlightMode),
62 _isCaseSensitive(isCaseSensitive) {}
65 * Gets the nickname highlighting policy
67 * @return HighlightNickType for the given network
69 inline HighlightNickType highlightMode() const { return _highlightMode; }
72 * Sets the nickname highlighting policy
74 * @param highlightMode Nickname highlighting mode
76 void setHighlightMode(HighlightNickType highlightMode) {
77 if (_highlightMode != highlightMode) {
78 _highlightMode = highlightMode;
79 invalidateNickCache();
84 * Gets the nickname case-sensitivity policy
86 * @return True if nickname highlights are case-sensitive, otherwise false
88 inline bool isCaseSensitive() const { return _isCaseSensitive; }
91 * Sets the nickname case-sensitivity policy
93 * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
95 void setCaseSensitive(bool isCaseSensitive) {
96 if (_isCaseSensitive != isCaseSensitive) {
97 _isCaseSensitive = isCaseSensitive;
98 invalidateNickCache();
103 * Checks if the given string matches the specified network's nickname matcher
105 * Updates cache when called if needed.
107 * @param string String to match against
108 * @param netId Network ID of source network
109 * @param currentNick Current nickname
110 * @param identityNicks All nicknames configured for the current identity
111 * @return True if match found, otherwise false
113 bool match(const QString &string, const NetworkId &netId, const QString ¤tNick,
114 const QStringList &identityNicks) const;
118 * Removes the specified network ID from the cache
120 * @param netId Network ID of source network
122 void removeNetwork(const NetworkId &netId) {
123 // Remove the network from the cache list
124 if (_nickMatchCache.remove(netId) > 0) {
125 qDebug() << "Cleared nickname matching cache for removed network ID" << netId;
130 struct NickMatchCache {
131 // These represent internal cache and should be safe to mutate in 'const' functions
132 QString nickCurrent = {}; ///< Last cached current nick
133 QStringList identityNicks = {}; ///< Last cached identity nicks
134 ExpressionMatch matcher = {}; ///< Expression match cache for nicks
138 * Update internal cache of nickname matching if needed
140 * @param netId Network ID of source network
141 * @param currentNick Current nickname
142 * @param identityNicks All nicknames configured for the current identity
144 void determineExpressions(const NetworkId &netId, const QString ¤tNick,
145 const QStringList &identityNicks) const;
148 * Invalidate all nickname match caches
150 * Use this after changing global configuration.
152 inline void invalidateNickCache() {
153 // Mark all as invalid
154 if (_nickMatchCache.size() > 0) {
155 _nickMatchCache.clear();
156 qDebug() << "Cleared all nickname matching cache (settings changed)";
160 // Global nickname configuration
161 /// Nickname highlighting mode
162 HighlightNickType _highlightMode = HighlightNickType::CurrentNick;
163 bool _isCaseSensitive = false; ///< If true, match nicknames with exact case
165 // These represent internal cache and should be safe to mutate in 'const' functions
166 mutable QHash<NetworkId, NickMatchCache> _nickMatchCache; ///< Per-network nick matching cache