Improve ChatMonitorFilter to use Message::Backlog rather than the timestamp
[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 const int progressUpdateDelay = 100;  // ms between progress signal updates
30
31 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
32   _msgsProcessed = _msgCount = 0;
33   _processing = false;
34   _processMode = TimerBased;
35   _processTimer.setInterval(0);
36   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
37 }
38
39 void QtUiMessageProcessor::reset() {
40   if(processMode() == TimerBased) {
41     if(_processTimer.isActive()) _processTimer.stop();
42     _processing = false;
43     _currentBatch.clear();
44     _processQueue.clear();
45   }
46 }
47
48 void QtUiMessageProcessor::process(Message &msg) {
49   checkForHighlight(msg);
50   Client::messageModel()->insertMessage(msg);
51   postProcess(msg);
52 }
53
54 void QtUiMessageProcessor::process(QList<Message> &msgs) {
55   _processQueue.append(msgs);
56   _msgCount += msgs.count();
57   if(!isProcessing()) startProcessing();
58   else updateProgress();
59 }
60
61 void QtUiMessageProcessor::startProcessing() {
62   if(processMode() == TimerBased) {
63     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
64     _processing = true;
65     _msgsProcessed = 0;
66     _msgCount = _currentBatch.count();
67     foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
68     updateProgress();
69     if(!_processTimer.isActive()) _processTimer.start();
70   }
71 }
72
73 void QtUiMessageProcessor::processNextMessage() {
74   if(_currentBatch.isEmpty()) {
75     if(_processQueue.isEmpty()) {
76       _processTimer.stop();
77       _processing = false;
78       _msgsProcessed = _msgCount = 0;
79       updateProgress();
80       return;
81     }
82     _currentBatch = _processQueue.takeFirst();
83   }
84   Message msg = _currentBatch.takeFirst();
85   process(msg);
86   _msgsProcessed++;
87   updateProgress();
88 }
89
90 void QtUiMessageProcessor::updateProgress(bool start) {
91   if(start) {
92     _progressTimer.start();
93     emit progressUpdated(_msgsProcessed, _msgCount);
94   } else {
95     if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
96       _progressTimer.restart();
97       emit progressUpdated(_msgsProcessed, _msgCount);
98     }
99   }
100 }
101
102 // TODO optimize checkForHighlight
103 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
104   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
105     return;
106
107   NotificationSettings notificationSettings;
108   const Network *net = Client::network(msg.bufferInfo().networkId());
109   if(net && !net->myNick().isEmpty()) {
110     QStringList nickList;
111     if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
112       nickList << net->myNick();
113     } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
114       const Identity *myIdentity = Client::identity(net->identity());
115       if(myIdentity)
116         nickList = myIdentity->nicks();
117     }
118     foreach(QString nickname, nickList) {
119       QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
120       if(nickRegExp.exactMatch(msg.contents())) {
121         msg.setFlags(msg.flags() | Message::Highlight);
122         return;
123       }
124     }
125
126     foreach(QVariant highlight, notificationSettings.highlightList()) {
127       QVariantMap highlightRule = highlight.toMap();
128       if(!highlightRule["enable"].toBool())
129         continue;
130       Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
131       QString name = highlightRule["name"].toString();
132       QRegExp userRegExp;
133       if(highlightRule["regex"].toBool()) {
134         userRegExp = QRegExp(name, caseSensitivity);
135       } else {
136         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
137       }
138       if(userRegExp.exactMatch(msg.contents())) {
139         msg.setFlags(msg.flags() | Message::Highlight);
140         return;
141       }
142     }
143   }
144 }