1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
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. *
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. *
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 ***************************************************************************/
24 #include <QPushButton>
25 #include <QDialogButtonBox>
26 #include <QAbstractButton>
27 #include <QFileDialog>
29 #include "PreferencesDialog.h"
31 PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) :
32 QDialog(parent, f), // Superclass
37 setWindowTitle("Preferences");
39 QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
40 QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
41 setLayout(verticalBox);
42 verticalBox->addLayout(horizontalBox1);
44 QLabel *dirLabel = new QLabel(tr("Download directory: "));
45 dirLineEdit_ = new QLineEdit(this);
46 QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
48 horizontalBox1->addWidget(dirLabel);
49 horizontalBox1->addWidget(dirLineEdit_);
50 horizontalBox1->addWidget(browseDirButton);
52 connect(browseDirButton, SIGNAL(clicked()),
53 this, SLOT(on_browseDirButtonClicked()));
56 dialogButtons_ = new QDialogButtonBox(this);
57 dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
58 | QDialogButtonBox::Apply
59 | QDialogButtonBox::Cancel);
61 verticalBox->addWidget(dialogButtons_);
63 connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
64 this, SLOT(on_buttonClicked(QAbstractButton*)));
66 // Set saved preference values to fields.
72 PreferencesDialog::~PreferencesDialog()
76 // ======================== SLOTS ========================
77 void PreferencesDialog::on_browseDirButtonClicked()
80 = new QFileDialog(this, "Download directory",
81 QString(), tr("Torrent files (*.torrent)"));
83 dialog->setFileMode(QFileDialog::Directory);
84 dialog->setOption(QFileDialog::ShowDirsOnly, true);
86 connect(dialog, SIGNAL(fileSelected(const QString&)),
87 this, SLOT(on_downloadDirectorySelected(const QString&)));
92 void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
94 switch (dialogButtons_->buttonRole ( button ) )
96 case QDialogButtonBox::AcceptRole :
97 qDebug() << "PreferencesDialog: OK";
101 case QDialogButtonBox::ApplyRole :
102 qDebug() << "PreferencesDialog: APPLY";
105 case QDialogButtonBox::RejectRole :
106 qDebug() << "PreferencesDialog: CANCEL";
114 void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
116 qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
117 // Torrent filename empty, do nothing.
121 dirLineEdit_->insert(directory);
123 /// @todo check that user has privileges to write to this directory.
127 // ========================= Private functions ==========================
128 void PreferencesDialog::WriteSettings()
130 settings_.setValue("download/directory", dirLineEdit_->text());
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.
136 void PreferencesDialog::ReadSettings()
138 dirLineEdit_->insert(settings_.value("download/directory").toString());
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.