1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmljsclientproxy.h"
34 #include "qmljsprivateapi.h"
35 #include "qmljsobserverclient.h"
36 #include "qmljsinspector.h"
38 #include <debugger/debuggerplugin.h>
39 #include <debugger/debuggerrunner.h>
40 #include <debugger/qml/qmlengine.h>
41 #include <debugger/qml/qmladapter.h>
42 #include <extensionsystem/pluginmanager.h>
43 #include <utils/qtcassert.h>
44 #include <projectexplorer/project.h>
46 #include <QtCore/QUrl>
47 #include <QtNetwork/QAbstractSocket>
49 using namespace QmlJSInspector::Internal;
51 ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
58 , m_isConnected(false)
60 m_requestObjectsTimer.setSingleShot(true);
61 m_requestObjectsTimer.setInterval(3000);
62 connect(&m_requestObjectsTimer, SIGNAL(timeout()), this, SLOT(refreshObjectTree()));
66 void ClientProxy::connectToServer()
68 m_engineClient = new QDeclarativeEngineDebug(m_adapter->connection(), this);
70 connect(m_engineClient, SIGNAL(newObjects()), this, SLOT(newObjects()));
72 m_observerClient = new QmlJSObserverClient(m_adapter->connection(), this);
74 connect(m_observerClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
75 this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
76 connect(m_observerClient, SIGNAL(currentObjectsChanged(QList<int>)),
77 SLOT(onCurrentObjectsChanged(QList<int>)));
78 connect(m_observerClient, SIGNAL(colorPickerActivated()),
79 SIGNAL(colorPickerActivated()));
80 connect(m_observerClient, SIGNAL(zoomToolActivated()),
81 SIGNAL(zoomToolActivated()));
82 connect(m_observerClient, SIGNAL(selectToolActivated()),
83 SIGNAL(selectToolActivated()));
84 connect(m_observerClient, SIGNAL(selectMarqueeToolActivated()),
85 SIGNAL(selectMarqueeToolActivated()));
86 connect(m_observerClient, SIGNAL(animationSpeedChanged(qreal)),
87 SIGNAL(animationSpeedChanged(qreal)));
88 connect(m_observerClient, SIGNAL(animationPausedChanged(bool)),
89 SIGNAL(animationPausedChanged(bool)));
90 connect(m_observerClient, SIGNAL(designModeBehaviorChanged(bool)),
91 SIGNAL(designModeBehaviorChanged(bool)));
92 connect(m_observerClient, SIGNAL(showAppOnTopChanged(bool)),
93 SIGNAL(showAppOnTopChanged(bool)));
94 connect(m_observerClient, SIGNAL(reloaded()), this,
95 SIGNAL(serverReloaded()));
96 connect(m_observerClient, SIGNAL(selectedColorChanged(QColor)),
97 SIGNAL(selectedColorChanged(QColor)));
98 connect(m_observerClient, SIGNAL(contextPathUpdated(QStringList)),
99 SIGNAL(contextPathUpdated(QStringList)));
100 connect(m_observerClient, SIGNAL(logActivity(QString,QString)),
101 m_adapter, SLOT(logServiceActivity(QString,QString)));
106 void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
109 if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
110 serviceName = client->name();
113 m_adapter->logServiceStatusChange(serviceName, status);
118 void ClientProxy::refreshObjectTree()
120 if (!m_contextQuery) {
121 m_requestObjectsTimer.stop();
122 qDeleteAll(m_objectTreeQuery);
123 m_objectTreeQuery.clear();
124 queryEngineContext(m_engines.value(0).debugId());
128 void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds, bool requestIfNeeded)
130 QList<QDeclarativeDebugObjectReference> selectedItems;
132 foreach (int debugId, debugIds) {
133 QDeclarativeDebugObjectReference ref = objectReferenceForId(debugId);
134 if (ref.debugId() != -1) {
135 selectedItems << ref;
136 } else if (requestIfNeeded) {
137 // ### FIXME right now, there's no way in the protocol to
138 // a) get some item and know its parent (although that's possible
139 // by adding it to a separate plugin)
140 // b) add children to part of an existing tree.
141 // So the only choice that remains is to update the complete
142 // tree when we have an unknown debug id.
147 emit selectedItemsChanged(selectedItems);
150 void ClientProxy::setSelectedItemsByObjectId(const QList<QDeclarativeDebugObjectReference> &objectRefs)
155 foreach (const QDeclarativeDebugObjectReference &ref, objectRefs) {
156 debugIds << ref.debugId();
159 m_observerClient->setCurrentObjects(debugIds);
163 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId) const
165 foreach (const QDeclarativeDebugObjectReference& it, m_rootObjects) {
166 QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, it);
167 if (result.debugId() == debugId)
170 return QDeclarativeDebugObjectReference();
173 void ClientProxy::log(LogDirection direction, const QString &message)
176 if (direction == LogSend) {
179 msg += " receiving ";
183 m_adapter->logServiceActivity("QDeclarativeDebug", msg);
186 QList<QDeclarativeDebugObjectReference> QmlJSInspector::Internal::ClientProxy::rootObjectReference() const
188 return m_rootObjects;
191 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId,
192 const QDeclarativeDebugObjectReference &objectRef) const
194 if (objectRef.debugId() == debugId)
197 foreach (const QDeclarativeDebugObjectReference &child, objectRef.children()) {
198 QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, child);
199 if (result.debugId() == debugId)
203 return QDeclarativeDebugObjectReference();
206 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(const QString &objectId) const
208 if (!objectId.isEmpty() && objectId[0].isLower()) {
209 const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
210 foreach (const QDeclarativeDebugObjectReference &ref, refs) {
211 if (ref.idString() == objectId)
215 return QDeclarativeDebugObjectReference();
218 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForLocation(const int line, const int column) const
220 const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
221 foreach (const QDeclarativeDebugObjectReference &ref, refs) {
222 if (ref.source().lineNumber() == line && ref.source().columnNumber() == column)
226 return QDeclarativeDebugObjectReference();
229 QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences() const
231 QList<QDeclarativeDebugObjectReference> result;
232 foreach (const QDeclarativeDebugObjectReference &it, m_rootObjects) {
233 result.append(objectReferences(it));
238 QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences(const QDeclarativeDebugObjectReference &objectRef) const
240 QList<QDeclarativeDebugObjectReference> result;
241 result.append(objectRef);
243 foreach (const QDeclarativeDebugObjectReference &child, objectRef.children()) {
244 result.append(objectReferences(child));
250 bool ClientProxy::setBindingForObject(int objectDebugId,
251 const QString &propertyName,
252 const QVariant &value,
255 if (objectDebugId == -1)
258 if (propertyName == QLatin1String("id"))
259 return false; // Crashes the QMLViewer.
264 log(LogSend, QString("SET_BINDING %1 %2 %3 %4").arg(QString::number(objectDebugId), propertyName, value.toString(), QString(isLiteralValue ? "true" : "false")));
266 bool result = m_engineClient->setBindingForObject(objectDebugId, propertyName, value.toString(), isLiteralValue);
269 log(LogSend, QString("failed!"));
274 bool ClientProxy::setMethodBodyForObject(int objectDebugId, const QString &methodName, const QString &methodBody)
276 if (objectDebugId == -1)
282 log(LogSend, QString("SET_METHOD_BODY %1 %2 %3").arg(QString::number(objectDebugId), methodName, methodBody));
284 bool result = m_engineClient->setMethodBody(objectDebugId, methodName, methodBody);
287 log(LogSend, QString("failed!"));
292 bool ClientProxy::resetBindingForObject(int objectDebugId, const QString& propertyName)
294 if (objectDebugId == -1)
300 log(LogSend, QString("RESET_BINDING %1 %2").arg(QString::number(objectDebugId), propertyName));
302 bool result = m_engineClient->resetBindingForObject(objectDebugId, propertyName);
305 log(LogSend, QString("failed!"));
310 QDeclarativeDebugExpressionQuery *ClientProxy::queryExpressionResult(int objectDebugId, const QString &expr)
312 if (objectDebugId == -1)
318 bool block = m_adapter->disableJsDebugging(true);
320 log(LogSend, QString("EVAL_EXPRESSION %1 %2").arg(QString::number(objectDebugId), expr));
321 QDeclarativeDebugExpressionQuery *query
322 = m_engineClient->queryExpressionResult(objectDebugId, expr, m_engineClient);
324 m_adapter->disableJsDebugging(block);
328 void ClientProxy::clearComponentCache()
331 m_observerClient->clearComponentCache();
334 bool ClientProxy::addObjectWatch(int objectDebugId)
336 if (objectDebugId == -1)
343 if (m_objectWatches.keys().contains(objectDebugId))
346 QDeclarativeDebugObjectReference ref = objectReferenceForId(objectDebugId);
347 if (ref.debugId() != objectDebugId)
350 // is flooding the debugging output log!
351 // log(LogSend, QString("WATCH_PROPERTY %1").arg(objectDebugId));
353 QDeclarativeDebugWatch *watch = m_engineClient->addWatch(ref, m_engineClient);
354 m_objectWatches.insert(objectDebugId, watch);
356 connect(watch,SIGNAL(valueChanged(QByteArray,QVariant)),this,SLOT(objectWatchTriggered(QByteArray,QVariant)));
361 void ClientProxy::objectWatchTriggered(const QByteArray &propertyName, const QVariant &propertyValue)
363 // is flooding the debugging output log!
364 // log(LogReceive, QString("UPDATE_WATCH %1 %2").arg(QString::fromAscii(propertyName), propertyValue.toString()));
366 QDeclarativeDebugWatch *watch = dynamic_cast<QDeclarativeDebugWatch *>(QObject::sender());
368 emit propertyChanged(watch->objectDebugId(),propertyName, propertyValue);
371 bool ClientProxy::removeObjectWatch(int objectDebugId)
373 if (objectDebugId == -1)
376 if (!m_objectWatches.keys().contains(objectDebugId))
382 QDeclarativeDebugWatch *watch = m_objectWatches.value(objectDebugId);
383 disconnect(watch,SIGNAL(valueChanged(QByteArray,QVariant)), this, SLOT(objectWatchTriggered(QByteArray,QVariant)));
385 // is flooding the debugging output log!
386 // log(LogSend, QString("NO_WATCH %1").arg(QString::number(objectDebugId)));
388 m_engineClient->removeWatch(watch);
390 m_objectWatches.remove(objectDebugId);
396 void ClientProxy::removeAllObjectWatches()
398 foreach (int watchedObject, m_objectWatches.keys())
399 removeObjectWatch(watchedObject);
402 void ClientProxy::queryEngineContext(int id)
410 if (m_contextQuery) {
411 delete m_contextQuery;
415 log(LogSend, QString("LIST_OBJECTS %1").arg(QString::number(id)));
417 m_contextQuery = m_engineClient->queryRootContexts(QDeclarativeDebugEngineReference(id),
419 if (!m_contextQuery->isWaiting())
422 connect(m_contextQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
423 this, SLOT(contextChanged()));
426 void ClientProxy::contextChanged()
428 log(LogReceive, QString("LIST_OBJECTS_R"));
429 if (m_contextQuery) {
430 m_rootObjects.clear();
431 QDeclarativeDebugContextReference rootContext = m_contextQuery->rootContext();
432 delete m_contextQuery;
435 qDeleteAll(m_objectTreeQuery);
436 m_objectTreeQuery.clear();
437 m_requestObjectsTimer.stop();
439 fetchContextObjectRecursive(rootContext);
443 void ClientProxy::fetchContextObjectRecursive(const QDeclarativeDebugContextReference& context)
448 foreach (const QDeclarativeDebugObjectReference & obj, context.objects()) {
450 log(LogSend, QString("FETCH_OBJECT %1").arg(obj.idString()));
452 QDeclarativeDebugObjectQuery* query
453 = m_engineClient->queryObjectRecursive(obj, m_engineClient);
454 if (!query->isWaiting()) {
455 query->deleteLater(); //ignore errors;
457 m_objectTreeQuery << query;
459 SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
460 SLOT(objectTreeFetched(QDeclarativeDebugQuery::State)));
463 foreach (const QDeclarativeDebugContextReference& child, context.contexts()) {
464 fetchContextObjectRecursive(child);
469 void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
471 QDeclarativeDebugObjectQuery *query = qobject_cast<QDeclarativeDebugObjectQuery *>(sender());
472 if (!query || state == QDeclarativeDebugQuery::Error) {
477 log(LogReceive, QString("FETCH_OBJECT_R %1").arg(query->object().idString()));
479 m_rootObjects.append(query->object());
481 int removed = m_objectTreeQuery.removeAll(query);
482 Q_ASSERT(removed == 1);
486 if (m_objectTreeQuery.isEmpty()) {
487 int old_count = m_debugIdHash.count();
488 m_debugIdHash.clear();
489 m_debugIdHash.reserve(old_count + 1);
490 foreach (const QDeclarativeDebugObjectReference &it, m_rootObjects)
491 buildDebugIdHashRecursive(it);
492 emit objectTreeUpdated();
495 if (!m_observerClient->currentObjects().isEmpty())
496 onCurrentObjectsChanged(m_observerClient->currentObjects(), false);
498 m_observerClient->setObjectIdList(m_rootObjects);
503 void ClientProxy::buildDebugIdHashRecursive(const QDeclarativeDebugObjectReference& ref)
505 QString filename = ref.source().url().toLocalFile();
506 int lineNum = ref.source().lineNumber();
507 int colNum = ref.source().columnNumber();
510 // handle the case where the url contains the revision number encoded. (for object created by the debugger)
511 static QRegExp rx("(.*)_(\\d+):(\\d+)$");
512 if (rx.exactMatch(filename)) {
513 filename = rx.cap(1);
514 rev = rx.cap(2).toInt();
515 lineNum += rx.cap(3).toInt() - 1;
518 filename = InspectorUi::instance()->findFileInProject(filename);
520 // append the debug ids in the hash
521 m_debugIdHash[qMakePair<QString, int>(filename, rev)][qMakePair<int, int>(lineNum, colNum)].append(ref.debugId());
523 foreach (const QDeclarativeDebugObjectReference &it, ref.children())
524 buildDebugIdHashRecursive(it);
528 void ClientProxy::reloadQmlViewer()
531 m_observerClient->reloadViewer();
534 void ClientProxy::setDesignModeBehavior(bool inDesignMode)
537 m_observerClient->setDesignModeBehavior(inDesignMode);
540 void ClientProxy::setAnimationSpeed(qreal slowDownFactor)
543 m_observerClient->setAnimationSpeed(slowDownFactor);
546 void ClientProxy::setAnimationPaused(bool paused)
549 m_observerClient->setAnimationPaused(paused);
552 void ClientProxy::changeToColorPickerTool()
555 m_observerClient->changeToColorPickerTool();
558 void ClientProxy::changeToZoomTool()
561 m_observerClient->changeToZoomTool();
563 void ClientProxy::changeToSelectTool()
566 m_observerClient->changeToSelectTool();
569 void ClientProxy::changeToSelectMarqueeTool()
572 m_observerClient->changeToSelectMarqueeTool();
575 void ClientProxy::showAppOnTop(bool showOnTop)
578 m_observerClient->showAppOnTop(showOnTop);
581 void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
582 const QStringList &imports, const QString &filename)
585 m_observerClient->createQmlObject(qmlText, parentDebugId, imports, filename);
588 void ClientProxy::destroyQmlObject(int debugId)
591 m_observerClient->destroyQmlObject(debugId);
594 void ClientProxy::reparentQmlObject(int debugId, int newParent)
597 m_observerClient->reparentQmlObject(debugId, newParent);
600 void ClientProxy::setContextPathIndex(int contextIndex)
603 m_observerClient->setContextPathIndex(contextIndex);
606 void ClientProxy::updateConnected()
608 bool isConnected = m_observerClient && m_observerClient->status() == QDeclarativeDebugClient::Enabled
609 && m_engineClient && m_engineClient->status() == QDeclarativeEngineDebug::Enabled;
611 if (isConnected != m_isConnected) {
612 m_isConnected = isConnected;
622 void ClientProxy::reloadEngines()
625 emit connectionStatusMessage("[Inspector] Waiting for response to previous engine query");
632 emit aboutToReloadEngines();
634 log(LogSend, QString("LIST_ENGINES"));
636 m_engineQuery = m_engineClient->queryAvailableEngines(m_engineClient);
637 if (!m_engineQuery->isWaiting()) {
640 connect(m_engineQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
641 this, SLOT(updateEngineList()));
645 QList<QDeclarativeDebugEngineReference> ClientProxy::engines() const
650 void ClientProxy::updateEngineList()
652 log(LogReceive, QString("LIST_ENGINES_R"));
654 m_engines = m_engineQuery->engines();
655 delete m_engineQuery;
658 emit enginesChanged();
661 Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
666 bool ClientProxy::isConnected() const
668 return m_isConnected;
671 void ClientProxy::newObjects()
673 log(LogReceive, QString("OBJECT_CREATED"));
674 if (!m_requestObjectsTimer.isActive())
675 m_requestObjectsTimer.start();