b969251d43df8fa281ce2dde505242f044801277
[quassel.git] / src / client / clientbacklogmanager.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 "clientbacklogmanager.h"
22
23 #include <algorithm>
24 #include <ctime>
25
26 #include <QDebug>
27
28 #include "abstractmessageprocessor.h"
29 #include "backlogrequester.h"
30 #include "backlogsettings.h"
31 #include "client.h"
32
33 ClientBacklogManager::ClientBacklogManager(QObject* parent)
34     : BacklogManager(parent)
35 {}
36
37 QVariantList ClientBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
38 {
39     _buffersRequested << bufferId;
40     return BacklogManager::requestBacklog(bufferId, first, last, limit, additional);
41 }
42
43 void ClientBacklogManager::receiveBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, QVariantList msgs)
44 {
45     Q_UNUSED(first)
46     Q_UNUSED(last)
47     Q_UNUSED(limit)
48     Q_UNUSED(additional)
49
50     emit messagesReceived(bufferId, msgs.count());
51
52     MessageList msglist;
53     foreach (QVariant v, msgs) {
54         Message msg = v.value<Message>();
55         msg.setFlags(msg.flags() | Message::Backlog);
56         msglist << msg;
57     }
58
59     if (isBuffering()) {
60         bool lastPart = !_requester->buffer(bufferId, msglist);
61         updateProgress(_requester->totalBuffers() - _requester->buffersWaiting(), _requester->totalBuffers());
62         if (lastPart) {
63             dispatchMessages(_requester->bufferedMessages(), true);
64             _requester->flushBuffer();
65         }
66     }
67     else {
68         dispatchMessages(msglist);
69     }
70 }
71
72 void ClientBacklogManager::receiveBacklogAll(MsgId first, MsgId last, int limit, int additional, QVariantList msgs)
73 {
74     Q_UNUSED(first)
75     Q_UNUSED(last)
76     Q_UNUSED(limit)
77     Q_UNUSED(additional)
78
79     MessageList msglist;
80     foreach (QVariant v, msgs) {
81         Message msg = v.value<Message>();
82         msg.setFlags(msg.flags() | Message::Backlog);
83         msglist << msg;
84     }
85
86     dispatchMessages(msglist);
87 }
88
89 void ClientBacklogManager::requestInitialBacklog()
90 {
91     if (_initBacklogRequested) {
92         Q_ASSERT(_requester);
93         qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
94         return;
95     }
96
97     BacklogSettings settings;
98     switch (settings.requesterType()) {
99     case BacklogRequester::GlobalUnread:
100         _requester = new GlobalUnreadBacklogRequester(this);
101         break;
102     case BacklogRequester::PerBufferUnread:
103         _requester = new PerBufferUnreadBacklogRequester(this);
104         break;
105     case BacklogRequester::PerBufferFixed:
106     default:
107         _requester = new FixedBacklogRequester(this);
108     };
109
110     _requester->requestInitialBacklog();
111     _initBacklogRequested = true;
112     if (_requester->isBuffering()) {
113         updateProgress(0, _requester->totalBuffers());
114     }
115 }
116
117 BufferIdList ClientBacklogManager::filterNewBufferIds(const BufferIdList& bufferIds)
118 {
119     BufferIdList newBuffers;
120     QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
121     foreach (BufferId bufferId, bufferIds) {
122         if (_buffersRequested.contains(bufferId) || !availableBuffers.contains(bufferId))
123             continue;
124         newBuffers << bufferId;
125     }
126     return newBuffers;
127 }
128
129 void ClientBacklogManager::checkForBacklog(const QList<BufferId>& bufferIds)
130 {
131     // we ingore all backlogrequests until we had our initial request
132     if (!_initBacklogRequested) {
133         return;
134     }
135
136     if (!_requester) {
137         // during client start up this message is to be expected in some situations.
138         qDebug() << "ClientBacklogManager::checkForBacklog(): no active backlog requester.";
139         return;
140     }
141     switch (_requester->type()) {
142     case BacklogRequester::GlobalUnread:
143         break;
144     case BacklogRequester::PerBufferUnread:
145     case BacklogRequester::PerBufferFixed:
146     default: {
147         BufferIdList buffers = filterNewBufferIds(bufferIds);
148         if (!buffers.isEmpty())
149             _requester->requestBacklog(buffers);
150     }
151     };
152 }
153
154 bool ClientBacklogManager::isBuffering()
155 {
156     return _requester && _requester->isBuffering();
157 }
158
159 void ClientBacklogManager::dispatchMessages(const MessageList& messages, bool sort)
160 {
161     if (messages.isEmpty())
162         return;
163
164     MessageList msgs = messages;
165
166     clock_t start_t = clock();
167     if (sort)
168         std::sort(msgs.begin(), msgs.end());
169     Client::messageProcessor()->process(msgs);
170     clock_t end_t = clock();
171
172     emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(messages.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
173 }
174
175 void ClientBacklogManager::reset()
176 {
177     delete _requester;
178     _requester = nullptr;
179     _initBacklogRequested = false;
180     _buffersRequested.clear();
181 }