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)
43 enum HighlightNickType {
49 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
50 HighlightRuleManager &operator=(const HighlightRuleManager &other);
57 bool isCaseSensitive = false;
58 bool isEnabled = true;
59 bool isInverse = false;
63 HighlightRule(int id_, QString name_, bool isRegEx_, bool isCaseSensitive_, bool isEnabled_, bool isInverse_,
64 QString sender_, QString chanName_)
65 : id(id_), name(std::move(name_)), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_),
66 isEnabled(isEnabled_), isInverse(isInverse_), sender(std::move(sender_)), chanName(std::move(chanName_))
69 bool operator!=(const HighlightRule &other) const
71 return (id != other.id ||
73 isRegEx != other.isRegEx ||
74 isCaseSensitive != other.isCaseSensitive ||
75 isEnabled != other.isEnabled ||
76 isInverse != other.isInverse ||
77 sender != other.sender ||
78 chanName != other.chanName);
82 using HighlightRuleList = QList<HighlightRule>;
84 int indexOf(int rule) const;
85 inline bool contains(int rule) const { return indexOf(rule) != -1; }
86 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
87 inline int count() const { return _highlightRuleList.count(); }
88 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
89 inline void clear() { _highlightRuleList.clear(); }
90 inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
91 inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
92 inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
96 inline int highlightNick() { return _highlightNick; }
97 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
99 //! Check if a message matches the HighlightRule
100 /** This method checks if a message matches the users highlight rules.
101 * \param msg The Message that should be checked
103 bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks);
106 virtual QVariantMap initHighlightRuleList() const;
107 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
109 //! Request removal of an ignore rule based on the rule itself.
110 /** Use this method if you want to remove a single ignore rule
111 * and get that synced with the core immediately.
112 * \param highlightRule A valid ignore rule
114 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
115 virtual void removeHighlightRule(int highlightRule);
117 //! Request toggling of "isEnabled" flag of a given ignore rule.
118 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
119 * and get that synced with the core immediately.
120 * \param highlightRule A valid ignore rule
122 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
123 virtual void toggleHighlightRule(int highlightRule);
125 //! Request an HighlightRule to be added to the ignore list
126 /** Items added to the list with this method, get immediately synced with the core
127 * \param name The rule
128 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
129 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
130 * \param isEnabled If the rule is active
131 * @param chanName The channel in which the rule should apply
133 virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
134 bool isInverse, const QString &sender, const QString &chanName)
136 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
141 virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
142 bool isInverse, const QString &sender, const QString &chanName);
144 virtual inline void requestSetHighlightNick(int highlightNick)
146 REQUEST(ARG(highlightNick))
149 inline void setHighlightNick(int highlightNick) { _highlightNick = static_cast<HighlightNickType>(highlightNick); }
151 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
153 REQUEST(ARG(nicksCaseSensitive))
156 inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
159 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
161 bool match(const QString &msgContents,
162 const QString &msgSender,
163 Message::Type msgType,
164 Message::Flags msgFlags,
165 const QString &bufferName,
166 const QString ¤tNick,
167 const QStringList identityNicks);
170 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
173 HighlightRuleList _highlightRuleList;
174 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
175 bool _nicksCaseSensitive = false;