src/declarative/debugger/qdeclarativedebugclient.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "private/qdeclarativedebugclient_p.h"
       
    43 
       
    44 #include "private/qpacketprotocol_p.h"
       
    45 
       
    46 #include <QtCore/qdebug.h>
       
    47 #include <QtCore/qstringlist.h>
       
    48 
       
    49 #include <private/qobject_p.h>
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 class QDeclarativeDebugConnectionPrivate : public QObject
       
    54 {
       
    55     Q_OBJECT
       
    56 public:
       
    57     QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c);
       
    58     QDeclarativeDebugConnection *q;
       
    59     QPacketProtocol *protocol;
       
    60 
       
    61     QStringList enabled;
       
    62     QHash<QString, QDeclarativeDebugClient *> plugins;
       
    63 public Q_SLOTS:
       
    64     void connected();
       
    65     void readyRead();
       
    66 };
       
    67 
       
    68 QDeclarativeDebugConnectionPrivate::QDeclarativeDebugConnectionPrivate(QDeclarativeDebugConnection *c)
       
    69 : QObject(c), q(c), protocol(0)
       
    70 {
       
    71     protocol = new QPacketProtocol(q, this);
       
    72     QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
       
    73     QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
       
    74 }
       
    75 
       
    76 void QDeclarativeDebugConnectionPrivate::connected()
       
    77 {
       
    78     QPacket pack;
       
    79     pack << QString(QLatin1String("QDeclarativeDebugServer")) << enabled;
       
    80     protocol->send(pack);
       
    81 }
       
    82 
       
    83 void QDeclarativeDebugConnectionPrivate::readyRead()
       
    84 {
       
    85     QPacket pack = protocol->read();
       
    86     QString name; QByteArray message;
       
    87     pack >> name >> message;
       
    88 
       
    89     QHash<QString, QDeclarativeDebugClient *>::Iterator iter = 
       
    90         plugins.find(name);
       
    91     if (iter == plugins.end()) {
       
    92         qWarning() << "QDeclarativeDebugConnection: Message received for missing plugin" << name;
       
    93     } else {
       
    94         (*iter)->messageReceived(message);
       
    95     }
       
    96 }
       
    97 
       
    98 QDeclarativeDebugConnection::QDeclarativeDebugConnection(QObject *parent)
       
    99 : QTcpSocket(parent), d(new QDeclarativeDebugConnectionPrivate(this))
       
   100 {
       
   101 }
       
   102 
       
   103 bool QDeclarativeDebugConnection::isConnected() const
       
   104 {
       
   105     return state() == ConnectedState;
       
   106 }
       
   107 
       
   108 class QDeclarativeDebugClientPrivate : public QObjectPrivate
       
   109 {
       
   110     Q_DECLARE_PUBLIC(QDeclarativeDebugClient)
       
   111 public:
       
   112     QDeclarativeDebugClientPrivate();
       
   113 
       
   114     QString name;
       
   115     QDeclarativeDebugConnection *client;
       
   116     bool enabled;
       
   117 };
       
   118 
       
   119 QDeclarativeDebugClientPrivate::QDeclarativeDebugClientPrivate()
       
   120 : client(0), enabled(false)
       
   121 {
       
   122 }
       
   123 
       
   124 QDeclarativeDebugClient::QDeclarativeDebugClient(const QString &name, 
       
   125                                            QDeclarativeDebugConnection *parent)
       
   126 : QObject(*(new QDeclarativeDebugClientPrivate), parent)
       
   127 {
       
   128     Q_D(QDeclarativeDebugClient);
       
   129     d->name = name;
       
   130     d->client = parent;
       
   131 
       
   132     if (!d->client)
       
   133         return;
       
   134 
       
   135     if (d->client->d->plugins.contains(name)) {
       
   136         qWarning() << "QDeclarativeDebugClient: Conflicting plugin name" << name;
       
   137         d->client = 0;
       
   138     } else {
       
   139         d->client->d->plugins.insert(name, this);
       
   140     }
       
   141 }
       
   142 
       
   143 QString QDeclarativeDebugClient::name() const
       
   144 {
       
   145     Q_D(const QDeclarativeDebugClient);
       
   146     return d->name;
       
   147 }
       
   148 
       
   149 bool QDeclarativeDebugClient::isEnabled() const
       
   150 {
       
   151     Q_D(const QDeclarativeDebugClient);
       
   152     return d->enabled;
       
   153 }
       
   154 
       
   155 void QDeclarativeDebugClient::setEnabled(bool e)
       
   156 {
       
   157     Q_D(QDeclarativeDebugClient);
       
   158     if (e == d->enabled)
       
   159         return;
       
   160 
       
   161     d->enabled = e;
       
   162 
       
   163     if (d->client) {
       
   164         if (e) 
       
   165             d->client->d->enabled.append(d->name);
       
   166         else
       
   167             d->client->d->enabled.removeAll(d->name);
       
   168 
       
   169         if (d->client->state() == QTcpSocket::ConnectedState) {
       
   170             QPacket pack;
       
   171             pack << QString(QLatin1String("QDeclarativeDebugServer"));
       
   172             if (e) pack << (int)1;
       
   173             else pack << (int)2;
       
   174             pack << d->name;
       
   175             d->client->d->protocol->send(pack);
       
   176         }
       
   177     }
       
   178 }
       
   179 
       
   180 bool QDeclarativeDebugClient::isConnected() const
       
   181 {
       
   182     Q_D(const QDeclarativeDebugClient);
       
   183 
       
   184     if (!d->client)
       
   185         return false;
       
   186     return d->client->isConnected();
       
   187 }
       
   188 
       
   189 void QDeclarativeDebugClient::sendMessage(const QByteArray &message)
       
   190 {
       
   191     Q_D(QDeclarativeDebugClient);
       
   192 
       
   193     if (!d->client || !d->client->isConnected())
       
   194         return;
       
   195 
       
   196     QPacket pack;
       
   197     pack << d->name << message;
       
   198     d->client->d->protocol->send(pack);
       
   199 }
       
   200 
       
   201 void QDeclarativeDebugClient::messageReceived(const QByteArray &)
       
   202 {
       
   203 }
       
   204 
       
   205 QT_END_NAMESPACE
       
   206 
       
   207 #include <qdeclarativedebugclient.moc>