improved debugging facilities for the bufferview overlay
[quassel.git] / src / client / clientbacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "clientbacklogmanager.h"
22
23 #include "abstractmessageprocessor.h"
24 #include "backlogsettings.h"
25 #include "backlogrequester.h"
26 #include "client.h"
27
28 #include <ctime>
29
30 #include <QDebug>
31
32 ClientBacklogManager::ClientBacklogManager(QObject *parent)
33   : BacklogManager(parent),
34     _requester(0)
35 {
36 }
37
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);
41 }
42
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)
45
46   emit messagesReceived(bufferId, msgs.count());
47
48   MessageList msglist;
49   foreach(QVariant v, msgs) {
50     Message msg = v.value<Message>();
51     msg.setFlags(msg.flags() | Message::Backlog);
52     msglist << msg;
53   }
54
55   if(isBuffering()) {
56     bool lastPart = !_requester->buffer(bufferId, msglist);
57     updateProgress(_requester->totalBuffers() - _requester->buffersWaiting(), _requester->totalBuffers());
58     if(lastPart) {
59       stopBuffering();
60     }
61   } else {
62     dispatchMessages(msglist);
63   }
64 }
65
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)
68
69   MessageList msglist;
70   foreach(QVariant v, msgs) {
71     Message msg = v.value<Message>();
72     msg.setFlags(msg.flags() | Message::Backlog);
73     msglist << msg;
74   }
75
76   dispatchMessages(msglist);
77 }
78
79 void ClientBacklogManager::requestInitialBacklog() {
80   if(_requester && !_buffersRequested.isEmpty()) {
81     qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
82     return;
83   }
84
85   BacklogSettings settings;
86   switch(settings.requesterType()) {
87   case BacklogRequester::GlobalUnread:
88     _requester = new GlobalUnreadBacklogRequester(this);
89     break;
90   case BacklogRequester::PerBufferUnread:
91     _requester = new PerBufferUnreadBacklogRequester(this);
92     break;
93   case BacklogRequester::PerBufferFixed:
94   default:
95     _requester = new FixedBacklogRequester(this);
96   };
97
98   _requester->requestInitialBacklog();
99   if(_requester->isBuffering()) {
100     updateProgress(0, _requester->totalBuffers());
101   }
102 }
103
104 BufferIdList ClientBacklogManager::filterNewBufferIds(const BufferIdList &bufferIds) {
105   BufferIdList newBuffers;
106   QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
107   foreach(BufferId bufferId, bufferIds) {
108     if(_buffersRequested.contains(bufferId) || !availableBuffers.contains(bufferId))
109       continue;
110     newBuffers << bufferId;
111   }
112   return newBuffers;
113 }
114
115 void ClientBacklogManager::checkForBacklog(const QList<BufferId> &bufferIds) {
116   if(!_requester) {
117     // during client start up this message is to be expected in some situations.
118     qDebug() << "ClientBacklogManager::checkForBacklog(): no active backlog requester (yet?).";
119     return;
120   }
121   switch(_requester->type()) {
122   case BacklogRequester::GlobalUnread:
123     break;
124   case BacklogRequester::PerBufferUnread:
125   case BacklogRequester::PerBufferFixed:
126   default:
127     {
128       BufferIdList buffers = filterNewBufferIds(bufferIds);
129       if(!buffers.isEmpty())
130         _requester->requestBacklog(buffers);
131     }
132   };
133 }
134
135 void ClientBacklogManager::stopBuffering() {
136   Q_ASSERT(_requester);
137   dispatchMessages(_requester->bufferedMessages(), true);
138 }
139
140 bool ClientBacklogManager::isBuffering() {
141   return _requester && _requester->isBuffering();
142 }
143
144 void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort) {
145   if(messages.isEmpty())
146     return;
147
148   MessageList msgs = messages;
149
150   clock_t start_t = clock();
151   if(sort)
152     qSort(msgs);
153   Client::messageProcessor()->process(msgs);
154   clock_t end_t = clock();
155
156   emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(messages.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
157 }
158
159 void ClientBacklogManager::reset() {
160   delete _requester;
161   _requester = 0;
162   _buffersRequested.clear();
163 }