1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 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 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "backlogrequester.h" |
22 | |
23 | #include <QObject> |
24 | |
25 | #include "backlogsettings.h" |
26 | #include "bufferviewoverlay.h" |
27 | #include "clientbacklogmanager.h" |
28 | |
29 | BacklogRequester::BacklogRequester(bool buffering, RequesterType requesterType, ClientBacklogManager *backlogManager) |
30 | : backlogManager(backlogManager), |
31 | _isBuffering(buffering), |
32 | _requesterType(requesterType), |
33 | _totalBuffers(0) |
34 | { |
35 | Q_ASSERT(backlogManager); |
36 | } |
37 | |
38 | |
39 | void BacklogRequester::setWaitingBuffers(const QSet<BufferId> &buffers) |
40 | { |
41 | _buffersWaiting = buffers; |
42 | _totalBuffers = _buffersWaiting.count(); |
43 | } |
44 | |
45 | |
46 | void BacklogRequester::addWaitingBuffer(BufferId buffer) |
47 | { |
48 | _buffersWaiting << buffer; |
49 | _totalBuffers++; |
50 | } |
51 | |
52 | |
53 | bool BacklogRequester::buffer(BufferId bufferId, const MessageList &messages) |
54 | { |
55 | _bufferedMessages << messages; |
56 | _buffersWaiting.remove(bufferId); |
57 | return !_buffersWaiting.isEmpty(); |
58 | } |
59 | |
60 | |
61 | BufferIdList BacklogRequester::allBufferIds() const |
62 | { |
63 | QSet<BufferId> bufferIds = Client::bufferViewOverlay()->bufferIds(); |
64 | bufferIds += Client::bufferViewOverlay()->tempRemovedBufferIds(); |
65 | return bufferIds.toList(); |
66 | } |
67 | |
68 | |
69 | void BacklogRequester::flushBuffer() |
70 | { |
71 | if (!_buffersWaiting.isEmpty()) { |
72 | qWarning() << Q_FUNC_INFO << "was called before all backlog was received:" |
73 | << _buffersWaiting.count() << "buffers are waiting." ; |
74 | } |
75 | _bufferedMessages.clear(); |
76 | _totalBuffers = 0; |
77 | _buffersWaiting.clear(); |
78 | } |
79 | |
80 | |
81 | // ======================================== |
82 | // FIXED BACKLOG REQUESTER |
83 | // ======================================== |
84 | FixedBacklogRequester::FixedBacklogRequester(ClientBacklogManager *backlogManager) |
85 | : BacklogRequester(true, BacklogRequester::PerBufferFixed, backlogManager) |
86 | { |
87 | BacklogSettings backlogSettings; |
88 | _backlogCount = backlogSettings.fixedBacklogAmount(); |
89 | } |
90 | |
91 | |
92 | void FixedBacklogRequester::requestBacklog(const BufferIdList &bufferIds) |
93 | { |
94 | setWaitingBuffers(bufferIds); |
95 | backlogManager->emitMessagesRequested(QObject::tr("Requesting a total of up to %1 backlog messages for %2 buffers" ).arg(_backlogCount * bufferIds.count()).arg(bufferIds.count())); |
96 | foreach(BufferId bufferId, bufferIds) { |
97 | backlogManager->requestBacklog(bufferId, -1, -1, _backlogCount); |
98 | } |
99 | } |
100 | |
101 | |
102 | // ======================================== |
103 | // GLOBAL UNREAD BACKLOG REQUESTER |
104 | // ======================================== |
105 | GlobalUnreadBacklogRequester::GlobalUnreadBacklogRequester(ClientBacklogManager *backlogManager) |
106 | : BacklogRequester(false, BacklogRequester::GlobalUnread, backlogManager) |
107 | { |
108 | BacklogSettings backlogSettings; |
109 | _limit = backlogSettings.globalUnreadBacklogLimit(); |
110 | _additional = backlogSettings.globalUnreadBacklogAdditional(); |
111 | } |
112 | |
113 | |
114 | void GlobalUnreadBacklogRequester::requestInitialBacklog() |
115 | { |
116 | MsgId oldestUnreadMessage; |
117 | foreach(BufferId bufferId, allBufferIds()) { |
118 | MsgId msgId = Client::networkModel()->lastSeenMsgId(bufferId); |
119 | if (!oldestUnreadMessage.isValid() || oldestUnreadMessage > msgId) |
120 | oldestUnreadMessage = msgId; |
121 | } |
122 | backlogManager->emitMessagesRequested(QObject::tr("Requesting up to %1 of all unread backlog messages (plus additional %2)" ).arg(_limit).arg(_additional)); |
123 | backlogManager->requestBacklogAll(oldestUnreadMessage, -1, _limit, _additional); |
124 | } |
125 | |
126 | |
127 | // ======================================== |
128 | // PER BUFFER UNREAD BACKLOG REQUESTER |
129 | // ======================================== |
130 | PerBufferUnreadBacklogRequester::PerBufferUnreadBacklogRequester(ClientBacklogManager *backlogManager) |
131 | : BacklogRequester(true, BacklogRequester::PerBufferUnread, backlogManager) |
132 | { |
133 | BacklogSettings backlogSettings; |
134 | _limit = backlogSettings.perBufferUnreadBacklogLimit(); |
135 | _additional = backlogSettings.perBufferUnreadBacklogAdditional(); |
136 | } |
137 | |
138 | |
139 | void PerBufferUnreadBacklogRequester::requestBacklog(const BufferIdList &bufferIds) |
140 | { |
141 | setWaitingBuffers(bufferIds); |
142 | 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())); |
143 | foreach(BufferId bufferId, bufferIds) { |
144 | backlogManager->requestBacklog(bufferId, Client::networkModel()->lastSeenMsgId(bufferId), -1, _limit, _additional); |
145 | } |
146 | } |
147 | |