#include "qmldebug.h" #include "qmldebugclient.h" #include #include class QmlEngineDebugClient : public QmlDebugClient { public: QmlEngineDebugClient(QmlDebugConnection *client, QmlEngineDebugPrivate *p); protected: virtual void messageReceived(const QByteArray &); private: QmlEngineDebugPrivate *priv; }; class QmlEngineDebugPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QmlEngineDebug) public: QmlEngineDebugPrivate(QmlDebugConnection *); void message(const QByteArray &); QmlEngineDebugClient client; int nextId; int getId(); void decode(QDataStream &, QmlDebugContextReference &); void decode(QDataStream &, QmlDebugObjectReference &, bool simple); static void remove(QmlEngineDebug *, QmlDebugEnginesQuery *); static void remove(QmlEngineDebug *, QmlDebugRootContextQuery *); static void remove(QmlEngineDebug *, QmlDebugObjectQuery *); QHash enginesQuery; QHash rootContextQuery; QHash objectQuery; }; QmlEngineDebugClient::QmlEngineDebugClient(QmlDebugConnection *client, QmlEngineDebugPrivate *p) : QmlDebugClient(QLatin1String("QmlEngine"), client), priv(p) { setEnabled(true); } void QmlEngineDebugClient::messageReceived(const QByteArray &data) { priv->message(data); } QmlEngineDebugPrivate::QmlEngineDebugPrivate(QmlDebugConnection *c) : client(c, this), nextId(0) { } int QmlEngineDebugPrivate::getId() { return nextId++; } void QmlEngineDebugPrivate::remove(QmlEngineDebug *c, QmlDebugEnginesQuery *q) { QmlEngineDebugPrivate *p = (QmlEngineDebugPrivate *)QObjectPrivate::get(c); p->enginesQuery.remove(q->m_queryId); } void QmlEngineDebugPrivate::remove(QmlEngineDebug *c, QmlDebugRootContextQuery *q) { QmlEngineDebugPrivate *p = (QmlEngineDebugPrivate *)QObjectPrivate::get(c); p->rootContextQuery.remove(q->m_queryId); } void QmlEngineDebugPrivate::remove(QmlEngineDebug *c, QmlDebugObjectQuery *q) { QmlEngineDebugPrivate *p = (QmlEngineDebugPrivate *)QObjectPrivate::get(c); p->objectQuery.remove(q->m_queryId); } Q_DECLARE_METATYPE(QmlDebugObjectReference); void QmlEngineDebugPrivate::decode(QDataStream &ds, QmlDebugObjectReference &o, bool simple) { QmlEngineDebugServer::QmlObjectData data; ds >> data; o.m_debugId = data.objectId; o.m_class = data.objectType; o.m_name = data.objectName; o.m_source.m_url = data.url; o.m_source.m_lineNumber = data.lineNumber; o.m_source.m_columnNumber = data.columnNumber; if (simple) return; int propCount; ds >> propCount; for (int ii = 0; ii < propCount; ++ii) { QmlEngineDebugServer::QmlObjectProperty data; ds >> data; QmlDebugPropertyReference prop; prop.m_name = data.name; if (data.type == QmlEngineDebugServer::QmlObjectProperty::Basic) prop.m_value = data.value; else if (data.type == QmlEngineDebugServer::QmlObjectProperty::Object) { QmlDebugObjectReference obj; obj.m_debugId = prop.m_value.toInt(); prop.m_value = qVariantFromValue(obj); } o.m_properties << prop; } int childCount; bool recur; ds >> childCount >> recur; for (int ii = 0; ii < childCount; ++ii) { o.m_children.append(QmlDebugObjectReference()); decode(ds, o.m_children.last(), !recur); } } void QmlEngineDebugPrivate::decode(QDataStream &ds, QmlDebugContextReference &c) { ds >> c.m_name >> c.m_debugId; int contextCount; ds >> contextCount; for (int ii = 0; ii < contextCount; ++ii) { c.m_contexts.append(QmlDebugContextReference()); decode(ds, c.m_contexts.last()); } int objectCount; ds >> objectCount; for (int ii = 0; ii < objectCount; ++ii) { QmlDebugObjectReference obj; decode(ds, obj, true); c.m_objects << obj; } } void QmlEngineDebugPrivate::message(const QByteArray &data) { QDataStream ds(data); QByteArray type; ds >> type; if (type == "LIST_ENGINES_R") { int queryId; ds >> queryId; QmlDebugEnginesQuery *query = enginesQuery.value(queryId); if (!query) return; enginesQuery.remove(queryId); int count; ds >> count; for (int ii = 0; ii < count; ++ii) { QmlDebugEngineReference ref; ds >> ref.m_name; ds >> ref.m_debugId; query->m_engines << ref; } query->m_client = 0; query->setState(QmlDebugQuery::Completed); } else if (type == "LIST_OBJECTS_R") { int queryId; ds >> queryId; QmlDebugRootContextQuery *query = rootContextQuery.value(queryId); if (!query) return; rootContextQuery.remove(queryId); if (!ds.atEnd()) decode(ds, query->m_context); query->m_client = 0; query->setState(QmlDebugQuery::Completed); } else if (type == "FETCH_OBJECT_R") { int queryId; ds >> queryId; QmlDebugObjectQuery *query = objectQuery.value(queryId); if (!query) return; objectQuery.remove(queryId); if (!ds.atEnd()) decode(ds, query->m_object, false); query->m_client = 0; query->setState(QmlDebugQuery::Completed); } } QmlEngineDebug::QmlEngineDebug(QmlDebugConnection *client, QObject *parent) : QObject(*(new QmlEngineDebugPrivate(client)), parent) { } QmlDebugWatch *QmlEngineDebug::addWatch(const QmlDebugPropertyReference &, QObject *) { qWarning("QmlEngineDebug::addWatch(): Not implemented"); return 0; } QmlDebugWatch *QmlEngineDebug::addWatch(const QmlDebugContextReference &, const QString &, QObject *) { qWarning("QmlEngineDebug::addWatch(): Not implemented"); return 0; } QmlDebugWatch *QmlEngineDebug::addWatch(const QmlDebugObjectReference &, const QString &, QObject *) { qWarning("QmlEngineDebug::addWatch(): Not implemented"); return 0; } QmlDebugWatch *QmlEngineDebug::addWatch(const QmlDebugObjectReference &, QObject *) { qWarning("QmlEngineDebug::addWatch(): Not implemented"); return 0; } QmlDebugWatch *QmlEngineDebug::addWatch(const QmlDebugFileReference &, QObject *) { qWarning("QmlEngineDebug::addWatch(): Not implemented"); return 0; } QmlDebugEnginesQuery *QmlEngineDebug::queryAvailableEngines(QObject *parent) { Q_D(QmlEngineDebug); QmlDebugEnginesQuery *query = new QmlDebugEnginesQuery(parent); if (d->client.isConnected()) { query->m_client = this; int queryId = d->getId(); query->m_queryId = queryId; d->enginesQuery.insert(queryId, query); QByteArray message; QDataStream ds(&message, QIODevice::WriteOnly); ds << QByteArray("LIST_ENGINES") << queryId; d->client.sendMessage(message); } else { query->m_state = QmlDebugQuery::Error; } return query; } QmlDebugRootContextQuery *QmlEngineDebug::queryRootContexts(const QmlDebugEngineReference &engine, QObject *parent) { Q_D(QmlEngineDebug); QmlDebugRootContextQuery *query = new QmlDebugRootContextQuery(parent); if (d->client.isConnected() && engine.debugId() != -1) { query->m_client = this; int queryId = d->getId(); query->m_queryId = queryId; d->rootContextQuery.insert(queryId, query); QByteArray message; QDataStream ds(&message, QIODevice::WriteOnly); ds << QByteArray("LIST_OBJECTS") << queryId << engine.debugId(); d->client.sendMessage(message); } else { query->m_state = QmlDebugQuery::Error; } return query; } QmlDebugObjectQuery *QmlEngineDebug::queryObject(const QmlDebugObjectReference &object, QObject *parent) { Q_D(QmlEngineDebug); QmlDebugObjectQuery *query = new QmlDebugObjectQuery(parent); if (d->client.isConnected() && object.debugId() != -1) { query->m_client = this; int queryId = d->getId(); query->m_queryId = queryId; d->objectQuery.insert(queryId, query); QByteArray message; QDataStream ds(&message, QIODevice::WriteOnly); ds << QByteArray("FETCH_OBJECT") << queryId << object.debugId() << false; d->client.sendMessage(message); } else { query->m_state = QmlDebugQuery::Error; } return query; } QmlDebugObjectQuery *QmlEngineDebug::queryObjectRecursive(const QmlDebugObjectReference &object, QObject *parent) { Q_D(QmlEngineDebug); QmlDebugObjectQuery *query = new QmlDebugObjectQuery(parent); if (d->client.isConnected() && object.debugId() != -1) { query->m_client = this; int queryId = d->getId(); query->m_queryId = queryId; d->objectQuery.insert(queryId, query); QByteArray message; QDataStream ds(&message, QIODevice::WriteOnly); ds << QByteArray("FETCH_OBJECT") << queryId << object.debugId() << true; d->client.sendMessage(message); } else { query->m_state = QmlDebugQuery::Error; } return query; } QmlDebugQuery::QmlDebugQuery(QObject *parent) : QObject(parent), m_state(Waiting) { } QmlDebugQuery::State QmlDebugQuery::state() const { return m_state; } bool QmlDebugQuery::isWaiting() const { return m_state == Waiting; } void QmlDebugQuery::setState(State s) { if (m_state == s) return; m_state = s; emit stateChanged(m_state); } QmlDebugEnginesQuery::QmlDebugEnginesQuery(QObject *parent) : QmlDebugQuery(parent), m_client(0), m_queryId(-1) { } QmlDebugEnginesQuery::~QmlDebugEnginesQuery() { if (m_client && m_queryId != -1) QmlEngineDebugPrivate::remove(m_client, this); } QList QmlDebugEnginesQuery::engines() const { return m_engines; } QmlDebugRootContextQuery::QmlDebugRootContextQuery(QObject *parent) : QmlDebugQuery(parent), m_client(0), m_queryId(-1) { } QmlDebugRootContextQuery::~QmlDebugRootContextQuery() { if (m_client && m_queryId != -1) QmlEngineDebugPrivate::remove(m_client, this); } QmlDebugContextReference QmlDebugRootContextQuery::rootContext() const { return m_context; } QmlDebugObjectQuery::QmlDebugObjectQuery(QObject *parent) : QmlDebugQuery(parent), m_client(0), m_queryId(-1) { } QmlDebugObjectQuery::~QmlDebugObjectQuery() { if (m_client && m_queryId != -1) QmlEngineDebugPrivate::remove(m_client, this); } QmlDebugObjectReference QmlDebugObjectQuery::object() const { return m_object; } QmlDebugEngineReference::QmlDebugEngineReference() : m_debugId(-1) { } QmlDebugEngineReference::QmlDebugEngineReference(int debugId) : m_debugId(debugId) { } QmlDebugEngineReference::QmlDebugEngineReference(const QmlDebugEngineReference &o) : m_debugId(o.m_debugId), m_name(o.m_name) { } QmlDebugEngineReference & QmlDebugEngineReference::operator=(const QmlDebugEngineReference &o) { m_debugId = o.m_debugId; m_name = o.m_name; return *this; } int QmlDebugEngineReference::debugId() const { return m_debugId; } QString QmlDebugEngineReference::name() const { return m_name; } QmlDebugObjectReference::QmlDebugObjectReference() : m_debugId(-1) { } QmlDebugObjectReference::QmlDebugObjectReference(int debugId) : m_debugId(debugId) { } QmlDebugObjectReference::QmlDebugObjectReference(const QmlDebugObjectReference &o) : m_debugId(o.m_debugId), m_class(o.m_class), m_name(o.m_name), m_source(o.m_source), m_properties(o.m_properties), m_children(o.m_children) { } QmlDebugObjectReference & QmlDebugObjectReference::operator=(const QmlDebugObjectReference &o) { m_debugId = o.m_debugId; m_class = o.m_class; m_name = o.m_name; m_source = o.m_source; m_properties = o.m_properties; m_children = o.m_children; return *this; } int QmlDebugObjectReference::debugId() const { return m_debugId; } QString QmlDebugObjectReference::className() const { return m_class; } QString QmlDebugObjectReference::name() const { return m_name; } QmlDebugFileReference QmlDebugObjectReference::source() const { return m_source; } QList QmlDebugObjectReference::properties() const { return m_properties; } QList QmlDebugObjectReference::children() const { return m_children; } QmlDebugContextReference::QmlDebugContextReference() : m_debugId(-1) { } QmlDebugContextReference::QmlDebugContextReference(const QmlDebugContextReference &o) : m_debugId(o.m_debugId), m_name(o.m_name), m_objects(o.m_objects), m_contexts(o.m_contexts) { } QmlDebugContextReference &QmlDebugContextReference::operator=(const QmlDebugContextReference &o) { m_debugId = o.m_debugId; m_name = o.m_name; m_objects = o.m_objects; m_contexts = o.m_contexts; return *this; } int QmlDebugContextReference::debugId() const { return m_debugId; } QString QmlDebugContextReference::name() const { return m_name; } QList QmlDebugContextReference::objects() const { return m_objects; } QList QmlDebugContextReference::contexts() const { return m_contexts; } QmlDebugFileReference::QmlDebugFileReference() : m_lineNumber(-1), m_columnNumber(-1) { } QmlDebugFileReference::QmlDebugFileReference(const QmlDebugFileReference &o) : m_url(o.m_url), m_lineNumber(o.m_lineNumber), m_columnNumber(o.m_columnNumber) { } QmlDebugFileReference &QmlDebugFileReference::operator=(const QmlDebugFileReference &o) { m_url = o.m_url; m_lineNumber = o.m_lineNumber; m_columnNumber = o.m_columnNumber; return *this; } QUrl QmlDebugFileReference::url() const { return m_url; } void QmlDebugFileReference::setUrl(const QUrl &u) { m_url = u; } int QmlDebugFileReference::lineNumber() const { return m_lineNumber; } void QmlDebugFileReference::setLineNumber(int l) { m_lineNumber = l; } int QmlDebugFileReference::columnNumber() const { return m_columnNumber; } void QmlDebugFileReference::setColumnNumber(int c) { m_columnNumber = c; } QmlDebugPropertyReference::QmlDebugPropertyReference() { } QmlDebugPropertyReference::QmlDebugPropertyReference(const QmlDebugPropertyReference &o) : m_name(o.m_name), m_value(o.m_value), m_binding(o.m_binding) { } QmlDebugPropertyReference &QmlDebugPropertyReference::operator=(const QmlDebugPropertyReference &o) { m_name = o.m_name; m_value = o.m_value; m_binding = o.m_binding; return *this; } QString QmlDebugPropertyReference::name() const { return m_name; } QVariant QmlDebugPropertyReference::value() const { return m_value; } QString QmlDebugPropertyReference::binding() const { return m_binding; }