2012-01-13 11:52:37 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* 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 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ChangeMasterKeyWidget.h"
|
|
|
|
#include "ui_ChangeMasterKeyWidget.h"
|
|
|
|
|
2012-04-11 07:53:46 -04:00
|
|
|
#include <QtGui/QMessageBox>
|
|
|
|
|
2012-01-13 11:52:37 -05:00
|
|
|
#include "keys/FileKey.h"
|
|
|
|
#include "keys/PasswordKey.h"
|
2012-06-10 12:26:35 -04:00
|
|
|
#include "gui/FileDialog.h"
|
2012-01-13 11:52:37 -05:00
|
|
|
|
|
|
|
ChangeMasterKeyWidget::ChangeMasterKeyWidget(QWidget* parent)
|
2012-04-24 07:23:09 -04:00
|
|
|
: DialogyWidget(parent)
|
2012-01-13 11:52:37 -05:00
|
|
|
, m_ui(new Ui::ChangeMasterKeyWidget())
|
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(generateKey()));
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
|
2012-04-19 13:32:01 -04:00
|
|
|
connect(m_ui->togglePasswordButton, SIGNAL(toggled(bool)), SLOT(togglePassword(bool)));
|
2012-06-10 12:26:35 -04:00
|
|
|
connect(m_ui->createKeyFileButton, SIGNAL(clicked()), SLOT(createKeyFile()));
|
2012-06-10 12:40:05 -04:00
|
|
|
connect(m_ui->browseKeyFileButton, SIGNAL(clicked()), SLOT(browseKeyFile()));
|
2012-01-13 11:52:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ChangeMasterKeyWidget::~ChangeMasterKeyWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-19 13:32:01 -04:00
|
|
|
void ChangeMasterKeyWidget::togglePassword(bool checked)
|
|
|
|
{
|
|
|
|
m_ui->enterPasswordEdit->setEchoMode(checked ? QLineEdit::Password : QLineEdit::Normal);
|
|
|
|
m_ui->repeatPasswordEdit->setEchoMode(checked ? QLineEdit::Password : QLineEdit::Normal);
|
|
|
|
}
|
|
|
|
|
2012-06-10 12:26:35 -04:00
|
|
|
void ChangeMasterKeyWidget::createKeyFile()
|
|
|
|
{
|
2012-06-11 12:39:16 -04:00
|
|
|
QString filters = QString("%1 (*.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
|
|
|
QString fileName = fileDialog()->getSaveFileName(this, tr("Create Key File..."), QString(), filters);
|
2012-06-10 12:26:35 -04:00
|
|
|
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
QString errorMsg;
|
|
|
|
bool created = FileKey::create(fileName, &errorMsg);
|
2012-06-11 12:39:16 -04:00
|
|
|
if (!created) {
|
2012-06-10 12:26:35 -04:00
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Unable to create Key File : ") + errorMsg);
|
|
|
|
}
|
2012-06-11 12:39:16 -04:00
|
|
|
else {
|
|
|
|
m_ui->keyFileCombo->setEditText(fileName);
|
2012-06-10 12:26:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 12:40:05 -04:00
|
|
|
void ChangeMasterKeyWidget::browseKeyFile()
|
|
|
|
{
|
2012-06-11 12:39:16 -04:00
|
|
|
QString filters = QString("%1 (*.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);
|
2012-06-10 12:40:05 -04:00
|
|
|
|
2012-06-11 12:39:16 -04:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
m_ui->keyFileCombo->setEditText(fileName);
|
2012-06-10 12:40:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 11:52:37 -05:00
|
|
|
void ChangeMasterKeyWidget::clearForms()
|
|
|
|
{
|
|
|
|
m_key.clear();
|
|
|
|
|
|
|
|
m_ui->passwordGroup->setChecked(true);
|
|
|
|
m_ui->enterPasswordEdit->setText("");
|
|
|
|
m_ui->repeatPasswordEdit->setText("");
|
|
|
|
m_ui->keyFileGroup->setChecked(false);
|
2012-04-19 13:32:01 -04:00
|
|
|
m_ui->togglePasswordButton->setChecked(true);
|
2012-05-11 06:39:06 -04:00
|
|
|
// TODO: clear m_ui->keyFileCombo
|
2012-04-22 18:18:10 -04:00
|
|
|
|
|
|
|
m_ui->enterPasswordEdit->setFocus();
|
2012-01-13 11:52:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CompositeKey ChangeMasterKeyWidget::newMasterKey()
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLabel* ChangeMasterKeyWidget::headlineLabel()
|
|
|
|
{
|
|
|
|
return m_ui->headlineLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::generateKey()
|
|
|
|
{
|
|
|
|
m_key.clear();
|
|
|
|
|
|
|
|
if (m_ui->passwordGroup->isChecked()) {
|
2012-04-11 07:53:46 -04:00
|
|
|
if (m_ui->enterPasswordEdit->text() == m_ui->repeatPasswordEdit->text()) {
|
|
|
|
if (m_ui->enterPasswordEdit->text().isEmpty()) {
|
2012-04-18 16:08:22 -04:00
|
|
|
if (QMessageBox::question(this, tr("Question"),
|
|
|
|
tr("Do you really want to use an empty string as password?"),
|
|
|
|
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
|
2012-04-11 07:53:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_key.addKey(PasswordKey(m_ui->enterPasswordEdit->text()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Different passwords supplied."));
|
|
|
|
m_ui->enterPasswordEdit->setText("");
|
|
|
|
m_ui->repeatPasswordEdit->setText("");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-13 11:52:37 -05:00
|
|
|
}
|
|
|
|
if (m_ui->keyFileGroup->isChecked()) {
|
|
|
|
FileKey fileKey;
|
|
|
|
QString errorMsg;
|
2012-04-11 17:40:53 -04:00
|
|
|
if (!fileKey.load(m_ui->keyFileCombo->currentText(), &errorMsg)) {
|
2012-05-11 06:39:06 -04:00
|
|
|
// TODO: error handling
|
2012-01-13 11:52:37 -05:00
|
|
|
}
|
|
|
|
m_key.addKey(fileKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT editFinished(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::reject()
|
|
|
|
{
|
|
|
|
Q_EMIT editFinished(false);
|
|
|
|
}
|