maemosshthread.cpp 6.27 KB
Newer Older
ck's avatar
ck committed
1
2
/****************************************************************************
**
hjk's avatar
hjk committed
3
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
ck's avatar
ck committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the 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 "maemosshthread.h"

namespace Qt4ProjectManager {
namespace Internal {

47
48
template <class SshConnection> MaemoSshThread<SshConnection>::MaemoSshThread(const Core::SshServerInfo &server)
    : m_server(server), m_stopRequested(false)
ck's avatar
ck committed
49
50
51
{
}

52
template <class SshConnection> MaemoSshThread<SshConnection>::~MaemoSshThread()
ck's avatar
ck committed
53
54
55
56
57
{
    stop();
    wait();
}

58
template <class SshConnection> void MaemoSshThread<SshConnection>::run()
ck's avatar
ck committed
59
{
60
61
62
63
64
    if (m_stopRequested)
        return;

    if (!runInternal())
        m_error = m_connection->error();
ck's avatar
ck committed
65
66
}

67
template<class SshConnection> void MaemoSshThread<SshConnection>::stop()
ck's avatar
ck committed
68
{
ck's avatar
ck committed
69
70
    m_mutex.lock();
    m_stopRequested = true;
71
    m_waitCond.wakeAll();
ck's avatar
ck committed
72
73
    const bool hasConnection = !m_connection.isNull();
    if (hasConnection)
74
75
        m_connection->quit();
    m_mutex.unlock();
ck's avatar
ck committed
76
77
}

78
template<class SshConnection> void MaemoSshThread<SshConnection>::waitForStop()
ck's avatar
ck committed
79
{
80
81
82
83
84
85
86
87
88
    m_mutex.lock();
    while (!stopRequested())
        m_waitCond.wait(&m_mutex);
    m_mutex.unlock();
}

template<class SshConnection> void MaemoSshThread<SshConnection>::createConnection()
{
    typename SshConnection::Ptr connection = SshConnection::create(m_server);
ck's avatar
ck committed
89
    m_mutex.lock();
ck's avatar
ck committed
90
    m_connection = connection;
ck's avatar
ck committed
91
    m_mutex.unlock();
ck's avatar
ck committed
92
}
ck's avatar
ck committed
93

94
MaemoSshRunner::MaemoSshRunner(const Core::SshServerInfo &server,
ck's avatar
ck committed
95
                               const QString &command)
96
97
    : MaemoSshThread<Core::InteractiveSshConnection>(server),
      m_command(command)
ck's avatar
ck committed
98
{
99
    m_prompt = server.uname == QLatin1String("root") ? "#" : "$";
ck's avatar
ck committed
100
101
}

102
bool MaemoSshRunner::runInternal()
ck's avatar
ck committed
103
{
104
105
106
107
108
109
110
    createConnection();
    connect(m_connection.data(), SIGNAL(remoteOutput(QByteArray)),
            this, SLOT(handleRemoteOutput(QByteArray)));
    m_endMarkerCount = 0;
    m_promptEncountered = false;
    if (!m_connection->start())
        return false;
ck's avatar
ck committed
111
    if (stopRequested())
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        return true;

    waitForStop();
    return !m_connection->hasError();
}

void MaemoSshRunner::handleRemoteOutput(const QByteArray &output)
{
    // Wait for a prompt before sending the command.
    if (!m_promptEncountered) {
        if (output.indexOf(m_prompt) != -1) {
            m_promptEncountered = true;

            /*
             * We don't have access to the remote process management, so we
             * try to track the lifetime of the process by adding a second command
             * that prints a rare character. When it occurs for the second time (the
             * first one is the echo from the remote terminal), we assume the
             * process has finished. If anyone actually prints this special character
             * in their application, they are out of luck.
             */
            const QString finalCommand = m_command + QLatin1String(";echo ")
                + QString::fromUtf8(EndMarker) + QLatin1Char('\n');
            if (!m_connection->sendInput(finalCommand.toUtf8()))
                stop();
        }
ck's avatar
ck committed
138
        return;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    }

    /*
     * The output the user should see is everything after the first
     * and before the last occurrence of our marker string.
     * Note: We don't currently handle the case of an incomplete unicode
     *       character being sent.
     */
    int firstCharToEmit;
    int charsToEmitCount;
    const int endMarkerPos = output.indexOf(EndMarker);
    if (endMarkerPos != -1) {
        if (m_endMarkerCount++ == 0) {
            firstCharToEmit = endMarkerPos + EndMarker.count() + 1;
            int endMarkerPos2
                    = output.indexOf(EndMarker, firstCharToEmit);
            if (endMarkerPos2 != -1) {
                ++ m_endMarkerCount;
                charsToEmitCount = endMarkerPos2 - firstCharToEmit;
            } else {
                charsToEmitCount = -1;
            }
        } else {
            firstCharToEmit = 0;
            charsToEmitCount = endMarkerPos;
        }
    } else {
        if (m_endMarkerCount == 0) {
            charsToEmitCount = 0;
        } else {
            firstCharToEmit = 0;
            charsToEmitCount = -1;
        }
    }

    if (charsToEmitCount != 0)
        emit remoteOutput(QString::fromUtf8(output.data() + firstCharToEmit,
                                            charsToEmitCount));
    if (m_endMarkerCount == 2)
        stop();
ck's avatar
ck committed
179
180
}

181
182
183
184
185
186
187
const QByteArray MaemoSshRunner::EndMarker(QString(QChar(0x13a0)).toUtf8());


MaemoSshDeployer::MaemoSshDeployer(const Core::SshServerInfo &server,
    const QList<Core::SftpTransferInfo> &deploySpecs)
    : MaemoSshThread<Core::SftpConnection>(server),
      m_deploySpecs(deploySpecs)
ck's avatar
ck committed
188
189
190
{
}

191
bool MaemoSshDeployer::runInternal()
ck's avatar
ck committed
192
{
193
194
195
    createConnection();
    if (!m_connection->start())
        return false;
ck's avatar
ck committed
196
    if (stopRequested())
197
198
199
        return true;

    connect(m_connection.data(), SIGNAL(fileCopied(QString)),
ck's avatar
ck committed
200
            this, SIGNAL(fileCopied(QString)));
201
    return m_connection->transferFiles(m_deploySpecs);
ck's avatar
ck committed
202
203
204
205
}

} // namespace Internal
} // namespace Qt4ProjectManager