1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmldebugservice_p.h"
43 #include "qqmldebugservice_p_p.h"
44 #include "qqmldebugserver_p.h"
46 #include <QtCore/QDebug>
47 #include <QtCore/QStringList>
51 QQmlDebugServicePrivate::QQmlDebugServicePrivate()
56 QQmlDebugService::QQmlDebugService(const QString &name, float version, QObject *parent)
57 : QObject(*(new QQmlDebugServicePrivate), parent)
59 Q_D(QQmlDebugService);
62 d->server = QQmlDebugServer::instance();
63 d->state = QQmlDebugService::NotConnected;
68 QQmlDebugService::QQmlDebugService(QQmlDebugServicePrivate &dd,
69 const QString &name, float version, QObject *parent)
72 Q_D(QQmlDebugService);
75 d->server = QQmlDebugServer::instance();
76 d->state = QQmlDebugService::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 QQmlDebugService::State QQmlDebugService::registerService()
85 Q_D(QQmlDebugService);
89 if (d->server->serviceNames().contains(d->name)) {
90 qWarning() << "QQmlDebugService: Conflicting plugin name" << d->name;
93 d->server->addService(this);
98 QQmlDebugService::~QQmlDebugService()
100 Q_D(const QQmlDebugService);
102 d->server->removeService(this);
106 QString QQmlDebugService::name() const
108 Q_D(const QQmlDebugService);
112 float QQmlDebugService::version() const
114 Q_D(const QQmlDebugService);
118 QQmlDebugService::State QQmlDebugService::state() const
120 Q_D(const QQmlDebugService);
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 QQmlDebugService::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 *QQmlDebugService::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 QQmlDebugService::isDebuggingEnabled()
207 return QQmlDebugServer::instance() != 0;
210 bool QQmlDebugService::hasDebuggingClient()
212 return QQmlDebugServer::instance() != 0
213 && QQmlDebugServer::instance()->hasDebuggingClient();
216 QString QQmlDebugService::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 QQmlDebugService::sendMessage(const QByteArray &message)
233 sendMessages(QList<QByteArray>() << message);
236 void QQmlDebugService::sendMessages(const QList<QByteArray> &messages)
238 Q_D(QQmlDebugService);
240 if (state() != Enabled)
243 d->server->sendMessages(this, messages);
246 bool QQmlDebugService::waitForMessage()
248 Q_D(QQmlDebugService);
250 if (state() != Enabled)
253 return d->server->waitForMessage(this);
256 void QQmlDebugService::stateAboutToBeChanged(State)
260 void QQmlDebugService::stateChanged(State)
264 void QQmlDebugService::messageReceived(const QByteArray &)