From 83e2a378508935b400d83aa08f8a89d4f7e78339 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Fri, 1 Oct 2010 16:28:45 +0200
Subject: [PATCH] QmlObserver: Disable UI when no server plugin exists

Reviewed-by: Christiaan Janssen
---
 src/plugins/debugger/qml/qmladapter.h         |  1 -
 .../qmljsinspector/qmljsclientproxy.cpp       | 29 +++++++++++++-----
 src/plugins/qmljsinspector/qmljsclientproxy.h |  4 ++-
 src/plugins/qmljsinspector/qmljsinspector.cpp | 30 +++++++++++++++----
 src/plugins/qmljsinspector/qmljsinspector.h   |  2 ++
 5 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/src/plugins/debugger/qml/qmladapter.h b/src/plugins/debugger/qml/qmladapter.h
index 329faec69a1..64478569d7b 100644
--- a/src/plugins/debugger/qml/qmladapter.h
+++ b/src/plugins/debugger/qml/qmladapter.h
@@ -76,7 +76,6 @@ public:
     void logServiceStatusChange(const QString &service, QDeclarativeDebugClient::Status newStatus);
 
 signals:
-    void aboutToDisconnect();
     void connected();
     void disconnected();
     void connectionStartupFailed();
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index 59d430f5e02..d86f59cb78a 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -53,10 +53,10 @@ ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
     , m_designClient(0)
     , m_engineQuery(0)
     , m_contextQuery(0)
+    , m_isConnected(false)
 {
     m_requestObjectsTimer.setSingleShot(true);
     m_requestObjectsTimer.setInterval(3000);
-    connect(m_adapter, SIGNAL(aboutToDisconnect()), SLOT(disconnectFromServer()));
     connect(m_client, SIGNAL(newObjects()), this, SLOT(newObjects()));
     connect(&m_requestObjectsTimer, SIGNAL(timeout()), this, SLOT(refreshObjectTree()));
     connectToServer();
@@ -66,8 +66,6 @@ void ClientProxy::connectToServer()
 {
     m_designClient = new QmlJSObserverClient(m_adapter->connection(), this);
 
-    if (m_designClient->status() == QDeclarativeDebugClient::Enabled)
-        emit connected();
 
     m_adapter->logServiceStatusChange(m_designClient->name(), m_designClient->status());
 
@@ -93,7 +91,9 @@ void ClientProxy::connectToServer()
         SIGNAL(selectedColorChanged(QColor)));
     connect(m_designClient, SIGNAL(contextPathUpdated(QStringList)),
         SIGNAL(contextPathUpdated(QStringList)));
+
     reloadEngines();
+    updateConnected();
 }
 
 void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
@@ -105,8 +105,7 @@ void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
 
     m_adapter->logServiceStatusChange(serviceName, status);
 
-    if (status == QDeclarativeDebugClient::Enabled)
-        emit connected();
+    updateConnected();
 }
 
 void ClientProxy::disconnectFromServer()
@@ -147,6 +146,8 @@ void ClientProxy::disconnectFromServer()
 
     qDeleteAll(m_objectTreeQuery);
     m_objectTreeQuery.clear();
+
+    updateConnected();
 }
 
 void ClientProxy::refreshObjectTree()
@@ -494,6 +495,21 @@ void ClientProxy::setContextPathIndex(int contextIndex)
         m_designClient->setContextPathIndex(contextIndex);
 }
 
+void ClientProxy::updateConnected()
+{
+    bool isConnected = m_designClient && m_designClient->status() == QDeclarativeDebugClient::Enabled
+            && m_client && m_client->status() == QDeclarativeEngineDebug::Enabled;
+
+    if (isConnected != m_isConnected) {
+        m_isConnected = isConnected;
+        if (isConnected) {
+            emit connected();
+        } else {
+            emit disconnected();
+        }
+    }
+}
+
 void ClientProxy::reloadEngines()
 {
     if (m_engineQuery) {
@@ -532,8 +548,7 @@ Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
 
 bool ClientProxy::isConnected() const
 {
-    return m_designClient && m_designClient->status() == QDeclarativeDebugClient::Enabled
-            && m_client && m_client->status() == QDeclarativeEngineDebug::Enabled;
+    return m_isConnected;
 }
 
 void ClientProxy::newObjects()
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index 81a7a5d08c5..bfd628e64f0 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -92,7 +92,6 @@ signals:
     void selectedItemsChanged(const QList<QDeclarativeDebugObjectReference> &selectedItems);
 
     void connected();
-    void aboutToDisconnect();
     void disconnected();
 
     void colorPickerActivated();
@@ -136,6 +135,7 @@ private slots:
     void newObjects();
 
 private:
+    void updateConnected();
     void reloadEngines();
 
     QList<QDeclarativeDebugObjectReference> objectReferences(const QDeclarativeDebugObjectReference &objectRef) const;
@@ -157,6 +157,8 @@ private:
     QList<QDeclarativeDebugEngineReference> m_engines;
     QTimer m_requestObjectsTimer;
     DebugIdHash m_debugIdHash;
+
+    bool m_isConnected;
 };
 
 } // namespace Internal
