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