QXmppClient Class
The QXmppClient class is the main class for connecting to an XMPP server. More...
| Header: | #include <QXmppClient.h> |
| Inherits: | QXmppLoggable |
- List of all members, including inherited members
- Deprecated members
- QXmppClient is part of Core classes.
Public Types
(since QXmpp 1.5) | EmptyResult |
| enum | Error { NoError, SocketError, KeepAliveError, XmppStreamError } |
(since QXmpp 1.6) enum | InitialExtensions { NoExtensions, BasicExtensions } |
(since QXmpp 1.5) | IqResult |
| enum | State { DisconnectedState, ConnectingState, ConnectedState } |
| enum | StreamManagementState { NoStreamManagement, NewStream, ResumedStream } |
Properties
Public Functions
| QXmppClient(QObject *parent = nullptr) | |
(since QXmpp 1.6) | QXmppClient(QXmppClient::InitialExtensions initialExtensions, QObject *parent = nullptr) |
| bool | addExtension(QXmppClientExtension *extension) |
(since QXmpp 1.5) T * | addNewExtension(Args... args) |
| QXmppPresence | clientPresence() const |
| QXmppConfiguration & | configuration() |
(since QXmpp 1.5) QXmppE2eeExtension * | encryptionExtension() const |
| QList<QXmppClientExtension *> | extensions() const |
| T * | findExtension() const |
(since QXmpp 1.2) int | indexOfExtension() const |
| bool | insertExtension(int index, QXmppClientExtension *extension) |
(since QXmpp 1.0) bool | isActive() const |
| bool | isAuthenticated() const |
| bool | isConnected() const |
| QXmppLogger * | logger() const |
| bool | removeExtension(QXmppClientExtension *extension) |
(since QXmpp 1.5) QXmppTask<QXmpp::SendResult> | reply(QXmppStanza &&stanza, const std::optional<QXmppE2eeMetadata> &e2eeMetadata, const std::optional<QXmppSendStanzaParams> ¶ms = {}) |
(since QXmpp 1.5) QXmppTask<QXmpp::SendResult> | send(QXmppStanza &&stanza, const std::optional<QXmppSendStanzaParams> & = {}) |
(since QXmpp 1.5) QXmppTask<QXmppClient::EmptyResult> | sendGenericIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> & = {}) |
(since QXmpp 1.5) QXmppTask<QXmppClient::IqResult> | sendIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> & = {}) |
(since QXmpp 1.5) QXmppTask<QXmpp::SendResult> | sendSensitive(QXmppStanza &&stanza, const std::optional<QXmppSendStanzaParams> ¶ms = {}) |
(since QXmpp 1.5) QXmppTask<QXmppClient::IqResult> | sendSensitiveIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> ¶ms = {}) |
(since QXmpp 1.0) void | setActive(bool active) |
| void | setClientPresence(const QXmppPresence &presence) |
(since QXmpp 1.5) void | setEncryptionExtension(QXmppE2eeExtension *extension) |
| void | setLogger(QXmppLogger *logger) |
| QAbstractSocket::SocketError | socketError() |
| QString | socketErrorString() const |
| QXmppClient::State | state() const |
(since QXmpp 1.4) QXmppClient::StreamManagementState | streamManagementState() const |
| QXmppStanza::Error::Condition | xmppStreamError() |
Public Slots
| void | connectToServer(const QString &jid, const QString &password) |
| void | connectToServer(const QXmppConfiguration &config, const QXmppPresence &initialPresence = {}) |
| void | disconnectFromServer() |
Signals
| void | connected() |
(since QXmpp 1.8) void | credentialsChanged() |
| void | disconnected() |
| void | error(QXmppClient::Error) |
(since QXmpp 1.7) void | errorOccurred(const QXmppError &error) |
| void | iqReceived(const QXmppIq &iq) |
| void | loggerChanged(QXmppLogger *logger) |
| void | messageReceived(const QXmppMessage &message) |
| void | presenceReceived(const QXmppPresence &presence) |
| void | sslErrors(const QList<QSslError> &errors) |
| void | stateChanged(QXmppClient::State state) |
Related Non-Members
| QLatin1String | QXmppVersion() |
Detailed Description
It provides the core functionality for establishing a connection, sending and receiving stanzas, and managing client extensions.
This class will provide the handle/reference to QXmppRosterManager (roster management), QXmppVCardManager (vCard manager), and QXmppVersionManager (software version information).
By default, the client will automatically try reconnecting to the server. You can change that behaviour using QXmppConfiguration::setAutoReconnectionEnabled().
Not all the managers or extensions have been enabled by default. One can enable/disable the managers using the functions addExtension() and removeExtension(). findExtension() can be used to find a reference/pointer to a particular instantiated and enabled manager.
List of managers enabled by default:
- QXmppRosterManager
- QXmppVCardManager
- QXmppVersionManager
- QXmppDiscoveryManager
- QXmppEntityTimeManager
Connection details
If no explicit host and port are configured, the client will look up the SRV records of the domain of the configured JID. Since QXmpp 1.8 both TCP and direct TLS records are looked up and connection via direct TLS is preferred as it saves the extra round trip from STARTTLS. See also XEP-0368: SRV records for XMPP over TLS.
On connection errors the other SRV records are tested too (if multiple are available).
For servers without SRV records or if looking up the records did not succeed, domain and the default port of 5223 (TLS) and 5222 (TCP/STARTTLS) are tried.
Usage of FAST token-based authentication
QXmpp uses XEP-0484: Fast Authentication Streamlining Tokens if enabled and supported by the server. FAST tokens can be requested after a first time authentication using a password or another strong authentication mechanism. The tokens can then be used to log in, without a password. The tokens are linked to a specific device ID (set via the SASL 2 user agent) and only this device can use the token. Tokens also expire and are rotated by the server.
The advantage of this mechanism is that a client does not necessarily need to store the password of an account and in the future clients that are logged in could be listed and logged out manually. FAST also allows for performance improvements as it only requires one round trip for authentication (and may be included in TLS 0-RTT data although that is not implemented in QXmpp) while other mechanisms like SCRAM need multiple round trips.
FAST itself is enabled by default (see QXmppConfiguration::useFastTokenAuthentication()), but you also need to set a SASL user agent with a stable device ID, so FAST can be used.
After that you can login and use QXmppCredentials to serialize the token data and store it permanently. Note that the token may change over time, though.
Member Type Documentation
[alias, since QXmpp 1.5] QXmppClient::EmptyResult
Result of a generic request without a return value. Contains Success in case everything went well. If the returned IQ contained an error a QXmppStanza::Error is reported.
This typedef was introduced in QXmpp 1.5.
enum QXmppClient::Error
An enumeration for type of error. Error could come due a TCP socket or XML stream or due to various stanzas.
| Constant | Value | Description |
|---|---|---|
QXmppClient::NoError | 0 | No error. |
QXmppClient::SocketError | 1 | Error due to TCP socket. |
QXmppClient::KeepAliveError | 2 | Error due to no response to a keep alive. |
QXmppClient::XmppStreamError | 3 | Error due to XML stream. |
[since QXmpp 1.6] enum QXmppClient::InitialExtensions
Used to decide which extensions should be added by default.
| Constant | Value | Description |
|---|---|---|
QXmppClient::NoExtensions | 0 | Creates a client without any extensions. |
QXmppClient::BasicExtensions | 1 | Creates a client with the default set of extensions. |
This enum was introduced in QXmpp 1.6.
[alias, since QXmpp 1.5] QXmppClient::IqResult
Result of an IQ request, either contains the QDomElement of the IQ reponse (in case of an 'result' IQ type) or it contains an QXmppError with a QXmppStanza::Error (on type 'error') or a QXmpp::SendError.
This typedef was introduced in QXmpp 1.5.
enum QXmppClient::State
This enumeration describes a client state.
| Constant | Value | Description |
|---|---|---|
QXmppClient::DisconnectedState | 0 | Disconnected from the server. |
QXmppClient::ConnectingState | 1 | Trying to connect to the server. |
QXmppClient::ConnectedState | 2 | Connected to the server. |
enum QXmppClient::StreamManagementState
Describes the use of XEP-0198: Stream Management
| Constant | Value | Description |
|---|---|---|
QXmppClient::NoStreamManagement | 0 | Stream Management is not used. |
QXmppClient::NewStream | 1 | Stream Management is used and the previous stream has not been resumed. |
QXmppClient::ResumedStream | 2 | Stream Management is used and the previous stream has been resumed. |
Property Documentation
logger : QXmppLogger*
The QXmppLogger associated with the current QXmppClient
Access functions:
Notifier signal:
| void | loggerChanged(QXmppLogger *logger) |
[read-only] state : State
The client's current state
Access functions:
| QXmppClient::State | state() const |
Notifier signal:
| void | stateChanged(QXmppClient::State state) |
Member Function Documentation
QXmppClient::QXmppClient(QObject *parent = nullptr)
Creates a QXmppClient object. parent is passed to the QObject's constructor.
[since QXmpp 1.6] QXmppClient::QXmppClient(QXmppClient::InitialExtensions initialExtensions, QObject *parent = nullptr)
Creates a QXmppClient object.
initialExtensions can be used to set the initial set of extensions. parent is passed to the QObject's constructor. The default value is 0.
This function was introduced in QXmpp 1.6.
bool QXmppClient::addExtension(QXmppClientExtension *extension)
Registers a new extension with the client. Returns true on success.
[since QXmpp 1.5] template <typename T, typename... Args> T *QXmppClient::addNewExtension(Args... args)
Creates a new extension (forwarding args to its constructor) and adds it to the client.
Returns the newly created extension.
This function was introduced in QXmpp 1.5.
QXmppPresence QXmppClient::clientPresence() const
Returns the client's current presence.
See also setClientPresence().
QXmppConfiguration &QXmppClient::configuration()
Returns a modifiable reference to the current configuration of QXmppClient.
[slot] void QXmppClient::connectToServer(const QString &jid, const QString &password)
Overloaded function to simply connect to an XMPP server with a jid and password for the account.
Note: This slot is overloaded. To connect to this slot:
// Connect using qOverload:
connect(sender, &SenderClass::signal,
xmppClient, qOverload(&QXmppClient::connectToServer));
// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
xmppClient, [receiver = xmppClient](const QString &jid, const QString &password) { receiver->connectToServer(jid, password); });
For more examples and approaches, see connecting to overloaded slots.
[slot] void QXmppClient::connectToServer(const QXmppConfiguration &config, const QXmppPresence &initialPresence = {})
Attempts to connect to the XMPP server. Server details and other configurations are specified using the config parameter. Use signals connected(), error(QXmppClient::Error) and disconnected() to know the status of the connection.
config specifies the configuration object for connecting the XMPP server. This contains the host name, user, password etc. See QXmppConfiguration for details. initialPresence is the initial presence which will be set for this user after establishing the session. The default value is QXmppPresence::Available.
Note: This slot is overloaded. To connect to this slot:
// Connect using qOverload:
connect(sender, &SenderClass::signal,
xmppClient, qOverload(&QXmppClient::connectToServer));
// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
xmppClient, [receiver = xmppClient](const QXmppConfiguration &config, const QXmppPresence &initialPresence) { receiver->connectToServer(config, initialPresence); });
For more examples and approaches, see connecting to overloaded slots.
[signal] void QXmppClient::connected()
This signal is emitted when the client connects successfully to the XMPP server i.e. when a successful XMPP connection is established. XMPP Connection involves following sequential steps:
- TCP socket connection
- Client sends start stream
- Server sends start stream
- TLS negotiation (encryption)
- Authentication
- Resource binding
- Session establishment
After all these steps a successful XMPP connection is established and connected() signal is emitted.
After the connected() signal is emitted QXmpp will send the roster request to the server. On receiving the roster, QXmpp will emit QXmppRosterManager::rosterReceived(). After this signal, QXmppRosterManager object gets populated and you can use findExtension<QXmppRosterManager>() to get the handle of QXmppRosterManager object.
[signal, since QXmpp 1.8] void QXmppClient::credentialsChanged()
Emitted when the credentials, e.g. tokens have changed.
This means that the QXmppCredentials in the QXmppConfiguration of this client has changed.
This function was introduced in QXmpp 1.8.
[slot] void QXmppClient::disconnectFromServer()
Disconnects the client and the current presence of client changes to QXmppPresence::Unavailable and status text changes to "Logged out".
Note: Make sure that the clientPresence is changed to QXmppPresence::Available, if you are again calling connectToServer() after calling the disconnectFromServer() function.
[signal] void QXmppClient::disconnected()
This signal is emitted when the XMPP connection disconnects.
[since QXmpp 1.5] QXmppE2eeExtension *QXmppClient::encryptionExtension() const
Returns the currently used encryption extension.
This function was introduced in QXmpp 1.5.
See also setEncryptionExtension().
[signal] void QXmppClient::error(QXmppClient::Error)
This signal is emitted when the XMPP connection encounters any error. The QXmppClient::Error parameter specifies the type of error occurred. It could be due to TCP socket or the xml stream or the stanza. Depending upon the type of error occurred use the respective get function to know the error.
[signal, since QXmpp 1.7] void QXmppClient::errorOccurred(const QXmppError &error)
This signal is emitted when the XMPP connection encounters any fatal error.
error is a detailed error object with a description. It may contain
- QAbstractSocket::SocketError
- QXmpp::TimeoutError
- QXmpp::StreamError
- QXmpp::AuthenticationError
- QXmpp::BindError
This function was introduced in QXmpp 1.7.
QList<QXmppClientExtension *> QXmppClient::extensions() const
Returns a list containing all the client's extensions.
template <typename T> T *QXmppClient::findExtension() const
Returns the extension which can be cast into type T*, or 0 if there is no such extension.
Usage example:
QXmppDiscoveryManager* ext = client->findExtension<QXmppDiscoveryManager>();
if(ext)
{
//extension found, do stuff...
}
[since QXmpp 1.2] template <typename T> int QXmppClient::indexOfExtension() const
Returns the index of an extension
Usage example:
int index = client->indexOfExtension<QXmppDiscoveryManager>();
if (index > 0) {
// extension found, do stuff...
} else {
// extension not found
}
This function was introduced in QXmpp 1.2.
bool QXmppClient::insertExtension(int index, QXmppClientExtension *extension)
Registers a new extension with the client at the given index. Returns true on success.
[signal] void QXmppClient::iqReceived(const QXmppIq &iq)
This signal is emitted when IQs of type result or error are received by the client and no registered QXmppClientExtension could handle it.
This is useful when it is only important to check whether the response of an IQ was successful. However, the recommended way is still to use an additional QXmppClientExtension for this kind of tasks.
iq is the received IQ stanza.
[since QXmpp 1.0] bool QXmppClient::isActive() const
Returns true if the current client state is "active", false if it is "inactive". See XEP-0352: Client State Indication for details.
This function was introduced in QXmpp 1.0.
bool QXmppClient::isAuthenticated() const
Returns true if the client has authenticated with the XMPP server.
bool QXmppClient::isConnected() const
Returns true if the client is connected to the XMPP server.
QXmppLogger *QXmppClient::logger() const
Returns the QXmppLogger associated with the current QXmppClient.
Note: Getter function for property logger.
See also setLogger().
[signal] void QXmppClient::loggerChanged(QXmppLogger *logger)
This signal is emitted when the logger changes.
Note: Notifier signal for property logger.
[signal] void QXmppClient::messageReceived(const QXmppMessage &message)
Notifies that an XMPP message stanza is received. The QXmppMessage parameter contains the details of the message sent to this client. In other words whenever someone sends you a message this signal is emitted.
message is the received message stanza.
[signal] void QXmppClient::presenceReceived(const QXmppPresence &presence)
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.
presence is the received presence stanza.
bool QXmppClient::removeExtension(QXmppClientExtension *extension)
Returns whether the extension was unregistered. Unregisters the given extension from the client. If the extension is found, it will be destroyed.
extension.
[since QXmpp 1.5] QXmppTask<QXmpp::SendResult> QXmppClient::reply(QXmppStanza &&stanza, const std::optional<QXmppE2eeMetadata> &e2eeMetadata, const std::optional<QXmppSendStanzaParams> ¶ms = {})
Sends the stanza with the same encryption as e2eeMetadata.
When there is no e2eeMetadata given this always sends the stanza without end-to-end encryption. Intended to be used for replies to IQs and messages.
params and stanza.
This function was introduced in QXmpp 1.5.
[since QXmpp 1.5] QXmppTask<QXmpp::SendResult> QXmppClient::send(QXmppStanza &&stanza, const std::optional<QXmppSendStanzaParams> & = {})
Sends a packet always without end-to-end-encryption.
This does the same as send(), but does not do any end-to-end encryption on the stanza.
Returns a QXmppTask that makes it possible to track the state of the packet.
This function was introduced in QXmpp 1.5.
[since QXmpp 1.5] QXmppTask<QXmppClient::EmptyResult> QXmppClient::sendGenericIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> & = {})
Sends an IQ iq and returns possible stanza errors.
If you want to parse a special IQ response in the result case, you can use sendIq() and parse the returned QDomElement.
Returns QXmpp::Success (on response type 'result') or the contained QXmppStanza::Error (on response type 'error').
This function was introduced in QXmpp 1.5.
[since QXmpp 1.5] QXmppTask<QXmppClient::IqResult> QXmppClient::sendIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> & = {})
Sends an IQ packet and returns the response asynchronously.
This is useful for further processing and parsing of the returned QDomElement. If you don't expect a special response, you may want use sendGenericIq().
IQs of type 'error' are parsed automatically and returned as QXmppError with a contained QXmppStanza::Error.
This does not do any end-to-encryption on the IQ.
iq.
This function was introduced in QXmpp 1.5.
See also sendSensitiveIq().
[since QXmpp 1.5] QXmppTask<QXmpp::SendResult> QXmppClient::sendSensitive(QXmppStanza &&stanza, const std::optional<QXmppSendStanzaParams> ¶ms = {})
Sends a packet and reports the result via QXmppTask.
If stream management is enabled, the task continues to be active until the server acknowledges the packet. On success, QXmpp::SendSuccess with acknowledged == true is reported and the task finishes.
If connection errors occur, the packet is resent if possible. If reconnecting is not possible, an error is reported.
Returns a QXmppTask that makes it possible to track the state of the packet.
params and stanza.
This function was introduced in QXmpp 1.5.
[since QXmpp 1.5] QXmppTask<QXmppClient::IqResult> QXmppClient::sendSensitiveIq(QXmppIq &&iq, const std::optional<QXmppSendStanzaParams> ¶ms = {})
Tries to encrypt and send an IQ packet and returns the response asynchronously.
This can be used for sensitive IQ requests performed from client to client. Most IQ requests like service discovery requests cannot be end-to-end encrypted or it only makes little sense to do so. This is why the default sendIq() does not do any additional end-to-end encryption.
IQs of type 'error' are parsed automatically and returned as QXmppError with a contained QXmppStanza::Error.
params and iq.
This function was introduced in QXmpp 1.5.
[since QXmpp 1.0] void QXmppClient::setActive(bool active)
Sets the client state as described in XEP-0352: Client State Indication.
Since QXmpp 1.8, the state is restored across reconnects. QXmpp will re-send the state of 'inactive' on connection if that was set before. Stream resumptions are also handled.
active.
This function was introduced in QXmpp 1.0.
See also isActive().
void QXmppClient::setClientPresence(const QXmppPresence &presence)
Changes the presence of the connected client.
The connection to the server will be updated accordingly:
- If the presence type is QXmppPresence::Unavailable, the connection to the server will be closed.
- Otherwise, the connection to the server will be established as needed.
presence is a QXmppPresence object.
See also clientPresence().
[since QXmpp 1.5] void QXmppClient::setEncryptionExtension(QXmppE2eeExtension *extension)
Sets the extension to be used for end-to-end-encryption.
extension.
This function was introduced in QXmpp 1.5.
See also encryptionExtension().
void QXmppClient::setLogger(QXmppLogger *logger)
Sets the QXmppLogger associated with the current QXmppClient.
logger.
Note: Setter function for property logger.
See also logger().
QAbstractSocket::SocketError QXmppClient::socketError()
Returns the socket error if error() is QXmppClient::SocketError.
QString QXmppClient::socketErrorString() const
Returns the human-readable description of the last socket error if error() is QXmppClient::SocketError.
[signal] void QXmppClient::sslErrors(const QList<QSslError> &errors)
This signal is emitted to indicate that one or more SSL errors were encountered while establishing the identity of the server.
errors.
QXmppClient::State QXmppClient::state() const
Returns the client's current state.
Note: Getter function for property state.
[signal] void QXmppClient::stateChanged(QXmppClient::State state)
This signal is emitted when the client state changes.
Note: Notifier signal for property state.
[since QXmpp 1.4] QXmppClient::StreamManagementState QXmppClient::streamManagementState() const
Returns the current XEP-0198: Stream Management state of the connection.
Upon connection of the client this can be used to check whether the previous stream has been resumed.
This function was introduced in QXmpp 1.4.
QXmppStanza::Error::Condition QXmppClient::xmppStreamError()
Returns the XMPP stream error if QXmppClient::Error is QXmppClient::XmppStreamError.
Related Non-Members
QLatin1String QXmppVersion()
Returns the version of QXmpp used at compile time as a string.