Skip to content

Commit

Permalink
- FEATURE: Added an option to automatically delete torrents when they…
Browse files Browse the repository at this point in the history
… reach a given ratio (>= 1.0)
  • Loading branch information
Christophe Dumez committed Sep 9, 2007
1 parent 078c80c commit 862d24a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions Changelog
Expand Up @@ -37,6 +37,7 @@
- FEATURE: Added an option to add torrents in paused state
- FEATURE: Added an option to set the max number of connections per torrent
- FEATURE: Added an option to set the max number of uploads per torrent
- FEATURE: Added an option to automatically delete torrents when they reach a given ratio (>= 1.0)
- I18N: Added Hungarian translation
- I18N: Added Brazilian translation
- BUGFIX: Progress of paused torrents is now correct on restart
Expand Down
2 changes: 2 additions & 0 deletions TODO
Expand Up @@ -63,6 +63,7 @@
* beta 7
- Add "Mark all as read" feature for RSS
- update doc for plugins (and add screenies)
- update doc for options
- Redesign options (fully)
- Review torrent content selection
* check the one in ktorrent
Expand Down Expand Up @@ -102,6 +103,7 @@ beta6->beta7 changelog:
- FEATURE: Allow to disable UPnP/NAT-PMP/LSD
- FEATURE: Added an option to set the max number of connections per torrent
- FEATURE: Added an option to set the max number of uploads per torrent
- FEATURE: Added an option to automatically delete torrents when they reach a given ratio (>= 1.0)
- BUGFIX: In torrent content, it is now easier to filter all torrents using right click menu
- BUGFIX: Updated man page / README / INSTALL
- BUGFIX: Paused torrents could be displayed as connected for a sec after checking
Expand Down
35 changes: 24 additions & 11 deletions src/GUI.cpp
Expand Up @@ -112,6 +112,7 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for
connect(BTSession, SIGNAL(scanDirFoundTorrents(const QStringList&)), this, SLOT(processScannedFiles(const QStringList&)));
connect(BTSession, SIGNAL(newDownloadedTorrent(QString, QString)), this, SLOT(processDownloadedFiles(QString, QString)));
connect(BTSession, SIGNAL(downloadFromUrlFailure(QString, QString)), this, SLOT(handleDownloadFromUrlFailure(QString, QString)));
connect(BTSession, SIGNAL(torrent_deleted(QString, QString, bool)), this, SLOT(deleteTorrent(QString, QString, bool)));
qDebug("create tabWidget");
tabs = new QTabWidget();
// Download torrents tab
Expand Down Expand Up @@ -661,6 +662,16 @@ void GUI::on_actionDelete_Permanently_triggered() {
}
}

void GUI::deleteTorrent(QString hash, QString fileName, bool finished) {
if(finished) {
finishedTorrentTab->deleteTorrent(hash);
} else {
downloadingTorrentTab->deleteTorrent(hash);
}
// Update info bar
downloadingTorrentTab->setInfoBar(tr("'%1' was removed because its ratio reached the maximum value you set.", "%1 is a file name").arg(fileName));
}

