1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-26 06:25:27 +00:00

Merge pull request #4623 from glassez/libt11

Add libtorrent v1.1 basic support
This commit is contained in:
sledgehammer999 2016-01-31 05:57:10 -06:00
commit f23cd9204c
11 changed files with 108 additions and 39 deletions

View File

@ -26,6 +26,7 @@
* exception statement from your version. * exception statement from your version.
*/ */
#include <libtorrent/version.hpp>
#include "cachestatus.h" #include "cachestatus.h"
using namespace BitTorrent; using namespace BitTorrent;
@ -50,7 +51,11 @@ qreal CacheStatus::readRatio() const
int CacheStatus::jobQueueLength() const int CacheStatus::jobQueueLength() const
{ {
#if LIBTORRENT_VERSION_NUM < 10100
return m_nativeStatus.job_queue_length; return m_nativeStatus.job_queue_length;
#else
return m_nativeStatus.queued_jobs;
#endif
} }
int CacheStatus::averageJobTime() const int CacheStatus::averageJobTime() const

View File

@ -29,8 +29,8 @@
#ifndef BITTORRENT_CACHESTATUS_H #ifndef BITTORRENT_CACHESTATUS_H
#define BITTORRENT_CACHESTATUS_H #define BITTORRENT_CACHESTATUS_H
#include <libtorrent/disk_io_thread.hpp>
#include <QtGlobal> #include <QtGlobal>
#include <libtorrent/disk_io_thread.hpp>
namespace BitTorrent namespace BitTorrent
{ {

View File

@ -47,6 +47,8 @@ using namespace BitTorrent;
#include <queue> #include <queue>
#include <vector> #include <vector>
#include <boost/bind.hpp>
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
#include <libtorrent/lazy_entry.hpp> #include <libtorrent/lazy_entry.hpp>
#include <libtorrent/bencode.hpp> #include <libtorrent/bencode.hpp>
@ -358,7 +360,12 @@ void Session::setSessionSettings()
sessionSettings.active_lsd_limit = -1; sessionSettings.active_lsd_limit = -1;
// Outgoing ports // Outgoing ports
#if LIBTORRENT_VERSION_NUM < 10100
sessionSettings.outgoing_ports = std::make_pair(pref->outgoingPortsMin(), pref->outgoingPortsMax()); sessionSettings.outgoing_ports = std::make_pair(pref->outgoingPortsMin(), pref->outgoingPortsMax());
#else
sessionSettings.outgoing_port = pref->outgoingPortsMin();
sessionSettings.num_outgoing_ports = pref->outgoingPortsMax() - pref->outgoingPortsMin();
#endif
// Ignore limits on LAN // Ignore limits on LAN
qDebug() << "Ignore limits on LAN" << pref->getIgnoreLimitsOnLAN(); qDebug() << "Ignore limits on LAN" << pref->getIgnoreLimitsOnLAN();
sessionSettings.ignore_limits_on_local_network = pref->getIgnoreLimitsOnLAN(); sessionSettings.ignore_limits_on_local_network = pref->getIgnoreLimitsOnLAN();
@ -951,10 +958,8 @@ bool Session::addTorrent(QString source, const AddTorrentParams &params)
adjustLimits(); adjustLimits();
// use common 2nd step of torrent addition // use common 2nd step of torrent addition
libt::add_torrent_alert *alert = new libt::add_torrent_alert(handle, libt::add_torrent_params(), libt::error_code());
m_addingTorrents.insert(hash, addDataFromParams(params)); m_addingTorrents.insert(hash, addDataFromParams(params));
handleAddTorrentAlert(alert); createTorrentHandle(handle);
delete alert;
return true; return true;
} }
@ -2060,26 +2065,18 @@ void Session::dispatchTorrentAlert(libt::alert *a)
torrent->handleAlert(a); torrent->handleAlert(a);
} }
void Session::handleAddTorrentAlert(libtorrent::add_torrent_alert *p) void Session::createTorrentHandle(const libt::torrent_handle &nativeHandle)
{ {
Logger *const logger = Logger::instance();
if (p->error) {
qDebug("/!\\ Error: Failed to add torrent!");
QString msg = Utils::String::fromStdString(p->message());
logger->addMessage(tr("Couldn't add torrent. Reason: %1").arg(msg), Log::WARNING);
emit addTorrentFailed(msg);
return;
}
// Magnet added for preload its metadata // Magnet added for preload its metadata
if (!m_addingTorrents.contains(p->handle.info_hash())) return; if (!m_addingTorrents.contains(nativeHandle.info_hash())) return;
AddTorrentData data = m_addingTorrents.take(p->handle.info_hash()); AddTorrentData data = m_addingTorrents.take(nativeHandle.info_hash());
TorrentHandle *const torrent = new TorrentHandle(this, p->handle, data); TorrentHandle *const torrent = new TorrentHandle(this, nativeHandle, data);
m_torrents.insert(torrent->hash(), torrent); m_torrents.insert(torrent->hash(), torrent);
Preferences *const pref = Preferences::instance(); Preferences *const pref = Preferences::instance();
Logger *const logger = Logger::instance();
bool fromMagnetUri = !torrent->hasMetadata(); bool fromMagnetUri = !torrent->hasMetadata();
@ -2133,7 +2130,20 @@ void Session::handleAddTorrentAlert(libtorrent::add_torrent_alert *p)
emit torrentAdded(torrent); emit torrentAdded(torrent);
} }
void Session::handleTorrentRemovedAlert(libtorrent::torrent_removed_alert *p) void Session::handleAddTorrentAlert(libt::add_torrent_alert *p)
{
if (p->error) {
qDebug("/!\\ Error: Failed to add torrent!");
QString msg = Utils::String::fromStdString(p->message());
Logger::instance()->addMessage(tr("Couldn't add torrent. Reason: %1").arg(msg), Log::WARNING);
emit addTorrentFailed(msg);
}
else {
createTorrentHandle(p->handle);
}
}
void Session::handleTorrentRemovedAlert(libt::torrent_removed_alert *p)
{ {
if (m_loadedMetadata.contains(p->info_hash)) if (m_loadedMetadata.contains(p->info_hash))
emit metadataLoaded(m_loadedMetadata.take(p->info_hash)); emit metadataLoaded(m_loadedMetadata.take(p->info_hash));
@ -2256,7 +2266,6 @@ void Session::handleListenSucceededAlert(libt::listen_succeeded_alert *p)
void Session::handleListenFailedAlert(libt::listen_failed_alert *p) void Session::handleListenFailedAlert(libt::listen_failed_alert *p)
{ {
boost::system::error_code ec;
QString proto = "TCP"; QString proto = "TCP";
if (p->sock_type == libt::listen_failed_alert::udp) if (p->sock_type == libt::listen_failed_alert::udp)
proto = "UDP"; proto = "UDP";
@ -2268,12 +2277,24 @@ void Session::handleListenFailedAlert(libt::listen_failed_alert *p)
proto = "I2P"; proto = "I2P";
else if (p->sock_type == libt::listen_failed_alert::socks5) else if (p->sock_type == libt::listen_failed_alert::socks5)
proto = "SOCKS5"; proto = "SOCKS5";
#if LIBTORRENT_VERSION_NUM < 10100
boost::system::error_code ec;
qDebug() << "Failed listening on " << proto << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); qDebug() << "Failed listening on " << proto << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port();
Logger::instance()->addMessage( Logger::instance()->addMessage(
tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4.", tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4.",
"e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use") "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use.")
.arg(p->endpoint.address().to_string(ec).c_str()).arg(proto).arg(QString::number(p->endpoint.port())) .arg(p->endpoint.address().to_string(ec).c_str()).arg(proto).arg(QString::number(p->endpoint.port()))
.arg(QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL); .arg(QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL);
#else
qDebug() << "Failed listening on " << proto << p->listen_interface();
Logger::instance()->addMessage(
tr("qBittorrent failed listening on interface %1 [%2]. Reason: %3.",
"e.g: qBittorrent failed listening on interface 192.168.0.1 [TCP]. Reason: already in use.")
.arg(QString::fromUtf8(p->listen_interface()))
.arg(proto)
.arg(Utils::String::fromStdString(p->error.message())), Log::CRITICAL);
#endif
} }
void Session::handleExternalIPAlert(libt::external_ip_alert *p) void Session::handleExternalIPAlert(libt::external_ip_alert *p)

View File

@ -38,6 +38,8 @@
#include <QWaitCondition> #include <QWaitCondition>
#include <QNetworkConfigurationManager> #include <QNetworkConfigurationManager>
#include <libtorrent/version.hpp>
#include "base/tristatebool.h" #include "base/tristatebool.h"
#include "base/types.h" #include "base/types.h"
#include "torrentinfo.h" #include "torrentinfo.h"
@ -45,13 +47,24 @@
namespace libtorrent namespace libtorrent
{ {
class session; class session;
struct torrent_handle;
class entry; class entry;
struct add_torrent_params; struct add_torrent_params;
struct pe_settings; struct pe_settings;
struct proxy_settings;
struct session_settings; struct session_settings;
struct session_status; struct session_status;
#if LIBTORRENT_VERSION_NUM < 10100
struct proxy_settings;
#else
namespace aux
{
struct proxy_settings;
}
typedef aux::proxy_settings proxy_settings;
#endif
class alert; class alert;
struct torrent_alert; struct torrent_alert;
struct state_update_alert; struct state_update_alert;
@ -313,6 +326,8 @@ namespace BitTorrent
void handleListenFailedAlert(libtorrent::listen_failed_alert *p); void handleListenFailedAlert(libtorrent::listen_failed_alert *p);
void handleExternalIPAlert(libtorrent::external_ip_alert *p); void handleExternalIPAlert(libtorrent::external_ip_alert *p);
void createTorrentHandle(const libtorrent::torrent_handle &nativeHandle);
void saveResumeData(); void saveResumeData();
bool writeResumeDataFile(TorrentHandle *const torrent, const libtorrent::entry &data); bool writeResumeDataFile(TorrentHandle *const torrent, const libtorrent::entry &data);

View File

@ -55,7 +55,7 @@ using namespace BitTorrent;
// name starts with a . // name starts with a .
bool fileFilter(const std::string &f) bool fileFilter(const std::string &f)
{ {
return (libt::filename(f)[0] != '.'); return !Utils::Fs::fileName(Utils::String::fromStdString(f)).startsWith('.');
} }
TorrentCreatorThread::TorrentCreatorThread(QObject *parent) TorrentCreatorThread::TorrentCreatorThread(QObject *parent)

View File

@ -40,6 +40,10 @@
#include <libtorrent/alert_types.hpp> #include <libtorrent/alert_types.hpp>
#include <libtorrent/create_torrent.hpp> #include <libtorrent/create_torrent.hpp>
#include <libtorrent/magnet_uri.hpp> #include <libtorrent/magnet_uri.hpp>
#if LIBTORRENT_VERSION_NUM >= 10100
#include <libtorrent/time.hpp>
#endif
#include <boost/bind.hpp> #include <boost/bind.hpp>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@ -433,7 +437,7 @@ bool TorrentHandle::connectPeer(const PeerAddress &peerAddress)
libt::address addr = libt::address::from_string(Utils::String::toStdString(peerAddress.ip.toString()), ec); libt::address addr = libt::address::from_string(Utils::String::toStdString(peerAddress.ip.toString()), ec);
if (ec) return false; if (ec) return false;
libt::asio::ip::tcp::endpoint ep(addr, peerAddress.port); boost::asio::ip::tcp::endpoint ep(addr, peerAddress.port);
SAFE_CALL_BOOL(connect_peer, ep); SAFE_CALL_BOOL(connect_peer, ep);
} }
@ -847,7 +851,7 @@ qulonglong TorrentHandle::eta() const
QVector<qreal> TorrentHandle::filesProgress() const QVector<qreal> TorrentHandle::filesProgress() const
{ {
std::vector<libt::size_type> fp; std::vector<boost::int64_t> fp;
QVector<qreal> result; QVector<qreal> result;
SAFE_CALL(file_progress, fp, libt::torrent_handle::piece_granularity); SAFE_CALL(file_progress, fp, libt::torrent_handle::piece_granularity);
@ -1022,9 +1026,9 @@ qreal TorrentHandle::maxRatio(bool *usesGlobalRatio) const
qreal TorrentHandle::realRatio() const qreal TorrentHandle::realRatio() const
{ {
libt::size_type upload = m_nativeStatus.all_time_upload; boost::int64_t upload = m_nativeStatus.all_time_upload;
// special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent // special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent
libt::size_type download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download; boost::int64_t download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download;
if (download == 0) if (download == 0)
return (upload == 0) ? 0.0 : MAX_RATIO; return (upload == 0) ? 0.0 : MAX_RATIO;
@ -1066,7 +1070,11 @@ int TorrentHandle::connectionsLimit() const
qlonglong TorrentHandle::nextAnnounce() const qlonglong TorrentHandle::nextAnnounce() const
{ {
#if LIBTORRENT_VERSION_NUM < 10100
return m_nativeStatus.next_announce.total_seconds(); return m_nativeStatus.next_announce.total_seconds();
#else
return libt::duration_cast<libt::seconds>(m_nativeStatus.next_announce).count();
#endif
} }
void TorrentHandle::setName(const QString &name) void TorrentHandle::setName(const QString &name)
@ -1689,8 +1697,11 @@ libtorrent::torrent_handle TorrentHandle::nativeHandle() const
void TorrentHandle::updateTorrentInfo() void TorrentHandle::updateTorrentInfo()
{ {
if (!hasMetadata()) return; if (!hasMetadata()) return;
#if LIBTORRENT_VERSION_NUM < 10100
m_torrentInfo = TorrentInfo(m_nativeStatus.torrent_file); m_torrentInfo = TorrentInfo(m_nativeStatus.torrent_file);
#else
m_torrentInfo = TorrentInfo(m_nativeStatus.torrent_file.lock());
#endif
} }
void TorrentHandle::initialize() void TorrentHandle::initialize()

View File

@ -38,6 +38,11 @@
#include <QHash> #include <QHash>
#include <libtorrent/torrent_handle.hpp> #include <libtorrent/torrent_handle.hpp>
#include <libtorrent/version.hpp>
#if LIBTORRENT_VERSION_NUM >= 10100
#include <libtorrent/torrent_status.hpp>
#endif
#include <boost/function.hpp> #include <boost/function.hpp>
#include "base/tristatebool.h" #include "base/tristatebool.h"

View File

@ -43,8 +43,8 @@
namespace libt = libtorrent; namespace libt = libtorrent;
using namespace BitTorrent; using namespace BitTorrent;
TorrentInfo::TorrentInfo(boost::intrusive_ptr<const libt::torrent_info> nativeInfo) TorrentInfo::TorrentInfo(NativeConstPtr nativeInfo)
: m_nativeInfo(const_cast<libt::torrent_info *>(nativeInfo.get())) : m_nativeInfo(nativeInfo)
{ {
} }
@ -63,7 +63,7 @@ TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString &error)
{ {
error.clear(); error.clear();
libt::error_code ec; libt::error_code ec;
TorrentInfo info(new libt::torrent_info(Utils::String::toStdString(Utils::Fs::toNativePath(path)), ec)); TorrentInfo info(NativePtr(new libt::torrent_info(Utils::String::toStdString(Utils::Fs::toNativePath(path)), ec)));
if (ec) { if (ec) {
error = QString::fromUtf8(ec.message().c_str()); error = QString::fromUtf8(ec.message().c_str());
qDebug("Cannot load .torrent file: %s", qPrintable(error)); qDebug("Cannot load .torrent file: %s", qPrintable(error));
@ -214,10 +214,10 @@ QByteArray TorrentInfo::metadata() const
void TorrentInfo::renameFile(uint index, const QString &newPath) void TorrentInfo::renameFile(uint index, const QString &newPath)
{ {
if (!isValid()) return; if (!isValid()) return;
m_nativeInfo->rename_file(index, Utils::String::toStdString(newPath)); nativeInfo()->rename_file(index, Utils::String::toStdString(newPath));
} }
boost::intrusive_ptr<libtorrent::torrent_info> TorrentInfo::nativeInfo() const TorrentInfo::NativePtr TorrentInfo::nativeInfo() const
{ {
return m_nativeInfo; return *reinterpret_cast<const NativePtr *>(&m_nativeInfo);
} }

View File

@ -30,7 +30,9 @@
#define BITTORRENT_TORRENTINFO_H #define BITTORRENT_TORRENTINFO_H
#include <QtGlobal> #include <QtGlobal>
#include <libtorrent/torrent_info.hpp> #include <libtorrent/torrent_info.hpp>
#include <libtorrent/version.hpp>
class QString; class QString;
class QUrl; class QUrl;
@ -47,7 +49,15 @@ namespace BitTorrent
class TorrentInfo class TorrentInfo
{ {
public: public:
explicit TorrentInfo(boost::intrusive_ptr<const libtorrent::torrent_info> nativeInfo = boost::intrusive_ptr<const libtorrent::torrent_info>()); #if LIBTORRENT_VERSION_NUM < 10100
typedef boost::intrusive_ptr<const libtorrent::torrent_info> NativeConstPtr;
typedef boost::intrusive_ptr<libtorrent::torrent_info> NativePtr;
#else
typedef boost::shared_ptr<const libtorrent::torrent_info> NativeConstPtr;
typedef boost::shared_ptr<libtorrent::torrent_info> NativePtr;
#endif
explicit TorrentInfo(NativeConstPtr nativeInfo = NativeConstPtr());
TorrentInfo(const TorrentInfo &other); TorrentInfo(const TorrentInfo &other);
static TorrentInfo loadFromFile(const QString &path, QString &error); static TorrentInfo loadFromFile(const QString &path, QString &error);
@ -77,10 +87,11 @@ namespace BitTorrent
QByteArray metadata() const; QByteArray metadata() const;
void renameFile(uint index, const QString &newPath); void renameFile(uint index, const QString &newPath);
boost::intrusive_ptr<libtorrent::torrent_info> nativeInfo() const;
NativePtr nativeInfo() const;
private: private:
boost::intrusive_ptr<libtorrent::torrent_info> m_nativeInfo; NativeConstPtr m_nativeInfo;
}; };
} }

View File

@ -30,6 +30,10 @@
#define BITTORRENT_TRACKERENTRY_H #define BITTORRENT_TRACKERENTRY_H
#include <libtorrent/torrent_info.hpp> #include <libtorrent/torrent_info.hpp>
#include <libtorrent/version.hpp>
#if LIBTORRENT_VERSION_NUM >= 10100
#include <libtorrent/announce_entry.hpp>
#endif
class QString; class QString;

View File

@ -15,9 +15,6 @@ QMAKE_LFLAGS += "/OPT:REF /OPT:ICF"
RC_FILE = qbittorrent.rc RC_FILE = qbittorrent.rc
# Enable Wide characters
DEFINES += TORRENT_USE_WPATH
# Adapt the lib names/versions accordingly # Adapt the lib names/versions accordingly
CONFIG(debug, debug|release) { CONFIG(debug, debug|release) {
LIBS += libtorrentd.lib \ LIBS += libtorrentd.lib \