1/***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21#include "corebacklogmanager.h"
22#include "core.h"
23#include "coresession.h"
24
25#include <QDebug>
26
27INIT_SYNCABLE_OBJECT(CoreBacklogManager)
28CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
29 : BacklogManager(coreSession),
30 _coreSession(coreSession)
31{
32}
33
34
35QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
36{
37 QVariantList backlog;
38 QList<Message> msgList;
39 msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
40
41 QList<Message>::const_iterator msgIter = msgList.constBegin();
42 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
43 while (msgIter != msgListEnd) {
44 backlog << qVariantFromValue(*msgIter);
45 msgIter++;
46 }
47
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();
53 else
54 oldestMessage = msgList.last().msgId();
55 }
56
57 if (first != -1) {
58 last = first;
59 }
60 else {
61 last = oldestMessage;
62 }
63
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);
72 msgIter++;
73 }
74 }
75 }
76
77 return backlog;
78}
79
80
81QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
82{
83 QVariantList backlog;
84 QList<Message> msgList;
85 msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
86
87 QList<Message>::const_iterator msgIter = msgList.constBegin();
88 QList<Message>::const_iterator msgListEnd = msgList.constEnd();
89 while (msgIter != msgListEnd) {
90 backlog << qVariantFromValue(*msgIter);
91 msgIter++;
92 }
93
94 if (additional) {
95 if (first != -1) {
96 last = first;
97 }
98 else {
99 last = -1;
100 if (!msgList.isEmpty()) {
101 if (msgList.first().msgId() < msgList.last().msgId())
102 last = msgList.first().msgId();
103 else
104 last = msgList.last().msgId();
105 }
106 }
107 msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
108 msgIter = msgList.constBegin();
109 msgListEnd = msgList.constEnd();
110 while (msgIter != msgListEnd) {
111 backlog << qVariantFromValue(*msgIter);
112 msgIter++;
113 }
114 }
115
116 return backlog;
117}
118