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);
134 if(rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
135 if(rule.chanName.startsWith("!")) {
136 QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
137 if(rx.exactMatch(msg.bufferInfo().bufferName()))
140 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
141 if(!rx.exactMatch(msg.bufferInfo().bufferName()))
148 rx = QRegExp(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
150 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
152 bool match = (rx.indexIn(msg.contents()) >= 0);
154 msg.setFlags(msg.flags() | Message::Highlight);
161 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
162 _nicksCaseSensitive = variant.toBool();
165 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
166 QVariantList varList = variant.toList();
168 _highlightRules.clear();
169 QVariantList::const_iterator iter = varList.constBegin();
170 while(iter != varList.constEnd()) {
171 QVariantMap rule = iter->toMap();
172 _highlightRules << HighlightRule(rule["Name"].toString(),
173 rule["Enable"].toBool(),
174 rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
175 rule["RegEx"].toBool(),
176 rule["Chan"].toString());
181 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
182 _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();