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