Fix initial backlog for buffers with no new messages
[quassel.git] / src / core / corebacklogmanager.cpp
index 5e3724f..9ef4b89 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  *
@@ -24,6 +24,7 @@
 
 #include <QDebug>
 
+INIT_SYNCABLE_OBJECT(CoreBacklogManager)
 CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
   : BacklogManager(coreSession),
     _coreSession(coreSession)
@@ -42,24 +43,31 @@ QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first,
     msgIter++;
   }
 
-  if(additional) {
+  if(additional && limit != 0) {
+    MsgId oldestMessage = first;
+    if(!msgList.isEmpty()) {
+      if(msgList.first().msgId() < msgList.last().msgId())
+        oldestMessage = msgList.first().msgId();
+      else
+        oldestMessage = msgList.last().msgId();
+    }
+
     if(first != -1) {
       last = first;
     } else {
-      last = -1;
-      if(!msgList.isEmpty()) {
-       if(msgList.first().msgId() < msgList.last().msgId())
-         last = msgList.first().msgId();
-       else
-         last = msgList.last().msgId();
-      }
+      last = oldestMessage;
     }
-    msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
-    msgIter = msgList.constBegin();
-    msgListEnd = msgList.constEnd();
-    while(msgIter != msgListEnd) {
-      backlog << qVariantFromValue(*msgIter);
-      msgIter++;
+
+    // only fetch additional messages if they continue seemlessly
+    // that is, if the list of messages is not truncated by the limit
+    if(last == oldestMessage) {
+      msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
+      msgIter = msgList.constBegin();
+      msgListEnd = msgList.constEnd();
+      while(msgIter != msgListEnd) {
+        backlog << qVariantFromValue(*msgIter);
+        msgIter++;
+      }
     }
   }
 
@@ -67,7 +75,6 @@ QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first,
 }
 
 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional) {
-  qDebug() << "CoreBacklogManager::requestBacklogAll" << first << last << limit << additional;
   QVariantList backlog;
   QList<Message> msgList;
   msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);