qmljsclientproxy.cpp 18.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**************************************************************************
**
** 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.
**
**************************************************************************/
29

30
#include "qmljsclientproxy.h"
31
#include "qmljsprivateapi.h"
32
#include "qmljsobserverclient.h"
33
#include "qmljsinspector.h"
34

35
36
37
#include <debugger/debuggerplugin.h>
#include <debugger/debuggerrunner.h>
#include <debugger/qml/qmlengine.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
38
#include <debugger/qml/qmladapter.h>
39
#include <extensionsystem/pluginmanager.h>
40
#include <utils/qtcassert.h>
41
#include <projectexplorer/project.h>
42
43
44
45
46

#include <QUrl>
#include <QAbstractSocket>
#include <QDebug>

47
48
49
50
enum {
    debug = false
};

51
52
using namespace QmlJSInspector::Internal;

53
ClientProxy::ClientProxy(Debugger::QmlAdapter *adapter, QObject *parent)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
54
55
56
57
58
59
    : QObject(parent)
    , m_adapter(adapter)
    , m_client(m_adapter->client())
    , m_designClient(0)
    , m_engineQuery(0)
    , m_contextQuery(0)
60
    , m_isConnected(false)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
61
{
62
63
64
65
    m_requestObjectsTimer.setSingleShot(true);
    m_requestObjectsTimer.setInterval(3000);
    connect(m_client, SIGNAL(newObjects()), this, SLOT(newObjects()));
    connect(&m_requestObjectsTimer, SIGNAL(timeout()), this, SLOT(refreshObjectTree()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
66
67
68
69
70
    connectToServer();
}

void ClientProxy::connectToServer()
{
71
    m_designClient = new QmlJSObserverClient(m_adapter->connection(), this);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
72

73
74
75
76
77

    m_adapter->logServiceStatusChange(m_designClient->name(), m_designClient->status());

    connect(m_designClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
             this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    connect(m_designClient, SIGNAL(currentObjectsChanged(QList<int>)),
        SLOT(onCurrentObjectsChanged(QList<int>)));
    connect(m_designClient, SIGNAL(colorPickerActivated()),
        SIGNAL(colorPickerActivated()));
    connect(m_designClient, SIGNAL(zoomToolActivated()),
        SIGNAL(zoomToolActivated()));
    connect(m_designClient, SIGNAL(selectToolActivated()),
        SIGNAL(selectToolActivated()));
    connect(m_designClient, SIGNAL(selectMarqueeToolActivated()),
        SIGNAL(selectMarqueeToolActivated()));
    connect(m_designClient, SIGNAL(animationSpeedChanged(qreal)),
        SIGNAL(animationSpeedChanged(qreal)));
    connect(m_designClient, SIGNAL(designModeBehaviorChanged(bool)),
        SIGNAL(designModeBehaviorChanged(bool)));
    connect(m_designClient, SIGNAL(reloaded()), this,
        SIGNAL(serverReloaded()));
    connect(m_designClient, SIGNAL(selectedColorChanged(QColor)),
        SIGNAL(selectedColorChanged(QColor)));
    connect(m_designClient, SIGNAL(contextPathUpdated(QStringList)),
        SIGNAL(contextPathUpdated(QStringList)));
98
99

    updateConnected();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
100
101
}

102
103
104
105
106
107
108
109
110
void ClientProxy::clientStatusChanged(QDeclarativeDebugClient::Status status)
{
    QString serviceName;
    if (QDeclarativeDebugClient *client = qobject_cast<QDeclarativeDebugClient*>(sender())) {
        serviceName = client->name();
    }

    m_adapter->logServiceStatusChange(serviceName, status);

111
    updateConnected();
112
113
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
114
void ClientProxy::disconnectFromServer()
115
{
116
    if (m_designClient) {
117
118
        disconnect(m_designClient, SIGNAL(connectedStatusChanged(QDeclarativeDebugClient::Status)),
                 this, SLOT(clientStatusChanged(QDeclarativeDebugClient::Status)));
119
        disconnect(m_designClient, SIGNAL(currentObjectsChanged(QList<int>)),
hjk's avatar
hjk committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            this, SLOT(onCurrentObjectsChanged(QList<int>)));
        disconnect(m_designClient, SIGNAL(colorPickerActivated()),
            this, SIGNAL(colorPickerActivated()));
        disconnect(m_designClient, SIGNAL(zoomToolActivated()),
            this, SIGNAL(zoomToolActivated()));
        disconnect(m_designClient, SIGNAL(selectToolActivated()),
            this, SIGNAL(selectToolActivated()));
        disconnect(m_designClient, SIGNAL(selectMarqueeToolActivated()),
            this, SIGNAL(selectMarqueeToolActivated()));
        disconnect(m_designClient, SIGNAL(animationSpeedChanged(qreal)),
            this, SIGNAL(animationSpeedChanged(qreal)));
        disconnect(m_designClient, SIGNAL(designModeBehaviorChanged(bool)),
            this, SIGNAL(designModeBehaviorChanged(bool)));
        disconnect(m_designClient, SIGNAL(selectedColorChanged(QColor)),
            this, SIGNAL(selectedColorChanged(QColor)));
        disconnect(m_designClient, SIGNAL(contextPathUpdated(QStringList)),
            this, SIGNAL(contextPathUpdated(QStringList)));
137

138
139
        delete m_designClient;
        m_designClient = 0;
140
141
    }

Lasse Holmstedt's avatar
Lasse Holmstedt committed
142
143
144
    if (m_engineQuery)
        delete m_engineQuery;
    m_engineQuery = 0;
145

Lasse Holmstedt's avatar
Lasse Holmstedt committed
146
147
148
    if (m_contextQuery)
        delete m_contextQuery;
    m_contextQuery = 0;
149

150
151
    qDeleteAll(m_objectTreeQuery);
    m_objectTreeQuery.clear();
152
153

    updateConnected();
154
155
}

156
157
void ClientProxy::refreshObjectTree()
{
158
    if (!m_contextQuery) {
159
        m_requestObjectsTimer.stop();
160
161
162
        qDeleteAll(m_objectTreeQuery);
        m_objectTreeQuery.clear();
        queryEngineContext(m_engines.value(0).debugId());
163
164
165
    }
}

166
void ClientProxy::onCurrentObjectsChanged(const QList< int >& debugIds, bool requestIfNeeded)
167
168
169
{
    QList<QDeclarativeDebugObjectReference> selectedItems;

hjk's avatar
hjk committed
170
    foreach (int debugId, debugIds) {
171
172
173
        QDeclarativeDebugObjectReference ref = objectReferenceForId(debugId);
        if (ref.debugId() != -1) {
            selectedItems << ref;
174
        } else if (requestIfNeeded) {
175
            // ### FIXME right now, there's no way in the protocol to
hjk's avatar
hjk committed
176
177
            // a) get some item and know its parent (although that's possible
            //    by adding it to a separate plugin)
178
            // b) add children to part of an existing tree.
hjk's avatar
hjk committed
179
180
            // So the only choice that remains is to update the complete
            // tree when we have an unknown debug id.
181
            // break;
182
183
184
        }
    }

185
    emit selectedItemsChanged(selectedItems);
186
187
}

188
189
void ClientProxy::setSelectedItemsByObjectId(const QList<QDeclarativeDebugObjectReference> &objectRefs)
{
190
    if (isConnected())
191
192
193
194
195
        m_designClient->setSelectedItemsByObjectId(objectRefs);
}

QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId) const
{
196
197
198
199
200
201
    foreach (const QDeclarativeDebugObjectReference& it, m_rootObjects) {
        QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, it);
        if (result.debugId() == debugId)
            return result;
    }
    return QDeclarativeDebugObjectReference();
202
203
}

204
QList<QDeclarativeDebugObjectReference> QmlJSInspector::Internal::ClientProxy::rootObjectReference() const
205
{
206
    return m_rootObjects;
207
208
}

209
210
QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(int debugId,
                                                                   const QDeclarativeDebugObjectReference &objectRef) const
211
{
212
213
214
215
216
217
218
219
220
221
    if (objectRef.debugId() == debugId)
        return objectRef;

    foreach(const QDeclarativeDebugObjectReference &child, objectRef.children()) {
        QDeclarativeDebugObjectReference result = objectReferenceForId(debugId, child);
        if (result.debugId() == debugId)
            return result;
    }

    return QDeclarativeDebugObjectReference();
222
223
}

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
QDeclarativeDebugObjectReference ClientProxy::objectReferenceForId(const QString &objectId) const
{
    if (!objectId.isEmpty() && objectId[0].isLower()) {
        const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
        foreach (const QDeclarativeDebugObjectReference &ref, refs) {
            if (ref.idString() == objectId)
                return ref;
        }
    }
    return QDeclarativeDebugObjectReference();
}

QDeclarativeDebugObjectReference ClientProxy::objectReferenceForLocation(const int line, const int column) const
{
    const QList<QDeclarativeDebugObjectReference> refs = objectReferences();
    foreach (const QDeclarativeDebugObjectReference &ref, refs) {
        if (ref.source().lineNumber() == line && ref.source().columnNumber() == column)
            return ref;
    }

    return QDeclarativeDebugObjectReference();
}

247
QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences() const
248
{
249
250
    QList<QDeclarativeDebugObjectReference> result;
    foreach(const QDeclarativeDebugObjectReference &it, m_rootObjects) {
251
        result.append(objectReferences(it));
252
253
    }
    return result;
254
255
}

256
QList<QDeclarativeDebugObjectReference> ClientProxy::objectReferences(const QDeclarativeDebugObjectReference &objectRef) const
257
258
{
    QList<QDeclarativeDebugObjectReference> result;
259
    result.append(objectRef);
260
261

    foreach(const QDeclarativeDebugObjectReference &child, objectRef.children()) {
262
        result.append(objectReferences(child));
263
264
265
266
267
    }

    return result;
}

Roberto Raggi's avatar
Roberto Raggi committed
268
269
270
271
bool ClientProxy::setBindingForObject(int objectDebugId,
                                      const QString &propertyName,
                                      const QVariant &value,
                                      bool isLiteralValue)
272
{
273
274
    if (debug)
        qDebug() << "setBindingForObject():" << objectDebugId << propertyName << value;
275
    if (objectDebugId == -1)
276
277
        return false;

278
279
    if (propertyName == QLatin1String("id"))
        return false; // Crashes the QMLViewer.
280

281
282
283
    bool result = m_client->setBindingForObject(objectDebugId, propertyName, value.toString(), isLiteralValue);

    return result;
284
285
}

286
287
bool ClientProxy::setMethodBodyForObject(int objectDebugId, const QString &methodName, const QString &methodBody)
{
288
289
    if (debug)
        qDebug() << "setMethodBodyForObject():" << objectDebugId << methodName << methodBody;
290
291
292
293
294
    if (objectDebugId == -1)
        return 0;
    return m_client->setMethodBody(objectDebugId, methodName, methodBody);
}

295
296
bool ClientProxy::resetBindingForObject(int objectDebugId, const QString& propertyName)
{
297
298
    if (debug)
        qDebug() << "resetBindingForObject():" << objectDebugId << propertyName;
299
300
301
302
303
304
    if (objectDebugId == -1)
        return false;
    //    if (propertyName == QLatin1String("id"))  return false;
    return m_client->resetBindingForObject(objectDebugId, propertyName);
}

305
306
QDeclarativeDebugExpressionQuery *ClientProxy::queryExpressionResult(int objectDebugId, const QString &expr, QObject *parent)
{
307
308
    if (debug)
        qDebug() << "queryExpressionResult():" << objectDebugId << expr << parent;
309
310
311
312
313
    if (objectDebugId != -1)
        return m_client->queryExpressionResult(objectDebugId,expr,parent);
    return 0;
}

314
315
void ClientProxy::clearComponentCache()
{
316
    if (isConnected())
317
318
        m_designClient->clearComponentCache();
}
319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
void ClientProxy::queryEngineContext(int id)
{
    if (id < 0)
        return;

    if (m_contextQuery) {
        delete m_contextQuery;
        m_contextQuery = 0;
    }

    m_contextQuery = m_client->queryRootContexts(QDeclarativeDebugEngineReference(id), this);
    if (!m_contextQuery->isWaiting())
        contextChanged();
    else
Roberto Raggi's avatar
Roberto Raggi committed
334
335
        connect(m_contextQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
                this, SLOT(contextChanged()));
336
337
338
339
340
}

void ClientProxy::contextChanged()
{
    if (m_contextQuery) {
341
342
        m_rootObjects.clear();
        QDeclarativeDebugContextReference rootContext = m_contextQuery->rootContext();
Roberto Raggi's avatar
Roberto Raggi committed
343
344
        delete m_contextQuery;
        m_contextQuery = 0;
345

346
347
        qDeleteAll(m_objectTreeQuery);
        m_objectTreeQuery.clear();
348
        m_requestObjectsTimer.stop();
349

350
        fetchContextObjectRecusrsive(rootContext);
351
    }
352
}
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
void ClientProxy::fetchContextObjectRecusrsive(const QDeclarativeDebugContextReference& context)
{
    foreach (const QDeclarativeDebugObjectReference & obj, context.objects()) {
        QDeclarativeDebugObjectQuery* query = m_client->queryObjectRecursive(obj, this);
        if (!query->isWaiting()) {
            query->deleteLater(); //ignore errors;
        } else {
            m_objectTreeQuery << query;
            connect(query,
                    SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
                    SLOT(objectTreeFetched(QDeclarativeDebugQuery::State)));
        }
    }
    foreach (const QDeclarativeDebugContextReference& child, context.contexts()) {
        fetchContextObjectRecusrsive(child);
    }
370
371
}

372

373
374
void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
{
375
376
377
    QDeclarativeDebugObjectQuery *query = qobject_cast<QDeclarativeDebugObjectQuery *>(sender());
    if (!query || state == QDeclarativeDebugQuery::Error) {
        delete query;
378
379
380
        return;
    }

381
    m_rootObjects.append(query->object());
382

383
384
385
386
    int removed = m_objectTreeQuery.removeAll(query);
    Q_ASSERT(removed == 1);
    Q_UNUSED(removed);
    delete query;
387

388
    if (m_objectTreeQuery.isEmpty()) {
389
390
391
392
393
        int old_count = m_debugIdHash.count();
        m_debugIdHash.clear();
        m_debugIdHash.reserve(old_count + 1);
        foreach(const QDeclarativeDebugObjectReference &it, m_rootObjects)
            buildDebugIdHashRecursive(it);
394
        emit objectTreeUpdated();
395

396
        if (isConnected()) {
397
398
            if (!m_designClient->selectedItemIds().isEmpty())
                onCurrentObjectsChanged(m_designClient->selectedItemIds(), false);
399

400
401
            m_designClient->setObjectIdList(m_rootObjects);
        }
402
    }
403
404
}

405
406
407
408
409
410
void ClientProxy::buildDebugIdHashRecursive(const QDeclarativeDebugObjectReference& ref)
{
    QString filename = ref.source().url().toLocalFile();
    int lineNum = ref.source().lineNumber();
    int colNum = ref.source().columnNumber();
    int rev = 0;
411
412

    // handle the case where the url contains the revision number encoded. (for object created by the debugger)
413
    static QRegExp rx("^file://(.*)_(\\d+):(\\d+)$");
414
415
416
417
418
419
    if (rx.exactMatch(filename)) {
        filename = rx.cap(1);
        rev = rx.cap(2).toInt();
        lineNum += rx.cap(3).toInt() - 1;
    }

Olivier Goffart's avatar
Olivier Goffart committed
420
    //convert the filename to a canonical filename in case of shadow build.
421
422
423
424
425
426
427
428
429
430
    bool isShadowBuild = InspectorUi::instance()->isShadowBuildProject();
    if (isShadowBuild && rev == 0) {
        QString shadowBuildDir = InspectorUi::instance()->debugProjectBuildDirectory();

        if (filename.startsWith(shadowBuildDir)) {
            ProjectExplorer::Project *debugProject = InspectorUi::instance()->debugProject();
            filename = debugProject->projectDirectory() + filename.mid(shadowBuildDir.length());
        }
    }

431
    // append the debug ids in the hash
432
433
434
435
436
437
438
    m_debugIdHash[qMakePair<QString, int>(filename, rev)][qMakePair<int, int>(lineNum, colNum)].append(ref.debugId());

    foreach(const QDeclarativeDebugObjectReference &it, ref.children())
        buildDebugIdHashRecursive(it);
}


439
void ClientProxy::reloadQmlViewer()
440
{
441
    if (isConnected())
442
443
444
        m_designClient->reloadViewer();
}

445
446
void ClientProxy::setDesignModeBehavior(bool inDesignMode)
{
447
    if (isConnected())
448
449
450
        m_designClient->setDesignModeBehavior(inDesignMode);
}

451
452
void ClientProxy::setAnimationSpeed(qreal slowdownFactor)
{
453
    if (isConnected())
454
455
456
457
458
        m_designClient->setAnimationSpeed(slowdownFactor);
}

void ClientProxy::changeToColorPickerTool()
{
459
    if (isConnected())
460
461
462
463
464
        m_designClient->changeToColorPickerTool();
}

void ClientProxy::changeToZoomTool()
{
465
    if (isConnected())
466
467
468
469
        m_designClient->changeToZoomTool();
}
void ClientProxy::changeToSelectTool()
{
470
    if (isConnected())
471
        m_designClient->changeToSelectTool();
472
473
}

474
475
void ClientProxy::changeToSelectMarqueeTool()
{
476
    if (isConnected())
477
478
479
        m_designClient->changeToSelectMarqueeTool();
}

480
void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
481
482
                                  const QStringList &imports, const QString &filename)
{
483
    if (isConnected())
484
        m_designClient->createQmlObject(qmlText, parentDebugId, imports, filename);
485
486
}

487
void ClientProxy::destroyQmlObject(int debugId)
488
{
489
    if (isConnected())
490
491
492
        m_designClient->destroyQmlObject(debugId);
}

493
494
void ClientProxy::reparentQmlObject(int debugId, int newParent)
{
495
    if (isConnected())
496
497
498
        m_designClient->reparentQmlObject(debugId, newParent);
}

499
500
void ClientProxy::setContextPathIndex(int contextIndex)
{
501
    if (isConnected())
502
503
504
        m_designClient->setContextPathIndex(contextIndex);
}

505
506
507
508
509
510
511
512
513
void ClientProxy::updateConnected()
{
    bool isConnected = m_designClient && m_designClient->status() == QDeclarativeDebugClient::Enabled
            && m_client && m_client->status() == QDeclarativeEngineDebug::Enabled;

    if (isConnected != m_isConnected) {
        m_isConnected = isConnected;
        if (isConnected) {
            emit connected();
514
            reloadEngines();
515
516
517
518
519
520
        } else {
            emit disconnected();
        }
    }
}

521
522
523
524
525
526
527
528
529
530
531
532
533
void ClientProxy::reloadEngines()
{
    if (m_engineQuery) {
        emit connectionStatusMessage("[Inspector] Waiting for response to previous engine query");
        return;
    }

    emit aboutToReloadEngines();

    m_engineQuery = m_client->queryAvailableEngines(this);
    if (!m_engineQuery->isWaiting())
        updateEngineList();
    else
Roberto Raggi's avatar
Roberto Raggi committed
534
        connect(m_engineQuery, SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
535
536
537
538
539
540
541
542
543
544
545
                         this, SLOT(updateEngineList()));
}

QList<QDeclarativeDebugEngineReference> ClientProxy::engines() const
{
    return m_engines;
}

void ClientProxy::updateEngineList()
{
    m_engines = m_engineQuery->engines();
Roberto Raggi's avatar
Roberto Raggi committed
546
547
    delete m_engineQuery;
    m_engineQuery = 0;
548
549
550

    emit enginesChanged();
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
551

552
Debugger::QmlAdapter *ClientProxy::qmlAdapter() const
Lasse Holmstedt's avatar
Lasse Holmstedt committed
553
554
555
556
557
558
{
    return m_adapter;
}

bool ClientProxy::isConnected() const
{
559
    return m_isConnected;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
560
}
561
562
563
564
565
566

void ClientProxy::newObjects()
{
    if (!m_requestObjectsTimer.isActive())
        m_requestObjectsTimer.start();
}