Fix crash on end of backlog (cf. BR 248)
[quassel.git] / src / qtui / qtuimessageprocessor.cpp
index 54ee570..46a7f61 100644 (file)
 #include "messagemodel.h"
 #include "network.h"
 
-QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
+const int progressUpdateDelay = 100;  // ms between progress signal updates
 
+QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
+  : AbstractMessageProcessor(parent),
+    _processing(false),
+    _processMode(TimerBased),
+    _msgsProcessed(0),
+    _msgCount(0)
+{
+  NotificationSettings notificationSettings;
+  _highlightNick = notificationSettings.highlightNick();
+  highlightListChanged(notificationSettings.highlightList());
+  notificationSettings.notify("highlightList", this, SLOT(highlightListChanged(const QVariant &)));
+  notificationSettings.notify("highlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
+  
+  _processTimer.setInterval(0);
+  connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
+}
 
+void QtUiMessageProcessor::reset() {
+  if(processMode() == TimerBased) {
+    if(_processTimer.isActive()) _processTimer.stop();
+    _processing = false;
+    _currentBatch.clear();
+    _processQueue.clear();
+  }
 }
 
-void QtUiMessageProcessor::processMessage(Message &msg) {
+void QtUiMessageProcessor::process(Message &msg) {
   checkForHighlight(msg);
   Client::messageModel()->insertMessage(msg);
+  postProcess(msg);
 }
 
-void QtUiMessageProcessor::processMessages(QList<Message> &msgs) {
-  foreach(Message msg, msgs) {
-    checkForHighlight(msg);
-    Client::messageModel()->insertMessage(msg);
+void QtUiMessageProcessor::process(QList<Message> &msgs) {
+  if(msgs.isEmpty()) return;
+  _processQueue.append(msgs);
+  _msgCount += msgs.count();
+  if(!isProcessing()) startProcessing();
+  else updateProgress();
+}
+
+void QtUiMessageProcessor::startProcessing() {
+  if(processMode() == TimerBased) {
+    if(_currentBatch.isEmpty() && _processQueue.isEmpty()) return;
+    _processing = true;
+    _msgsProcessed = 0;
+    _msgCount = _currentBatch.count();
+    foreach(QList<Message> msglist, _processQueue) _msgCount += msglist.count();
+    updateProgress();
+    if(!_processTimer.isActive()) _processTimer.start();
+  }
+}
+
+void QtUiMessageProcessor::processNextMessage() {
+  if(_currentBatch.isEmpty()) {
+    if(_processQueue.isEmpty()) {
+      _processTimer.stop();
+      _processing = false;
+      _msgsProcessed = _msgCount = 0;
+      updateProgress();
+      return;
+    }
+    _currentBatch = _processQueue.takeFirst();
+  }
+  Message msg = _currentBatch.takeFirst();
+  process(msg);
+  _msgsProcessed++;
+  updateProgress();
+}
+
+void QtUiMessageProcessor::updateProgress(bool start) {
+  if(start) {
+    _progressTimer.start();
+    emit progressUpdated(_msgsProcessed, _msgCount);
+  } else {
+    if(_msgCount == 0 || _progressTimer.elapsed() >= progressUpdateDelay) {
+      _progressTimer.restart();
+      emit progressUpdated(_msgsProcessed, _msgCount);
+    }
   }
 }
 
-// TODO optimize checkForHighlight
 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
     return;
 
-  NotificationSettings notificationSettings;
+  //NotificationSettings notificationSettings;
   const Network *net = Client::network(msg.bufferInfo().networkId());
   if(net && !net->myNick().isEmpty()) {
     QStringList nickList;
-    if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
+    if(_highlightNick == NotificationSettings::CurrentNick) {
       nickList << net->myNick();
-    } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
+    } else if(_highlightNick == NotificationSettings::AllNicks) {
       const Identity *myIdentity = Client::identity(net->identity());
       if(myIdentity)
         nickList = myIdentity->nicks();
@@ -67,17 +132,16 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) {
       }
     }
 
-    foreach(QVariant highlight, notificationSettings.highlightList()) {
-      QVariantMap highlightRule = highlight.toMap();
-      if(!highlightRule["enable"].toBool())
-        continue;
-      Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
-      QString name = highlightRule["name"].toString();
+    for(int i = 0; i < _highlightRules.count(); i++) {
+      const HighlightRule &rule = _highlightRules[i];
+      if(!rule.isEnabled)
+       continue;
+
       QRegExp userRegExp;
-      if(highlightRule["regex"].toBool()) {
-        userRegExp = QRegExp(name, caseSensitivity);
+      if(rule.isRegExp) {
+        userRegExp = QRegExp(rule.name, rule.caseSensitive);
       } else {
-        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
+        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
       }
       if(userRegExp.exactMatch(msg.contents())) {
         msg.setFlags(msg.flags() | Message::Highlight);
@@ -86,3 +150,23 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) {
     }
   }
 }
+
+void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
+  QVariantList varList = variant.toList();
+
+  _highlightRules.clear();
+  QVariantList::const_iterator iter = varList.constBegin();
+  QVariantList::const_iterator iterEnd = varList.constEnd();
+  while(iter != iterEnd) {
+    QVariantMap rule;
+    _highlightRules << HighlightRule(rule["name"].toString(),
+                                    rule["enable"].toBool(),
+                                    rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
+                                    rule["regex"].toBool());
+    iter++;
+  }
+}
+
+void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
+  _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
+}