2010-08-13 14:18:10 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "qmladapter.h"
|
|
|
|
|
#include "qmldebuggerclient.h"
|
|
|
|
|
#include "qmljsprivateapi.h"
|
|
|
|
|
|
|
|
|
|
#include "debuggerengine.h"
|
|
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
#include <QtCore/QDebug>
|
2010-08-13 14:18:10 +02:00
|
|
|
|
|
|
|
|
namespace Debugger {
|
2010-09-13 13:30:35 +02:00
|
|
|
|
|
|
|
|
struct QmlAdapterPrivate {
|
|
|
|
|
explicit QmlAdapterPrivate(DebuggerEngine *engine, QmlAdapter *q);
|
|
|
|
|
|
|
|
|
|
QWeakPointer<DebuggerEngine> m_engine;
|
|
|
|
|
Internal::QmlDebuggerClient *m_qmlClient;
|
|
|
|
|
QDeclarativeEngineDebug *m_mainClient;
|
|
|
|
|
|
|
|
|
|
QTimer *m_connectionTimer;
|
|
|
|
|
int m_connectionAttempts;
|
|
|
|
|
int m_maxConnectionAttempts;
|
|
|
|
|
QDeclarativeDebugConnection *m_conn;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QmlAdapterPrivate::QmlAdapterPrivate(DebuggerEngine *engine, QmlAdapter *q) :
|
|
|
|
|
m_engine(engine)
|
|
|
|
|
, m_qmlClient(0)
|
|
|
|
|
, m_mainClient(0)
|
|
|
|
|
, m_connectionTimer(new QTimer(q))
|
|
|
|
|
, m_connectionAttempts(0)
|
|
|
|
|
, m_conn(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
2010-08-13 14:18:10 +02:00
|
|
|
|
|
|
|
|
QmlAdapter::QmlAdapter(DebuggerEngine *engine, QObject *parent)
|
2010-09-13 13:30:35 +02:00
|
|
|
: QObject(parent), d(new QmlAdapterPrivate(engine, this))
|
|
|
|
|
{
|
|
|
|
|
d->m_connectionTimer->setInterval(200);
|
|
|
|
|
connect(d->m_connectionTimer, SIGNAL(timeout()), SLOT(pollInferior()));
|
|
|
|
|
}
|
2010-08-13 14:18:10 +02:00
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
QmlAdapter::~QmlAdapter()
|
2010-08-13 14:18:10 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::beginConnection()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_connectionAttempts = 0;
|
|
|
|
|
d->m_connectionTimer->start();
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-18 13:54:12 +02:00
|
|
|
void QmlAdapter::pauseConnection()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_connectionTimer->stop();
|
2010-08-18 13:54:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::closeConnection()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
if (d->m_connectionTimer->isActive()) {
|
|
|
|
|
d->m_connectionTimer->stop();
|
2010-08-18 13:54:12 +02:00
|
|
|
} else {
|
2010-09-13 13:30:35 +02:00
|
|
|
if (d->m_conn) {
|
|
|
|
|
d->m_conn->disconnectFromHost();
|
2010-08-18 13:54:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 14:18:10 +02:00
|
|
|
void QmlAdapter::pollInferior()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
++d->m_connectionAttempts;
|
2010-08-13 14:18:10 +02:00
|
|
|
|
|
|
|
|
if (connectToViewer()) {
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_connectionTimer->stop();
|
|
|
|
|
d->m_connectionAttempts = 0;
|
|
|
|
|
} else if (d->m_connectionAttempts == d->m_maxConnectionAttempts) {
|
2010-08-13 14:18:10 +02:00
|
|
|
emit connectionStartupFailed();
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_connectionTimer->stop();
|
|
|
|
|
d->m_connectionAttempts = 0;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlAdapter::connectToViewer()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
if (d->m_engine.isNull() || (d->m_conn && d->m_conn->state() != QAbstractSocket::UnconnectedState))
|
2010-08-13 14:18:10 +02:00
|
|
|
return false;
|
|
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_conn = new QDeclarativeDebugConnection(this);
|
|
|
|
|
connect(d->m_conn, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
|
2010-08-13 14:18:10 +02:00
|
|
|
SLOT(connectionStateChanged()));
|
2010-09-13 13:30:35 +02:00
|
|
|
connect(d->m_conn, SIGNAL(error(QAbstractSocket::SocketError)),
|
2010-08-27 13:11:55 +02:00
|
|
|
SLOT(connectionErrorOccurred(QAbstractSocket::SocketError)));
|
2010-08-13 14:18:10 +02:00
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
QString address = d->m_engine.data()->startParameters().qmlServerAddress;
|
|
|
|
|
QString port = QString::number(d->m_engine.data()->startParameters().qmlServerPort);
|
2010-08-13 14:18:10 +02:00
|
|
|
showConnectionStatusMessage(tr("Connect to debug server %1:%2").arg(address).arg(port));
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_conn->connectToHost(d->m_engine.data()->startParameters().qmlServerAddress,
|
|
|
|
|
d->m_engine.data()->startParameters().qmlServerPort);
|
|
|
|
|
|
2010-08-13 14:18:10 +02:00
|
|
|
|
|
|
|
|
// blocks until connected; if no connection is available, will fail immediately
|
2010-09-13 13:30:35 +02:00
|
|
|
if (!d->m_conn->waitForConnected())
|
2010-08-13 14:18:10 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-27 13:11:55 +02:00
|
|
|
void QmlAdapter::connectionErrorOccurred(QAbstractSocket::SocketError socketError)
|
2010-08-13 14:18:10 +02:00
|
|
|
{
|
|
|
|
|
showConnectionErrorMessage(tr("Error: (%1) %2", "%1=error code, %2=error message")
|
2010-09-13 13:30:35 +02:00
|
|
|
.arg(d->m_conn->error()).arg(d->m_conn->errorString()));
|
2010-08-13 14:18:10 +02:00
|
|
|
|
|
|
|
|
// this is only an error if we are already connected and something goes wrong.
|
|
|
|
|
if (isConnected())
|
2010-08-27 13:11:55 +02:00
|
|
|
emit connectionError(socketError);
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-30 14:05:20 +02:00
|
|
|
void QmlAdapter::clientStatusChanged(QDeclarativeDebugClient::Status status)
|
|
|
|
|
{
|
|
|
|
|
QString serviceName;
|
|
|
|
|
if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
|
|
|
|
|
serviceName = client->name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logServiceStatusChange(serviceName, status);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 14:18:10 +02:00
|
|
|
void QmlAdapter::connectionStateChanged()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
switch (d->m_conn->state()) {
|
2010-08-13 14:18:10 +02:00
|
|
|
case QAbstractSocket::UnconnectedState:
|
|
|
|
|
{
|
|
|
|
|
showConnectionStatusMessage(tr("disconnected.\n\n"));
|
|
|
|
|
emit disconnected();
|
|
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
if (!d->m_mainClient) {
|
|
|
|
|
d->m_mainClient = new QDeclarativeEngineDebug(d->m_conn, this);
|
2010-09-30 14:05:20 +02:00
|
|
|
logServiceStatusChange(QLatin1String("QmlObserver"), static_cast<QDeclarativeDebugClient::Status>(d->m_mainClient->status()));
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createDebuggerClient();
|
|
|
|
|
//reloadEngines();
|
|
|
|
|
emit connected();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case QAbstractSocket::ClosingState:
|
|
|
|
|
showConnectionStatusMessage(tr("closing..."));
|
|
|
|
|
break;
|
|
|
|
|
case QAbstractSocket::BoundState:
|
|
|
|
|
case QAbstractSocket::ListeningState:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::createDebuggerClient()
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_qmlClient = new Internal::QmlDebuggerClient(d->m_conn);
|
2010-08-13 14:18:10 +02:00
|
|
|
|
2010-09-30 14:05:20 +02:00
|
|
|
connect(d->m_qmlClient, SIGNAL(newStatus(QDeclarativeDebugClient::Status)),
|
|
|
|
|
this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
|
2010-09-13 13:30:35 +02:00
|
|
|
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)));
|
2010-08-13 14:18:10 +02:00
|
|
|
|
2010-09-30 14:05:20 +02:00
|
|
|
logServiceStatusChange(d->m_qmlClient->name(), d->m_qmlClient->status());
|
|
|
|
|
|
2010-08-13 14:18:10 +02:00
|
|
|
//engine->startSuccessful(); // FIXME: AAA: port to new debugger states
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlAdapter::isConnected() const
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
return d->m_conn && d->m_qmlClient && d->m_conn->state() == QAbstractSocket::ConnectedState;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlAdapter::isUnconnected() const
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
return !d->m_conn || d->m_conn->state() == QAbstractSocket::UnconnectedState;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDeclarativeEngineDebug *QmlAdapter::client() const
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
return d->m_mainClient;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDeclarativeDebugConnection *QmlAdapter::connection() const
|
|
|
|
|
{
|
|
|
|
|
if (!isConnected())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2010-09-13 13:30:35 +02:00
|
|
|
return d->m_conn;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::showConnectionStatusMessage(const QString &message)
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
if (!d->m_engine.isNull())
|
|
|
|
|
d->m_engine.data()->showMessage(QLatin1String("QmlJSDebugger: ") + message, LogStatus);
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::showConnectionErrorMessage(const QString &message)
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
if (!d->m_engine.isNull())
|
|
|
|
|
d->m_engine.data()->showMessage(QLatin1String("QmlJSDebugger: ") + message, LogError);
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlAdapter::setMaxConnectionAttempts(int maxAttempts)
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_maxConnectionAttempts = maxAttempts;
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
void QmlAdapter::setConnectionAttemptInterval(int interval)
|
|
|
|
|
{
|
2010-09-13 13:30:35 +02:00
|
|
|
d->m_connectionTimer->setInterval(interval);
|
2010-08-13 14:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-30 14:05:20 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 14:18:10 +02:00
|
|
|
} // namespace Debugger
|