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 _processQueue.append(msgs);
65 _msgCount += msgs.count();
66 if(!isProcessing()) startProcessing();
67 else updateProgress();
70 void QtUiMessageProcessor::startProcessing() {
71 if(processMode() == TimerBased) {
72 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
75 _msgCount = _currentBatch.count();
76 foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
78 if(!_processTimer.isActive()) _processTimer.start();
82 void QtUiMessageProcessor::processNextMessage() {
83 if(_currentBatch.isEmpty()) {
84 if(_processQueue.isEmpty()) {
87 _msgsProcessed = _msgCount = 0;
91 _currentBatch = _processQueue.takeFirst();
93 Message msg = _currentBatch.takeFirst();
99 void QtUiMessageProcessor::updateProgress(bool start) {
101 _progressTimer.start();
102 emit progressUpdated(_msgsProcessed, _msgCount);
104 if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
105 _progressTimer.restart();
106 emit progressUpdated(_msgsProcessed, _msgCount);
111 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
112 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
115 //NotificationSettings notificationSettings;
116 const Network *net = Client::network(msg.bufferInfo().networkId());
117 if(net && !net->myNick().isEmpty()) {
118 QStringList nickList;
119 if(_highlightNick == NotificationSettings::CurrentNick) {
120 nickList << net->myNick();
121 } else if(_highlightNick == NotificationSettings::AllNicks) {
122 const Identity *myIdentity = Client::identity(net->identity());
124 nickList = myIdentity->nicks();
126 foreach(QString nickname, nickList) {
127 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
128 if(nickRegExp.exactMatch(msg.contents())) {
129 msg.setFlags(msg.flags() | Message::Highlight);
134 for(int i = 0; i < _highlightRules.count(); i++) {
135 const HighlightRule &rule = _highlightRules[i];
141 userRegExp = QRegExp(rule.name, rule.caseSensitive);
143 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
145 if(userRegExp.exactMatch(msg.contents())) {
146 msg.setFlags(msg.flags() | Message::Highlight);
153 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
154 QVariantList varList = variant.toList();
156 _highlightRules.clear();
157 QVariantList::const_iterator iter = varList.constBegin();
158 QVariantList::const_iterator iterEnd = varList.constEnd();
159 while(iter != iterEnd) {
161 _highlightRules << HighlightRule(rule["name"].toString(),
162 rule["enable"].toBool(),
163 rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
164 rule["regex"].toBool());
169 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
170 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();