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 "qtrapids/dbus.hpp"
30 #include "PreferencesDialog.h"
34 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f, QtRapidsServer *server) :
35 QDialog(parent, f), // Superclass
38 uploadRateSpinBox_(NULL),
39 downloadRateSpinBox_(NULL),
43 setWindowTitle("Preferences");
45 QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
46 //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
47 QGridLayout *grid = new QGridLayout;
48 setLayout(verticalBox);
49 verticalBox->addLayout(grid);
51 QLabel *dirLabel = new QLabel(tr("Download directory: "));
52 dirLineEdit_ = new QLineEdit(this);
53 QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
55 QLabel *uploadLabel = new QLabel(tr("Max. upload rate: "));
56 QLabel *downloadLabel = new QLabel(tr("Max. download rate: "));
57 uploadRateSpinBox_ = new QSpinBox(this);
58 downloadRateSpinBox_ = new QSpinBox(this);
60 grid->addWidget(dirLabel, 0, 0);
61 grid->addWidget(dirLineEdit_, 0, 1);
62 grid->addWidget(browseDirButton, 0, 2);
64 grid->addWidget(uploadLabel, 1, 0);
65 grid->addWidget(uploadRateSpinBox_, 1, 1);
66 grid->addWidget(downloadLabel, 2, 0);
67 grid->addWidget(downloadRateSpinBox_, 2, 1);
69 uploadRateSpinBox_->setRange(0, 1000);
70 uploadRateSpinBox_->setSuffix(" kB/s");
71 downloadRateSpinBox_->setRange(0, 1000);
72 downloadRateSpinBox_->setSuffix(" kB/s");
74 // horizontalBox1->addWidget(dirLabel);
75 // horizontalBox1->addWidget(dirLineEdit_);
76 // horizontalBox1->addWidget(browseDirButton);
78 connect(browseDirButton, SIGNAL(clicked()),
79 this, SLOT(on_browseDirButtonClicked()));
82 dialogButtons_ = new QDialogButtonBox(this);
83 dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
84 | QDialogButtonBox::Apply
85 | QDialogButtonBox::Cancel);
87 verticalBox->addWidget(dialogButtons_);
89 connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
90 this, SLOT(on_buttonClicked(QAbstractButton*)));
92 // Set saved preference values to fields.
98 PreferencesDialog::~PreferencesDialog()
102 // ======================== SLOTS ========================
103 void PreferencesDialog::on_browseDirButtonClicked()
105 QString dir = QFileDialog::getExistingDirectory(this, tr("Download directory"), QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
106 on_downloadDirectorySelected(dir);
110 = new QFileDialog(this, "Download directory",
111 QString(), tr("Torrent files (*.torrent)"));
113 dialog->setFileMode(QFileDialog::Directory);
114 dialog->setOption(QFileDialog::ShowDirsOnly, true);
116 connect(dialog, SIGNAL(fileSelected(const QString&)),
117 this, SLOT(on_downloadDirectorySelected(const QString&)));
123 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
125 switch (dialogButtons_->buttonRole ( button ) ) {
126 case QDialogButtonBox::AcceptRole :
127 qDebug() << "PreferencesDialog: OK";
129 done(QDialog::Accepted);
131 case QDialogButtonBox::ApplyRole :
132 qDebug() << "PreferencesDialog: APPLY";
135 case QDialogButtonBox::RejectRole :
136 qDebug() << "PreferencesDialog: CANCEL";
137 done(QDialog::Rejected);
144 void PreferencesDialog::on_downloadDirectorySelected(QString directory)
146 qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
147 // Torrent filename empty, do nothing.
151 dirLineEdit_->clear();
152 dirLineEdit_->insert(directory);
154 /// @todo check that user has privileges to write to this directory.
158 // ========================= Private functions ==========================
159 void PreferencesDialog::WriteSettings()
161 int ulRate = 1000*uploadRateSpinBox_->value();
162 int dlRate = 1000*downloadRateSpinBox_->value();
164 settings_.setValue("download/directory", dirLineEdit_->text());
165 //settings_.setValue("net/uploadRate", ulRate);
166 //settings_.setValue("net/downloadRate", dlRate);
168 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
169 // settings are written also by QSettings() destructor and by event loop at regular interval.
171 // If server pointer was given, apply settings immediately.
173 /// @todo Use DBus interface
174 /// @todo Set parameters for server
175 /// @todo Add speedlimit functionality to Server.
176 qtrapids::ParamsMap_t options;
177 options["net/downloadRate"] = QString::number(dlRate);
178 options["net/uploadRate"] = QString::number(ulRate);
179 server_->setOptions(options);
180 // btSession_->setUploadRateLimit(ulRate);
181 // btSession_->setDownloadRateLimit(dlRate);
186 void PreferencesDialog::ReadSettings()
188 dirLineEdit_->insert(settings_.value("download/directory").toString());
189 uploadRateSpinBox_->setValue(settings_.value("net/uploadRate").toInt()/1000);
190 downloadRateSpinBox_->setValue(settings_.value("net/downloadRate").toInt()/1000);
192 // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
193 // settings are written also by QSettings() destructor and by event loop at regular interval.