1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qmqttpublishproperties.h"
5#include "qmqttpublishproperties_p.h"
6#include "qmqtttype.h"
7
8#include <QtCore/QLoggingCategory>
9
10QT_BEGIN_NAMESPACE
11
12Q_DECLARE_LOGGING_CATEGORY(lcMqttClient)
13
14/*!
15 \class QMqttPublishProperties
16
17 \inmodule QtMqtt
18 \since 5.12
19
20 \brief The QMqttPublishProperties class represents configuration
21 options for sending or receiving a message.
22
23 Invoking QMqttClient::publish() to send a message to a broker can
24 include QMqttPublishProperties to provide additional arguments on
25 how the message should be treated on the broker.
26
27 Furthermore receiving a message by an instantiated subscription
28 might contain publish properties which have been forwarded or
29 adapted by the server.
30
31 \note Publish properties are part of the MQTT 5.0 specification and
32 cannot be used when connecting with a lower protocol level. See
33 QMqttClient::ProtocolVersion for more information.
34*/
35
36/*!
37 \enum QMqttPublishProperties::PublishPropertyDetail
38
39 This enum type specifies the available properties set by the
40 server or the client when creating a message.
41
42 \value None
43 No property has been specified.
44 \value PayloadFormatIndicator
45 The type of content of the message.
46 \value MessageExpiryInterval
47 The duration a message is valid.
48 \value TopicAlias
49 The topic alias for this message.
50 \value ResponseTopic
51 The topic the receipient should respond to.
52 \value CorrelationData
53 An identifier of the response message.
54 \value UserProperty
55 Additional properties set by the user.
56 \value SubscriptionIdentifier
57 An identifier of subscriptions matching the publication.
58 \value ContentType
59 A description of the content of the message.
60*/
61
62QMqttPublishProperties::QMqttPublishProperties() : data(new QMqttPublishPropertiesData)
63{
64}
65
66/*!
67 \internal
68*/
69QMqttPublishProperties::QMqttPublishProperties(const QMqttPublishProperties &) = default;
70
71/*!
72 \internal
73*/
74QMqttPublishProperties &QMqttPublishProperties::operator=(const QMqttPublishProperties &rhs)
75{
76 if (this != &rhs)
77 data.operator=(o: rhs.data);
78 return *this;
79}
80
81QMqttPublishProperties::~QMqttPublishProperties() = default;
82
83/*!
84 Returns the available properties specified in this instance. When a message
85 is created, it does not need to include all properties. This function
86 serves as an indicator of those properties which have been explicitly
87 set.
88*/
89QMqttPublishProperties::PublishPropertyDetails QMqttPublishProperties::availableProperties() const
90{
91 return data->details;
92}
93
94/*!
95 Returns the payload format indicator.
96*/
97QMqtt::PayloadFormatIndicator QMqttPublishProperties::payloadFormatIndicator() const
98{
99 return data->payloadIndicator;
100}
101
102/*!
103 Sets the payload format indicator to \a indicator.
104*/
105void QMqttPublishProperties::setPayloadFormatIndicator(QMqtt::PayloadFormatIndicator indicator)
106{
107 data->details |= QMqttPublishProperties::PayloadFormatIndicator;
108 data->payloadIndicator = indicator;
109}
110
111/*!
112 Returns the message expiry interval. This value specifies the number
113 of seconds a server is allowed to forward the message. If the interval
114 expires, the server must delete the message and abort publishing it.
115*/
116quint32 QMqttPublishProperties::messageExpiryInterval() const
117{
118 return data->messageExpiry;
119}
120
121/*!
122 Sets the message expiry interval to \a interval.
123*/
124void QMqttPublishProperties::setMessageExpiryInterval(quint32 interval)
125{
126 data->details |= QMqttPublishProperties::MessageExpiryInterval;
127 data->messageExpiry = interval;
128}
129
130/*!
131 Returns the topic alias used for publishing a message.
132*/
133quint16 QMqttPublishProperties::topicAlias() const
134{
135 return data->topicAlias;
136}
137
138/*!
139 Sets the topic alias for publishing a message to \a alias. A topic alias
140 value must be greater than zero and less than the maximum topic alias
141 specified by the server.
142
143 \sa QMqttServerConnectionProperties::maximumTopicAlias()
144*/
145void QMqttPublishProperties::setTopicAlias(quint16 alias)
146{
147 if (alias == 0) {
148 qCDebug(lcMqttClient) << "A topic alias with value 0 is not allowed.";
149 return;
150 }
151 data->details |= QMqttPublishProperties::TopicAlias;
152 data->topicAlias = alias;
153}
154
155/*!
156 Returns the response topic a user should use as a follow up to
157 a request.
158*/
159QString QMqttPublishProperties::responseTopic() const
160{
161 return data->responseTopic;
162}
163
164/*!
165 Sets the response topic to \a topic.
166*/
167void QMqttPublishProperties::setResponseTopic(const QString &topic)
168{
169 data->details |= QMqttPublishProperties::ResponseTopic;
170 data->responseTopic = topic;
171}
172
173/*!
174 Returns the correlation data.
175*/
176QByteArray QMqttPublishProperties::correlationData() const
177{
178 return data->correlationData;
179}
180
181/*!
182 Sets the correlation data to \a correlation.
183*/
184void QMqttPublishProperties::setCorrelationData(const QByteArray &correlation)
185{
186 data->details |= QMqttPublishProperties::CorrelationData;
187 data->correlationData = correlation;
188}
189
190/*!
191 Returns the user properties of a message.
192*/
193QMqttUserProperties QMqttPublishProperties::userProperties() const
194{
195 return data->userProperties;
196}
197
198/*!
199 Sets the user properties of a message to \a properties.
200*/
201void QMqttPublishProperties::setUserProperties(const QMqttUserProperties &properties)
202{
203 data->details |= QMqttPublishProperties::UserProperty;
204 data->userProperties = properties;
205}
206
207/*!
208 Returns the subscription identifiers of subscriptions matching
209 the topic filter of the message.
210*/
211QList<quint32> QMqttPublishProperties::subscriptionIdentifiers() const
212{
213 return data->subscriptionIdentifier;
214}
215
216/*!
217 Sets the subscription identifiers to \a ids.
218*/
219void QMqttPublishProperties::setSubscriptionIdentifiers(const QList<quint32> &ids)
220{
221 if (ids.contains(t: quint32(0))) {
222 qCDebug(lcMqttClient) << "A subscription identifier with value 0 is not allowed.";
223 return;
224 }
225 data->details |= QMqttPublishProperties::SubscriptionIdentifier;
226 data->subscriptionIdentifier = ids;
227}
228
229/*!
230 Returns the content type of the message.
231*/
232QString QMqttPublishProperties::contentType() const
233{
234 return data->contentType;
235}
236
237/*!
238 Sets the content type of the message to \a type.
239*/
240void QMqttPublishProperties::setContentType(const QString &type)
241{
242 data->details |= QMqttPublishProperties::ContentType;
243 data->contentType = type;
244}
245
246/*!
247 \class QMqttMessageStatusProperties
248
249 \inmodule QtMqtt
250 \since 5.12
251
252 \brief The QMqttMessageStatusProperties class represents additional
253 information provided by the server during message delivery.
254
255 Depending on the QoS level of a message being sent by QMqttClient::publish(),
256 a server reports the state of delivery. Additionally to the QMqtt::MessageStatus,
257 complementary information might be included by the server. These are exposed to
258 users via QMqttMessageStatusProperties.
259
260 \note Message status properties are part of the MQTT 5.0 specification and
261 cannot be used when connecting with a lower protocol level. See
262 QMqttClient::ProtocolVersion for more information.
263*/
264QMqttMessageStatusProperties::QMqttMessageStatusProperties() : data(new QMqttMessageStatusPropertiesData)
265{
266
267}
268
269QMqttMessageStatusProperties &QMqttMessageStatusProperties::operator=(const QMqttMessageStatusProperties &rhs)
270{
271 if (this != &rhs)
272 data.operator=(o: rhs.data);
273 return *this;
274}
275
276/*!
277 Returns the reason code of a failed message delivery.
278*/
279QMqtt::ReasonCode QMqttMessageStatusProperties::reasonCode() const
280{
281 return data->reasonCode;
282}
283
284/*!
285 Returns the reason string of a failed message delivery.
286*/
287QString QMqttMessageStatusProperties::reason() const
288{
289 return data->reasonString;
290}
291
292/*!
293 Returns properties specified in conjunction with a message.
294*/
295QMqttUserProperties QMqttMessageStatusProperties::userProperties() const
296{
297 return data->userProperties;
298}
299
300QMqttMessageStatusProperties::~QMqttMessageStatusProperties() = default;
301QMqttMessageStatusProperties::QMqttMessageStatusProperties(const QMqttMessageStatusProperties &) = default;
302
303QT_END_NAMESPACE
304

source code of qtmqtt/src/mqtt/qmqttpublishproperties.cpp