QXmpp Version: 1.10.0
QXmppSceEnvelope_p.h
1// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5//
6// W A R N I N G
7// -------------
8//
9// This file is not part of the QXmpp API.
10//
11// This header file may change from version to version without notice,
12// or even be removed.
13//
14// We mean it.
15//
16
17#ifndef QXMPPSCEENVELOPE_P_H
18#define QXMPPSCEENVELOPE_P_H
19
20#include "QXmppUtils.h"
21
22#include <QDateTime>
23#include <QDomElement>
24
25class QXmppSceEnvelopeReader
26{
27public:
28 QXmppSceEnvelopeReader(QDomElement &&element)
29 : element(std::move(element))
30 {
31 }
32
33 inline QDomElement contentElement()
34 {
35 return element.firstChildElement(QStringLiteral("content"));
36 }
37 inline QString from()
38 {
39 return element.firstChildElement(QStringLiteral("from")).attribute(QStringLiteral("jid"));
40 }
41 inline QString to()
42 {
43 return element.firstChildElement(QStringLiteral("to")).attribute(QStringLiteral("jid"));
44 }
45 inline QDateTime timestamp()
46 {
48 element.firstChildElement(QStringLiteral("time")).attribute(QStringLiteral("stamp")));
49 }
50
51 // rpad is usually not needed (but can be parsed manually if really needed)
52
53private:
54 QDomElement element;
55};
56
57class QXmppSceEnvelopeWriter
58{
59public:
60 QXmppSceEnvelopeWriter(QXmlStreamWriter &writer)
61 : writer(writer)
62 {
63 }
64
65 inline void start()
66 {
67 writer.writeStartElement(QStringLiteral("envelope"));
68 writer.writeDefaultNamespace(QStringLiteral("urn:xmpp:sce:1"));
69 }
70 inline void end()
71 {
72 writer.writeEndElement();
73 }
74 template<typename Functor>
75 void writeContent(Functor writeContent)
76 {
77 writer.writeStartElement(QStringLiteral("content"));
78 writeContent();
79 writer.writeEndElement();
80 }
81 inline void writeFrom(const QString &jid)
82 {
83 writer.writeStartElement(QStringLiteral("from"));
84 writer.writeAttribute(QStringLiteral("jid"), jid);
85 writer.writeEndElement();
86 }
87 inline void writeTo(const QString &jid)
88 {
89 writer.writeStartElement(QStringLiteral("to"));
90 writer.writeAttribute(QStringLiteral("jid"), jid);
91 writer.writeEndElement();
92 }
93 inline void writeTimestamp(const QDateTime &timestamp)
94 {
95 writer.writeStartElement(QStringLiteral("time"));
96 writer.writeAttribute(QStringLiteral("stamp"), QXmppUtils::datetimeToString(timestamp));
97 writer.writeEndElement();
98 }
99 inline void writeRpad(const QString &value)
100 {
101 writer.writeStartElement(QStringLiteral("rpad"));
102 writer.writeCharacters(value);
103 writer.writeEndElement();
104 }
105
106private:
107 QXmlStreamWriter &writer;
108};
109
110#endif // QXMPPSCEENVELOPE_P_H
static QString datetimeToString(const QDateTime &dt)
Definition: QXmppUtils.cpp:120
static QDateTime datetimeFromString(QStringView str)
Definition: QXmppUtils.cpp:103