OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljsdebugclient / qdeclarativedebugclient.cpp
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 ** 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
14 ** this package.
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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativedebugclient_p.h"
43
44 #include "qpacketprotocol_p.h"
45
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
48
49 namespace QmlJsDebugClient {
50
51 const int protocolVersion = 1;
52 const QString serverId = QLatin1String("QDeclarativeDebugServer");
53 const QString clientId = QLatin1String("QDeclarativeDebugClient");
54
55 class QDeclarativeDebugClientPrivate
56 {
57 //    Q_DECLARE_PUBLIC(QDeclarativeDebugClient)
58 public:
59     QDeclarativeDebugClientPrivate();
60
61     QString name;
62     QDeclarativeDebugConnection *connection;
63 };
64
65 class QDeclarativeDebugConnectionPrivate : public QObject
66 {
67     Q_OBJECT
68 public:
69     QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c);
70     QDeclarativeDebugConnection *q;
71     QPacketProtocol *protocol;
72
73     bool gotHello;
74     QStringList serverPlugins;
75     QHash<QString, QDeclarativeDebugClient *> plugins;
76
77     void advertisePlugins();
78
79 public Q_SLOTS:
80     void connected();
81     void readyRead();
82 };
83
84 QDeclarativeDebugConnectionPrivate::QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c)
85 : QObject(c), q(c), protocol(0), gotHello(false)
86 {
87     protocol = new QPacketProtocol(q, this);
88     QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
89     QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
90 }
91
92 void QDeclarativeDebugConnectionPrivate::advertisePlugins()
93 {
94     if (!q->isConnected() || !gotHello)
95         return;
96
97     QPacket pack;
98     pack << serverId << 1 << plugins.keys();
99     protocol->send(pack);
100     q->flush();
101 }
102
103 void QDeclarativeDebugConnectionPrivate::connected()
104 {
105     QPacket pack;
106     pack << serverId << 0 << protocolVersion << plugins.keys();
107     protocol->send(pack);
108     q->flush();
109 }
110
111 void QDeclarativeDebugConnectionPrivate::readyRead()
112 {
113     if (!gotHello) {
114         QPacket pack = protocol->read();
115         QString name;
116
117         pack >> name;
118
119         bool validHello = false;
120         if (name == clientId) {
121             int op = -1;
122             pack >> op;
123             if (op == 0) {
124                 int version = -1;
125                 pack >> version;
126                 if (version == protocolVersion) {
127                     pack >> serverPlugins;
128                     validHello = true;
129                 }
130             }
131         }
132
133         if (!validHello) {
134             qWarning("QDeclarativeDebugConnection: Invalid hello message");
135             QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
136             return;
137         }
138         gotHello = true;
139
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);
146         }
147     }
148
149     while (protocol->packetsAvailable()) {
150         QPacket pack = protocol->read();
151         QString name;
152         pack >> name;
153
154         if (name == clientId) {
155             int op = -1;
156             pack >> op;
157
158             if (op == 1) {
159                 // Service Discovery
160                 QStringList oldServerPlugins = serverPlugins;
161                 pack >> serverPlugins;
162
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;
169
170                     if (oldServerPlugins.contains(pluginName)
171                             != serverPlugins.contains(pluginName)) {
172                         iter.value()->statusChanged(newStatus);
173                     }
174                 }
175             } else {
176                 qWarning() << "QDeclarativeDebugConnection: Unknown control message id" << op;
177             }
178         } else {
179             QByteArray message;
180             pack >> message;
181
182             QHash<QString, QDeclarativeDebugClient *>::Iterator iter =
183                 plugins.find(name);
184             if (iter == plugins.end()) {
185                 qWarning() << "QDeclarativeDebugConnection: Message received for missing plugin" << name;
186             } else {
187                 (*iter)->messageReceived(message);
188             }
189         }
190     }
191 }
192
193 QDeclarativeDebugConnection::QDeclarativeDebugConnection(QObject *parent)
194 : QTcpSocket(parent), d(new QDeclarativeDebugConnectionPrivate(this))
195 {
196 }
197
198 QDeclarativeDebugConnection::~QDeclarativeDebugConnection()
199 {
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);
204     }
205 }
206
207 bool QDeclarativeDebugConnection::isConnected() const
208 {
209     return state() == ConnectedState;
210 }
211
212 QDeclarativeDebugClientPrivate::QDeclarativeDebugClientPrivate()
213 : connection(0)
214 {
215 }
216
217 QDeclarativeDebugClient::QDeclarativeDebugClient(const QString &name, 
218                                            QDeclarativeDebugConnection *parent)
219 : QObject(parent), d_ptr(new QDeclarativeDebugClientPrivate())
220 {
221     Q_D(QDeclarativeDebugClient);
222     d->name = name;
223     d->connection = parent;
224
225     if (!d->connection)
226         return;
227
228     if (d->connection->d->plugins.contains(name)) {
229         qWarning() << "QDeclarativeDebugClient: Conflicting plugin name" << name;
230         d->connection = 0;
231     } else {
232         d->connection->d->plugins.insert(name, this);
233         d->connection->d->advertisePlugins();
234     }
235 }
236
237 QDeclarativeDebugClient::~QDeclarativeDebugClient()
238 {
239     Q_D(QDeclarativeDebugClient);
240     if (d->connection && d->connection->d) {
241         d->connection->d->plugins.remove(d->name);
242         d->connection->d->advertisePlugins();
243     }
244 }
245
246 QString QDeclarativeDebugClient::name() const
247 {
248     Q_D(const QDeclarativeDebugClient);
249     return d->name;
250 }
251
252 QDeclarativeDebugClient::Status QDeclarativeDebugClient::status() const
253 {
254     Q_D(const QDeclarativeDebugClient);
255     if (!d->connection
256         || !d->connection->isConnected()
257         || !d->connection->d->gotHello)
258         return NotConnected;
259
260     if (d->connection->d->serverPlugins.contains(d->name))
261         return Enabled;
262
263     return Unavailable;
264 }
265
266 void QDeclarativeDebugClient::sendMessage(const QByteArray &message)
267 {
268     Q_D(QDeclarativeDebugClient);
269     if (status() != Enabled)
270         return;
271
272     QPacket pack;
273     pack << d->name << message;
274     d->connection->d->protocol->send(pack);
275     d->connection->flush();
276 }
277
278 void QDeclarativeDebugClient::statusChanged(Status)
279 {
280 }
281
282 void QDeclarativeDebugClient::messageReceived(const QByteArray &)
283 {
284 }
285
286 }
287
288 #include <qdeclarativedebugclient.moc>