From 19f1df4fe63b7ddf6700eb1ebd725145ad87b337 Mon Sep 17 00:00:00 2001
From: ck <qt-info@nokia.com>
Date: Wed, 23 Dec 2009 11:57:56 +0100
Subject: [PATCH] Maemo: Split up MaemoSshConnection.

We now have two subclasses for deploying files and
running commands, respectively.
---
 .../qt-maemo/maemosettingspage.cpp            | 10 +--
 .../qt-maemo/maemosshconnection.cpp           | 89 ++++++++++++-------
 .../qt-maemo/maemosshconnection.h             | 56 ++++++++++--
 .../qt-maemo/maemosshrunner.cpp               |  4 +-
 .../qt-maemo/maemosshrunner.h                 |  2 +-
 5 files changed, 114 insertions(+), 47 deletions(-)

diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
index 970e91b430d..109469e5c60 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
@@ -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");
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.cpp
index 76f3240fa9f..b99a3555b1a 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.cpp
@@ -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
 
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.h b/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.h
index dea9cd732e5..8129775d0f1 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshconnection.h
@@ -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
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.cpp
index 31b5d56888c..551be4654eb 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.cpp
@@ -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();
 }
 
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.h b/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.h
index e4ee1abbc72..ebffe6250a3 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshrunner.h
@@ -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
-- 
GitLab