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 "private/qdeclarativedebugclient_p.h"
44 #include "private/qpacketprotocol_p.h"
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
49 #include <private/qobject_p.h>
53 const int protocolVersion = 1;
54 const QString serverId = QLatin1String("QDeclarativeDebugServer");
55 const QString clientId = QLatin1String("QDeclarativeDebugClient");
57 class QDeclarativeDebugClientPrivate : public QObjectPrivate
59 Q_DECLARE_PUBLIC(QDeclarativeDebugClient)
61 QDeclarativeDebugClientPrivate();
64 QDeclarativeDebugConnection *connection;
67 class QDeclarativeDebugConnectionPrivate : public QObject
71 QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c);
72 QDeclarativeDebugConnection *q;
73 QPacketProtocol *protocol;
76 QStringList serverPlugins;
77 QHash<QString, QDeclarativeDebugClient *> plugins;
79 void advertisePlugins();
86 QDeclarativeDebugConnectionPrivate::QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c)
87 : QObject(c), q(c), protocol(0), gotHello(false)
89 protocol = new QPacketProtocol(q, this);
90 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
91 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
94 void QDeclarativeDebugConnectionPrivate::advertisePlugins()
96 if (!q->isConnected())
100 pack << serverId << 1 << plugins.keys();
101 protocol->send(pack);
105 void QDeclarativeDebugConnectionPrivate::connected()
108 pack << serverId << 0 << protocolVersion << plugins.keys();
109 protocol->send(pack);
113 void QDeclarativeDebugConnectionPrivate::readyRead()
116 QPacket pack = protocol->read();
121 bool validHello = false;
122 if (name == clientId) {
128 if (version == protocolVersion) {
129 pack >> serverPlugins;
136 qWarning("QDeclarativeDebugConnection: Invalid hello message");
137 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
143 QHash<QString, QDeclarativeDebugClient *>::Iterator iter = plugins.begin();
144 for (; iter != plugins.end(); ++iter) {
145 QDeclarativeDebugClient::Status newStatus = QDeclarativeDebugClient::Unavailable;
146 if (serverPlugins.contains(iter.key()))
147 newStatus = QDeclarativeDebugClient::Enabled;
148 iter.value()->statusChanged(newStatus);
152 while (protocol->packetsAvailable()) {
153 QPacket pack = protocol->read();
157 if (name == clientId) {
163 QStringList oldServerPlugins = serverPlugins;
164 pack >> serverPlugins;
166 QHash<QString, QDeclarativeDebugClient *>::Iterator iter = plugins.begin();
167 for (; iter != plugins.end(); ++iter) {
168 const QString pluginName = iter.key();
169 QDeclarativeDebugClient::Status newStatus = QDeclarativeDebugClient::Unavailable;
170 if (serverPlugins.contains(pluginName))
171 newStatus = QDeclarativeDebugClient::Enabled;
173 if (oldServerPlugins.contains(pluginName)
174 != serverPlugins.contains(pluginName)) {
175 iter.value()->statusChanged(newStatus);
179 qWarning() << "QDeclarativeDebugConnection: Unknown control message id" << op;
185 QHash<QString, QDeclarativeDebugClient *>::Iterator iter =
187 if (iter == plugins.end()) {
188 qWarning() << "QDeclarativeDebugConnection: Message received for missing plugin" << name;
190 (*iter)->messageReceived(message);
196 QDeclarativeDebugConnection::QDeclarativeDebugConnection(QObject *parent)
197 : QTcpSocket(parent), d(new QDeclarativeDebugConnectionPrivate(this))
201 QDeclarativeDebugConnection::~QDeclarativeDebugConnection()
203 QHash<QString, QDeclarativeDebugClient*>::iterator iter = d->plugins.begin();
204 for (; iter != d->plugins.end(); ++iter) {
205 iter.value()->d_func()->connection = 0;
206 iter.value()->statusChanged(QDeclarativeDebugClient::NotConnected);
210 bool QDeclarativeDebugConnection::isConnected() const
212 return state() == ConnectedState;
215 QDeclarativeDebugClientPrivate::QDeclarativeDebugClientPrivate()
220 QDeclarativeDebugClient::QDeclarativeDebugClient(const QString &name,
221 QDeclarativeDebugConnection *parent)
222 : QObject(*(new QDeclarativeDebugClientPrivate), parent)
224 Q_D(QDeclarativeDebugClient);
226 d->connection = parent;
231 if (d->connection->d->plugins.contains(name)) {
232 qWarning() << "QDeclarativeDebugClient: Conflicting plugin name" << name;
235 d->connection->d->plugins.insert(name, this);
236 d->connection->d->advertisePlugins();
240 QDeclarativeDebugClient::~QDeclarativeDebugClient()
242 Q_D(const QDeclarativeDebugClient);
243 if (d->connection && d->connection->d) {
244 d->connection->d->plugins.remove(d->name);
245 d->connection->d->advertisePlugins();
249 QString QDeclarativeDebugClient::name() const
251 Q_D(const QDeclarativeDebugClient);
255 QDeclarativeDebugClient::Status QDeclarativeDebugClient::status() const
257 Q_D(const QDeclarativeDebugClient);
259 || !d->connection->isConnected()
260 || !d->connection->d->gotHello)
263 if (d->connection->d->serverPlugins.contains(d->name))
269 void QDeclarativeDebugClient::sendMessage(const QByteArray &message)
271 Q_D(QDeclarativeDebugClient);
273 if (status() != Enabled)
277 pack << d->name << message;
278 d->connection->d->protocol->send(pack);
279 d->connection->d->q->flush();
282 void QDeclarativeDebugClient::statusChanged(Status)
286 void QDeclarativeDebugClient::messageReceived(const QByteArray &)
292 #include <qdeclarativedebugclient.moc>