core: Replace QList by std::vector in the storage API
[quassel.git] / src / core / corebacklogmanager.cpp
index bb1d93f..deb2ac9 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2015 by the Quassel Project                        *
+ *   Copyright (C) 2005-2019 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  ***************************************************************************/
 
 #include "corebacklogmanager.h"
-#include "core.h"
-#include "coresession.h"
+
+#include <algorithm>
+#include <iterator>
 
 #include <QDebug>
 
-INIT_SYNCABLE_OBJECT(CoreBacklogManager)
-CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
-    : BacklogManager(coreSession),
-    _coreSession(coreSession)
-{
-}
+#include "core.h"
+#include "coresession.h"
 
+CoreBacklogManager::CoreBacklogManager(CoreSession* coreSession)
+    : BacklogManager(coreSession)
+    , _coreSession(coreSession)
+{}
 
 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
 {
     QVariantList backlog;
-    QList<Message> msgList;
-    msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
-
-    QList<Message>::const_iterator msgIter = msgList.constBegin();
-    QList<Message>::const_iterator msgListEnd = msgList.constEnd();
-    while (msgIter != msgListEnd) {
-        backlog << qVariantFromValue(*msgIter);
-        msgIter++;
-    }
+    auto msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
+
+    std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+        return QVariant::fromValue(msg);
+    });
 
     if (additional && limit != 0) {
         MsgId oldestMessage = first;
-        if (!msgList.isEmpty()) {
-            if (msgList.first().msgId() < msgList.last().msgId())
-                oldestMessage = msgList.first().msgId();
+        if (!msgList.empty()) {
+            if (msgList.front().msgId() < msgList.back().msgId())
+                oldestMessage = msgList.front().msgId();
             else
-                oldestMessage = msgList.last().msgId();
+                oldestMessage = msgList.back().msgId();
         }
 
         if (first != -1) {
@@ -65,31 +62,61 @@ QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first,
         // 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++;
-            }
+            std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+                return QVariant::fromValue(msg);
+            });
         }
     }
 
     return backlog;
 }
 
+QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
+{
+    QVariantList backlog;
+    auto msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
+
+    std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+        return QVariant::fromValue(msg);
+    });
+
+    if (additional && limit != 0) {
+        MsgId oldestMessage = first;
+        if (!msgList.empty()) {
+            if (msgList.front().msgId() < msgList.back().msgId())
+                oldestMessage = msgList.front().msgId();
+            else
+                oldestMessage = msgList.back().msgId();
+        }
+
+        if (first != -1) {
+            last = first;
+        }
+        else {
+            last = oldestMessage;
+        }
+
+        // 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::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags});
+            std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+                return QVariant::fromValue(msg);
+            });
+        }
+    }
+
+    return backlog;
+}
 
 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
 {
     QVariantList backlog;
-    QList<Message> msgList;
-    msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
-
-    QList<Message>::const_iterator msgIter = msgList.constBegin();
-    QList<Message>::const_iterator msgListEnd = msgList.constEnd();
-    while (msgIter != msgListEnd) {
-        backlog << qVariantFromValue(*msgIter);
-        msgIter++;
-    }
+    auto msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
+
+    std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+        return QVariant::fromValue(msg);
+    });
 
     if (additional) {
         if (first != -1) {
@@ -97,20 +124,48 @@ QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int
         }
         else {
             last = -1;
-            if (!msgList.isEmpty()) {
-                if (msgList.first().msgId() < msgList.last().msgId())
-                    last = msgList.first().msgId();
+            if (!msgList.empty()) {
+                if (msgList.front().msgId() < msgList.back().msgId())
+                    last = msgList.front().msgId();
                 else
-                    last = msgList.last().msgId();
+                    last = msgList.back().msgId();
             }
         }
         msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
-        msgIter = msgList.constBegin();
-        msgListEnd = msgList.constEnd();
-        while (msgIter != msgListEnd) {
-            backlog << qVariantFromValue(*msgIter);
-            msgIter++;
+        std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+            return QVariant::fromValue(msg);
+        });
+    }
+
+    return backlog;
+}
+
+QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type, int flags)
+{
+    QVariantList backlog;
+    auto msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
+
+    std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+        return QVariant::fromValue(msg);
+    });
+
+    if (additional) {
+        if (first != -1) {
+            last = first;
+        }
+        else {
+            last = -1;
+            if (!msgList.empty()) {
+                if (msgList.front().msgId() < msgList.back().msgId())
+                    last = msgList.front().msgId();
+                else
+                    last = msgList.back().msgId();
+            }
         }
+        msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
+        std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
+            return QVariant::fromValue(msg);
+        });
     }
 
     return backlog;