Make custom highlights work again
[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   _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
40   _highlightNick = notificationSettings.highlightNick();
41   highlightListChanged(notificationSettings.highlightList());
42   notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
43   notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
44   notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
45
46   _processTimer.setInterval(0);
47   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
48 }
49
50 void QtUiMessageProcessor::reset() {
51   if(processMode() == TimerBased) {
52     if(_processTimer.isActive()) _processTimer.stop();
53     _processing = false;
54     _currentBatch.clear();
55     _processQueue.clear();
56   }
57 }
58
59 void QtUiMessageProcessor::process(Message &msg) {
60   checkForHighlight(msg);
61   Client::messageModel()->insertMessage(msg);
62   postProcess(msg);
63 }
64
65 void QtUiMessageProcessor::process(QList<Message> &msgs) {
66   QList<Message>::iterator msgIter = msgs.begin();
67   QList<Message>::iterator msgIterEnd = msgs.end();
68   while(msgIter != msgIterEnd) {
69     checkForHighlight(*msgIter);
70     postProcess(*msgIter);
71     msgIter++;
72   }
73   Client::messageModel()->insertMessages(msgs);
74   return;
75
76
77   if(msgs.isEmpty()) return;
78   _processQueue.append(msgs);
79   _msgCount += msgs.count();
80   if(!isProcessing()) startProcessing();
81   else updateProgress();
82 }
83
84 void QtUiMessageProcessor::startProcessing() {
85   if(processMode() == TimerBased) {
86     if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
87     _processing = true;
88     _msgsProcessed = 0;
89     _msgCount = _currentBatch.count();
90     foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
91     updateProgress();
92     if(!_processTimer.isActive()) _processTimer.start();
93   }
94 }
95
96 void QtUiMessageProcessor::processNextMessage() {
97   if(_currentBatch.isEmpty()) {
98     if(_processQueue.isEmpty()) {
99       _processTimer.stop();
100       _processing = false;
101       _msgsProcessed = _msgCount = 0;
102       updateProgress();
103       return;
104     }
105     _currentBatch = _processQueue.takeFirst();
106   }
107   Message msg = _currentBatch.takeFirst();
108   process(msg);
109   _msgsProcessed++;
110   updateProgress();
111 }
112
113 void QtUiMessageProcessor::updateProgress(bool start) {
114   if(start) {
115     _progressTimer.start();
116     emit progressUpdated(_msgsProcessed, _msgCount);
117   } else {
118     if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
119       _progressTimer.restart();
120       emit progressUpdated(_msgsProcessed, _msgCount);
121     }
122   }
123 }
124
125 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
126   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
127     return;
128
129   // TODO: Cache this (per network)
130   const Network *net = Client::network(msg.bufferInfo().networkId());
131   if(net && !net->myNick().isEmpty()) {
132     QStringList nickList;
133     if(_highlightNick == NotificationSettings::CurrentNick) {
134       nickList << net->myNick();
135     } else if(_highlightNick == NotificationSettings::AllNicks) {
136       const Identity *myIdentity = Client::identity(net->identity());
137       if(myIdentity)
138         nickList = myIdentity->nicks();
139     }
140     foreach(QString nickname, nickList) {
141       QRegExp nickRegExp("\\b" + QRegExp::escape(nickname) + "\\b",
142                           _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
143       if(nickRegExp.indexIn(msg.contents()) >= 0) {
144         msg.setFlags(msg.flags() | Message::Highlight);
145         return;
146       }
147     }
148
149     for(int i = 0; i < _highlightRules.count(); i++) {
150       const HighlightRule &rule = _highlightRules.at(i);
151       if(!rule.isEnabled)
152         continue;
153
154       QRegExp userRegExp;
155       if(rule.isRegExp) {
156         userRegExp = QRegExp(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
157       } else {
158         userRegExp = QRegExp("\\b" + QRegExp::escape(rule.name) + "\\b", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
159       }
160       if(userRegExp.exactMatch(msg.contents())) {
161         msg.setFlags(msg.flags() | Message::Highlight);
162         return;
163       }
164     }
165   }
166 }
167
168 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
169   _nicksCaseSensitive = variant.toBool();
170 }
171
172 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
173   QVariantList varList = variant.toList();
174
175   _highlightRules.clear();
176   QVariantList::const_iterator iter = varList.constBegin();
177   while(iter != varList.constEnd()) {
178     QVariantMap rule = iter->toMap();
179     _highlightRules << HighlightRule(rule["Name"].toString(),
180                                      rule["Enable"].toBool(),
181                                      rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
182                                      rule["RegEx"].toBool());
183     iter++;
184   }
185 }
186
187 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
188   _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
189 }