f06030d3bf89101e8555661e5647731fb01445c1
[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::reset() {
37   if(processMode() == TimerBased) {
38     if(_processTimer.isActive()) _processTimer.stop();
39     _processing = false;
40     _currentBatch.clear();
41     _processQueue.clear();
42   }
43 }
44
45 void QtUiMessageProcessor::process(Message &msg) {
46   checkForHighlight(msg);
47   Client::messageModel()->insertMessage(msg);
48   postProcess(msg);
49 }
50
51 void QtUiMessageProcessor::process(QList<Message> &msgs) {
52   _processQueue.append(msgs);
53   if(!isProcessing()) startProcessing();
54 }
55
56 void QtUiMessageProcessor::startProcessing() {
57   if(processMode() == TimerBased) {
58     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
59     _processing = true;
60     if(!_processTimer.isActive()) _processTimer.start();
61   }
62 }
63
64 void QtUiMessageProcessor::processNextMessage() {
65   if(_currentBatch.isEmpty()) {
66     if(_processQueue.isEmpty()) {
67       _processTimer.stop();
68       _processing = false;
69       return;
70     }
71     _currentBatch = _processQueue.takeFirst();
72   }
73   Message msg = _currentBatch.takeFirst();
74   process(msg);
75 }
76
77 // TODO optimize checkForHighlight
78 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
79   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
80     return;
81
82   NotificationSettings notificationSettings;
83   const Network *net = Client::network(msg.bufferInfo().networkId());
84   if(net && !net->myNick().isEmpty()) {
85     QStringList nickList;
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());
90       if(myIdentity)
91         nickList = myIdentity->nicks();
92     }
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);
97         return;
98       }
99     }
100
101     foreach(QVariant highlight, notificationSettings.highlightList()) {
102       QVariantMap highlightRule = highlight.toMap();
103       if(!highlightRule["enable"].toBool())
104         continue;
105       Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
106       QString name = highlightRule["name"].toString();
107       QRegExp userRegExp;
108       if(highlightRule["regex"].toBool()) {
109         userRegExp = QRegExp(name, caseSensitivity);
110       } else {
111         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
112       }
113       if(userRegExp.exactMatch(msg.contents())) {
114         msg.setFlags(msg.flags() | Message::Highlight);
115         return;
116       }
117     }
118   }
119 }