1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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),
38 NotificationSettings notificationSettings;
39 _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
40 _highlightNick = notificationSettings.highlightNick();
41 highlightListChanged(notificationSettings.highlightList());
42 notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
43 notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
44 notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
46 _processTimer.setInterval(0);
47 connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
50 void QtUiMessageProcessor::reset() {
51 if(processMode() == TimerBased) {
52 if(_processTimer.isActive()) _processTimer.stop();
54 _currentBatch.clear();
55 _processQueue.clear();
59 void QtUiMessageProcessor::process(Message &msg) {
60 checkForHighlight(msg);
61 Client::messageModel()->insertMessage(msg);
65 void QtUiMessageProcessor::process(QList<Message> &msgs) {
66 QList<Message>::iterator msgIter = msgs.begin();
67 QList<Message>::iterator msgIterEnd = msgs.end();
68 while(msgIter != msgIterEnd) {
69 checkForHighlight(*msgIter);
70 postProcess(*msgIter);
73 Client::messageModel()->insertMessages(msgs);
77 if(msgs.isEmpty()) return;
78 _processQueue.append(msgs);
79 _msgCount += msgs.count();
80 if(!isProcessing()) startProcessing();
81 else updateProgress();
84 void QtUiMessageProcessor::startProcessing() {
85 if(processMode() == TimerBased) {
86 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
89 _msgCount = _currentBatch.count();
90 foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
92 if(!_processTimer.isActive()) _processTimer.start();
96 void QtUiMessageProcessor::processNextMessage() {
97 if(_currentBatch.isEmpty()) {
98 if(_processQueue.isEmpty()) {
101 _msgsProcessed = _msgCount = 0;
105 _currentBatch = _processQueue.takeFirst();
107 Message msg = _currentBatch.takeFirst();
113 void QtUiMessageProcessor::updateProgress(bool start) {
115 _progressTimer.start();
116 emit progressUpdated(_msgsProcessed, _msgCount);
118 if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
119 _progressTimer.restart();
120 emit progressUpdated(_msgsProcessed, _msgCount);
125 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
126 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
129 // TODO: Cache this (per network)
130 const Network *net = Client::network(msg.bufferInfo().networkId());
131 if(net && !net->myNick().isEmpty()) {
132 QStringList nickList;
133 if(_highlightNick == NotificationSettings::CurrentNick) {
134 nickList << net->myNick();
135 } else if(_highlightNick == NotificationSettings::AllNicks) {
136 const Identity *myIdentity = Client::identity(net->identity());
138 nickList = myIdentity->nicks();
140 foreach(QString nickname, nickList) {
141 QRegExp nickRegExp("\\b" + QRegExp::escape(nickname) + "(\\W|\\b|$)", // + "\\b", this does not seem to work for trailing ` -> upstream bug?
142 _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
143 if(nickRegExp.indexIn(msg.contents()) >= 0) {
144 msg.setFlags(msg.flags() | Message::Highlight);
149 for(int i = 0; i < _highlightRules.count(); i++) {
150 const HighlightRule &rule = _highlightRules.at(i);
156 QRegExp rx(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
157 match = rx.exactMatch(msg.contents());
159 QRegExp rx("\\b" + QRegExp::escape(rule.name) + "\\b", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
160 match = (rx.indexIn(msg.contents()) >= 0);
163 msg.setFlags(msg.flags() | Message::Highlight);
170 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
171 _nicksCaseSensitive = variant.toBool();
174 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
175 QVariantList varList = variant.toList();
177 _highlightRules.clear();
178 QVariantList::const_iterator iter = varList.constBegin();
179 while(iter != varList.constEnd()) {
180 QVariantMap rule = iter->toMap();
181 _highlightRules << HighlightRule(rule["Name"].toString(),
182 rule["Enable"].toBool(),
183 rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
184 rule["RegEx"].toBool());
189 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
190 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();