common: Port remote nicks to NickHighlightMatcher
[quassel.git] / src / common / nickhighlightmatcher.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include <QHash>
24 #include <QString>
25 #include <QStringList>
26
27 #include "expressionmatch.h"
28 #include "types.h"
29
30 /**
31  * Nickname matcher with automatic caching for performance
32  */
33 class NickHighlightMatcher
34 {
35 public:
36     /// Nickname highlighting mode
37 #if QT_VERSION >= 0x050000
38     enum class HighlightNickType {
39 #else
40     enum HighlightNickType {
41 #endif
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
45     };
46     // NOTE: Keep this in sync with HighlightRuleManager::HighlightNickType and
47     // NotificationSettings::HighlightNickType!
48
49     /**
50      * Construct an empty NicknameMatcher
51      */
52     NickHighlightMatcher() {}
53
54     /**
55      * Construct a configured NicknameMatcher
56      *
57      * @param highlightMode    Nickname highlighting mode
58      * @param isCaseSensitive  If true, nick matching is case-sensitive, otherwise case-insensitive
59      */
60     NickHighlightMatcher(HighlightNickType highlightMode, bool isCaseSensitive)
61         : _highlightMode(highlightMode),
62           _isCaseSensitive(isCaseSensitive) {}
63
64     /**
65      * Gets the nickname highlighting policy
66      *
67      * @return HighlightNickType for the given network
68      */
69     inline HighlightNickType highlightMode() const { return _highlightMode; }
70
71     /**
72      * Sets the nickname highlighting policy
73      *
74      * @param highlightMode Nickname highlighting mode
75      */
76     void setHighlightMode(HighlightNickType highlightMode) {
77         if (_highlightMode != highlightMode) {
78             _highlightMode = highlightMode;
79             invalidateNickCache();
80         }
81     }
82
83     /**
84      * Gets the nickname case-sensitivity policy
85      *
86      * @return True if nickname highlights are case-sensitive, otherwise false
87      */
88     inline bool isCaseSensitive() const { return _isCaseSensitive; }
89
90     /**
91      * Sets the nickname case-sensitivity policy
92      *
93      * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
94      */
95     void setCaseSensitive(bool isCaseSensitive) {
96         if (_isCaseSensitive != isCaseSensitive) {
97             _isCaseSensitive = isCaseSensitive;
98             invalidateNickCache();
99         }
100     }
101
102     /**
103      * Checks if the given string matches the specified network's nickname matcher
104      *
105      * Updates cache when called if needed.
106      *
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
112      */
113     bool match(const QString &string, const NetworkId &netId, const QString &currentNick,
114                const QStringList &identityNicks) const;
115
116 public slots:
117     /**
118      * Removes the specified network ID from the cache
119      *
120      * @param netId Network ID of source network
121      */
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;
126         }
127     }
128
129 private:
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
135     };
136
137     /**
138      * Update internal cache of nickname matching if needed
139      *
140      * @param netId          Network ID of source network
141      * @param currentNick    Current nickname
142      * @param identityNicks  All nicknames configured for the current identity
143      */
144     void determineExpressions(const NetworkId &netId, const QString &currentNick,
145                               const QStringList &identityNicks) const;
146
147     /**
148      * Invalidate all nickname match caches
149      *
150      * Use this after changing global configuration.
151      */
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)";
157         }
158     }
159
160     // Global nickname configuration
161     /// Nickname highlighting mode
162     HighlightNickType _highlightMode = HighlightNickType::CurrentNick;
163     bool _isCaseSensitive = false;  ///< If true, match nicknames with exact case
164
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
167
168 };