3fb46806e9b6a8cfee7b82667b8c1181091d5293
[quassel.git] / src / client / backlogrequester.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #ifndef BACKLOGREQUESTER_H
22 #define BACKLOGREQUESTER_H
23
24 #include <QList>
25
26 #include "client.h"
27 #include "message.h"
28 #include "networkmodel.h"
29 #include "types.h"
30
31 class ClientBacklogManager;
32
33 class BacklogRequester
34 {
35 public:
36     enum RequesterType {
37         InvalidRequester = 0,
38         PerBufferFixed,
39         PerBufferUnread,
40         GlobalUnread
41     };
42
43     BacklogRequester(bool buffering, RequesterType requesterType, ClientBacklogManager *backlogManger);
44     virtual inline ~BacklogRequester() {}
45
46     inline bool isBuffering() { return _isBuffering; }
47     inline RequesterType type() { return _requesterType; }
48     inline const QList<Message> &bufferedMessages() { return _bufferedMessages; }
49
50     inline int buffersWaiting() const { return _buffersWaiting.count(); }
51     inline int totalBuffers() const { return _totalBuffers; }
52
53     bool buffer(BufferId bufferId, const MessageList &messages); //! returns false if it was the last missing backlogpart
54
55     virtual void requestBacklog(const BufferIdList &bufferIds) = 0;
56     virtual inline void requestInitialBacklog() { requestBacklog(allBufferIds()); }
57
58     virtual void flushBuffer();
59
60 protected:
61     BufferIdList allBufferIds() const;
62     inline void setWaitingBuffers(const QList<BufferId> &buffers) { setWaitingBuffers(buffers.toSet()); }
63     void setWaitingBuffers(const QSet<BufferId> &buffers);
64     void addWaitingBuffer(BufferId buffer);
65
66     ClientBacklogManager *backlogManager;
67
68 private:
69     bool _isBuffering;
70     RequesterType _requesterType;
71     MessageList _bufferedMessages;
72     int _totalBuffers;
73     QSet<BufferId> _buffersWaiting;
74 };
75
76
77 // ========================================
78 //  FIXED BACKLOG REQUESTER
79 // ========================================
80 class FixedBacklogRequester : public BacklogRequester
81 {
82 public:
83     FixedBacklogRequester(ClientBacklogManager *backlogManager);
84     void requestBacklog(const BufferIdList &bufferIds) override;
85
86 private:
87     int _backlogCount;
88 };
89
90
91 // ========================================
92 //  GLOBAL UNREAD BACKLOG REQUESTER
93 // ========================================
94 class GlobalUnreadBacklogRequester : public BacklogRequester
95 {
96 public:
97     GlobalUnreadBacklogRequester(ClientBacklogManager *backlogManager);
98     void requestInitialBacklog() override;
99     void requestBacklog(const BufferIdList &) override {}
100
101 private:
102     int _limit;
103     int _additional;
104 };
105
106
107 // ========================================
108 //  PER BUFFER UNREAD BACKLOG REQUESTER
109 // ========================================
110 class PerBufferUnreadBacklogRequester : public BacklogRequester
111 {
112 public:
113     PerBufferUnreadBacklogRequester(ClientBacklogManager *backlogManager);
114     void requestBacklog(const BufferIdList &bufferIds) override;
115
116 private:
117     int _limit;
118     int _additional;
119 };
120
121
122 #endif //BACKLOGREQUESTER_H