Added build script of Debian
[qtrapids] / src / gui / 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 "QBittorrentSession.h"
29 #include "PreferencesDialog.h"
30
31 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f,
32                                                                                                                                                  qtrapids::QBittorrentSession *const btSession) :
33                 QDialog(parent, f), // Superclass
34                 dirLineEdit_(NULL),
35                 dialogButtons_(NULL),
36                 uploadRateSpinBox_(NULL),
37                 downloadRateSpinBox_(NULL),
38                 btSession_(btSession),
39                 settings_()
40 {
41         setWindowTitle("Preferences");
42
43         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
44         //QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
45         QGridLayout *grid = new QGridLayout;
46         setLayout(verticalBox);
47         verticalBox->addLayout(grid);
48
49         QLabel *dirLabel = new QLabel(tr("Download directory: "));
50         dirLineEdit_ = new QLineEdit(this);
51         QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
52         
53         QLabel *uploadLabel = new QLabel(tr("Max. upload rate: "));
54         QLabel *downloadLabel = new QLabel(tr("Max. download rate: "));
55         uploadRateSpinBox_ = new QSpinBox(this);
56         downloadRateSpinBox_ = new QSpinBox(this);
57         
58         grid->addWidget(dirLabel, 0, 0);
59         grid->addWidget(dirLineEdit_, 0, 1);
60         grid->addWidget(browseDirButton, 0, 2);
61         
62         grid->addWidget(uploadLabel, 1, 0);
63         grid->addWidget(uploadRateSpinBox_, 1, 1);
64         grid->addWidget(downloadLabel, 2, 0);
65         grid->addWidget(downloadRateSpinBox_, 2, 1);
66         
67         uploadRateSpinBox_->setRange(0, 1000);
68         uploadRateSpinBox_->setSuffix(" kB/s");
69         downloadRateSpinBox_->setRange(0, 1000);
70         downloadRateSpinBox_->setSuffix(" kB/s");
71         
72 //      horizontalBox1->addWidget(dirLabel);
73 //      horizontalBox1->addWidget(dirLineEdit_);
74 //      horizontalBox1->addWidget(browseDirButton);
75
76         connect(browseDirButton, SIGNAL(clicked()),
77                 this, SLOT(on_browseDirButtonClicked()));
78
79
80         dialogButtons_ = new QDialogButtonBox(this);
81         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
82                                            | QDialogButtonBox::Apply
83                                            | QDialogButtonBox::Cancel);
84
85         verticalBox->addWidget(dialogButtons_);
86
87         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
88                 this, SLOT(on_buttonClicked(QAbstractButton*)));
89
90         // Set saved preference values to fields.
91         ReadSettings();
92
93 }
94
95
96 PreferencesDialog::~PreferencesDialog()
97 {
98 }
99
100 // ======================== SLOTS ========================
101 void PreferencesDialog::on_browseDirButtonClicked()
102 {
103         QFileDialog *dialog
104         = new QFileDialog(this, "Download directory",
105                           QString(), tr("Torrent files (*.torrent)"));
106
107         dialog->setFileMode(QFileDialog::Directory);
108         dialog->setOption(QFileDialog::ShowDirsOnly, true);
109
110         connect(dialog, SIGNAL(fileSelected(const QString&)),
111                 this, SLOT(on_downloadDirectorySelected(const QString&)));
112
113         dialog->show();
114 }
115
116 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
117 {
118         switch (dialogButtons_->buttonRole ( button ) ) {
119         case QDialogButtonBox::AcceptRole :
120                 qDebug() << "PreferencesDialog: OK";
121                 WriteSettings();
122                 done(QDialog::Accepted);
123                 break;
124         case QDialogButtonBox::ApplyRole :
125                 qDebug() << "PreferencesDialog: APPLY";
126                 WriteSettings();
127                 break;
128         case QDialogButtonBox::RejectRole :
129                 qDebug() << "PreferencesDialog: CANCEL";
130                 done(QDialog::Rejected);
131                 break;
132         default:
133                 return;
134         }
135 }
136
137 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
138 {
139         qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
140         // Torrent filename empty, do nothing.
141         if (directory == "")
142                 return;
143
144         dirLineEdit_->clear();
145         dirLineEdit_->insert(directory);
146
147         /// @todo check that user has privileges to write to this directory.
148 }
149
150
151 // ========================= Private functions ==========================
152 void PreferencesDialog::WriteSettings()
153 {
154         int ulRate = 1000*uploadRateSpinBox_->value();
155         int dlRate = 1000*downloadRateSpinBox_->value();
156         
157         settings_.setValue("download/directory", dirLineEdit_->text());
158         settings_.setValue("network/uploadRate", ulRate);
159         settings_.setValue("network/downloadRate", dlRate);
160         
161         // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
162         // settings are written also by QSettings() destructor and by event loop at regular interval.
163         
164         // If session pointer was given, apply settings immediately.
165         if (btSession_) {
166                 btSession_->setUploadRateLimit(ulRate);
167                 btSession_->setDownloadRateLimit(dlRate);
168         }
169         
170 }
171
172 void PreferencesDialog::ReadSettings()
173 {
174         dirLineEdit_->insert(settings_.value("download/directory").toString());
175         uploadRateSpinBox_->setValue(settings_.value("network/uploadRate").toInt()/1000);
176         downloadRateSpinBox_->setValue(settings_.value("network/downloadRate").toInt()/1000);
177         
178         // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
179         // settings are written also by QSettings() destructor and by event loop at regular interval.
180 }
181