- Plugin interface added to MainWindow
[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     {
96     case QDialogButtonBox::AcceptRole :
97         qDebug() << "PreferencesDialog: OK";
98         WriteSettings();
99         close();
100         break;
101     case QDialogButtonBox::ApplyRole :
102         qDebug() << "PreferencesDialog: APPLY";
103         WriteSettings();
104         break;
105     case QDialogButtonBox::RejectRole :
106         qDebug() << "PreferencesDialog: CANCEL";
107         close();
108         break;
109     default:
110         return;
111     }
112 }
113
114 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
115 {
116     qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
117     // Torrent filename empty, do nothing.
118     if (directory == "")
119         return;
120
121     dirLineEdit_->insert(directory);
122
123     /// @todo check that user has privileges to write to this directory.
124 }
125
126
127 // ========================= Private functions ==========================
128 void PreferencesDialog::WriteSettings()
129 {
130     settings_.setValue("download/directory", dirLineEdit_->text());
131
132     // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
133     // settings are written also by QSettings() destructor and by event loop at regular interval.
134 }
135
136 void PreferencesDialog::ReadSettings()
137 {
138     dirLineEdit_->insert(settings_.value("download/directory").toString());
139
140     // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
141     // settings are written also by QSettings() destructor and by event loop at regular interval.
142 }
143