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