b1158d9c15f25bef9a3fb9b5bbabdc6c0a5e1959
[quassel.git] / src / common / ignorelistmanager.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 IGNORELISTMANAGER_H
22 #define IGNORELISTMANAGER_H
23
24 #include <QString>
25 #include <QRegExp>
26
27 #include "message.h"
28 #include "syncableobject.h"
29
30 class IgnoreListManager : public SyncableObject
31 {
32     SYNCABLE_OBJECT
33         Q_OBJECT
34 public:
35     inline IgnoreListManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); }
36     IgnoreListManager &operator=(const IgnoreListManager &other);
37
38     enum IgnoreType {
39         SenderIgnore,
40         MessageIgnore,
41         CtcpIgnore
42     };
43
44     enum StrictnessType {
45         UnmatchedStrictness = 0,
46         SoftStrictness = 1,
47         HardStrictness = 2
48     };
49
50     enum ScopeType {
51         GlobalScope,
52         NetworkScope,
53         ChannelScope,
54     };
55
56     struct IgnoreListItem {
57         IgnoreType type;
58         QString ignoreRule;
59         bool isRegEx;
60         StrictnessType strictness;
61         ScopeType scope;
62         QString scopeRule;
63         bool isActive;
64         QRegExp regEx;
65         IgnoreListItem() {}
66         IgnoreListItem(IgnoreType type_, const QString &ignoreRule_, bool isRegEx_, StrictnessType strictness_,
67             ScopeType scope_, const QString &scopeRule_, bool isActive_)
68             : type(type_), ignoreRule(ignoreRule_), isRegEx(isRegEx_), strictness(strictness_), scope(scope_), scopeRule(scopeRule_), isActive(isActive_), regEx(ignoreRule_) {
69             regEx.setCaseSensitivity(Qt::CaseInsensitive);
70             if (!isRegEx_) {
71                 regEx.setPatternSyntax(QRegExp::Wildcard);
72             }
73         }
74         bool operator!=(const IgnoreListItem &other)
75         {
76             return (type != other.type ||
77                     ignoreRule != other.ignoreRule ||
78                     isRegEx != other.isRegEx ||
79                     strictness != other.strictness ||
80                     scope != other.scope ||
81                     scopeRule != other.scopeRule ||
82                     isActive != other.isActive);
83         }
84     };
85     typedef QList<IgnoreListItem> IgnoreList;
86
87     int indexOf(const QString &ignore) const;
88     inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
89     inline bool isEmpty() const { return _ignoreList.isEmpty(); }
90     inline int count() const { return _ignoreList.count(); }
91     inline void removeAt(int index) { _ignoreList.removeAt(index); }
92     inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
93     inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
94     inline const IgnoreList &ignoreList() const { return _ignoreList; }
95
96     //! Check if a message matches the IgnoreRule
97     /** This method checks if a message matches the users ignorelist.
98       * \param msg The Message that should be checked
99       * \param network The networkname the message belongs to
100       * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
101       */
102     inline StrictnessType match(const Message &msg, const QString &network = QString()) { return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName()); }
103
104     bool ctcpMatch(const QString sender, const QString &network, const QString &type = QString());
105
106 //  virtual void addIgnoreListItem(const IgnoreListItem &item);
107
108 public slots:
109     virtual QVariantMap initIgnoreList() const;
110     virtual void initSetIgnoreList(const QVariantMap &ignoreList);
111
112     //! Request removal of an ignore rule based on the rule itself.
113     /** Use this method if you want to remove a single ignore rule
114       * and get that synced with the core immediately.
115       * \param ignoreRule A valid ignore rule
116       */
117     virtual inline void requestRemoveIgnoreListItem(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
118     virtual void removeIgnoreListItem(const QString &ignoreRule);
119
120     //! Request toggling of "isActive" flag of a given ignore rule.
121     /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
122       * and get that synced with the core immediately.
123       * \param ignoreRule A valid ignore rule
124       */
125     virtual inline void requestToggleIgnoreRule(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
126     virtual void toggleIgnoreRule(const QString &ignoreRule);
127
128     //! Request an IgnoreListItem to be added to the ignore list
129     /** Items added to the list with this method, get immediately synced with the core
130       * \param type The IgnoreType of the new rule
131       * \param ignoreRule The rule itself
132       * \param isRegEx Signals if the rule should be interpreted as a regular expression
133       * \param strictness Th StrictnessType that should be applied
134       * \param scope The ScopeType that should be set
135       * \param scopeRule A string of semi-colon separated network- or channelnames
136       * \param isActive Signals if the rule is enabled or not
137       */
138     virtual inline void requestAddIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
139         int scope, const QString &scopeRule, bool isActive)
140     {
141         REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
142     }
143
144
145     virtual void addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
146         int scope, const QString &scopeRule, bool isActive);
147
148 protected:
149     void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
150     bool scopeMatch(const QString &scopeRule, const QString &string) const; // scopeRule is a ';'-separated list, string is a network/channel-name
151
152     StrictnessType _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName);
153
154 signals:
155     void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
156
157 private:
158     IgnoreList _ignoreList;
159 };
160
161
162 #endif // IGNORELISTMANAGER_H