1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "corebacklogmanager.h"
29 #include "coresession.h"
31 CoreBacklogManager::CoreBacklogManager(CoreSession* coreSession)
32 : BacklogManager(coreSession)
33 , _coreSession(coreSession)
36 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
39 auto msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
41 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
42 return QVariant::fromValue(msg);
45 if (additional && limit != 0) {
46 MsgId oldestMessage = first;
47 if (!msgList.empty()) {
48 if (msgList.front().msgId() < msgList.back().msgId())
49 oldestMessage = msgList.front().msgId();
51 oldestMessage = msgList.back().msgId();
61 // only fetch additional messages if they continue seemlessly
62 // that is, if the list of messages is not truncated by the limit
63 if (last == oldestMessage) {
64 msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
65 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
66 return QVariant::fromValue(msg);
74 QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
77 auto msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
79 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
80 return QVariant::fromValue(msg);
83 if (additional && limit != 0) {
84 MsgId oldestMessage = first;
85 if (!msgList.empty()) {
86 if (msgList.front().msgId() < msgList.back().msgId())
87 oldestMessage = msgList.front().msgId();
89 oldestMessage = msgList.back().msgId();
99 // only fetch additional messages if they continue seemlessly
100 // that is, if the list of messages is not truncated by the limit
101 if (last == oldestMessage) {
102 msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags});
103 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
104 return QVariant::fromValue(msg);
112 QVariantList CoreBacklogManager::requestBacklogForward(BufferId bufferId, MsgId first, MsgId last, int limit, int type, int flags)
114 QVariantList backlog;
115 auto msgList = Core::requestMsgsForward(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
117 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
118 return QVariant::fromValue(msg);
124 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
126 QVariantList backlog;
127 auto msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
129 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
130 return QVariant::fromValue(msg);
139 if (!msgList.empty()) {
140 if (msgList.front().msgId() < msgList.back().msgId())
141 last = msgList.front().msgId();
143 last = msgList.back().msgId();
146 msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
147 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
148 return QVariant::fromValue(msg);
155 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type, int flags)
157 QVariantList backlog;
158 auto msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
160 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
161 return QVariant::fromValue(msg);
170 if (!msgList.empty()) {
171 if (msgList.front().msgId() < msgList.back().msgId())
172 last = msgList.front().msgId();
174 last = msgList.back().msgId();
177 msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
178 std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
179 return QVariant::fromValue(msg);