Enable plain old QTimer-based asynchronous message processing. More fancy stuff will...
[quassel.git] / src / qtui / qtuimessageprocessor.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 by the Quassel Project                          *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
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.                                           *
9 *                                                                         *
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.                          *
14 *                                                                         *
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 ***************************************************************************/
20
21 #include "qtuimessageprocessor.h"
22
23 #include "client.h"
24 #include "clientsettings.h"
25 #include "identity.h"
26 #include "messagemodel.h"
27 #include "network.h"
28
29 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
30   _processing = false;
31   _processMode = TimerBased;
32   _processTimer.setInterval(0);
33   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
34 }
35
36 void QtUiMessageProcessor::process(Message &msg) {
37   checkForHighlight(msg);
38   Client::messageModel()->insertMessage(msg);
39   postProcess(msg);
40 }
41
42 void QtUiMessageProcessor::process(QList<Message> &msgs) {
43   _processQueue.append(msgs);
44   if(!isProcessing()) startProcessing();
45 }
46
47 void QtUiMessageProcessor::startProcessing() {
48   if(processMode() == TimerBased) {
49     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
50     _processing = true;
51     if(!_processTimer.isActive()) _processTimer.start();
52   }
53 }
54
55 void QtUiMessageProcessor::processNextMessage() {
56   if(_currentBatch.isEmpty()) {
57     if(_processQueue.isEmpty()) {
58       _processTimer.stop();
59       _processing = false;
60       return;
61     }
62     _currentBatch = _processQueue.takeFirst();
63   }
64   Message msg = _currentBatch.takeFirst();
65   process(msg);
66 }
67
68 // TODO optimize checkForHighlight
69 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
70   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
71     return;
72
73   NotificationSettings notificationSettings;
74   const Network *net = Client::network(msg.bufferInfo().networkId());
75   if(net && !net->myNick().isEmpty()) {
76     QStringList nickList;
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());
81       if(myIdentity)
82         nickList = myIdentity->nicks();
83     }
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);
88         return;
89       }
90     }
91
92     foreach(QVariant highlight, notificationSettings.highlightList()) {
93       QVariantMap highlightRule = highlight.toMap();
94       if(!highlightRule["enable"].toBool())
95         continue;
96       Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
97       QString name = highlightRule["name"].toString();
98       QRegExp userRegExp;
99       if(highlightRule["regex"].toBool()) {
100         userRegExp = QRegExp(name, caseSensitivity);
101       } else {
102         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
103       }
104       if(userRegExp.exactMatch(msg.contents())) {
105         msg.setFlags(msg.flags() | Message::Highlight);
106         return;
107       }
108     }
109   }
110 }