modernize: Use override instead of virtual
[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 #include "core.h"
23 #include "coresession.h"
24
25 #include <QDebug>
26
27 CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
28     : BacklogManager(coreSession),
29     _coreSession(coreSession)
30 {
31 }
32
33
34 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
35 {
36     QVariantList backlog;
37     QList<Message> msgList;
38     msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
39
40     QList<Message>::const_iterator msgIter = msgList.constBegin();
41     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
42     while (msgIter != msgListEnd) {
43         backlog << qVariantFromValue(*msgIter);
44         ++msgIter;
45     }
46
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();
52             else
53                 oldestMessage = msgList.last().msgId();
54         }
55
56         if (first != -1) {
57             last = first;
58         }
59         else {
60             last = oldestMessage;
61         }
62
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);
71                 ++msgIter;
72             }
73         }
74     }
75
76     return backlog;
77 }
78
79
80 QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags)
81 {
82     QVariantList backlog;
83     QList<Message> msgList;
84     msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
85
86     QList<Message>::const_iterator msgIter = msgList.constBegin();
87     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
88     while (msgIter != msgListEnd) {
89         backlog << qVariantFromValue(*msgIter);
90         ++msgIter;
91     }
92
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();
98             else
99                 oldestMessage = msgList.last().msgId();
100         }
101
102         if (first != -1) {
103             last = first;
104         }
105         else {
106             last = oldestMessage;
107         }
108
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);
117                 ++msgIter;
118             }
119         }
120     }
121
122     return backlog;
123 }
124
125
126 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
127 {
128     QVariantList backlog;
129     QList<Message> msgList;
130     msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
131
132     QList<Message>::const_iterator msgIter = msgList.constBegin();
133     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
134     while (msgIter != msgListEnd) {
135         backlog << qVariantFromValue(*msgIter);
136         ++msgIter;
137     }
138
139     if (additional) {
140         if (first != -1) {
141             last = first;
142         }
143         else {
144             last = -1;
145             if (!msgList.isEmpty()) {
146                 if (msgList.first().msgId() < msgList.last().msgId())
147                     last = msgList.first().msgId();
148                 else
149                     last = msgList.last().msgId();
150             }
151         }
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);
157             ++msgIter;
158         }
159     }
160
161     return backlog;
162 }
163
164
165 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type,
166                                                            int flags)
167 {
168     QVariantList backlog;
169     QList<Message> msgList;
170     msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
171
172     QList<Message>::const_iterator msgIter = msgList.constBegin();
173     QList<Message>::const_iterator msgListEnd = msgList.constEnd();
174     while (msgIter != msgListEnd) {
175         backlog << qVariantFromValue(*msgIter);
176         ++msgIter;
177     }
178
179     if (additional) {
180         if (first != -1) {
181             last = first;
182         }
183         else {
184             last = -1;
185             if (!msgList.isEmpty()) {
186                 if (msgList.first().msgId() < msgList.last().msgId())
187                     last = msgList.first().msgId();
188                 else
189                     last = msgList.last().msgId();
190             }
191         }
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);
197             ++msgIter;
198         }
199     }
200
201     return backlog;
202 }