1 /****************************************************************************
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qdeclarativedebugclient_p.h"
44 #include "qpacketprotocol_p.h"
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
49 namespace QmlJsDebugClient {
51 const int protocolVersion = 1;
52 const QString serverId = QLatin1String("QDeclarativeDebugServer");
53 const QString clientId = QLatin1String("QDeclarativeDebugClient");
55 class QDeclarativeDebugClientPrivate
57 // Q_DECLARE_PUBLIC(QDeclarativeDebugClient)
59 QDeclarativeDebugClientPrivate();
62 QDeclarativeDebugConnection *connection;
65 class QDeclarativeDebugConnectionPrivate : public QObject
69 QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c);
70 QDeclarativeDebugConnection *q;
71 QPacketProtocol *protocol;
74 QStringList serverPlugins;
75 QHash<QString, QDeclarativeDebugClient *> plugins;
77 void advertisePlugins();
84 QDeclarativeDebugConnectionPrivate::QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c)
85 : QObject(c), q(c), protocol(0), gotHello(false)
87 protocol = new QPacketProtocol(q, this);
88 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
89 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
92 void QDeclarativeDebugConnectionPrivate::advertisePlugins()
94 if (!q->isConnected() || !gotHello)
98 pack << serverId << 1 << plugins.keys();
103 void QDeclarativeDebugConnectionPrivate::connected()
106 pack << serverId << 0 << protocolVersion << plugins.keys();
107 protocol->send(pack);
111 void QDeclarativeDebugConnectionPrivate::readyRead()
114 QPacket pack = protocol->read();
119 bool validHello = false;
120 if (name == clientId) {
126 if (version == protocolVersion) {
127 pack >> serverPlugins;
134 qWarning("QDeclarativeDebugConnection: Invalid hello message");
135 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
140 QHash<QString, QDeclarativeDebugClient *>::Iterator iter = plugins.begin();
141 for (; iter != plugins.end(); ++iter) {
142 QDeclarativeDebugClient::Status newStatus = QDeclarativeDebugClient::Unavailable;
143 if (serverPlugins.contains(iter.key()))
144 newStatus = QDeclarativeDebugClient::Enabled;
145 iter.value()->statusChanged(newStatus);
149 while (protocol->packetsAvailable()) {
150 QPacket pack = protocol->read();
154 if (name == clientId) {
160 QStringList oldServerPlugins = serverPlugins;
161 pack >> serverPlugins;
163 QHash<QString, QDeclarativeDebugClient *>::Iterator iter = plugins.begin();
164 for (; iter != plugins.end(); ++iter) {
165 const QString pluginName = iter.key();
166 QDeclarativeDebugClient::Status newStatus = QDeclarativeDebugClient::Unavailable;
167 if (serverPlugins.contains(pluginName))
168 newStatus = QDeclarativeDebugClient::Enabled;
170 if (oldServerPlugins.contains(pluginName)
171 != serverPlugins.contains(pluginName)) {
172 iter.value()->statusChanged(newStatus);
176 qWarning() << "QDeclarativeDebugConnection: Unknown control message id" << op;
182 QHash<QString, QDeclarativeDebugClient *>::Iterator iter =
184 if (iter == plugins.end()) {
185 qWarning() << "QDeclarativeDebugConnection: Message received for missing plugin" << name;
187 (*iter)->messageReceived(message);
193 QDeclarativeDebugConnection::QDeclarativeDebugConnection(QObject *parent)
194 : QTcpSocket(parent), d(new QDeclarativeDebugConnectionPrivate(this))
198 QDeclarativeDebugConnection::~QDeclarativeDebugConnection()
200 QHash<QString, QDeclarativeDebugClient*>::iterator iter = d->plugins.begin();
201 for (; iter != d->plugins.end(); ++iter) {
202 iter.value()->d_func()->connection = 0;
203 iter.value()->statusChanged(QDeclarativeDebugClient::NotConnected);
207 bool QDeclarativeDebugConnection::isConnected() const
209 return state() == ConnectedState;
212 QDeclarativeDebugClientPrivate::QDeclarativeDebugClientPrivate()
217 QDeclarativeDebugClient::QDeclarativeDebugClient(const QString &name,
218 QDeclarativeDebugConnection *parent)
219 : QObject(parent), d_ptr(new QDeclarativeDebugClientPrivate())
221 Q_D(QDeclarativeDebugClient);
223 d->connection = parent;
228 if (d->connection->d->plugins.contains(name)) {
229 qWarning() << "QDeclarativeDebugClient: Conflicting plugin name" << name;
232 d->connection->d->plugins.insert(name, this);
233 d->connection->d->advertisePlugins();
237 QDeclarativeDebugClient::~QDeclarativeDebugClient()
239 Q_D(QDeclarativeDebugClient);
240 if (d->connection && d->connection->d) {
241 d->connection->d->plugins.remove(d->name);
242 d->connection->d->advertisePlugins();
246 QString QDeclarativeDebugClient::name() const
248 Q_D(const QDeclarativeDebugClient);
252 QDeclarativeDebugClient::Status QDeclarativeDebugClient::status() const
254 Q_D(const QDeclarativeDebugClient);
256 || !d->connection->isConnected()
257 || !d->connection->d->gotHello)
260 if (d->connection->d->serverPlugins.contains(d->name))
266 void QDeclarativeDebugClient::sendMessage(const QByteArray &message)
268 Q_D(QDeclarativeDebugClient);
269 if (status() != Enabled)
273 pack << d->name << message;
274 d->connection->d->protocol->send(pack);
275 d->connection->flush();
278 void QDeclarativeDebugClient::statusChanged(Status)
282 void QDeclarativeDebugClient::messageReceived(const QByteArray &)
288 #include <qdeclarativedebugclient.moc>