+/***************************************************************************
+ * Copyright (C) 2009 by Lassi Väätämöinen *
+ * lassi.vaatamoinen@ixonos.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#include <QDebug>
+#include <QBoxLayout>
+#include <QLineEdit>
+#include <QLabel>
+#include <QPushButton>
+#include <QDialogButtonBox>
+#include <QAbstractButton>
+#include <QFileDialog>
+
+#include "PreferencesDialog.h"
+
+PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) :
+ QDialog(parent, f), // Superclass
+ dirLineEdit_(NULL),
+ dialogButtons_(NULL),
+ settings_()
+{
+ setWindowTitle("Preferences");
+
+ QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
+ QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight);
+ setLayout(verticalBox);
+ verticalBox->addLayout(horizontalBox1);
+
+ QLabel *dirLabel = new QLabel(tr("Download directory: "));
+ dirLineEdit_ = new QLineEdit(this);
+ QPushButton *browseDirButton = new QPushButton(tr("Browse.."));
+
+ horizontalBox1->addWidget(dirLabel);
+ horizontalBox1->addWidget(dirLineEdit_);
+ horizontalBox1->addWidget(browseDirButton);
+
+ connect(browseDirButton, SIGNAL(clicked()),
+ this, SLOT(on_browseDirButtonClicked()));
+
+
+ dialogButtons_ = new QDialogButtonBox(this);
+ dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
+ | QDialogButtonBox::Apply
+ | QDialogButtonBox::Cancel);
+
+ verticalBox->addWidget(dialogButtons_);
+
+ connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
+ this, SLOT(on_buttonClicked(QAbstractButton*)));
+
+ // Set saved preference values to fields.
+ ReadSettings();
+
+}
+
+
+PreferencesDialog::~PreferencesDialog()
+{
+}
+
+// ======================== SLOTS ========================
+void PreferencesDialog::on_browseDirButtonClicked()
+{
+ QFileDialog *dialog
+ = new QFileDialog(this, "Download directory",
+ QString(), tr("Torrent files (*.torrent)"));
+
+ dialog->setFileMode(QFileDialog::Directory);
+ dialog->setOption(QFileDialog::ShowDirsOnly, true);
+
+ connect(dialog, SIGNAL(fileSelected(const QString&)),
+ this, SLOT(on_downloadDirectorySelected(const QString&)));
+
+ dialog->show();
+}
+
+void PreferencesDialog::on_buttonClicked(QAbstractButton* button)
+{
+ switch (dialogButtons_->buttonRole ( button ) )
+ {
+ case QDialogButtonBox::AcceptRole :
+ qDebug() << "PreferencesDialog: OK";
+ WriteSettings();
+ close();
+ break;
+ case QDialogButtonBox::ApplyRole :
+ qDebug() << "PreferencesDialog: APPLY";
+ WriteSettings();
+ break;
+ case QDialogButtonBox::RejectRole :
+ qDebug() << "PreferencesDialog: CANCEL";
+ close();
+ break;
+ default:
+ return;
+ }
+}
+
+void PreferencesDialog::on_downloadDirectorySelected(const QString& directory)
+{
+ qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory;
+ // Torrent filename empty, do nothing.
+ if (directory == "")
+ return;
+
+ dirLineEdit_->insert(directory);
+
+ /// @todo check that user has privileges to write to this directory.
+}
+
+
+// ========================= Private functions ==========================
+void PreferencesDialog::WriteSettings()
+{
+ settings_.setValue("download/directory", dirLineEdit_->text());
+
+ // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
+ // settings are written also by QSettings() destructor and by event loop at regular interval.
+}
+
+void PreferencesDialog::ReadSettings()
+{
+ dirLineEdit_->insert(settings_.value("download/directory").toString());
+
+ // NOTE: We might need to call QSettigns::sync() here to instantly write settings.
+ // settings are written also by QSettings() destructor and by event loop at regular interval.
+}
+