1 /***************************************************************************
2 * Copyright (C) 2005-09 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)
36 NotificationSettings notificationSettings;
37 _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
38 _highlightNick = notificationSettings.highlightNick();
39 highlightListChanged(notificationSettings.highlightList());
40 notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
41 notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
42 notificationSettings.notify("Highlights/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);
60 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);
71 Client::messageModel()->insertMessages(msgs);
75 if(msgs.isEmpty()) return;
76 _processQueue.append(msgs);
81 void QtUiMessageProcessor::startProcessing() {
82 if(processMode() == TimerBased) {
83 if(_currentBatch.isEmpty() && _processQueue.isEmpty())
86 if(!_processTimer.isActive())
87 _processTimer.start();
91 void QtUiMessageProcessor::processNextMessage() {
92 if(_currentBatch.isEmpty()) {
93 if(_processQueue.isEmpty()) {
98 _currentBatch = _processQueue.takeFirst();
100 Message msg = _currentBatch.takeFirst();
104 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
105 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
108 // TODO: Cache this (per network)
109 const Network *net = Client::network(msg.bufferInfo().networkId());
110 if(net && !net->myNick().isEmpty()) {
111 QStringList nickList;
112 if(_highlightNick == NotificationSettings::CurrentNick) {
113 nickList << net->myNick();
114 } else if(_highlightNick == NotificationSettings::AllNicks) {
115 const Identity *myIdentity = Client::identity(net->identity());
117 nickList = myIdentity->nicks();
118 if(!nickList.contains(net->myNick()))
119 nickList.prepend(net->myNick());
121 foreach(QString nickname, nickList) {
122 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
123 if(nickRegExp.indexIn(msg.contents()) >= 0) {
124 msg.setFlags(msg.flags() | Message::Highlight);
129 for(int i = 0; i < _highlightRules.count(); i++) {
130 const HighlightRule &rule = _highlightRules.at(i);
136 QRegExp rx(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
137 match = rx.exactMatch(msg.contents());
139 QRegExp rx("\\W" + QRegExp::escape(rule.name) + "\\W", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
140 match = (rx.indexIn(msg.contents()) >= 0);
143 msg.setFlags(msg.flags() | Message::Highlight);
150 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
151 _nicksCaseSensitive = variant.toBool();
154 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
155 QVariantList varList = variant.toList();
157 _highlightRules.clear();
158 QVariantList::const_iterator iter = varList.constBegin();
159 while(iter != varList.constEnd()) {
160 QVariantMap rule = iter->toMap();
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();