Minor fixes
[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         QString dir = QFileDialog::getExistingDirectory(this, tr("Download directory"), QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
108         on_downloadDirectorySelected(dir);
109         
110         /*
111         QFileDialog *dialog
112         = new QFileDialog(this, "Download directory",
113                           QString(), tr("Torrent files (*.torrent)"));
114
115         dialog->setFileMode(QFileDialog::Directory);
116         dialog->setOption(QFileDialog::ShowDirsOnly, true);
117
118         connect(dialog, SIGNAL(fileSelected(const QString&)),
119                 this, SLOT(on_downloadDirectorySelected(const QString&)));
120
121         dialog->show();
122         */
123 }
124
125 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
126 {
127         switch (dialogButtons_->buttonRole ( button ) ) {
128         case QDialogButtonBox::AcceptRole :
129                 qDebug() << "PreferencesDialog: OK";
130                 WriteSettings();
131                 done(QDialog::Accepted);
132                 break;
133         case QDialogButtonBox::ApplyRole :
134                 qDebug() << "PreferencesDialog: APPLY";
135                 WriteSettings();
136                 break;
137         case QDialogButtonBox::RejectRole :
138                 qDebug() << "PreferencesDialog: CANCEL";
139                 done(QDialog::Rejected);
140                 break;
141         default:
142                 return;
143         }
144 }
145
146 void PreferencesDialog::on_downloadDirectorySelected(QString directory)
147 {
148         qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
149         // Torrent filename empty, do nothing.
150         if (directory == "")
151                 return;
152
153         dirLineEdit_->clear();
154         dirLineEdit_->insert(directory);
155
156         /// @todo check that user has privileges to write to this directory.
157 }
158
159
160 // ========================= Private functions ==========================
161 void PreferencesDialog::WriteSettings()
162 {
163         int ulRate = 1000*uploadRateSpinBox_->value();
164         int dlRate = 1000*downloadRateSpinBox_->value();
165         
166         settings_.setValue("download/directory", dirLineEdit_->text());
167         //settings_.setValue("net/uploadRate", ulRate);
168         //settings_.setValue("net/downloadRate", dlRate);
169         
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.
172         
173         // If server pointer was given, apply settings immediately.
174         if (server_) {
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);
184         }
185         
186 }
187
188 void PreferencesDialog::ReadSettings()
189 {
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);
193         
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.
196 }
197