Rename QDeclarativeEngineDebugServer to ~Service
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qdeclarativeenginedebugservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativeenginedebugservice_p.h"
43
44 #include "private/qdeclarativeboundsignal_p.h"
45 #include "qdeclarativeengine.h"
46 #include "private/qdeclarativemetatype_p.h"
47 #include "qdeclarativeproperty.h"
48 #include "private/qdeclarativeproperty_p.h"
49 #include "private/qdeclarativebinding_p.h"
50 #include "private/qdeclarativecontext_p.h"
51 #include "private/qdeclarativewatcher_p.h"
52 #include "private/qdeclarativevaluetype_p.h"
53 #include "private/qdeclarativevmemetaobject_p.h"
54 #include "private/qdeclarativeexpression_p.h"
55 #include "private/qdeclarativepropertychanges_p.h"
56
57 #include <QtCore/qdebug.h>
58 #include <QtCore/qmetaobject.h>
59
60 QT_BEGIN_NAMESPACE
61
62 Q_GLOBAL_STATIC(QDeclarativeEngineDebugService, qmlEngineDebugService);
63
64 QDeclarativeEngineDebugService *QDeclarativeEngineDebugService::instance()
65 {
66     return qmlEngineDebugService();
67 }
68
69 QDeclarativeEngineDebugService::QDeclarativeEngineDebugService(QObject *parent)
70 : QDeclarativeDebugService(QLatin1String("QDeclarativeEngine"), parent),
71     m_watch(new QDeclarativeWatcher(this))
72 {
73     QObject::connect(m_watch, SIGNAL(propertyChanged(int,int,QMetaProperty,QVariant)),
74                      this, SLOT(propertyChanged(int,int,QMetaProperty,QVariant)));
75 }
76
77 QDataStream &operator<<(QDataStream &ds, 
78                         const QDeclarativeEngineDebugService::QDeclarativeObjectData &data)
79 {
80     ds << data.url << data.lineNumber << data.columnNumber << data.idString
81        << data.objectName << data.objectType << data.objectId << data.contextId;
82     return ds;
83 }
84
85 QDataStream &operator>>(QDataStream &ds, 
86                         QDeclarativeEngineDebugService::QDeclarativeObjectData &data)
87 {
88     ds >> data.url >> data.lineNumber >> data.columnNumber >> data.idString
89        >> data.objectName >> data.objectType >> data.objectId >> data.contextId;
90     return ds;
91 }
92
93 QDataStream &operator<<(QDataStream &ds, 
94                         const QDeclarativeEngineDebugService::QDeclarativeObjectProperty &data)
95 {
96     ds << (int)data.type << data.name << data.value << data.valueTypeName
97        << data.binding << data.hasNotifySignal;
98     return ds;
99 }
100
101 QDataStream &operator>>(QDataStream &ds,  
102                         QDeclarativeEngineDebugService::QDeclarativeObjectProperty &data)
103 {
104     int type;
105     ds >> type >> data.name >> data.value >> data.valueTypeName
106        >> data.binding >> data.hasNotifySignal;
107     data.type = (QDeclarativeEngineDebugService::QDeclarativeObjectProperty::Type)type;
108     return ds;
109 }
110
111 static inline bool isSignalPropertyName(const QString &signalName)
112 {
113     // see QmlCompiler::isSignalPropertyName
114     return signalName.length() >= 3 && signalName.startsWith(QLatin1String("on")) &&
115            signalName.at(2).isLetter() && signalName.at(2).isUpper();
116 }
117
118 static bool hasValidSignal(QObject *object, const QString &propertyName)
119 {
120     if (!isSignalPropertyName(propertyName))
121         return false;
122
123     QString signalName = propertyName.mid(2);
124     signalName[0] = signalName.at(0).toLower();
125
126     int sigIdx = QDeclarativePropertyPrivate::findSignalByName(object->metaObject(), signalName.toLatin1()).methodIndex();
127
128     if (sigIdx == -1)
129         return false;
130
131     return true;
132 }
133
134 QDeclarativeEngineDebugService::QDeclarativeObjectProperty
135 QDeclarativeEngineDebugService::propertyData(QObject *obj, int propIdx)
136 {
137     QDeclarativeObjectProperty rv;
138
139     QMetaProperty prop = obj->metaObject()->property(propIdx);
140
141     rv.type = QDeclarativeObjectProperty::Unknown;
142     rv.valueTypeName = QString::fromUtf8(prop.typeName());
143     rv.name = QString::fromUtf8(prop.name());
144     rv.hasNotifySignal = prop.hasNotifySignal();
145     QDeclarativeAbstractBinding *binding = 
146         QDeclarativePropertyPrivate::binding(QDeclarativeProperty(obj, rv.name));
147     if (binding)
148         rv.binding = binding->expression();
149
150     if (QDeclarativeValueTypeFactory::isValueType(prop.userType())) {
151         rv.type = QDeclarativeObjectProperty::Basic;
152     } else if (QDeclarativeMetaType::isQObject(prop.userType()))  {
153         rv.type = QDeclarativeObjectProperty::Object;
154     } else if (QDeclarativeMetaType::isList(prop.userType())) {
155         rv.type = QDeclarativeObjectProperty::List;
156     }
157
158     QVariant value;
159     if (rv.type != QDeclarativeObjectProperty::Unknown && prop.userType() != 0) {
160         value = prop.read(obj);
161     }
162     rv.value = valueContents(value);
163
164     return rv;
165 }
166
167 QVariant QDeclarativeEngineDebugService::valueContents(const QVariant &value) const
168 {
169     int userType = value.userType();
170
171     if (value.type() == QVariant::List) {
172         QVariantList contents;
173         QVariantList list = value.toList();
174         int count = list.size();
175         for (int i = 0; i < count; i++)
176             contents << valueContents(list.at(i));
177         return contents;
178     }
179
180     if (QDeclarativeValueTypeFactory::isValueType(userType))
181         return value;
182
183     if (QDeclarativeMetaType::isQObject(userType)) {
184         QObject *o = QDeclarativeMetaType::toQObject(value);
185         if (o) {
186             QString name = o->objectName();
187             if (name.isEmpty())
188                 name = QLatin1String("<unnamed object>");
189             return name;
190         }
191     }
192
193     return QLatin1String("<unknown value>");
194 }
195
196 void QDeclarativeEngineDebugService::buildObjectDump(QDataStream &message,
197                                            QObject *object, bool recur, bool dumpProperties)
198 {
199     message << objectData(object);
200
201     QObjectList children = object->children();
202     
203     int childrenCount = children.count();
204     for (int ii = 0; ii < children.count(); ++ii) {
205         if (qobject_cast<QDeclarativeContext*>(children[ii]) || QDeclarativeBoundSignal::cast(children[ii]))
206             --childrenCount;
207     }
208
209     message << childrenCount << recur;
210
211     QList<QDeclarativeObjectProperty> fakeProperties;
212
213     for (int ii = 0; ii < children.count(); ++ii) {
214         QObject *child = children.at(ii);
215         if (qobject_cast<QDeclarativeContext*>(child))
216             continue;
217         QDeclarativeBoundSignal *signal = QDeclarativeBoundSignal::cast(child);
218         if (signal) {
219             if (!dumpProperties)
220                 continue;
221             QDeclarativeObjectProperty prop;
222             prop.type = QDeclarativeObjectProperty::SignalProperty;
223             prop.hasNotifySignal = false;
224             QDeclarativeExpression *expr = signal->expression();
225             if (expr) {
226                 prop.value = expr->expression();
227                 QObject *scope = expr->scopeObject();
228                 if (scope) {
229                     QString sig = QLatin1String(scope->metaObject()->method(signal->index()).signature());
230                     int lparen = sig.indexOf(QLatin1Char('('));
231                     if (lparen >= 0) {
232                         QString methodName = sig.mid(0, lparen);
233                         prop.name = QLatin1String("on") + methodName[0].toUpper()
234                                 + methodName.mid(1);
235                     }
236                 }
237             }
238             fakeProperties << prop;
239         } else {
240             if (recur)
241                 buildObjectDump(message, child, recur, dumpProperties);
242             else
243                 message << objectData(child);
244         }
245     }
246
247     if (!dumpProperties) {
248         message << 0;
249         return;
250     }
251
252     QList<int> propertyIndexes;
253     for (int ii = 0; ii < object->metaObject()->propertyCount(); ++ii) {
254         if (object->metaObject()->property(ii).isScriptable())
255             propertyIndexes << ii;
256     }
257
258     message << propertyIndexes.size() + fakeProperties.count();
259
260     for (int ii = 0; ii < propertyIndexes.size(); ++ii)
261         message << propertyData(object, propertyIndexes.at(ii));
262
263     for (int ii = 0; ii < fakeProperties.count(); ++ii)
264         message << fakeProperties[ii];
265 }
266
267 void QDeclarativeEngineDebugService::prepareDeferredObjects(QObject *obj)
268 {
269     qmlExecuteDeferred(obj);
270
271     QObjectList children = obj->children();
272     for (int ii = 0; ii < children.count(); ++ii) {
273         QObject *child = children.at(ii);
274         prepareDeferredObjects(child);
275     }
276
277 }
278
279 void QDeclarativeEngineDebugService::buildObjectList(QDataStream &message, QDeclarativeContext *ctxt)
280 {
281     QDeclarativeContextData *p = QDeclarativeContextData::get(ctxt);
282
283     QString ctxtName = ctxt->objectName();
284     int ctxtId = QDeclarativeDebugService::idForObject(ctxt);
285
286     message << ctxtName << ctxtId; 
287
288     int count = 0;
289
290     QDeclarativeContextData *child = p->childContexts;
291     while (child) {
292         ++count;
293         child = child->nextChild;
294     }
295
296     message << count;
297
298     child = p->childContexts;
299     while (child) {
300         buildObjectList(message, child->asQDeclarativeContext());
301         child = child->nextChild;
302     }
303
304     // Clean deleted objects
305     QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(ctxt);
306     for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
307         if (!ctxtPriv->instances.at(ii)) {
308             ctxtPriv->instances.removeAt(ii);
309             --ii;
310         }
311     }
312
313     message << ctxtPriv->instances.count();
314     for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
315         message << objectData(ctxtPriv->instances.at(ii));
316     }
317 }
318
319 void QDeclarativeEngineDebugService::buildStatesList(QDeclarativeContext *ctxt, bool cleanList=false)
320 {
321     if (cleanList)
322         m_allStates.clear();
323
324     QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(ctxt);
325     for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
326         buildStatesList(ctxtPriv->instances.at(ii));
327     }
328
329     QDeclarativeContextData *child = QDeclarativeContextData::get(ctxt)->childContexts;
330     while (child) {
331         buildStatesList(child->asQDeclarativeContext());
332         child = child->nextChild;
333     }
334 }
335
336 void QDeclarativeEngineDebugService::buildStatesList(QObject *obj)
337 {
338     if (QDeclarativeState *state = qobject_cast<QDeclarativeState *>(obj)) {
339             m_allStates.append(state);
340     }
341
342     QObjectList children = obj->children();
343     for (int ii = 0; ii < children.count(); ++ii) {
344         buildStatesList(children.at(ii));
345     }
346 }
347
348 QDeclarativeEngineDebugService::QDeclarativeObjectData
349 QDeclarativeEngineDebugService::objectData(QObject *object)
350 {
351     QDeclarativeData *ddata = QDeclarativeData::get(object);
352     QDeclarativeObjectData rv;
353     if (ddata && ddata->outerContext) {
354         rv.url = ddata->outerContext->url;
355         rv.lineNumber = ddata->lineNumber;
356         rv.columnNumber = ddata->columnNumber;
357     } else {
358         rv.lineNumber = -1;
359         rv.columnNumber = -1;
360     }
361
362     QDeclarativeContext *context = qmlContext(object);
363     if (context) {
364         QDeclarativeContextData *cdata = QDeclarativeContextData::get(context);
365         if (cdata)
366             rv.idString = cdata->findObjectId(object);
367     }
368
369     rv.objectName = object->objectName();
370     rv.objectId = QDeclarativeDebugService::idForObject(object);
371     rv.contextId = QDeclarativeDebugService::idForObject(qmlContext(object));
372
373     QDeclarativeType *type = QDeclarativeMetaType::qmlType(object->metaObject());
374     if (type) {
375         QString typeName = QLatin1String(type->qmlTypeName());
376         int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
377         rv.objectType = lastSlash < 0 ? typeName : typeName.mid(lastSlash+1);
378     } else {
379         rv.objectType = QString::fromUtf8(object->metaObject()->className());
380         int marker = rv.objectType.indexOf(QLatin1String("_QMLTYPE_"));
381         if (marker != -1)
382             rv.objectType = rv.objectType.left(marker);
383     }
384
385     return rv;
386 }
387
388 void QDeclarativeEngineDebugService::messageReceived(const QByteArray &message)
389 {
390     QDataStream ds(message);
391
392     QByteArray type;
393     ds >> type;
394
395     if (type == "LIST_ENGINES") {
396         int queryId;
397         ds >> queryId;
398
399         QByteArray reply;
400         QDataStream rs(&reply, QIODevice::WriteOnly);
401         rs << QByteArray("LIST_ENGINES_R");
402         rs << queryId << m_engines.count();
403
404         for (int ii = 0; ii < m_engines.count(); ++ii) {
405             QDeclarativeEngine *engine = m_engines.at(ii);
406
407             QString engineName = engine->objectName();
408             int engineId = QDeclarativeDebugService::idForObject(engine);
409
410             rs << engineName << engineId;
411         }
412
413         sendMessage(reply);
414     } else if (type == "LIST_OBJECTS") {
415         int queryId;
416         int engineId = -1;
417         ds >> queryId >> engineId;
418
419         QDeclarativeEngine *engine = 
420             qobject_cast<QDeclarativeEngine *>(QDeclarativeDebugService::objectForId(engineId));
421
422         QByteArray reply;
423         QDataStream rs(&reply, QIODevice::WriteOnly);
424         rs << QByteArray("LIST_OBJECTS_R") << queryId;
425
426         if (engine) {
427             buildObjectList(rs, engine->rootContext());
428             buildStatesList(engine->rootContext(), true);
429         }
430
431         sendMessage(reply);
432     } else if (type == "FETCH_OBJECT") {
433         int queryId;
434         int objectId;
435         bool recurse;
436         bool dumpProperties = true;
437
438         ds >> queryId >> objectId >> recurse >> dumpProperties;
439
440         QObject *object = QDeclarativeDebugService::objectForId(objectId);
441
442         QByteArray reply;
443         QDataStream rs(&reply, QIODevice::WriteOnly);
444         rs << QByteArray("FETCH_OBJECT_R") << queryId;
445
446         if (object) {
447             if (recurse)
448                 prepareDeferredObjects(object);
449             buildObjectDump(rs, object, recurse, dumpProperties);
450         }
451
452         sendMessage(reply);
453     } else if (type == "WATCH_OBJECT") {
454         int queryId;
455         int objectId;
456
457         ds >> queryId >> objectId;
458         bool ok = m_watch->addWatch(queryId, objectId);
459
460         QByteArray reply;
461         QDataStream rs(&reply, QIODevice::WriteOnly);
462         rs << QByteArray("WATCH_OBJECT_R") << queryId << ok;
463
464         sendMessage(reply);
465     } else if (type == "WATCH_PROPERTY") {
466         int queryId;
467         int objectId;
468         QByteArray property;
469
470         ds >> queryId >> objectId >> property;
471         bool ok = m_watch->addWatch(queryId, objectId, property);
472
473         QByteArray reply;
474         QDataStream rs(&reply, QIODevice::WriteOnly);
475         rs << QByteArray("WATCH_PROPERTY_R") << queryId << ok;
476
477         sendMessage(reply);
478     } else if (type == "WATCH_EXPR_OBJECT") {
479         int queryId;
480         int debugId;
481         QString expr;
482
483         ds >> queryId >> debugId >> expr;
484         bool ok = m_watch->addWatch(queryId, debugId, expr);
485
486         QByteArray reply;
487         QDataStream rs(&reply, QIODevice::WriteOnly);
488         rs << QByteArray("WATCH_EXPR_OBJECT_R") << queryId << ok;
489         sendMessage(reply);
490     } else if (type == "NO_WATCH") {
491         int queryId;
492
493         ds >> queryId;
494         m_watch->removeWatch(queryId);
495     } else if (type == "EVAL_EXPRESSION") {
496         int queryId;
497         int objectId;
498         QString expr;
499
500         ds >> queryId >> objectId >> expr;
501
502         QObject *object = QDeclarativeDebugService::objectForId(objectId);
503         QDeclarativeContext *context = qmlContext(object);
504         QVariant result;
505         if (object && context) {
506             QDeclarativeExpression exprObj(context, object, expr);
507             bool undefined = false;
508             QVariant value = exprObj.evaluate(&undefined);
509             if (undefined)
510                 result = QLatin1String("<undefined>");
511             else
512                 result = valueContents(value);
513         } else {
514             result = QLatin1String("<unknown context>");
515         }
516
517         QByteArray reply;
518         QDataStream rs(&reply, QIODevice::WriteOnly);
519         rs << QByteArray("EVAL_EXPRESSION_R") << queryId << result;
520
521         sendMessage(reply);
522     } else if (type == "SET_BINDING") {
523         int objectId;
524         QString propertyName;
525         QVariant expr;
526         bool isLiteralValue;
527         QString filename;
528         int line;
529         ds >> objectId >> propertyName >> expr >> isLiteralValue;
530         if (!ds.atEnd()) { // backward compatibility from 2.1, 2.2
531             ds >> filename >> line;
532         }
533         setBinding(objectId, propertyName, expr, isLiteralValue, filename, line);
534     } else if (type == "RESET_BINDING") {
535         int objectId;
536         QString propertyName;
537         ds >> objectId >> propertyName;
538         resetBinding(objectId, propertyName);
539     } else if (type == "SET_METHOD_BODY") {
540         int objectId;
541         QString methodName;
542         QString methodBody;
543         ds >> objectId >> methodName >> methodBody;
544         setMethodBody(objectId, methodName, methodBody);
545     }
546 }
547
548 void QDeclarativeEngineDebugService::setBinding(int objectId,
549                                                const QString &propertyName,
550                                                const QVariant &expression,
551                                                bool isLiteralValue,
552                                                QString filename,
553                                                int line)
554 {
555     QObject *object = objectForId(objectId);
556     QDeclarativeContext *context = qmlContext(object);
557
558     if (object && context) {
559         QDeclarativeProperty property(object, propertyName, context);
560         if (property.isValid()) {
561
562             bool inBaseState = true;
563
564             foreach(QWeakPointer<QDeclarativeState> statePointer, m_allStates) {
565                 if (QDeclarativeState *state = statePointer.data()) {
566                     // here we assume that the revert list on itself defines the base state
567                     if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) {
568                         inBaseState = false;
569
570                         QDeclarativeBinding *newBinding = 0;
571                         if (!isLiteralValue) {
572                             newBinding = new QDeclarativeBinding(expression.toString(), object, context);
573                             newBinding->setTarget(property);
574                             newBinding->setNotifyOnValueChanged(true);
575                             newBinding->setSourceLocation(filename, line);
576                         }
577
578                         state->changeBindingInRevertList(object, propertyName, newBinding);
579
580                         if (isLiteralValue)
581                             state->changeValueInRevertList(object, propertyName, expression);
582                     }
583                 }
584             }
585
586             if (inBaseState) {
587                 if (isLiteralValue) {
588                     property.write(expression);
589                 } else if (hasValidSignal(object, propertyName)) {
590                     QDeclarativeExpression *declarativeExpression = new QDeclarativeExpression(context, object, expression.toString());
591                     QDeclarativePropertyPrivate::setSignalExpression(property, declarativeExpression);
592                     declarativeExpression->setSourceLocation(filename, line);
593                 } else if (property.isProperty()) {
594                     QDeclarativeBinding *binding = new QDeclarativeBinding(expression.toString(), object, context);
595                     binding->setTarget(property);
596                     binding->setSourceLocation(filename, line);
597                     binding->setNotifyOnValueChanged(true);
598                     QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::setBinding(property, binding);
599                     if (oldBinding)
600                         oldBinding->destroy();
601                     binding->update();
602                 } else {
603                     qWarning() << "QDeclarativeEngineDebugService::setBinding: unable to set property" << propertyName << "on object" << object;
604                 }
605             }
606
607         } else {
608             // not a valid property
609             if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
610                 if (isLiteralValue) {
611                     propertyChanges->changeValue(propertyName, expression);
612                 } else {
613                     propertyChanges->changeExpression(propertyName, expression.toString());
614                 }
615             } else {
616                 qWarning() << "QDeclarativeEngineDebugService::setBinding: unable to set property" << propertyName << "on object" << object;
617             }
618         }
619     }
620 }
621
622 void QDeclarativeEngineDebugService::resetBinding(int objectId, const QString &propertyName)
623 {
624     QObject *object = objectForId(objectId);
625     QDeclarativeContext *context = qmlContext(object);
626
627     if (object && context) {
628         if (object->property(propertyName.toLatin1()).isValid()) {
629             QDeclarativeProperty property(object, propertyName);
630             QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(property);
631             if (oldBinding) {
632                 QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::setBinding(property, 0);
633                 if (oldBinding)
634                     oldBinding->destroy();
635             }
636             if (property.isResettable()) {
637                 // Note: this will reset the property in any case, without regard to states
638                 // Right now almost no QDeclarativeItem has reset methods for its properties (with the
639                 // notable exception of QDeclarativeAnchors), so this is not a big issue
640                 // later on, setBinding does take states into account
641                 property.reset();
642             } else {
643                 // overwrite with default value
644                 if (QDeclarativeType *objType = QDeclarativeMetaType::qmlType(object->metaObject())) {
645                     if (QObject *emptyObject = objType->create()) {
646                         if (emptyObject->property(propertyName.toLatin1()).isValid()) {
647                             QVariant defaultValue = QDeclarativeProperty(emptyObject, propertyName).read();
648                             if (defaultValue.isValid()) {
649                                 setBinding(objectId, propertyName, defaultValue, true);
650                             }
651                         }
652                         delete emptyObject;
653                     }
654                 }
655             }
656         } else if (hasValidSignal(object, propertyName)) {
657             QDeclarativeProperty property(object, propertyName, context);
658             QDeclarativePropertyPrivate::setSignalExpression(property, 0);
659     } else {
660             if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
661                 propertyChanges->removeProperty(propertyName);
662             }
663         }
664     }
665 }
666
667 void QDeclarativeEngineDebugService::setMethodBody(int objectId, const QString &method, const QString &body)
668 {
669     QObject *object = objectForId(objectId);
670     QDeclarativeContext *context = qmlContext(object);
671     if (!object || !context || !context->engine())
672         return;
673     QDeclarativeContextData *contextData = QDeclarativeContextData::get(context);
674     if (!contextData)
675         return;
676
677     QDeclarativePropertyCache::Data dummy;
678     QDeclarativePropertyCache::Data *prop = 
679         QDeclarativePropertyCache::property(context->engine(), object, method, dummy);
680
681     if (!prop || !prop->isVMEFunction())
682         return;
683
684     QMetaMethod metaMethod = object->metaObject()->method(prop->coreIndex);
685     QList<QByteArray> paramNames = metaMethod.parameterNames();
686
687     QString paramStr;
688     for (int ii = 0; ii < paramNames.count(); ++ii) {
689         if (ii != 0) paramStr.append(QLatin1String(","));
690         paramStr.append(QString::fromUtf8(paramNames.at(ii)));
691     }
692
693     QString jsfunction = QLatin1String("(function ") + method + QLatin1String("(") + paramStr + 
694                          QLatin1String(") {");
695     jsfunction += body;
696     jsfunction += QLatin1String("\n})");
697
698     QDeclarativeVMEMetaObject *vmeMetaObject = 
699         static_cast<QDeclarativeVMEMetaObject*>(QObjectPrivate::get(object)->metaObject);
700     Q_ASSERT(vmeMetaObject); // the fact we found the property above should guarentee this
701
702     int lineNumber = vmeMetaObject->vmeMethodLineNumber(prop->coreIndex);
703     vmeMetaObject->setVmeMethod(prop->coreIndex, QDeclarativeExpressionPrivate::evalFunction(contextData, object, jsfunction, contextData->url.toString(), lineNumber));
704 }
705
706 void QDeclarativeEngineDebugService::propertyChanged(int id, int objectId, const QMetaProperty &property, const QVariant &value)
707 {
708     QByteArray reply;
709     QDataStream rs(&reply, QIODevice::WriteOnly);
710
711     rs << QByteArray("UPDATE_WATCH") << id << objectId << QByteArray(property.name()) << valueContents(value);
712
713     sendMessage(reply);
714 }
715
716 void QDeclarativeEngineDebugService::addEngine(QDeclarativeEngine *engine)
717 {
718     Q_ASSERT(engine);
719     Q_ASSERT(!m_engines.contains(engine));
720
721     m_engines.append(engine);
722 }
723
724 void QDeclarativeEngineDebugService::remEngine(QDeclarativeEngine *engine)
725 {
726     Q_ASSERT(engine);
727     Q_ASSERT(m_engines.contains(engine));
728
729     m_engines.removeAll(engine);
730 }
731
732 void QDeclarativeEngineDebugService::objectCreated(QDeclarativeEngine *engine, QObject *object)
733 {
734     Q_ASSERT(engine);
735     Q_ASSERT(m_engines.contains(engine));
736
737     int engineId = QDeclarativeDebugService::idForObject(engine);
738     int objectId = QDeclarativeDebugService::idForObject(object);
739
740     QByteArray reply;
741     QDataStream rs(&reply, QIODevice::WriteOnly);
742
743     rs << QByteArray("OBJECT_CREATED") << engineId << objectId;
744     sendMessage(reply);
745 }
746
747 QT_END_NAMESPACE