e1466da467f4e4095038ca5a575dab0eff094339
[quassel.git] / src / core / corebacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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
23 #include <QDebug>
24
25 #include "core.h"
26 #include "coresession.h"
27
28 CoreBacklogManager::CoreBacklogManager(CoreSession* coreSession)
29     : BacklogManager(coreSession)
30     , _coreSession(coreSession)
31 {}
32
33 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
34 {
35     QVariantList backlog;
36     QList<Message> msgList;
37     msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
38
39     QList<Message>::const_iterator msgIter = msgList.constBegin();
40     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
41     while (msgIter != msgListEnd) {
42         backlog << qVariantFromValue(*msgIter);
43         ++msgIter;
44     }
45
46     if (additional && limit != 0) {
47         MsgId oldestMessage = first;
48         if (!msgList.isEmpty()) {
49             if (msgList.first().msgId() < msgList.last().msgId())
50                 oldestMessage = msgList.first().msgId();
51             else
52                 oldestMessage = msgList.last().msgId();
53         }
54
55         if (first != -1) {
56             last = first;
57         }
58         else {
59             last = oldestMessage;
60         }
61
62         // only fetch additional messages if they continue seemlessly
63         // that is, if the list of messages is not truncated by the limit
64         if (last == oldestMessage) {
65             msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
66             msgIter = msgList.constBegin();
67             msgListEnd = msgList.constEnd();
68             while (msgIter != msgListEnd) {
69                 backlog << qVariantFromValue(*msgIter);
70                 ++msgIter;
71             }
72         }
73     }
74
75     return backlog;
76 }
77
78 QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
79 {
80     QVariantList backlog;
81     QList<Message> msgList;
82     msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
83
84     QList<Message>::const_iterator msgIter = msgList.constBegin();
85     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
86     while (msgIter != msgListEnd) {
87         backlog << qVariantFromValue(*msgIter);
88         ++msgIter;
89     }
90
91     if (additional && limit != 0) {
92         MsgId oldestMessage = first;
93         if (!msgList.isEmpty()) {
94             if (msgList.first().msgId() < msgList.last().msgId())
95                 oldestMessage = msgList.first().msgId();
96             else
97                 oldestMessage = msgList.last().msgId();
98         }
99
100         if (first != -1) {
101             last = first;
102         }
103         else {
104             last = oldestMessage;
105         }
106
107         // only fetch additional messages if they continue seemlessly
108         // that is, if the list of messages is not truncated by the limit
109         if (last == oldestMessage) {
110             msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags});
111             msgIter = msgList.constBegin();
112             msgListEnd = msgList.constEnd();
113             while (msgIter != msgListEnd) {
114                 backlog << qVariantFromValue(*msgIter);
115                 ++msgIter;
116             }
117         }
118     }
119
120     return backlog;
121 }
122
123 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
124 {
125     QVariantList backlog;
126     QList<Message> msgList;
127     msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
128
129     QList<Message>::const_iterator msgIter = msgList.constBegin();
130     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
131     while (msgIter != msgListEnd) {
132         backlog << qVariantFromValue(*msgIter);
133         ++msgIter;
134     }
135
136     if (additional) {
137         if (first != -1) {
138             last = first;
139         }
140         else {
141             last = -1;
142             if (!msgList.isEmpty()) {
143                 if (msgList.first().msgId() < msgList.last().msgId())
144                     last = msgList.first().msgId();
145                 else
146                     last = msgList.last().msgId();
147             }
148         }
149         msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
150         msgIter = msgList.constBegin();
151         msgListEnd = msgList.constEnd();
152         while (msgIter != msgListEnd) {
153             backlog << qVariantFromValue(*msgIter);
154             ++msgIter;
155         }
156     }
157
158     return backlog;
159 }
160
161 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type, int flags)
162 {
163     QVariantList backlog;
164     QList<Message> msgList;
165     msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
166
167     QList<Message>::const_iterator msgIter = msgList.constBegin();
168     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
169     while (msgIter != msgListEnd) {
170         backlog << qVariantFromValue(*msgIter);
171         ++msgIter;
172     }
173
174     if (additional) {
175         if (first != -1) {
176             last = first;
177         }
178         else {
179             last = -1;
180             if (!msgList.isEmpty()) {
181                 if (msgList.first().msgId() < msgList.last().msgId())
182                     last = msgList.first().msgId();
183                 else
184                     last = msgList.last().msgId();
185             }
186         }
187         msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
188         msgIter = msgList.constBegin();
189         msgListEnd = msgList.constEnd();
190         while (msgIter != msgListEnd) {
191             backlog << qVariantFromValue(*msgIter);
192             ++msgIter;
193         }
194     }
195
196     return backlog;
197 }