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

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