Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeenginedebug.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 ** No Commercial Usage
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativeenginedebug_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(QDeclarativeEngineDebugServer, qmlEngineDebugServer);
63
64 QDeclarativeEngineDebugServer *QDeclarativeEngineDebugServer::instance()
65 {
66     return qmlEngineDebugServer();
67 }
68
69 QDeclarativeEngineDebugServer::QDeclarativeEngineDebugServer(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 QDeclarativeEngineDebugServer::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                         QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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                         QDeclarativeEngineDebugServer::QDeclarativeObjectProperty &data)
103 {
104     int type;
105     ds >> type >> data.name >> data.value >> data.valueTypeName
106        >> data.binding >> data.hasNotifySignal;
107     data.type = (QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::QDeclarativeObjectProperty 
135 QDeclarativeEngineDebugServer::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     QVariant value;
151     if (prop.userType() != 0) {
152         value = prop.read(obj);
153     }
154     rv.value = valueContents(value);
155
156     if (QDeclarativeValueTypeFactory::isValueType(prop.userType())) {
157         rv.type = QDeclarativeObjectProperty::Basic;
158     } else if (QDeclarativeMetaType::isQObject(prop.userType()))  {
159         rv.type = QDeclarativeObjectProperty::Object;
160     } else if (QDeclarativeMetaType::isList(prop.userType())) {
161         rv.type = QDeclarativeObjectProperty::List;
162     }
163
164     return rv;
165 }
166
167 QVariant QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::QDeclarativeObjectData 
349 QDeclarativeEngineDebugServer::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 QDeclarativeEngineDebugServer::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         ds >> objectId >> propertyName >> expr >> isLiteralValue;
528         setBinding(objectId, propertyName, expr, isLiteralValue);
529     } else if (type == "RESET_BINDING") {
530         int objectId;
531         QString propertyName;
532         ds >> objectId >> propertyName;
533         resetBinding(objectId, propertyName);
534     } else if (type == "SET_METHOD_BODY") {
535         int objectId;
536         QString methodName;
537         QString methodBody;
538         ds >> objectId >> methodName >> methodBody;
539         setMethodBody(objectId, methodName, methodBody);
540     }
541 }
542
543 void QDeclarativeEngineDebugServer::setBinding(int objectId,
544                                                const QString &propertyName,
545                                                const QVariant &expression,
546                                                bool isLiteralValue)
547 {
548     QObject *object = objectForId(objectId);
549     QDeclarativeContext *context = qmlContext(object);
550
551     if (object && context) {
552         QDeclarativeProperty property(object, propertyName, context);
553         if (property.isValid()) {
554
555             bool inBaseState = true;
556
557             foreach(QWeakPointer<QDeclarativeState> statePointer, m_allStates) {
558                 if (QDeclarativeState *state = statePointer.data()) {
559                     // here we assume that the revert list on itself defines the base state
560                     if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) {
561                         inBaseState = false;
562
563                         QDeclarativeBinding *newBinding = 0;
564                         if (!isLiteralValue) {
565                             newBinding = new QDeclarativeBinding(expression.toString(), object, context);
566                             newBinding->setTarget(property);
567                             newBinding->setNotifyOnValueChanged(true);
568                         }
569
570                         state->changeBindingInRevertList(object, propertyName, newBinding);
571
572                         if (isLiteralValue)
573                             state->changeValueInRevertList(object, propertyName, expression);
574                     }
575                 }
576             }
577
578             if (inBaseState) {
579                 if (isLiteralValue) {
580                     property.write(expression);
581                 } else if (hasValidSignal(object, propertyName)) {
582                     QDeclarativeExpression *declarativeExpression = new QDeclarativeExpression(context, object, expression.toString());
583                     QDeclarativeExpression *oldExpression = QDeclarativePropertyPrivate::setSignalExpression(property, declarativeExpression);
584                     declarativeExpression->setSourceLocation(oldExpression->sourceFile(), oldExpression->lineNumber());
585                 } else if (property.isProperty()) {
586                     QDeclarativeBinding *binding = new QDeclarativeBinding(expression.toString(), object, context);
587                     binding->setTarget(property);
588                     binding->setNotifyOnValueChanged(true);
589                     QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::setBinding(property, binding);
590                     if (oldBinding)
591                         oldBinding->destroy();
592                     binding->update();
593                 } else {
594                     qWarning() << "QDeclarativeEngineDebugServer::setBinding: unable to set property" << propertyName << "on object" << object;
595                 }
596             }
597
598         } else {
599             // not a valid property
600             if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
601                 if (isLiteralValue) {
602                     propertyChanges->changeValue(propertyName, expression);
603                 } else {
604                     propertyChanges->changeExpression(propertyName, expression.toString());
605                 }
606             } else {
607                 qWarning() << "QDeclarativeEngineDebugServer::setBinding: unable to set property" << propertyName << "on object" << object;
608             }
609         }
610     }
611 }
612
613 void QDeclarativeEngineDebugServer::resetBinding(int objectId, const QString &propertyName)
614 {
615     QObject *object = objectForId(objectId);
616     QDeclarativeContext *context = qmlContext(object);
617
618     if (object && context) {
619         if (object->property(propertyName.toLatin1()).isValid()) {
620             QDeclarativeProperty property(object, propertyName);
621             QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(property);
622             if (oldBinding) {
623                 QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::setBinding(property, 0);
624                 if (oldBinding)
625                     oldBinding->destroy();
626             }
627             if (property.isResettable()) {
628                 // Note: this will reset the property in any case, without regard to states
629                 // Right now almost no QDeclarativeItem has reset methods for its properties (with the
630                 // notable exception of QDeclarativeAnchors), so this is not a big issue
631                 // later on, setBinding does take states into account
632                 property.reset();
633             } else {
634                 // overwrite with default value
635                 if (QDeclarativeType *objType = QDeclarativeMetaType::qmlType(object->metaObject())) {
636                     if (QObject *emptyObject = objType->create()) {
637                         if (emptyObject->property(propertyName.toLatin1()).isValid()) {
638                             QVariant defaultValue = QDeclarativeProperty(emptyObject, propertyName).read();
639                             if (defaultValue.isValid()) {
640                                 setBinding(objectId, propertyName, defaultValue, true);
641                             }
642                         }
643                         delete emptyObject;
644                     }
645                 }
646             }
647         } else {
648             if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
649                 propertyChanges->removeProperty(propertyName);
650             }
651         }
652     }
653 }
654
655 void QDeclarativeEngineDebugServer::setMethodBody(int objectId, const QString &method, const QString &body)
656 {
657     QObject *object = objectForId(objectId);
658     QDeclarativeContext *context = qmlContext(object);
659     if (!object || !context || !context->engine())
660         return;
661     QDeclarativeContextData *contextData = QDeclarativeContextData::get(context);
662     if (!contextData)
663         return;
664
665     QDeclarativePropertyCache::Data dummy;
666     QDeclarativePropertyCache::Data *prop = 
667         QDeclarativePropertyCache::property(context->engine(), object, method, dummy);
668
669     if (!prop || !(prop->flags & QDeclarativePropertyCache::Data::IsVMEFunction))
670         return;
671
672     QMetaMethod metaMethod = object->metaObject()->method(prop->coreIndex);
673     QList<QByteArray> paramNames = metaMethod.parameterNames();
674
675     QString paramStr;
676     for (int ii = 0; ii < paramNames.count(); ++ii) {
677         if (ii != 0) paramStr.append(QLatin1String(","));
678         paramStr.append(QString::fromUtf8(paramNames.at(ii)));
679     }
680
681     QString jsfunction = QLatin1String("(function ") + method + QLatin1String("(") + paramStr + 
682                          QLatin1String(") {");
683     jsfunction += body;
684     jsfunction += QLatin1String("\n})");
685
686     QDeclarativeVMEMetaObject *vmeMetaObject = 
687         static_cast<QDeclarativeVMEMetaObject*>(QObjectPrivate::get(object)->metaObject);
688     Q_ASSERT(vmeMetaObject); // the fact we found the property above should guarentee this
689
690     int lineNumber = vmeMetaObject->vmeMethodLineNumber(prop->coreIndex);
691     vmeMetaObject->setVmeMethod(prop->coreIndex, QDeclarativeExpressionPrivate::evalInObjectScope(contextData, object, jsfunction, contextData->url.toString(), lineNumber, 0));
692 }
693
694 void QDeclarativeEngineDebugServer::propertyChanged(int id, int objectId, const QMetaProperty &property, const QVariant &value)
695 {
696     QByteArray reply;
697     QDataStream rs(&reply, QIODevice::WriteOnly);
698
699     rs << QByteArray("UPDATE_WATCH") << id << objectId << QByteArray(property.name()) << valueContents(value);
700
701     sendMessage(reply);
702 }
703
704 void QDeclarativeEngineDebugServer::addEngine(QDeclarativeEngine *engine)
705 {
706     Q_ASSERT(engine);
707     Q_ASSERT(!m_engines.contains(engine));
708
709     m_engines.append(engine);
710 }
711
712 void QDeclarativeEngineDebugServer::remEngine(QDeclarativeEngine *engine)
713 {
714     Q_ASSERT(engine);
715     Q_ASSERT(m_engines.contains(engine));
716
717     m_engines.removeAll(engine);
718 }
719
720 void QDeclarativeEngineDebugServer::objectCreated(QDeclarativeEngine *engine, QObject *object)
721 {
722     Q_ASSERT(engine);
723     Q_ASSERT(m_engines.contains(engine));
724
725     int engineId = QDeclarativeDebugService::idForObject(engine);
726     int objectId = QDeclarativeDebugService::idForObject(object);
727
728     QByteArray reply;
729     QDataStream rs(&reply, QIODevice::WriteOnly);
730
731     rs << QByteArray("OBJECT_CREATED") << engineId << objectId;
732     sendMessage(reply);
733 }
734
735 QT_END_NAMESPACE