2007-04-10 13:07:33 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2007-04-10 13:07:33 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-04-10 13:07:33 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2007-04-10 13:07:33 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BANDWIDTH_ALLOCATION_H
|
|
|
|
#define BANDWIDTH_ALLOCATION_H
|
|
|
|
|
|
|
|
#include <QDialog>
|
2007-04-10 13:43:26 +04:00
|
|
|
#include <QList>
|
2007-07-14 14:50:38 +04:00
|
|
|
#include <QSettings>
|
2007-04-10 13:07:33 +04:00
|
|
|
#include "ui_bandwidth_limit.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "bittorrent.h"
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg {
|
2007-04-10 14:05:04 +04:00
|
|
|
Q_OBJECT
|
|
|
|
|
2007-04-10 13:07:33 +04:00
|
|
|
public:
|
2007-07-14 17:38:29 +04:00
|
|
|
BandwidthAllocationDialog(QWidget *parent, bool uploadMode, bittorrent *BTSession, QStringList hashes): QDialog(parent), uploadMode(uploadMode), hashes(hashes){
|
2007-04-10 13:07:33 +04:00
|
|
|
setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2007-07-14 14:50:38 +04:00
|
|
|
qDebug("Bandwidth allocation dialog creation");
|
2007-04-10 13:07:33 +04:00
|
|
|
this->BTSession = BTSession;
|
2007-07-14 14:50:38 +04:00
|
|
|
if(hashes.size() == 0)
|
|
|
|
global = true;
|
|
|
|
else
|
|
|
|
global = false;
|
2007-04-10 13:07:33 +04:00
|
|
|
if(uploadMode)
|
|
|
|
lblTitle->setText(tr("Upload limit:"));
|
|
|
|
else
|
|
|
|
lblTitle->setText(tr("Download limit:"));
|
|
|
|
connect(bandwidthSlider, SIGNAL(valueChanged(int)), this, SLOT(updateBandwidthLabel(int)));
|
2007-07-14 14:50:38 +04:00
|
|
|
if(!global){
|
2007-07-14 17:38:29 +04:00
|
|
|
unsigned int nbTorrents = hashes.size();
|
2007-07-14 14:50:38 +04:00
|
|
|
if(!nbTorrents) close();
|
2007-08-04 09:29:35 +04:00
|
|
|
int val = 0;
|
|
|
|
int max = -1;
|
2007-07-14 14:50:38 +04:00
|
|
|
if(nbTorrents == 1){
|
2007-07-14 17:38:29 +04:00
|
|
|
torrent_handle h = BTSession->getTorrentHandle(hashes.at(0));
|
2007-07-17 15:45:36 +04:00
|
|
|
if(uploadMode){
|
2007-08-04 09:29:35 +04:00
|
|
|
if(h.upload_limit() > 0)
|
|
|
|
val = (int)(h.upload_limit() / 1024.);
|
|
|
|
if(BTSession->getSession()->upload_rate_limit() > 0)
|
|
|
|
max = (int)(BTSession->getSession()->upload_rate_limit() / 1024.);
|
2007-07-17 15:45:36 +04:00
|
|
|
}else{
|
2007-08-04 09:29:35 +04:00
|
|
|
if(h.download_limit() > 0)
|
|
|
|
val = (int)(h.download_limit() / 1024.);
|
|
|
|
if(BTSession->getSession()->download_rate_limit() > 0){
|
|
|
|
qDebug("there is a global download rate limit at: %d kb/s", (int)(BTSession->getSession()->download_rate_limit() / 1024.));
|
|
|
|
max = (int)(BTSession->getSession()->download_rate_limit() / 1024.);
|
|
|
|
}
|
2007-07-17 15:45:36 +04:00
|
|
|
}
|
|
|
|
if(max != -1)
|
|
|
|
bandwidthSlider->setMaximum(max);
|
2007-07-14 14:50:38 +04:00
|
|
|
qDebug("Bandwidth limit: %d", val);
|
2007-07-17 15:45:36 +04:00
|
|
|
if(val > bandwidthSlider->maximum())
|
|
|
|
val = bandwidthSlider->maximum();
|
|
|
|
else if(val < bandwidthSlider->minimum())
|
2007-08-04 09:29:35 +04:00
|
|
|
val = 0;
|
2007-07-14 14:50:38 +04:00
|
|
|
bandwidthSlider->setValue(val);
|
2007-08-04 09:29:35 +04:00
|
|
|
if(val == 0) {
|
2007-07-14 14:50:38 +04:00
|
|
|
limit_lbl->setText(tr("Unlimited", "Unlimited (bandwidth)"));
|
|
|
|
kb_lbl->setText("");
|
|
|
|
} else {
|
|
|
|
limit_lbl->setText(QString(misc::toString(val).c_str()));
|
|
|
|
}
|
2007-04-10 13:43:26 +04:00
|
|
|
}else{
|
2007-07-14 14:50:38 +04:00
|
|
|
qDebug("More than one torrent selected, no initilization");
|
2007-08-04 09:29:35 +04:00
|
|
|
bandwidthSlider->setValue(0);
|
2007-07-14 14:50:38 +04:00
|
|
|
limit_lbl->setText(tr("Unlimited", "Unlimited (bandwidth)"));
|
|
|
|
kb_lbl->setText("");
|
2007-04-10 13:43:26 +04:00
|
|
|
}
|
2007-07-14 14:50:38 +04:00
|
|
|
}else{
|
|
|
|
// Global limit
|
2007-08-04 09:29:35 +04:00
|
|
|
int val = 0;
|
2007-07-14 14:50:38 +04:00
|
|
|
session *s = BTSession->getSession();
|
2007-08-04 09:29:35 +04:00
|
|
|
if(uploadMode){
|
|
|
|
if(s->upload_rate_limit() > 0)
|
|
|
|
val = (int)(s->upload_rate_limit()/1024.);
|
|
|
|
}else{
|
|
|
|
if(s->download_rate_limit() > 0)
|
|
|
|
val = (int)(s->download_rate_limit()/1024.);
|
|
|
|
}
|
|
|
|
if(val == 0){
|
|
|
|
bandwidthSlider->setValue(0);
|
2007-04-12 21:17:04 +04:00
|
|
|
limit_lbl->setText(tr("Unlimited", "Unlimited (bandwidth)"));
|
|
|
|
kb_lbl->setText("");
|
2007-07-14 14:50:38 +04:00
|
|
|
}else{
|
|
|
|
bandwidthSlider->setValue(val);
|
2007-04-12 21:17:04 +04:00
|
|
|
}
|
|
|
|
}
|
2007-07-14 14:50:38 +04:00
|
|
|
connect(buttonBox, SIGNAL(accepted()), this, SLOT(setBandwidth()));
|
|
|
|
show();
|
2007-04-10 13:07:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~BandwidthAllocationDialog(){
|
|
|
|
qDebug("Deleting bandwidth allocation dialog");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected slots:
|
|
|
|
void updateBandwidthLabel(int val){
|
2007-08-04 09:29:35 +04:00
|
|
|
if(val == 0){
|
2007-04-10 13:07:33 +04:00
|
|
|
limit_lbl->setText(tr("Unlimited", "Unlimited (bandwidth)"));
|
|
|
|
kb_lbl->setText("");
|
|
|
|
}else{
|
|
|
|
limit_lbl->setText(QString(misc::toString(val).c_str()));
|
|
|
|
kb_lbl->setText(tr("KiB/s"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-10 13:31:25 +04:00
|
|
|
void setBandwidth(){
|
2007-07-14 17:38:29 +04:00
|
|
|
qDebug("setBandwidth called");
|
2007-04-10 13:31:25 +04:00
|
|
|
int val = bandwidthSlider->value();
|
2007-07-14 14:50:38 +04:00
|
|
|
if(!global){
|
2007-07-14 17:38:29 +04:00
|
|
|
QString hash;
|
2007-07-14 14:50:38 +04:00
|
|
|
if(uploadMode) {
|
2007-07-14 17:38:29 +04:00
|
|
|
foreach(hash, hashes) {
|
2007-08-04 09:29:35 +04:00
|
|
|
if(!val)
|
|
|
|
BTSession->setUploadLimit(hash, -1);
|
|
|
|
else
|
|
|
|
BTSession->setUploadLimit(hash, val*1024);
|
2007-07-14 14:50:38 +04:00
|
|
|
qDebug("Setting upload limit");
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-14 17:38:29 +04:00
|
|
|
foreach(hash, hashes) {
|
2007-08-04 09:29:35 +04:00
|
|
|
if(!val)
|
|
|
|
BTSession->setDownloadLimit(hash, -1);
|
|
|
|
else
|
|
|
|
BTSession->setDownloadLimit(hash, val*1024);
|
2007-07-14 14:50:38 +04:00
|
|
|
qDebug("Setting download limit");
|
|
|
|
}
|
2007-04-12 21:17:04 +04:00
|
|
|
}
|
2007-07-14 14:50:38 +04:00
|
|
|
}else{
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
session *s = BTSession->getSession();
|
|
|
|
if(uploadMode){
|
2007-08-04 09:29:35 +04:00
|
|
|
if(!val)
|
|
|
|
s->set_upload_rate_limit(-1);
|
|
|
|
else
|
|
|
|
s->set_upload_rate_limit(val*1024);
|
2007-07-14 14:50:38 +04:00
|
|
|
settings.setValue("Options/Main/UPLimit", val);
|
|
|
|
}else{
|
2007-08-04 09:29:35 +04:00
|
|
|
if(!val)
|
|
|
|
s->set_download_rate_limit(-1);
|
|
|
|
else
|
|
|
|
s->set_download_rate_limit(val*1024);
|
2007-07-14 14:50:38 +04:00
|
|
|
settings.setValue("Options/Main/DLLimit", val);
|
2007-04-12 21:17:04 +04:00
|
|
|
}
|
2007-04-10 13:43:26 +04:00
|
|
|
}
|
2007-04-10 13:31:25 +04:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2007-04-10 13:07:33 +04:00
|
|
|
private:
|
|
|
|
bool uploadMode;
|
2007-07-14 14:50:38 +04:00
|
|
|
bool global;
|
2007-04-10 13:07:33 +04:00
|
|
|
bittorrent *BTSession;
|
2007-07-14 17:38:29 +04:00
|
|
|
QStringList hashes;
|
2007-04-10 13:07:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|