cmake: Autogenerate most of the .qrc resource files
[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     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
41     };
42     // NOTE: Keep this in sync with HighlightRuleManager::HighlightNickType and
43     // NotificationSettings::HighlightNickType!
44
45     /**
46      * Construct an empty NicknameMatcher
47      */
48     NickHighlightMatcher() {}
49
50     /**
51      * Construct a configured NicknameMatcher
52      *
53      * @param highlightMode    Nickname highlighting mode
54      * @param isCaseSensitive  If true, nick matching is case-sensitive, otherwise case-insensitive
55      */
56     NickHighlightMatcher(HighlightNickType highlightMode, bool isCaseSensitive)
57         : _highlightMode(highlightMode),
58           _isCaseSensitive(isCaseSensitive) {}
59
60     /**
61      * Gets the nickname highlighting policy
62      *
63      * @return HighlightNickType for the given network
64      */
65     inline HighlightNickType highlightMode() const { return _highlightMode; }
66
67     /**
68      * Sets the nickname highlighting policy
69      *
70      * @param highlightMode Nickname highlighting mode
71      */
72     void setHighlightMode(HighlightNickType highlightMode) {
73         if (_highlightMode != highlightMode) {
74             _highlightMode = highlightMode;
75             invalidateNickCache();
76         }
77     }
78
79     /**
80      * Gets the nickname case-sensitivity policy
81      *
82      * @return True if nickname highlights are case-sensitive, otherwise false
83      */
84     inline bool isCaseSensitive() const { return _isCaseSensitive; }
85
86     /**
87      * Sets the nickname case-sensitivity policy
88      *
89      * @param isCaseSensitive If true, nick matching is case-sensitive, otherwise case-insensitive
90      */
91     void setCaseSensitive(bool isCaseSensitive) {
92         if (_isCaseSensitive != isCaseSensitive) {
93             _isCaseSensitive = isCaseSensitive;
94             invalidateNickCache();
95         }
96     }
97
98     /**
99      * Checks if the given string matches the specified network's nickname matcher
100      *
101      * Updates cache when called if needed.
102      *
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
108      */
109     bool match(const QString &string, const NetworkId &netId, const QString &currentNick,
110                const QStringList &identityNicks) const;
111
112 public slots:
113     /**
114      * Removes the specified network ID from the cache
115      *
116      * @param netId Network ID of source network
117      */
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;
122         }
123     }
124
125 private:
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
131     };
132
133     /**
134      * Update internal cache of nickname matching if needed
135      *
136      * @param netId          Network ID of source network
137      * @param currentNick    Current nickname
138      * @param identityNicks  All nicknames configured for the current identity
139      */
140     void determineExpressions(const NetworkId &netId, const QString &currentNick,
141                               const QStringList &identityNicks) const;
142
143     /**
144      * Invalidate all nickname match caches
145      *
146      * Use this after changing global configuration.
147      */
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)";
153         }
154     }
155
156     // Global nickname configuration
157     /// Nickname highlighting mode
158     HighlightNickType _highlightMode = HighlightNickType::CurrentNick;
159     bool _isCaseSensitive = false;  ///< If true, match nicknames with exact case
160
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
163
164 };