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