1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 "qtuimessageprocessor.h"
24 #include "clientsettings.h"
26 #include "messagemodel.h"
29 const int progressUpdateDelay = 100; // ms between progress signal updates
31 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
32 : AbstractMessageProcessor(parent),
34 _processMode(TimerBased)
36 NotificationSettings notificationSettings;
37 _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
38 _highlightNick = notificationSettings.highlightNick();
39 highlightListChanged(notificationSettings.highlightList());
40 notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
41 notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
42 notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
44 _processTimer.setInterval(0);
45 connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
49 void QtUiMessageProcessor::reset()
51 if (processMode() == TimerBased) {
52 if (_processTimer.isActive()) _processTimer.stop();
54 _currentBatch.clear();
55 _processQueue.clear();
60 void QtUiMessageProcessor::process(Message &msg)
62 checkForHighlight(msg);
64 Client::messageModel()->insertMessage(msg);
68 void QtUiMessageProcessor::process(QList<Message> &msgs)
70 QList<Message>::iterator msgIter = msgs.begin();
71 QList<Message>::iterator msgIterEnd = msgs.end();
72 while (msgIter != msgIterEnd) {
73 checkForHighlight(*msgIter);
77 Client::messageModel()->insertMessages(msgs);
80 if (msgs.isEmpty()) return;
81 _processQueue.append(msgs);
87 void QtUiMessageProcessor::startProcessing()
89 if (processMode() == TimerBased) {
90 if (_currentBatch.isEmpty() && _processQueue.isEmpty())
93 if (!_processTimer.isActive())
94 _processTimer.start();
99 void QtUiMessageProcessor::processNextMessage()
101 if (_currentBatch.isEmpty()) {
102 if (_processQueue.isEmpty()) {
103 _processTimer.stop();
107 _currentBatch = _processQueue.takeFirst();
109 Message msg = _currentBatch.takeFirst();
114 void QtUiMessageProcessor::checkForHighlight(Message &msg)
116 if (!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
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();
126 else if (_highlightNick == NotificationSettings::AllNicks) {
127 const Identity *myIdentity = Client::identity(net->identity());
129 nickList = myIdentity->nicks();
130 if (!nickList.contains(net->myNick()))
131 nickList.prepend(net->myNick());
133 foreach(QString nickname, nickList) {
134 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
135 if (nickRegExp.indexIn(msg.contents()) >= 0) {
136 msg.setFlags(msg.flags() | Message::Highlight);
141 for (int i = 0; i < _highlightRules.count(); i++) {
142 const HighlightRule &rule = _highlightRules.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(msg.bufferInfo().bufferName()))
153 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
154 if (!rx.exactMatch(msg.bufferInfo().bufferName()))
161 rx = QRegExp(rule.name, rule.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
164 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
166 bool match = (rx.indexIn(msg.contents()) >= 0);
168 msg.setFlags(msg.flags() | Message::Highlight);
176 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant)
178 _nicksCaseSensitive = variant.toBool();
182 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant)
184 QVariantList varList = variant.toList();
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());
200 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant)
202 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();