2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 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.
35 #include <libtorrent/add_torrent_params.hpp>
36 #include <libtorrent/fwd.hpp>
37 #include <libtorrent/torrent_handle.hpp>
38 #include <libtorrent/torrent_info.hpp>
39 #include <libtorrent/torrent_status.hpp>
50 #include "base/path.h"
51 #include "base/tagset.h"
53 #include "speedmonitor.h"
55 #include "torrentcontentlayout.h"
56 #include "torrentinfo.h"
57 #include "trackerentry.h"
62 struct LoadTorrentParams
;
64 enum class MoveStorageMode
71 enum class MaintenanceJob
80 lt::operation_t operation
;
83 class TorrentImpl final
: public Torrent
86 Q_DISABLE_COPY_MOVE(TorrentImpl
)
89 TorrentImpl(SessionImpl
*session
, lt::session
*nativeSession
90 , const lt::torrent_handle
&nativeHandle
, const LoadTorrentParams
¶ms
);
91 ~TorrentImpl() override
;
95 InfoHash
infoHash() const override
;
96 QString
name() const override
;
97 QDateTime
creationDate() const override
;
98 QString
creator() const override
;
99 QString
comment() const override
;
100 bool isPrivate() const override
;
101 qlonglong
totalSize() const override
;
102 qlonglong
wantedSize() const override
;
103 qlonglong
completedSize() const override
;
104 qlonglong
pieceLength() const override
;
105 qlonglong
wastedSize() const override
;
106 QString
currentTracker() const override
;
108 bool isAutoTMMEnabled() const override
;
109 void setAutoTMMEnabled(bool enabled
) override
;
110 Path
savePath() const override
;
111 void setSavePath(const Path
&path
) override
;
112 Path
downloadPath() const override
;
113 void setDownloadPath(const Path
&path
) override
;
114 Path
actualStorageLocation() const override
;
115 Path
rootPath() const override
;
116 Path
contentPath() const override
;
117 QString
category() const override
;
118 bool belongsToCategory(const QString
&category
) const override
;
119 bool setCategory(const QString
&category
) override
;
121 TagSet
tags() const override
;
122 bool hasTag(const QString
&tag
) const override
;
123 bool addTag(const QString
&tag
) override
;
124 bool removeTag(const QString
&tag
) override
;
125 void removeAllTags() override
;
127 int filesCount() const override
;
128 int piecesCount() const override
;
129 int piecesHave() const override
;
130 qreal
progress() const override
;
131 QDateTime
addedTime() const override
;
132 qreal
ratioLimit() const override
;
133 int seedingTimeLimit() const override
;
135 Path
filePath(int index
) const override
;
136 Path
actualFilePath(int index
) const override
;
137 qlonglong
fileSize(int index
) const override
;
138 PathList
filePaths() const override
;
139 QVector
<DownloadPriority
> filePriorities() const override
;
141 TorrentInfo
info() const override
;
142 bool isFinished() const override
;
143 bool isPaused() const override
;
144 bool isQueued() const override
;
145 bool isForced() const override
;
146 bool isChecking() const override
;
147 bool isDownloading() const override
;
148 bool isMoving() const override
;
149 bool isUploading() const override
;
150 bool isCompleted() const override
;
151 bool isActive() const override
;
152 bool isInactive() const override
;
153 bool isErrored() const override
;
154 bool isSequentialDownload() const override
;
155 bool hasFirstLastPiecePriority() const override
;
156 TorrentState
state() const override
;
157 bool hasMetadata() const override
;
158 bool hasMissingFiles() const override
;
159 bool hasError() const override
;
160 int queuePosition() const override
;
161 QVector
<TrackerEntry
> trackers() const override
;
162 QVector
<QUrl
> urlSeeds() const override
;
163 QString
error() const override
;
164 qlonglong
totalDownload() const override
;
165 qlonglong
totalUpload() const override
;
166 qlonglong
activeTime() const override
;
167 qlonglong
finishedTime() const override
;
168 qlonglong
eta() const override
;
169 QVector
<qreal
> filesProgress() const override
;
170 int seedsCount() const override
;
171 int peersCount() const override
;
172 int leechsCount() const override
;
173 int totalSeedsCount() const override
;
174 int totalPeersCount() const override
;
175 int totalLeechersCount() const override
;
176 QDateTime
lastSeenComplete() const override
;
177 QDateTime
completedTime() const override
;
178 qlonglong
timeSinceUpload() const override
;
179 qlonglong
timeSinceDownload() const override
;
180 qlonglong
timeSinceActivity() const override
;
181 int downloadLimit() const override
;
182 int uploadLimit() const override
;
183 bool superSeeding() const override
;
184 bool isDHTDisabled() const override
;
185 bool isPEXDisabled() const override
;
186 bool isLSDDisabled() const override
;
187 QVector
<PeerInfo
> peers() const override
;
188 QBitArray
pieces() const override
;
189 QBitArray
downloadingPieces() const override
;
190 QVector
<int> pieceAvailability() const override
;
191 qreal
distributedCopies() const override
;
192 qreal
maxRatio() const override
;
193 int maxSeedingTime() const override
;
194 qreal
realRatio() const override
;
195 int uploadPayloadRate() const override
;
196 int downloadPayloadRate() const override
;
197 qlonglong
totalPayloadUpload() const override
;
198 qlonglong
totalPayloadDownload() const override
;
199 int connectionsCount() const override
;
200 int connectionsLimit() const override
;
201 qlonglong
nextAnnounce() const override
;
202 QVector
<qreal
> availableFileFractions() const override
;
204 void setName(const QString
&name
) override
;
205 void setSequentialDownload(bool enable
) override
;
206 void setFirstLastPiecePriority(bool enabled
) override
;
207 void pause() override
;
208 void resume(TorrentOperatingMode mode
= TorrentOperatingMode::AutoManaged
) override
;
209 void forceReannounce(int index
= -1) override
;
210 void forceDHTAnnounce() override
;
211 void forceRecheck() override
;
212 void renameFile(int index
, const Path
&path
) override
;
213 void prioritizeFiles(const QVector
<DownloadPriority
> &priorities
) override
;
214 void setRatioLimit(qreal limit
) override
;
215 void setSeedingTimeLimit(int limit
) override
;
216 void setUploadLimit(int limit
) override
;
217 void setDownloadLimit(int limit
) override
;
218 void setSuperSeeding(bool enable
) override
;
219 void setDHTDisabled(bool disable
) override
;
220 void setPEXDisabled(bool disable
) override
;
221 void setLSDDisabled(bool disable
) override
;
222 void flushCache() const override
;
223 void addTrackers(QVector
<TrackerEntry
> trackers
) override
;
224 void removeTrackers(const QStringList
&trackers
) override
;
225 void replaceTrackers(QVector
<TrackerEntry
> trackers
) override
;
226 void addUrlSeeds(const QVector
<QUrl
> &urlSeeds
) override
;
227 void removeUrlSeeds(const QVector
<QUrl
> &urlSeeds
) override
;
228 bool connectPeer(const PeerAddress
&peerAddress
) override
;
229 void clearPeers() override
;
230 void setMetadata(const TorrentInfo
&torrentInfo
) override
;
232 StopCondition
stopCondition() const override
;
233 void setStopCondition(StopCondition stopCondition
) override
;
235 QString
createMagnetURI() const override
;
236 nonstd::expected
<QByteArray
, QString
> exportToBuffer() const override
;
237 nonstd::expected
<void, QString
> exportToFile(const Path
&path
) const override
;
239 void fetchPeerInfo(std::function
<void (QVector
<PeerInfo
>)> resultHandler
) const override
;
240 void fetchURLSeeds(std::function
<void (QVector
<QUrl
>)> resultHandler
) const override
;
241 void fetchPieceAvailability(std::function
<void (QVector
<int>)> resultHandler
) const override
;
242 void fetchDownloadingPieces(std::function
<void (QBitArray
)> resultHandler
) const override
;
243 void fetchAvailableFileFractions(std::function
<void (QVector
<qreal
>)> resultHandler
) const override
;
245 bool needSaveResumeData() const;
248 lt::torrent_handle
nativeHandle() const;
250 void handleAlert(const lt::alert
*a
);
251 void handleStateUpdate(const lt::torrent_status
&nativeStatus
);
252 void handleCategoryOptionsChanged();
253 void handleAppendExtensionToggled();
254 void saveResumeData(lt::resume_data_flags_t flags
= {});
255 void handleMoveStorageJobFinished(const Path
&path
, bool hasOutstandingJob
);
256 void fileSearchFinished(const Path
&savePath
, const PathList
&fileNames
);
257 TrackerEntry
updateTrackerEntry(const lt::announce_entry
&announceEntry
, const QMap
<TrackerEntry::Endpoint
, int> &updateInfo
);
260 using EventTrigger
= std::function
<void ()>;
262 std::shared_ptr
<const lt::torrent_info
> nativeTorrentInfo() const;
264 void updateStatus(const lt::torrent_status
&nativeStatus
);
265 void updateProgress();
268 void handleFastResumeRejectedAlert(const lt::fastresume_rejected_alert
*p
);
269 void handleFileCompletedAlert(const lt::file_completed_alert
*p
);
270 void handleFileErrorAlert(const lt::file_error_alert
*p
);
271 #ifdef QBT_USES_LIBTORRENT2
272 void handleFilePrioAlert(const lt::file_prio_alert
*p
);
274 void handleFileRenamedAlert(const lt::file_renamed_alert
*p
);
275 void handleFileRenameFailedAlert(const lt::file_rename_failed_alert
*p
);
276 void handleMetadataReceivedAlert(const lt::metadata_received_alert
*p
);
277 void handlePerformanceAlert(const lt::performance_alert
*p
) const;
278 void handleSaveResumeDataAlert(const lt::save_resume_data_alert
*p
);
279 void handleSaveResumeDataFailedAlert(const lt::save_resume_data_failed_alert
*p
);
280 void handleTorrentCheckedAlert(const lt::torrent_checked_alert
*p
);
281 void handleTorrentFinishedAlert(const lt::torrent_finished_alert
*p
);
282 void handleTorrentPausedAlert(const lt::torrent_paused_alert
*p
);
283 void handleTorrentResumedAlert(const lt::torrent_resumed_alert
*p
);
285 bool isMoveInProgress() const;
287 void setAutoManaged(bool enable
);
289 Path
wantedActualPath(int index
, const Path
&path
) const;
290 void adjustStorageLocation();
291 void doRenameFile(int index
, const Path
&path
);
292 void moveStorage(const Path
&newPath
, MoveStorageMode mode
);
293 void manageIncompleteFiles();
294 void applyFirstLastPiecePriority(bool enabled
);
296 void prepareResumeData(const lt::add_torrent_params
¶ms
);
297 void endReceivedMetadataHandling(const Path
&savePath
, const PathList
&fileNames
);
300 nonstd::expected
<lt::entry
, QString
> exportTorrent() const;
302 template <typename Func
, typename Callback
>
303 void invokeAsync(Func func
, Callback resultHandler
) const;
305 SessionImpl
*const m_session
= nullptr;
306 lt::session
*m_nativeSession
= nullptr;
307 lt::torrent_handle m_nativeHandle
;
308 mutable lt::torrent_status m_nativeStatus
;
309 TorrentState m_state
= TorrentState::Unknown
;
310 TorrentInfo m_torrentInfo
;
311 PathList m_filePaths
;
312 QHash
<lt::file_index_t
, int> m_indexMap
;
313 QVector
<DownloadPriority
> m_filePriorities
;
314 QBitArray m_completedFiles
;
315 SpeedMonitor m_payloadRateMonitor
;
319 // m_moveFinishedTriggers is activated only when the following conditions are met:
320 // all file rename jobs complete, all file move jobs complete
321 QQueue
<EventTrigger
> m_moveFinishedTriggers
;
322 int m_renameCount
= 0;
323 bool m_storageIsMoving
= false;
325 QQueue
<EventTrigger
> m_statusUpdatedTriggers
;
327 MaintenanceJob m_maintenanceJob
= MaintenanceJob::None
;
329 QVector
<TrackerEntry
> m_trackerEntries
;
330 QVector
<QUrl
> m_urlSeeds
;
331 FileErrorInfo m_lastFileError
;
340 int m_seedingTimeLimit
;
341 TorrentOperatingMode m_operatingMode
;
342 TorrentContentLayout m_contentLayout
;
343 bool m_hasFinishedStatus
;
344 bool m_hasMissingFiles
= false;
345 bool m_hasFirstLastPiecePriority
= false;
348 StopCondition m_stopCondition
= StopCondition::None
;
350 bool m_unchecked
= false;
352 lt::add_torrent_params m_ltAddTorrentParams
;
354 int m_downloadLimit
= 0;
355 int m_uploadLimit
= 0;
358 QVector
<std::int64_t> m_filesProgress
;