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 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
31 _processMode = TimerBased;
32 _processTimer.setInterval(0);
33 connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
36 void QtUiMessageProcessor::reset() {
37 if(processMode() == TimerBased) {
38 if(_processTimer.isActive()) _processTimer.stop();
40 _currentBatch.clear();
41 _processQueue.clear();
45 void QtUiMessageProcessor::process(Message &msg) {
46 checkForHighlight(msg);
47 Client::messageModel()->insertMessage(msg);
51 void QtUiMessageProcessor::process(QList<Message> &msgs) {
52 _processQueue.append(msgs);
53 if(!isProcessing()) startProcessing();
56 void QtUiMessageProcessor::startProcessing() {
57 if(processMode() == TimerBased) {
58 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
60 if(!_processTimer.isActive()) _processTimer.start();
64 void QtUiMessageProcessor::processNextMessage() {
65 if(_currentBatch.isEmpty()) {
66 if(_processQueue.isEmpty()) {
71 _currentBatch = _processQueue.takeFirst();
73 Message msg = _currentBatch.takeFirst();
77 // TODO optimize checkForHighlight
78 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
79 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
82 NotificationSettings notificationSettings;
83 const Network *net = Client::network(msg.bufferInfo().networkId());
84 if(net && !net->myNick().isEmpty()) {
86 if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
87 nickList << net->myNick();
88 } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
89 const Identity *myIdentity = Client::identity(net->identity());
91 nickList = myIdentity->nicks();
93 foreach(QString nickname, nickList) {
94 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
95 if(nickRegExp.exactMatch(msg.contents())) {
96 msg.setFlags(msg.flags() | Message::Highlight);
101 foreach(QVariant highlight, notificationSettings.highlightList()) {
102 QVariantMap highlightRule = highlight.toMap();
103 if(!highlightRule["enable"].toBool())
105 Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
106 QString name = highlightRule["name"].toString();
108 if(highlightRule["regex"].toBool()) {
109 userRegExp = QRegExp(name, caseSensitivity);
111 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
113 if(userRegExp.exactMatch(msg.contents())) {
114 msg.setFlags(msg.flags() | Message::Highlight);