1 /***************************************************************************
2 * Copyright (C) 2005-2018 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"
23 #include "coresession.h"
27 CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
28 : BacklogManager(coreSession),
29 _coreSession(coreSession)
34 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
37 QList<Message> msgList;
38 msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
40 QList<Message>::const_iterator msgIter = msgList.constBegin();
41 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
42 while (msgIter != msgListEnd) {
43 backlog << qVariantFromValue(*msgIter);
47 if (additional && limit != 0) {
48 MsgId oldestMessage = first;
49 if (!msgList.isEmpty()) {
50 if (msgList.first().msgId() < msgList.last().msgId())
51 oldestMessage = msgList.first().msgId();
53 oldestMessage = msgList.last().msgId();
63 // only fetch additional messages if they continue seemlessly
64 // that is, if the list of messages is not truncated by the limit
65 if (last == oldestMessage) {
66 msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
67 msgIter = msgList.constBegin();
68 msgListEnd = msgList.constEnd();
69 while (msgIter != msgListEnd) {
70 backlog << qVariantFromValue(*msgIter);
80 QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
83 QList<Message> msgList;
84 msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
86 QList<Message>::const_iterator msgIter = msgList.constBegin();
87 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
88 while (msgIter != msgListEnd) {
89 backlog << qVariantFromValue(*msgIter);
93 if (additional && limit != 0) {
94 MsgId oldestMessage = first;
95 if (!msgList.isEmpty()) {
96 if (msgList.first().msgId() < msgList.last().msgId())
97 oldestMessage = msgList.first().msgId();
99 oldestMessage = msgList.last().msgId();
106 last = oldestMessage;
109 // only fetch additional messages if they continue seemlessly
110 // that is, if the list of messages is not truncated by the limit
111 if (last == oldestMessage) {
112 msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags});
113 msgIter = msgList.constBegin();
114 msgListEnd = msgList.constEnd();
115 while (msgIter != msgListEnd) {
116 backlog << qVariantFromValue(*msgIter);
126 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
128 QVariantList backlog;
129 QList<Message> msgList;
130 msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
132 QList<Message>::const_iterator msgIter = msgList.constBegin();
133 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
134 while (msgIter != msgListEnd) {
135 backlog << qVariantFromValue(*msgIter);
145 if (!msgList.isEmpty()) {
146 if (msgList.first().msgId() < msgList.last().msgId())
147 last = msgList.first().msgId();
149 last = msgList.last().msgId();
152 msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
153 msgIter = msgList.constBegin();
154 msgListEnd = msgList.constEnd();
155 while (msgIter != msgListEnd) {
156 backlog << qVariantFromValue(*msgIter);
165 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type,
168 QVariantList backlog;
169 QList<Message> msgList;
170 msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
172 QList<Message>::const_iterator msgIter = msgList.constBegin();
173 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
174 while (msgIter != msgListEnd) {
175 backlog << qVariantFromValue(*msgIter);
185 if (!msgList.isEmpty()) {
186 if (msgList.first().msgId() < msgList.last().msgId())
187 last = msgList.first().msgId();
189 last = msgList.last().msgId();
192 msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
193 msgIter = msgList.constBegin();
194 msgListEnd = msgList.constEnd();
195 while (msgIter != msgListEnd) {
196 backlog << qVariantFromValue(*msgIter);