QXmpp Version: 1.10.0
QXmppDataFormBase.h
1// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPDATAFORMBASED_H
6#define QXMPPDATAFORMBASED_H
7
8#include "QXmppDataForm.h"
9
10#include <optional>
11
12class QXmppDataForm;
13
14class QXMPP_EXPORT QXmppDataFormBase
15{
16public:
17 virtual QXmppDataForm toDataForm() const;
18
19protected:
20 static bool fromDataForm(const QXmppDataForm &form, QXmppDataFormBase &parent);
21
22 virtual QString formType() const = 0;
23 virtual void parseForm(const QXmppDataForm &) = 0;
24 virtual void serializeForm(QXmppDataForm &) const = 0;
25
26 std::optional<quint32> parseUInt(const QVariant &variant)
27 {
28 bool ok;
29 if (const auto result = variant.toString().toUInt(&ok); ok) {
30 return result;
31 }
32 return std::nullopt;
33 }
34
35 std::optional<quint64> parseULongLong(const QVariant &variant)
36 {
37 bool ok;
38 if (const auto result = variant.toString().toULongLong(&ok); ok) {
39 return result;
40 }
41 return std::nullopt;
42 }
43
44 std::optional<bool> parseBool(const QVariant &variant)
45 {
46 if (variant.type() == QVariant::Bool) {
47 return variant.toBool();
48 }
49 return std::nullopt;
50 }
51
52 template<typename T>
53 static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
54 {
55 form.fields() << QXmppDataForm::Field(type, name, value);
56 }
57
58 template<typename T>
59 static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
60 {
61 if (!value.isNull()) {
62 serializeValue(form, type, name.toString(), value);
63 }
64 }
65
66 template<typename T>
67 static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
68 {
69 if (!value.isEmpty()) {
70 serializeValue(form, type, name.toString(), value);
71 }
72 }
73
74 template<typename T, typename ValueConverter = T (*)(T)>
75 static void serializeOptional(
76 QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const std::optional<T> &optional, ValueConverter convert = [](T a) { return a; })
77 {
78 if (optional.has_value()) {
79 serializeValue(form, type, name.toString(), convert(*optional));
80 }
81 }
82
83 template<typename T>
84 static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, std::optional<T> optional)
85 {
86 if (optional.has_value()) {
87 serializeValue(form, type, name.toString(), QString::number(*optional));
88 }
89 }
90
91 static void serializeDatetime(QXmppDataForm &form, const QString &name, const QDateTime &datetime, QXmppDataForm::Field::Type type = QXmppDataForm::Field::TextSingleField);
92};
93
94class QXmppExtensibleDataFormBasePrivate;
95
97{
98public:
104
108
109 QList<QXmppDataForm::Field> unknownFields() const;
110 void setUnknownFields(const QList<QXmppDataForm::Field> &unknownFields);
111
112protected:
113 void parseForm(const QXmppDataForm &) override;
114 void serializeForm(QXmppDataForm &) const override;
115
116 virtual bool parseField(const QXmppDataForm::Field &);
117
118private:
119 QSharedDataPointer<QXmppExtensibleDataFormBasePrivate> d;
120};
121
122#endif // QXMPPDATAFORMBASED_H
Definition: QXmppDataFormBase.h:15
static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, std::optional< T > optional)
Definition: QXmppDataFormBase.h:84
std::optional< quint32 > parseUInt(const QVariant &variant)
Definition: QXmppDataFormBase.h:26
virtual void serializeForm(QXmppDataForm &) const =0
std::optional< quint64 > parseULongLong(const QVariant &variant)
Definition: QXmppDataFormBase.h:35
virtual QString formType() const =0
static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
Definition: QXmppDataFormBase.h:59
static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
Definition: QXmppDataFormBase.h:53
virtual void parseForm(const QXmppDataForm &)=0
static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
Definition: QXmppDataFormBase.h:67
static void serializeOptional(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const std::optional< T > &optional, ValueConverter convert=[](T a) { return a;})
Definition: QXmppDataFormBase.h:75
std::optional< bool > parseBool(const QVariant &variant)
Definition: QXmppDataFormBase.h:44
Definition: QXmppDataForm.h:89
Type
This enum is used to describe a field's type.
Definition: QXmppDataForm.h:92
Definition: QXmppDataForm.h:27
QList< Field > fields() const
Definition: QXmppDataForm.cpp:688
Definition: QXmppDataFormBase.h:97