fixing some more issues with initial backlog fetching
[quassel.git] / src / client / clientbacklogmanager.cpp
index 742d305..00d49d2 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -35,6 +35,11 @@ ClientBacklogManager::ClientBacklogManager(QObject *parent)
 {
 }
 
+QVariantList ClientBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional) {
+  _buffersRequested << bufferId;
+  return BacklogManager::requestBacklog(bufferId, first, last, limit, additional);
+}
+
 void ClientBacklogManager::receiveBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, QVariantList msgs) {
   Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)
 
@@ -48,10 +53,10 @@ void ClientBacklogManager::receiveBacklog(BufferId bufferId, MsgId first, MsgId
   }
 
   if(isBuffering()) {
-    if(!_requester->buffer(bufferId, msglist)) {
-      // this was the last part to buffer
+    bool lastPart = !_requester->buffer(bufferId, msglist);
+    updateProgress(_requester->totalBuffers() - _requester->buffersWaiting(), _requester->totalBuffers());
+    if(lastPart) {
       stopBuffering();
-      reset();
     }
   } else {
     dispatchMessages(msglist);
@@ -69,7 +74,6 @@ void ClientBacklogManager::receiveBacklogAll(MsgId first, MsgId last, int limit,
   }
 
   dispatchMessages(msglist);
-  reset();
 }
 
 void ClientBacklogManager::requestInitialBacklog() {
@@ -91,12 +95,45 @@ void ClientBacklogManager::requestInitialBacklog() {
     _requester = new FixedBacklogRequester(this);
   };
 
-  _requester->requestBacklog();
+  _requester->requestInitialBacklog();
+  if(_requester->isBuffering()) {
+    updateProgress(0, _requester->totalBuffers());
+  }
+}
+
+BufferIdList ClientBacklogManager::filterNewBufferIds(const BufferIdList &bufferIds) {
+  BufferIdList newBuffers;
+  QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
+  foreach(BufferId bufferId, bufferIds) {
+    if(_buffersRequested.contains(bufferId) || !availableBuffers.contains(bufferId))
+      continue;
+    newBuffers << bufferId;
+  }
+  return newBuffers;
+}
+
+void ClientBacklogManager::checkForBacklog(const QList<BufferId> &bufferIds) {
+  if(!_requester) {
+    // during client start up this message is to be expected in some situations.
+    qDebug() << "ClientBacklogManager::checkForBacklog(): no active backlog requester (yet?).";
+    return;
+  }
+  switch(_requester->type()) {
+  case BacklogRequester::GlobalUnread:
+    break;
+  case BacklogRequester::PerBufferUnread:
+  case BacklogRequester::PerBufferFixed:
+  default:
+    {
+      BufferIdList buffers = filterNewBufferIds(bufferIds);
+      if(!buffers.isEmpty())
+        _requester->requestBacklog(buffers);
+    }
+  };
 }
 
 void ClientBacklogManager::stopBuffering() {
   Q_ASSERT(_requester);
-
   dispatchMessages(_requester->bufferedMessages(), true);
 }
 
@@ -105,6 +142,9 @@ bool ClientBacklogManager::isBuffering() {
 }
 
 void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort) {
+  if(messages.isEmpty())
+    return;
+
   MessageList msgs = messages;
 
   clock_t start_t = clock();
@@ -113,10 +153,11 @@ void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool so
   Client::messageProcessor()->process(msgs);
   clock_t end_t = clock();
 
-  emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(msgs.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
+  emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(messages.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
 }
 
 void ClientBacklogManager::reset() {
   delete _requester;
   _requester = 0;
+  _buffersRequested.clear();
 }