FEATURE: Torrent files can be exported to a given directory

BUGFIX: Fix crash when double-clicking on a torrent that has no metadata to open its save path
This commit is contained in:
Christophe Dumez 2010-01-31 13:09:46 +00:00
parent 9bd40a9b79
commit a03ad3de23
9 changed files with 225 additions and 26 deletions

View file

@ -3,6 +3,7 @@
- FEATURE: Bandwidth scheduler (automatically use alternative speed limits for a given period)
- FEATURE: Added "Added/Completed On" columns to transfer list
- FEATURE: Added "Upload/Download limit" columns to transfer list
- FEATURE: Torrent files can be exported to a given directory
* Mon Jan 18 2010 - Christophe Dumez <chris@qbittorrent.org> - v2.1.0
- FEATURE: Graphical User Interface can be disabled at compilation time (headless running)

View file

@ -68,7 +68,7 @@ enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4, SOCKS4=5};
enum VersionType { NORMAL,ALPHA,BETA,RELEASE_CANDIDATE,DEVEL };
// Main constructor
Bittorrent::Bittorrent() : preAllocateAll(false), addInPause(false), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), DHTEnabled(false), current_dht_port(0), queueingEnabled(false), exiting(false) {
Bittorrent::Bittorrent() : preAllocateAll(false), addInPause(false), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), DHTEnabled(false), current_dht_port(0), queueingEnabled(false), torrentExport(false), exiting(false) {
#ifndef DISABLE_GUI
geoipDBLoaded = false;
resolve_countries = false;
@ -280,6 +280,15 @@ void Bittorrent::configureSession() {
//Interval first
enableDirectoryScanning(scan_dir);
}
// * Export Dir
bool newTorrentExport = Preferences::isTorrentExportEnabled();
if(torrentExport != newTorrentExport) {
torrentExport = newTorrentExport;
if(torrentExport) {
qDebug("Torrent export is enabled, exporting the current torrents");
exportTorrentFiles(Preferences::getExportDir());
}
}
// Connection
// * Ports binding
unsigned short old_listenPort = getListenPort();
@ -1086,8 +1095,8 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr
if(appendqBExtension)
appendqBextensionToTorrent(h, true);
#endif
}
QString newFile = torrentBackup.path() + QDir::separator() + hash + ".torrent";
// Backup torrent file
QString newFile = torrentBackup.absoluteFilePath(hash + ".torrent");
if(file != newFile) {
// Delete file from torrentBackup directory in case it exists because
// QFile::copy() do not overwrite
@ -1095,6 +1104,20 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr
// Copy it to torrentBackup directory
QFile::copy(file, newFile);
}
// Copy the torrent file to the export folder
if(torrentExport) {
QDir exportPath(Preferences::getExportDir());
if(exportPath.exists() || exportPath.mkpath(exportPath.absolutePath())) {
QString torrent_path = exportPath.absoluteFilePath(h.name()+".torrent");
if(QFile::exists(torrent_path) && misc::sameFiles(file, torrent_path)) {
// Append hash to torrent name to make it unique
torrent_path = exportPath.absoluteFilePath(h.name()+"-"+h.hash()+".torrent");
}
QFile::copy(file, torrent_path);
//h.save_torrent_file(torrent_path);
}
}
}
if(!fastResume && (!addInPause || (Preferences::useAdditionDialog() && !fromScanDir))) {
// Start torrent because it was added in paused state
h.resume();
@ -1121,6 +1144,43 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr
return h;
}
void Bittorrent::exportTorrentFiles(QString path) {
Q_ASSERT(torrentExport);
QDir exportDir(path);
if(!exportDir.exists()) {
if(!exportDir.mkpath(exportDir.absolutePath())) {
std::cerr << "Error: Could not create torrent export directory: " << exportDir.absolutePath().toLocal8Bit().data() << std::endl;
return;
}
}
QDir torrentBackup(misc::BTBackupLocation());
std::vector<torrent_handle> handles = s->get_torrents();
std::vector<torrent_handle>::iterator itr;
for(itr=handles.begin(); itr != handles.end(); itr++) {
QTorrentHandle h(*itr);
if(!h.is_valid()) {
std::cerr << "Torrent Export: torrent is invalid, skipping..." << std::endl;
continue;
}
QString src_path = torrentBackup.absoluteFilePath(h.hash()+".torrent");
if(QFile::exists(src_path)) {
QString dst_path = exportDir.absoluteFilePath(h.name()+".torrent");
if(QFile::exists(dst_path)) {
if(!misc::sameFiles(src_path, dst_path)) {
dst_path = exportDir.absoluteFilePath(h.name()+"-"+h.hash()+".torrent");
} else {
qDebug("Torrent Export: Destination file exists, skipping...");
continue;
}
}
qDebug("Export Torrent: %s -> %s", src_path.toLocal8Bit().data(), dst_path.toLocal8Bit().data());
QFile::copy(src_path, dst_path);
} else {
std::cerr << "Error: could not export torrent "<< h.hash().toLocal8Bit().data() << ", maybe it has not metadata yet." <<std::endl;
}
}
}
// Set the maximum number of opened connections
void Bittorrent::setMaxConnections(int maxConnec) {
s->set_max_connections(maxConnec);
@ -1791,8 +1851,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
if(QFile::exists(file))
QFile::remove(file);
qDebug("Saving fastresume data in %s", file.toLocal8Bit().data());
if (p->resume_data)
{
if (p->resume_data) {
boost::filesystem::ofstream out(fs::path(torrentBackup.path().toLocal8Bit().data()) / file.toLocal8Bit().data(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), *p->resume_data);
@ -1818,6 +1877,26 @@ void Bittorrent::addConsoleMessage(QString msg, QString) {
QDir torrentBackup(misc::BTBackupLocation());
if(!QFile::exists(torrentBackup.path()+QDir::separator()+h.hash()+QString(".torrent")))
h.save_torrent_file(torrentBackup.path()+QDir::separator()+h.hash()+QString(".torrent"));
// Copy the torrent file to the export folder
if(torrentExport) {
QDir exportPath(Preferences::getExportDir());
if(exportPath.exists() || exportPath.mkpath(exportPath.absolutePath())) {
QString torrent_path = exportPath.absoluteFilePath(h.name()+".torrent");
bool duplicate = false;
if(QFile::exists(torrent_path)) {
// Append hash to torrent name to make it unique
torrent_path = exportPath.absoluteFilePath(h.name()+"-"+h.hash()+".torrent");
duplicate = true;
}
h.save_torrent_file(torrent_path);
if(duplicate) {
// Remove duplicate file if indentical
if(misc::sameFiles(exportPath.absoluteFilePath(h.name()+".torrent"), torrent_path)) {
QFile::remove(torrent_path);
}
}
}
}
if(h.is_paused()) {
// XXX: Unfortunately libtorrent-rasterbar does not send a torrent_paused_alert
// and the torrent can be paused when metadata is received

View file

@ -118,6 +118,7 @@ private:
bool PeXEnabled;
bool queueingEnabled;
bool appendLabelToSavePath;
bool torrentExport;
#ifdef LIBTORRENT_0_15
bool appendqBExtension;
#endif
@ -247,6 +248,7 @@ protected slots:
void readAlerts();
void deleteBigRatios();
void takeETASamples();
void exportTorrentFiles(QString path);
signals:
void addedTorrent(QTorrentHandle& h);

View file

@ -124,6 +124,27 @@ public:
return x;
}
static bool sameFiles(QString path1, QString path2) {
QFile f1(path1);
if(!f1.exists()) return false;
QFile f2(path2);
if(!f2.exists()) return false;
QByteArray content1, content2;
if(f1.open(QIODevice::ReadOnly)) {
content1 = f1.readAll();
f1.close();
} else {
return false;
}
if(f2.open(QIODevice::ReadOnly)) {
content1 = f2.readAll();
f2.close();
} else {
return false;
}
return content1 == content2;
}
static void copyDir(QString src_path, QString dst_path) {
QDir sourceDir(src_path);
if(!sourceDir.exists()) return;

View file

@ -153,9 +153,7 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
// Downloads tab
connect(checkTempFolder, SIGNAL(toggled(bool)), this, SLOT(enableTempPathInput(bool)));
connect(checkScanDir, SIGNAL(toggled(bool)), this, SLOT(enableDirScan(bool)));
connect(actionTorrentDlOnDblClBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enableApplyButton()));
connect(actionTorrentFnOnDblClBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enableApplyButton()));
connect(checkTempFolder, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(checkExportDir, SIGNAL(toggled(bool)), this, SLOT(enableTorrentExport(bool)));
// Connection tab
connect(checkUploadLimit, SIGNAL(toggled(bool)), this, SLOT(enableUploadLimit(bool)));
connect(checkDownloadLimit, SIGNAL(toggled(bool)), this, SLOT(enableDownloadLimit(bool)));
@ -206,6 +204,11 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
connect(checkStartPaused, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(checkScanDir, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(textScanDir, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton()));
connect(checkExportDir, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(textExportDir, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton()));
connect(actionTorrentDlOnDblClBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enableApplyButton()));
connect(actionTorrentFnOnDblClBox, SIGNAL(currentIndexChanged(int)), this, SLOT(enableApplyButton()));
connect(checkTempFolder, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
// Connection tab
connect(spinPort, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton()));
connect(checkUPnP, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
@ -394,6 +397,7 @@ void options_imp::saveOptions(){
settings.setValue(QString::fromUtf8("AdditionDialog"), useAdditionDialog());
settings.setValue(QString::fromUtf8("StartInPause"), addTorrentsInPause());
settings.setValue(QString::fromUtf8("ScanDir"), getScanDir());
Preferences::setExportDir(getExportDir());
settings.setValue(QString::fromUtf8("DblClOnTorDl"), getActionOnDblClOnTorrentDl());
settings.setValue(QString::fromUtf8("DblClOnTorFn"), getActionOnDblClOnTorrentFn());
// End Downloads preferences
@ -627,6 +631,19 @@ void options_imp::loadOptions(){
textScanDir->setText(strValue);
enableDirScan(checkScanDir->isChecked());
}
strValue = Preferences::getExportDir();
if(strValue.isEmpty()) {
// Disable
checkExportDir->setChecked(false);
enableTorrentExport(checkExportDir->isChecked());
} else {
// enable
checkExportDir->setChecked(true);
textExportDir->setText(strValue);
enableTorrentExport(checkExportDir->isChecked());
}
intValue = Preferences::getActionOnDblClOnTorrentDl();
if(intValue >= actionTorrentDlOnDblClBox->count())
intValue = 0;
@ -1267,6 +1284,11 @@ void options_imp::enableDirScan(bool checked){
browseScanDirButton->setEnabled(checked);
}
void options_imp::enableTorrentExport(bool checked) {
textExportDir->setEnabled(checked);
browseExportDirButton->setEnabled(checked);
}
bool options_imp::isSlashScreenDisabled() const {
return checkNoSplash->isChecked();
}
@ -1365,6 +1387,14 @@ QString options_imp::getScanDir() const {
}
}
QString options_imp::getExportDir() const {
if(checkExportDir->isChecked()){
return misc::expandPath(textExportDir->text());
}else{
return QString::null;
}
}
// Return action on double-click on a downloading torrent set in options
int options_imp::getActionOnDblClOnTorrentDl() const {
if(actionTorrentDlOnDblClBox->currentIndex()<1)

View file

@ -83,6 +83,7 @@ protected:
bool addTorrentsInPause() const;
bool isDirScanEnabled() const;
QString getScanDir() const;
QString getExportDir() const;
int getActionOnDblClOnTorrentDl() const;
int getActionOnDblClOnTorrentFn() const;
// Connection options
@ -135,6 +136,7 @@ protected slots:
void enableDownloadLimit(bool checked);
void enableTempPathInput(bool checked);
void enableDirScan(bool checked);
void enableTorrentExport(bool checked);
void enablePeerProxy(int comboIndex);
void enablePeerProxyAuth(bool checked);
void enableHTTPProxy(int comboIndex);

View file

@ -214,6 +214,24 @@ public:
settings.setValue(QString::fromUtf8("Preferences/Downloads/ScanDir"), path);
}
static bool isTorrentExportEnabled() {
QSettings settings("qBittorrent", "qBittorrent");
return !settings.value(QString::fromUtf8("Preferences/Downloads/TorrentExport"), QString()).toString().isEmpty();
}
static QString getExportDir() {
QSettings settings("qBittorrent", "qBittorrent");
return settings.value(QString::fromUtf8("Preferences/Downloads/TorrentExport"), QString()).toString();
}
static void setExportDir(QString path) {
path = path.trimmed();
if(path.isEmpty())
path = QString();
QSettings settings("qBittorrent", "qBittorrent");
settings.setValue(QString::fromUtf8("Preferences/Downloads/TorrentExport"), path);
}
static int getActionOnDblClOnTorrentDl() {
QSettings settings("qBittorrent", "qBittorrent");
return settings.value(QString::fromUtf8("Preferences/Downloads/DblClOnTorDl"), 0).toInt();

View file

@ -581,7 +581,10 @@ void TransferListWidget::torrentDoubleClicked(QModelIndex index) {
}
break;
case OPEN_DEST:
if(h.has_metadata())
QDesktopServices::openUrl("file://" + h.root_path());
else
QDesktopServices::openUrl("file://" + h.save_path());
break;
}
}

View file

@ -629,7 +629,7 @@ QGroupBox {
<x>0</x>
<y>0</y>
<width>644</width>
<height>504</height>
<height>583</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_17">
@ -645,9 +645,6 @@ QGroupBox {
<string>File system</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<property name="spacing">
<number>3</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="styleSheet">
@ -811,6 +808,52 @@ QGroupBox {
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="checkExportDir">
<property name="text">
<string>Copy .torrent files to:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_15">
<item>
<widget class="QLineEdit" name="textExportDir">
<property name="enabled">
<bool>false</bool>
</property>
<property name="styleSheet">
<string>QLineEdit {
margin-left: 23px;
}</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="browseExportDirButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>22</width>
<height>22</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>25</width>
<height>27</height>
</size>
</property>
<property name="icon">
<iconset resource="../icons.qrc">
<normaloff>:/Icons/oxygen/browse.png</normaloff>:/Icons/oxygen/browse.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="checkAppendqB">
<property name="text">
@ -1393,8 +1436,8 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>620</width>
<height>490</height>
<width>366</width>
<height>332</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_33">
@ -1798,8 +1841,8 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>620</width>
<height>490</height>
<width>466</width>
<height>415</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_20">
@ -2746,8 +2789,8 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>219</width>
<height>221</height>
<width>620</width>
<height>490</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_23">