Browse Source

- Use torrent addition dialog for Magnet URIs too

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
ba0c7334b7
  1. 13
      src/GUI.cpp
  2. 22
      src/bittorrent.cpp
  3. 13
      src/misc.h
  4. 50
      src/torrentadditiondlg.h
  5. 13
      src/ui/torrentadditiondlg.ui

13
src/GUI.cpp

@ -651,8 +651,12 @@ void GUI::processParams(const QStringList& params) {
BTSession->downloadFromUrl(param); BTSession->downloadFromUrl(param);
}else{ }else{
if(param.startsWith("magnet:", Qt::CaseInsensitive)) { if(param.startsWith("magnet:", Qt::CaseInsensitive)) {
// FIXME: Possibily skipped torrent addition dialog if(useTorrentAdditionDialog) {
torrentAdditionDialog *dialog = new torrentAdditionDialog(this, BTSession);
dialog->showLoadMagnetURI(param);
} else {
BTSession->addMagnetUri(param); BTSession->addMagnetUri(param);
}
} else { } else {
if(useTorrentAdditionDialog) { if(useTorrentAdditionDialog) {
torrentAdditionDialog *dialog = new torrentAdditionDialog(this, BTSession); torrentAdditionDialog *dialog = new torrentAdditionDialog(this, BTSession);
@ -833,9 +837,16 @@ void GUI::showNotificationBaloon(QString title, QString msg) const {
*****************************************************/ *****************************************************/
void GUI::downloadFromURLList(const QStringList& url_list) { void GUI::downloadFromURLList(const QStringList& url_list) {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
foreach(const QString url, url_list) { foreach(const QString url, url_list) {
if(url.startsWith("magnet:", Qt::CaseInsensitive)) { if(url.startsWith("magnet:", Qt::CaseInsensitive)) {
if(useTorrentAdditionDialog) {
torrentAdditionDialog *dialog = new torrentAdditionDialog(this, BTSession);
dialog->showLoadMagnetURI(url);
} else {
BTSession->addMagnetUri(url); BTSession->addMagnetUri(url);
}
} else { } else {
BTSession->downloadFromUrl(url); BTSession->downloadFromUrl(url);
} }

22
src/bittorrent.cpp

@ -769,6 +769,14 @@ QTorrentHandle Bittorrent::addMagnetUri(QString magnet_uri, bool resumed) {
} else { } else {
p.save_path = defaultTempPath.toLocal8Bit().data(); p.save_path = defaultTempPath.toLocal8Bit().data();
} }
#ifdef LIBTORRENT_0_15
// Skip checking and directly start seeding (new in libtorrent v0.15)
if(TorrentTempData::isSeedingMode(hash)){
p.seed_mode=true;
} else {
p.seed_mode=false;
}
#endif
// Preallocate all? // Preallocate all?
if(preAllocateAll) if(preAllocateAll)
p.storage_mode = storage_mode_allocate; p.storage_mode = storage_mode_allocate;
@ -803,17 +811,19 @@ QTorrentHandle Bittorrent::addMagnetUri(QString magnet_uri, bool resumed) {
if(!resumed) { if(!resumed) {
// Sequential download // Sequential download
if(TorrentTempData::hasTempData(hash)) { if(TorrentTempData::hasTempData(hash)) {
qDebug("addMagnetUri: Setting download as sequential (from tmp data)"); qDebug("addMagnetURI Setting download as sequential (from tmp data)");
h.set_sequential_download(TorrentTempData::isSequential(hash)); h.set_sequential_download(TorrentTempData::isSequential(hash));
} }
QString label = TorrentTempData::getLabel(hash);
// Save persistent data for new torrent // Save persistent data for new torrent
Q_ASSERT(h.is_valid()); TorrentPersistentData::saveTorrentPersistentData(h);
qDebug("addMagnetUri: hash: %s", h.hash().toLocal8Bit().data()); // Save Label
TorrentPersistentData::saveTorrentPersistentData(h, true); if(!label.isEmpty()) {
qDebug("Persistent data saved"); TorrentPersistentData::saveLabel(hash, label);
}
// Save save_path // Save save_path
if(!defaultTempPath.isEmpty()) { if(!defaultTempPath.isEmpty()) {
qDebug("addMagnetUri: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data()); qDebug("addTorrent: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data());
TorrentPersistentData::saveSavePath(hash, savePath); TorrentPersistentData::saveSavePath(hash, savePath);
} }
} }

13
src/misc.h

@ -41,6 +41,7 @@
#include <QList> #include <QList>
#include <QPair> #include <QPair>
#include <QThread> #include <QThread>
#include <QUrl>
#include <ctime> #include <ctime>
#include <QDateTime> #include <QDateTime>
#include <boost/date_time/posix_time/posix_time_types.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp>
@ -435,6 +436,18 @@ public:
return false; return false;
} }
static QString magnetUriToName(QString magnet_uri) {
QString name = "";
QRegExp regHex("dn=([^&]+)");
int pos = regHex.indexIn(magnet_uri);
if(pos > -1) {
QString found = regHex.cap(1);
// URL decode
name = QUrl::fromPercentEncoding(found.toLocal8Bit()).replace("+", " ");
}
return name;
}
static QString magnetUriToHash(QString magnet_uri) { static QString magnetUriToHash(QString magnet_uri) {
QString hash = ""; QString hash = "";
QRegExp regHex("urn:btih:([0-9A-Za-z]+)"); QRegExp regHex("urn:btih:([0-9A-Za-z]+)");

50
src/torrentadditiondlg.h

@ -69,6 +69,7 @@ private:
unsigned int nbFiles; unsigned int nbFiles;
boost::intrusive_ptr<torrent_info> t; boost::intrusive_ptr<torrent_info> t;
QStringList files_path; QStringList files_path;
bool is_magnet;
public: public:
torrentAdditionDialog(QWidget *parent, Bittorrent* _BTSession) : QDialog(parent) { torrentAdditionDialog(QWidget *parent, Bittorrent* _BTSession) : QDialog(parent) {
@ -148,7 +149,39 @@ public:
settings.setValue("TorrentAdditionDlg/pos", pos()); settings.setValue("TorrentAdditionDlg/pos", pos());
} }
void showLoadMagnetURI(QString magnet_uri) {
is_magnet = true;
this->from_url = magnet_uri;
// Disable useless widgets
torrentContentList->setVisible(false);
torrentContentLbl->setVisible(false);
collapseAllButton->setVisible(false);
expandAllButton->setVisible(false);
// Get torrent hash
hash = misc::magnetUriToHash(magnet_uri);
if(hash.isEmpty()) {
BTSession->addConsoleMessage(tr("Unable to decode magnet link:")+QString::fromUtf8(" '")+from_url+QString::fromUtf8("'"), QString::fromUtf8("red"));
return;
}
fileName = misc::magnetUriToName(magnet_uri);
if(fileName.isEmpty()) fileName = tr("Magnet Link");
fileNameLbl->setText(QString::fromUtf8("<center><b>")+fileName+QString::fromUtf8("</b></center>"));
// Update display
updateDiskSpaceLabels();
// Load custom labels
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters"));
QStringList customLabels = settings.value("customLabels", QStringList()).toStringList();
comboLabel->addItem("");
foreach(const QString& label, customLabels) {
comboLabel->addItem(label);
}
// Show dialog
show();
}
void showLoad(QString filePath, QString from_url=QString::null){ void showLoad(QString filePath, QString from_url=QString::null){
is_magnet = false;
if(!QFile::exists(filePath)) { if(!QFile::exists(filePath)) {
close(); close();
return; return;
@ -314,7 +347,7 @@ public slots:
void updateDiskSpaceLabels() { void updateDiskSpaceLabels() {
long long available = misc::freeDiskSpaceOnPath(misc::expandPath(savePathTxt->text())); long long available = misc::freeDiskSpaceOnPath(misc::expandPath(savePathTxt->text()));
lbl_disk_space->setText(misc::friendlyUnit(available)); lbl_disk_space->setText(misc::friendlyUnit(available));
if(!is_magnet) {
// Determine torrent size // Determine torrent size
qulonglong torrent_size = 0; qulonglong torrent_size = 0;
unsigned int nbFiles = t->num_files(); unsigned int nbFiles = t->num_files();
@ -325,6 +358,7 @@ public slots:
torrent_size += t->file_at(i).size; torrent_size += t->file_at(i).size;
} }
lbl_torrent_size->setText(misc::friendlyUnit(torrent_size)); lbl_torrent_size->setText(misc::friendlyUnit(torrent_size));
// Check if free space is sufficient // Check if free space is sufficient
if(available > 0) { if(available > 0) {
if((unsigned long long)available > torrent_size) { if((unsigned long long)available > torrent_size) {
@ -339,6 +373,7 @@ public slots:
label_space_msg->setText(""); label_space_msg->setText("");
} }
} }
}
void on_browseButton_clicked(){ void on_browseButton_clicked(){
QString dir; QString dir;
@ -389,6 +424,7 @@ public slots:
TorrentTempData::setSequential(hash, checkIncrementalDL->isChecked()); TorrentTempData::setSequential(hash, checkIncrementalDL->isChecked());
// Save files path // Save files path
// Loads files path in the torrent // Loads files path in the torrent
if(!is_magnet) {
bool path_changed = false; bool path_changed = false;
for(uint i=0; i<nbFiles; ++i) { for(uint i=0; i<nbFiles; ++i) {
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
@ -403,11 +439,12 @@ public slots:
if(path_changed) { if(path_changed) {
TorrentTempData::setFilesPath(hash, files_path); TorrentTempData::setFilesPath(hash, files_path);
} }
}
#ifdef LIBTORRENT_0_15 #ifdef LIBTORRENT_0_15
// Skip file checking and directly start seeding // Skip file checking and directly start seeding
if(addInSeed->isChecked()) { if(addInSeed->isChecked()) {
// Check if local file(s) actually exist // Check if local file(s) actually exist
if(savePath.exists(misc::toQString(t->name()))) { if(is_magnet || savePath.exists(misc::toQString(t->name()))) {
TorrentTempData::setSeedingMode(hash, true); TorrentTempData::setSeedingMode(hash, true);
} else { } else {
QMessageBox::warning(0, tr("Seeding mode error"), tr("You chose to skip file checking. However, local files do not seem to exist in the current destionation folder. Please disable this feature or update the save path.")); QMessageBox::warning(0, tr("Seeding mode error"), tr("You chose to skip file checking. However, local files do not seem to exist in the current destionation folder. Please disable this feature or update the save path."));
@ -416,14 +453,19 @@ public slots:
} }
#endif #endif
// Check if there is at least one selected file // Check if there is at least one selected file
if(allFiltered()){ if(!is_magnet && allFiltered()){
QMessageBox::warning(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent")); QMessageBox::warning(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent"));
return; return;
} }
// save filtered files // save filtered files
if(!is_magnet)
savePiecesPriorities(); savePiecesPriorities();
// Add to download list // Add to download list
QTorrentHandle h = BTSession->addTorrent(filePath, false, from_url); QTorrentHandle h;
if(is_magnet)
h = BTSession->addMagnetUri(from_url, false);
else
h = BTSession->addTorrent(filePath, false, from_url);
if(addInPause->isChecked() && h.is_valid()) if(addInPause->isChecked() && h.is_valid())
h.pause(); h.pause();
close(); close();

13
src/ui/torrentadditiondlg.ui

@ -279,6 +279,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<layout class="QHBoxLayout"> <layout class="QHBoxLayout">
<property name="spacing"> <property name="spacing">

Loading…
Cancel
Save