QmlDebugging: Revert the names of services
[profile/ivi/qtdeclarative.git] / src / qml / debugger / qqmldebugservice.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 QtQml 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 "qqmldebugservice_p.h"
43 #include "qqmldebugservice_p_p.h"
44 #include "qqmldebugserver_p.h"
45
46 #include <QtCore/QDebug>
47 #include <QtCore/QStringList>
48
49 QT_BEGIN_NAMESPACE
50
51 QQmlDebugServicePrivate::QQmlDebugServicePrivate()
52     : server(0)
53 {
54 }
55
56 QQmlDebugService::QQmlDebugService(const QString &name, float version, QObject *parent)
57     : QObject(*(new QQmlDebugServicePrivate), parent)
58 {
59     Q_D(QQmlDebugService);
60     d->name = name;
61     d->version = version;
62     d->server = QQmlDebugServer::instance();
63     d->state = QQmlDebugService::NotConnected;
64
65
66 }
67
68 QQmlDebugService::QQmlDebugService(QQmlDebugServicePrivate &dd,
69                                                    const QString &name, float version, QObject *parent)
70     : QObject(dd, parent)
71 {
72     Q_D(QQmlDebugService);
73     d->name = name;
74     d->version = version;
75     d->server = QQmlDebugServer::instance();
76     d->state = QQmlDebugService::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 QQmlDebugService::State QQmlDebugService::registerService()
84 {
85     Q_D(QQmlDebugService);
86     if (!d->server)
87         return NotConnected;
88
89     if (d->server->serviceNames().contains(d->name)) {
90         qWarning() << "QQmlDebugService: Conflicting plugin name" << d->name;
91         d->server = 0;
92     } else {
93         d->server->addService(this);
94     }
95     return state();
96 }
97
98 QQmlDebugService::~QQmlDebugService()
99 {
100     Q_D(const QQmlDebugService);
101     if (d->server) {
102         d->server->removeService(this);
103     }
104 }
105
106 QString QQmlDebugService::name() const
107 {
108     Q_D(const QQmlDebugService);
109     return d->name;
110 }
111
112 float QQmlDebugService::version() const
113 {
114     Q_D(const QQmlDebugService);
115     return d->version;
116 }
117
118 QQmlDebugService::State QQmlDebugService::state() const
119 {
120     Q_D(const QQmlDebugService);
121     return d->state;
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 QQmlDebugService::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 *QQmlDebugService::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 QQmlDebugService::isDebuggingEnabled()
206 {
207     return QQmlDebugServer::instance() != 0;
208 }
209
210 bool QQmlDebugService::hasDebuggingClient()
211 {
212     return QQmlDebugServer::instance() != 0
213             && QQmlDebugServer::instance()->hasDebuggingClient();
214 }
215
216 QString QQmlDebugService::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 QQmlDebugService::sendMessage(const QByteArray &message)
232 {
233     sendMessages(QList<QByteArray>() << message);
234 }
235
236 void QQmlDebugService::sendMessages(const QList<QByteArray> &messages)
237 {
238     Q_D(QQmlDebugService);
239
240     if (state() != Enabled)
241         return;
242
243     d->server->sendMessages(this, messages);
244 }
245
246 bool QQmlDebugService::waitForMessage()
247 {
248     Q_D(QQmlDebugService);
249
250     if (state() != Enabled)
251         return false;
252
253     return d->server->waitForMessage(this);
254 }
255
256 void QQmlDebugService::stateAboutToBeChanged(State)
257 {
258 }
259
260 void QQmlDebugService::stateChanged(State)
261 {
262 }
263
264 void QQmlDebugService::messageReceived(const QByteArray &)
265 {
266 }
267
268 QT_END_NAMESPACE