no longer requesting backlog for all buffers but only for active bufferviews
[quassel.git] / src / client / clientbacklogmanager.cpp
index 8ccaa4b..afd7e1f 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,12 +35,8 @@ ClientBacklogManager::ClientBacklogManager(QObject *parent)
 {
 }
 
-void ClientBacklogManager::receiveBacklog(BufferId bufferId, int limit, int offset, QVariantList msgs) {
-  Q_UNUSED(limit)
-  Q_UNUSED(offset)
-
-  if(msgs.isEmpty())
-    return;
+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)
 
   emit messagesReceived(bufferId, msgs.count());
 
@@ -51,9 +47,12 @@ void ClientBacklogManager::receiveBacklog(BufferId bufferId, int limit, int offs
     msglist << msg;
   }
 
+  _backlogReceived << bufferId;
+
   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();
     }
   } else {
@@ -61,6 +60,19 @@ void ClientBacklogManager::receiveBacklog(BufferId bufferId, int limit, int offs
   }
 }
 
+void ClientBacklogManager::receiveBacklogAll(MsgId first, MsgId last, int limit, int additional, QVariantList msgs) {
+  Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)
+
+  MessageList msglist;
+  foreach(QVariant v, msgs) {
+    Message msg = v.value<Message>();
+    msg.setFlags(msg.flags() | Message::Backlog);
+    msglist << msg;
+  }
+
+  dispatchMessages(msglist);
+}
+
 void ClientBacklogManager::requestInitialBacklog() {
   if(_requester) {
     qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
@@ -70,20 +82,52 @@ void ClientBacklogManager::requestInitialBacklog() {
   BacklogSettings settings;
   switch(settings.requesterType()) {
   case BacklogRequester::GlobalUnread:
+    _requester = new GlobalUnreadBacklogRequester(this);
+    break;
   case BacklogRequester::PerBufferUnread:
+    _requester = new PerBufferUnreadBacklogRequester(this);
+    break;
   case BacklogRequester::PerBufferFixed:
   default:
     _requester = new FixedBacklogRequester(this);
   };
 
   _requester->requestBacklog();
+  if(_requester->isBuffering()) {
+    updateProgress(0, _requester->totalBuffers());
+  }
 }
 
-void ClientBacklogManager::stopBuffering() {
+void ClientBacklogManager::checkForBacklog(const BufferId bufferId) {
+  if(_backlogReceived.contains(bufferId))
+    return;
+
+  QList<BufferId> bufferIds;
+  bufferIds << bufferId;
+  checkForBacklog(bufferIds);
+}
+
+void ClientBacklogManager::checkForBacklog(const QList<BufferId> &bufferIds) {
   Q_ASSERT(_requester);
+  switch(_requester->type()) {
+  case BacklogRequester::GlobalUnread:
+    break;
+  case BacklogRequester::PerBufferUnread:
+  case BacklogRequester::PerBufferFixed:
+  default:
+    {
+      QList<BufferId> buffers;
+      foreach(BufferId bufferId, bufferIds)
+        if(!_backlogReceived.contains(bufferId))
+          buffers << bufferId;
+      _requester->requestBacklog(buffers);
+    }
+  };
+}
 
+void ClientBacklogManager::stopBuffering() {
+  Q_ASSERT(_requester);
   dispatchMessages(_requester->bufferedMessages(), true);
-  reset();
 }
 
 bool ClientBacklogManager::isBuffering() {
@@ -91,6 +135,9 @@ bool ClientBacklogManager::isBuffering() {
 }
 
 void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort) {
+  if(messages.isEmpty())
+    return;
+
   MessageList msgs = messages;
 
   clock_t start_t = clock();
@@ -99,10 +146,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;
+  _backlogReceived.clear();
 }