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