From 226a82d3dc74384d820ea63a1b330eb2ad341042 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Thu, 30 Sep 2010 14:05:20 +0200
Subject: [PATCH] QmlDebugger: Log status of different services

Reviewed-by: Christiaan Janssen
---
 src/plugins/debugger/qml/qmladapter.cpp       | 35 +++++++++++
 src/plugins/debugger/qml/qmladapter.h         |  5 ++
 .../debugger/qml/qmldebuggerclient.cpp        |  5 ++
 src/plugins/debugger/qml/qmldebuggerclient.h  |  2 +
 src/plugins/debugger/qml/qmlengine.cpp        |  7 +++
 src/plugins/debugger/qml/qmlengine.h          |  1 +
 .../qmljsinspector/qmljsclientproxy.cpp       | 60 ++++++++++++-------
 src/plugins/qmljsinspector/qmljsclientproxy.h |  2 +-
 .../qmljsinspector/qmljsobserverclient.cpp    |  5 ++
 .../qmljsinspector/qmljsobserverclient.h      |  3 +
 10 files changed, 102 insertions(+), 23 deletions(-)

diff --git a/src/plugins/debugger/qml/qmladapter.cpp b/src/plugins/debugger/qml/qmladapter.cpp
index fbea843d19e..985fc4d9810 100644
--- a/src/plugins/debugger/qml/qmladapter.cpp
+++ b/src/plugins/debugger/qml/qmladapter.cpp
@@ -143,6 +143,16 @@ void QmlAdapter::connectionErrorOccurred(QAbstractSocket::SocketError socketErro
         emit connectionError(socketError);
 }
 
