f4043361a8718b8331e34451b7b6d1abb4f48248
[qtrapids] / src / client / PreferencesDialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20 #include <QDebug>
21 #include <QBoxLayout>
22 #include <QLineEdit>
23 #include <QLabel>
24 #include <QPushButton>
25 #include <QSpinBox>
26 #include <QDialogButtonBox>
27 #include <QAbstractButton>
28 #include <QFileDialog>
29
30 #include "qtrapids/dbus.hpp"
31 #include "proxy.h"
32 #include "PreferencesDialog.h"
33
34
35
36 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f, QtRapidsServer *server) :
37                 QDialog(parent, f), // Superclass
38                 dirLineEdit_(NULL),
39                 dialogButtons_(NULL),
40                 uploadRateSpinBox_(NULL),
41                 downloadRateSpinBox_(NULL),
42                 server_(server),
43                 settings_()
44 {
45         setWindowTitle("Preferences");
46
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);
52
53         QLabel *dirLabel = new QLabel(tr("Download directory: "));
54         dirLineEdit_ = new QLineEdit(this);
55         QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
56         
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);
61         
62         grid->addWidget(dirLabel, 0, 0);
63         grid->addWidget(dirLineEdit_, 0, 1);
64         grid->addWidget(browseDirButton, 0, 2);
65         
66         grid->addWidget(uploadLabel, 1, 0);
67         grid->addWidget(uploadRateSpinBox_, 1, 1);
68         grid->addWidget(downloadLabel, 2, 0);
69         grid->addWidget(downloadRateSpinBox_, 2, 1);
70         
71         uploadRateSpinBox_->setRange(0, 1000);
72         uploadRateSpinBox_->setSuffix(" kB/s");
73         downloadRateSpinBox_->setRange(0, 1000);
74         downloadRateSpinBox_->setSuffix(" kB/s");
75         
76 //      horizontalBox1->addWidget(dirLabel);
77 //      horizontalBox1->addWidget(dirLineEdit_);
78 //      horizontalBox1->addWidget(browseDirButton);
79
80         connect(browseDirButton, SIGNAL(clicked()),
81                 this, SLOT(on_browseDirButtonClicked()));
82
83
84         dialogButtons_ = new QDialogButtonBox(this);
85         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
86                                            | QDialogButtonBox::Apply
87                                            | QDialogButtonBox::Cancel);
88
89         verticalBox->addWidget(dialogButtons_);
90
91         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
92                 this, SLOT(on_buttonClicked(QAbstractButton*)));
93
94         // Set saved preference values to fields.
95         ReadSettings();
96
97 }
98
99
100 PreferencesDialog::~PreferencesDialog()
101 {
102 }
103
104 // ======================== SLOTS ========================
105 void PreferencesDialog::on_browseDirButtonClicked()
106 {
107         QFileDialog *dialog
108         = new QFileDialog(this, "Download directory",
109                           QString(), tr("Torrent files (*.torrent)"));
110
111         dialog->setFileMode(QFileDialog::Directory);
112         dialog->setOption(QFileDialog::ShowDirsOnly, true);
113
114         connect(dialog, SIGNAL(fileSelected(const QString&)),
115                 this, SLOT(on_downloadDirectorySelected(const QString&)));
116
117         dialog->show();
118 }
119
120 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
121 {
122         switch (dialogButtons_->buttonRole ( button ) ) {
123         case QDialogButtonBox::AcceptRole :
124                 qDebug() << "PreferencesDialog: OK";
125                 WriteSettings();
126                 done(QDialog::Accepted);
127                 break;
128         case QDialogButtonBox::ApplyRole :
129                 qDebug() << "PreferencesDialog: APPLY";
130                 WriteSettings();
131                 break;
132         case QDialogButtonBox::RejectRole :
133                 qDebug() << "PreferencesDialog: CANCEL";
134                 done(QDialog::Rejected);
135                 break;
136         default:
137                 return;
138         }
139 }
140
141 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
142 {
143         qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
144         // Torrent filename empty, do nothing.
145         if (directory == "")
146                 return;
147
148         dirLineEdit_->clear();
149         dirLineEdit_->insert(directory);
150
151         /// @todo check that user has privileges to write to this directory.
152 }
153
154
155 // ========================= Private functions ==========================
156 void PreferencesDialog::WriteSettings()
157 {
158         int ulRate = 1000*uploadRateSpinBox_->value();
159         int dlRate = 1000*downloadRateSpinBox_->value();
160         
161         settings_.setValue("download/directory", dirLineEdit_->text());
162         //settings_.setValue("net/uploadRate", ulRate);
163         //settings_.setValue("net/downloadRate", dlRate);
164         
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.
167         
168         // If server pointer was given, apply settings immediately.
169         if (server_) {
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);
179         }
180         
181 }
182
183 void PreferencesDialog::ReadSettings()
184 {
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);
188         
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.
191 }
192