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

Maemo: Introduce dedicated class for key deploying.

We want to re-use that functionality in our wizard.
parent 011f2f98
......@@ -38,13 +38,13 @@
#include "maemoconfigtestdialog.h"
#include "maemodeviceconfigurations.h"
#include "maemokeydeployer.h"
#include "maemoremoteprocessesdialog.h"
#include "maemosshconfigdialog.h"
#include <coreplugin/icore.h>
#include <coreplugin/ssh/sshremoteprocessrunner.h>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QRegExp>
#include <QtCore/QSettings>
......@@ -102,9 +102,14 @@ MaemoDeviceConfigurationsSettingsWidget::MaemoDeviceConfigurationsSettingsWidget
m_ui(new Ui_MaemoDeviceConfigurationsSettingsWidget),
m_devConfigs(MaemoDeviceConfigurations::cloneInstance()),
m_nameValidator(new NameValidator(m_devConfigs.data(), this)),
m_keyDeployer(new MaemoKeyDeployer(this)),
m_saveSettingsRequested(false)
{
initGui();
connect(m_keyDeployer, SIGNAL(error(QString)), this,
SLOT(handleDeploymentError(QString)), Qt::QueuedConnection);
connect(m_keyDeployer, SIGNAL(finishedSuccessfully()),
SLOT(handleDeploymentSuccess()));
}
MaemoDeviceConfigurationsSettingsWidget::~MaemoDeviceConfigurationsSettingsWidget()
......@@ -374,9 +379,6 @@ void MaemoDeviceConfigurationsSettingsWidget::setPrivateKey(const QString &path)
void MaemoDeviceConfigurationsSettingsWidget::deployKey()
{
if (m_keyDeployer)
return;
const SshConnectionParameters sshParams = currentConfig()->sshParameters();
const QString &dir = QFileInfo(sshParams.privateKeyFile).path();
QString publicKeyFileName = QFileDialog::getOpenFileName(this,
......@@ -385,71 +387,29 @@ void MaemoDeviceConfigurationsSettingsWidget::deployKey()
if (publicKeyFileName.isEmpty())
return;
QFile keyFile(publicKeyFileName);
QByteArray key;
const bool keyFileAccessible = keyFile.open(QIODevice::ReadOnly);
if (keyFileAccessible)
key = keyFile.readAll();
if (!keyFileAccessible || keyFile.error() != QFile::NoError) {
QMessageBox::critical(this, tr("Deployment Failed"),
tr("Could not read public key file '%1'.").arg(publicKeyFileName));
return;
}
disconnect(m_ui->deployKeyButton, 0, this, 0);
m_ui->deployKeyButton->setText(tr("Stop Deploying"));
connect(m_ui->deployKeyButton, SIGNAL(clicked()), this,
SLOT(stopDeploying()));
m_keyDeployer = SshRemoteProcessRunner::create(sshParams);
connect(m_keyDeployer.data(), SIGNAL(connectionError(Core::SshError)), this,
SLOT(handleConnectionFailure()));
connect(m_keyDeployer.data(), SIGNAL(processClosed(int)), this,
SLOT(handleKeyUploadFinished(int)));
const QByteArray command = "test -d .ssh "
"|| mkdir .ssh && chmod 0700 .ssh && echo '"
+ key + "' >> .ssh/authorized_keys && chmod 0700 .ssh/authorized_keys";
m_keyDeployer->run(command);
SLOT(finishDeployment()));
m_keyDeployer->deployPublicKey(sshParams, publicKeyFileName);
}
void MaemoDeviceConfigurationsSettingsWidget::handleConnectionFailure()
void MaemoDeviceConfigurationsSettingsWidget::handleDeploymentError(const QString &errorMsg)
{
if (!m_keyDeployer)
return;
QMessageBox::critical(this, tr("Deployment Failed"),
tr("Could not connect to host: %1")
.arg(m_keyDeployer->connection()->errorString()));
stopDeploying();
QMessageBox::critical(this, tr("Deployment Failed"), errorMsg);
finishDeployment();
}
void MaemoDeviceConfigurationsSettingsWidget::handleKeyUploadFinished(int exitStatus)
void MaemoDeviceConfigurationsSettingsWidget::handleDeploymentSuccess()
{
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
if (!m_keyDeployer)
return;
if (exitStatus == SshRemoteProcess::ExitedNormally
&& m_keyDeployer->process()->exitCode() == 0) {
QMessageBox::information(this, tr("Deployment Succeeded"),
tr("Key was successfully deployed."));
} else {
QMessageBox::critical(this, tr("Deployment Failed"),
tr("Key deployment failed: %1.")
.arg(m_keyDeployer->process()->errorString()));
}
stopDeploying();
QMessageBox::information(this, tr("Deployment Succeeded"),
tr("Key was successfully deployed."));
finishDeployment();
}
void MaemoDeviceConfigurationsSettingsWidget::stopDeploying()
void MaemoDeviceConfigurationsSettingsWidget::finishDeployment()
{
if (m_keyDeployer) {
disconnect(m_keyDeployer.data(), 0, this, 0);
m_keyDeployer = SshRemoteProcessRunner::Ptr();
}
m_keyDeployer->stopDeployment();
m_ui->deployKeyButton->disconnect();
m_ui->deployKeyButton->setText(tr("Deploy Public Key ..."));
connect(m_ui->deployKeyButton, SIGNAL(clicked()), this, SLOT(deployKey()));
......@@ -457,7 +417,7 @@ void MaemoDeviceConfigurationsSettingsWidget::stopDeploying()
void MaemoDeviceConfigurationsSettingsWidget::currentConfigChanged(int index)
{
stopDeploying();
finishDeployment();
if (index == -1) {
m_ui->removeConfigButton->setEnabled(false);
m_ui->testConfigButton->setEnabled(false);
......
......@@ -37,7 +37,6 @@
#include <QtCore/QList>
#include <QtCore/QScopedPointer>
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
#include <QtGui/QWidget>
......@@ -57,6 +56,7 @@ namespace Internal {
class NameValidator;
class MaemoDeviceConfig;
class MaemoDeviceConfigurations;
class MaemoKeyDeployer;
class MaemoDeviceConfigurationsSettingsWidget : public QWidget
{
......@@ -95,9 +95,9 @@ private slots:
// For key deploying.
void deployKey();
void stopDeploying();
void handleConnectionFailure();
void handleKeyUploadFinished(int exitStatus);
void finishDeployment();
void handleDeploymentError(const QString &errorMsg);
void handleDeploymentSuccess();
private:
void initGui();
......@@ -110,9 +110,9 @@ private:
void updatePortsWarningLabel();
Ui_MaemoDeviceConfigurationsSettingsWidget *m_ui;
QSharedPointer<Core::SshRemoteProcessRunner> m_keyDeployer;
const QScopedPointer<MaemoDeviceConfigurations> m_devConfigs;
NameValidator * const m_nameValidator;
MaemoKeyDeployer *const m_keyDeployer;
bool m_saveSettingsRequested;
};
......
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "maemodeviceconfigwizard.h"
#include "ui_maemodeviceconfigwizardkeycreationpage.h"
#include "ui_maemodeviceconfigwizardpreviouskeysetupcheckpage.h"
......
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAEMODEVICECONFIGWIZARD_H
#define MAEMODEVICECONFIGWIZARD_H
......
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "maemokeydeployer.h"
#include <coreplugin/ssh/sshremoteprocessrunner.h>
#include <QtCore/QFile>
using namespace Core;
namespace Qt4ProjectManager {
namespace Internal {
MaemoKeyDeployer::MaemoKeyDeployer(QObject *parent)
: QObject(parent)
{
}
MaemoKeyDeployer::~MaemoKeyDeployer()
{
cleanup();
}
void MaemoKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
const QString &keyFilePath)
{
cleanup();
m_deployProcess = SshRemoteProcessRunner::create(sshParams);
QFile keyFile(keyFilePath);
QByteArray key;
const bool keyFileAccessible = keyFile.open(QIODevice::ReadOnly);
if (keyFileAccessible)
key = keyFile.readAll();
if (!keyFileAccessible || keyFile.error() != QFile::NoError) {
emit error(tr("Could not read public key file '%1'.").arg(keyFilePath));
return;
}
connect(m_deployProcess.data(), SIGNAL(connectionError(Core::SshError)), this,
SLOT(handleConnectionFailure()));
connect(m_deployProcess.data(), SIGNAL(processClosed(int)), this,
SLOT(handleKeyUploadFinished(int)));
const QByteArray command = "test -d .ssh "
"|| mkdir .ssh && chmod 0700 .ssh && echo '"
+ key + "' >> .ssh/authorized_keys && chmod 0700 .ssh/authorized_keys";
m_deployProcess->run(command);
}
void MaemoKeyDeployer::handleConnectionFailure()
{
if (!m_deployProcess)
return;
const QString errorMsg = m_deployProcess->connection()->errorString();
cleanup();
emit error(tr("Connection failed: %1").arg(errorMsg));
}
void MaemoKeyDeployer::handleKeyUploadFinished(int exitStatus)
{
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
if (!m_deployProcess)
return;
const int exitCode = m_deployProcess->process()->exitCode();
const QString errorMsg = m_deployProcess->process()->errorString();
cleanup();
if (exitStatus == SshRemoteProcess::ExitedNormally && exitCode == 0)
emit finishedSuccessfully();
else
emit error(tr("Key deployment failed: %1.").arg(errorMsg));
}
void MaemoKeyDeployer::stopDeployment()
{
cleanup();
}
void MaemoKeyDeployer::cleanup()
{
if (m_deployProcess) {
disconnect(m_deployProcess.data(), 0, this, 0);
m_deployProcess.clear();
}
}
} // namespace Internal
} // namespace Qt4ProjectManager
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAEMOKEYDEPLOYER_H
#define MAEMOKEYDEPLOYER_H
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
namespace Core {
class SshConnectionParameters;
class SshRemoteProcessRunner;
}
namespace Qt4ProjectManager {
namespace Internal {
class MaemoKeyDeployer : public QObject
{
Q_OBJECT
public:
explicit MaemoKeyDeployer(QObject *parent = 0);
~MaemoKeyDeployer();
void deployPublicKey(const Core::SshConnectionParameters &sshParams,
const QString &keyFilePath);
void stopDeployment();
signals:
void error(const QString &errorMsg);
void finishedSuccessfully();
private slots:
void handleConnectionFailure();
void handleKeyUploadFinished(int exitStatus);
private:
void cleanup();
QSharedPointer<Core::SshRemoteProcessRunner> m_deployProcess;
};
} // namespace Internal
} // namespace Qt4ProjectManager
#endif // MAEMOKEYDEPLOYER_H
......@@ -47,7 +47,8 @@ HEADERS += \
$$PWD/qt4maemotargetfactory.h \
$$PWD/qt4maemotarget.h \
$$PWD/qt4maemodeployconfiguration.h \
$$PWD/maemodeviceconfigwizard.h
$$PWD/maemodeviceconfigwizard.h \
$$PWD/maemokeydeployer.h
SOURCES += \
$$PWD/maemoconfigtestdialog.cpp \
......@@ -95,7 +96,8 @@ SOURCES += \
$$PWD/qt4maemotargetfactory.cpp \
$$PWD/qt4maemotarget.cpp \
$$PWD/qt4maemodeployconfiguration.cpp \
$$PWD/maemodeviceconfigwizard.cpp
$$PWD/maemodeviceconfigwizard.cpp \
$$PWD/maemokeydeployer.cpp
FORMS += \
$$PWD/maemoconfigtestdialog.ui \
......
Supports Markdown
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