Browse Source

Import new trackers from magnet link in case of duplicate torrent (closes #111)

adaptive-webui-19844
Christophe Dumez 12 years ago
parent
commit
806ab07865
  1. 20
      src/misc.cpp
  2. 6
      src/misc.h
  3. 28
      src/qtlibtorrent/qbtsession.cpp
  4. 3
      src/qtlibtorrent/qbtsession.h

20
src/misc.cpp

@ -309,7 +309,7 @@ QString misc::bcLinkToMagnet(QString bc_link) { @@ -309,7 +309,7 @@ QString misc::bcLinkToMagnet(QString bc_link) {
return magnet;
}
QString misc::magnetUriToName(QString magnet_uri) {
QString misc::magnetUriToName(const QString& magnet_uri) {
QString name = "";
QRegExp regHex("dn=([^&]+)");
const int pos = regHex.indexIn(magnet_uri);
@ -321,7 +321,23 @@ QString misc::magnetUriToName(QString magnet_uri) { @@ -321,7 +321,23 @@ QString misc::magnetUriToName(QString magnet_uri) {
return name;
}
QString misc::magnetUriToHash(QString magnet_uri) {
QList<QUrl> misc::magnetUriToTrackers(const QString& magnet_uri)
{
QList<QUrl> trackers;
QRegExp rx("tr=([^&]+)");
int pos = 0;
while ((pos = rx.indexIn(magnet_uri, pos)) != -1) {
const QUrl tracker = QUrl::fromEncoded(rx.cap(1).toUtf8());
qDebug() << Q_FUNC_INFO << "Found tracker: " << tracker.toString();
trackers << tracker;
pos += rx.matchedLength();
}
return trackers;
}
QString misc::magnetUriToHash(const QString& magnet_uri) {
QString hash = "";
QRegExp regHex("urn:btih:([0-9A-Za-z]+)");
// Hex

6
src/misc.h

@ -42,6 +42,7 @@ @@ -42,6 +42,7 @@
#include <QPoint>
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QCoreApplication>
#ifndef DISABLE_GUI
#include <QIcon>
@ -99,8 +100,9 @@ public: @@ -99,8 +100,9 @@ public:
// value must be given in bytes
static QString friendlyUnit(qreal val, bool is_speed = false);
static bool isPreviewable(QString extension);
static QString magnetUriToName(QString magnet_uri);
static QString magnetUriToHash(QString magnet_uri);
static QString magnetUriToName(const QString& magnet_uri);
static QString magnetUriToHash(const QString& magnet_uri);
static QList<QUrl> magnetUriToTrackers(const QString& magnet_uri);
static QString bcLinkToMagnet(QString bc_link);
// Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m".

28
src/qtlibtorrent/qbtsession.cpp

@ -921,6 +921,11 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool f @@ -921,6 +921,11 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool f
if (s->find_torrent(QStringToSha1(hash)).is_valid()) {
qDebug("/!\\ Torrent is already in download list");
addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(magnet_uri));
// Check if the torrent contains trackers or url seeds we don't know about
// and add them
QTorrentHandle h_ex = getTorrentHandle(hash);
mergeTorrents(h_ex, magnet_uri);
return h;
}
@ -1301,6 +1306,29 @@ void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool m @@ -1301,6 +1306,29 @@ void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool m
TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet);
}
void QBtSession::mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri)
{
QList<QUrl> new_trackers = misc::magnetUriToTrackers(magnet_uri);
bool trackers_added = false;
foreach (const QUrl& new_tracker, new_trackers) {
bool found = false;
std::vector<announce_entry> existing_trackers = h_ex.trackers();
foreach (const announce_entry& existing_tracker, existing_trackers) {
if (new_tracker == QUrl(existing_tracker.url.c_str())) {
found = true;
break;
}
}
if (!found) {
h_ex.add_tracker(announce_entry(new_tracker.toString().toStdString()));
trackers_added = true;
}
}
if (trackers_added)
addConsoleMessage(tr("Note: new trackers were added to the existing torrent."));
}
void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torrent_info> t) {
// Check if the torrent contains trackers or url seeds we don't know about
// and add them

3
src/qtlibtorrent/qbtsession.h

@ -191,7 +191,8 @@ private slots: @@ -191,7 +191,8 @@ private slots:
void sendNotificationEmail(const QTorrentHandle &h);
void autoRunExternalProgram(const QTorrentHandle &h, bool async=true);
void cleanUpAutoRunProcess(int);
void mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t);
void mergeTorrents(QTorrentHandle& h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t);
void mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri);
void exportTorrentFile(const QTorrentHandle &h, TorrentExportFolder folder = RegularTorrentExportFolder);
void initWebUi();
void handleIPFilterParsed(int ruleCount);

Loading…
Cancel
Save