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;
40 int HighlightRuleManager::indexOf(const QString &name) const
42 for (int i = 0; i < _highlightRuleList.count(); i++) {
43 if (_highlightRuleList[i].name == name)
50 QVariantMap HighlightRuleManager::initHighlightRuleList() const
52 QVariantMap highlightRuleListMap;
55 QVariantList isCaseSensitive;
56 QVariantList isActive;
59 for (int i = 0; i < _highlightRuleList.count(); i++) {
60 name << _highlightRuleList[i].name;
61 isRegEx << _highlightRuleList[i].isRegEx;
62 isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
63 isActive << _highlightRuleList[i].isEnabled;
64 channel << _highlightRuleList[i].chanName;
67 highlightRuleListMap["name"] = name;
68 highlightRuleListMap["isRegEx"] = isRegEx;
69 highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
70 highlightRuleListMap["isEnabled"] = isActive;
71 highlightRuleListMap["channel"] = channel;
72 return highlightRuleListMap;
76 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
78 QStringList name = highlightRuleList["name"].toStringList();
79 QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
80 QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
81 QVariantList isActive = highlightRuleList["isEnabled"].toList();
82 QStringList channel = highlightRuleList["channel"].toStringList();
84 int count = name.count();
85 if (count != isRegEx.count() || count != isCaseSensitive.count() ||
86 count != isActive.count() || count != channel.count()) {
87 qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
91 _highlightRuleList.clear();
92 for (int i = 0; i < name.count(); i++) {
93 _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
94 isActive[i].toBool(), channel[i]);
98 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
99 const QString &channel)
101 if (contains(name)) {
105 HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel);
106 _highlightRuleList << newItem;
108 SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(channel))
112 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks)
114 if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
118 if (!currentNick.isEmpty()) {
119 QStringList nickList;
120 if (_highlightNick == CurrentNick) {
121 nickList << currentNick;
123 else if (_highlightNick == AllNicks) {
124 nickList = identityNicks;
125 if (!nickList.contains(currentNick))
126 nickList.prepend(currentNick);
128 foreach(QString nickname, nickList) {
129 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
130 if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
135 for (int i = 0; i < _highlightRuleList.count(); i++) {
136 const HighlightRule &rule = _highlightRuleList.at(i);
140 if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
141 if (rule.chanName.startsWith("!")) {
142 QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
143 if (rx.exactMatch(bufferName))
147 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
148 if (!rx.exactMatch(bufferName))
155 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
158 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
160 bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
170 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
172 removeAt(indexOf(highlightRule));
173 SYNC(ARG(highlightRule))
177 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
179 int idx = indexOf(highlightRule);
182 _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
183 SYNC(ARG(highlightRule))