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