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