1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 enum HighlightNickType {
45 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
46 HighlightRuleManager &operator=(const HighlightRuleManager &other);
48 struct HighlightRule {
51 bool isCaseSensitive = false;
52 bool isEnabled = true;
53 bool isInverse = false;
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_)) {
62 bool operator!=(const HighlightRule &other)
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);
73 typedef QList<HighlightRule> HighlightRuleList;
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; }
85 inline HighlightNickType highlightNick() { return _highlightNick; }
86 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
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
92 bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks);
95 virtual QVariantMap initHighlightRuleList() const;
96 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
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
103 virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
104 virtual void removeHighlightRule(const QString &highlightRule);
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
111 virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
112 virtual void toggleHighlightRule(const QString &highlightRule);
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
122 virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
123 bool isInverse, const QString &sender, const QString &chanName)
125 REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
129 virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
130 bool isInverse, const QString &sender, const QString &chanName);
132 virtual inline void requestSetHighlightNick(HighlightNickType highlightNick)
134 REQUEST(ARG(highlightNick))
136 inline void setHighlightNick(HighlightNickType highlightNick) { _highlightNick = highlightNick; }
138 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
140 REQUEST(ARG(nicksCaseSensitive))
142 inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
145 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
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 ¤tNick,
153 const QStringList identityNicks);
156 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
159 HighlightRuleList _highlightRuleList;
160 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
161 bool _nicksCaseSensitive = false;