diff --git a/src/filesystemwatcher.h b/src/filesystemwatcher.h index 2522946ab..dff31c42f 100644 --- a/src/filesystemwatcher.h +++ b/src/filesystemwatcher.h @@ -22,6 +22,7 @@ #endif #include "fs_utils.h" +#include "misc.h" #ifndef CIFS_MAGIC_NUMBER #define CIFS_MAGIC_NUMBER 0xFF534D42 @@ -118,7 +119,7 @@ private: public: FileSystemWatcher(QObject *parent): QFileSystemWatcher(parent) { - m_filters << "*.torrent"; + m_filters << "*.torrent" << "*.magnet"; connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(scanLocalFolder(QString))); } @@ -271,7 +272,13 @@ private: const QStringList files = dir.entryList(m_filters, QDir::Files, QDir::Unsorted); foreach (const QString &file, files) { const QString file_abspath = dir.absoluteFilePath(file); - if (fsutils::isValidTorrentFile(file_abspath)) { + if (file_abspath.endsWith(".magnet")) { + QFile f(file_abspath); + if (f.open(QIODevice::ReadOnly) + && !misc::magnetUriToHash(QString::fromLocal8Bit(f.readAll())).isEmpty()) { + torrents << file_abspath; + } + } else if (fsutils::isValidTorrentFile(file_abspath)) { torrents << file_abspath; } else { if (!m_partialTorrents.contains(file_abspath)) { diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index e71759c22..c4cf38df7 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -924,7 +924,7 @@ void QBtSession::loadTorrentSettings(QTorrentHandle& h) { #endif } -QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed) { +QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool fromScanDir, const QString &filePath) { Preferences pref; QTorrentHandle h; const QString hash(misc::magnetUriToHash(magnet_uri)); @@ -952,7 +952,7 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed) { add_torrent_params p = initializeAddTorrentParams(hash); // Get save path - const QString savePath(getSavePath(hash, false)); + const QString savePath(getSavePath(hash, fromScanDir, filePath)); if (!defaultTempPath.isEmpty() && !TorrentPersistentData::isSeed(hash) && resumed) { qDebug("addMagnetURI: Temp folder is enabled."); QString torrent_tmp_path = defaultTempPath.replace("\\", "/"); @@ -1742,6 +1742,17 @@ bool QBtSession::isFilePreviewPossible(const QString &hash) const { void QBtSession::addTorrentsFromScanFolder(QStringList &pathList) { foreach (const QString &file, pathList) { qDebug("File %s added", qPrintable(file)); + if (file.endsWith(".magnet")) { + QFile f(file); + if (!f.open(QIODevice::ReadOnly)) { + qDebug("Failed to open magnet file: %s", qPrintable(f.errorString())); + } else { + const QString link = QString::fromLocal8Bit(f.readAll()); + addMagnetUri(link, false, true, file); + f.remove(); + } + continue; + } try { torrent_info t(file.toUtf8().constData()); if (t.is_valid()) diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index 0a4b3765a..07ae8b0be 100644 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -105,7 +105,7 @@ public: public slots: QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); - QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); + QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false, bool fromScanDir=false, const QString &filePath=QString()); void loadSessionState(); void saveSessionState(); void downloadFromUrl(const QString &url, const QList& cookies = QList());