Files
qt-creator/src/plugins/debugger/qml/qmladapter.cpp

305 lines
9.1 KiB
C++
Raw Normal View History

/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "qmladapter.h"
2011-01-10 10:14:23 +01:00
#include "qmlengine.h"
#include "qmlv8debuggerclient.h"
#include "qscriptdebuggerclient.h"
#include <qmldebug/qdebugmessageclient.h>
#include <utils/qtcassert.h>
#include <QDebug>
namespace Debugger {
2011-01-12 13:51:26 +01:00
namespace Internal {
QmlAdapter::QmlAdapter(DebuggerEngine *engine, QObject *parent)
: QObject(parent)
, m_engine(engine)
, m_qmlClient(0)
, m_conn(0)
, m_msgClient(0)
{
m_connectionTimer.setInterval(4000);
m_connectionTimer.setSingleShot(true);
connect(&m_connectionTimer, SIGNAL(timeout()), SLOT(checkConnectionState()));
m_conn = new QmlDebugConnection(this);
connect(m_conn, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
SLOT(connectionStateChanged()));
connect(m_conn, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(connectionErrorOccurred(QAbstractSocket::SocketError)));
createDebuggerClients();
m_msgClient = new QDebugMessageClient(m_conn);
connect(m_msgClient, SIGNAL(newStatus(QmlDebugClient::Status)),
this, SLOT(clientStatusChanged(QmlDebugClient::Status)));
}
QmlAdapter::~QmlAdapter()
{
}
void QmlAdapter::beginConnectionTcp(const QString &address, quint16 port)
{
if (m_engine.isNull()
|| (m_conn && m_conn->state() != QAbstractSocket::UnconnectedState))
return;
showConnectionStatusMessage(tr("Connecting to debug server %1:%2").arg(address).arg(
QString::number(port)));
m_conn->connectToHost(address, port);
//A timeout to check the connection state
m_connectionTimer.start();
}
void QmlAdapter::beginConnectionOst(const QString &channel)
{
if (m_engine.isNull()
|| (m_conn && m_conn->state() != QAbstractSocket::UnconnectedState))
return;
showConnectionStatusMessage(tr("Connecting to debug server on %1").arg(channel));
m_conn->connectToOst(channel);
//A timeout to check the connection state
m_connectionTimer.start();
}
void QmlAdapter::closeConnection()
{
if (m_connectionTimer.isActive()) {
m_connectionTimer.stop();
} else {
if (m_conn) {
m_conn->close();
}
}
}
void QmlAdapter::connectionErrorOccurred(QAbstractSocket::SocketError socketError)
{
showConnectionStatusMessage(tr("Error: (%1) %2", "%1=error code, %2=error message")
.arg(socketError).arg(m_conn->errorString()));
// this is only an error if we are already connected and something goes wrong.
if (isConnected()) {
emit connectionError(socketError);
} else {
m_connectionTimer.stop();
emit connectionStartupFailed();
}
}
void QmlAdapter::clientStatusChanged(QmlDebugClient::Status status)
{
QString serviceName;
float version = 0;
if (QmlDebugClient *client = qobject_cast<QmlDebugClient*>(sender())) {
serviceName = client->name();
version = client->serviceVersion();
}
logServiceStatusChange(serviceName, version, status);
}
void QmlAdapter::debugClientStatusChanged(QmlDebugClient::Status status)
{
if (status != QmlDebugClient::Enabled)
return;
QmlDebugClient *client = qobject_cast<QmlDebugClient*>(sender());
QTC_ASSERT(client, return);
m_qmlClient = qobject_cast<Internal::BaseQmlDebuggerClient *>(client);
m_qmlClient->startSession();
}
void QmlAdapter::connectionStateChanged()
{
switch (m_conn->state()) {
2011-02-17 15:57:26 +01:00
case QAbstractSocket::UnconnectedState:
{
showConnectionStatusMessage(tr("disconnected.\n\n"));
emit disconnected();
2011-02-17 15:57:26 +01:00
break;
}
case QAbstractSocket::HostLookupState:
showConnectionStatusMessage(tr("resolving host..."));
break;
case QAbstractSocket::ConnectingState:
showConnectionStatusMessage(tr("connecting to debug server..."));
break;
case QAbstractSocket::ConnectedState:
{
showConnectionStatusMessage(tr("connected.\n"));
m_connectionTimer.stop();
2011-02-17 15:57:26 +01:00
//reloadEngines();
emit connected();
break;
}
case QAbstractSocket::ClosingState:
showConnectionStatusMessage(tr("closing..."));
break;
case QAbstractSocket::BoundState:
case QAbstractSocket::ListeningState:
break;
}
}
void QmlAdapter::checkConnectionState()
{
if (!isConnected()) {
closeConnection();
emit connectionStartupFailed();
}
}
void QmlAdapter::createDebuggerClients()
{
Internal::QScriptDebuggerClient *debugClient1 = new Internal::QScriptDebuggerClient(m_conn);
connect(debugClient1, SIGNAL(newStatus(QmlDebugClient::Status)),
this, SLOT(clientStatusChanged(QmlDebugClient::Status)));
connect(debugClient1, SIGNAL(newStatus(QmlDebugClient::Status)),
this, SLOT(debugClientStatusChanged(QmlDebugClient::Status)));
Internal::QmlV8DebuggerClient *debugClient2 = new Internal::QmlV8DebuggerClient(m_conn);
connect(debugClient2, SIGNAL(newStatus(QmlDebugClient::Status)),
this, SLOT(clientStatusChanged(QmlDebugClient::Status)));
connect(debugClient2, SIGNAL(newStatus(QmlDebugClient::Status)),
this, SLOT(debugClientStatusChanged(QmlDebugClient::Status)));
m_debugClients.insert(debugClient1->name(),debugClient1);
m_debugClients.insert(debugClient2->name(),debugClient2);
debugClient1->setEngine((Internal::QmlEngine*)(m_engine.data()));
debugClient2->setEngine((Internal::QmlEngine*)(m_engine.data()));
}
bool QmlAdapter::isConnected() const
{
return m_conn && m_qmlClient && m_conn->state() == QAbstractSocket::ConnectedState;
}
QmlDebugConnection *QmlAdapter::connection() const
{
return m_conn;
}
DebuggerEngine *QmlAdapter::debuggerEngine() const
{
return m_engine.data();
}
void QmlAdapter::showConnectionStatusMessage(const QString &message)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(QLatin1String("QML Debugger: ") + message, LogStatus);
}
void QmlAdapter::showConnectionErrorMessage(const QString &message)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(QLatin1String("QML Debugger: ") + message, LogError);
}
bool QmlAdapter::disableJsDebugging(bool block)
{
if (m_engine.isNull())
return block;
bool isBlocked = m_engine.data()->state() == InferiorRunOk;
if (isBlocked == block)
return block;
if (block)
m_engine.data()->continueInferior();
else
m_engine.data()->requestInterruptInferior();
return isBlocked;
}
Internal::BaseQmlDebuggerClient *QmlAdapter::activeDebuggerClient()
{
return m_qmlClient;
}
QHash<QString, Internal::BaseQmlDebuggerClient*> QmlAdapter::debuggerClients()
{
return m_debugClients;
}
QDebugMessageClient *QmlAdapter::messageClient() const
{
return m_msgClient;
}
void QmlAdapter::logServiceStatusChange(const QString &service, float version,
QmlDebugClient::Status newStatus)
{
switch (newStatus) {
case QmlDebugClient::Unavailable: {
showConnectionStatusMessage(tr("Status of '%1' Version: %2 changed to 'unavailable'.").
arg(service).arg(QString::number(version)));
break;
}
case QmlDebugClient::Enabled: {
showConnectionStatusMessage(tr("Status of '%1' Version: %2 changed to 'enabled'.").
arg(service).arg(QString::number(version)));
break;
}
case QmlDebugClient::NotConnected: {
showConnectionStatusMessage(tr("Status of '%1' Version: %2 changed to 'not connected'.").
arg(service).arg(QString::number(version)));
break;
}
}
}
void QmlAdapter::logServiceActivity(const QString &service, const QString &logMessage)
{
if (!m_engine.isNull())
m_engine.data()->showMessage(service + QLatin1Char(' ') + logMessage, LogDebug);
}
} // namespace Internal
} // namespace Debugger