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 INIT_SYNCABLE_OBJECT(CoreBacklogManager)
28 CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
29 : BacklogManager(coreSession),
30 _coreSession(coreSession)
35 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
38 QList<Message> msgList;
39 msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
41 QList<Message>::const_iterator msgIter = msgList.constBegin();
42 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
43 while (msgIter != msgListEnd) {
44 backlog << qVariantFromValue(*msgIter);
48 if (additional && limit != 0) {
49 MsgId oldestMessage = first;
50 if (!msgList.isEmpty()) {
51 if (msgList.first().msgId() < msgList.last().msgId())
52 oldestMessage = msgList.first().msgId();
54 oldestMessage = msgList.last().msgId();
64 // only fetch additional messages if they continue seemlessly
65 // that is, if the list of messages is not truncated by the limit
66 if (last == oldestMessage) {
67 msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
68 msgIter = msgList.constBegin();
69 msgListEnd = msgList.constEnd();
70 while (msgIter != msgListEnd) {
71 backlog << qVariantFromValue(*msgIter);
81 QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
84 QList<Message> msgList;
85 msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
87 QList<Message>::const_iterator msgIter = msgList.constBegin();
88 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
89 while (msgIter != msgListEnd) {
90 backlog << qVariantFromValue(*msgIter);
94 if (additional && limit != 0) {
95 MsgId oldestMessage = first;
96 if (!msgList.isEmpty()) {
97 if (msgList.first().msgId() < msgList.last().msgId())
98 oldestMessage = msgList.first().msgId();
100 oldestMessage = msgList.last().msgId();
107 last = oldestMessage;
110 // only fetch additional messages if they continue seemlessly
111 // that is, if the list of messages is not truncated by the limit
112 if (last == oldestMessage) {
113 msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags});
114 msgIter = msgList.constBegin();
115 msgListEnd = msgList.constEnd();
116 while (msgIter != msgListEnd) {
117 backlog << qVariantFromValue(*msgIter);
127 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
129 QVariantList backlog;
130 QList<Message> msgList;
131 msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
133 QList<Message>::const_iterator msgIter = msgList.constBegin();
134 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
135 while (msgIter != msgListEnd) {
136 backlog << qVariantFromValue(*msgIter);
146 if (!msgList.isEmpty()) {
147 if (msgList.first().msgId() < msgList.last().msgId())
148 last = msgList.first().msgId();
150 last = msgList.last().msgId();
153 msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
154 msgIter = msgList.constBegin();
155 msgListEnd = msgList.constEnd();
156 while (msgIter != msgListEnd) {
157 backlog << qVariantFromValue(*msgIter);
166 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type,
169 QVariantList backlog;
170 QList<Message> msgList;
171 msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
173 QList<Message>::const_iterator msgIter = msgList.constBegin();
174 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
175 while (msgIter != msgListEnd) {
176 backlog << qVariantFromValue(*msgIter);
186 if (!msgList.isEmpty()) {
187 if (msgList.first().msgId() < msgList.last().msgId())
188 last = msgList.first().msgId();
190 last = msgList.last().msgId();
193 msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
194 msgIter = msgList.constBegin();
195 msgListEnd = msgList.constEnd();
196 while (msgIter != msgListEnd) {
197 backlog << qVariantFromValue(*msgIter);