1// Copyright (C) 2017 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 "qmqttsubscription.h"
6#include "qmqttsubscription_p.h"
7#include "qmqttclient.h"
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \class QMqttSubscription
13
14 \inmodule QtMqtt
15 \brief The QMqttSubscription class receives notifications from an MQTT
16 broker about a specified topic.
17*/
18
19/*!
20 \property QMqttSubscription::state
21 \brief This property holds the state of the subscription.
22*/
23
24/*!
25 \property QMqttSubscription::qos
26 \brief This property holds the QoS level at which the subscription has been
27 made.
28
29 The QoS level of the subscription specifies the \e maximum QoS level at
30 which the client will receive messages. The publisher can still send
31 messages at a lower level.
32*/
33
34/*!
35 \property QMqttSubscription::topic
36 \brief This property holds the topic of the subscription.
37*/
38
39/*!
40 \enum QMqttSubscription::SubscriptionState
41
42 This enum type describes the states a subscription can have.
43
44 \value Unsubscribed
45 The topic has been unsubscribed from.
46 \value SubscriptionPending
47 A request for a subscription has been sent, but is has not been
48 confirmed by the broker yet.
49 \value Subscribed
50 The subscription was successful and messages will be received.
51 \value UnsubscriptionPending
52 A request to unsubscribe from a topic has been sent, but it has not
53 been confirmed by the broker yet.
54 \value Error
55 An error occured.
56*/
57
58/*!
59 \fn QMqttSubscription::messageReceived(QMqttMessage msg)
60
61 This signal is emitted when the new message \a msg has been received.
62*/
63
64/*!
65 \property QMqttSubscription::reason
66 \since 5.12
67 \brief This property holds the reason string for the subscription.
68
69 A reason string is used by the server to provide additional information
70 about the subscription. It is optional for the server to send it.
71*/
72
73/*!
74 \property QMqttSubscription::reasonCode
75 \since 5.12
76 \brief This property holds the reason code for the subscription.
77
78 The reason code specifies the error type if a subscription has failed,
79 or the level of QoS for success.
80*/
81
82/*!
83 \property QMqttSubscription::sharedSubscription
84 \since 5.12
85 \brief This property holds whether the subscription is shared.
86*/
87
88/*!
89 \property QMqttSubscription::sharedSubscriptionName
90 \since 5.12
91 \brief This property holds the name of the shared subscription.
92*/
93
94QMqttSubscription::QMqttSubscription(QObject *parent) : QObject(*(new QMqttSubscriptionPrivate), parent)
95{
96
97}
98
99/*!
100 Deletes a subscription. If the \l topic was not already unsubscribed from,
101 it will be unsubscribed from automatically.
102*/
103QMqttSubscription::~QMqttSubscription()
104{
105 Q_D(const QMqttSubscription);
106 if (d->m_state == Subscribed)
107 unsubscribe();
108}
109
110QMqttSubscription::SubscriptionState QMqttSubscription::state() const
111{
112 Q_D(const QMqttSubscription);
113 return d->m_state;
114}
115
116QMqttTopicFilter QMqttSubscription::topic() const
117{
118 Q_D(const QMqttSubscription);
119 return d->m_topic;
120}
121
122quint8 QMqttSubscription::qos() const
123{
124 Q_D(const QMqttSubscription);
125 return d->m_qos;
126}
127
128QString QMqttSubscription::reason() const
129{
130 Q_D(const QMqttSubscription);
131 return d->m_reasonString;
132}
133
134QMqtt::ReasonCode QMqttSubscription::reasonCode() const
135{
136 Q_D(const QMqttSubscription);
137 return d->m_reasonCode;
138}
139
140/*!
141 \since 5.12
142
143 Returns the user properties received from the broker when the subscription
144 has been accepted.
145
146 \note This function will only provide valid data when the client
147 specifies QMqttClient::MQTT_5_0 as QMqttClient::ProtocolVersion.
148*/
149QMqttUserProperties QMqttSubscription::userProperties() const
150{
151 Q_D(const QMqttSubscription);
152 return d->m_userProperties;
153}
154
155bool QMqttSubscription::isSharedSubscription() const
156{
157 Q_D(const QMqttSubscription);
158 return d->m_shared;
159}
160
161QString QMqttSubscription::sharedSubscriptionName() const
162{
163 Q_D(const QMqttSubscription);
164 return d->m_sharedSubscriptionName;
165}
166
167void QMqttSubscription::setState(QMqttSubscription::SubscriptionState state)
168{
169 Q_D(QMqttSubscription);
170 if (d->m_state == state)
171 return;
172
173 d->m_state = state;
174 emit stateChanged(state: d->m_state);
175}
176
177/*!
178 Unsubscribes from \l topic.
179
180 \note This might affect all shared pointer instances returned by
181 \l QMqttClient::subscribe().
182*/
183void QMqttSubscription::unsubscribe()
184{
185 Q_D(QMqttSubscription);
186 d->m_client->unsubscribe(topic: d->m_topic);
187}
188
189void QMqttSubscription::setTopic(const QMqttTopicFilter &topic)
190{
191 Q_D(QMqttSubscription);
192 d->m_topic = topic;
193}
194
195void QMqttSubscription::setClient(QMqttClient *client)
196{
197 Q_D(QMqttSubscription);
198 d->m_client = client;
199}
200
201void QMqttSubscription::setQos(quint8 qos)
202{
203 Q_D(QMqttSubscription);
204 d->m_qos = qos;
205}
206
207void QMqttSubscription::setSharedSubscription(bool s)
208{
209 Q_D(QMqttSubscription);
210 d->m_shared = s;
211}
212
213void QMqttSubscription::setSharedSubscriptionName(const QString &name)
214{
215 Q_D(QMqttSubscription);
216 d->m_sharedSubscriptionName = name;
217}
218
219QMqttSubscriptionPrivate::QMqttSubscriptionPrivate()
220 : QObjectPrivate()
221{
222
223}
224
225QT_END_NAMESPACE
226

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