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;
61 for (int i = 0; i < _highlightRuleList.count(); i++) {
62 name << _highlightRuleList[i].name;
63 isRegEx << _highlightRuleList[i].isRegEx;
64 isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
65 isActive << _highlightRuleList[i].isEnabled;
66 channel << _highlightRuleList[i].chanName;
69 highlightRuleListMap["name"] = name;
70 highlightRuleListMap["isRegEx"] = isRegEx;
71 highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
72 highlightRuleListMap["isEnabled"] = isActive;
73 highlightRuleListMap["channel"] = channel;
74 highlightRuleListMap["highlightNick"] = _highlightNick;
75 highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
76 return highlightRuleListMap;
80 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
82 QStringList name = highlightRuleList["name"].toStringList();
83 QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
84 QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
85 QVariantList isActive = highlightRuleList["isEnabled"].toList();
86 QStringList channel = highlightRuleList["channel"].toStringList();
88 int count = name.count();
89 if (count != isRegEx.count() || count != isCaseSensitive.count() ||
90 count != isActive.count() || count != channel.count()) {
91 qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
95 _highlightRuleList.clear();
96 for (int i = 0; i < name.count(); i++) {
97 _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
98 isActive[i].toBool(), channel[i]);
100 _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt());
101 _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
104 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
105 const QString &channel)
107 if (contains(name)) {
111 HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel);
112 _highlightRuleList << newItem;
114 SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(channel))
118 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks)
120 if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
124 if (!currentNick.isEmpty()) {
125 QStringList nickList;
126 if (_highlightNick == CurrentNick) {
127 nickList << currentNick;
129 else if (_highlightNick == AllNicks) {
130 nickList = identityNicks;
131 if (!nickList.contains(currentNick))
132 nickList.prepend(currentNick);
134 foreach(QString nickname, nickList) {
135 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
136 if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
141 for (int i = 0; i < _highlightRuleList.count(); i++) {
142 const HighlightRule &rule = _highlightRuleList.at(i);
146 if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
147 if (rule.chanName.startsWith("!")) {
148 QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
149 if (rx.exactMatch(bufferName))
153 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
154 if (!rx.exactMatch(bufferName))
161 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
164 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
166 bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
176 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
178 removeAt(indexOf(highlightRule));
179 SYNC(ARG(highlightRule))
183 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
185 int idx = indexOf(highlightRule);
188 _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
189 SYNC(ARG(highlightRule))
192 bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks)
194 return _match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);