Semi-yearly copyright bump
[quassel.git] / src / common / highlightrulemanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #pragma once
22
23 #include <utility>
24
25 #include <QRegExp>
26 #include <QString>
27 #include <QStringList>
28 #include <QVariantList>
29 #include <QVariantMap>
30
31 #include "message.h"
32 #include "syncableobject.h"
33
34 class HighlightRuleManager : public SyncableObject
35 {
36     SYNCABLE_OBJECT
37         Q_OBJECT
38 public:
39     enum HighlightNickType {
40         NoNick = 0x00,
41         CurrentNick = 0x01,
42         AllNicks = 0x02
43     };
44
45     inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
46     HighlightRuleManager &operator=(const HighlightRuleManager &other);
47
48     struct HighlightRule {
49         QString name;
50         bool isRegEx = false;
51         bool isCaseSensitive = false;
52         bool isEnabled = true;
53         bool isInverse = false;
54         QString sender;
55         QString chanName;
56         HighlightRule() {}
57         HighlightRule(QString name_, bool isRegEx_, bool isCaseSensitive_, bool isEnabled_, bool isInverse_,
58                       QString sender_, QString chanName_)
59             : name(std::move(name_)), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_), isEnabled(isEnabled_),
60               isInverse(isInverse_), sender(std::move(sender_)), chanName(std::move(chanName_)) {
61         }
62         bool operator!=(const HighlightRule &other)
63         {
64             return (name != other.name ||
65                     isRegEx != other.isRegEx ||
66                     isCaseSensitive != other.isCaseSensitive ||
67                     isEnabled != other.isEnabled ||
68                     isInverse != other.isInverse ||
69                     sender != other.sender ||
70                     chanName != other.chanName);
71         }
72     };
73     typedef QList<HighlightRule> HighlightRuleList;
74
75     int indexOf(const QString &rule) const;
76     inline bool contains(const QString &rule) const { return indexOf(rule) != -1; }
77     inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
78     inline int count() const { return _highlightRuleList.count(); }
79     inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
80     inline void clear() { _highlightRuleList.clear(); }
81     inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
82     inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
83     inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
84
85     inline HighlightNickType highlightNick() { return _highlightNick; }
86     inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
87
88     //! Check if a message matches the HighlightRule
89     /** This method checks if a message matches the users highlight rules.
90       * \param msg The Message that should be checked
91       */
92     bool match(const Message &msg, const QString &currentNick, const QStringList &identityNicks);
93
94 public slots:
95     virtual QVariantMap initHighlightRuleList() const;
96     virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
97
98     //! Request removal of an ignore rule based on the rule itself.
99     /** Use this method if you want to remove a single ignore rule
100       * and get that synced with the core immediately.
101       * \param highlightRule A valid ignore rule
102       */
103     virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
104     virtual void removeHighlightRule(const QString &highlightRule);
105
106     //! Request toggling of "isEnabled" flag of a given ignore rule.
107     /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
108       * and get that synced with the core immediately.
109       * \param highlightRule A valid ignore rule
110       */
111     virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
112     virtual void toggleHighlightRule(const QString &highlightRule);
113
114     //! Request an HighlightRule to be added to the ignore list
115     /** Items added to the list with this method, get immediately synced with the core
116       * \param name The rule
117       * \param isRegEx If the rule should be interpreted as a nickname, or a regex
118       * \param isCaseSensitive If the rule should be interpreted as case-sensitive
119       * \param isEnabled If the rule is active
120       * @param chanName The channel in which the rule should apply
121       */
122     virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
123                                                 bool isInverse, const QString &sender, const QString &chanName)
124     {
125         REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
126     }
127
128
129     virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
130                                   bool isInverse, const QString &sender, const QString &chanName);
131
132     virtual inline void requestSetHighlightNick(HighlightNickType highlightNick)
133     {
134         REQUEST(ARG(highlightNick))
135     }
136     inline void setHighlightNick(HighlightNickType highlightNick) { _highlightNick = highlightNick; }
137
138     virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
139     {
140         REQUEST(ARG(nicksCaseSensitive))
141     }
142     inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
143
144 protected:
145     void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
146
147     bool match(const QString &msgContents,
148                const QString &msgSender,
149                Message::Type msgType,
150                Message::Flags msgFlags,
151                const QString &bufferName,
152                const QString &currentNick,
153                const QStringList identityNicks);
154
155 signals:
156     void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
157
158 private:
159     HighlightRuleList _highlightRuleList;
160     HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
161     bool _nicksCaseSensitive = false;
162 };