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) : AbstractMessageProcessor(parent) {
32 _msgsProcessed = _msgCount = 0;
34 _processMode = TimerBased;
35 _processTimer.setInterval(0);
36 connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
39 void QtUiMessageProcessor::reset() {
40 if(processMode() == TimerBased) {
41 if(_processTimer.isActive()) _processTimer.stop();
43 _currentBatch.clear();
44 _processQueue.clear();
48 void QtUiMessageProcessor::process(Message &msg) {
49 checkForHighlight(msg);
50 Client::messageModel()->insertMessage(msg);
54 void QtUiMessageProcessor::process(QList<Message> &msgs) {
55 _processQueue.append(msgs);
56 _msgCount += msgs.count();
57 if(!isProcessing()) startProcessing();
58 else updateProgress();
61 void QtUiMessageProcessor::startProcessing() {
62 if(processMode() == TimerBased) {
63 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
66 _msgCount = _currentBatch.count();
67 foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
69 if(!_processTimer.isActive()) _processTimer.start();
73 void QtUiMessageProcessor::processNextMessage() {
74 if(_currentBatch.isEmpty()) {
75 if(_processQueue.isEmpty()) {
78 _msgsProcessed = _msgCount = 0;
82 _currentBatch = _processQueue.takeFirst();
84 Message msg = _currentBatch.takeFirst();
90 void QtUiMessageProcessor::updateProgress(bool start) {
92 _progressTimer.start();
93 emit progressUpdated(_msgsProcessed, _msgCount);
95 if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
96 _progressTimer.restart();
97 emit progressUpdated(_msgsProcessed, _msgCount);
102 // TODO optimize checkForHighlight
103 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
104 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
107 NotificationSettings notificationSettings;
108 const Network *net = Client::network(msg.bufferInfo().networkId());
109 if(net && !net->myNick().isEmpty()) {
110 QStringList nickList;
111 if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
112 nickList << net->myNick();
113 } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
114 const Identity *myIdentity = Client::identity(net->identity());
116 nickList = myIdentity->nicks();
118 foreach(QString nickname, nickList) {
119 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
120 if(nickRegExp.exactMatch(msg.contents())) {
121 msg.setFlags(msg.flags() | Message::Highlight);
126 foreach(QVariant highlight, notificationSettings.highlightList()) {
127 QVariantMap highlightRule = highlight.toMap();
128 if(!highlightRule["enable"].toBool())
130 Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
131 QString name = highlightRule["name"].toString();
133 if(highlightRule["regex"].toBool()) {
134 userRegExp = QRegExp(name, caseSensitivity);
136 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
138 if(userRegExp.exactMatch(msg.contents())) {
139 msg.setFlags(msg.flags() | Message::Highlight);