1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include <QPushButton>
26 #include <QDialogButtonBox>
27 #include <QAbstractButton>
28 #include <QFileDialog>
30 #include "qtrapids/dbus.hpp"
32 #include "PreferencesDialog.h"
36 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f, QtRapidsServer *server) :
37 QDialog(parent, f), // Superclass
40 uploadRateSpinBox_(NULL),
41 downloadRateSpinBox_(NULL),
45 setWindowTitle("Preferences");
47 QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
48 //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
49 QGridLayout *grid = new QGridLayout;
50 setLayout(verticalBox);
51 verticalBox->addLayout(grid);
53 QLabel *dirLabel = new QLabel(tr("Download directory: "));
54 dirLineEdit_ = new QLineEdit(this);
55 QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
57 QLabel *uploadLabel = new QLabel(tr("Max. upload rate: "));
58 QLabel *downloadLabel = new QLabel(tr("Max. download rate: "));
59 uploadRateSpinBox_ = new QSpinBox(this);
60 downloadRateSpinBox_ = new QSpinBox(this);
62 grid->addWidget(dirLabel, 0, 0);
63 grid->addWidget(dirLineEdit_, 0, 1);
64 grid->addWidget(browseDirButton, 0, 2);
66 grid->addWidget(uploadLabel, 1, 0);
67 grid->addWidget(uploadRateSpinBox_, 1, 1);
68 grid->addWidget(downloadLabel, 2, 0);
69 grid->addWidget(downloadRateSpinBox_, 2, 1);
71 uploadRateSpinBox_->setRange(0, 1000);
72 uploadRateSpinBox_->setSuffix(" kB/s");
73 downloadRateSpinBox_->setRange(0, 1000);
74 downloadRateSpinBox_->setSuffix(" kB/s");
76 // horizontalBox1->addWidget(dirLabel);
77 // horizontalBox1->addWidget(dirLineEdit_);
78 // horizontalBox1->addWidget(browseDirButton);
80 connect(browseDirButton, SIGNAL(clicked()),
81 this, SLOT(on_browseDirButtonClicked()));
84 dialogButtons_ = new QDialogButtonBox(this);
85 dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
86 | QDialogButtonBox::Apply
87 | QDialogButtonBox::Cancel);
89 verticalBox->addWidget(dialogButtons_);
91 connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
92 this, SLOT(on_buttonClicked(QAbstractButton*)));
94 // Set saved preference values to fields.
100 PreferencesDialog::~PreferencesDialog()
104 // ======================== SLOTS ========================
105 void PreferencesDialog::on_browseDirButtonClicked()
107 QString dir = QFileDialog::getExistingDirectory(this, tr("Download directory"), QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
108 on_downloadDirectorySelected(dir);
112 = new QFileDialog(this, "Download directory",
113 QString(), tr("Torrent files (*.torrent)"));
115 dialog->setFileMode(QFileDialog::Directory);
116 dialog->setOption(QFileDialog::ShowDirsOnly, true);
118 connect(dialog, SIGNAL(fileSelected(const QString&)),
119 this, SLOT(on_downloadDirectorySelected(const QString&)));
125 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
127 switch (dialogButtons_->buttonRole ( button ) ) {
128 case QDialogButtonBox::AcceptRole :
129 qDebug() << "PreferencesDialog: OK";
131 done(QDialog::Accepted);
133 case QDialogButtonBox::ApplyRole :
134 qDebug() << "PreferencesDialog: APPLY";
137 case QDialogButtonBox::RejectRole :
138 qDebug() << "PreferencesDialog: CANCEL";
139 done(QDialog::Rejected);
146 void PreferencesDialog::on_downloadDirectorySelected(QString directory)
148 qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
149 // Torrent filename empty, do nothing.
153 dirLineEdit_->clear();
154 dirLineEdit_->insert(directory);
156 /// @todo check that user has privileges to write to this directory.
160 // ========================= Private functions ==========================
161 void PreferencesDialog::WriteSettings()
163 int ulRate = 1000*uploadRateSpinBox_->value();
164 int dlRate = 1000*downloadRateSpinBox_->value();
166 settings_.setValue("download/directory", dirLineEdit_->text());
167 //settings_.setValue("net/uploadRate", ulRate);
168 //settings_.setValue("net/downloadRate", dlRate);
170 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
171 // settings are written also by QSettings() destructor and by event loop at regular interval.
173 // If server pointer was given, apply settings immediately.
175 /// @todo Use DBus interface
176 /// @todo Set parameters for server
177 /// @todo Add speedlimit functionality to Server.
178 qtrapids::ParamsMap_t options;
179 options["net/downloadRate"] = QString::number(dlRate);
180 options["net/uploadRate"] = QString::number(ulRate);
181 server_->setOptions(options);
182 // btSession_->setUploadRateLimit(ulRate);
183 // btSession_->setDownloadRateLimit(dlRate);
188 void PreferencesDialog::ReadSettings()
190 dirLineEdit_->insert(settings_.value("download/directory").toString());
191 uploadRateSpinBox_->setValue(settings_.value("net/uploadRate").toInt()/1000);
192 downloadRateSpinBox_->setValue(settings_.value("net/downloadRate").toInt()/1000);
194 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
195 // settings are written also by QSettings() destructor and by event loop at regular interval.