1 /****************************************************************************
3 ** Copyright (C) 2012 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 ** 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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qdeclarativedebugservice_p.h"
43 #include "qdeclarativedebugservice_p_p.h"
44 #include "qdeclarativedebugserver_p.h"
46 #include <QtCore/QDebug>
47 #include <QtCore/QStringList>
51 QDeclarativeDebugServicePrivate::QDeclarativeDebugServicePrivate()
56 QDeclarativeDebugService::QDeclarativeDebugService(const QString &name, float version, QObject *parent)
57 : QObject(*(new QDeclarativeDebugServicePrivate), parent)
59 Q_D(QDeclarativeDebugService);
62 d->server = QDeclarativeDebugServer::instance();
63 d->status = QDeclarativeDebugService::NotConnected;
68 QDeclarativeDebugService::QDeclarativeDebugService(QDeclarativeDebugServicePrivate &dd,
69 const QString &name, float version, QObject *parent)
72 Q_D(QDeclarativeDebugService);
75 d->server = QDeclarativeDebugServer::instance();
76 d->status = QDeclarativeDebugService::NotConnected;
80 Registers the service. This should be called in the constructor of the inherited class. From
81 then on the service might get asynchronous calls to messageReceived().
83 QDeclarativeDebugService::Status QDeclarativeDebugService::registerService()
85 Q_D(QDeclarativeDebugService);
89 if (d->server->serviceNames().contains(d->name)) {
90 qWarning() << "QDeclarativeDebugService: Conflicting plugin name" << d->name;
93 d->server->addService(this);
98 QDeclarativeDebugService::~QDeclarativeDebugService()
100 Q_D(const QDeclarativeDebugService);
102 d->server->removeService(this);
106 QString QDeclarativeDebugService::name() const
108 Q_D(const QDeclarativeDebugService);
112 float QDeclarativeDebugService::version() const
114 Q_D(const QDeclarativeDebugService);
118 QDeclarativeDebugService::Status QDeclarativeDebugService::status() const
120 Q_D(const QDeclarativeDebugService);
126 struct ObjectReference
128 QPointer<QObject> object;
132 struct ObjectReferenceHash
134 ObjectReferenceHash() : nextId(0) {}
136 QHash<QObject *, ObjectReference> objects;
137 QHash<int, QObject *> ids;
143 Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash);
147 Returns a unique id for \a object. Calling this method multiple times
148 for the same object will return the same id.
150 int QDeclarativeDebugService::idForObject(QObject *object)
155 ObjectReferenceHash *hash = objectReferenceHash();
156 QHash<QObject *, ObjectReference>::Iterator iter =
157 hash->objects.find(object);
159 if (iter == hash->objects.end()) {
160 int id = hash->nextId++;
162 hash->ids.insert(id, object);
163 iter = hash->objects.insert(object, ObjectReference());
164 iter->object = object;
166 } else if (iter->object != object) {
167 int id = hash->nextId++;
169 hash->ids.remove(iter->id);
171 hash->ids.insert(id, object);
172 iter->object = object;
179 Returns the object for unique \a id. If the object has not previously been
180 assigned an id, through idForObject(), then 0 is returned. If the object
181 has been destroyed, 0 is returned.
183 QObject *QDeclarativeDebugService::objectForId(int id)
185 ObjectReferenceHash *hash = objectReferenceHash();
187 QHash<int, QObject *>::Iterator iter = hash->ids.find(id);
188 if (iter == hash->ids.end())
192 QHash<QObject *, ObjectReference>::Iterator objIter =
193 hash->objects.find(*iter);
194 Q_ASSERT(objIter != hash->objects.end());
196 if (objIter->object == 0) {
197 hash->ids.erase(iter);
198 hash->objects.erase(objIter);
205 bool QDeclarativeDebugService::isDebuggingEnabled()
207 return QDeclarativeDebugServer::instance() != 0;
210 bool QDeclarativeDebugService::hasDebuggingClient()
212 return QDeclarativeDebugServer::instance() != 0
213 && QDeclarativeDebugServer::instance()->hasDebuggingClient();
216 QString QDeclarativeDebugService::objectToString(QObject *obj)
219 return QLatin1String("NULL");
221 QString objectName = obj->objectName();
222 if(objectName.isEmpty())
223 objectName = QLatin1String("<unnamed>");
225 QString rv = QString::fromUtf8(obj->metaObject()->className()) +
226 QLatin1String(": ") + objectName;
231 void QDeclarativeDebugService::sendMessage(const QByteArray &message)
233 sendMessages(QList<QByteArray>() << message);
236 void QDeclarativeDebugService::sendMessages(const QList<QByteArray> &messages)
238 Q_D(QDeclarativeDebugService);
240 if (status() != Enabled)
243 d->server->sendMessages(this, messages);
246 bool QDeclarativeDebugService::waitForMessage()
248 Q_D(QDeclarativeDebugService);
250 if (status() != Enabled)
253 return d->server->waitForMessage(this);
256 void QDeclarativeDebugService::statusChanged(Status)
260 void QDeclarativeDebugService::messageReceived(const QByteArray &)