1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef FILEMANAGER_H
52#define FILEMANAGER_H
53
54#include <QBitArray>
55#include <QList>
56#include <QMutex>
57#include <QThread>
58#include <QWaitCondition>
59
60#include "metainfo.h"
61
62QT_BEGIN_NAMESPACE
63class QByteArray;
64class QFile;
65class QTimerEvent;
66QT_END_NAMESPACE
67
68class FileManager : public QThread
69{
70 Q_OBJECT
71
72public:
73 FileManager(QObject *parent = nullptr);
74 virtual ~FileManager();
75
76 inline void setMetaInfo(const MetaInfo &info) { metaInfo = info; }
77 inline void setDestinationFolder(const QString &directory) { destinationPath = directory; }
78
79 int read(int pieceIndex, int offset, int length);
80 void write(int pieceIndex, int offset, const QByteArray &data);
81 void verifyPiece(int pieceIndex);
82 inline qint64 totalSize() const { return totalLength; }
83
84 inline int pieceCount() const { return numPieces; }
85 int pieceLengthAt(int pieceIndex) const;
86
87 QBitArray completedPieces() const;
88 void setCompletedPieces(const QBitArray &pieces);
89
90 QString errorString() const;
91
92public slots:
93 void startDataVerification();
94
95signals:
96 void dataRead(int id, int pieceIndex, int offset, const QByteArray &data);
97 void error();
98 void verificationProgress(int percent);
99 void verificationDone();
100 void pieceVerified(int pieceIndex, bool verified);
101
102protected:
103 void run() override;
104
105private slots:
106 bool verifySinglePiece(int pieceIndex);
107 void wakeUp();
108
109private:
110 bool generateFiles();
111 QByteArray readBlock(int pieceIndex, int offset, int length);
112 bool writeBlock(int pieceIndex, int offset, const QByteArray &data);
113 void verifyFileContents();
114
115 struct WriteRequest {
116 int pieceIndex;
117 int offset;
118 QByteArray data;
119 };
120 struct ReadRequest {
121 int pieceIndex;
122 int offset;
123 int length;
124 int id;
125 };
126
127 QString errString;
128 QString destinationPath;
129 MetaInfo metaInfo;
130 QList<QFile *> files;
131 QList<QByteArray> sha1s;
132 QBitArray verifiedPieces;
133
134 bool newFile;
135 int pieceLength;
136 qint64 totalLength;
137 int numPieces;
138 int readId;
139 bool startVerification;
140 bool quit;
141 bool wokeUp;
142
143 QList<WriteRequest> writeRequests;
144 QList<ReadRequest> readRequests;
145 QList<int> pendingVerificationRequests;
146 QList<int> newPendingVerificationRequests;
147 QList<qint64> fileSizes;
148
149 mutable QMutex mutex;
150 mutable QWaitCondition cond;
151};
152
153#endif
154

source code of qtbase/examples/network/torrent/filemanager.h