File: backlogrequester.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 (137 lines) | stat: -rw-r--r-- 5,626 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
/***************************************************************************
 *   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 "backlogrequester.h"

#include <QObject>

#include "backlogsettings.h"
#include "bufferviewoverlay.h"
#include "clientbacklogmanager.h"

BacklogRequester::BacklogRequester(bool buffering, RequesterType requesterType, ClientBacklogManager *backlogManager)
    : backlogManager(backlogManager),
    _isBuffering(buffering),
    _requesterType(requesterType),
    _totalBuffers(0)
{
    Q_ASSERT(backlogManager);
}

void BacklogRequester::setWaitingBuffers(const BufferIdList& buffers)
{
    _buffersWaiting = {buffers.begin(), buffers.end()};
    _totalBuffers = int(_buffersWaiting.size());
}


bool BacklogRequester::buffer(BufferId bufferId, const MessageList &messages)
{
    _bufferedMessages << messages;
    _buffersWaiting.erase(bufferId);
    return !_buffersWaiting.empty();
}


BufferIdList BacklogRequester::allBufferIds() const
{
    QSet<BufferId> bufferIds = Client::bufferViewOverlay()->bufferIds();
    bufferIds += Client::bufferViewOverlay()->tempRemovedBufferIds();
    return bufferIds.values();
}


void BacklogRequester::flushBuffer()
{
    if (!_buffersWaiting.empty()) {
        qWarning() << Q_FUNC_INFO << "was called before all backlog was received:" << _buffersWaiting.size() << "buffers are waiting.";
    }
    _bufferedMessages.clear();
    _totalBuffers = 0;
    _buffersWaiting.clear();
}


// ========================================
//  FIXED BACKLOG REQUESTER
// ========================================
FixedBacklogRequester::FixedBacklogRequester(ClientBacklogManager *backlogManager)
    : BacklogRequester(true, BacklogRequester::PerBufferFixed, backlogManager)
{
    BacklogSettings backlogSettings;
    _backlogCount = backlogSettings.fixedBacklogAmount();
}


void FixedBacklogRequester::requestBacklog(const BufferIdList &bufferIds)
{
    setWaitingBuffers(bufferIds);
    backlogManager->emitMessagesRequested(QObject::tr("Requesting a total of up to %1 backlog messages for %2 buffers").arg(_backlogCount * bufferIds.count()).arg(bufferIds.count()));
    foreach(BufferId bufferId, bufferIds) {
        backlogManager->requestBacklog(bufferId, -1, -1, _backlogCount);
    }
}


// ========================================
//  GLOBAL UNREAD BACKLOG REQUESTER
// ========================================
GlobalUnreadBacklogRequester::GlobalUnreadBacklogRequester(ClientBacklogManager *backlogManager)
    : BacklogRequester(false, BacklogRequester::GlobalUnread, backlogManager)
{
    BacklogSettings backlogSettings;
    _limit = backlogSettings.globalUnreadBacklogLimit();
    _additional = backlogSettings.globalUnreadBacklogAdditional();
}


void GlobalUnreadBacklogRequester::requestInitialBacklog()
{
    MsgId oldestUnreadMessage;
    foreach(BufferId bufferId, allBufferIds()) {
        MsgId msgId = Client::networkModel()->lastSeenMsgId(bufferId);
        if (!oldestUnreadMessage.isValid() || oldestUnreadMessage > msgId)
            oldestUnreadMessage = msgId;
    }
    backlogManager->emitMessagesRequested(QObject::tr("Requesting up to %1 of all unread backlog messages (plus additional %2)").arg(_limit).arg(_additional));
    backlogManager->requestBacklogAll(oldestUnreadMessage, -1, _limit, _additional);
}


// ========================================
//  PER BUFFER UNREAD BACKLOG REQUESTER
// ========================================
PerBufferUnreadBacklogRequester::PerBufferUnreadBacklogRequester(ClientBacklogManager *backlogManager)
    : BacklogRequester(true, BacklogRequester::PerBufferUnread, backlogManager)
{
    BacklogSettings backlogSettings;
    _limit = backlogSettings.perBufferUnreadBacklogLimit();
    _additional = backlogSettings.perBufferUnreadBacklogAdditional();
}


void PerBufferUnreadBacklogRequester::requestBacklog(const BufferIdList &bufferIds)
{
    setWaitingBuffers(bufferIds);
    backlogManager->emitMessagesRequested(QObject::tr("Requesting a total of up to %1 unread backlog messages for %2 buffers").arg((_limit + _additional) * bufferIds.count()).arg(bufferIds.count()));
    foreach(BufferId bufferId, bufferIds) {
        backlogManager->requestBacklog(bufferId, Client::networkModel()->lastSeenMsgId(bufferId), -1, _limit, _additional);
    }
}