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 ***************************************************************************/
21 #ifndef HIGHLIGHTRULELISTMANAGER_H
22 #define HIGHLIGHTRULELISTMANAGER_H
28 #include "syncableobject.h"
30 class HighlightRuleManager : public SyncableObject
35 enum HighlightNickType {
41 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
42 HighlightRuleManager &operator=(const HighlightRuleManager &other);
44 struct HighlightRule {
47 bool isCaseSensitive = false;
48 bool isEnabled = true;
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_) {
55 bool operator!=(const HighlightRule &other)
57 return (name != other.name ||
58 isRegEx != other.isRegEx ||
59 isCaseSensitive != other.isCaseSensitive ||
60 isEnabled != other.isEnabled ||
61 chanName != other.chanName);
64 typedef QList<HighlightRule> HighlightRuleList;
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 void clear() { _highlightRuleList.clear(); }
72 inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
73 inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
74 inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
76 inline HighlightNickType highlightNick() { return _highlightNick; }
77 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
79 //! Check if a message matches the HighlightRule
80 /** This method checks if a message matches the users highlight rules.
81 * \param msg The Message that should be checked
83 inline bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks) { return _match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks); }
86 virtual QVariantMap initHighlightRuleList() const;
87 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
89 //! Request removal of an ignore rule based on the rule itself.
90 /** Use this method if you want to remove a single ignore rule
91 * and get that synced with the core immediately.
92 * \param highlightRule A valid ignore rule
94 virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
95 virtual void removeHighlightRule(const QString &highlightRule);
97 //! Request toggling of "isEnabled" flag of a given ignore rule.
98 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
99 * and get that synced with the core immediately.
100 * \param highlightRule A valid ignore rule
102 virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
103 virtual void toggleHighlightRule(const QString &highlightRule);
105 //! Request an HighlightRule to be added to the ignore list
106 /** Items added to the list with this method, get immediately synced with the core
107 * \param name The rule
108 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
109 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
110 * \param isEnabled If the rule is active
111 * @param chanName The channel in which the rule should apply
113 virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
114 const QString &chanName)
116 REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(chanName))
120 virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive,
121 bool isEnabled, const QString &chanName);
123 virtual inline void requestSetHighlightNick(HighlightNickType highlightNick)
125 REQUEST(ARG(highlightNick))
127 inline void setHighlightNick(HighlightNickType highlightNick) { _highlightNick = highlightNick; }
129 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
131 REQUEST(ARG(nicksCaseSensitive))
133 inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; }
136 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
138 bool _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks);
141 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, QString chanName);
144 HighlightRuleList _highlightRuleList;
145 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
146 bool _nicksCaseSensitive = false;
150 #endif // HIGHLIGHTRULELISTMANAGER_H