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