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 QList<Message>::iterator msgIter = msgs.begin();
65 QList<Message>::iterator msgIterEnd = msgs.end();
66 while(msgIter != msgIterEnd) {
67 checkForHighlight(*msgIter);
68 postProcess(*msgIter);
71 Client::messageModel()->insertMessages(msgs);
75 if(msgs.isEmpty()) return;
76 _processQueue.append(msgs);
77 _msgCount += msgs.count();
78 if(!isProcessing()) startProcessing();
79 else updateProgress();
82 void QtUiMessageProcessor::startProcessing() {
83 if(processMode() == TimerBased) {
84 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
87 _msgCount = _currentBatch.count();
88 foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
90 if(!_processTimer.isActive()) _processTimer.start();
94 void QtUiMessageProcessor::processNextMessage() {
95 if(_currentBatch.isEmpty()) {
96 if(_processQueue.isEmpty()) {
99 _msgsProcessed = _msgCount = 0;
103 _currentBatch = _processQueue.takeFirst();
105 Message msg = _currentBatch.takeFirst();
111 void QtUiMessageProcessor::updateProgress(bool start) {
113 _progressTimer.start();
114 emit progressUpdated(_msgsProcessed, _msgCount);
116 if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
117 _progressTimer.restart();
118 emit progressUpdated(_msgsProcessed, _msgCount);
123 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
124 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
127 //NotificationSettings notificationSettings;
128 const Network *net = Client::network(msg.bufferInfo().networkId());
129 if(net && !net->myNick().isEmpty()) {
130 QStringList nickList;
131 if(_highlightNick == NotificationSettings::CurrentNick) {
132 nickList << net->myNick();
133 } else if(_highlightNick == NotificationSettings::AllNicks) {
134 const Identity *myIdentity = Client::identity(net->identity());
136 nickList = myIdentity->nicks();
138 foreach(QString nickname, nickList) {
139 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
140 if(nickRegExp.exactMatch(msg.contents())) {
141 msg.setFlags(msg.flags() | Message::Highlight);
146 for(int i = 0; i < _highlightRules.count(); i++) {
147 const HighlightRule &rule = _highlightRules[i];
153 userRegExp = QRegExp(rule.name, rule.caseSensitive);
155 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
157 if(userRegExp.exactMatch(msg.contents())) {
158 msg.setFlags(msg.flags() | Message::Highlight);
165 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
166 QVariantList varList = variant.toList();
168 _highlightRules.clear();
169 QVariantList::const_iterator iter = varList.constBegin();
170 QVariantList::const_iterator iterEnd = varList.constEnd();
171 while(iter != iterEnd) {
173 _highlightRules << HighlightRule(rule["name"].toString(),
174 rule["enable"].toBool(),
175 rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
176 rule["regex"].toBool());
181 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
182 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();