File: clientbacklogmanager.cpp

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (189 lines) | stat: -rw-r--r-- 6,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "clientbacklogmanager.h"

#include "abstractmessageprocessor.h"
#include "backlogsettings.h"
#include "backlogrequester.h"
#include "client.h"
#include "util.h"

#include <ctime>

#include <QDebug>

INIT_SYNCABLE_OBJECT(ClientBacklogManager)
ClientBacklogManager::ClientBacklogManager(QObject *parent)
    : BacklogManager(parent),
    _requester(0),
    _initBacklogRequested(false)
{
}


QVariantList ClientBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional)
{
    _buffersRequested << bufferId;
    return BacklogManager::requestBacklog(bufferId, first, last, limit, additional);
}


void ClientBacklogManager::receiveBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, QVariantList msgs)
{
    Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)

    emit messagesReceived(bufferId, msgs.count());

    MessageList msglist;
    foreach(QVariant v, msgs) {
        Message msg = v.value<Message>();
        msg.setFlags(msg.flags() | Message::Backlog);
        msglist << msg;
    }

    if (isBuffering()) {
        bool lastPart = !_requester->buffer(bufferId, msglist);
        updateProgress(_requester->totalBuffers() - _requester->buffersWaiting(), _requester->totalBuffers());
        if (lastPart) {
            dispatchMessages(_requester->bufferedMessages(), true);
            _requester->flushBuffer();
        }
    }
    else {
        dispatchMessages(msglist);
    }
}


void ClientBacklogManager::receiveBacklogAll(MsgId first, MsgId last, int limit, int additional, QVariantList msgs)
{
    Q_UNUSED(first) Q_UNUSED(last) Q_UNUSED(limit) Q_UNUSED(additional)

    MessageList msglist;
    foreach(QVariant v, msgs) {
        Message msg = v.value<Message>();
        msg.setFlags(msg.flags() | Message::Backlog);
        msglist << msg;
    }

    dispatchMessages(msglist);
}


void ClientBacklogManager::requestInitialBacklog()
{
    if (_initBacklogRequested) {
        Q_ASSERT(_requester);
        qWarning() << "ClientBacklogManager::requestInitialBacklog() called twice in the same session! (Backlog has already been requested)";
        return;
    }

    BacklogSettings settings;
    switch (settings.requesterType()) {
    case BacklogRequester::GlobalUnread:
        _requester = new GlobalUnreadBacklogRequester(this);
        break;
    case BacklogRequester::PerBufferUnread:
        _requester = new PerBufferUnreadBacklogRequester(this);
        break;
    case BacklogRequester::PerBufferFixed:
    default:
        _requester = new FixedBacklogRequester(this);
    };

    _requester->requestInitialBacklog();
    _initBacklogRequested = true;
    if (_requester->isBuffering()) {
        updateProgress(0, _requester->totalBuffers());
    }
}


BufferIdList ClientBacklogManager::filterNewBufferIds(const BufferIdList &bufferIds)
{
    BufferIdList newBuffers;
    QSet<BufferId> availableBuffers = toQSet(Client::networkModel()->allBufferIds());
    foreach (BufferId bufferId, bufferIds) {
        if (_buffersRequested.contains(bufferId) || !availableBuffers.contains(bufferId))
            continue;
        newBuffers << bufferId;
    }
    return newBuffers;
}


void ClientBacklogManager::checkForBacklog(const QList<BufferId> &bufferIds)
{
    // we ingore all backlogrequests until we had our initial request
    if (!_initBacklogRequested) {
        return;
    }

    if (!_requester) {
        // during client start up this message is to be expected in some situations.
        qDebug() << "ClientBacklogManager::checkForBacklog(): no active backlog requester.";
        return;
    }
    switch (_requester->type()) {
    case BacklogRequester::GlobalUnread:
        break;
    case BacklogRequester::PerBufferUnread:
    case BacklogRequester::PerBufferFixed:
    default:
    {
        BufferIdList buffers = filterNewBufferIds(bufferIds);
        if (!buffers.isEmpty())
            _requester->requestBacklog(buffers);
    }
    };
}


bool ClientBacklogManager::isBuffering()
{
    return _requester && _requester->isBuffering();
}


void ClientBacklogManager::dispatchMessages(const MessageList &messages, bool sort)
{
    if (messages.isEmpty())
        return;

    MessageList msgs = messages;

    clock_t start_t = clock();
    if (sort)
        qSort(msgs);
    Client::messageProcessor()->process(msgs);
    clock_t end_t = clock();

    emit messagesProcessed(tr("Processed %1 messages in %2 seconds.").arg(messages.count()).arg((float)(end_t - start_t) / CLOCKS_PER_SEC));
}


void ClientBacklogManager::reset()
{
    delete _requester;
    _requester = 0;
    _initBacklogRequested = false;
    _buffersRequested.clear();
}