mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-23 04:54:18 +00:00
Use newer libtorrent API
This commit is contained in:
parent
33b225ac6d
commit
c4dbe84832
@ -65,6 +65,10 @@
|
|||||||
#include <libtorrent/torrent_info.hpp>
|
#include <libtorrent/torrent_info.hpp>
|
||||||
#include <libtorrent/version.hpp>
|
#include <libtorrent/version.hpp>
|
||||||
|
|
||||||
|
#if (LIBTORRENT_VERSION_NUM >= 10200)
|
||||||
|
#include <libtorrent/read_resume_data.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "base/algorithm.h"
|
#include "base/algorithm.h"
|
||||||
#include "base/exceptions.h"
|
#include "base/exceptions.h"
|
||||||
#include "base/global.h"
|
#include "base/global.h"
|
||||||
@ -1843,17 +1847,15 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
|
|||||||
params.savePath = normalizeSavePath(params.savePath, "");
|
params.savePath = normalizeSavePath(params.savePath, "");
|
||||||
|
|
||||||
if (!params.category.isEmpty()) {
|
if (!params.category.isEmpty()) {
|
||||||
if (!m_categories.contains(params.category) && !addCategory(params.category)) {
|
if (!m_categories.contains(params.category) && !addCategory(params.category))
|
||||||
qWarning() << "Couldn't create category" << params.category;
|
|
||||||
params.category = "";
|
params.category = "";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool fromMagnetUri = magnetUri.isValid();
|
||||||
// If empty then Automatic mode, otherwise Manual mode
|
// If empty then Automatic mode, otherwise Manual mode
|
||||||
QString savePath = params.savePath.isEmpty() ? categorySavePath(params.category) : params.savePath;
|
QString savePath = params.savePath.isEmpty() ? categorySavePath(params.category) : params.savePath;
|
||||||
lt::add_torrent_params p;
|
lt::add_torrent_params p;
|
||||||
InfoHash hash;
|
InfoHash hash;
|
||||||
const bool fromMagnetUri = magnetUri.isValid();
|
|
||||||
|
|
||||||
if (fromMagnetUri) {
|
if (fromMagnetUri) {
|
||||||
hash = magnetUri.hash();
|
hash = magnetUri.hash();
|
||||||
@ -1887,8 +1889,58 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
|
|||||||
|
|
||||||
p = magnetUri.addTorrentParams();
|
p = magnetUri.addTorrentParams();
|
||||||
}
|
}
|
||||||
else if (torrentInfo.isValid()) {
|
else {
|
||||||
if (!params.restored) {
|
if (!torrentInfo.isValid()) {
|
||||||
|
// We can have an invalid torrentInfo when there isn't a matching
|
||||||
|
// .torrent file to the .fastresume we loaded. Possibly from a
|
||||||
|
// failed upgrade.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = torrentInfo.hash();
|
||||||
|
}
|
||||||
|
|
||||||
|
// We should not add the torrent if it is already
|
||||||
|
// processed or is pending to add to session
|
||||||
|
if (m_addingTorrents.contains(hash) || m_loadedMetadata.contains(hash))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
TorrentHandle *const torrent = m_torrents.value(hash);
|
||||||
|
if (torrent) { // a duplicate torrent is added
|
||||||
|
if (torrent->isPrivate() || (!fromMagnetUri && torrentInfo.isPrivate()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// merge trackers and web seeds
|
||||||
|
torrent->addTrackers(fromMagnetUri ? magnetUri.trackers() : torrentInfo.trackers());
|
||||||
|
torrent->addUrlSeeds(fromMagnetUri ? magnetUri.urlSeeds() : torrentInfo.urlSeeds());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record if .fastresume is complete, that is whether it contains
|
||||||
|
// the required fields to resume a torrent.
|
||||||
|
bool hasCompleteFastresume = false;
|
||||||
|
|
||||||
|
if (!fromMagnetUri) {
|
||||||
|
if (params.restored) { // load from existing fastresume
|
||||||
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
|
p.resume_data = std::vector<char> {fastresumeData.constData()
|
||||||
|
, (fastresumeData.constData() + fastresumeData.size())};
|
||||||
|
p.flags |= lt::add_torrent_params::flag_use_resume_save_path;
|
||||||
|
|
||||||
|
// Still setup the default parameters and let libtorrent handle
|
||||||
|
// the parameter merging
|
||||||
|
hasCompleteFastresume = false;
|
||||||
|
#else
|
||||||
|
lt::error_code ec;
|
||||||
|
p = lt::read_resume_data(fastresumeData, ec);
|
||||||
|
|
||||||
|
// libtorrent will always apply `file_priorities` to torrents,
|
||||||
|
// if the field is present then the fastresume is considered to
|
||||||
|
// be correctly generated and should be complete.
|
||||||
|
hasCompleteFastresume = !p.file_priorities.empty();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else { // new torrent
|
||||||
if (!params.hasRootFolder)
|
if (!params.hasRootFolder)
|
||||||
torrentInfo.stripRootFolder();
|
torrentInfo.stripRootFolder();
|
||||||
|
|
||||||
@ -1906,77 +1958,10 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
|
|||||||
if (!contentName.isEmpty() && (contentName != torrentInfo.name()))
|
if (!contentName.isEmpty() && (contentName != torrentInfo.name()))
|
||||||
params.name = contentName;
|
params.name = contentName;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
p.ti = torrentInfo.nativeInfo();
|
|
||||||
hash = torrentInfo.hash();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// We can have an invalid torrentInfo when there isn't a matching
|
|
||||||
// .torrent file to the .fastresume we loaded. Possibly from a
|
|
||||||
// failed upgrade.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We should not add torrent if it already
|
|
||||||
// processed or adding to session
|
|
||||||
if (m_addingTorrents.contains(hash) || m_loadedMetadata.contains(hash)) return false;
|
|
||||||
|
|
||||||
TorrentHandle *const torrent = m_torrents.value(hash);
|
|
||||||
if (torrent) {
|
|
||||||
if (torrent->isPrivate() || (!fromMagnetUri && torrentInfo.isPrivate()))
|
|
||||||
return false;
|
|
||||||
torrent->addTrackers(fromMagnetUri ? magnetUri.trackers() : torrentInfo.trackers());
|
|
||||||
torrent->addUrlSeeds(fromMagnetUri ? magnetUri.urlSeeds() : torrentInfo.urlSeeds());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
qDebug("Adding torrent...");
|
|
||||||
qDebug(" -> Hash: %s", qUtf8Printable(hash));
|
|
||||||
|
|
||||||
// Preallocation mode
|
|
||||||
if (isPreallocationEnabled())
|
|
||||||
p.storage_mode = lt::storage_mode_allocate;
|
|
||||||
else
|
|
||||||
p.storage_mode = lt::storage_mode_sparse;
|
|
||||||
|
|
||||||
#if (LIBTORRENT_VERSION_NUM < 10200)
|
|
||||||
p.flags |= lt::add_torrent_params::flag_paused; // Start in pause
|
|
||||||
p.flags &= ~lt::add_torrent_params::flag_auto_managed; // Because it is added in paused state
|
|
||||||
p.flags &= ~lt::add_torrent_params::flag_duplicate_is_error; // Already checked
|
|
||||||
#else
|
|
||||||
p.flags |= lt::torrent_flags::paused; // Start in pause
|
|
||||||
p.flags &= ~lt::torrent_flags::auto_managed; // Because it is added in paused state
|
|
||||||
p.flags &= ~lt::torrent_flags::duplicate_is_error; // Already checked
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Seeding mode
|
|
||||||
// Skip checking and directly start seeding (new in libtorrent v0.15)
|
|
||||||
if (params.skipChecking) {
|
|
||||||
#if (LIBTORRENT_VERSION_NUM < 10200)
|
|
||||||
p.flags |= lt::add_torrent_params::flag_seed_mode;
|
|
||||||
#else
|
|
||||||
p.flags |= lt::torrent_flags::seed_mode;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
#if (LIBTORRENT_VERSION_NUM < 10200)
|
|
||||||
p.flags &= ~lt::add_torrent_params::flag_seed_mode;
|
|
||||||
#else
|
|
||||||
p.flags &= ~lt::torrent_flags::seed_mode;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fromMagnetUri) {
|
|
||||||
if (params.restored) {
|
|
||||||
// Set torrent fast resume data
|
|
||||||
p.resume_data = std::vector<char> {fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size()};
|
|
||||||
p.flags |= lt::add_torrent_params::flag_use_resume_save_path;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Q_ASSERT(p.file_priorities.empty());
|
Q_ASSERT(p.file_priorities.empty());
|
||||||
std::transform(params.filePriorities.cbegin(), params.filePriorities.cend()
|
std::transform(params.filePriorities.cbegin(), params.filePriorities.cend()
|
||||||
, std::back_inserter(p.file_priorities), [](DownloadPriority priority)
|
, std::back_inserter(p.file_priorities), [](const DownloadPriority priority)
|
||||||
{
|
{
|
||||||
#if (LIBTORRENT_VERSION_NUM < 10200)
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
return static_cast<boost::uint8_t>(priority);
|
return static_cast<boost::uint8_t>(priority);
|
||||||
@ -1986,28 +1971,65 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
|
|||||||
#endif
|
#endif
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.ti = torrentInfo.nativeInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.restored && !params.paused) {
|
if (!hasCompleteFastresume) {
|
||||||
// Make sure the torrent will restored in "paused" state
|
// Common
|
||||||
// Then we will start it if needed
|
|
||||||
#if (LIBTORRENT_VERSION_NUM < 10200)
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
p.flags |= lt::add_torrent_params::flag_stop_when_ready;
|
p.flags |= lt::add_torrent_params::flag_paused; // Start in pause
|
||||||
|
p.flags &= ~lt::add_torrent_params::flag_auto_managed; // Because it is added in paused state
|
||||||
|
p.flags &= ~lt::add_torrent_params::flag_duplicate_is_error; // Already checked
|
||||||
#else
|
#else
|
||||||
p.flags |= lt::torrent_flags::stop_when_ready;
|
p.flags |= lt::torrent_flags::paused; // Start in pause
|
||||||
|
p.flags &= ~lt::torrent_flags::auto_managed; // Because it is added in paused state
|
||||||
|
p.flags &= ~lt::torrent_flags::duplicate_is_error; // Already checked
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
// Limits
|
// Limits
|
||||||
p.max_connections = maxConnectionsPerTorrent();
|
p.max_connections = maxConnectionsPerTorrent();
|
||||||
p.max_uploads = maxUploadsPerTorrent();
|
p.max_uploads = maxUploadsPerTorrent();
|
||||||
p.save_path = Utils::Fs::toNativePath(savePath).toStdString();
|
p.save_path = Utils::Fs::toNativePath(savePath).toStdString();
|
||||||
p.upload_limit = params.uploadLimit;
|
p.upload_limit = params.uploadLimit;
|
||||||
p.download_limit = params.downloadLimit;
|
p.download_limit = params.downloadLimit;
|
||||||
|
|
||||||
|
// Preallocation mode
|
||||||
|
p.storage_mode = isPreallocationEnabled()
|
||||||
|
? lt::storage_mode_allocate : lt::storage_mode_sparse;
|
||||||
|
|
||||||
|
// Seeding mode
|
||||||
|
// Skip checking and directly start seeding
|
||||||
|
if (params.skipChecking) {
|
||||||
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
|
p.flags |= lt::add_torrent_params::flag_seed_mode;
|
||||||
|
#else
|
||||||
|
p.flags |= lt::torrent_flags::seed_mode;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
|
p.flags &= ~lt::add_torrent_params::flag_seed_mode;
|
||||||
|
#else
|
||||||
|
p.flags &= ~lt::torrent_flags::seed_mode;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.restored && !params.paused) {
|
||||||
|
// Make sure the torrent will restored in "paused" state
|
||||||
|
// Then we will start it if needed
|
||||||
|
#if (LIBTORRENT_VERSION_NUM < 10200)
|
||||||
|
p.flags |= lt::add_torrent_params::flag_stop_when_ready;
|
||||||
|
#else
|
||||||
|
p.flags |= lt::torrent_flags::stop_when_ready;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_addingTorrents.insert(hash, params);
|
m_addingTorrents.insert(hash, params);
|
||||||
// Adding torrent to BitTorrent session
|
// Adding torrent to BitTorrent session
|
||||||
m_nativeSession->async_add_torrent(p);
|
m_nativeSession->async_add_torrent(p);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user