1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmljsclientproxy.h"
35 #include "qmljsprivateapi.h"
36 #include "qmljsobserverclient.h"
37 #include "qmljsinspector.h"
39 #include <debugger/debuggerplugin.h>
40 #include <debugger/debuggerrunner.h>
41 #include <debugger/qml/qmlengine.h>
42 #include <debugger/qml/qmladapter.h>
43 #include <extensionsystem/pluginmanager.h>
44 #include <utils/qtcassert.h>
45 #include <projectexplorer/project.h>
48 #include <QAbstractSocket>
55 using namespace QmlJSInspector::Internal;
57 ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
60 , m_engineClient(m_adapter->client())
64 , m_isConnected(false)
66 m_requestObjectsTimer.setSingleShot(true);
67 m_requestObjectsTimer.setInterval(3000);
68 connect(m_engineClient, SIGNAL(newObjects()), this, SLOT(newObjects()));
69 connect(&m_requestObjectsTimer, SIGNAL(timeout()), this, SLOT(refreshObjectTree()));
73 void ClientProxy::connectToServer()
75 m_observerClient = new QmlJSObserverClient(m_adapter->connection(), this);
77 connect(m_observerClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
78 this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
79 connect(m_observerClient, SIGNAL(currentObjectsChanged(QList<int>)),
80 SLOT(onCurrentObjectsChanged(QList<int>)));
81 connect(m_observerClient, SIGNAL(colorPickerActivated()),
82 SIGNAL(colorPickerActivated()));
83 connect(m_observerClient, SIGNAL(zoomToolActivated()),
84 SIGNAL(zoomToolActivated()));
85 connect(m_observerClient, SIGNAL(selectToolActivated()),
86 SIGNAL(selectToolActivated()));
87 connect(m_observerClient, SIGNAL(selectMarqueeToolActivated()),
88 SIGNAL(selectMarqueeToolActivated()));
89 connect(m_observerClient, SIGNAL(animationSpeedChanged(qreal)),
90 SIGNAL(animationSpeedChanged(qreal)));
91 connect(m_observerClient, SIGNAL(designModeBehaviorChanged(bool)),
92 SIGNAL(designModeBehaviorChanged(bool)));
93 connect(m_observerClient, SIGNAL(showAppOnTopChanged(bool)),
94 SIGNAL(showAppOnTopChanged(bool)));
95 connect(m_observerClient, SIGNAL(reloaded()), this,
96 SIGNAL(serverReloaded()));
97 connect(m_observerClient, SIGNAL(selectedColorChanged(QColor)),
98 SIGNAL(selectedColorChanged(QColor)));
99 connect(m_observerClient, SIGNAL(contextPathUpdated(QStringList)),
100 SIGNAL(contextPathUpdated(QStringList)));
101 connect(m_observerClient, SIGNAL(logActivity(QString,QString)),
102 m_adapter, SLOT(logServiceActivity(QString,QString)));
107 void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
110 if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
111 serviceName = client->name();
114 m_adapter->logServiceStatusChange(serviceName, status);
119 void ClientProxy::disconnectFromServer()
121 if (m_observerClient) {
122 disconnect(m_observerClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
123 this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
124 disconnect(m_observerClient, SIGNAL(currentObjectsChanged(QList<int>)),
125 this, SLOT(onCurrentObjectsChanged(QList<int>)));
126 disconnect(m_observerClient, SIGNAL(colorPickerActivated()),
127 this, SIGNAL(colorPickerActivated()));
128 disconnect(m_observerClient, SIGNAL(zoomToolActivated()),
129 this, SIGNAL(zoomToolActivated()));
130 disconnect(m_observerClient, SIGNAL(selectToolActivated()),
131 this, SIGNAL(selectToolActivated()));
132 disconnect(m_observerClient, SIGNAL(selectMarqueeToolActivated()),
133 this, SIGNAL(selectMarqueeToolActivated()));
134 disconnect(m_observerClient, SIGNAL(animationSpeedChanged(qreal)),
135 this, SIGNAL(animationSpeedChanged(qreal)));
136 disconnect(m_observerClient, SIGNAL(designModeBehaviorChanged(bool)),
137 this, SIGNAL(designModeBehaviorChanged(bool)));
138 disconnect(m_observerClient, SIGNAL(selectedColorChanged(QColor)),
139 this, SIGNAL(selectedColorChanged(QColor)));
140 disconnect(m_observerClient, SIGNAL(contextPathUpdated(QStringList)),
141 this, SIGNAL(contextPathUpdated(QStringList)));
142 disconnect(m_observerClient, SIGNAL(logActivity(QString,QString)),
143 m_adapter, SLOT(logServiceActivity(QString,QString)));
145 delete m_observerClient;
146 m_observerClient = 0;
150 delete m_engineQuery;
154 delete m_contextQuery;
157 qDeleteAll(m_objectTreeQuery);
158 m_objectTreeQuery.clear();
163 void ClientProxy::refreshObjectTree()
165 if (!m_contextQuery) {
166 m_requestObjectsTimer.stop();
167 qDeleteAll(m_objectTreeQuery);
168 m_objectTreeQuery.clear();
169 queryEngineContext(m_engines.value(0).debugId());
173 void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds, bool requestIfNeeded)
175 QList<QDeclarativeDebugObjectReference> selectedItems;
177 foreach (int debugId, debugIds) {
178 QDeclarativeDebugObjectReference ref = objectReferenceForId(debugId);
179 if (ref.debugId() != -1) {
180 selectedItems << ref;
181 } else if (requestIfNeeded) {
182 // ### FIXME right now, there's no way in the protocol to
183 // a) get some item and know its parent (although that's possible
184 // by adding it to a separate plugin)
185 // b) add children to part of an existing tree.
186 // So the only choice that remains is to update the complete
187 // tree when we have an unknown debug id.
192 emit selectedItemsChanged(selectedItems);
195 void ClientProxy::setSelectedItemsByObjectId(const QList<QDeclarativeDebugObjectReference> &objectRefs)
200 foreach (const QDeclarativeDebugObjectReference &ref, objectRefs) {
201 debugIds << ref.debugId();
204 m_observerClient->setCurrentObjects(debugIds);
208 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId) const
210 foreach (const QDeclarativeDebugObjectReference& it, m_rootObjects) {
211 QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, it);
212 if (result.debugId() == debugId)
215 return QDeclarativeDebugObjectReference();
218 QList<QDeclarativeDebugObjectReference> QmlJSInspector::Internal::ClientProxy::rootObjectReference() const
220 return m_rootObjects;
223 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId,
224 const QDeclarativeDebugObjectReference &objectRef) const
226 if (objectRef.debugId() == debugId)
229 foreach(const QDeclarativeDebugObjectReference &child, objectRef.children()) {
230 QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, child);
231 if (result.debugId() == debugId)
235 return QDeclarativeDebugObjectReference();
238 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(const QString &objectId) const
240 if (!objectId.isEmpty() && objectId[0].isLower()) {
241 const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
242 foreach (const QDeclarativeDebugObjectReference &ref, refs) {
243 if (ref.idString() == objectId)
247 return QDeclarativeDebugObjectReference();
250 QDeclarativeDebugObjectReference ClientProxy::objectReferenceForLocation(const int line, const int column) const
252 const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
253 foreach (const QDeclarativeDebugObjectReference &ref, refs) {
254 if (ref.source().lineNumber() == line && ref.source().columnNumber() == column)
258 return QDeclarativeDebugObjectReference();
261 QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences() const
263 QList<QDeclarativeDebugObjectReference> result;
264 foreach(const QDeclarativeDebugObjectReference &it, m_rootObjects) {
265 result.append(objectReferences(it));
270 QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences(const QDeclarativeDebugObjectReference &objectRef) const
272 QList<QDeclarativeDebugObjectReference> result;
273 result.append(objectRef);
275 foreach(const QDeclarativeDebugObjectReference &child, objectRef.children()) {
276 result.append(objectReferences(child));
282 bool ClientProxy::setBindingForObject(int objectDebugId,
283 const QString &propertyName,
284 const QVariant &value,
288 qDebug() << "setBindingForObject():" << objectDebugId << propertyName << value;
289 if (objectDebugId == -1)
292 if (propertyName == QLatin1String("id"))
293 return false; // Crashes the QMLViewer.
295 bool result = m_engineClient->setBindingForObject(objectDebugId, propertyName, value.toString(), isLiteralValue);
300 bool ClientProxy::setMethodBodyForObject(int objectDebugId, const QString &methodName, const QString &methodBody)
303 qDebug() << "setMethodBodyForObject():" << objectDebugId << methodName << methodBody;
304 if (objectDebugId == -1)
306 return m_engineClient->setMethodBody(objectDebugId, methodName, methodBody);
309 bool ClientProxy::resetBindingForObject(int objectDebugId, const QString& propertyName)
312 qDebug() << "resetBindingForObject():" << objectDebugId << propertyName;
313 if (objectDebugId == -1)
315 // if (propertyName == QLatin1String("id")) return false;
316 return m_engineClient->resetBindingForObject(objectDebugId, propertyName);
319 QDeclarativeDebugExpressionQuery *ClientProxy::queryExpressionResult(int objectDebugId, const QString &expr, QObject *parent)
322 qDebug() << "queryExpressionResult():" << objectDebugId << expr << parent;
323 if (objectDebugId != -1)
324 return m_engineClient->queryExpressionResult(objectDebugId,expr,parent);
328 void ClientProxy::clearComponentCache()
331 m_observerClient->clearComponentCache();
334 void ClientProxy::queryEngineContext(int id)
339 if (m_contextQuery) {
340 delete m_contextQuery;
344 m_contextQuery = m_engineClient->queryRootContexts(QDeclarativeDebugEngineReference(id), this);
345 if (!m_contextQuery->isWaiting())
348 connect(m_contextQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
349 this, SLOT(contextChanged()));
352 void ClientProxy::contextChanged()
354 if (m_contextQuery) {
355 m_rootObjects.clear();
356 QDeclarativeDebugContextReference rootContext = m_contextQuery->rootContext();
357 delete m_contextQuery;
360 qDeleteAll(m_objectTreeQuery);
361 m_objectTreeQuery.clear();
362 m_requestObjectsTimer.stop();
364 fetchContextObjectRecursive(rootContext);
368 void ClientProxy::fetchContextObjectRecursive(const QDeclarativeDebugContextReference& context)
370 foreach (const QDeclarativeDebugObjectReference & obj, context.objects()) {
371 QDeclarativeDebugObjectQuery* query = m_engineClient->queryObjectRecursive(obj, this);
372 if (!query->isWaiting()) {
373 query->deleteLater(); //ignore errors;
375 m_objectTreeQuery << query;
377 SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
378 SLOT(objectTreeFetched(QDeclarativeDebugQuery::State)));
381 foreach (const QDeclarativeDebugContextReference& child, context.contexts()) {
382 fetchContextObjectRecursive(child);
387 void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
389 QDeclarativeDebugObjectQuery *query = qobject_cast<QDeclarativeDebugObjectQuery *>(sender());
390 if (!query || state == QDeclarativeDebugQuery::Error) {
395 m_rootObjects.append(query->object());
397 int removed = m_objectTreeQuery.removeAll(query);
398 Q_ASSERT(removed == 1);
402 if (m_objectTreeQuery.isEmpty()) {
403 int old_count = m_debugIdHash.count();
404 m_debugIdHash.clear();
405 m_debugIdHash.reserve(old_count + 1);
406 foreach(const QDeclarativeDebugObjectReference &it, m_rootObjects)
407 buildDebugIdHashRecursive(it);
408 emit objectTreeUpdated();
411 if (!m_observerClient->currentObjects().isEmpty())
412 onCurrentObjectsChanged(m_observerClient->currentObjects(), false);
414 m_observerClient->setObjectIdList(m_rootObjects);
419 void ClientProxy::buildDebugIdHashRecursive(const QDeclarativeDebugObjectReference& ref)
421 QString filename = ref.source().url().toLocalFile();
422 int lineNum = ref.source().lineNumber();
423 int colNum = ref.source().columnNumber();
426 // handle the case where the url contains the revision number encoded. (for object created by the debugger)
427 static QRegExp rx("(.*)_(\\d+):(\\d+)$");
428 if (rx.exactMatch(filename)) {
429 filename = rx.cap(1);
430 rev = rx.cap(2).toInt();
431 lineNum += rx.cap(3).toInt() - 1;
434 //convert the filename to a canonical filename in case of shadow build.
435 bool isShadowBuild = InspectorUi::instance()->isShadowBuildProject();
436 if (isShadowBuild && rev == 0) {
437 QString shadowBuildDir = InspectorUi::instance()->debugProjectBuildDirectory();
439 if (filename.startsWith(shadowBuildDir)) {
440 ProjectExplorer::Project *debugProject = InspectorUi::instance()->debugProject();
441 filename = debugProject->projectDirectory() + filename.mid(shadowBuildDir.length());
445 // append the debug ids in the hash
446 m_debugIdHash[qMakePair<QString, int>(filename, rev)][qMakePair<int, int>(lineNum, colNum)].append(ref.debugId());
448 foreach(const QDeclarativeDebugObjectReference &it, ref.children())
449 buildDebugIdHashRecursive(it);
453 void ClientProxy::reloadQmlViewer()
456 m_observerClient->reloadViewer();
459 void ClientProxy::setDesignModeBehavior(bool inDesignMode)
462 m_observerClient->setDesignModeBehavior(inDesignMode);
465 void ClientProxy::setAnimationSpeed(qreal slowdownFactor)
468 m_observerClient->setAnimationSpeed(slowdownFactor);
471 void ClientProxy::changeToColorPickerTool()
474 m_observerClient->changeToColorPickerTool();
477 void ClientProxy::changeToZoomTool()
480 m_observerClient->changeToZoomTool();
482 void ClientProxy::changeToSelectTool()
485 m_observerClient->changeToSelectTool();
488 void ClientProxy::changeToSelectMarqueeTool()
491 m_observerClient->changeToSelectMarqueeTool();
494 void ClientProxy::showAppOnTop(bool showOnTop)
497 m_observerClient->showAppOnTop(showOnTop);
500 void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
501 const QStringList &imports, const QString &filename)
504 m_observerClient->createQmlObject(qmlText, parentDebugId, imports, filename);
507 void ClientProxy::destroyQmlObject(int debugId)
510 m_observerClient->destroyQmlObject(debugId);
513 void ClientProxy::reparentQmlObject(int debugId, int newParent)
516 m_observerClient->reparentQmlObject(debugId, newParent);
519 void ClientProxy::setContextPathIndex(int contextIndex)
522 m_observerClient->setContextPathIndex(contextIndex);
525 void ClientProxy::updateConnected()
527 bool isConnected = m_observerClient && m_observerClient->status() == QDeclarativeDebugClient::Enabled
528 && m_engineClient && m_engineClient->status() == QDeclarativeEngineDebug::Enabled;
530 if (isConnected != m_isConnected) {
531 m_isConnected = isConnected;
541 void ClientProxy::reloadEngines()
544 emit connectionStatusMessage("[Inspector] Waiting for response to previous engine query");
548 emit aboutToReloadEngines();
550 m_engineQuery = m_engineClient->queryAvailableEngines(this);
551 if (!m_engineQuery->isWaiting())
554 connect(m_engineQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
555 this, SLOT(updateEngineList()));
558 QList<QDeclarativeDebugEngineReference> ClientProxy::engines() const
563 void ClientProxy::updateEngineList()
565 m_engines = m_engineQuery->engines();
566 delete m_engineQuery;
569 emit enginesChanged();
572 Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
577 bool ClientProxy::isConnected() const
579 return m_isConnected;
582 void ClientProxy::newObjects()
584 if (!m_requestObjectsTimer.isActive())
585 m_requestObjectsTimer.start();