1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qdeclarativedebugservice_p.h"
43 #include "private/qdeclarativedebugservice_p_p.h"
44 #include "private/qdeclarativedebugserver_p.h"
46 #include <QtCore/QDebug>
47 #include <QtCore/QStringList>
51 QDeclarativeDebugServicePrivate::QDeclarativeDebugServicePrivate()
56 QDeclarativeDebugService::QDeclarativeDebugService(const QString &name, QObject *parent)
57 : QObject(*(new QDeclarativeDebugServicePrivate), parent)
59 Q_D(QDeclarativeDebugService);
61 d->server = QDeclarativeDebugServer::instance();
62 d->status = QDeclarativeDebugService::NotConnected;
67 if (d->server->serviceNames().contains(name)) {
68 qWarning() << "QDeclarativeDebugService: Conflicting plugin name" << name;
71 d->server->addService(this);
75 QDeclarativeDebugService::~QDeclarativeDebugService()
77 Q_D(const QDeclarativeDebugService);
79 d->server->removeService(this);
83 QString QDeclarativeDebugService::name() const
85 Q_D(const QDeclarativeDebugService);
89 QDeclarativeDebugService::Status QDeclarativeDebugService::status() const
91 Q_D(const QDeclarativeDebugService);
97 struct ObjectReference
99 QPointer<QObject> object;
103 struct ObjectReferenceHash
105 ObjectReferenceHash() : nextId(0) {}
107 QHash<QObject *, ObjectReference> objects;
108 QHash<int, QObject *> ids;
114 Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash);
118 Returns a unique id for \a object. Calling this method multiple times
119 for the same object will return the same id.
121 int QDeclarativeDebugService::idForObject(QObject *object)
126 ObjectReferenceHash *hash = objectReferenceHash();
127 QHash<QObject *, ObjectReference>::Iterator iter =
128 hash->objects.find(object);
130 if (iter == hash->objects.end()) {
131 int id = hash->nextId++;
133 hash->ids.insert(id, object);
134 iter = hash->objects.insert(object, ObjectReference());
135 iter->object = object;
137 } else if (iter->object != object) {
138 int id = hash->nextId++;
140 hash->ids.remove(iter->id);
142 hash->ids.insert(id, object);
143 iter->object = object;
150 Returns the object for unique \a id. If the object has not previously been
151 assigned an id, through idForObject(), then 0 is returned. If the object
152 has been destroyed, 0 is returned.
154 QObject *QDeclarativeDebugService::objectForId(int id)
156 ObjectReferenceHash *hash = objectReferenceHash();
158 QHash<int, QObject *>::Iterator iter = hash->ids.find(id);
159 if (iter == hash->ids.end())
163 QHash<QObject *, ObjectReference>::Iterator objIter =
164 hash->objects.find(*iter);
165 Q_ASSERT(objIter != hash->objects.end());
167 if (objIter->object == 0) {
168 hash->ids.erase(iter);
169 hash->objects.erase(objIter);
176 bool QDeclarativeDebugService::isDebuggingEnabled()
178 return QDeclarativeDebugServer::instance() != 0;
181 bool QDeclarativeDebugService::hasDebuggingClient()
183 return QDeclarativeDebugServer::instance() != 0
184 && QDeclarativeDebugServer::instance()->hasDebuggingClient();
187 QString QDeclarativeDebugService::objectToString(QObject *obj)
190 return QLatin1String("NULL");
192 QString objectName = obj->objectName();
193 if(objectName.isEmpty())
194 objectName = QLatin1String("<unnamed>");
196 QString rv = QString::fromUtf8(obj->metaObject()->className()) +
197 QLatin1String(": ") + objectName;
202 void QDeclarativeDebugService::sendMessage(const QByteArray &message)
204 Q_D(QDeclarativeDebugService);
206 if (status() != Enabled)
209 d->server->sendMessage(this, message);
212 void QDeclarativeDebugService::statusChanged(Status)
216 void QDeclarativeDebugService::messageReceived(const QByteArray &)