From ccc55a6149694a37d9fa5c38d6426cdbd403a0c4 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Wed, 14 Mar 2012 15:33:33 +0100
Subject: [PATCH] Debugger: Properly exit when engine run failed

Instantly stop the engine if e.g. the port is blocked. If we let
it running until the user closes the dialog, the timeout for automatic
connects would be triggered, and we'd get yet another dialog afterwards
telling the user that the connection failed.

Change-Id: Ifd0e9d743f3b8f4ba853be032d9527559ef6004e
Reviewed-by: Aurindam Jana <aurindam.jana@nokia.com>
---
 src/plugins/debugger/qml/qmlengine.cpp | 40 +++++++++++++++-----------
 src/plugins/debugger/qml/qmlengine.h   |  3 +-
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/src/plugins/debugger/qml/qmlengine.cpp b/src/plugins/debugger/qml/qmlengine.cpp
index 10f9961802f..b329fa777e9 100644
--- a/src/plugins/debugger/qml/qmlengine.cpp
+++ b/src/plugins/debugger/qml/qmlengine.cpp
@@ -346,7 +346,7 @@ QmlEngine::QmlEngine(const DebuggerStartParameters &startParameters,
     connect(&d->m_outputParser, SIGNAL(noOutputMessage()),
             this, SLOT(beginConnection()));
     connect(&d->m_outputParser, SIGNAL(errorMessage(QString)),
-            this, SLOT(connectionStartupFailed(QString)));
+            this, SLOT(appStartupFailed(QString)));
 
     // Only wait 8 seconds for the 'Waiting for connection' on application ouput, then just try to connect
     // (application output might be redirected / blocked)
@@ -434,7 +434,7 @@ void QmlEngine::beginConnection(quint16 port)
     }
 }
 
-void QmlEngine::connectionStartupFailed(const QString &errorMessage)
+void QmlEngine::connectionStartupFailed()
 {
     if (isSlaveEngine()) {
         if (masterEngine()->state() != InferiorRunOk) {
@@ -447,19 +447,11 @@ void QmlEngine::connectionStartupFailed(const QString &errorMessage)
     QMessageBox *infoBox = new QMessageBox(Core::ICore::mainWindow());
     infoBox->setIcon(QMessageBox::Critical);
     infoBox->setWindowTitle(tr("Qt Creator"));
-    if (qobject_cast<QmlAdapter *>(sender())) {
-        infoBox->setText(tr("Could not connect to the in-process QML debugger."
-                            "\nDo you want to retry?"));
-        infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel |
-                                    QMessageBox::Help);
-        infoBox->setDefaultButton(QMessageBox::Retry);
-    }
-    if (qobject_cast<QmlJsDebugClient::QDeclarativeOutputParser *>(sender())) {
-        infoBox->setText(tr("Could not connect to the in-process QML debugger."
-                            "\n%1").arg(errorMessage));
-        infoBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Help);
-        infoBox->setDefaultButton(QMessageBox::Ok);
-    }
+    infoBox->setText(tr("Could not connect to the in-process QML debugger."
+                        "\nDo you want to retry?"));
+    infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel |
+                                QMessageBox::Help);
+    infoBox->setDefaultButton(QMessageBox::Retry);
     infoBox->setModal(true);
 
     connect(infoBox, SIGNAL(finished(int)),
@@ -468,6 +460,22 @@ void QmlEngine::connectionStartupFailed(const QString &errorMessage)
     infoBox->show();
 }
 
+void QmlEngine::appStartupFailed(const QString &errorMessage)
+{
+    QMessageBox *infoBox = new QMessageBox(Core::ICore::mainWindow());
+    infoBox->setIcon(QMessageBox::Critical);
+    infoBox->setWindowTitle(tr("Qt Creator"));
+    infoBox->setText(tr("Could not connect to the in-process QML debugger."
+                        "\n%1").arg(errorMessage));
+    infoBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Help);
+    infoBox->setDefaultButton(QMessageBox::Ok);
+    connect(infoBox, SIGNAL(finished(int)),
+            this, SLOT(errorMessageBoxFinished(int)));
+    infoBox->show();
+
+    notifyEngineRunFailed();
+}
+
 void QmlEngine::errorMessageBoxFinished(int result)
 {
     switch (result) {
@@ -484,7 +492,7 @@ void QmlEngine::errorMessageBoxFinished(int result)
         if (state() == InferiorRunOk) {
             notifyInferiorSpontaneousStop();
             notifyInferiorIll();
-        } else {
+        } else if (state() == EngineRunRequested) {
             notifyEngineRunFailed();
         }
         break;
diff --git a/src/plugins/debugger/qml/qmlengine.h b/src/plugins/debugger/qml/qmlengine.h
index 25e11669563..4e1bb26082d 100644
--- a/src/plugins/debugger/qml/qmlengine.h
+++ b/src/plugins/debugger/qml/qmlengine.h
@@ -163,7 +163,8 @@ signals:
 private slots:
     void beginConnection(quint16 port = 0);
     void connectionEstablished();
-    void connectionStartupFailed(const QString &errorMessage = QString());
+    void connectionStartupFailed();
+    void appStartupFailed(const QString &errorMessage);
     void connectionError(QAbstractSocket::SocketError error);
     void serviceConnectionError(const QString &service);
     void appendMessage(const QString &msg, Utils::OutputFormat);
-- 
GitLab