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 #include "highlightrulemanager.h"
26 #include <QStringList>
28 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
29 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
34 SyncableObject::operator=(other);
35 _highlightRuleList = other._highlightRuleList;
36 _nicksCaseSensitive = other._nicksCaseSensitive;
37 _highlightNick = other._highlightNick;
42 int HighlightRuleManager::indexOf(const QString &name) const
44 for (int i = 0; i < _highlightRuleList.count(); i++) {
45 if (_highlightRuleList[i].name == name)
52 QVariantMap HighlightRuleManager::initHighlightRuleList() const
54 QVariantMap highlightRuleListMap;
57 QVariantList isCaseSensitive;
58 QVariantList isActive;
59 QVariantList isInverse;
62 for (int i = 0; i < _highlightRuleList.count(); i++) {
63 name << _highlightRuleList[i].name;
64 isRegEx << _highlightRuleList[i].isRegEx;
65 isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
66 isActive << _highlightRuleList[i].isEnabled;
67 isInverse << _highlightRuleList[i].isInverse;
68 channel << _highlightRuleList[i].chanName;
71 highlightRuleListMap["name"] = name;
72 highlightRuleListMap["isRegEx"] = isRegEx;
73 highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
74 highlightRuleListMap["isEnabled"] = isActive;
75 highlightRuleListMap["isInverse"] = isInverse;
76 highlightRuleListMap["channel"] = channel;
77 highlightRuleListMap["highlightNick"] = _highlightNick;
78 highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
79 return highlightRuleListMap;
83 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
85 QStringList name = highlightRuleList["name"].toStringList();
86 QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
87 QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
88 QVariantList isActive = highlightRuleList["isEnabled"].toList();
89 QVariantList isInverse = highlightRuleList["isInverse"].toList();
90 QStringList channel = highlightRuleList["channel"].toStringList();
92 int count = name.count();
93 if (count != isRegEx.count() || count != isCaseSensitive.count() ||
94 count != isActive.count() || count != channel.count()) {
95 qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
99 _highlightRuleList.clear();
100 for (int i = 0; i < name.count(); i++) {
101 _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
102 isActive[i].toBool(), isInverse[i].toBool(), channel[i]);
104 _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt());
105 _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
108 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
109 bool isInverse, const QString &channel)
111 if (contains(name)) {
115 HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, channel);
116 _highlightRuleList << newItem;
118 SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(channel))
122 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks)
124 if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
128 bool matches = false;
130 for (int i = 0; i < _highlightRuleList.count(); i++) {
131 const HighlightRule &rule = _highlightRuleList.at(i);
135 if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
136 if (rule.chanName.startsWith("!")) {
137 QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
138 if (rx.exactMatch(bufferName))
142 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
143 if (!rx.exactMatch(bufferName))
150 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
153 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
155 bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
157 // If an inverse rule matches, then we know that we never want to return a highlight.
158 if (rule.isInverse) {
169 if (!currentNick.isEmpty()) {
170 QStringList nickList;
171 if (_highlightNick == CurrentNick) {
172 nickList << currentNick;
174 else if (_highlightNick == AllNicks) {
175 nickList = identityNicks;
176 if (!nickList.contains(currentNick))
177 nickList.prepend(currentNick);
180 for(const QString &nickname : nickList) {
181 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
182 if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
191 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
193 removeAt(indexOf(highlightRule));
194 SYNC(ARG(highlightRule))
198 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
200 int idx = indexOf(highlightRule);
203 _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
204 SYNC(ARG(highlightRule))
207 bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks)
209 return _match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);