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 _highlightNick = notificationSettings.highlightNick();
40 highlightListChanged(notificationSettings.highlightList());
41 notificationSettings.notify("highlightList", this, SLOT(highlightListChanged(const QVariant &)));
42 notificationSettings.notify("highlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
44 _processTimer.setInterval(0);
45 connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
48 void QtUiMessageProcessor::reset() {
49 if(processMode() == TimerBased) {
50 if(_processTimer.isActive()) _processTimer.stop();
52 _currentBatch.clear();
53 _processQueue.clear();
57 void QtUiMessageProcessor::process(Message &msg) {
58 checkForHighlight(msg);
59 Client::messageModel()->insertMessage(msg);
63 void QtUiMessageProcessor::process(QList<Message> &msgs) {
64 if(msgs.isEmpty()) return;
65 _processQueue.append(msgs);
66 _msgCount += msgs.count();
67 if(!isProcessing()) startProcessing();
68 else updateProgress();
71 void QtUiMessageProcessor::startProcessing() {
72 if(processMode() == TimerBased) {
73 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
76 _msgCount = _currentBatch.count();
77 foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
79 if(!_processTimer.isActive()) _processTimer.start();
83 void QtUiMessageProcessor::processNextMessage() {
84 if(_currentBatch.isEmpty()) {
85 if(_processQueue.isEmpty()) {
88 _msgsProcessed = _msgCount = 0;
92 _currentBatch = _processQueue.takeFirst();
94 Message msg = _currentBatch.takeFirst();
100 void QtUiMessageProcessor::updateProgress(bool start) {
102 _progressTimer.start();
103 emit progressUpdated(_msgsProcessed, _msgCount);
105 if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
106 _progressTimer.restart();
107 emit progressUpdated(_msgsProcessed, _msgCount);
112 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
113 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
116 //NotificationSettings notificationSettings;
117 const Network *net = Client::network(msg.bufferInfo().networkId());
118 if(net && !net->myNick().isEmpty()) {
119 QStringList nickList;
120 if(_highlightNick == NotificationSettings::CurrentNick) {
121 nickList << net->myNick();
122 } else if(_highlightNick == NotificationSettings::AllNicks) {
123 const Identity *myIdentity = Client::identity(net->identity());
125 nickList = myIdentity->nicks();
127 foreach(QString nickname, nickList) {
128 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
129 if(nickRegExp.exactMatch(msg.contents())) {
130 msg.setFlags(msg.flags() | Message::Highlight);
135 for(int i = 0; i < _highlightRules.count(); i++) {
136 const HighlightRule &rule = _highlightRules[i];
142 userRegExp = QRegExp(rule.name, rule.caseSensitive);
144 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
146 if(userRegExp.exactMatch(msg.contents())) {
147 msg.setFlags(msg.flags() | Message::Highlight);
154 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
155 QVariantList varList = variant.toList();
157 _highlightRules.clear();
158 QVariantList::const_iterator iter = varList.constBegin();
159 QVariantList::const_iterator iterEnd = varList.constEnd();
160 while(iter != iterEnd) {
162 _highlightRules << HighlightRule(rule["name"].toString(),
163 rule["enable"].toBool(),
164 rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
165 rule["regex"].toBool());
170 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
171 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();