a906b953d0667b36db5c2a73a4cb85078ed5a0d0
[quassel.git] / src / common / ignorelistmanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef IGNORELISTMANAGER_H
22 #define IGNORELISTMANAGER_H
23
24 #include <QString>
25
26 #include "syncableobject.h"
27
28 class Message;
29
30 class IgnoreListManager : public SyncableObject
31 {
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   };
41
42   enum StrictnessType {
43     UnmatchedStrictness = 0,
44     SoftStrictness = 1,
45     HardStrictness = 2
46   };
47
48   enum ScopeType {
49     GlobalScope,
50     NetworkScope,
51     ChannelScope,
52   };
53
54   struct IgnoreListItem {
55     IgnoreType type;
56     QString ignoreRule;
57     bool isRegEx;
58     StrictnessType strictness;
59     ScopeType scope;
60     QString scopeRule;
61     bool isActive;
62     IgnoreListItem() {}
63     IgnoreListItem(IgnoreType type_, const QString &ignoreRule_, bool isRegEx_, StrictnessType strictness_,
64                ScopeType scope_, const QString &scopeRule_, bool isActive_)
65         : type(type_), ignoreRule(ignoreRule_), isRegEx(isRegEx_), strictness(strictness_), scope(scope_), scopeRule(scopeRule_), isActive(isActive_)  {}
66     bool operator!=(const IgnoreListItem &other) {
67       return (type != other.type ||
68         ignoreRule != other.ignoreRule ||
69         isRegEx != other.isRegEx ||
70         strictness != other.strictness ||
71         scope != other.scope ||
72         scopeRule != other.scopeRule ||
73         isActive != other.isActive);
74     }
75   };
76   typedef QList<IgnoreListItem> IgnoreList;
77
78   int indexOf(const QString &ignore) const;
79   inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
80   inline bool isEmpty() const { return _ignoreList.isEmpty(); }
81   inline int count() const { return _ignoreList.count(); }
82   inline void removeAt(int index) { _ignoreList.removeAt(index); }
83   inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
84   inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
85   inline const IgnoreList &ignoreList() const { return _ignoreList; }
86
87   //! Check if a message matches the IgnoreRule
88   /** This method checks if a message matches the users ignorelist.
89     * \param msg The Message that should be checked
90     * \param network The networkname the message belongs to
91     * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
92     */
93   StrictnessType match(const Message &msg, const QString &network = QString());
94
95 public slots:
96   virtual QVariantMap initIgnoreList() const;
97   virtual void initSetIgnoreList(const QVariantMap &ignoreList);
98
99   virtual void addIgnoreListItem(IgnoreType type, const QString &ignoreRule, bool isRegEx, StrictnessType strictness,
100                              ScopeType scope, const QString &scopeRule, bool isActive);
101   virtual void addIgnoreListItem(const IgnoreListItem &item);
102 protected:
103   void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
104
105 signals:
106   void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
107
108 private:
109   // scopeRule is a ; separated list, string is a network/channel-name
110   bool scopeMatch(const QString &scopeRule, const QString &string);
111   IgnoreList _ignoreList;
112 };
113
114 #endif // IGNORELISTMANAGER_H