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::process(Message &msg) {
37 checkForHighlight(msg);
38 Client::messageModel()->insertMessage(msg);
42 void QtUiMessageProcessor::process(QList<Message> &msgs) {
43 _processQueue.append(msgs);
44 if(!isProcessing()) startProcessing();
47 void QtUiMessageProcessor::startProcessing() {
48 if(processMode() == TimerBased) {
49 if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
51 if(!_processTimer.isActive()) _processTimer.start();
55 void QtUiMessageProcessor::processNextMessage() {
56 if(_currentBatch.isEmpty()) {
57 if(_processQueue.isEmpty()) {
62 _currentBatch = _processQueue.takeFirst();
64 Message msg = _currentBatch.takeFirst();
68 // TODO optimize checkForHighlight
69 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
70 if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
73 NotificationSettings notificationSettings;
74 const Network *net = Client::network(msg.bufferInfo().networkId());
75 if(net && !net->myNick().isEmpty()) {
77 if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
78 nickList << net->myNick();
79 } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
80 const Identity *myIdentity = Client::identity(net->identity());
82 nickList = myIdentity->nicks();
84 foreach(QString nickname, nickList) {
85 QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
86 if(nickRegExp.exactMatch(msg.contents())) {
87 msg.setFlags(msg.flags() | Message::Highlight);
92 foreach(QVariant highlight, notificationSettings.highlightList()) {
93 QVariantMap highlightRule = highlight.toMap();
94 if(!highlightRule["enable"].toBool())
96 Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
97 QString name = highlightRule["name"].toString();
99 if(highlightRule["regex"].toBool()) {
100 userRegExp = QRegExp(name, caseSensitivity);
102 userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
104 if(userRegExp.exactMatch(msg.contents())) {
105 msg.setFlags(msg.flags() | Message::Highlight);