| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "private/qdeclarativedebugservice_p.h" | 
|---|
| 43 | #include "private/qdeclarativedebugservice_p_p.h" | 
|---|
| 44 | #include "private/qdeclarativedebugserver_p.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include <QtCore/QDebug> | 
|---|
| 47 | #include <QtCore/QStringList> | 
|---|
| 48 |  | 
|---|
| 49 | QT_BEGIN_NAMESPACE | 
|---|
| 50 |  | 
|---|
| 51 | QDeclarativeDebugServicePrivate::QDeclarativeDebugServicePrivate() | 
|---|
| 52 | : server(0) | 
|---|
| 53 | { | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | QDeclarativeDebugService::QDeclarativeDebugService(const QString &name, QObject *parent) | 
|---|
| 57 | : QObject(*(new QDeclarativeDebugServicePrivate), parent) | 
|---|
| 58 | { | 
|---|
| 59 | Q_D(QDeclarativeDebugService); | 
|---|
| 60 | d->name = name; | 
|---|
| 61 | d->server = QDeclarativeDebugServer::instance(); | 
|---|
| 62 | d->status = QDeclarativeDebugService::NotConnected; | 
|---|
| 63 |  | 
|---|
| 64 | if (!d->server) | 
|---|
| 65 | return; | 
|---|
| 66 |  | 
|---|
| 67 | if (d->server->serviceNames().contains(name)) { | 
|---|
| 68 | qWarning() << "QDeclarativeDebugService: Conflicting plugin name" << name; | 
|---|
| 69 | d->server = 0; | 
|---|
| 70 | } else { | 
|---|
| 71 | d->server->addService(this); | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | QDeclarativeDebugService::~QDeclarativeDebugService() | 
|---|
| 76 | { | 
|---|
| 77 | Q_D(const QDeclarativeDebugService); | 
|---|
| 78 | if (d->server) { | 
|---|
| 79 | d->server->removeService(this); | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | QString QDeclarativeDebugService::name() const | 
|---|
| 84 | { | 
|---|
| 85 | Q_D(const QDeclarativeDebugService); | 
|---|
| 86 | return d->name; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | QDeclarativeDebugService::Status QDeclarativeDebugService::status() const | 
|---|
| 90 | { | 
|---|
| 91 | Q_D(const QDeclarativeDebugService); | 
|---|
| 92 | return d->status; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | namespace { | 
|---|
| 96 |  | 
|---|
| 97 | struct ObjectReference | 
|---|
| 98 | { | 
|---|
| 99 | QPointer<QObject> object; | 
|---|
| 100 | int id; | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | struct ObjectReferenceHash | 
|---|
| 104 | { | 
|---|
| 105 | ObjectReferenceHash() : nextId(0) {} | 
|---|
| 106 |  | 
|---|
| 107 | QHash<QObject *, ObjectReference> objects; | 
|---|
| 108 | QHash<int, QObject *> ids; | 
|---|
| 109 |  | 
|---|
| 110 | int nextId; | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | } | 
|---|
| 114 | Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash); | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 | /*! | 
|---|
| 118 | Returns a unique id for \a object.  Calling this method multiple times | 
|---|
| 119 | for the same object will return the same id. | 
|---|
| 120 | */ | 
|---|
| 121 | int QDeclarativeDebugService::idForObject(QObject *object) | 
|---|
| 122 | { | 
|---|
| 123 | if (!object) | 
|---|
| 124 | return -1; | 
|---|
| 125 |  | 
|---|
| 126 | ObjectReferenceHash *hash = objectReferenceHash(); | 
|---|
| 127 | QHash<QObject *, ObjectReference>::Iterator iter = | 
|---|
| 128 | hash->objects.find(object); | 
|---|
| 129 |  | 
|---|
| 130 | if (iter == hash->objects.end()) { | 
|---|
| 131 | int id = hash->nextId++; | 
|---|
| 132 |  | 
|---|
| 133 | hash->ids.insert(id, object); | 
|---|
| 134 | iter = hash->objects.insert(object, ObjectReference()); | 
|---|
| 135 | iter->object = object; | 
|---|
| 136 | iter->id = id; | 
|---|
| 137 | } else if (iter->object != object) { | 
|---|
| 138 | int id = hash->nextId++; | 
|---|
| 139 |  | 
|---|
| 140 | hash->ids.remove(iter->id); | 
|---|
| 141 |  | 
|---|
| 142 | hash->ids.insert(id, object); | 
|---|
| 143 | iter->object = object; | 
|---|
| 144 | iter->id = id; | 
|---|
| 145 | } | 
|---|
| 146 | return iter->id; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /*! | 
|---|
| 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. | 
|---|
| 153 | */ | 
|---|
| 154 | QObject *QDeclarativeDebugService::objectForId(int id) | 
|---|
| 155 | { | 
|---|
| 156 | ObjectReferenceHash *hash = objectReferenceHash(); | 
|---|
| 157 |  | 
|---|
| 158 | QHash<int, QObject *>::Iterator iter = hash->ids.find(id); | 
|---|
| 159 | if (iter == hash->ids.end()) | 
|---|
| 160 | return 0; | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 | QHash<QObject *, ObjectReference>::Iterator objIter = | 
|---|
| 164 | hash->objects.find(*iter); | 
|---|
| 165 | Q_ASSERT(objIter != hash->objects.end()); | 
|---|
| 166 |  | 
|---|
| 167 | if (objIter->object == 0) { | 
|---|
| 168 | hash->ids.erase(iter); | 
|---|
| 169 | hash->objects.erase(objIter); | 
|---|
| 170 | return 0; | 
|---|
| 171 | } else { | 
|---|
| 172 | return *iter; | 
|---|
| 173 | } | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | bool QDeclarativeDebugService::isDebuggingEnabled() | 
|---|
| 177 | { | 
|---|
| 178 | return QDeclarativeDebugServer::instance() != 0; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | bool QDeclarativeDebugService::hasDebuggingClient() | 
|---|
| 182 | { | 
|---|
| 183 | return QDeclarativeDebugServer::instance() != 0 | 
|---|
| 184 | && QDeclarativeDebugServer::instance()->hasDebuggingClient(); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | QString QDeclarativeDebugService::objectToString(QObject *obj) | 
|---|
| 188 | { | 
|---|
| 189 | if(!obj) | 
|---|
| 190 | return QLatin1String("NULL"); | 
|---|
| 191 |  | 
|---|
| 192 | QString objectName = obj->objectName(); | 
|---|
| 193 | if(objectName.isEmpty()) | 
|---|
| 194 | objectName = QLatin1String("<unnamed>"); | 
|---|
| 195 |  | 
|---|
| 196 | QString rv = QString::fromUtf8(obj->metaObject()->className()) + | 
|---|
| 197 | QLatin1String(": ") + objectName; | 
|---|
| 198 |  | 
|---|
| 199 | return rv; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | void QDeclarativeDebugService::sendMessage(const QByteArray &message) | 
|---|
| 203 | { | 
|---|
| 204 | Q_D(QDeclarativeDebugService); | 
|---|
| 205 |  | 
|---|
| 206 | if (status() != Enabled) | 
|---|
| 207 | return; | 
|---|
| 208 |  | 
|---|
| 209 | d->server->sendMessage(this, message); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | void QDeclarativeDebugService::statusChanged(Status) | 
|---|
| 213 | { | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | void QDeclarativeDebugService::messageReceived(const QByteArray &) | 
|---|
| 217 | { | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | QT_END_NAMESPACE | 
|---|