+void QmlAdapter::clientStatusChanged(QDeclarativeDebugClient::Status status)
+{
+    QString serviceName;
+    if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
+        serviceName = client->name();
+    }
+
+    logServiceStatusChange(serviceName, status);
+}
+
 void QmlAdapter::connectionStateChanged()
 {
     switch (d->m_conn->state()) {
@@ -165,6 +175,7 @@ void QmlAdapter::connectionStateChanged()
 
             if (!d->m_mainClient) {
                 d->m_mainClient = new QDeclarativeEngineDebug(d->m_conn, this);
+                logServiceStatusChange(QLatin1String("QmlObserver"), static_cast<QDeclarativeDebugClient::Status>(d->m_mainClient->status()));
             }
 
             createDebuggerClient();
@@ -185,11 +196,15 @@ void QmlAdapter::createDebuggerClient()
 {
     d->m_qmlClient = new Internal::QmlDebuggerClient(d->m_conn);
 
+    connect(d->m_qmlClient, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
+            this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
     connect(d->m_engine.data(), SIGNAL(sendMessage(QByteArray)),
             d->m_qmlClient, SLOT(slotSendMessage(QByteArray)));
     connect(d->m_qmlClient, SIGNAL(messageWasReceived(QByteArray)),
             d->m_engine.data(), SLOT(messageReceived(QByteArray)));
 
+    logServiceStatusChange(d->m_qmlClient->name(), d->m_qmlClient->status());
+
     //engine->startSuccessful();  // FIXME: AAA: port to new debugger states
 }
 
@@ -237,4 +252,24 @@ void QmlAdapter::setConnectionAttemptInterval(int interval)
     d->m_connectionTimer->setInterval(interval);
 }
 
+void QmlAdapter::logServiceStatusChange(const QString &service, QDeclarativeDebugClient::Status newStatus)
+{
+    switch (newStatus) {
+    case QDeclarativeDebugClient::Unavailable: {
+        showConnectionErrorMessage(tr("Error: Cannot connect to debug service '%1'. Debugging functionality will be limited.").arg(service));
+        emit serviceConnectionError(service);
+        break;
+    }
+    case QDeclarativeDebugClient::Enabled: {
+        showConnectionStatusMessage(tr("Connected to debug service '%1'.").arg(service));
+        break;
+    }
+
+    case QDeclarativeDebugClient::NotConnected: {
+        showConnectionStatusMessage(tr("Not connected to debug service '%1'.").arg(service));
+        break;
+    }
+    }
+}
+
 } // namespace Debugger
diff --git a/src/plugins/debugger/qml/qmladapter.h b/src/plugins/debugger/qml/qmladapter.h
index 6118a05543f..8cbce251397 100644
--- a/src/plugins/debugger/qml/qmladapter.h
+++ b/src/plugins/debugger/qml/qmladapter.h
@@ -36,6 +36,7 @@
 #include "debugger_global.h"
 
 #include <QtNetwork/QAbstractSocket>
+#include "qmldebuggerclient.h"
 
 namespace QmlJsDebugClient {
 class QDeclarativeEngineDebug;
@@ -72,15 +73,19 @@ public:
     void setMaxConnectionAttempts(int maxAttempts);
     void setConnectionAttemptInterval(int interval);
 
+    void logServiceStatusChange(const QString &service, QDeclarativeDebugClient::Status newStatus);
+
 signals:
     void aboutToDisconnect();
     void connected();
     void disconnected();
     void connectionStartupFailed();
     void connectionError(QAbstractSocket::SocketError socketError);
+    void serviceConnectionError(const QString serviceName);
 
 private slots:
     void connectionErrorOccurred(QAbstractSocket::SocketError socketError);
+    void clientStatusChanged(QDeclarativeDebugClient::Status status);
     void connectionStateChanged();
     void pollInferior();
 
diff --git a/src/plugins/debugger/qml/qmldebuggerclient.cpp b/src/plugins/debugger/qml/qmldebuggerclient.cpp
index c3b81549365..b0953df3004 100644
--- a/src/plugins/debugger/qml/qmldebuggerclient.cpp
+++ b/src/plugins/debugger/qml/qmldebuggerclient.cpp
@@ -43,6 +43,11 @@ QmlDebuggerClient::~QmlDebuggerClient()
 {
 }
 
+void QmlDebuggerClient::statusChanged(Status status)
+{
+    emit newStatus(status);
+}
+
 void QmlDebuggerClient::messageReceived(const QByteArray &data)
 {
     emit messageWasReceived(data);
diff --git a/src/plugins/debugger/qml/qmldebuggerclient.h b/src/plugins/debugger/qml/qmldebuggerclient.h
index 191e3570d52..271f632a94a 100644
--- a/src/plugins/debugger/qml/qmldebuggerclient.h
+++ b/src/plugins/debugger/qml/qmldebuggerclient.h
@@ -44,12 +44,14 @@ public:
     virtual ~QmlDebuggerClient();
 
 signals:
+    void newStatus(QDeclarativeDebugClient::Status status);
     void messageWasReceived(const QByteArray &data);
 
 private Q_SLOTS:
     void slotSendMessage(const QByteArray &message);
 
 protected:
+    virtual void statusChanged(Status status);
     virtual void messageReceived(const QByteArray &data);
 };
 
diff --git a/src/plugins/debugger/qml/qmlengine.cpp b/src/plugins/debugger/qml/qmlengine.cpp
index a0b2be77468..b4c361d55b3 100644
--- a/src/plugins/debugger/qml/qmlengine.cpp
+++ b/src/plugins/debugger/qml/qmlengine.cpp
@@ -218,6 +218,12 @@ void QmlEngine::connectionError(QAbstractSocket::SocketError socketError)
         plugin()->showMessage(tr("QML Debugger: Remote host closed connection."), StatusBar);
 }
 
+
+void QmlEngine::serviceConnectionError(const QString &serviceName)
+{
+    plugin()->showMessage(tr("QML Debugger: Couldn't connect to service '%1'.").arg(serviceName), StatusBar);
+}
+
 void QmlEngine::runEngine()
 {
     QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
@@ -327,6 +333,7 @@ void QmlEngine::setupEngine()
     d->m_adapter->setConnectionAttemptInterval(ConnectionAttemptDefaultInterval);
     connect(d->m_adapter, SIGNAL(connectionError(QAbstractSocket::SocketError)),
             SLOT(connectionError(QAbstractSocket::SocketError)));
+    connect(d->m_adapter, SIGNAL(serviceConnectionError(QString)), SLOT(serviceConnectionError(QString)));
     connect(d->m_adapter, SIGNAL(connected()), SLOT(connectionEstablished()));
     connect(d->m_adapter, SIGNAL(connectionStartupFailed()), SLOT(connectionStartupFailed()));
 
diff --git a/src/plugins/debugger/qml/qmlengine.h b/src/plugins/debugger/qml/qmlengine.h
index 5f12e25eee5..998a183aa4b 100644
--- a/src/plugins/debugger/qml/qmlengine.h
+++ b/src/plugins/debugger/qml/qmlengine.h
@@ -117,6 +117,7 @@ private slots:
     void connectionEstablished();
     void connectionStartupFailed();
     void connectionError(QAbstractSocket::SocketError error);
+    void serviceConnectionError(const QString &service);
 
 private:
     void expandObject(const QByteArray &iname, quint64 objectId);
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index fca7bb4fe0c..4bd763c7fde 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -65,8 +65,14 @@ ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
 void ClientProxy::connectToServer()
 {
     m_designClient = new QmlJSObserverClient(m_adapter->connection(), this);
-    emit connected();
 
+    if (m_designClient->status() == QDeclarativeDebugClient::Enabled)
+        emit connected();
+
+    m_adapter->logServiceStatusChange(m_designClient->name(), m_designClient->status());
+
+    connect(m_designClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
+             this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
     connect(m_designClient, SIGNAL(currentObjectsChanged(QList<int>)),
         SLOT(onCurrentObjectsChanged(QList<int>)));
     connect(m_designClient, SIGNAL(colorPickerActivated()),
@@ -90,9 +96,24 @@ void ClientProxy::connectToServer()
     reloadEngines();
 }
 
+void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
+{
+    QString serviceName;
+    if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
+        serviceName = client->name();
+    }
+
+    m_adapter->logServiceStatusChange(serviceName, status);
+
+    if (status == QDeclarativeDebugClient::Enabled)
+        emit connected();
+}
+
 void ClientProxy::disconnectFromServer()
 {
     if (m_designClient) {
+        disconnect(m_designClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
+                 this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
         disconnect(m_designClient, SIGNAL(currentObjectsChanged(QList<int>)),
             this, SLOT(onCurrentObjectsChanged(QList<int>)));
         disconnect(m_designClient, SIGNAL(colorPickerActivated()),
@@ -164,7 +185,7 @@ void ClientProxy::onCurrentObjectsChanged(const QList< int >& debugIds, bool req
 
 void ClientProxy::setSelectedItemsByObjectId(const QList<QDeclarativeDebugObjectReference> &objectRefs)
 {
-    if (isConnected() && m_designClient)
+    if (isConnected())
         m_designClient->setSelectedItemsByObjectId(objectRefs);
 }
 
@@ -285,7 +306,7 @@ QDeclarativeDebugExpressionQuery *ClientProxy::queryExpressionResult(int objectD
 
 void ClientProxy::clearComponentCache()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->clearComponentCache();
 }
 
@@ -365,7 +386,7 @@ void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
             buildDebugIdHashRecursive(it);
         emit objectTreeUpdated();
 
-        if (isDesignClientConnected()) {
+        if (isConnected()) {
             if (!m_designClient->selectedItemIds().isEmpty())
                 onCurrentObjectsChanged(m_designClient->selectedItemIds(), false);
 
@@ -410,76 +431,70 @@ void ClientProxy::buildDebugIdHashRecursive(const QDeclarativeDebugObjectReferen
 
 void ClientProxy::reloadQmlViewer()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->reloadViewer();
 }
 
 void ClientProxy::setDesignModeBehavior(bool inDesignMode)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->setDesignModeBehavior(inDesignMode);
 }
 
 void ClientProxy::setAnimationSpeed(qreal slowdownFactor)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->setAnimationSpeed(slowdownFactor);
 }
 
 void ClientProxy::changeToColorPickerTool()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->changeToColorPickerTool();
 }
 
 void ClientProxy::changeToZoomTool()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->changeToZoomTool();
 }
 void ClientProxy::changeToSelectTool()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->changeToSelectTool();
 }
 
 void ClientProxy::changeToSelectMarqueeTool()
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->changeToSelectMarqueeTool();
 }
 
 void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
                                   const QStringList &imports, const QString &filename)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->createQmlObject(qmlText, parentDebugId, imports, filename);
 }
 
 void ClientProxy::destroyQmlObject(int debugId)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->destroyQmlObject(debugId);
 }
 
 void ClientProxy::reparentQmlObject(int debugId, int newParent)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->reparentQmlObject(debugId, newParent);
 }
 
 void ClientProxy::setContextPathIndex(int contextIndex)
 {
-    if (isDesignClientConnected())
+    if (isConnected())
         m_designClient->setContextPathIndex(contextIndex);
 }
 
