Commit cb8ec57c authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Rework SSH key generation dialog.



- Simplify user interaction.
- Clean up implementation.

Change-Id: I13c98f38a7859f9cff4b1538338ac76335052886
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@nokia.com>
parent 1a0c0a82
......@@ -29,6 +29,7 @@
**
**************************************************************************/
#include "sshkeycreationdialog.h"
#include "ui_sshkeycreationdialog.h"
#include "linuxdeviceconfiguration.h"
......@@ -36,97 +37,91 @@
#include <utils/fileutils.h>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtGui/QApplication>
#include <QtGui/QDesktopServices>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtNetwork/QHostInfo>
using namespace Utils;
using namespace RemoteLinux::Internal;
SshKeyCreationDialog::SshKeyCreationDialog(QWidget *parent)
: QDialog(parent)
, home(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
, m_keyGenerator(new SshKeyGenerator)
: QDialog(parent), m_keyGenerator(0), m_ui(new Ui::SshKeyCreationDialog)
{
m_ui.setupUi(this);
connect(m_ui.rsa, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
connect(m_ui.dsa, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
connect(m_ui.generateButton, SIGNAL(clicked()), this, SLOT(generateSshKey()));
connect(m_ui.savePublicKey, SIGNAL(clicked()), this, SLOT(savePublicKey()));
connect(m_ui.savePrivateKey, SIGNAL(clicked()), this, SLOT(savePrivateKey()));
m_ui->setupUi(this);
m_ui->privateKeyFilePathChooser->setExpectedKind(PathChooser::File);
const QString defaultPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation)
+ QLatin1String("/.ssh/qtc_id");
m_ui->privateKeyFilePathChooser->setPath(defaultPath);
filePathChanged();
connect(m_ui->rsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged()));
connect(m_ui->dsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged()));
connect(m_ui->privateKeyFilePathChooser, SIGNAL(changed(QString)), SLOT(filePathChanged()));
connect(m_ui->generateButton, SIGNAL(clicked()), this, SLOT(generateKeys()));
}
SshKeyCreationDialog::~SshKeyCreationDialog()
{
delete m_keyGenerator;
}
void SshKeyCreationDialog::slotToggled()
void SshKeyCreationDialog::keyTypeChanged()
{
m_ui.comboBox->setCurrentIndex(0);
m_ui.comboBox->setEnabled(m_ui.rsa->isChecked());
m_ui->comboBox->setCurrentIndex(0);
m_ui->comboBox->setEnabled(m_ui->rsa->isChecked());
}
void SshKeyCreationDialog::generateSshKey()
void SshKeyCreationDialog::generateKeys()
{
const SshKeyGenerator::KeyType keyType = m_ui.rsa->isChecked()
const SshKeyGenerator::KeyType keyType = m_ui->rsa->isChecked()
? SshKeyGenerator::Rsa
: SshKeyGenerator::Dsa;
if (!m_keyGenerator)
m_keyGenerator = new SshKeyGenerator;
QApplication::setOverrideCursor(Qt::BusyCursor);
const bool success = m_keyGenerator->generateKeys(keyType, SshKeyGenerator::Mixed,
m_ui->comboBox->currentText().toUShort());
QApplication::restoreOverrideCursor();
if (m_keyGenerator->generateKeys(keyType, SshKeyGenerator::Mixed,
m_ui.comboBox->currentText().toUShort())) {
m_ui.plainTextEdit->setPlainText(m_keyGenerator->publicKey());
m_ui.savePublicKey->setEnabled(true);
m_ui.savePrivateKey->setEnabled(true);
if (success) {
saveKeys();
} else {
m_ui.plainTextEdit->setPlainText(m_keyGenerator->error());
QMessageBox::critical(this, tr("Key Generation Failed"), m_keyGenerator->error());
}
QApplication::restoreOverrideCursor();
}
void SshKeyCreationDialog::savePublicKey()
void SshKeyCreationDialog::filePathChanged()
{
saveKey(true);
m_ui->generateButton->setEnabled(!privateKeyFilePath().isEmpty());
m_ui->publicKeyFileLabel->setText(privateKeyFilePath() + QLatin1String(".pub"));
}
void SshKeyCreationDialog::savePrivateKey()
void SshKeyCreationDialog::saveKeys()
{
saveKey(false);
}
const QString parentDir = QFileInfo(privateKeyFilePath()).dir().path();
if (!QDir::root().mkpath(parentDir)) {
QMessageBox::critical(this, tr("Failure To Save Key File"),
tr("Failed to create directory: '%1'.").arg(parentDir));
return;
}
void SshKeyCreationDialog::checkSshDir()
{
QDir dir(home + QString::fromLatin1("/.ssh"));
if (!dir.exists())
dir.mkpath(home + QString::fromLatin1("/.ssh"));
FileSaver privSaver(privateKeyFilePath());
privSaver.write(m_keyGenerator->privateKey());
if (!privSaver.finalize(this))
return;
QFile::setPermissions(privateKeyFilePath(), QFile::ReadOwner | QFile::WriteOwner);
FileSaver pubSaver(m_ui->publicKeyFileLabel->text());
pubSaver.write(m_keyGenerator->publicKey());
if (pubSaver.finalize(this))
accept();
}
void SshKeyCreationDialog::saveKey(bool publicKey)
QString SshKeyCreationDialog::privateKeyFilePath() const
{
checkSshDir();
const QString suggestedTypeSuffix =
m_keyGenerator->type() == SshKeyGenerator::Rsa ? "rsa" : "dsa";
const QString suggestedName = home + QString::fromLatin1("/.ssh/id_%1%2")
.arg(suggestedTypeSuffix).arg(publicKey ? ".pub" : "");
const QString dlgTitle
= publicKey ? tr("Save Public Key File") : tr("Save Private Key File");
const QString fileName
= QFileDialog::getSaveFileName(this, dlgTitle, suggestedName);
if (fileName.isEmpty())
return;
Utils::FileSaver saver(fileName);
saver.write(publicKey
? m_keyGenerator->publicKey()
: m_keyGenerator->privateKey());
if (saver.finalize(this) && !publicKey)
emit privateKeyGenerated(fileName);
if (!publicKey)
QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
return m_ui->privateKeyFilePathChooser->path();
}
......@@ -32,9 +32,6 @@
#ifndef SSHKEYCREATIONDIALOG_H
#define SSHKEYCREATIONDIALOG_H
#include "ui_sshkeycreationdialog.h"
#include <QtCore/QScopedPointer>
#include <QtGui/QDialog>
namespace Utils {
......@@ -43,6 +40,9 @@ class SshKeyGenerator;
namespace RemoteLinux {
namespace Internal {
namespace Ui {
class SshKeyCreationDialog;
}
class SshKeyCreationDialog : public QDialog
{
......@@ -51,23 +51,18 @@ public:
SshKeyCreationDialog(QWidget *parent = 0);
~SshKeyCreationDialog();
signals:
void privateKeyGenerated(const QString &path);
private slots:
void slotToggled();
void generateSshKey();
void savePublicKey();
void savePrivateKey();
void keyTypeChanged();
void generateKeys();
void filePathChanged();
private:
void checkSshDir();
void saveKey(bool publicKey);
void saveKeys();
QString privateKeyFilePath() const;
private:
QString home;
QScopedPointer<Utils::SshKeyGenerator> m_keyGenerator;
Ui::SshKeyCreationDialog m_ui;
Utils::SshKeyGenerator *m_keyGenerator;
Ui::SshKeyCreationDialog *m_ui;
};
} // namespace Internal
......
......@@ -9,8 +9,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>275</height>
<width>295</width>
<height>223</height>
</rect>
</property>
<property name="sizePolicy">
......@@ -19,78 +19,24 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>500</width>
<height>275</height>
</size>
</property>
<property name="windowTitle">
<string>SSH Key Configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Options</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="keySize">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Key &amp;size:</string>
</property>
<property name="buddy">
<cstring>comboBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox">
<item>
<property name="text">
<string notr="true">1024</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">2048</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">4096</string>
</property>
</item>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>328</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="keyAlgo">
<property name="text">
<string>Key algorithm:</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QRadioButton" name="rsa">
......@@ -136,54 +82,45 @@
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Key</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<item row="2" column="0">
<widget class="QLabel" name="keySize">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string notr="true"/>
</property>
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="plainText">
<string/>
<property name="text">
<string>Key &amp;size:</string>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
<property name="buddy">
<cstring>comboBox</cstring>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="generateButton">
<property name="text">
<string>&amp;Generate SSH Key</string>
</property>
<widget class="QComboBox" name="comboBox">
<item>
<property name="text">
<string notr="true">1024</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">2048</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">4096</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
......@@ -195,60 +132,103 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="savePublicKey">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Save P&amp;ublic Key...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="savePrivateKey">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Save Pr&amp;ivate Key...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<widget class="QLabel" name="privateKeyFileLabel">
<property name="text">
<string>Private key file:</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Public key file:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="Utils::PathChooser" name="privateKeyFilePathChooser" native="true"/>
</item>
<item row="8" column="1">
<widget class="QLabel" name="publicKeyFileLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>6</number>
</property>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QPushButton" name="generateButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<property name="text">
<string>&amp;Generate And Save Key Pair</string>
</property>
</spacer>
</widget>
</item>
<item>
<widget class="QPushButton" name="closeButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&amp;Close</string>
<string>&amp;Cancel</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>closeButton</tabstop>
</tabstops>
<customwidgets>
<customwidget>
<class>Utils::PathChooser</class>
<extends>QWidget</extends>
<header location="global">utils/pathchooser.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
......@@ -258,8 +238,8 @@
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>351</x>
<y>96</y>
<x>195</x>
<y>184</y>
</hint>
<hint type="destinationlabel">
<x>381</x>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment