1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
22 #include <QPushButton>
24 #include <QDialogButtonBox>
25 #include <QAbstractButton>
26 #include <QFileDialog>
28 #include "QBittorrentSession.h"
29 #include "PreferencesDialog.h"
31 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f,
32 qtrapids::QBittorrentSession *const btSession) :
33 QDialog(parent, f), // Superclass
36 uploadRateSpinBox_(NULL),
37 downloadRateSpinBox_(NULL),
38 btSession_(btSession),
41 setWindowTitle("Preferences");
43 QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
44 //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
45 QGridLayout *grid = new QGridLayout;
46 setLayout(verticalBox);
47 verticalBox->addLayout(grid);
49 QLabel *dirLabel = new QLabel(tr("Download directory: "));
50 dirLineEdit_ = new QLineEdit(this);
51 QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
53 QLabel *uploadLabel = new QLabel(tr("Max. upload rate: "));
54 QLabel *downloadLabel = new QLabel(tr("Max. download rate: "));
55 uploadRateSpinBox_ = new QSpinBox(this);
56 downloadRateSpinBox_ = new QSpinBox(this);
58 grid->addWidget(dirLabel, 0, 0);
59 grid->addWidget(dirLineEdit_, 0, 1);
60 grid->addWidget(browseDirButton, 0, 2);
62 grid->addWidget(uploadLabel, 1, 0);
63 grid->addWidget(uploadRateSpinBox_, 1, 1);
64 grid->addWidget(downloadLabel, 2, 0);
65 grid->addWidget(downloadRateSpinBox_, 2, 1);
67 uploadRateSpinBox_->setRange(0, 1000);
68 uploadRateSpinBox_->setSuffix(" kB/s");
69 downloadRateSpinBox_->setRange(0, 1000);
70 downloadRateSpinBox_->setSuffix(" kB/s");
72 // horizontalBox1->addWidget(dirLabel);
73 // horizontalBox1->addWidget(dirLineEdit_);
74 // horizontalBox1->addWidget(browseDirButton);
76 connect(browseDirButton, SIGNAL(clicked()),
77 this, SLOT(on_browseDirButtonClicked()));
80 dialogButtons_ = new QDialogButtonBox(this);
81 dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
82 | QDialogButtonBox::Apply
83 | QDialogButtonBox::Cancel);
85 verticalBox->addWidget(dialogButtons_);
87 connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
88 this, SLOT(on_buttonClicked(QAbstractButton*)));
90 // Set saved preference values to fields.
96 PreferencesDialog::~PreferencesDialog()
100 // ======================== SLOTS ========================
101 void PreferencesDialog::on_browseDirButtonClicked()
104 = new QFileDialog(this, "Download directory",
105 QString(), tr("Torrent files (*.torrent)"));
107 dialog->setFileMode(QFileDialog::Directory);
108 dialog->setOption(QFileDialog::ShowDirsOnly, true);
110 connect(dialog, SIGNAL(fileSelected(const QString&)),
111 this, SLOT(on_downloadDirectorySelected(const QString&)));
116 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
118 switch (dialogButtons_->buttonRole ( button ) ) {
119 case QDialogButtonBox::AcceptRole :
120 qDebug() << "PreferencesDialog: OK";
122 done(QDialog::Accepted);
124 case QDialogButtonBox::ApplyRole :
125 qDebug() << "PreferencesDialog: APPLY";
128 case QDialogButtonBox::RejectRole :
129 qDebug() << "PreferencesDialog: CANCEL";
130 done(QDialog::Rejected);
137 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
139 qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
140 // Torrent filename empty, do nothing.
144 dirLineEdit_->clear();
145 dirLineEdit_->insert(directory);
147 /// @todo check that user has privileges to write to this directory.
151 // ========================= Private functions ==========================
152 void PreferencesDialog::WriteSettings()
154 int ulRate = 1000*uploadRateSpinBox_->value();
155 int dlRate = 1000*downloadRateSpinBox_->value();
157 settings_.setValue("download/directory", dirLineEdit_->text());
158 settings_.setValue("network/uploadRate", ulRate);
159 settings_.setValue("network/downloadRate", dlRate);
161 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
162 // settings are written also by QSettings() destructor and by event loop at regular interval.
164 // If session pointer was given, apply settings immediately.
166 btSession_->setUploadRateLimit(ulRate);
167 btSession_->setDownloadRateLimit(dlRate);
172 void PreferencesDialog::ReadSettings()
174 dirLineEdit_->insert(settings_.value("download/directory").toString());
175 uploadRateSpinBox_->setValue(settings_.value("network/uploadRate").toInt()/1000);
176 downloadRateSpinBox_->setValue(settings_.value("network/downloadRate").toInt()/1000);
178 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
179 // settings are written also by QSettings() destructor and by event loop at regular interval.