2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
32 #include <QtContainerFwd>
37 #include "base/3rdparty/expected.hpp"
38 #include "base/pathfwd.h"
39 #include "base/tagset.h"
40 #include "sharelimitaction.h"
41 #include "torrentcontenthandler.h"
50 enum class DownloadPriority
;
61 struct TrackerEntryStatus
;
63 // Using `Q_ENUM_NS()` without a wrapper namespace in our case is not advised
64 // since `Q_NAMESPACE` cannot be used when the same namespace resides at different files.
65 // https://www.kdab.com/new-qt-5-8-meta-object-support-namespaces/#comment-143779
66 inline namespace TorrentOperatingModeNS
70 enum class TorrentOperatingMode
76 Q_ENUM_NS(TorrentOperatingMode
)
79 enum class TorrentState
85 ForcedDownloadingMetadata
,
109 std::size_t qHash(TorrentState key
, std::size_t seed
= 0);
111 class Torrent
: public TorrentContentHandler
114 Q_DISABLE_COPY_MOVE(Torrent
)
117 enum class StopCondition
120 MetadataReceived
= 1,
123 Q_ENUM(StopCondition
)
125 static const qreal USE_GLOBAL_RATIO
;
126 static const qreal NO_RATIO_LIMIT
;
128 static const int USE_GLOBAL_SEEDING_TIME
;
129 static const int NO_SEEDING_TIME_LIMIT
;
131 static const int USE_GLOBAL_INACTIVE_SEEDING_TIME
;
132 static const int NO_INACTIVE_SEEDING_TIME_LIMIT
;
134 static const qreal MAX_RATIO
;
135 static const int MAX_SEEDING_TIME
;
136 static const int MAX_INACTIVE_SEEDING_TIME
;
138 using TorrentContentHandler::TorrentContentHandler
;
140 virtual Session
*session() const = 0;
142 virtual InfoHash
infoHash() const = 0;
143 virtual QString
name() const = 0;
144 virtual QDateTime
creationDate() const = 0;
145 virtual QString
creator() const = 0;
146 virtual QString
comment() const = 0;
147 virtual bool isPrivate() const = 0;
148 virtual qlonglong
totalSize() const = 0;
149 virtual qlonglong
wantedSize() const = 0;
150 virtual qlonglong
completedSize() const = 0;
151 virtual qlonglong
pieceLength() const = 0;
152 virtual qlonglong
wastedSize() const = 0;
153 virtual QString
currentTracker() const = 0;
155 // 1. savePath() - the path where all the files and subfolders of torrent are stored.
156 // 1.1 downloadPath() - the path where all the files and subfolders of torrent are stored until torrent has finished downloading.
157 // 2. rootPath() - absolute path of torrent file tree (first common subfolder of torrent files); empty string if torrent has no root folder.
158 // 3. contentPath() - absolute path of torrent content (root path for multifile torrents, absolute file path for singlefile torrents).
161 // Suppose we have three torrent with following structures and save path `/home/user/torrents`:
163 // Torrent A (multifile)
174 // Torrent A* (Torrent A in "strip root folder" mode)
177 // Torrent B (singlefile)
184 // Torrent C (singlefile)
190 // | | rootPath | contentPath |
191 // |---|------------------------------|--------------------------------------------|
192 // | A | /home/user/torrents/torrentA | /home/user/torrents/torrentA |
193 // | A*| <empty> | /home/user/torrents |
194 // | B | /home/user/torrents/torrentB | /home/user/torrents/torrentB/subdir1/file1 |
195 // | C | <empty> | /home/user/torrents/file1 |
197 virtual bool isAutoTMMEnabled() const = 0;
198 virtual void setAutoTMMEnabled(bool enabled
) = 0;
199 virtual Path
savePath() const = 0;
200 virtual void setSavePath(const Path
&savePath
) = 0;
201 virtual Path
downloadPath() const = 0;
202 virtual void setDownloadPath(const Path
&downloadPath
) = 0;
203 virtual Path
rootPath() const = 0;
204 virtual Path
contentPath() const = 0;
205 virtual QString
category() const = 0;
206 virtual bool belongsToCategory(const QString
&category
) const = 0;
207 virtual bool setCategory(const QString
&category
) = 0;
209 virtual TagSet
tags() const = 0;
210 virtual bool hasTag(const Tag
&tag
) const = 0;
211 virtual bool addTag(const Tag
&tag
) = 0;
212 virtual bool removeTag(const Tag
&tag
) = 0;
213 virtual void removeAllTags() = 0;
215 virtual int piecesCount() const = 0;
216 virtual int piecesHave() const = 0;
217 virtual qreal
progress() const = 0;
218 virtual QDateTime
addedTime() const = 0;
221 virtual qreal
ratioLimit() const = 0;
222 virtual void setRatioLimit(qreal limit
) = 0;
223 virtual int seedingTimeLimit() const = 0;
224 virtual void setSeedingTimeLimit(int limit
) = 0;
225 virtual int inactiveSeedingTimeLimit() const = 0;
226 virtual void setInactiveSeedingTimeLimit(int limit
) = 0;
227 virtual ShareLimitAction
shareLimitAction() const = 0;
228 virtual void setShareLimitAction(ShareLimitAction action
) = 0;
230 virtual PathList
filePaths() const = 0;
232 virtual TorrentInfo
info() const = 0;
233 virtual bool isFinished() const = 0;
234 virtual bool isStopped() const = 0;
235 virtual bool isQueued() const = 0;
236 virtual bool isForced() const = 0;
237 virtual bool isChecking() const = 0;
238 virtual bool isDownloading() const = 0;
239 virtual bool isMoving() const = 0;
240 virtual bool isUploading() const = 0;
241 virtual bool isCompleted() const = 0;
242 virtual bool isActive() const = 0;
243 virtual bool isInactive() const = 0;
244 virtual bool isErrored() const = 0;
245 virtual bool isSequentialDownload() const = 0;
246 virtual bool hasFirstLastPiecePriority() const = 0;
247 virtual TorrentState
state() const = 0;
248 virtual bool hasMissingFiles() const = 0;
249 virtual bool hasError() const = 0;
250 virtual int queuePosition() const = 0;
251 virtual QVector
<TrackerEntryStatus
> trackers() const = 0;
252 virtual QVector
<QUrl
> urlSeeds() const = 0;
253 virtual QString
error() const = 0;
254 virtual qlonglong
totalDownload() const = 0;
255 virtual qlonglong
totalUpload() const = 0;
256 virtual qlonglong
activeTime() const = 0;
257 virtual qlonglong
finishedTime() const = 0;
258 virtual qlonglong
eta() const = 0;
259 virtual int seedsCount() const = 0;
260 virtual int peersCount() const = 0;
261 virtual int leechsCount() const = 0;
262 virtual int totalSeedsCount() const = 0;
263 virtual int totalPeersCount() const = 0;
264 virtual int totalLeechersCount() const = 0;
265 virtual QDateTime
lastSeenComplete() const = 0;
266 virtual QDateTime
completedTime() const = 0;
267 virtual qlonglong
timeSinceUpload() const = 0;
268 virtual qlonglong
timeSinceDownload() const = 0;
269 virtual qlonglong
timeSinceActivity() const = 0;
270 virtual int downloadLimit() const = 0;
271 virtual int uploadLimit() const = 0;
272 virtual bool superSeeding() const = 0;
273 virtual bool isDHTDisabled() const = 0;
274 virtual bool isPEXDisabled() const = 0;
275 virtual bool isLSDDisabled() const = 0;
276 virtual QVector
<PeerInfo
> peers() const = 0;
277 virtual QBitArray
pieces() const = 0;
278 virtual QBitArray
downloadingPieces() const = 0;
279 virtual QVector
<int> pieceAvailability() const = 0;
280 virtual qreal
distributedCopies() const = 0;
281 virtual qreal
maxRatio() const = 0;
282 virtual int maxSeedingTime() const = 0;
283 virtual int maxInactiveSeedingTime() const = 0;
284 virtual qreal
realRatio() const = 0;
285 virtual int uploadPayloadRate() const = 0;
286 virtual int downloadPayloadRate() const = 0;
287 virtual qlonglong
totalPayloadUpload() const = 0;
288 virtual qlonglong
totalPayloadDownload() const = 0;
289 virtual int connectionsCount() const = 0;
290 virtual int connectionsLimit() const = 0;
291 virtual qlonglong
nextAnnounce() const = 0;
293 virtual void setName(const QString
&name
) = 0;
294 virtual void setSequentialDownload(bool enable
) = 0;
295 virtual void setFirstLastPiecePriority(bool enabled
) = 0;
296 virtual void stop() = 0;
297 virtual void start(TorrentOperatingMode mode
= TorrentOperatingMode::AutoManaged
) = 0;
298 virtual void forceReannounce(int index
= -1) = 0;
299 virtual void forceDHTAnnounce() = 0;
300 virtual void forceRecheck() = 0;
301 virtual void setUploadLimit(int limit
) = 0;
302 virtual void setDownloadLimit(int limit
) = 0;
303 virtual void setSuperSeeding(bool enable
) = 0;
304 virtual void setDHTDisabled(bool disable
) = 0;
305 virtual void setPEXDisabled(bool disable
) = 0;
306 virtual void setLSDDisabled(bool disable
) = 0;
307 virtual void addTrackers(QVector
<TrackerEntry
> trackers
) = 0;
308 virtual void removeTrackers(const QStringList
&trackers
) = 0;
309 virtual void replaceTrackers(QVector
<TrackerEntry
> trackers
) = 0;
310 virtual void addUrlSeeds(const QVector
<QUrl
> &urlSeeds
) = 0;
311 virtual void removeUrlSeeds(const QVector
<QUrl
> &urlSeeds
) = 0;
312 virtual bool connectPeer(const PeerAddress
&peerAddress
) = 0;
313 virtual void clearPeers() = 0;
314 virtual void setMetadata(const TorrentInfo
&torrentInfo
) = 0;
316 virtual StopCondition
stopCondition() const = 0;
317 virtual void setStopCondition(StopCondition stopCondition
) = 0;
318 virtual SSLParameters
getSSLParameters() const = 0;
319 virtual void setSSLParameters(const SSLParameters
&sslParams
) = 0;
321 virtual QString
createMagnetURI() const = 0;
322 virtual nonstd::expected
<QByteArray
, QString
> exportToBuffer() const = 0;
323 virtual nonstd::expected
<void, QString
> exportToFile(const Path
&path
) const = 0;
325 virtual void fetchPeerInfo(std::function
<void (QVector
<PeerInfo
>)> resultHandler
) const = 0;
326 virtual void fetchURLSeeds(std::function
<void (QVector
<QUrl
>)> resultHandler
) const = 0;
327 virtual void fetchPieceAvailability(std::function
<void (QVector
<int>)> resultHandler
) const = 0;
328 virtual void fetchDownloadingPieces(std::function
<void (QBitArray
)> resultHandler
) const = 0;
330 TorrentID
id() const;
331 bool isRunning() const;
332 qlonglong
remainingSize() const;
334 void toggleSequentialDownload();
335 void toggleFirstLastPiecePriority();
339 Q_DECLARE_METATYPE(BitTorrent::TorrentState
)