Commit 19f1df4f authored by ck's avatar ck

Maemo: Split up MaemoSshConnection.

We now have two subclasses for deploying files and
running commands, respectively.
parent b3d30899
......@@ -156,7 +156,7 @@ private:
PortAndTimeoutValidator m_portValidator;
PortAndTimeoutValidator m_timeoutValidator;
NameValidator m_nameValidator;
#ifdef USE_SSH_LIBS
#ifdef USE_SSH_LIB
MaemoSshRunner *m_deviceTester;
#endif
QString m_deviceTestOutput;
......@@ -213,7 +213,7 @@ MaemoSettingsWidget::MaemoSettingsWidget(QWidget *parent)
m_ui(new Ui_maemoSettingsWidget),
m_devConfs(MaemoDeviceConfigurations::instance().devConfigs()),
m_nameValidator(m_devConfs)
#ifdef USE_SSH_LIBS
#ifdef USE_SSH_LIB
, m_deviceTester(0)
#endif
......@@ -392,7 +392,7 @@ void MaemoSettingsWidget::keyFileEditingFinished()
void MaemoSettingsWidget::testConfig()
{
#ifdef USE_SSH_LIBS
#ifdef USE_SSH_LIB
qDebug("Oh yes, this config will be tested!");
if (m_deviceTester)
return;
......@@ -430,7 +430,7 @@ void MaemoSettingsWidget::processSshOutput(const QString &data)
void MaemoSettingsWidget::handleSshFinished()
{
#ifdef USE_SSH_LIBS
#ifdef USE_SSH_LIB
qDebug("================> %s", Q_FUNC_INFO);
if (!m_deviceTester)
return;
......@@ -449,7 +449,7 @@ void MaemoSettingsWidget::handleSshFinished()
void MaemoSettingsWidget::stopConfigTest()
{
#ifdef USE_SSH_LIBS
#ifdef USE_SSH_LIB
qDebug("================> %s", Q_FUNC_INFO);
if (m_deviceTester) {
qDebug("Actually doing something");
......
......@@ -56,15 +56,9 @@ namespace {
ne7ssh ssh;
}
MaemoSshConnection::Ptr MaemoSshConnection::connect(
const MaemoDeviceConfig &devConf, bool shell)
{
return MaemoSshConnection::Ptr(new MaemoSshConnection(devConf, shell));
}
MaemoSshConnection::MaemoSshConnection(const MaemoDeviceConfig &devConf,
bool shell)
: m_channel(-1), m_prompt(0), m_stopRequested(false)
: m_channel(-1), m_stopRequested(false)
{
const QString *authString;
int (ne7ssh::*connFunc)(const char *, int, const char *, const char *, bool, int);
......@@ -79,54 +73,89 @@ MaemoSshConnection::MaemoSshConnection(const MaemoDeviceConfig &devConf,
devConf.uname.toAscii(), authString->toAscii(), shell, devConf.timeout);
if (m_channel == -1)
throw MaemoSshException(tr("Could not connect to host"));
if (shell) {
m_prompt = devConf.uname == QLatin1String("root") ? "# " : "$ ";
if (!ssh.waitFor(m_channel, m_prompt, devConf.timeout)) {
const QString error = tr("Could not start remote shell: %1").
arg(ssh.errors()->pop(m_channel));
ssh.close(m_channel);
throw MaemoSshException(error);
}
}
}
MaemoSshConnection::~MaemoSshConnection()
{
qDebug("%s", Q_FUNC_INFO);
if (m_prompt) {
ssh.send("exit\n", m_channel);
ssh.waitFor(m_channel, m_prompt, 1);
ssh.close(m_channel);
}
const char *MaemoSshConnection::lastError()
{
return ssh.errors()->pop(channel());
}
void MaemoSshConnection::stop()
{
m_stopRequested = true;
}
MaemoInteractiveSshConnection::MaemoInteractiveSshConnection(const MaemoDeviceConfig &devConf)
: MaemoSshConnection(devConf, true), m_prompt(0)
{
m_prompt = devConf.uname == QLatin1String("root") ? "# " : "$ ";
if (!ssh.waitFor(channel(), m_prompt, devConf.timeout)) {
const QString error
= tr("Could not start remote shell: %1").arg(lastError());
throw MaemoSshException(error);
}
}
ssh.close(m_channel);
MaemoInteractiveSshConnection::~MaemoInteractiveSshConnection()
{
ssh.send("exit\n", channel());
ssh.waitFor(channel(), m_prompt, 1);
}
void MaemoSshConnection::runCommand(const QString &command)
void MaemoInteractiveSshConnection::runCommand(const QString &command)
{
if (!ssh.send((command + QLatin1String("\n")).toLatin1().data(),
m_channel)) {
channel())) {
throw MaemoSshException(tr("Error running command: %1")
.arg(ssh.errors()->pop(m_channel)));
.arg(lastError()));
}
bool done;
do {
done = ssh.waitFor(m_channel, m_prompt, 3);
const char * const error = ssh.errors()->pop(m_channel);
done = ssh.waitFor(channel(), m_prompt, 2);
const char * const error = lastError();
if (error)
throw MaemoSshException(tr("SSH error: %1").arg(error));
const char * const output = ssh.read(m_channel);
const char * const output = ssh.read(channel());
if (output)
emit remoteOutput(QLatin1String(output));
} while (!done && !m_stopRequested);
} while (!done && !stopRequested());
}
void MaemoSshConnection::stopCommand()
MaemoInteractiveSshConnection::Ptr MaemoInteractiveSshConnection::create(const MaemoDeviceConfig &devConf)
{
m_stopRequested = true;
return Ptr(new MaemoInteractiveSshConnection(devConf));
}
MaemoSftpConnection::MaemoSftpConnection(const MaemoDeviceConfig &devConf)
: MaemoSshConnection(devConf, false)
{
// TODO: Initialize sftp subsystem
}
MaemoSftpConnection::~MaemoSftpConnection()
{
}
void MaemoSftpConnection::transferFiles(const QStringList &filePaths,
const QStringList &targetDirs)
{
}
MaemoSftpConnection::Ptr MaemoSftpConnection::create(const MaemoDeviceConfig &devConf)
{
return Ptr(new MaemoSftpConnection(devConf));
}
} // namespace Internal
} // namespace Qt4ProjectManager
......
......@@ -42,12 +42,17 @@
#ifndef MAEMOSSHCONNECTION_H
#define MAEMOSSHCONNECTION_H
// #define USE_SSH_LIB
#ifdef USE_SSH_LIB
#include <QtCore/QCoreApplication>
#include <QtCore/QScopedPointer>
#include <QtCore/QSharedPointer>
QT_BEGIN_NAMESPACE
class QStringList;
QT_END_NAMESPACE
class ne7ssh;
namespace Qt4ProjectManager {
......@@ -66,26 +71,59 @@ private:
class MaemoSshConnection : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(MaemoSshConnection)
friend class MaemoSshFacility;
public:
typedef QSharedPointer<MaemoSshConnection> Ptr;
void stop();
virtual ~MaemoSshConnection();
protected:
MaemoSshConnection(const MaemoDeviceConfig &devConf, bool shell);
int channel() const { return m_channel; }
bool stopRequested() const {return m_stopRequested; }
const char *lastError();
private:
int m_channel;
volatile bool m_stopRequested;
};
class MaemoInteractiveSshConnection : public MaemoSshConnection
{
Q_OBJECT
Q_DISABLE_COPY(MaemoInteractiveSshConnection)
public:
typedef QSharedPointer<MaemoInteractiveSshConnection> Ptr;
static Ptr connect(const MaemoDeviceConfig &devConf, bool shell);
static Ptr create(const MaemoDeviceConfig &devConf);
void runCommand(const QString &command);
void stopCommand();
~MaemoSshConnection();
virtual ~MaemoInteractiveSshConnection();
signals:
void remoteOutput(const QString &output);
private:
MaemoSshConnection(const MaemoDeviceConfig &devConf, bool shell);
MaemoInteractiveSshConnection(const MaemoDeviceConfig &devConf);
int m_channel;
const char *m_prompt;
volatile bool m_stopRequested;
};
class MaemoSftpConnection : public MaemoSshConnection
{
Q_OBJECT
Q_DISABLE_COPY(MaemoSftpConnection)
public:
typedef QSharedPointer<MaemoSftpConnection> Ptr;
static Ptr create(const MaemoDeviceConfig &devConf);
void transferFiles(const QStringList &filePaths,
const QStringList &targetDirs);
virtual ~MaemoSftpConnection();
signals:
void fileCopied(const QString &filePath);
private:
MaemoSftpConnection(const MaemoDeviceConfig &devConf);
};
} // namespace Internal
......
......@@ -54,7 +54,7 @@ MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf, const QString &
void MaemoSshRunner::run()
{
try {
m_connection = MaemoSshConnection::connect(m_devConf, true);
m_connection = MaemoInteractiveSshConnection::create(m_devConf);
emit connectionEstablished();
connect(m_connection.data(), SIGNAL(remoteOutput(QString)),
this, SIGNAL(remoteOutput(QString)));
......@@ -67,7 +67,7 @@ void MaemoSshRunner::run()
void MaemoSshRunner::stop()
{
if (!m_connection.isNull())
m_connection->stopCommand();
m_connection->stop();
wait();
}
......
......@@ -71,7 +71,7 @@ private:
const MaemoDeviceConfig m_devConf;
const QString m_command;
QString m_error;
MaemoSshConnection::Ptr m_connection;
MaemoInteractiveSshConnection::Ptr m_connection;
};
} // namespace Internal
......
Markdown is supported
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