Commit c57e2eaf authored by kh1's avatar kh1
Browse files

Implement ssh key generation dialog.

Reviewed-by: ck
parent cfe771cc
......@@ -38,6 +38,7 @@
#include "maemoconfigtestdialog.h"
#include "maemodeviceconfigurations.h"
#include "maemosshconfigdialog.h"
#include "maemosshthread.h"
#include <QtCore/QRegExp>
......@@ -118,7 +119,6 @@ MaemoSettingsWidget::MaemoSettingsWidget(QWidget *parent)
m_nameValidator(new NameValidator(m_devConfs)),
m_timeoutValidator(new TimeoutValidator),
m_keyDeployer(0)
{
initGui();
}
......@@ -160,6 +160,7 @@ void MaemoSettingsWidget::initGui()
m_ui->nameLineEdit->setValidator(m_nameValidator);
m_ui->timeoutLineEdit->setValidator(m_timeoutValidator);
m_ui->keyFileLineEdit->setExpectedKind(Utils::PathChooser::File);
foreach (const MaemoDeviceConfig &devConf, m_devConfs)
m_ui->configurationComboBox->addItem(devConf.name);
connect(m_ui->configurationComboBox, SIGNAL(currentIndexChanged(int)),
......@@ -350,27 +351,49 @@ void MaemoSettingsWidget::testConfig()
dialog->open();
}
void MaemoSettingsWidget::showGenerateSshKeyDialog()
{
MaemoSshConfigDialog dialog(this);
connect(&dialog, SIGNAL(publicKeyGenerated(QString)), this,
SLOT(setPublicKey(QString)));
connect(&dialog, SIGNAL(privateKeyGenerated(QString)), this,
SLOT(setPrivateKey(QString)));
dialog.exec();
}
void MaemoSettingsWidget::setPublicKey(const QString &path)
{
m_publicKey = path;
}
void MaemoSettingsWidget::setPrivateKey(const QString &path)
{
m_ui->keyFileLineEdit->setPath(path);
keyFileEditingFinished();
}
void MaemoSettingsWidget::deployKey()
{
if (m_keyDeployer)
return;
const QString &dir = QFileInfo(currentConfig().keyFile).path();
const QString &keyFile = QFileDialog::getOpenFileName(this,
tr("Choose public key file"), dir,
tr("Public Key Files(*.pub);;All Files (*)"));
if (keyFile.isEmpty())
if (!QFileInfo(m_publicKey).exists()) {
const QString &dir = QFileInfo(currentConfig().keyFile).path();
m_publicKey = QFileDialog::getOpenFileName(this,
tr("Choose public key file"), dir,
tr("Public Key Files(*.pub);;All Files (*)"));
}
if (m_publicKey.isEmpty())
return;
m_ui->deployKeyButton->disconnect();
SshDeploySpec deploySpec(keyFile, homeDirOnDevice(currentConfig().uname)
SshDeploySpec deploySpec(m_publicKey, homeDirOnDevice(currentConfig().uname)
+ QLatin1String("/.ssh/authorized_keys"), true);
m_keyDeployer = new MaemoSshDeployer(currentConfig(), QList<SshDeploySpec>() << deploySpec);
connect(m_keyDeployer, SIGNAL(finished()),
this, SLOT(handleDeployThreadFinished()));
connect(m_keyDeployer, SIGNAL(finished()), this, SLOT(handleDeployThreadFinished()));
m_ui->deployKeyButton->setText(tr("Stop deploying"));
connect(m_ui->deployKeyButton, SIGNAL(clicked()),
this, SLOT(stopDeploying()));
connect(m_ui->deployKeyButton, SIGNAL(clicked()), this, SLOT(stopDeploying()));
m_keyDeployer->start();
}
......@@ -410,11 +433,13 @@ void MaemoSettingsWidget::currentConfigChanged(int index)
if (index == -1) {
m_ui->removeConfigButton->setEnabled(false);
m_ui->testConfigButton->setEnabled(false);
m_ui->generateKeyButton->setEnabled(false);
clearDetails();
m_ui->detailsWidget->setEnabled(false);
} else {
m_ui->removeConfigButton->setEnabled(true);
m_ui->testConfigButton->setEnabled(true);
m_ui->generateKeyButton->setEnabled(true);
m_ui->configurationComboBox->setCurrentIndex(index);
display(currentConfig());
}
......
......@@ -82,6 +82,10 @@ private slots:
// For configuration testing.
void testConfig();
void showGenerateSshKeyDialog();
void setPublicKey(const QString &path);
void setPrivateKey(const QString &path);
// For key deploying.
void deployKey();
void handleDeployThreadFinished();
......@@ -104,6 +108,7 @@ private:
NameValidator * const m_nameValidator;
TimeoutValidator * const m_timeoutValidator;
MaemoSshDeployer *m_keyDeployer;
QString m_publicKey;
};
} // namespace Internal
......
......@@ -33,96 +33,105 @@
****************************************************************************/
#include "maemosshconfigdialog.h"
#include "maemosshthread.h"
#include <coreplugin/icore.h>
#include "maemodeviceconfigurations.h"
#include "ne7sshobject.h"
#include <ne7ssh.h>
#include <QtCore/QDir>
#include <QtNetwork/QHostInfo>
#include <QtGui/QApplication>
#include <QtGui/QDesktopServices>
#include <QtGui/QFileDialog>
using namespace Qt4ProjectManager::Internal;
MaemoSshConfigDialog::MaemoSshConfigDialog(QWidget *parent)
: QDialog(parent)
, m_keyDeployer(0)
, home(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
{
m_ui.setupUi(this);
const QLatin1String root("MaemoSsh/");
QSettings *settings = Core::ICore::instance()->settings();
m_ui.useKeyFromPath->setChecked(settings->value(root + QLatin1String("userKey"),
false).toBool());
m_ui.keyFileLineEdit->setExpectedKind(Utils::PathChooser::File);
m_ui.keyFileLineEdit->setPath(settings->value(root + QLatin1String("keyPath"),
QDir::toNativeSeparators(QDir::homePath() + QLatin1String("/.ssh/id_rsa.pub")))
.toString());
connect(m_ui.rsa, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
connect(m_ui.dsa, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
connect(m_ui.deployButton, SIGNAL(clicked()), this, SLOT(deployKey()));
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()));
}
MaemoSshConfigDialog::~MaemoSshConfigDialog()
{
}
void MaemoSshConfigDialog::slotToggled()
{
m_ui.comboBox->setCurrentIndex(0);
m_ui.comboBox->setEnabled(m_ui.rsa->isChecked());
}
void MaemoSshConfigDialog::generateSshKey()
{
if (!m_ui.keyFileLineEdit->isValid()) {
ne7ssh ssh;
ssh.generateKeyPair("rsa", "test", "id_rsa", "id_rsa.pub");
algorithm = m_ui.rsa->isChecked() ? "rsa" : "dsa";
tmpKey = QDir::tempPath().append(QLatin1Char('/') + algorithm).toUtf8();
QByteArray userId = QString(home.mid(home.lastIndexOf(QLatin1Char('/')) + 1)
+ QLatin1Char('@') + QHostInfo::localHostName()).toUtf8();
QFile::remove(tmpKey);
QFile::remove(tmpKey + ".pub");
QApplication::setOverrideCursor(Qt::BusyCursor);
QSharedPointer<ne7ssh> ssh = Ne7SshObject::instance()->get();
if (ssh->generateKeyPair(algorithm, userId, tmpKey, tmpKey + ".pub",
m_ui.comboBox->currentText().toUShort())) {
QFile file(tmpKey + ".pub");
if (file.open(QIODevice::ReadOnly))
m_ui.plainTextEdit->setPlainText(file.readAll());
m_ui.savePublicKey->setEnabled(true);
m_ui.savePrivateKey->setEnabled(true);
} else {
m_ui.infoLabel->setText("An public key has been created already.");
m_ui.plainTextEdit->setPlainText(tr("Could not create SSH key pair."));
}
QApplication::restoreOverrideCursor();
}
void MaemoSshConfigDialog::deployKey()
void MaemoSshConfigDialog::savePublicKey()
{
if (m_keyDeployer)
return;
if (m_ui.keyFileLineEdit->validatePath(m_ui.keyFileLineEdit->path())) {
m_ui.deployButton->disconnect();
//SshDeploySpec deploySpec(keyFile, homeDirOnDevice(currentConfig().uname)
// + QLatin1String("/.ssh/authorized_keys"), true);
//m_keyDeployer = new MaemoSshDeployer(currentConfig(), QList<SshDeploySpec>()
// << deploySpec);
//connect(m_keyDeployer, SIGNAL(finished()), this,
// SLOT(handleDeployThreadFinished()));
if (m_keyDeployer) {
m_keyDeployer->start();
m_ui.deployButton->setText(tr("Stop deploying"));
connect(m_ui.deployButton, SIGNAL(clicked()), this, SLOT(stopDeploying()));
}
} else {
m_ui.infoLabel->setText("The public key path is invalid.");
}
checkSshDir();
copyFile(QFileDialog::getSaveFileName(this, tr("Choose folder to save "
"public key file"), home + QString::fromLatin1("/.ssh/id_%1.pub")
.arg(algorithm.constData())), true);
}
void MaemoSshConfigDialog::handleDeployThreadFinished()
void MaemoSshConfigDialog::savePrivateKey()
{
if (!m_keyDeployer)
return;
checkSshDir();
copyFile(QFileDialog::getSaveFileName(this, tr("Choose folder to save "
"private key file"), home + QString::fromLatin1("/.ssh/id_%1")
.arg(algorithm.constData())), false);
}
if (m_keyDeployer->hasError()) {
m_ui.infoLabel->setText(tr("Key deployment failed: %1")
.arg(m_keyDeployer->error()));
} else {
m_ui.infoLabel->setText(tr("Key was successfully deployed."));
}
stopDeploying();
void MaemoSshConfigDialog::checkSshDir()
{
QDir dir(home + QString::fromLatin1("/.ssh"));
if (!dir.exists())
dir.mkpath(home + QString::fromLatin1("/.ssh"));
}
void MaemoSshConfigDialog::stopDeploying()
void MaemoSshConfigDialog::copyFile(const QString &file, bool pubKey)
{
if (m_keyDeployer) {
m_ui.deployButton->disconnect();
const bool buttonWasEnabled = m_ui.deployButton->isEnabled();
m_keyDeployer->disconnect();
m_keyDeployer->stop();
delete m_keyDeployer;
m_keyDeployer = 0;
m_ui.deployButton->setText(tr("Deploy Public Key"));
connect(m_ui.deployButton, SIGNAL(clicked()), this, SLOT(deployKey()));
m_ui.deployButton->setEnabled(buttonWasEnabled);
if (!file.isEmpty()) {
if (!QFile::exists(file) || QFile::remove(file)) {
QFile(tmpKey + (pubKey ? ".pub" : "")).copy(file);
if (pubKey)
emit publicKeyGenerated(file);
else
emit privateKeyGenerated(file);
}
}
}
......@@ -42,8 +42,6 @@
namespace Qt4ProjectManager {
namespace Internal {
class MaemoSshDeployer;
class MaemoSshConfigDialog : public QDialog
{
Q_OBJECT
......@@ -51,15 +49,25 @@ public:
MaemoSshConfigDialog(QWidget *parent = 0);
~MaemoSshConfigDialog();
signals:
void publicKeyGenerated(const QString &path);
void privateKeyGenerated(const QString &path);
private slots:
void slotToggled();
void generateSshKey();
void deployKey();
void handleDeployThreadFinished();
void stopDeploying();
void savePublicKey();
void savePrivateKey();
private:
void checkSshDir();
void copyFile(const QString &file, bool pubKey);
private:
QString home;
QByteArray tmpKey;
QByteArray algorithm;
Ui::MaemoSshConfigDialog m_ui;
MaemoSshDeployer *m_keyDeployer;
};
} // Qt4ProjectManager
......
......@@ -9,117 +9,218 @@
<rect>
<x>0</x>
<y>0</y>
<width>387</width>
<height>128</height>
<width>500</width>
<height>275</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<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">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="useKeyFromPath">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Use key from location:</string>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Options</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<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 size:</string>
</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">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QRadioButton" name="rsa">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>RSA</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="dsa">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>DSA</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="keyLabel">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Private key file:</string>
</property>
</widget>
</item>
<item>
<widget class="Utils::PathChooser" name="keyFileLineEdit" native="true">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="infoLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Key</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<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>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="generateButton">
<property name="text">
<string>Generate SSH Key</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>