- Added session-level upload/download-rate limit preferences
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Thu, 10 Dec 2009 14:40:55 +0000 (14:40 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Thu, 10 Dec 2009 14:40:55 +0000 (14:40 +0000)
git-svn-id: file:///svnroot/qtrapids/trunk@53 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda

src/gui/MainWindow.cpp
src/gui/MainWindow.h
src/gui/PreferencesDialog.cpp
src/gui/PreferencesDialog.h

index 273aadc..6ab93c6 100644 (file)
@@ -119,6 +119,7 @@ MainWindow::MainWindow():
                this, SLOT(on_alert(std::auto_ptr<Alert>)));
 
        LoadPlugins();
                this, SLOT(on_alert(std::auto_ptr<Alert>)));
 
        LoadPlugins();
+       RestoreSettings();
 }
 
 
 }
 
 
@@ -268,6 +269,12 @@ void MainWindow::LoadPlugins()
 }
 
 
 }
 
 
+void MainWindow::RestoreSettings()
+{
+       btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt());
+       btSession_.setUploadRateLimit(settings_.value("network/downloadRate").toInt());
+}
+
 
 // Opens torrent information from buffer data and adds torrent to session 
 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
 
 // Opens torrent information from buffer data and adds torrent to session 
 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
@@ -314,8 +321,9 @@ void MainWindow::on_quitAction_clicked()
 void MainWindow::on_preferencesAction_clicked()
 {
        if (!preferencesDialog_) {
 void MainWindow::on_preferencesAction_clicked()
 {
        if (!preferencesDialog_) {
-               preferencesDialog_ = new PreferencesDialog(this);
+               preferencesDialog_ = new PreferencesDialog(this, NULL, &btSession_);
        }
        }
+
        preferencesDialog_->show();
        preferencesDialog_->raise();
        preferencesDialog_->activateWindow();
        preferencesDialog_->show();
        preferencesDialog_->raise();
        preferencesDialog_->activateWindow();
index 53fd0af..3c5a61e 100644 (file)
@@ -74,6 +74,7 @@ class MainWindow : public QMainWindow, public qtrapids::PluginHostInterface {
        
        private:
                void LoadPlugins();
        
        private:
                void LoadPlugins();
+               void RestoreSettings();
                void StartTorrentFromBufferData(char const* data, int size);
                
        private:
                void StartTorrentFromBufferData(char const* data, int size);
                
        private:
index e541e90..099755d 100644 (file)
 #include <QLineEdit>
 #include <QLabel>
 #include <QPushButton>
 #include <QLineEdit>
 #include <QLabel>
 #include <QPushButton>
+#include <QSpinBox>
 #include <QDialogButtonBox>
 #include <QAbstractButton>
 #include <QFileDialog>
 
 #include <QDialogButtonBox>
 #include <QAbstractButton>
 #include <QFileDialog>
 
+#include "QBittorrentSession.h"
 #include "PreferencesDialog.h"
 
 #include "PreferencesDialog.h"
 
-PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) :
+PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f,
+                                                                                                                                                qtrapids::QBittorrentSession *const btSession) :
                QDialog(parent, f), // Superclass
                dirLineEdit_(NULL),
                dialogButtons_(NULL),
                QDialog(parent, f), // Superclass
                dirLineEdit_(NULL),
                dialogButtons_(NULL),
+               uploadRateSpinBox_(NULL),
+               downloadRateSpinBox_(NULL),
+               btSession_(btSession),
                settings_()
 {
        setWindowTitle("Preferences");
 
        QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
                settings_()
 {
        setWindowTitle("Preferences");
 
        QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
-       QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
+       //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
+       QGridLayout *grid = new QGridLayout;
        setLayout(verticalBox);
        setLayout(verticalBox);
-       verticalBox->addLayout(horizontalBox1);
+       verticalBox->addLayout(grid);
 
        QLabel *dirLabel = new QLabel(tr("Download directory: "));
        dirLineEdit_ = new QLineEdit(this);
        QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
 
        QLabel *dirLabel = new QLabel(tr("Download directory: "));
        dirLineEdit_ = new QLineEdit(this);
        QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
-
-       horizontalBox1->addWidget(dirLabel);
-       horizontalBox1->addWidget(dirLineEdit_);
-       horizontalBox1->addWidget(browseDirButton);
+       
+       QLabel *uploadLabel = new QLabel(tr("Max. upload rate: "));
+       QLabel *downloadLabel = new QLabel(tr("Max. download rate: "));
+       uploadRateSpinBox_ = new QSpinBox(this);
+       downloadRateSpinBox_ = new QSpinBox(this);
+       
+       grid->addWidget(dirLabel, 0, 0);
+       grid->addWidget(dirLineEdit_, 0, 1);
+       grid->addWidget(browseDirButton, 0, 2);
+       
+       grid->addWidget(uploadLabel, 1, 0);
+       grid->addWidget(uploadRateSpinBox_, 1, 1);
+       grid->addWidget(downloadLabel, 2, 0);
+       grid->addWidget(downloadRateSpinBox_, 2, 1);
+       
+       uploadRateSpinBox_->setRange(0, 1000);
+       uploadRateSpinBox_->setSuffix(" kB/s");
+       downloadRateSpinBox_->setRange(0, 1000);
+       downloadRateSpinBox_->setSuffix(" kB/s");
+       
+//     horizontalBox1->addWidget(dirLabel);
+//     horizontalBox1->addWidget(dirLineEdit_);
+//     horizontalBox1->addWidget(browseDirButton);
 
        connect(browseDirButton, SIGNAL(clicked()),
                this, SLOT(on_browseDirButtonClicked()));
 
        connect(browseDirButton, SIGNAL(clicked()),
                this, SLOT(on_browseDirButtonClicked()));
@@ -95,7 +121,7 @@ void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
        case QDialogButtonBox::AcceptRole :
                qDebug() << "PreferencesDialog: OK";
                WriteSettings();
        case QDialogButtonBox::AcceptRole :
                qDebug() << "PreferencesDialog: OK";
                WriteSettings();
-               close();
+               done(QDialog::Accepted);
                break;
        case QDialogButtonBox::ApplyRole :
                qDebug() << "PreferencesDialog: APPLY";
                break;
        case QDialogButtonBox::ApplyRole :
                qDebug() << "PreferencesDialog: APPLY";
@@ -103,7 +129,7 @@ void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
                break;
        case QDialogButtonBox::RejectRole :
                qDebug() << "PreferencesDialog: CANCEL";
                break;
        case QDialogButtonBox::RejectRole :
                qDebug() << "PreferencesDialog: CANCEL";
-               close();
+               done(QDialog::Rejected);
                break;
        default:
                return;
                break;
        default:
                return;
@@ -117,6 +143,7 @@ void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
        if (directory == "")
                return;
 
        if (directory == "")
                return;
 
+       dirLineEdit_->clear();
        dirLineEdit_->insert(directory);
 
        /// @todo check that user has privileges to write to this directory.
        dirLineEdit_->insert(directory);
 
        /// @todo check that user has privileges to write to this directory.
@@ -126,16 +153,30 @@ void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
 // ========================= Private functions ==========================
 void PreferencesDialog::WriteSettings()
 {
 // ========================= Private functions ==========================
 void PreferencesDialog::WriteSettings()
 {
+       int ulRate = 1000*uploadRateSpinBox_->value();
+       int dlRate = 1000*downloadRateSpinBox_->value();
+       
        settings_.setValue("download/directory", dirLineEdit_->text());
        settings_.setValue("download/directory", dirLineEdit_->text());
-
+       settings_.setValue("network/uploadRate", ulRate);
+       settings_.setValue("network/downloadRate", dlRate);
+       
        // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
        // settings are written also by QSettings() destructor and by event loop at regular interval.
        // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
        // settings are written also by QSettings() destructor and by event loop at regular interval.
+       
+       // If session pointer was given, apply settings immediately.
+       if (btSession_) {
+               btSession_->setUploadRateLimit(ulRate);
+               btSession_->setDownloadrateLimit(dlRate);
+       }
+       
 }
 
 void PreferencesDialog::ReadSettings()
 {
        dirLineEdit_->insert(settings_.value("download/directory").toString());
 }
 
 void PreferencesDialog::ReadSettings()
 {
        dirLineEdit_->insert(settings_.value("download/directory").toString());
-
+       uploadRateSpinBox_->setValue(settings_.value("network/uploadRate").toInt());
+       downloadRateSpinBox_->setValue(settings_.value("network/downloadRate").toInt());
+       
        // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
        // settings are written also by QSettings() destructor and by event loop at regular interval.
 }
        // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
        // settings are written also by QSettings() destructor and by event loop at regular interval.
 }
index 387a1e5..629acb7 100644 (file)
 
 class QAbstractButton;
 class QLineEdit;
 
 class QAbstractButton;
 class QLineEdit;
+class QSpinBox;
 class QDialogButtonBox;
 
 class QDialogButtonBox;
 
+namespace qtrapids {
+       class QBittorrentSession;
+}
+
+
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
@@ -37,7 +43,7 @@ class PreferencesDialog : public QDialog
        Q_OBJECT
 
 public:
        Q_OBJECT
 
 public:
-       PreferencesDialog(QWidget* parent = 0, Qt::WindowFlags f = 0);
+       PreferencesDialog(QWidget* parent = 0, Qt::WindowFlags f = 0, qtrapids::QBittorrentSession *btSession = 0);
 
        ~PreferencesDialog();
 
 
        ~PreferencesDialog();
 
@@ -49,6 +55,9 @@ private slots:
 private:
        QLineEdit *dirLineEdit_;
        QDialogButtonBox *dialogButtons_;
 private:
        QLineEdit *dirLineEdit_;
        QDialogButtonBox *dialogButtons_;
+       QSpinBox *uploadRateSpinBox_, *downloadRateSpinBox_;
+       
+       qtrapids::QBittorrentSession *const btSession_;
        QSettings settings_;
 
        // Private functions:
        QSettings settings_;
 
        // Private functions: