Implement core-side highlights
[quassel.git] / src / qtui / qtuimessageprocessor.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 "qtuimessageprocessor.h"
22
23 #include "client.h"
24 #include "clientsettings.h"
25 #include "identity.h"
26 #include "messagemodel.h"
27 #include "network.h"
28
29 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
30     : AbstractMessageProcessor(parent),
31     _processing(false),
32     _processMode(TimerBased)
33 {
34     NotificationSettings notificationSettings;
35     _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
36     _highlightNick = notificationSettings.highlightNick();
37     highlightListChanged(notificationSettings.highlightList());
38     notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
39     notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
40     notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
41
42     _processTimer.setInterval(0);
43     connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
44 }
45
46
47 void QtUiMessageProcessor::reset()
48 {
49     if (processMode() == TimerBased) {
50         if (_processTimer.isActive()) _processTimer.stop();
51         _processing = false;
52         _currentBatch.clear();
53         _processQueue.clear();
54     }
55 }
56
57
58 void QtUiMessageProcessor::process(Message &msg)
59 {
60     if (!Client::coreFeatures().testFlag(Quassel::Feature::CoreSideHighlights))
61         checkForHighlight(msg);
62     preProcess(msg);
63     Client::messageModel()->insertMessage(msg);
64 }
65
66
67 void QtUiMessageProcessor::process(QList<Message> &msgs)
68 {
69     QList<Message>::iterator msgIter = msgs.begin();
70     QList<Message>::iterator msgIterEnd = msgs.end();
71     while (msgIter != msgIterEnd) {
72         if (!Client::coreFeatures().testFlag(Quassel::Feature::CoreSideHighlights))
73             checkForHighlight(*msgIter);
74         preProcess(*msgIter);
75         ++msgIter;
76     }
77     Client::messageModel()->insertMessages(msgs);
78     return;
79
80     if (msgs.isEmpty()) return;
81     _processQueue.append(msgs);
82     if (!isProcessing())
83         startProcessing();
84 }
85
86
87 void QtUiMessageProcessor::startProcessing()
88 {
89     if (processMode() == TimerBased) {
90         if (_currentBatch.isEmpty() && _processQueue.isEmpty())
91             return;
92         _processing = true;
93         if (!_processTimer.isActive())
94             _processTimer.start();
95     }
96 }
97
98
99 void QtUiMessageProcessor::processNextMessage()
100 {
101     if (_currentBatch.isEmpty()) {
102         if (_processQueue.isEmpty()) {
103             _processTimer.stop();
104             _processing = false;
105             return;
106         }
107         _currentBatch = _processQueue.takeFirst();
108     }
109     Message msg = _currentBatch.takeFirst();
110     process(msg);
111 }
112
113
114 void QtUiMessageProcessor::checkForHighlight(Message &msg)
115 {
116     if (!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
117         return;
118
119     // TODO: Cache this (per network)
120     const Network *net = Client::network(msg.bufferInfo().networkId());
121     if (net && !net->myNick().isEmpty()) {
122         QStringList nickList;
123         if (_highlightNick == NotificationSettings::CurrentNick) {
124             nickList << net->myNick();
125         }
126         else if (_highlightNick == NotificationSettings::AllNicks) {
127             const Identity *myIdentity = Client::identity(net->identity());
128             if (myIdentity)
129                 nickList = myIdentity->nicks();
130             if (!nickList.contains(net->myNick()))
131                 nickList.prepend(net->myNick());
132         }
133         foreach(QString nickname, nickList) {
134             QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
135             if (nickRegExp.indexIn(stripFormatCodes(msg.contents())) >= 0) {
136                 msg.setFlags(msg.flags() | Message::Highlight);
137                 return;
138             }
139         }
140
141         for (int i = 0; i < _highlightRules.count(); i++) {
142             const HighlightRule &rule = _highlightRules.at(i);
143             if (!rule.isEnabled)
144                 continue;
145
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(msg.bufferInfo().bufferName()))
150                         continue;
151                 }
152                 else {
153                     QRegExp rx(rule.chanName, Qt::CaseInsensitive);
154                     if (!rx.exactMatch(msg.bufferInfo().bufferName()))
155                         continue;
156                 }
157             }
158
159             QRegExp rx;
160             if (rule.isRegExp) {
161                 rx = QRegExp(rule.name, rule.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
162             }
163             else {
164                 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
165             }
166             bool match = (rx.indexIn(stripFormatCodes(msg.contents())) >= 0);
167             if (match) {
168                 msg.setFlags(msg.flags() | Message::Highlight);
169                 return;
170             }
171         }
172     }
173 }
174
175
176 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant)
177 {
178     _nicksCaseSensitive = variant.toBool();
179 }
180
181
182 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant)
183 {
184     QVariantList varList = variant.toList();
185
186     _highlightRules.clear();
187     QVariantList::const_iterator iter = varList.constBegin();
188     while (iter != varList.constEnd()) {
189         QVariantMap rule = iter->toMap();
190         _highlightRules << HighlightRule(rule["Name"].toString(),
191             rule["Enable"].toBool(),
192             rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
193             rule["RegEx"].toBool(),
194             rule["Channel"].toString());
195         ++iter;
196     }
197 }
198
199
200 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant)
201 {
202     _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
203 }