QXmpp Version: 1.5.6
Loading...
Searching...
No Matches
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 ~QXmppDataFormBase() = default;
18
19 virtual QXmppDataForm toDataForm() const;
20
21protected:
22 static bool fromDataForm(const QXmppDataForm &form, QXmppDataFormBase &parent);
23
24 virtual QString formType() const = 0;
25 virtual void parseForm(const QXmppDataForm &) = 0;
26 virtual void serializeForm(QXmppDataForm &) const = 0;
27
28 std::optional<quint32> parseUInt(const QVariant &variant)
29 {
30 bool ok;
31 if (const auto result = variant.toString().toUInt(&ok); ok) {
32 return result;
33 }
34 return std::nullopt;
35 }
36
37 std::optional<quint64> parseULongLong(const QVariant &variant)
38 {
39 bool ok;
40 if (const auto result = variant.toString().toULongLong(&ok); ok) {
41 return result;
42 }
43 return std::nullopt;
44 }
45
46 std::optional<bool> parseBool(const QVariant &variant)
47 {
48 if (variant.type() == QVariant::Bool) {
49 return variant.toBool();
50 }
51 return std::nullopt;
52 }
53
54 template<typename T>
55 static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
56 {
57 form.fields() << QXmppDataForm::Field(type, name, value);
58 }
59
60 template<typename T>
61 static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
62 {
63 if (!value.isNull()) {
64 serializeValue(form, type, name, value);
65 }
66 }
67
68 template<typename T>
69 static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
70 {
71 if (!value.isEmpty()) {
72 serializeValue(form, type, name, value);
73 }
74 }
75
76 template<typename T, typename ValueConverter = T (*)(T)>
77 static void serializeOptional(
78 QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const std::optional<T> &optional, ValueConverter convert = [](T a) { return a; })
79 {
80 if (optional.has_value()) {
81 serializeValue(form, type, name, convert(*optional));
82 }
83 }
84
85 template<typename T>
86 static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, std::optional<T> optional)
87 {
88 if (optional.has_value()) {
89 serializeValue(form, type, name, QString::number(*optional));
90 }
91 }
92
93 static void serializeDatetime(QXmppDataForm &form, const QString &name, const QDateTime &datetime, QXmppDataForm::Field::Type type = QXmppDataForm::Field::TextSingleField);
94};
95
96class QXmppExtensibleDataFormBasePrivate;
97
99{
100public:
106
110
111 QList<QXmppDataForm::Field> unknownFields() const;
112 void setUnknownFields(const QList<QXmppDataForm::Field> &unknownFields);
113
114protected:
115 void parseForm(const QXmppDataForm &) override;
116 void serializeForm(QXmppDataForm &) const override;
117
118 virtual bool parseField(const QXmppDataForm::Field &);
119
120private:
121 QSharedDataPointer<QXmppExtensibleDataFormBasePrivate> d;
122};
123
124#endif // QXMPPDATAFORMBASED_H
Definition QXmppDataFormBase.h:15
static void serializeOptional(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const std::optional< T > &optional, ValueConverter convert=[](T a) { return a;})
Definition QXmppDataFormBase.h:77
std::optional< quint32 > parseUInt(const QVariant &variant)
Definition QXmppDataFormBase.h:28
virtual void serializeForm(QXmppDataForm &) const =0
std::optional< quint64 > parseULongLong(const QVariant &variant)
Definition QXmppDataFormBase.h:37
virtual QString formType() const =0
static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, std::optional< T > optional)
Definition QXmppDataFormBase.h:86
static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
Definition QXmppDataFormBase.h:55
static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
Definition QXmppDataFormBase.h:69
virtual void parseForm(const QXmppDataForm &)=0
static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
Definition QXmppDataFormBase.h:61
std::optional< bool > parseBool(const QVariant &variant)
Definition QXmppDataFormBase.h:46
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:684
Definition QXmppDataFormBase.h:99