f28913fc6cd741ec64f43b28ae208c89cf8f6ce1
[quassel.git] / src / client / clientbacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 ClientBacklogManager::ClientBacklogManager(QObject *parent)
31   : BacklogManager(parent),
32     _requester(0)
33 {
34 }
35
36 void ClientBacklogManager::receiveBacklog(BufferId bufferId, int lastMsgs, int offset, QVariantList msgs) {
37   Q_UNUSED(lastMsgs)
38   Q_UNUSED(offset)
39
40   if(msgs.isEmpty())
41     return;
42
43   MessageList msglist;
44   foreach(QVariant v, msgs) {
45     Message msg = v.value<Message>();
46     msg.setFlags(msg.flags() | Message::Backlog);
47     msglist << msg;
48   }
49
50   if(isBuffering()) {
51     if(!_requester->buffer(bufferId, msglist)) {
52       // this was the last part to buffer
53       stopBuffering();
54     }
55   } else {
56     dispatchMessages(msglist);
57   }
58 }
59
60 void ClientBacklogManager::requestInitialBacklog() {
61   if(_requester) {
62     qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
63     return;
64   }
65
66   BacklogSettings settings;
67   switch(settings.requesterType()) {
68   case BacklogRequester::GlobalUnread:
69   case BacklogRequester::PerBufferUnread:
70   case BacklogRequester::PerBufferFixed:
71   default:
72     _requester = new FixedBacklogRequester(this);
73   };
74
75   _requester->requestBacklog();
76 }
77
78 void ClientBacklogManager::stopBuffering() {
79   Q_ASSERT(_requester);
80
81   dispatchMessages(_requester->bufferedMessages(), true);
82
83   delete _requester;
84   _requester = 0;
85 }
86
87 bool ClientBacklogManager::isBuffering() {
88   return _requester && _requester->isBuffering();
89 }
90
91 void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort) {
92   MessageList msgs = messages;
93
94   clock_t start_t = clock();
95   if(sort)
96     qSort(msgs);
97   Client::messageProcessor()->process(msgs);
98   clock_t end_t = clock();
99
100   emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(msgs.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
101 }