OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-maemo / maemokeydeployer.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 ** GNU Lesser General Public License Usage
10 **
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.
17 **
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.
21 **
22 ** Other Usage
23 **
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.
26 **
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
29 **
30 **************************************************************************/
31
32 #include "maemokeydeployer.h"
33
34 #include <utils/ssh/sshremoteprocessrunner.h>
35
36 #include <QtCore/QFile>
37
38 using namespace Utils;
39
40 namespace Qt4ProjectManager {
41 namespace Internal {
42
43 MaemoKeyDeployer::MaemoKeyDeployer(QObject *parent)
44     : QObject(parent)
45 {
46 }
47
48 MaemoKeyDeployer::~MaemoKeyDeployer()
49 {
50     cleanup();
51 }
52
53 void MaemoKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
54     const QString &keyFilePath)
55 {
56     cleanup();
57     m_deployProcess = SshRemoteProcessRunner::create(sshParams);
58
59     QFile keyFile(keyFilePath);
60     QByteArray key;
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));
66         return;
67     }
68
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);
77 }
78
79 void MaemoKeyDeployer::handleConnectionFailure()
80 {
81     if (!m_deployProcess)
82         return;
83     const QString errorMsg = m_deployProcess->connection()->errorString();
84     cleanup();
85     emit error(tr("Connection failed: %1").arg(errorMsg));
86 }
87
88 void MaemoKeyDeployer::handleKeyUploadFinished(int exitStatus)
89 {
90     Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
91         || exitStatus == SshRemoteProcess::KilledBySignal
92         || exitStatus == SshRemoteProcess::ExitedNormally);
93
94     if (!m_deployProcess)
95         return;
96
97     const int exitCode = m_deployProcess->process()->exitCode();
98     const QString errorMsg = m_deployProcess->process()->errorString();
99     cleanup();
100     if (exitStatus == SshRemoteProcess::ExitedNormally && exitCode == 0)
101         emit finishedSuccessfully();
102     else
103         emit error(tr("Key deployment failed: %1.").arg(errorMsg));
104 }
105
106 void MaemoKeyDeployer::stopDeployment()
107 {
108     cleanup();
109 }
110
111 void MaemoKeyDeployer::cleanup()
112 {
113     if (m_deployProcess) {
114         disconnect(m_deployProcess.data(), 0, this, 0);
115         m_deployProcess.clear();
116     }
117 }
118
119
120 } // namespace Internal
121 } // namespace Qt4ProjectManager