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()
108 = new QFileDialog(this, "Download directory",
109 QString(), tr("Torrent files (*.torrent)"));
111 dialog->setFileMode(QFileDialog::Directory);
112 dialog->setOption(QFileDialog::ShowDirsOnly, true);
114 connect(dialog, SIGNAL(fileSelected(const QString&)),
115 this, SLOT(on_downloadDirectorySelected(const QString&)));
120 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
122 switch (dialogButtons_->buttonRole ( button ) ) {
123 case QDialogButtonBox::AcceptRole :
124 qDebug() << "PreferencesDialog: OK";
126 done(QDialog::Accepted);
128 case QDialogButtonBox::ApplyRole :
129 qDebug() << "PreferencesDialog: APPLY";
132 case QDialogButtonBox::RejectRole :
133 qDebug() << "PreferencesDialog: CANCEL";
134 done(QDialog::Rejected);
141 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
143 qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
144 // Torrent filename empty, do nothing.
148 dirLineEdit_->clear();
149 dirLineEdit_->insert(directory);
151 /// @todo check that user has privileges to write to this directory.
155 // ========================= Private functions ==========================
156 void PreferencesDialog::WriteSettings()
158 int ulRate = 1000*uploadRateSpinBox_->value();
159 int dlRate = 1000*downloadRateSpinBox_->value();
161 settings_.setValue("download/directory", dirLineEdit_->text());
162 //settings_.setValue("net/uploadRate", ulRate);
163 //settings_.setValue("net/downloadRate", dlRate);
165 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
166 // settings are written also by QSettings() destructor and by event loop at regular interval.
168 // If server pointer was given, apply settings immediately.
170 /// @todo Use DBus interface
171 /// @todo Set parameters for server
172 /// @todo Add speedlimit functionality to Server.
173 qtrapids::ParamsMap_t options;
174 options["net/downloadRate"] = QString::number(dlRate);
175 options["net/uploadRate"] = QString::number(ulRate);
176 server_->setOptions(options);
177 // btSession_->setUploadRateLimit(ulRate);
178 // btSession_->setDownloadRateLimit(dlRate);
183 void PreferencesDialog::ReadSettings()
185 dirLineEdit_->insert(settings_.value("download/directory").toString());
186 uploadRateSpinBox_->setValue(settings_.value("net/uploadRate").toInt()/1000);
187 downloadRateSpinBox_->setValue(settings_.value("net/downloadRate").toInt()/1000);
189 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
190 // settings are written also by QSettings() destructor and by event loop at regular interval.