diff options
| author | Linus Jahn <lnj@kaidan.im> | 2021-09-02 16:14:02 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2021-09-02 16:26:19 +0200 |
| commit | d422c44bb0e908a56058caf1cfdd605756ca6b64 (patch) | |
| tree | 2083af30d0c7cd746c96f4e2529e93e65a687010 /src/base | |
| parent | ebd683a4589e34ae4f142bc6ac64ae2b563d78fa (diff) | |
| download | qxmpp-d422c44bb0e908a56058caf1cfdd605756ca6b64.tar.gz | |
PubSubNodeConfig: Add Max option to maxItems
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/QXmppPubSubNodeConfig.cpp | 38 | ||||
| -rw-r--r-- | src/base/QXmppPubSubNodeConfig.h | 15 |
2 files changed, 43 insertions, 10 deletions
diff --git a/src/base/QXmppPubSubNodeConfig.cpp b/src/base/QXmppPubSubNodeConfig.cpp index c7010e5b..a1d96425 100644 --- a/src/base/QXmppPubSubNodeConfig.cpp +++ b/src/base/QXmppPubSubNodeConfig.cpp @@ -60,6 +60,16 @@ static const auto ALLOW_SUBSCRIPTIONS = QStringLiteral("pubsub#subscribe"); static const auto TITLE = QStringLiteral("pubsub#title"); static const auto PAYLOAD_TYPE = QStringLiteral("pubsub#type"); +// helper for std::visit +template<class... Ts> +struct overloaded : Ts... +{ + using Ts::operator()...; +}; +// explicit deduction guide (not needed as of C++20) +template<class... Ts> +overloaded(Ts...) -> overloaded<Ts...>; + class QXmppPubSubNodeConfigPrivate : public QSharedData { public: @@ -78,7 +88,7 @@ public: std::optional<quint32> itemExpiry; std::optional<QXmppPubSubNodeConfig::ItemPublisher> notificationItemPublisher; QString language; - std::optional<quint32> maxItems; + QXmppPubSubNodeConfig::ItemLimit maxItems; std::optional<quint32> maxPayloadSize; std::optional<QXmppPubSubNodeConfig::NodeType> nodeType; std::optional<QXmppPubSubNodeConfig::NotificationType> notificationType; @@ -436,12 +446,12 @@ void QXmppPubSubNodeConfig::setLanguage(const QString &language) d->language = language; } -std::optional<quint32> QXmppPubSubNodeConfig::maxItems() const +QXmppPubSubNodeConfig::ItemLimit QXmppPubSubNodeConfig::maxItems() const { return d->maxItems; } -void QXmppPubSubNodeConfig::setMaxItems(std::optional<quint32> maxItems) +void QXmppPubSubNodeConfig::setMaxItems(ItemLimit maxItems) { d->maxItems = maxItems; } @@ -663,7 +673,14 @@ bool QXmppPubSubNodeConfig::parseField(const QXmppDataForm::Field &field) } else if (key == LANGUAGE) { d->language = value.toString(); } else if (key == MAX_ITEMS) { - d->maxItems = parseUInt(value); + bool ok = false; + if (const auto maxItems = value.toULongLong(&ok); ok) { + d->maxItems = maxItems; + } else if (value.type() == QVariant::String && value.toString() == QStringLiteral("max")) { + d->maxItems = Max(); + } else { + d->maxItems = Unset(); + } } else if (key == MAX_PAYLOAD_SIZE) { d->maxPayloadSize = parseUInt(value); } else if (key == NODE_TYPE) { @@ -767,10 +784,15 @@ void QXmppPubSubNodeConfig::serializeForm(QXmppDataForm &form) const Type::TextSingleField, LANGUAGE, d->language); - serializeOptionalNumber(form, - Type::TextSingleField, - MAX_ITEMS, - d->maxItems); + std::visit(overloaded { + [](Unset) {}, + [&](uint64_t value) { + serializeValue(form, Type::TextSingleField, MAX_ITEMS, QString::number(value)); + }, + [&](Max) { + serializeValue(form, Type::TextSingleField, MAX_ITEMS, QStringLiteral("max")); + } }, + d->maxItems); serializeOptionalNumber(form, Type::TextSingleField, MAX_PAYLOAD_SIZE, diff --git a/src/base/QXmppPubSubNodeConfig.h b/src/base/QXmppPubSubNodeConfig.h index 16e26709..510feed6 100644 --- a/src/base/QXmppPubSubNodeConfig.h +++ b/src/base/QXmppPubSubNodeConfig.h @@ -27,11 +27,21 @@ #include "QXmppDataForm.h" #include "QXmppDataFormBase.h" +#include <variant> + class QXmppPubSubNodeConfigPrivate; class QXMPP_EXPORT QXmppPubSubNodeConfig : public QXmppExtensibleDataFormBase { public: + struct Unset + { + }; + struct Max + { + }; + using ItemLimit = std::variant<Unset, uint64_t, Max>; + enum AccessModel : uint8_t { Open, Presence, @@ -140,8 +150,9 @@ public: QString language() const; void setLanguage(const QString &language); - std::optional<quint32> maxItems() const; - void setMaxItems(std::optional<quint32> maxItems); + ItemLimit maxItems() const; + void setMaxItems(ItemLimit maxItems); + inline void resetMaxItems() { setMaxItems(Unset()); } std::optional<quint32> maxPayloadSize() const; void setMaxPayloadSize(std::optional<quint32> maxPayloadSize); |
