9f780f5170186f688c3639e67393888fc4be844d
[quassel.git] / src / common / highlightrulemanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "highlightrulemanager.h"
22 #include "util.h"
23
24 #include <QtCore>
25 #include <QDebug>
26 #include <QStringList>
27
28 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
29 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
30 {
31     if (this == &other)
32         return *this;
33
34     SyncableObject::operator=(other);
35     _highlightRuleList = other._highlightRuleList;
36     return *this;
37 }
38
39
40 int HighlightRuleManager::indexOf(const QString &name) const
41 {
42     for (int i = 0; i < _highlightRuleList.count(); i++) {
43         if (_highlightRuleList[i].name == name)
44             return i;
45     }
46     return -1;
47 }
48
49
50 QVariantMap HighlightRuleManager::initHighlightRuleList() const
51 {
52     QVariantMap highlightRuleListMap;
53     QStringList name;
54     QVariantList isRegEx;
55     QVariantList isCaseSensitive;
56     QVariantList isActive;
57     QStringList channel;
58
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;
65     }
66
67     highlightRuleListMap["name"] = name;
68     highlightRuleListMap["isRegEx"] = isRegEx;
69     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
70     highlightRuleListMap["isEnabled"] = isActive;
71     highlightRuleListMap["channel"] = channel;
72     return highlightRuleListMap;
73 }
74
75
76 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
77 {
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();
83
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)";
88         return;
89     }
90
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]);
95     }
96 }
97
98 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
99                                             const QString &channel)
100 {
101     if (contains(name)) {
102         return;
103     }
104
105     HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel);
106     _highlightRuleList << newItem;
107
108     SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(channel))
109 }
110
111
112 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString &currentNick, const QStringList identityNicks)
113 {
114     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
115        return false;
116     }
117
118     if (!currentNick.isEmpty()) {
119         QStringList nickList;
120         if (_highlightNick == CurrentNick) {
121             nickList << currentNick;
122         }
123         else if (_highlightNick == AllNicks) {
124             nickList = identityNicks;
125             if (!nickList.contains(currentNick))
126                 nickList.prepend(currentNick);
127         }
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) {
131                     return true;
132                 }
133             }
134
135         for (int i = 0; i < _highlightRuleList.count(); i++) {
136             const HighlightRule &rule = _highlightRuleList.at(i);
137             if (!rule.isEnabled)
138                 continue;
139
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))
144                         continue;
145                 }
146                 else {
147                     QRegExp rx(rule.chanName, Qt::CaseInsensitive);
148                     if (!rx.exactMatch(bufferName))
149                         continue;
150                 }
151             }
152
153             QRegExp rx;
154             if (rule.isRegEx) {
155                 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
156             }
157             else {
158                 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
159             }
160             bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
161             if (match) {
162                 return true;
163             }
164         }
165     }
166
167     return false;
168 }
169
170 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
171 {
172     removeAt(indexOf(highlightRule));
173     SYNC(ARG(highlightRule))
174 }
175
176
177 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
178 {
179     int idx = indexOf(highlightRule);
180     if (idx == -1)
181         return;
182     _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
183     SYNC(ARG(highlightRule))
184 }