1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "clientbacklogmanager.h"
23 #include "abstractmessageprocessor.h"
24 #include "backlogsettings.h"
25 #include "backlogrequester.h"
32 ClientBacklogManager::ClientBacklogManager(QObject *parent)
33 : BacklogManager(parent),
38 QVariantList ClientBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional) {
39 _buffersRequested << bufferId;
40 return BacklogManager::requestBacklog(bufferId, first, last, limit, additional);
43 void ClientBacklogManager::receiveBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, QVariantList msgs) {
44 Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)
46 emit messagesReceived(bufferId, msgs.count());
49 foreach(QVariant v, msgs) {
50 Message msg = v.value<Message>();
51 msg.setFlags(msg.flags() | Message::Backlog);
56 bool lastPart = !_requester->buffer(bufferId, msglist);
57 updateProgress(_requester->totalBuffers() - _requester->buffersWaiting(), _requester->totalBuffers());
62 dispatchMessages(msglist);
66 void ClientBacklogManager::receiveBacklogAll(MsgId first, MsgId last, int limit, int additional, QVariantList msgs) {
67 Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)
70 foreach(QVariant v, msgs) {
71 Message msg = v.value<Message>();
72 msg.setFlags(msg.flags() | Message::Backlog);
76 dispatchMessages(msglist);
79 void ClientBacklogManager::requestInitialBacklog() {
81 qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
85 BacklogSettings settings;
86 switch(settings.requesterType()) {
87 case BacklogRequester::GlobalUnread:
88 _requester = new GlobalUnreadBacklogRequester(this);
90 case BacklogRequester::PerBufferUnread:
91 _requester = new PerBufferUnreadBacklogRequester(this);
93 case BacklogRequester::PerBufferFixed:
95 _requester = new FixedBacklogRequester(this);
98 _requester->requestInitialBacklog();
99 if(_requester->isBuffering()) {
100 updateProgress(0, _requester->totalBuffers());
104 BufferIdList ClientBacklogManager::filterNewBufferIds(const BufferIdList &bufferIds) {
105 BufferIdList newBuffers;
106 foreach(BufferId bufferId, bufferIds) {
107 if(!_buffersRequested.contains(bufferId))
108 newBuffers << bufferId;
113 void ClientBacklogManager::checkForBacklog(const QList<BufferId> &bufferIds) {
115 // during client start up this message is to be expected in some situations.
116 qDebug() << "ClientBacklogManager::checkForBacklog(): no active backlog requester (yet?).";
119 switch(_requester->type()) {
120 case BacklogRequester::GlobalUnread:
122 case BacklogRequester::PerBufferUnread:
123 case BacklogRequester::PerBufferFixed:
126 BufferIdList buffers = filterNewBufferIds(bufferIds);
127 if(!buffers.isEmpty())
128 _requester->requestBacklog(buffers);
133 void ClientBacklogManager::stopBuffering() {
134 Q_ASSERT(_requester);
135 dispatchMessages(_requester->bufferedMessages(), true);
138 bool ClientBacklogManager::isBuffering() {
139 return _requester && _requester->isBuffering();
142 void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort) {
143 if(messages.isEmpty())
146 MessageList msgs = messages;
148 clock_t start_t = clock();
151 Client::messageProcessor()->process(msgs);
152 clock_t end_t = clock();
154 emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(messages.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
157 void ClientBacklogManager::reset() {
160 _buffersRequested.clear();