forked from qt-creator/qt-creator
QmlDebug: Rework public/private QmlDebugConnection
Follow the common pattern. The private object doesn't have to be a QObject after all. Change-Id: Ife5693444fb910b6803e47ea3742815d8aa8d63e Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
This commit is contained in:
@@ -53,12 +53,10 @@ public:
|
|||||||
QmlDebugConnection *connection;
|
QmlDebugConnection *connection;
|
||||||
};
|
};
|
||||||
|
|
||||||
class QmlDebugConnectionPrivate : public QObject
|
class QmlDebugConnectionPrivate
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
QmlDebugConnectionPrivate(QmlDebugConnection *c);
|
QmlDebugConnectionPrivate();
|
||||||
QmlDebugConnection *q;
|
|
||||||
QPacketProtocol *protocol;
|
QPacketProtocol *protocol;
|
||||||
QIODevice *device; // Currently a QTcpSocket
|
QIODevice *device; // Currently a QTcpSocket
|
||||||
|
|
||||||
@@ -71,11 +69,6 @@ public:
|
|||||||
|
|
||||||
void advertisePlugins();
|
void advertisePlugins();
|
||||||
void flush();
|
void flush();
|
||||||
|
|
||||||
public slots:
|
|
||||||
void connected();
|
|
||||||
void disconnected();
|
|
||||||
void readyRead();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QString QmlDebugConnection::socketStateToString(QAbstractSocket::SocketState state)
|
QString QmlDebugConnection::socketStateToString(QAbstractSocket::SocketState state)
|
||||||
@@ -109,8 +102,8 @@ QString QmlDebugConnection::socketErrorToString(QAbstractSocket::SocketError err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QmlDebugConnectionPrivate::QmlDebugConnectionPrivate(QmlDebugConnection *c)
|
QmlDebugConnectionPrivate::QmlDebugConnectionPrivate() :
|
||||||
: QObject(c), q(c), protocol(0), device(0), gotHello(false),
|
protocol(0), server(0), device(0), gotHello(false),
|
||||||
currentDataStreamVersion(QDataStream::Qt_4_7),
|
currentDataStreamVersion(QDataStream::Qt_4_7),
|
||||||
maximumDataStreamVersion(QDataStream::Qt_DefaultCompiledVersion)
|
maximumDataStreamVersion(QDataStream::Qt_DefaultCompiledVersion)
|
||||||
{
|
{
|
||||||
@@ -127,36 +120,39 @@ void QmlDebugConnectionPrivate::advertisePlugins()
|
|||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlDebugConnectionPrivate::connected()
|
void QmlDebugConnection::socketConnected()
|
||||||
{
|
{
|
||||||
QPacket pack(currentDataStreamVersion);
|
Q_D(QmlDebugConnection);
|
||||||
pack << serverId << 0 << protocolVersion << plugins.keys() << maximumDataStreamVersion;
|
QPacket pack(d->currentDataStreamVersion);
|
||||||
protocol->send(pack.data());
|
pack << serverId << 0 << protocolVersion << d->plugins.keys() << d->maximumDataStreamVersion;
|
||||||
flush();
|
d->protocol->send(pack.data());
|
||||||
|
d->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlDebugConnectionPrivate::disconnected()
|
void QmlDebugConnection::socketDisconnected()
|
||||||
{
|
{
|
||||||
if (gotHello) {
|
Q_D(QmlDebugConnection);
|
||||||
gotHello = false;
|
if (d->gotHello) {
|
||||||
QHash<QString, QmlDebugClient*>::iterator iter = plugins.begin();
|
d->gotHello = false;
|
||||||
for (; iter != plugins.end(); ++iter)
|
QHash<QString, QmlDebugClient*>::iterator iter = d->plugins.begin();
|
||||||
|
for (; iter != d->plugins.end(); ++iter)
|
||||||
iter.value()->stateChanged(QmlDebugClient::NotConnected);
|
iter.value()->stateChanged(QmlDebugClient::NotConnected);
|
||||||
emit q->disconnected();
|
emit disconnected();
|
||||||
}
|
}
|
||||||
delete protocol;
|
delete d->protocol;
|
||||||
protocol = 0;
|
d->protocol = 0;
|
||||||
if (device) {
|
if (d->device) {
|
||||||
// Don't immediately delete it as it may do some cleanup on returning from a signal.
|
// Don't immediately delete it as it may do some cleanup on returning from a signal.
|
||||||
device->deleteLater();
|
d->device->deleteLater();
|
||||||
device = 0;
|
d->device = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlDebugConnectionPrivate::readyRead()
|
void QmlDebugConnection::protocolReadyRead()
|
||||||
{
|
{
|
||||||
if (!gotHello) {
|
Q_D(QmlDebugConnection);
|
||||||
QPacket pack(currentDataStreamVersion, protocol->read());
|
if (!d->gotHello) {
|
||||||
|
QPacket pack(d->currentDataStreamVersion, d->protocol->read());
|
||||||
QString name;
|
QString name;
|
||||||
|
|
||||||
pack >> name;
|
pack >> name;
|
||||||
@@ -181,12 +177,12 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
float pluginVersion = 1.0;
|
float pluginVersion = 1.0;
|
||||||
if (i < pluginVersionsSize)
|
if (i < pluginVersionsSize)
|
||||||
pluginVersion = pluginVersions.at(i);
|
pluginVersion = pluginVersions.at(i);
|
||||||
serverPlugins.insert(pluginNames.at(i), pluginVersion);
|
d->serverPlugins.insert(pluginNames.at(i), pluginVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pack.atEnd()) {
|
if (!pack.atEnd()) {
|
||||||
pack >> currentDataStreamVersion;
|
pack >> d->currentDataStreamVersion;
|
||||||
if (currentDataStreamVersion > maximumDataStreamVersion)
|
if (d->currentDataStreamVersion > d->maximumDataStreamVersion)
|
||||||
qWarning() << "Server returned invalid data stream version!";
|
qWarning() << "Server returned invalid data stream version!";
|
||||||
}
|
}
|
||||||
validHello = true;
|
validHello = true;
|
||||||
@@ -196,24 +192,24 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
|
|
||||||
if (!validHello) {
|
if (!validHello) {
|
||||||
qWarning("QML Debug Client: Invalid hello message");
|
qWarning("QML Debug Client: Invalid hello message");
|
||||||
QObject::disconnect(protocol, &QPacketProtocol::readyRead,
|
QObject::disconnect(d->protocol, &QPacketProtocol::readyRead,
|
||||||
this, &QmlDebugConnectionPrivate::readyRead);
|
this, &QmlDebugConnection::protocolReadyRead);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
gotHello = true;
|
d->gotHello = true;
|
||||||
|
|
||||||
QHash<QString, QmlDebugClient *>::Iterator iter = plugins.begin();
|
QHash<QString, QmlDebugClient *>::Iterator iter = d->plugins.begin();
|
||||||
for (; iter != plugins.end(); ++iter) {
|
for (; iter != d->plugins.end(); ++iter) {
|
||||||
QmlDebugClient::State newState = QmlDebugClient::Unavailable;
|
QmlDebugClient::State newState = QmlDebugClient::Unavailable;
|
||||||
if (serverPlugins.contains(iter.key()))
|
if (d->serverPlugins.contains(iter.key()))
|
||||||
newState = QmlDebugClient::Enabled;
|
newState = QmlDebugClient::Enabled;
|
||||||
iter.value()->stateChanged(newState);
|
iter.value()->stateChanged(newState);
|
||||||
}
|
}
|
||||||
emit q->connected();
|
emit connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (protocol && protocol->packetsAvailable()) {
|
while (d->protocol && d->protocol->packetsAvailable()) {
|
||||||
QPacket pack(currentDataStreamVersion, protocol->read());
|
QPacket pack(d->currentDataStreamVersion, d->protocol->read());
|
||||||
QString name;
|
QString name;
|
||||||
pack >> name;
|
pack >> name;
|
||||||
|
|
||||||
@@ -223,8 +219,8 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
|
|
||||||
if (op == 1) {
|
if (op == 1) {
|
||||||
// Service Discovery
|
// Service Discovery
|
||||||
QHash<QString, float> oldServerPlugins = serverPlugins;
|
QHash<QString, float> oldServerPlugins = d->serverPlugins;
|
||||||
serverPlugins.clear();
|
d->serverPlugins.clear();
|
||||||
|
|
||||||
QStringList pluginNames;
|
QStringList pluginNames;
|
||||||
QList<float> pluginVersions;
|
QList<float> pluginVersions;
|
||||||
@@ -238,18 +234,18 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
float pluginVersion = 1.0;
|
float pluginVersion = 1.0;
|
||||||
if (i < pluginVersionsSize)
|
if (i < pluginVersionsSize)
|
||||||
pluginVersion = pluginVersions.at(i);
|
pluginVersion = pluginVersions.at(i);
|
||||||
serverPlugins.insert(pluginNames.at(i), pluginVersion);
|
d->serverPlugins.insert(pluginNames.at(i), pluginVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<QString, QmlDebugClient *>::Iterator iter = plugins.begin();
|
QHash<QString, QmlDebugClient *>::Iterator iter = d->plugins.begin();
|
||||||
for (; iter != plugins.end(); ++iter) {
|
for (; iter != d->plugins.end(); ++iter) {
|
||||||
const QString pluginName = iter.key();
|
const QString pluginName = iter.key();
|
||||||
QmlDebugClient::State newState = QmlDebugClient::Unavailable;
|
QmlDebugClient::State newState = QmlDebugClient::Unavailable;
|
||||||
if (serverPlugins.contains(pluginName))
|
if (d->serverPlugins.contains(pluginName))
|
||||||
newState = QmlDebugClient::Enabled;
|
newState = QmlDebugClient::Enabled;
|
||||||
|
|
||||||
if (oldServerPlugins.contains(pluginName)
|
if (oldServerPlugins.contains(pluginName)
|
||||||
!= serverPlugins.contains(pluginName)) {
|
!= d->serverPlugins.contains(pluginName)) {
|
||||||
iter.value()->stateChanged(newState);
|
iter.value()->stateChanged(newState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,9 +256,8 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
QByteArray message;
|
QByteArray message;
|
||||||
pack >> message;
|
pack >> message;
|
||||||
|
|
||||||
QHash<QString, QmlDebugClient *>::Iterator iter =
|
QHash<QString, QmlDebugClient *>::Iterator iter = d->plugins.find(name);
|
||||||
plugins.find(name);
|
if (iter == d->plugins.end())
|
||||||
if (iter == plugins.end())
|
|
||||||
qWarning() << "QML Debug Client: Message received for missing plugin" << name;
|
qWarning() << "QML Debug Client: Message received for missing plugin" << name;
|
||||||
else
|
else
|
||||||
(*iter)->messageReceived(message);
|
(*iter)->messageReceived(message);
|
||||||
@@ -271,13 +266,14 @@ void QmlDebugConnectionPrivate::readyRead()
|
|||||||
}
|
}
|
||||||
|
|
||||||
QmlDebugConnection::QmlDebugConnection(QObject *parent)
|
QmlDebugConnection::QmlDebugConnection(QObject *parent)
|
||||||
: QObject(parent), d(new QmlDebugConnectionPrivate(this))
|
: QObject(parent), d_ptr(new QmlDebugConnectionPrivate)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
QmlDebugConnection::~QmlDebugConnection()
|
QmlDebugConnection::~QmlDebugConnection()
|
||||||
{
|
{
|
||||||
d->disconnected();
|
Q_D(QmlDebugConnection);
|
||||||
|
socketDisconnected();
|
||||||
QHash<QString, QmlDebugClient*>::iterator iter = d->plugins.begin();
|
QHash<QString, QmlDebugClient*>::iterator iter = d->plugins.begin();
|
||||||
for (; iter != d->plugins.end(); ++iter)
|
for (; iter != d->plugins.end(); ++iter)
|
||||||
iter.value()->d_func()->connection = 0;
|
iter.value()->d_func()->connection = 0;
|
||||||
@@ -285,23 +281,27 @@ QmlDebugConnection::~QmlDebugConnection()
|
|||||||
|
|
||||||
bool QmlDebugConnection::isConnected() const
|
bool QmlDebugConnection::isConnected() const
|
||||||
{
|
{
|
||||||
|
Q_D(const QmlDebugConnection);
|
||||||
// gotHello can only be set if the connection is open.
|
// gotHello can only be set if the connection is open.
|
||||||
return d->gotHello;
|
return d->gotHello;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QmlDebugConnection::isConnecting() const
|
bool QmlDebugConnection::isConnecting() const
|
||||||
{
|
{
|
||||||
return !isConnected() && d->device;
|
Q_D(const QmlDebugConnection);
|
||||||
|
return !d->gotHello && d->device;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlDebugConnection::close()
|
void QmlDebugConnection::close()
|
||||||
{
|
{
|
||||||
|
Q_D(QmlDebugConnection);
|
||||||
if (d->device && d->device->isOpen())
|
if (d->device && d->device->isOpen())
|
||||||
d->device->close(); // will trigger disconnected() at some point.
|
d->device->close(); // will trigger disconnected() at some point.
|
||||||
}
|
}
|
||||||
|
|
||||||
float QmlDebugConnection::serviceVersion(const QString &serviceName) const
|
float QmlDebugConnection::serviceVersion(const QString &serviceName) const
|
||||||
{
|
{
|
||||||
|
Q_D(const QmlDebugConnection);
|
||||||
return d->serverPlugins.value(serviceName, -1);
|
return d->serverPlugins.value(serviceName, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,27 +316,31 @@ void QmlDebugConnectionPrivate::flush()
|
|||||||
|
|
||||||
void QmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
|
void QmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
|
||||||
{
|
{
|
||||||
d->disconnected();
|
Q_D(QmlDebugConnection);
|
||||||
QTcpSocket *socket = new QTcpSocket(d);
|
socketDisconnected();
|
||||||
|
QTcpSocket *socket = new QTcpSocket(this);
|
||||||
socket->setProxy(QNetworkProxy::NoProxy);
|
socket->setProxy(QNetworkProxy::NoProxy);
|
||||||
d->device = socket;
|
d->device = socket;
|
||||||
d->protocol = new QPacketProtocol(d->device, this);
|
d->protocol = new QPacketProtocol(socket, this);
|
||||||
connect(d->protocol, &QPacketProtocol::readyRead, d, &QmlDebugConnectionPrivate::readyRead);
|
QObject::connect(d->protocol, &QPacketProtocol::readyRead,
|
||||||
|
this, &QmlDebugConnection::protocolReadyRead);
|
||||||
connect(socket, &QAbstractSocket::stateChanged, this, &QmlDebugConnection::socketStateChanged);
|
connect(socket, &QAbstractSocket::stateChanged, this, &QmlDebugConnection::socketStateChanged);
|
||||||
connect(socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>
|
connect(socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>
|
||||||
(&QAbstractSocket::error), this, &QmlDebugConnection::socketError);
|
(&QAbstractSocket::error), this, &QmlDebugConnection::socketError);
|
||||||
connect(socket, &QAbstractSocket::connected, d, &QmlDebugConnectionPrivate::connected);
|
connect(socket, &QAbstractSocket::connected, this, &QmlDebugConnection::socketConnected);
|
||||||
connect(socket, &QAbstractSocket::disconnected, d, &QmlDebugConnectionPrivate::disconnected);
|
connect(socket, &QAbstractSocket::disconnected, this, &QmlDebugConnection::socketDisconnected);
|
||||||
socket->connectToHost(hostName, port);
|
socket->connectToHost(hostName, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
int QmlDebugConnection::currentDataStreamVersion() const
|
int QmlDebugConnection::currentDataStreamVersion() const
|
||||||
{
|
{
|
||||||
|
Q_D(const QmlDebugConnection);
|
||||||
return d->currentDataStreamVersion;
|
return d->currentDataStreamVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlDebugConnection::setMaximumDataStreamVersion(int maximumVersion)
|
void QmlDebugConnection::setMaximumDataStreamVersion(int maximumVersion)
|
||||||
{
|
{
|
||||||
|
Q_D(QmlDebugConnection);
|
||||||
d->maximumDataStreamVersion = maximumVersion;
|
d->maximumDataStreamVersion = maximumVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,5 +435,3 @@ void QmlDebugClient::messageReceived(const QByteArray &)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QmlDebug
|
} // namespace QmlDebug
|
||||||
|
|
||||||
#include <qmldebugclient.moc>
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ class QMLDEBUG_EXPORT QmlDebugConnection : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_DISABLE_COPY(QmlDebugConnection)
|
Q_DISABLE_COPY(QmlDebugConnection)
|
||||||
|
Q_DECLARE_PRIVATE(QmlDebugConnection)
|
||||||
public:
|
public:
|
||||||
QmlDebugConnection(QObject * = 0);
|
QmlDebugConnection(QObject * = 0);
|
||||||
~QmlDebugConnection();
|
~QmlDebugConnection();
|
||||||
@@ -66,9 +67,13 @@ signals:
|
|||||||
void socketError(QAbstractSocket::SocketError error);
|
void socketError(QAbstractSocket::SocketError error);
|
||||||
void socketStateChanged(QAbstractSocket::SocketState state);
|
void socketStateChanged(QAbstractSocket::SocketState state);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void socketConnected();
|
||||||
|
void socketDisconnected();
|
||||||
|
void protocolReadyRead();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class QmlDebugConnectionPrivate;
|
QScopedPointer<QmlDebugConnectionPrivate> d_ptr;
|
||||||
QmlDebugConnectionPrivate *d;
|
|
||||||
friend class QmlDebugClient;
|
friend class QmlDebugClient;
|
||||||
friend class QmlDebugClientPrivate;
|
friend class QmlDebugClientPrivate;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user