diff --git a/src/plugins/qmljsinspector/qmljsinspector.cpp b/src/plugins/qmljsinspector/qmljsinspector.cpp
index 4f0c39fe0eb..6c25985f643 100644
--- a/src/plugins/qmljsinspector/qmljsinspector.cpp
+++ b/src/plugins/qmljsinspector/qmljsinspector.cpp
@@ -501,6 +501,20 @@ void InspectorUi::setSelectedItemsByObjectReference(QList<QDeclarativeDebugObjec
         gotoObjectReferenceDefinition(objectReferences.first());
 }
 
+void InspectorUi::enable()
+{
+    m_toolbar->enable();
+    m_crumblePath->setEnabled(true);
+    m_objectTreeWidget->setEnabled(true);
+}
+
+void InspectorUi::disable()
+{
+    m_toolbar->disable();
+    m_crumblePath->setEnabled(false);
+    m_objectTreeWidget->setEnabled(false);
+}
+
 void InspectorUi::gotoObjectReferenceDefinition(const QDeclarativeDebugObjectReference &obj)
 {
     Q_UNUSED(obj);
@@ -686,8 +700,8 @@ void InspectorUi::disableLivePreview()
 void InspectorUi::setupToolbar(bool doConnect)
 {
     if (doConnect) {
-        connect(m_clientProxy, SIGNAL(connected()), m_toolbar, SLOT(enable()));
-        connect(m_clientProxy, SIGNAL(disconnected()), m_toolbar, SLOT(disable()));
+        connect(m_clientProxy, SIGNAL(connected()), this, SLOT(enable()));
+        connect(m_clientProxy, SIGNAL(disconnected()), this, SLOT(disable()));
 
         connect(m_toolbar, SIGNAL(designModeSelected(bool)), m_clientProxy, SLOT(setDesignModeBehavior(bool)));
         connect(m_toolbar, SIGNAL(reloadSelected()), m_clientProxy, SLOT(reloadQmlViewer()));
@@ -705,10 +719,9 @@ void InspectorUi::setupToolbar(bool doConnect)
         connect(m_clientProxy, SIGNAL(selectedColorChanged(QColor)), m_toolbar, SLOT(setSelectedColor(QColor)));
 
         connect(m_clientProxy, SIGNAL(animationSpeedChanged(qreal)), m_toolbar, SLOT(setAnimationSpeed(qreal)));
-        m_toolbar->enable();
     } else {
-        disconnect(m_clientProxy, SIGNAL(connected()), m_toolbar, SLOT(enable()));
-        disconnect(m_clientProxy, SIGNAL(disconnected()), m_toolbar, SLOT(disable()));
+        disconnect(m_clientProxy, SIGNAL(connected()), this, SLOT(enable()));
+        disconnect(m_clientProxy, SIGNAL(disconnected()), this, SLOT(disable()));
 
         disconnect(m_toolbar, SIGNAL(designModeSelected(bool)), m_clientProxy, SLOT(setDesignModeBehavior(bool)));
         disconnect(m_toolbar, SIGNAL(reloadSelected()), m_clientProxy, SLOT(reloadQmlViewer()));
@@ -726,6 +739,11 @@ void InspectorUi::setupToolbar(bool doConnect)
         disconnect(m_clientProxy, SIGNAL(selectedColorChanged(QColor)), m_toolbar, SLOT(setSelectedColor(QColor)));
 
         disconnect(m_clientProxy, SIGNAL(animationSpeedChanged(qreal)), m_toolbar, SLOT(setAnimationSpeed(qreal)));
-        m_toolbar->disable();
+    }
+
+    if (m_clientProxy && m_clientProxy->isConnected()) {
+        enable();
+    } else {
+        disable();
     }
 }
diff --git a/src/plugins/qmljsinspector/qmljsinspector.h b/src/plugins/qmljsinspector/qmljsinspector.h
index 494b0fde913..ae56e4eca1a 100644
--- a/src/plugins/qmljsinspector/qmljsinspector.h
+++ b/src/plugins/qmljsinspector/qmljsinspector.h
@@ -119,6 +119,8 @@ public slots:
     void setApplyChangesToQmlObserver(bool applyChanges);
 
 private slots:
+    void enable();
+    void disable();
     void gotoObjectReferenceDefinition(const QDeclarativeDebugObjectReference &obj);
 
     void setSelectedItemsByObjectReference(QList<QDeclarativeDebugObjectReference> objectReferences);
-- 
GitLab