Fix crash on end of backlog (cf. BR 248)
[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)
32   : AbstractMessageProcessor(parent),
33     _processing(false),
34     _processMode(TimerBased),
35     _msgsProcessed(0),
36     _msgCount(0)
37 {
38   NotificationSettings notificationSettings;
39   _highlightNick = notificationSettings.highlightNick();
40   highlightListChanged(notificationSettings.highlightList());
41   notificationSettings.notify("highlightList", this, SLOT(highlightListChanged(const QVariant &)));
42   notificationSettings.notify("highlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
43   
44   _processTimer.setInterval(0);
45   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
46 }
47
48 void QtUiMessageProcessor::reset() {
49   if(processMode() == TimerBased) {
50     if(_processTimer.isActive()) _processTimer.stop();
51     _processing = false;
52     _currentBatch.clear();
53     _processQueue.clear();
54   }
55 }
56
57 void QtUiMessageProcessor::process(Message &msg) {
58   checkForHighlight(msg);
59   Client::messageModel()->insertMessage(msg);
60   postProcess(msg);
61 }
62
63 void QtUiMessageProcessor::process(QList<Message> &msgs) {
64   if(msgs.isEmpty()) return;
65   _processQueue.append(msgs);
66   _msgCount += msgs.count();
67   if(!isProcessing()) startProcessing();
68   else updateProgress();
69 }
70
71 void QtUiMessageProcessor::startProcessing() {
72   if(processMode() == TimerBased) {
73     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
74     _processing = true;
75     _msgsProcessed = 0;
76     _msgCount = _currentBatch.count();
77     foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
78     updateProgress();
79     if(!_processTimer.isActive()) _processTimer.start();
80   }
81 }
82
83 void QtUiMessageProcessor::processNextMessage() {
84   if(_currentBatch.isEmpty()) {
85     if(_processQueue.isEmpty()) {
86       _processTimer.stop();
87       _processing = false;
88       _msgsProcessed = _msgCount = 0;
89       updateProgress();
90       return;
91     }
92     _currentBatch = _processQueue.takeFirst();
93   }
94   Message msg = _currentBatch.takeFirst();
95   process(msg);
96   _msgsProcessed++;
97   updateProgress();
98 }
99
100 void QtUiMessageProcessor::updateProgress(bool start) {
101   if(start) {
102     _progressTimer.start();
103     emit progressUpdated(_msgsProcessed, _msgCount);
104   } else {
105     if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
106       _progressTimer.restart();
107       emit progressUpdated(_msgsProcessed, _msgCount);
108     }
109   }
110 }
111
112 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
113   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
114     return;
115
116   //NotificationSettings notificationSettings;
117   const Network *net = Client::network(msg.bufferInfo().networkId());
118   if(net && !net->myNick().isEmpty()) {
119     QStringList nickList;
120     if(_highlightNick == NotificationSettings::CurrentNick) {
121       nickList << net->myNick();
122     } else if(_highlightNick == NotificationSettings::AllNicks) {
123       const Identity *myIdentity = Client::identity(net->identity());
124       if(myIdentity)
125         nickList = myIdentity->nicks();
126     }
127     foreach(QString nickname, nickList) {
128       QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
129       if(nickRegExp.exactMatch(msg.contents())) {
130         msg.setFlags(msg.flags() | Message::Highlight);
131         return;
132       }
133     }
134
135     for(int i = 0; i < _highlightRules.count(); i++) {
136       const HighlightRule &rule = _highlightRules[i];
137       if(!rule.isEnabled)
138         continue;
139
140       QRegExp userRegExp;
141       if(rule.isRegExp) {
142         userRegExp = QRegExp(rule.name, rule.caseSensitive);
143       } else {
144         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
145       }
146       if(userRegExp.exactMatch(msg.contents())) {
147         msg.setFlags(msg.flags() | Message::Highlight);
148         return;
149       }
150     }
151   }
152 }
153
154 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
155   QVariantList varList = variant.toList();
156
157   _highlightRules.clear();
158   QVariantList::const_iterator iter = varList.constBegin();
159   QVariantList::const_iterator iterEnd = varList.constEnd();
160   while(iter != iterEnd) {
161     QVariantMap rule;
162     _highlightRules << HighlightRule(rule["name"].toString(),
163                                      rule["enable"].toBool(),
164                                      rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
165                                      rule["regex"].toBool());
166     iter++;
167   }
168 }
169
170 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
171   _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
172 }