Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qdeclarativedebugservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativedebugservice_p.h"
43 #include "qdeclarativedebugservice_p_p.h"
44 #include "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, float version, QObject *parent)
57     : QObject(*(new QDeclarativeDebugServicePrivate), parent)
58 {
59     Q_D(QDeclarativeDebugService);
60     d->name = name;
61     d->version = version;
62     d->server = QDeclarativeDebugServer::instance();
63     d->status = QDeclarativeDebugService::NotConnected;
64
65
66 }
67
68 QDeclarativeDebugService::QDeclarativeDebugService(QDeclarativeDebugServicePrivate &dd,
69                                                    const QString &name, float version, QObject *parent)
70     : QObject(dd, parent)
71 {
72     Q_D(QDeclarativeDebugService);
73     d->name = name;
74     d->version = version;
75     d->server = QDeclarativeDebugServer::instance();
76     d->status = QDeclarativeDebugService::NotConnected;
77 }
78
79 /**
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().
82   */
83 QDeclarativeDebugService::Status QDeclarativeDebugService::registerService()
84 {
85     Q_D(QDeclarativeDebugService);
86     if (!d->server)
87         return NotConnected;
88
89     if (d->server->serviceNames().contains(d->name)) {
90         qWarning() << "QDeclarativeDebugService: Conflicting plugin name" << d->name;
91         d->server = 0;
92     } else {
93         d->server->addService(this);
94     }
95     return status();
96 }
97
98 QDeclarativeDebugService::~QDeclarativeDebugService()
99 {
100     Q_D(const QDeclarativeDebugService);
101     if (d->server) {
102         d->server->removeService(this);
103     }
104 }
105
106 QString QDeclarativeDebugService::name() const
107 {
108     Q_D(const QDeclarativeDebugService);
109     return d->name;
110 }
111
112 float QDeclarativeDebugService::version() const
113 {
114     Q_D(const QDeclarativeDebugService);
115     return d->version;
116 }
117
118 QDeclarativeDebugService::Status QDeclarativeDebugService::status() const
119 {
120     Q_D(const QDeclarativeDebugService);
121     return d->status;
122 }
123
124 namespace {
125
126 struct ObjectReference
127 {
128     QPointer<QObject> object;
129     int id;
130 };
131
132 struct ObjectReferenceHash
133 {
134     ObjectReferenceHash() : nextId(0) {}
135
136     QHash<QObject *, ObjectReference> objects;
137     QHash<int, QObject *> ids;
138
139     int nextId;
140 };
141
142 }
143 Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash);
144
145
146 /*!
147     Returns a unique id for \a object.  Calling this method multiple times
148     for the same object will return the same id.
149 */
150 int QDeclarativeDebugService::idForObject(QObject *object)
151 {
152     if (!object)
153         return -1;
154
155     ObjectReferenceHash *hash = objectReferenceHash();
156     QHash<QObject *, ObjectReference>::Iterator iter =
157             hash->objects.find(object);
158
159     if (iter == hash->objects.end()) {
160         int id = hash->nextId++;
161
162         hash->ids.insert(id, object);
163         iter = hash->objects.insert(object, ObjectReference());
164         iter->object = object;
165         iter->id = id;
166     } else if (iter->object != object) {
167         int id = hash->nextId++;
168
169         hash->ids.remove(iter->id);
170
171         hash->ids.insert(id, object);
172         iter->object = object;
173         iter->id = id;
174     }
175     return iter->id;
176 }
177
178 /*!
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.
182 */
183 QObject *QDeclarativeDebugService::objectForId(int id)
184 {
185     ObjectReferenceHash *hash = objectReferenceHash();
186
187     QHash<int, QObject *>::Iterator iter = hash->ids.find(id);
188     if (iter == hash->ids.end())
189         return 0;
190
191
192     QHash<QObject *, ObjectReference>::Iterator objIter =
193             hash->objects.find(*iter);
194     Q_ASSERT(objIter != hash->objects.end());
195
196     if (objIter->object == 0) {
197         hash->ids.erase(iter);
198         hash->objects.erase(objIter);
199         return 0;
200     } else {
201         return *iter;
202     }
203 }
204
205 bool QDeclarativeDebugService::isDebuggingEnabled()
206 {
207     return QDeclarativeDebugServer::instance() != 0;
208 }
209
210 bool QDeclarativeDebugService::hasDebuggingClient()
211 {
212     return QDeclarativeDebugServer::instance() != 0
213             && QDeclarativeDebugServer::instance()->hasDebuggingClient();
214 }
215
216 QString QDeclarativeDebugService::objectToString(QObject *obj)
217 {
218     if(!obj)
219         return QLatin1String("NULL");
220
221     QString objectName = obj->objectName();
222     if(objectName.isEmpty())
223         objectName = QLatin1String("<unnamed>");
224
225     QString rv = QString::fromUtf8(obj->metaObject()->className()) +
226             QLatin1String(": ") + objectName;
227
228     return rv;
229 }
230
231 void QDeclarativeDebugService::sendMessage(const QByteArray &message)
232 {
233     sendMessages(QList<QByteArray>() << message);
234 }
235
236 void QDeclarativeDebugService::sendMessages(const QList<QByteArray> &messages)
237 {
238     Q_D(QDeclarativeDebugService);
239
240     if (status() != Enabled)
241         return;
242
243     d->server->sendMessages(this, messages);
244 }
245
246 bool QDeclarativeDebugService::waitForMessage()
247 {
248     Q_D(QDeclarativeDebugService);
249
250     if (status() != Enabled)
251         return false;
252
253     return d->server->waitForMessage(this);
254 }
255
256 void QDeclarativeDebugService::statusChanged(Status)
257 {
258 }
259
260 void QDeclarativeDebugService::messageReceived(const QByteArray &)
261 {
262 }
263
264 QT_END_NAMESPACE