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