1 /*
2     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef BT_TORRENTFILESTREAM_H
8 #define BT_TORRENTFILESTREAM_H
9 
10 #include <QIODevice>
11 #include <QSharedPointer>
12 #include <QWeakPointer>
13 #include <ktorrent_export.h>
14 #include <util/constants.h>
15 
16 namespace bt
17 {
18 class ChunkManager;
19 class TorrentControl;
20 class TorrentInterface;
21 class BitSet;
22 
23 /**
24     QIODevice which streams a file of a torrent or the whole torrent (for single file torrents)
25     This object should not be manually constructed.
26 */
27 class KTORRENT_EXPORT TorrentFileStream : public QIODevice
28 {
29 public:
30     TorrentFileStream(TorrentControl *tc, ChunkManager *cman, bool streaming_mode, QObject *parent);
31     TorrentFileStream(TorrentControl *tc, Uint32 file_index, ChunkManager *cman, bool streaming_mode, QObject *parent);
32     ~TorrentFileStream() override;
33 
34     /// Open the device (only readonly access will be allowed)
35     bool open(QIODevice::OpenMode mode) override;
36 
37     /// Close the device
38     void close() override;
39 
40     /// Get the current stream position
41     qint64 pos() const override;
42 
43     /// Get the total size
44     qint64 size() const override;
45 
46     /// Seek, will fail if attempting to seek to a point which is not downloaded yet
47     bool seek(qint64 pos) override;
48 
49     /// Are we at the end of the file
50     bool atEnd() const override;
51 
52     /// Reset the stream
53     bool reset() override;
54 
55     /// How many bytes are there available
56     qint64 bytesAvailable() const override;
57 
58     /// The stream is not sequential
isSequential()59     bool isSequential() const override
60     {
61         return false;
62     }
63 
64     /// Get the path of the file
65     QString path() const;
66 
67     /// Get a BitSet of all the chunks of this TorrentFileStream
68     const BitSet &chunksBitSet() const;
69 
70     /// Get the current chunk relative to the first chunk of the file
71     Uint32 currentChunk() const;
72 
73     typedef QSharedPointer<TorrentFileStream> Ptr;
74     typedef QWeakPointer<TorrentFileStream> WPtr;
75 
76 protected:
77     qint64 writeData(const char *data, qint64 len) override;
78     qint64 readData(char *data, qint64 maxlen) override;
79     void emitReadChannelFinished();
80 
81 private:
82     void chunkDownloaded(bt::TorrentInterface *tc, bt::Uint32 chunk);
83 
84     class Private;
85     Private *d;
86 };
87 
88 }
89 
90 #endif // BT_TORRENTFILESTREAM_H
91