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