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