From 18a134ec377bb03781f926291a8c5f5a8c253360 Mon Sep 17 00:00:00 2001
From: ck <qt-info@nokia.com>
Date: Tue, 13 Apr 2010 16:30:52 +0200
Subject: [PATCH] Maemo: Kill old instances before deploying the application.

Reviewed-by: kh1
---
 .../qt-maemo/maemoruncontrol.cpp              | 53 +++++++++++++++----
 .../qt-maemo/maemoruncontrol.h                |  7 ++-
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.cpp
index f715d5a970e..e3af8e3db7a 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.cpp
@@ -72,18 +72,43 @@ void AbstractMaemoRunControl::start()
 {
     m_stoppedByUser = false;
     emit started();
-    startInternal();
+    startInitialCleanup();
+}
+
+void AbstractMaemoRunControl::startInitialCleanup()
+{
+    emit addToOutputWindow(this, tr("Cleaning up remote leftovers first ..."));
+    const QStringList appsToKill
+        = QStringList() << executableFileName() << QLatin1String("gdbserver");
+    killRemoteProcesses(appsToKill, true);
 }
 
 void AbstractMaemoRunControl::stop()
 {
     m_stoppedByUser = true;
-    if (isDeploying())
-        stopDeployment();
+    if (isCleaning())
+        m_initialCleaner->stop();
+    else if (isDeploying())
+        m_sshDeployer->stop();
     else
         stopInternal();
 }
 
+void AbstractMaemoRunControl::handleInitialCleanupFinished()
+{
+    if (m_stoppedByUser) {
+        emit addToOutputWindow(this, tr("Initial cleanup canceled by user."));
+        emit finished();
+    } else if (m_initialCleaner->hasError()) {
+        handleError(tr("Error running initial cleanup: %1.")
+                    .arg(m_initialCleaner->error()));
+        emit finished();
+    } else {
+        emit addToOutputWindow(this, tr("Initial cleanup done."));
+        startInternal();
+    }
+}
+
 void AbstractMaemoRunControl::startDeployment(bool forDebugging)
 {
     QTC_ASSERT(m_runConfig, return);
@@ -150,14 +175,14 @@ void AbstractMaemoRunControl::handleFileCopied()
     m_progress.setProgressValue(m_progress.progressValue() + 1);
 }
 
-void AbstractMaemoRunControl::stopDeployment()
+bool AbstractMaemoRunControl::isDeploying() const
 {
-    m_sshDeployer->stop();
+    return m_sshDeployer && m_sshDeployer->isRunning();
 }
 
-bool AbstractMaemoRunControl::isDeploying() const
+bool AbstractMaemoRunControl::isCleaning() const
 {
-    return m_sshDeployer && m_sshDeployer->isRunning();
+    return m_initialCleaner && m_initialCleaner->isRunning();
 }
 
 void AbstractMaemoRunControl::startExecution()
@@ -183,11 +208,12 @@ void AbstractMaemoRunControl::stopRunning(bool forDebugging)
         QStringList apps(executableFileName());
         if (forDebugging)
             apps << QLatin1String("gdbserver");
-        kill(apps);
+        killRemoteProcesses(apps, false);
     }
 }
 
-void AbstractMaemoRunControl::kill(const QStringList &apps)
+void AbstractMaemoRunControl::killRemoteProcesses(const QStringList &apps,
+                                                  bool initialCleanup)
 {
     QString niceKill;
     QString brutalKill;
@@ -197,8 +223,13 @@ void AbstractMaemoRunControl::kill(const QStringList &apps)
     }
     const QString remoteCall
         = niceKill + QLatin1String("sleep 1; ") + brutalKill;
-    m_sshStopper.reset(new MaemoSshRunner(m_devConfig, remoteCall));
-    m_sshStopper->start();
+    QScopedPointer<MaemoSshRunner> &runner
+        = initialCleanup ? m_initialCleaner : m_sshStopper;
+    runner.reset(new MaemoSshRunner(m_devConfig, remoteCall));
+    if (initialCleanup)
+        connect(runner.data(), SIGNAL(finished()),
+                this, SLOT(handleInitialCleanupFinished()));
+    runner->start();
 }
 
 void AbstractMaemoRunControl::handleDeployThreadFinished()
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.h b/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.h
index 74e6cd192a5..6640f1bb7a8 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemoruncontrol.h
@@ -73,7 +73,6 @@ protected:
 
     void startDeployment(bool forDebugging);
     void deploy();
-    void stopDeployment();
     void stopRunning(bool forDebugging);
     void startExecution();
     void handleError(const QString &errString);
@@ -86,6 +85,7 @@ protected:
     const QStringList options() const;
 private slots:
     virtual void handleRemoteOutput(const QString &output)=0;
+    void handleInitialCleanupFinished();
     void handleDeployThreadFinished();
     void handleRunThreadFinished();
     void handleFileCopied();
@@ -98,13 +98,16 @@ private:
     virtual void startInternal()=0;
     virtual void stopInternal()=0;
     virtual QString remoteCall() const=0;
-    void kill(const QStringList &apps);
+    void startInitialCleanup();
+    void killRemoteProcesses(const QStringList &apps, bool initialCleanup);
+    bool isCleaning() const;
     bool isDeploying() const;
 
     QFutureInterface<void> m_progress;
     QScopedPointer<MaemoSshDeployer> m_sshDeployer;
     QScopedPointer<MaemoSshRunner> m_sshRunner;
     QScopedPointer<MaemoSshRunner> m_sshStopper;
+    QScopedPointer<MaemoSshRunner> m_initialCleaner;
     bool m_stoppedByUser;
 
     struct Deployable
-- 
GitLab