// delete selected items in the list
void GUI::on_actionDelete_triggered() {
QStringList hashes;
Expand Down Expand Up @@ -778,8 +789,16 @@ void GUI::processDownloadedFiles(QString path, QString url) {
void GUI::configureSession(bool deleteOptions) {
qDebug("Configuring session");
// Downloads
// Save path
BTSession->setDefaultSavePath(options->getSavePath());
BTSession->preAllocateAllFiles(options->preAllocateAllFiles());
BTSession->startTorrentsInPause(options->addTorrentsInPause());
// * Scan dir
if(options->getScanDir().isNull()) {
BTSession->disableDirectoryScanning();
}else{
BTSession->enableDirectoryScanning(options->getScanDir());
}
// Connection
// * Ports binding
unsigned short old_listenPort = BTSession->getListenPort();
Expand Down Expand Up @@ -906,25 +925,19 @@ void GUI::configureSession(bool deleteOptions) {
downloadingTorrentTab->setInfoBar(tr("Encryption support [OFF]"), QString::fromUtf8("blue"));
}
BTSession->applyEncryptionSettings(encryptionSettings);
// IP Filter
// Configure session regarding options
BTSession->setDefaultSavePath(options->getSavePath());
// Apply ratio (0 if disabled)
// * Desired ratio
BTSession->setGlobalRatio(options->getDesiredRatio());
// Apply filtering settings
// * Maximum ratio
BTSession->setDeleteRatio(options->getDeleteRatio());
// Ip Filter
if(options->isFilteringEnabled()) {
BTSession->enableIPFilter(options->getFilter());
downloadingTorrentTab->setBottomTabEnabled(1, true);
}else{
BTSession->disableIPFilter();
downloadingTorrentTab->setBottomTabEnabled(1, false);
}
// Scan dir stuff
if(options->getScanDir().isNull()) {
BTSession->disableDirectoryScanning();
}else{
BTSession->enableDirectoryScanning(options->getScanDir());
}
// Clean up
if(deleteOptions) {
delete options;
}
Expand Down
1 change: 1 addition & 0 deletions src/GUI.h
Expand Up @@ -132,6 +132,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{
void processScannedFiles(const QStringList& params);
void processDownloadedFiles(QString path, QString url);
void downloadFromURLList(const QStringList& urls);
void deleteTorrent(QString hash, QString fileName, bool finished);
void finishedTorrent(QTorrentHandle& h) const;
void torrentChecked(QString hash) const;
void updateLists();
Expand Down
34 changes: 33 additions & 1 deletion src/bittorrent.cpp
Expand Up @@ -44,7 +44,7 @@
#define MAX_TRACKER_ERRORS 2

// Main constructor
bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false), addInPause(false), maxConnecsPerTorrent(500), maxUploadsPerTorrent(4) {
bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false), addInPause(false), maxConnecsPerTorrent(500), maxUploadsPerTorrent(4), max_ratio(-1) {
// To avoid some exceptions
fs::path::default_name_check(fs::no_check);
// Creating bittorrent session
Expand Down Expand Up @@ -100,6 +100,26 @@ void bittorrent::preAllocateAllFiles(bool b) {
}
}

void bittorrent::deleteBigRatios() {
if(max_ratio == -1) return;
std::vector<torrent_handle> handles = s->get_torrents();
unsigned int nbHandles = handles.size();
for(unsigned int i=0; i<nbHandles; ++i) {
QTorrentHandle h = handles[i];
if(!h.is_valid()) {
qDebug("/!\\ Error: Invalid handle");
continue;
}
QString hash = h.hash();
if(getRealRatio(hash) > max_ratio) {
bool finished = finishedTorrents.contains(hash);
QString fileName = h.name();
deleteTorrent(hash);
emit torrent_deleted(hash, fileName, finished);
}
}
}

void bittorrent::setDownloadLimit(QString hash, long val) {
QTorrentHandle h = getTorrentHandle(hash);
if(h.is_valid())
Expand Down Expand Up @@ -149,6 +169,9 @@ void bittorrent::updateETAs() {
}
}
}
// Delete big ratios
if(max_ratio != -1)
deleteBigRatios();
}

long bittorrent::getETA(QString hash) const{
Expand Down Expand Up @@ -929,6 +952,7 @@ void bittorrent::setUploadRateLimit(long rate) {
// libtorrent allow to adjust ratio for each torrent
// This function will apply to same ratio to all torrents
void bittorrent::setGlobalRatio(float ratio) {
if(ratio != -1 && ratio < 1.) ratio = 1.;
std::vector<torrent_handle> handles = s->get_torrents();
unsigned int nbHandles = handles.size();
for(unsigned int i=0; i<nbHandles; ++i) {
Expand All @@ -941,6 +965,14 @@ void bittorrent::setGlobalRatio(float ratio) {
}
}

// Torrents will a ratio superior to the given value will
// be automatically deleted
void bittorrent::setDeleteRatio(float ratio) {
if(ratio != -1 && ratio < 1.) ratio = 1.;
max_ratio = ratio;
deleteBigRatios();
}

bool bittorrent::loadTrackerFile(QString hash) {
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
QFile tracker_file(torrentBackup.path()+QDir::separator()+ hash + ".trackers");
Expand Down
5 changes: 4 additions & 1 deletion src/bittorrent.h
Expand Up @@ -61,6 +61,7 @@ class bittorrent : public QObject{
bool addInPause;
int maxConnecsPerTorrent;
int maxUploadsPerTorrent;
float max_ratio;

protected:
QString getSavePath(QString hash);
Expand Down Expand Up @@ -119,6 +120,7 @@ class bittorrent : public QObject{
void setDownloadRateLimit(long rate);
void setUploadRateLimit(long rate);
void setGlobalRatio(float ratio);
void setDeleteRatio(float ratio);
void setDHTPort(int dht_port);
void setProxySettings(proxy_settings proxySettings, bool trackers=true, bool peers=true, bool web_seeds=true, bool dht=true);
void setSessionSettings(session_settings sessionSettings);
Expand All @@ -142,6 +144,7 @@ class bittorrent : public QObject{
bool loadTrackerFile(QString hash);
void saveTrackerFile(QString hash);
void reloadTorrent(const QTorrentHandle &h); // This is protected now, call pauseAndReloadTorrent() instead
void deleteBigRatios();

signals:
void invalidTorrent(QString path);
Expand All @@ -161,7 +164,7 @@ class bittorrent : public QObject{
void fastResumeDataRejected(QString name);
void urlSeedProblem(QString url, QString msg);
void torrentFinishedChecking(QString hash);

void torrent_deleted(QString hash, QString fileName, bool finished);
};

#endif

0 comments on commit 862d24a

Please sign in to comment.