-
-bool ClientProxy::isDesignClientConnected() const
-{
-    return (m_designClient && m_adapter->isConnected());
-}
-
 void ClientProxy::reloadEngines()
 {
     if (m_engineQuery) {
@@ -518,7 +533,8 @@ Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
 
 bool ClientProxy::isConnected() const
 {
-    return m_adapter->isConnected();
+    return m_designClient && m_designClient->status() == QDeclarativeDebugClient::Enabled
+            && m_client && m_client->status() == QDeclarativeEngineDebug::Enabled;
 }
 
 void ClientProxy::newObjects()
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index 1c7c2162f66..81a7a5d08c5 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -125,6 +125,7 @@ public slots:
 private slots:
     void disconnectFromServer();
     void connectToServer();
+    void clientStatusChanged(QDeclarativeDebugClient::Status status);
 
     void contextChanged();
 
@@ -135,7 +136,6 @@ private slots:
     void newObjects();
 
 private:
-    bool isDesignClientConnected() const;
     void reloadEngines();
 
     QList<QDeclarativeDebugObjectReference> objectReferences(const QDeclarativeDebugObjectReference &objectRef) const;
diff --git a/src/plugins/qmljsinspector/qmljsobserverclient.cpp b/src/plugins/qmljsinspector/qmljsobserverclient.cpp
index 734977f2e48..aef5b972388 100644
--- a/src/plugins/qmljsinspector/qmljsobserverclient.cpp
+++ b/src/plugins/qmljsinspector/qmljsobserverclient.cpp
@@ -55,6 +55,11 @@ QmlJSObserverClient::QmlJSObserverClient(QDeclarativeDebugConnection *client,
 {
 }
 
+void QmlJSObserverClient::statusChanged(Status status)
+{
+    emit connectedStatusChanged(status);
+}
+
 void QmlJSObserverClient::messageReceived(const QByteArray &message)
 {
     QDataStream ds(message);
diff --git a/src/plugins/qmljsinspector/qmljsobserverclient.h b/src/plugins/qmljsinspector/qmljsobserverclient.h
index 7928984f898..2c7753a4397 100644
--- a/src/plugins/qmljsinspector/qmljsobserverclient.h
+++ b/src/plugins/qmljsinspector/qmljsobserverclient.h
@@ -80,6 +80,8 @@ public:
     void clearComponentCache();
 
 signals:
+    void connectedStatusChanged(QDeclarativeDebugClient::Status status);
+
     void currentObjectsChanged(const QList<int> &debugIds);
     void selectedColorChanged(const QColor &color);
     void colorPickerActivated();
@@ -92,6 +94,7 @@ signals:
     void contextPathUpdated(const QStringList &path);
 
 protected:
+    virtual void statusChanged(Status);
     virtual void messageReceived(const QByteArray &);
 
 private:
-- 
GitLab