Merge branch 'master' of git@git.quassel-irc.org:quassel
[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   QList<Message>::iterator msgIter = msgs.begin();
65   QList<Message>::iterator msgIterEnd = msgs.end();
66   while(msgIter != msgIterEnd) {
67     checkForHighlight(*msgIter);
68     postProcess(*msgIter);
69     msgIter++;
70   }
71   Client::messageModel()->insertMessages(msgs);
72   return;
73
74   
75   if(msgs.isEmpty()) return;
76   _processQueue.append(msgs);
77   _msgCount += msgs.count();
78   if(!isProcessing()) startProcessing();
79   else updateProgress();
80 }
81
82 void QtUiMessageProcessor::startProcessing() {
83   if(processMode() == TimerBased) {
84     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
85     _processing = true;
86     _msgsProcessed = 0;
87     _msgCount = _currentBatch.count();
88     foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
89     updateProgress();
90     if(!_processTimer.isActive()) _processTimer.start();
91   }
92 }
93
94 void QtUiMessageProcessor::processNextMessage() {
95   if(_currentBatch.isEmpty()) {
96     if(_processQueue.isEmpty()) {
97       _processTimer.stop();
98       _processing = false;
99       _msgsProcessed = _msgCount = 0;
100       updateProgress();
101       return;
102     }
103     _currentBatch = _processQueue.takeFirst();
104   }
105   Message msg = _currentBatch.takeFirst();
106   process(msg);
107   _msgsProcessed++;
108   updateProgress();
109 }
110
111 void QtUiMessageProcessor::updateProgress(bool start) {
112   if(start) {
113     _progressTimer.start();
114     emit progressUpdated(_msgsProcessed, _msgCount);
115   } else {
116     if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
117       _progressTimer.restart();
118       emit progressUpdated(_msgsProcessed, _msgCount);
119     }
120   }
121 }
122
123 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
124   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
125     return;
126
127   //NotificationSettings notificationSettings;
128   const Network *net = Client::network(msg.bufferInfo().networkId());
129   if(net && !net->myNick().isEmpty()) {
130     QStringList nickList;
131     if(_highlightNick == NotificationSettings::CurrentNick) {
132       nickList << net->myNick();
133     } else if(_highlightNick == NotificationSettings::AllNicks) {
134       const Identity *myIdentity = Client::identity(net->identity());
135       if(myIdentity)
136         nickList = myIdentity->nicks();
137     }
138     foreach(QString nickname, nickList) {
139       QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
140       if(nickRegExp.exactMatch(msg.contents())) {
141         msg.setFlags(msg.flags() | Message::Highlight);
142         return;
143       }
144     }
145
146     for(int i = 0; i < _highlightRules.count(); i++) {
147       const HighlightRule &rule = _highlightRules[i];
148       if(!rule.isEnabled)
149         continue;
150
151       QRegExp userRegExp;
152       if(rule.isRegExp) {
153         userRegExp = QRegExp(rule.name, rule.caseSensitive);
154       } else {
155         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
156       }
157       if(userRegExp.exactMatch(msg.contents())) {
158         msg.setFlags(msg.flags() | Message::Highlight);
159         return;
160       }
161     }
162   }
163 }
164
165 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
166   QVariantList varList = variant.toList();
167
168   _highlightRules.clear();
169   QVariantList::const_iterator iter = varList.constBegin();
170   QVariantList::const_iterator iterEnd = varList.constEnd();
171   while(iter != iterEnd) {
172     QVariantMap rule;
173     _highlightRules << HighlightRule(rule["name"].toString(),
174                                      rule["enable"].toBool(),
175                                      rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
176                                      rule["regex"].toBool());
177     iter++;
178   }
179 }
180
181 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
182   _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
183 }