1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
27 #include <QStringList>
28 #include <QVariantList>
29 #include <QVariantMap>
32 #include "syncableobject.h"
34 class HighlightRuleManager : public SyncableObject
39 Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
40 Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
42 enum HighlightNickType {
48 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
49 HighlightRuleManager &operator=(const HighlightRuleManager &other);
51 struct HighlightRule {
55 bool isCaseSensitive = false;
56 bool isEnabled = true;
57 bool isInverse = false;
61 HighlightRule(int id_, QString name_, bool isRegEx_, bool isCaseSensitive_, bool isEnabled_, bool isInverse_,
62 QString sender_, QString chanName_)
63 : id(id_), name(std::move(name_)), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_),
64 isEnabled(isEnabled_), isInverse(isInverse_), sender(std::move(sender_)), chanName(std::move(chanName_)) {
67 bool operator!=(const HighlightRule &other) {
68 return (id != other.id ||
70 isRegEx != other.isRegEx ||
71 isCaseSensitive != other.isCaseSensitive ||
72 isEnabled != other.isEnabled ||
73 isInverse != other.isInverse ||
74 sender != other.sender ||
75 chanName != other.chanName);
78 typedef QList<HighlightRule> HighlightRuleList;
80 int indexOf(int rule) const;
81 inline bool contains(int rule) const { return indexOf(rule) != -1; }
82 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
83 inline int count() const { return _highlightRuleList.count(); }
84 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
85 inline void clear() { _highlightRuleList.clear(); }
86 inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
87 inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
88 inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
92 inline int highlightNick() { return _highlightNick; }
93 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
95 //! Check if a message matches the HighlightRule
96 /** This method checks if a message matches the users highlight rules.
97 * \param msg The Message that should be checked
99 bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks);
102 virtual QVariantMap initHighlightRuleList() const;
103 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
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 highlightRule A valid ignore rule
110 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
111 virtual void removeHighlightRule(int highlightRule);
113 //! Request toggling of "isEnabled" flag of a given ignore rule.
114 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
115 * and get that synced with the core immediately.
116 * \param highlightRule A valid ignore rule
118 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
119 virtual void toggleHighlightRule(int highlightRule);
121 //! Request an HighlightRule to be added to the ignore list
122 /** Items added to the list with this method, get immediately synced with the core
123 * \param name The rule
124 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
125 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
126 * \param isEnabled If the rule is active
127 * @param chanName The channel in which the rule should apply
129 virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
130 bool isInverse, const QString &sender, const QString &chanName)
132 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
137 virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
138 bool isInverse, const QString &sender, const QString &chanName);
140 virtual inline void requestSetHighlightNick(int highlightNick)
142 REQUEST(ARG(highlightNick))
144 inline void setHighlightNick(int highlightNick) { _highlightNick = static_cast<HighlightNickType>(highlightNick); }
146 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
148 REQUEST(ARG(nicksCaseSensitive))
150 inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
153 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
155 bool match(const QString &msgContents,
156 const QString &msgSender,
157 Message::Type msgType,
158 Message::Flags msgFlags,
159 const QString &bufferName,
160 const QString ¤tNick,
161 const QStringList identityNicks);
164 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
167 HighlightRuleList _highlightRuleList;
168 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
169 bool _nicksCaseSensitive = false;