| 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 QtSCriptTools module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qscriptdebuggerfrontend_p.h" | 
|---|
| 43 | #include "qscriptdebuggerfrontend_p_p.h" | 
|---|
| 44 | #include "qscriptdebuggercommand_p.h" | 
|---|
| 45 | #include "qscriptdebuggerevent_p.h" | 
|---|
| 46 | #include "qscriptdebuggerresponse_p.h" | 
|---|
| 47 | #include "qscriptdebuggereventhandlerinterface_p.h" | 
|---|
| 48 | #include "qscriptdebuggerresponsehandlerinterface_p.h" | 
|---|
| 49 |  | 
|---|
| 50 | #include <QtCore/qcoreevent.h> | 
|---|
| 51 | #include <QtCore/qcoreapplication.h> | 
|---|
| 52 |  | 
|---|
| 53 | QT_BEGIN_NAMESPACE | 
|---|
| 54 |  | 
|---|
| 55 | /*! | 
|---|
| 56 | \class QScriptDebuggerFrontend | 
|---|
| 57 | \since 4.5 | 
|---|
| 58 | \internal | 
|---|
| 59 |  | 
|---|
| 60 | \brief The QScriptDebuggerFrontend class is the base class of debugger front-ends. | 
|---|
| 61 | */ | 
|---|
| 62 |  | 
|---|
| 63 | // helper class that's used to handle our custom Qt events | 
|---|
| 64 | class QScriptDebuggerFrontendEventReceiver : public QObject | 
|---|
| 65 | { | 
|---|
| 66 | public: | 
|---|
| 67 | QScriptDebuggerFrontendEventReceiver(QScriptDebuggerFrontendPrivate *frontend, | 
|---|
| 68 | QObject *parent = 0); | 
|---|
| 69 | ~QScriptDebuggerFrontendEventReceiver(); | 
|---|
| 70 |  | 
|---|
| 71 | bool event(QEvent *); | 
|---|
| 72 |  | 
|---|
| 73 | private: | 
|---|
| 74 | QScriptDebuggerFrontendPrivate *m_frontend; | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | QScriptDebuggerFrontendEventReceiver::QScriptDebuggerFrontendEventReceiver( | 
|---|
| 78 | QScriptDebuggerFrontendPrivate *frontend, QObject *parent) | 
|---|
| 79 | : QObject(parent), m_frontend(frontend) | 
|---|
| 80 | { | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | QScriptDebuggerFrontendEventReceiver::~QScriptDebuggerFrontendEventReceiver() | 
|---|
| 84 | { | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | bool QScriptDebuggerFrontendEventReceiver::event(QEvent *e) | 
|---|
| 88 | { | 
|---|
| 89 | return m_frontend->event(e); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | QScriptDebuggerFrontendPrivate::QScriptDebuggerFrontendPrivate() | 
|---|
| 94 | { | 
|---|
| 95 | eventHandler = 0; | 
|---|
| 96 | nextCommandId = 0; | 
|---|
| 97 | eventReceiver = new QScriptDebuggerFrontendEventReceiver(this); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | QScriptDebuggerFrontendPrivate::~QScriptDebuggerFrontendPrivate() | 
|---|
| 101 | { | 
|---|
| 102 | delete eventReceiver; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void QScriptDebuggerFrontendPrivate::postEvent(QEvent *e) | 
|---|
| 106 | { | 
|---|
| 107 | QCoreApplication::postEvent(eventReceiver, e); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | bool QScriptDebuggerFrontendPrivate::event(QEvent *e) | 
|---|
| 111 | { | 
|---|
| 112 | Q_Q(QScriptDebuggerFrontend); | 
|---|
| 113 | if (e->type() == QEvent::User+1) { | 
|---|
| 114 | QScriptDebuggerEventEvent *de = static_cast<QScriptDebuggerEventEvent*>(e); | 
|---|
| 115 | bool handled = q->notifyEvent(de->event()); | 
|---|
| 116 | if (handled) { | 
|---|
| 117 | q->scheduleCommand(QScriptDebuggerCommand::resumeCommand(), | 
|---|
| 118 | /*responseHandler=*/0); | 
|---|
| 119 | } | 
|---|
| 120 | return true; | 
|---|
| 121 | } else if (e->type() == QEvent::User+2) { | 
|---|
| 122 | processCommands(); | 
|---|
| 123 | return true; | 
|---|
| 124 | } | 
|---|
| 125 | return false; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void QScriptDebuggerFrontendPrivate::processCommands() | 
|---|
| 129 | { | 
|---|
| 130 | Q_Q(QScriptDebuggerFrontend); | 
|---|
| 131 | while (!pendingCommands.isEmpty()) { | 
|---|
| 132 | QScriptDebuggerCommand command(pendingCommands.takeFirst()); | 
|---|
| 133 | int id = pendingCommandIds.takeFirst(); | 
|---|
| 134 | q->processCommand(id, command); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | QScriptDebuggerFrontend::QScriptDebuggerFrontend() | 
|---|
| 139 | : d_ptr(new QScriptDebuggerFrontendPrivate()) | 
|---|
| 140 | { | 
|---|
| 141 | d_ptr->q_ptr = this; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | QScriptDebuggerFrontend::~QScriptDebuggerFrontend() | 
|---|
| 145 | { | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | QScriptDebuggerFrontend::QScriptDebuggerFrontend(QScriptDebuggerFrontendPrivate &dd) | 
|---|
| 149 | : d_ptr(&dd) | 
|---|
| 150 | { | 
|---|
| 151 | d_ptr->q_ptr = this; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | QScriptDebuggerEventHandlerInterface *QScriptDebuggerFrontend::eventHandler() const | 
|---|
| 155 | { | 
|---|
| 156 | Q_D(const QScriptDebuggerFrontend); | 
|---|
| 157 | return d->eventHandler; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | void QScriptDebuggerFrontend::setEventHandler(QScriptDebuggerEventHandlerInterface *eventHandler) | 
|---|
| 161 | { | 
|---|
| 162 | Q_D(QScriptDebuggerFrontend); | 
|---|
| 163 | d->eventHandler = eventHandler; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | /*! | 
|---|
| 167 | Schedules the given \a command for execution by this front-end, | 
|---|
| 168 | and returns a unique identifier associated with this command. | 
|---|
| 169 |  | 
|---|
| 170 | Subclasses can call this function to schedule custom commands. | 
|---|
| 171 |  | 
|---|
| 172 | \sa notifyCommandFinished() | 
|---|
| 173 | */ | 
|---|
| 174 | int QScriptDebuggerFrontend::scheduleCommand( | 
|---|
| 175 | const QScriptDebuggerCommand &command, | 
|---|
| 176 | QScriptDebuggerResponseHandlerInterface *responseHandler) | 
|---|
| 177 | { | 
|---|
| 178 | Q_D(QScriptDebuggerFrontend); | 
|---|
| 179 | int id = ++d->nextCommandId; | 
|---|
| 180 | d->pendingCommands.append(command); | 
|---|
| 181 | d->pendingCommandIds.append(id); | 
|---|
| 182 | if (responseHandler) | 
|---|
| 183 | d->responseHandlers.insert(id, responseHandler); | 
|---|
| 184 | if (d->pendingCommands.size() == 1) { | 
|---|
| 185 | QEvent *e = new QEvent(QEvent::Type(QEvent::User+2)); // ProcessCommands | 
|---|
| 186 | d->postEvent(e); | 
|---|
| 187 | } | 
|---|
| 188 | return id; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | /*! | 
|---|
| 192 | Subclasses should call this function when the command identified by | 
|---|
| 193 | the given \a id has finished and produced the given \a response. | 
|---|
| 194 |  | 
|---|
| 195 | \sa processCommand(), notifyEvent() | 
|---|
| 196 | */ | 
|---|
| 197 | void QScriptDebuggerFrontend::notifyCommandFinished(int id, const QScriptDebuggerResponse &response) | 
|---|
| 198 | { | 
|---|
| 199 | Q_D(QScriptDebuggerFrontend); | 
|---|
| 200 | if (d->responseHandlers.contains(id)) { | 
|---|
| 201 | QScriptDebuggerResponseHandlerInterface *handler = d->responseHandlers.take(id); | 
|---|
| 202 | Q_ASSERT(handler != 0); | 
|---|
| 203 | handler->handleResponse(response, id); | 
|---|
| 204 | } | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | /*! | 
|---|
| 208 | Subclasses should call this function when the given \a event is | 
|---|
| 209 | received from the back-end. | 
|---|
| 210 |  | 
|---|
| 211 | \sa notifyCommandFinished(), QScriptDebuggerBackend::event() | 
|---|
| 212 | */ | 
|---|
| 213 | bool QScriptDebuggerFrontend::notifyEvent(const QScriptDebuggerEvent &event) | 
|---|
| 214 | { | 
|---|
| 215 | Q_D(QScriptDebuggerFrontend); | 
|---|
| 216 | if (d->eventHandler) | 
|---|
| 217 | return d->eventHandler->debuggerEvent(event); | 
|---|
| 218 | return false; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | int QScriptDebuggerFrontend::scheduledCommandCount() const | 
|---|
| 222 | { | 
|---|
| 223 | Q_D(const QScriptDebuggerFrontend); | 
|---|
| 224 | return d->nextCommandId; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | /*! | 
|---|
| 228 | \fn void QScriptDebuggerFrontend::processCommand(int id, const QScriptDebuggerCommand &command) | 
|---|
| 229 |  | 
|---|
| 230 | Subclasses must reimplement this function to process the given \a command | 
|---|
| 231 | identified by \a id. Call notifyCommandFinished() when processing is | 
|---|
| 232 | complete. | 
|---|
| 233 | */ | 
|---|
| 234 |  | 
|---|
| 235 | QT_END_NAMESPACE | 
|---|