From 52412e367ef7f79dffb3e51bc3a90b03676fe95a Mon Sep 17 00:00:00 2001 From: Fabian Bumberger Date: Wed, 18 Sep 2013 18:29:47 +0200 Subject: Refactor QDeclarativeBluetoothSocket Change-Id: I7bd36d4b05a28788720b3301eed310c4f4b2d910 Reviewed-by: Alex Blasche --- .../bluetooth/qdeclarativebluetoothsocket.cpp | 98 +++++++++------------- .../bluetooth/qdeclarativebluetoothsocket_p.h | 36 ++++++-- 2 files changed, 72 insertions(+), 62 deletions(-) (limited to 'src/imports') diff --git a/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp b/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp index a3251c87..a864910a 100644 --- a/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp +++ b/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp @@ -51,8 +51,6 @@ #include #include -/* ==================== QDeclarativeBluetoothSocket ======================= */ - /*! \qmltype BluetoothSocket \instantiates QDeclarativeBluetoothSocket @@ -83,8 +81,8 @@ class QDeclarativeBluetoothSocketPrivate public: QDeclarativeBluetoothSocketPrivate(QDeclarativeBluetoothSocket *bs) : m_dbs(bs), m_service(0), m_socket(0), - m_error(QLatin1String("No Error")), - m_state(QLatin1String("No Service Set")), + m_error(QDeclarativeBluetoothSocket::NoError), + m_state(QDeclarativeBluetoothSocket::NoServiceSet), m_componentCompleted(false), m_connected(false) { @@ -100,13 +98,20 @@ public: { Q_ASSERT(m_service); //qDebug() << "Connecting to: " << m_service->serviceInfo()->device().address().toString(); - m_error = QLatin1String("No Error"); + m_error = QDeclarativeBluetoothSocket::NoError; if (m_socket) m_socket->deleteLater(); -// delete m_socket; - m_socket = new QBluetoothSocket(); + QBluetoothServiceInfo::Protocol socketProtocol; + if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::L2capProtocol) + socketProtocol = QBluetoothServiceInfo::L2capProtocol; + else if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::RfcommProtocol) + socketProtocol = QBluetoothServiceInfo::RfcommProtocol; + else + socketProtocol = QBluetoothServiceInfo::UnknownProtocol; + + m_socket = new QBluetoothSocket(socketProtocol); m_socket->connectToService(*m_service->serviceInfo()); QObject::connect(m_socket, SIGNAL(connected()), m_dbs, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), m_dbs, SLOT(socket_disconnected())); @@ -120,8 +125,8 @@ public: QDeclarativeBluetoothSocket *m_dbs; QDeclarativeBluetoothService *m_service; QBluetoothSocket *m_socket; - QString m_error; - QString m_state; + QDeclarativeBluetoothSocket::Error m_error; + QDeclarativeBluetoothSocket::SocketState m_state; bool m_componentCompleted; bool m_connected; QDataStream *m_stream; @@ -211,7 +216,7 @@ void QDeclarativeBluetoothSocket::setService(QDeclarativeBluetoothService *servi */ -bool QDeclarativeBluetoothSocket::connected() +bool QDeclarativeBluetoothSocket::connected() const { if (!d->m_socket) return false; @@ -237,14 +242,25 @@ void QDeclarativeBluetoothSocket::setConnected(bool connected) } /*! - \qmlproperty string BluetoothSocket::error + \qmlproperty enumeration BluetoothSocket::error - This property holds the string for the last reported error - This property is read-only. - */ + This property holds the last error that happened. + \list + \li \c{NoError} + \li \c{UnknownSocketError} + \li \c{ConnectionRefusedError} + \li \c{RemoteHostClosedError} + \li \c{HostNotFoundError} + \li \c{ServiceNotFoundError} + \li \c{NetworkError} + \li \c{UnsupportedProtocolError} + \endlist + + The errors are derived from \l QBluetoothSocket::SocketError. This property is read-only. +*/ -QString QDeclarativeBluetoothSocket::error() +QDeclarativeBluetoothSocket::Error QDeclarativeBluetoothSocket::error() const { return d->m_error; } @@ -261,61 +277,29 @@ void QDeclarativeBluetoothSocket::socket_disconnected() emit connectedChanged(); } -void QDeclarativeBluetoothSocket::socket_error(QBluetoothSocket::SocketError err) +void QDeclarativeBluetoothSocket::socket_error(QBluetoothSocket::SocketError error) { - if (err == QBluetoothSocket::ConnectionRefusedError) - d->m_error = QLatin1String("Connection Refused"); - else if (err == QBluetoothSocket::RemoteHostClosedError) - d->m_error = QLatin1String("Connection Closed by Remote Host"); - else if (err == QBluetoothSocket::HostNotFoundError) - d->m_error = QLatin1String("Host Not Found"); - else if (err == QBluetoothSocket::ServiceNotFoundError) - d->m_error = QLatin1String("Could not find service at remote host"); - else - d->m_error = QLatin1String("Unknown Error"); + d->m_error = static_cast(error); emit errorChanged(); } void QDeclarativeBluetoothSocket::socket_state(QBluetoothSocket::SocketState state) { - switch (state) { - case QBluetoothSocket::UnconnectedState: - d->m_state = QLatin1String("Unconnected"); - break; - case QBluetoothSocket::ServiceLookupState: - d->m_state = QLatin1String("Service Lookup"); - break; - case QBluetoothSocket::ConnectingState: - d->m_state = QLatin1String("Connecting"); - break; - case QBluetoothSocket::ConnectedState: - d->m_state = QLatin1String("Connected"); - break; - case QBluetoothSocket::ClosingState: - d->m_state = QLatin1String("Closing"); - break; - case QBluetoothSocket::ListeningState: - d->m_state = QLatin1String("Listening"); - break; - case QBluetoothSocket::BoundState: - d->m_state = QLatin1String("Bound"); - break; - } + d->m_state = static_cast(state); emit stateChanged(); } /*! - \qmlproperty string BluetoothSocket::state + \qmlproperty enumeration BluetoothSocket::state - This property holds the current state of the socket. The property can be one - of the following strings: + This property holds the current state of the socket. \list - \li \c{No Service Set} + \li \c{NoServiceSet} \li \c{Unconnected} - \li \c{Service Lookup} + \li \c{ServiceLookup} \li \c{Connecting} \li \c{Connected} \li \c{Closing} @@ -323,9 +307,9 @@ void QDeclarativeBluetoothSocket::socket_state(QBluetoothSocket::SocketState sta \li \c{Bound} \endlist - The states are derived from QBluetoothSocket::SocketState. This property is read-only. + The states are derived from \l QBluetoothSocket::SocketState. This property is read-only. */ -QString QDeclarativeBluetoothSocket::state() +QDeclarativeBluetoothSocket::SocketState QDeclarativeBluetoothSocket::state() const { return d->m_state; } @@ -398,7 +382,7 @@ void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarati d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; - d->m_error = QLatin1String("No Error"); + d->m_error = NoSocketerror; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); diff --git a/src/imports/bluetooth/qdeclarativebluetoothsocket_p.h b/src/imports/bluetooth/qdeclarativebluetoothsocket_p.h index 6e00d6ce..03145522 100644 --- a/src/imports/bluetooth/qdeclarativebluetoothsocket_p.h +++ b/src/imports/bluetooth/qdeclarativebluetoothsocket_p.h @@ -60,12 +60,38 @@ class QDeclarativeBluetoothSocket : public QObject, public QQmlParserStatus Q_OBJECT Q_PROPERTY(QDeclarativeBluetoothService *service READ service WRITE setService NOTIFY serviceChanged) Q_PROPERTY(bool connected READ connected WRITE setConnected NOTIFY connectedChanged) - Q_PROPERTY(QString error READ error NOTIFY errorChanged) - Q_PROPERTY(QString state READ state NOTIFY stateChanged) + Q_PROPERTY(Error error READ error NOTIFY errorChanged) + Q_PROPERTY(SocketState socketState READ state NOTIFY stateChanged) Q_PROPERTY(QString stringData READ stringData WRITE sendStringData NOTIFY dataAvailable) Q_INTERFACES(QQmlParserStatus) + Q_ENUMS(Error) + Q_ENUMS(SocketState) public: + + enum Error { + NoError = QBluetoothSocket::NoSocketError, + UnknownSocketError = QBluetoothSocket::UnknownSocketError, + ConnectionRefusedError = QBluetoothSocket::ConnectionRefusedError, + RemoteHostClosedError = QBluetoothSocket::RemoteHostClosedError, + HostNotFoundError = QBluetoothSocket::HostNotFoundError, + ServiceNotFoundError = QBluetoothSocket::ServiceNotFoundError, + NetworkError = QBluetoothSocket::NetworkError, + UnsupportedProtocolError = QBluetoothSocket::UnsupportedProtocolError, + NoSocketerror + }; + + enum SocketState { + Unconnected = QBluetoothSocket::UnconnectedState, + ServiceLookup = QBluetoothSocket::ServiceLookupState, + Connecting = QBluetoothSocket::ConnectingState, + Connected = QBluetoothSocket::ConnectedState, + Bound = QBluetoothSocket::BoundState, + Closing = QBluetoothSocket::ClosingState, + Listening = QBluetoothSocket::ListeningState, + NoServiceSet + }; + explicit QDeclarativeBluetoothSocket(QObject *parent = 0); explicit QDeclarativeBluetoothSocket(QDeclarativeBluetoothService *service, QObject *parent = 0); @@ -74,9 +100,9 @@ public: ~QDeclarativeBluetoothSocket(); QDeclarativeBluetoothService *service(); - bool connected(); - QString error(); - QString state(); + bool connected() const; + Error error() const; + SocketState state() const; QString stringData(); -- cgit v1.2.3