diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2011-06-13 19:47:22 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2011-06-13 19:47:22 +0000 |
| commit | d2d653088ad4f8e507ae590ca8dc4ef0ba1ea6a6 (patch) | |
| tree | 92425c1f05c8bba1aa824e94b20c336af64b93d9 /src | |
| parent | efe76b2fc0298fcf1f72f013bba77862be809a29 (diff) | |
| download | qxmpp-d2d653088ad4f8e507ae590ca8dc4ef0ba1ea6a6.tar.gz | |
make QXmppClient state accessible via "state" property
Diffstat (limited to 'src')
| -rw-r--r-- | src/QXmppClient.cpp | 90 | ||||
| -rw-r--r-- | src/QXmppClient.h | 25 | ||||
| -rw-r--r-- | src/QXmppOutgoingClient.cpp | 12 | ||||
| -rw-r--r-- | src/QXmppOutgoingClient.h | 2 | ||||
| -rw-r--r-- | src/QXmppStream.cpp | 2 | ||||
| -rw-r--r-- | src/QXmppStream.h | 2 |
6 files changed, 85 insertions, 48 deletions
diff --git a/src/QXmppClient.cpp b/src/QXmppClient.cpp index 20faa47f..9e30d548 100644 --- a/src/QXmppClient.cpp +++ b/src/QXmppClient.cpp @@ -21,6 +21,7 @@ * */ +#include <QSslSocket> #include "QXmppClient.h" #include "QXmppClientExtension.h" @@ -130,35 +131,35 @@ QXmppClient::QXmppClient(QObject *parent) d->addProperCapability(d->clientPresence); bool check = connect(d->stream, SIGNAL(elementReceived(const QDomElement&, bool&)), - this, SLOT(slotElementReceived(const QDomElement&, bool&))); + this, SLOT(_q_elementReceived(const QDomElement&, bool&))); Q_ASSERT(check); check = connect(d->stream, SIGNAL(messageReceived(const QXmppMessage&)), - this, SIGNAL(messageReceived(const QXmppMessage&))); + this, SIGNAL(messageReceived(const QXmppMessage&))); Q_ASSERT(check); check = connect(d->stream, SIGNAL(presenceReceived(const QXmppPresence&)), this, SIGNAL(presenceReceived(const QXmppPresence&))); Q_ASSERT(check); - check = connect(d->stream, SIGNAL(iqReceived(const QXmppIq&)), this, - SIGNAL(iqReceived(const QXmppIq&))); + check = connect(d->stream, SIGNAL(iqReceived(const QXmppIq&)), + this, SIGNAL(iqReceived(const QXmppIq&))); Q_ASSERT(check); - check = connect(d->stream, SIGNAL(disconnected()), this, - SIGNAL(disconnected())); + check = connect(d->stream->socket(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), + this, SLOT(_q_socketStateChanged(QAbstractSocket::SocketState))); Q_ASSERT(check); - check = connect(d->stream, SIGNAL(connected()), this, - SIGNAL(connected())); + check = connect(d->stream, SIGNAL(connected()), + this, SLOT(_q_streamConnected())); Q_ASSERT(check); - check = connect(d->stream, SIGNAL(connected()), this, - SLOT(xmppConnected())); + check = connect(d->stream, SIGNAL(disconnected()), + this, SLOT(_q_streamDisconnected())); Q_ASSERT(check); - check = connect(d->stream, SIGNAL(error(QXmppClient::Error)), this, - SIGNAL(error(QXmppClient::Error))); + check = connect(d->stream, SIGNAL(error(QXmppClient::Error)), + this, SIGNAL(error(QXmppClient::Error))); Q_ASSERT(check); check = setReconnectionManager(new QXmppReconnectionManager(this)); @@ -339,7 +340,7 @@ void QXmppClient::disconnectFromServer() bool QXmppClient::isConnected() const { - return d->stream && d->stream->isConnected(); + return d->stream->isConnected(); } /// Returns the reference to QXmppRosterManager object of the client. @@ -376,6 +377,19 @@ void QXmppClient::sendMessage(const QString& bareJid, const QString& message) } } +/// Returns the client's current state. + +QXmppClient::State QXmppClient::state() const +{ + if (d->stream->isConnected()) + return QXmppClient::ConnectedState; + else if (d->stream->socket()->state() != QAbstractSocket::UnconnectedState && + d->stream->socket()->state() != QAbstractSocket::ClosingState) + return QXmppClient::ConnectingState; + else + return QXmppClient::DisconnectedState; +} + /// Returns the client's current presence. /// @@ -399,10 +413,11 @@ QXmppPresence QXmppClient::clientPresence() const void QXmppClient::setClientPresence(const QXmppPresence& presence) { + d->clientPresence = presence; + d->addProperCapability(d->clientPresence); + if (presence.type() == QXmppPresence::Unavailable) { - d->clientPresence = presence; - // NOTE: we can't call disconnect() because it alters // the client presence if (d->stream->isConnected()) @@ -411,14 +426,10 @@ void QXmppClient::setClientPresence(const QXmppPresence& presence) d->stream->disconnectFromHost(); } } - else if (!d->stream->isConnected()) - connectToServer(d->stream->configuration(), presence); - else - { - d->clientPresence = presence; - d->addProperCapability(d->clientPresence); + else if (d->stream->isConnected()) sendPacket(d->clientPresence); - } + else + connectToServer(d->stream->configuration(), presence); } /// Function to get reconnection manager. By default there exists a reconnection @@ -469,7 +480,7 @@ bool QXmppClient::setReconnectionManager(QXmppReconnectionManager* QAbstractSocket::SocketError QXmppClient::socketError() { - return d->stream->socketError(); + return d->stream->socket()->error(); } /// Returns the XMPP stream error if QXmppClient::Error is QXmppClient::XmppStreamError. @@ -503,7 +514,7 @@ QXmppVersionManager& QXmppClient::versionManager() /// \param element /// \param handled -void QXmppClient::slotElementReceived(const QDomElement &element, bool &handled) +void QXmppClient::_q_elementReceived(const QDomElement &element, bool &handled) { foreach (QXmppClientExtension *extension, d->extensions) { @@ -515,6 +526,31 @@ void QXmppClient::slotElementReceived(const QDomElement &element, bool &handled) } } +void QXmppClient::_q_socketStateChanged(QAbstractSocket::SocketState socketState) +{ + Q_UNUSED(socketState); + emit stateChanged(state()); +} + +/// At connection establishment, send initial presence. + +void QXmppClient::_q_streamConnected() +{ + // notify managers + emit connected(); + emit stateChanged(QXmppClient::ConnectedState); + + // send initial presence + sendPacket(d->clientPresence); +} + +void QXmppClient::_q_streamDisconnected() +{ + // notify managers + emit disconnected(); + emit stateChanged(QXmppClient::DisconnectedState); +} + /// Returns the QXmppLogger associated with the current QXmppClient. QXmppLogger *QXmppClient::logger() @@ -535,10 +571,4 @@ void QXmppClient::setLogger(QXmppLogger *logger) d->logger, SLOT(log(QXmppLogger::MessageType, QString))); } -/// At connection establishment, send initial presence. - -void QXmppClient::xmppConnected() -{ - sendPacket(d->clientPresence); -} diff --git a/src/QXmppClient.h b/src/QXmppClient.h index d5d634df..4ce27abb 100644 --- a/src/QXmppClient.h +++ b/src/QXmppClient.h @@ -80,6 +80,8 @@ class QXmppVersionManager; class QXmppClient : public QXmppLoggable { Q_OBJECT + Q_ENUMS(Error State) + Q_PROPERTY(State state READ state NOTIFY stateChanged) public: /// An enumeration for type of error. @@ -91,6 +93,13 @@ public: XmppStreamError, ///< Error due to XML stream }; + enum State + { + DisconnectedState, + ConnectingState, + ConnectedState, + }; + QXmppClient(QObject *parent = 0); ~QXmppClient(); @@ -140,6 +149,7 @@ public: void setLogger(QXmppLogger *logger); QAbstractSocket::SocketError socketError(); + State state() const; QXmppStanza::Error::Condition xmppStreamError(); QXmppRosterManager& rosterManager(); @@ -187,19 +197,22 @@ signals: /// parameter contains the details of the message sent to this client. /// In other words whenever someone sends you a message this signal is /// emitted. - void messageReceived(const QXmppMessage&); + void messageReceived(const QXmppMessage &message); /// Notifies that an XMPP presence stanza is received. The QXmppPresence /// parameter contains the details of the presence sent to this client. /// This signal is emitted when someone login/logout or when someone's status /// changes Busy, Idle, Invisible etc. - void presenceReceived(const QXmppPresence&); + void presenceReceived(const QXmppPresence &presence); /// Notifies that an XMPP iq stanza is received. The QXmppIq /// parameter contains the details of the iq sent to this client. /// IQ stanzas provide a structured request-response mechanism. Roster /// management, setting-getting vCards etc is done using iq stanzas. - void iqReceived(const QXmppIq&); + void iqReceived(const QXmppIq &iq); + + /// This signal is emitted when the client state changes. + void stateChanged(State state); /// \cond // Deprecated in release 0.3.0 @@ -213,8 +226,10 @@ public slots: void sendMessage(const QString& bareJid, const QString& message); private slots: - void slotElementReceived(const QDomElement &element, bool &handled); - void xmppConnected(); + void _q_elementReceived(const QDomElement &element, bool &handled); + void _q_socketStateChanged(QAbstractSocket::SocketState state); + void _q_streamConnected(); + void _q_streamDisconnected(); private: QXmppClientPrivate * const d; diff --git a/src/QXmppOutgoingClient.cpp b/src/QXmppOutgoingClient.cpp index e35c4038..fd533565 100644 --- a/src/QXmppOutgoingClient.cpp +++ b/src/QXmppOutgoingClient.cpp @@ -61,7 +61,6 @@ public: // This object provides the configuration // required for connecting to the XMPP server. QXmppConfiguration config; - QAbstractSocket::SocketError socketError; QXmppStanza::Error::Condition xmppStreamError; // State data @@ -214,9 +213,9 @@ void QXmppOutgoingClient::socketSslErrors(const QList<QSslError> & error) socket()->ignoreSslErrors(); } -void QXmppOutgoingClient::socketError(QAbstractSocket::SocketError ee) +void QXmppOutgoingClient::socketError(QAbstractSocket::SocketError socketError) { - d->socketError = ee; + Q_UNUSED(socketError); emit error(QXmppClient::SocketError); warning(QString("Socket error: " + socket()->errorString())); } @@ -697,13 +696,6 @@ void QXmppOutgoingClient::sendNonSASLAuthQuery() sendPacket(authQuery); } -/// Returns the type of the last socket error that occured. - -QAbstractSocket::SocketError QXmppOutgoingClient::socketError() -{ - return d->socketError; -} - /// Returns the type of the last XMPP stream error that occured. QXmppStanza::Error::Condition QXmppOutgoingClient::xmppStreamError() diff --git a/src/QXmppOutgoingClient.h b/src/QXmppOutgoingClient.h index adb36505..548df12e 100644 --- a/src/QXmppOutgoingClient.h +++ b/src/QXmppOutgoingClient.h @@ -56,7 +56,7 @@ public: void connectToHost(); bool isConnected() const; - QAbstractSocket::SocketError socketError(); + QSslSocket *socket() const { return QXmppStream::socket(); }; QXmppStanza::Error::Condition xmppStreamError(); QXmppConfiguration& configuration(); diff --git a/src/QXmppStream.cpp b/src/QXmppStream.cpp index e6958f23..2221411a 100644 --- a/src/QXmppStream.cpp +++ b/src/QXmppStream.cpp @@ -156,7 +156,7 @@ bool QXmppStream::sendPacket(const QXmppPacket &packet) /// Returns the QSslSocket used for this stream. /// -QSslSocket *QXmppStream::socket() +QSslSocket *QXmppStream::socket() const { return d->socket; } diff --git a/src/QXmppStream.h b/src/QXmppStream.h index 422c80fc..f90fd101 100644 --- a/src/QXmppStream.h +++ b/src/QXmppStream.h @@ -61,7 +61,7 @@ signals: protected: // Access to underlying socket - QSslSocket *socket(); + QSslSocket *socket() const; void setSocket(QSslSocket *socket); // Overridable methods |
