cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / core / corebacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 seamlessly
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 seamlessly
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::requestBacklogForward(BufferId bufferId, MsgId first, MsgId last, int limit, int type, int flags)
113 {
114     QVariantList backlog;
115     auto msgList = Core::requestMsgsForward(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags});
116
117     std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
118         return QVariant::fromValue(msg);
119     });
120
121     return backlog;
122 }
123
124 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional)
125 {
126     QVariantList backlog;
127     auto msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
128
129     std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
130         return QVariant::fromValue(msg);
131     });
132
133     if (additional) {
134         if (first != -1) {
135             last = first;
136         }
137         else {
138             last = -1;
139             if (!msgList.empty()) {
140                 if (msgList.front().msgId() < msgList.back().msgId())
141                     last = msgList.front().msgId();
142                 else
143                     last = msgList.back().msgId();
144             }
145         }
146         msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
147         std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
148             return QVariant::fromValue(msg);
149         });
150     }
151
152     return backlog;
153 }
154
155 QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type, int flags)
156 {
157     QVariantList backlog;
158     auto msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags});
159
160     std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
161         return QVariant::fromValue(msg);
162     });
163
164     if (additional) {
165         if (first != -1) {
166             last = first;
167         }
168         else {
169             last = -1;
170             if (!msgList.empty()) {
171                 if (msgList.front().msgId() < msgList.back().msgId())
172                     last = msgList.front().msgId();
173                 else
174                     last = msgList.back().msgId();
175             }
176         }
177         msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags});
178         std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) {
179             return QVariant::fromValue(msg);
180         });
181     }
182
183     return backlog;
184 }