1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
9 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
14 ** Please review the following information to ensure the GNU Lesser General
15 ** Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
24 ** Alternatively, this file may be used in accordance with the terms and
25 ** conditions contained in a signed written agreement between you and Nokia.
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
30 **************************************************************************/
32 #include "maemokeydeployer.h"
34 #include <utils/ssh/sshremoteprocessrunner.h>
36 #include <QtCore/QFile>
38 using namespace Utils;
40 namespace Qt4ProjectManager {
43 MaemoKeyDeployer::MaemoKeyDeployer(QObject *parent)
48 MaemoKeyDeployer::~MaemoKeyDeployer()
53 void MaemoKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
54 const QString &keyFilePath)
57 m_deployProcess = SshRemoteProcessRunner::create(sshParams);
59 QFile keyFile(keyFilePath);
61 const bool keyFileAccessible = keyFile.open(QIODevice::ReadOnly);
62 if (keyFileAccessible)
63 key = keyFile.readAll();
64 if (!keyFileAccessible || keyFile.error() != QFile::NoError) {
65 emit error(tr("Could not read public key file '%1'.").arg(keyFilePath));
69 connect(m_deployProcess.data(), SIGNAL(connectionError(Utils::SshError)), this,
70 SLOT(handleConnectionFailure()));
71 connect(m_deployProcess.data(), SIGNAL(processClosed(int)), this,
72 SLOT(handleKeyUploadFinished(int)));
73 const QByteArray command = "test -d .ssh "
74 "|| mkdir .ssh && chmod 0700 .ssh && echo '"
75 + key + "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys";
76 m_deployProcess->run(command);
79 void MaemoKeyDeployer::handleConnectionFailure()
83 const QString errorMsg = m_deployProcess->connection()->errorString();
85 emit error(tr("Connection failed: %1").arg(errorMsg));
88 void MaemoKeyDeployer::handleKeyUploadFinished(int exitStatus)
90 Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
91 || exitStatus == SshRemoteProcess::KilledBySignal
92 || exitStatus == SshRemoteProcess::ExitedNormally);
97 const int exitCode = m_deployProcess->process()->exitCode();
98 const QString errorMsg = m_deployProcess->process()->errorString();
100 if (exitStatus == SshRemoteProcess::ExitedNormally && exitCode == 0)
101 emit finishedSuccessfully();
103 emit error(tr("Key deployment failed: %1.").arg(errorMsg));
106 void MaemoKeyDeployer::stopDeployment()
111 void MaemoKeyDeployer::cleanup()
113 if (m_deployProcess) {
114 disconnect(m_deployProcess.data(), 0, this, 0);
115 m_deployProcess.clear();
120 } // namespace Internal
121 } // namespace Qt4ProjectManager