e541e909ede6a006d40242b4739806b5278eb172
[qtrapids] / src / gui / 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 <QDialogButtonBox>
26 #include <QAbstractButton>
27 #include <QFileDialog>
28
29 #include "PreferencesDialog.h"
30
31 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) :
32                 QDialog(parent, f), // Superclass
33                 dirLineEdit_(NULL),
34                 dialogButtons_(NULL),
35                 settings_()
36 {
37         setWindowTitle("Preferences");
38
39         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
40         QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
41         setLayout(verticalBox);
42         verticalBox->addLayout(horizontalBox1);
43
44         QLabel *dirLabel = new QLabel(tr("Download directory: "));
45         dirLineEdit_ = new QLineEdit(this);
46         QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
47
48         horizontalBox1->addWidget(dirLabel);
49         horizontalBox1->addWidget(dirLineEdit_);
50         horizontalBox1->addWidget(browseDirButton);
51
52         connect(browseDirButton, SIGNAL(clicked()),
53                 this, SLOT(on_browseDirButtonClicked()));
54
55
56         dialogButtons_ = new QDialogButtonBox(this);
57         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
58                                            | QDialogButtonBox::Apply
59                                            | QDialogButtonBox::Cancel);
60
61         verticalBox->addWidget(dialogButtons_);
62
63         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
64                 this, SLOT(on_buttonClicked(QAbstractButton*)));
65
66         // Set saved preference values to fields.
67         ReadSettings();
68
69 }
70
71
72 PreferencesDialog::~PreferencesDialog()
73 {
74 }
75
76 // ======================== SLOTS ========================
77 void PreferencesDialog::on_browseDirButtonClicked()
78 {
79         QFileDialog *dialog
80         = new QFileDialog(this, "Download directory",
81                           QString(), tr("Torrent files (*.torrent)"));
82
83         dialog->setFileMode(QFileDialog::Directory);
84         dialog->setOption(QFileDialog::ShowDirsOnly, true);
85
86         connect(dialog, SIGNAL(fileSelected(const QString&)),
87                 this, SLOT(on_downloadDirectorySelected(const QString&)));
88
89         dialog->show();
90 }
91
92 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
93 {
94         switch (dialogButtons_->buttonRole ( button ) ) {
95         case QDialogButtonBox::AcceptRole :
96                 qDebug() << "PreferencesDialog: OK";
97                 WriteSettings();
98                 close();
99                 break;
100         case QDialogButtonBox::ApplyRole :
101                 qDebug() << "PreferencesDialog: APPLY";
102                 WriteSettings();
103                 break;
104         case QDialogButtonBox::RejectRole :
105                 qDebug() << "PreferencesDialog: CANCEL";
106                 close();
107                 break;
108         default:
109                 return;
110         }
111 }
112
113 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
114 {
115         qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
116         // Torrent filename empty, do nothing.
117         if (directory == "")
118                 return;
119
120         dirLineEdit_->insert(directory);
121
122         /// @todo check that user has privileges to write to this directory.
123 }
124
125
126 // ========================= Private functions ==========================
127 void PreferencesDialog::WriteSettings()
128 {
129         settings_.setValue("download/directory", dirLineEdit_->text());
130
131         // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
132         // settings are written also by QSettings() destructor and by event loop at regular interval.
133 }
134
135 void PreferencesDialog::ReadSettings()
136 {
137         dirLineEdit_->insert(settings_.value("download/directory").toString());
138
139         // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
140         // settings are written also by QSettings() destructor and by event loop at regular interval.
141 }
142