Implement inverse highlight rules
[quassel.git] / src / common / highlightrulemanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 #ifndef HIGHLIGHTRULELISTMANAGER_H
22 #define HIGHLIGHTRULELISTMANAGER_H
23
24 #include <QString>
25 #include <QRegExp>
26
27 #include "message.h"
28 #include "syncableobject.h"
29
30 class HighlightRuleManager : public SyncableObject
31 {
32     SYNCABLE_OBJECT
33         Q_OBJECT
34 public:
35     enum HighlightNickType {
36         NoNick = 0x00,
37         CurrentNick = 0x01,
38         AllNicks = 0x02
39     };
40
41     inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
42     HighlightRuleManager &operator=(const HighlightRuleManager &other);
43
44     struct HighlightRule {
45         QString name;
46         bool isRegEx = false;
47         bool isCaseSensitive = false;
48         bool isEnabled = true;
49         bool isInverse = false;
50         QString chanName;
51         HighlightRule() {}
52         HighlightRule(const QString &name_, bool isRegEx_, bool isCaseSensitive_,
53                        bool isEnabled_, bool isInverse_, const QString &chanName_)
54             : name(name_), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_), isEnabled(isEnabled_),
55               isInverse(isInverse_), chanName(chanName_) {
56         }
57         bool operator!=(const HighlightRule &other)
58         {
59             return (name != other.name ||
60                     isRegEx != other.isRegEx ||
61                     isCaseSensitive != other.isCaseSensitive ||
62                     isEnabled != other.isEnabled ||
63                     isInverse != other.isInverse ||
64                     chanName != other.chanName);
65         }
66     };
67     typedef QList<HighlightRule> HighlightRuleList;
68
69     int indexOf(const QString &rule) const;
70     inline bool contains(const QString &rule) const { return indexOf(rule) != -1; }
71     inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
72     inline int count() const { return _highlightRuleList.count(); }
73     inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
74     inline void clear() { _highlightRuleList.clear(); }
75     inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
76     inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
77     inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
78
79     inline HighlightNickType highlightNick() { return _highlightNick; }
80     inline bool  nicksCaseSensitive() { return _nicksCaseSensitive; }
81
82     //! Check if a message matches the HighlightRule
83     /** This method checks if a message matches the users highlight rules.
84       * \param msg The Message that should be checked
85       */
86     bool match(const Message &msg, const QString &currentNick, const QStringList &identityNicks);
87
88 public slots:
89     virtual QVariantMap initHighlightRuleList() const;
90     virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
91
92     //! Request removal of an ignore rule based on the rule itself.
93     /** Use this method if you want to remove a single ignore rule
94       * and get that synced with the core immediately.
95       * \param highlightRule A valid ignore rule
96       */
97     virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
98     virtual void removeHighlightRule(const QString &highlightRule);
99
100     //! Request toggling of "isEnabled" flag of a given ignore rule.
101     /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
102       * and get that synced with the core immediately.
103       * \param highlightRule A valid ignore rule
104       */
105     virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
106     virtual void toggleHighlightRule(const QString &highlightRule);
107
108     //! Request an HighlightRule to be added to the ignore list
109     /** Items added to the list with this method, get immediately synced with the core
110       * \param name The rule
111       * \param isRegEx If the rule should be interpreted as a nickname, or a regex
112       * \param isCaseSensitive If the rule should be interpreted as case-sensitive
113       * \param isEnabled If the rule is active
114       * @param chanName The channel in which the rule should apply
115       */
116     virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
117                                                 bool isInverse, const QString &chanName)
118     {
119         REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(chanName))
120     }
121
122
123     virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive,
124                                   bool isEnabled, bool isInverse, const QString &chanName);
125
126     virtual inline void requestSetHighlightNick(HighlightNickType highlightNick)
127     {
128         REQUEST(ARG(highlightNick))
129     }
130     inline void setHighlightNick(HighlightNickType highlightNick) { _highlightNick = highlightNick; }
131
132     virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
133     {
134         REQUEST(ARG(nicksCaseSensitive))
135     }
136     inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
137
138 protected:
139     void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
140
141     bool _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString &currentNick, const QStringList identityNicks);
142
143 signals:
144     void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString chanName);
145
146 private:
147     HighlightRuleList _highlightRuleList;
148     HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
149     bool _nicksCaseSensitive = false;
150 };
151
152
153 #endif // HIGHLIGHTRULELISTMANAGER_H