QXmpp::Jid Class

class QXmpp::Jid

An XMPP address (JID) as defined in RFC 7622. More...

Header: #include <QXmppJid.h>
Since: QXmpp 1.18

Public Functions

Jid()
QXmpp::Jid bare() const
QStringView domain() const
QByteArray domainAce() const
QXmpp::Jid domainJid() const
bool hasLocalpart() const
bool hasResource() const
bool isBare() const
bool isDomain() const
bool isFull() const
bool isNull() const
QStringView localpart() const
QStringView resource() const
QString toDisplayString() const
QString toString() const
QString unescapedLocalpart() const
std::variant<QXmpp::Jid, QXmpp::JidParsingError> withResource(QStringView resource) const
operator QString() const

Static Public Members

QString escapeLocalpart(QStringView localpart)
std::variant<QXmpp::Jid, QXmpp::JidParsingError> fromParts(QStringView localpart, QStringView domain, QStringView resource = {})
std::optional<QXmpp::Jid> fromString(QStringView jid)
std::variant<QXmpp::Jid, QXmpp::JidParsingError> fromUserInput(QStringView input)
std::variant<QXmpp::Jid, QXmpp::JidParsingError> parse(QStringView jid)
QString unescapeLocalpart(QStringView localpart)
size_t qHash(const QXmpp::Jid &jid, size_t seed = 0)
QDebug operator<<(QDebug debug, const QXmpp::Jid &jid)
std::strong_ordering operator<=>(const QXmpp::Jid &a, const QXmpp::Jid &b)
bool operator==(const QXmpp::Jid &a, const QXmpp::Jid &b)

Detailed Description

A Jid is always stored in its canonical form: the localpart is enforced using the PRECIS UsernameCaseMapped profile (RFC 8265), the domainpart is converted to Unicode labels (IDNA2008) and lowercased, and the resourcepart is enforced using the PRECIS OpaqueString profile. As a result, two Jids can be compared with == and used as hash keys directly. Note that this also means that the original case of the localpart and domainpart is not preserved: Juliet@Example.com becomes juliet@example.com.

A Jid can only be created from valid input; a default-constructed Jid is null.

auto jid = QXmpp::Jid::fromString(u"Juliet@Example.com/Balcony");
jid->localpart();  // "juliet"
jid->bare();       // juliet@example.com

Member Function Documentation

[constexpr noexcept default] Jid::Jid()

Constructs a null Jid.

QXmpp::Jid Jid::bare() const

Returns the Jid without its resourcepart.

QStringView Jid::domain() const

Returns the domainpart with Unicode labels (U-labels).

See also domainAce().

QByteArray Jid::domainAce() const

Returns the domainpart in ASCII-compatible encoding (A-labels), e.g. for DNS lookups.

IP literals are returned unchanged.

QXmpp::Jid Jid::domainJid() const

Returns a Jid consisting only of the domainpart.

[static] QString Jid::escapeLocalpart(QStringView localpart)

Escapes the characters in localpart that are not allowed in a localpart according to XEP-0106: JID Escaping.

Use this to build a localpart from user input or from an address of a foreign system. Leading and trailing spaces are removed, since they cannot be escaped.

QXmpp::Jid::escapeLocalpart(u"d'artagnan");  // "d\27artagnan"

[static] std::variant<QXmpp::Jid, QXmpp::JidParsingError> Jid::fromParts(QStringView localpart, QStringView domain, QStringView resource = {})

Creates a Jid from its parts and enforces them.

An empty localpart or resource means that the JID has no localpart or resourcepart. The domain is required. The localpart must already be escaped, see escapeLocalpart().

[static] std::optional<QXmpp::Jid> Jid::fromString(QStringView jid)

Parses jid like parse(), but returns std::nullopt instead of an error.

[static] std::variant<QXmpp::Jid, QXmpp::JidParsingError> Jid::fromUserInput(QStringView input)

Creates a Jid from a JID entered by a user, e.g. in a dialog for adding a contact.

In contrast to parse(), whitespace and invisible characters at the beginning and end of input and of its parts are ignored, a slash at the end is ignored and the localpart is escaped as defined in XEP-0106: JID Escaping, so that a user can enter d'artagnan@musketeers.lit. The localpart ends at the last @ before the first /, so the localpart of user@host@example.com is user@host.

QXmpp::Jid::fromUserInput(u" d'artagnan@musketeers.lit ");  // d\27artagnan@musketeers.lit

Localparts without characters that need to be escaped are used as they are, so that already escaped JIDs, e.g. copied from another client, are not escaped twice. Use toDisplayString() to show the result to the user.

bool Jid::hasLocalpart() const

Returns true if the Jid has a localpart.

bool Jid::hasResource() const

Returns true if the Jid has a resourcepart.

bool Jid::isBare() const

Returns true if the Jid is not null and has no resourcepart.

bool Jid::isDomain() const

Returns true if the Jid consists of a domainpart only.

bool Jid::isFull() const

Returns true if the Jid has a resourcepart.

bool Jid::isNull() const

Returns true if this is a default-constructed Jid.

QStringView Jid::localpart() const

Returns the localpart in its escaped (wire) form, or an empty view if there is none.

See also unescapedLocalpart().

[static] std::variant<QXmpp::Jid, QXmpp::JidParsingError> Jid::parse(QStringView jid)

Parses and enforces the JID string jid.

The string is split into its parts as described in RFC 7622, section 3.2, before any of the parts is normalized.

QStringView Jid::resource() const

Returns the resourcepart, or an empty view if there is none.

QString Jid::toDisplayString() const

Returns the Jid for presentation to users, with XEP-0106: JID Escaping reverted.

The result must not be used for anything else than display.

QString Jid::toString() const

Returns the canonical string representation of the Jid, as used on the wire.

[static] QString Jid::unescapeLocalpart(QStringView localpart)

Reverts XEP-0106: JID Escaping of localpart.

Only the escape sequences defined in XEP-0106 are replaced; any other backslash sequence is kept as is.

QString Jid::unescapedLocalpart() const

Returns the localpart with XEP-0106: JID Escaping reverted, for presentation to users.

std::variant<QXmpp::Jid, QXmpp::JidParsingError> Jid::withResource(QStringView resource) const

Returns a copy of the bare Jid with resource as resourcepart.

Returns an error if this Jid is null or resource is not a valid resourcepart.

Jid::operator QString() const

Returns the canonical string representation of the Jid, like toString().

This allows passing a Jid to functions taking a JID as QString. Comparisons with a QString compare the canonical form with the string as it is.

Related Non-Members

[noexcept] size_t qHash(const QXmpp::Jid &jid, size_t seed = 0)

Returns the hash value for jid using seed.

QDebug operator<<(QDebug debug, const QXmpp::Jid &jid)

Writes jid to debug.

std::strong_ordering operator<=>(const QXmpp::Jid &a, const QXmpp::Jid &b)

Orders a and b by their canonical string representation.

bool operator==(const QXmpp::Jid &a, const QXmpp::Jid &b)

Returns true if a and b are the same JID (RFC 7622, section 3.2.3, 3.3 and 3.4).