summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqttsubscription.cpp
blob: 0a1eaee1044b1e10306b4eef9fb506c60dd29701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qmqttsubscription.h"
#include "qmqttsubscription_p.h"
#include "qmqttclient.h"

QT_BEGIN_NAMESPACE

/*!
    \class QMqttSubscription

    \inmodule QtMqtt
    \brief The QMqttSubscription class receives notifications from an MQTT
    broker about a specified topic.
*/

/*!
    \property QMqttSubscription::state
    \brief This property holds the state of the subscription.
*/

/*!
    \property QMqttSubscription::qos
    \brief This property holds the QoS level at which the subscription has been
    made.

    The QoS level of the subscription specifies the \e maximum QoS level at
    which the client will receive messages. The publisher can still send
    messages at a lower level.
*/

/*!
    \property QMqttSubscription::topic
    \brief This property holds the topic of the subscription.
*/

/*!
    \enum QMqttSubscription::SubscriptionState

    This enum type describes the states a subscription can have.

    \value Unsubscribed
           The topic has been unsubscribed from.
    \value SubscriptionPending
           A request for a subscription has been sent, but is has not been
           confirmed by the broker yet.
    \value Subscribed
           The subscription was successful and messages will be received.
    \value UnsubscriptionPending
           A request to unsubscribe from a topic has been sent, but it has not
           been confirmed by the broker yet.
    \value Error
           An error occured.
*/

/*!
    \fn QMqttSubscription::messageReceived(QMqttMessage msg)

    This signal is emitted when the new message \a msg has been received.
*/

/*!
    \property QMqttSubscription::reason
    \since 5.12
    \brief This property holds the reason string for the subscription.

    A reason string is used by the server to provide additional information
    about the subscription. It is optional for the server to send it.
*/

/*!
    \property QMqttSubscription::reasonCode
    \since 5.12
    \brief This property holds the reason code for the subscription.

    The reason code specifies the error type if a subscription has failed,
    or the level of QoS for success.
*/

/*!
    \property QMqttSubscription::sharedSubscription
    \since 5.12
    \brief This property holds whether the subscription is shared.
*/

/*!
    \property QMqttSubscription::sharedSubscriptionName
    \since 5.12
    \brief This property holds the name of the shared subscription.
*/

QMqttSubscription::QMqttSubscription(QObject *parent) : QObject(*(new QMqttSubscriptionPrivate), parent)
{

}

/*!
    Deletes a subscription. If the \l topic was not already unsubscribed from,
    it will be unsubscribed from automatically.
*/
QMqttSubscription::~QMqttSubscription()
{
    Q_D(const QMqttSubscription);
    if (d->m_state == Subscribed)
        unsubscribe();
}

QMqttSubscription::SubscriptionState QMqttSubscription::state() const
{
    Q_D(const QMqttSubscription);
    return d->m_state;
}

QMqttTopicFilter QMqttSubscription::topic() const
{
    Q_D(const QMqttSubscription);
    return d->m_topic;
}

quint8 QMqttSubscription::qos() const
{
    Q_D(const QMqttSubscription);
    return d->m_qos;
}

QString QMqttSubscription::reason() const
{
    Q_D(const QMqttSubscription);
    return d->m_reasonString;
}

QMqtt::ReasonCode QMqttSubscription::reasonCode() const
{
    Q_D(const QMqttSubscription);
    return d->m_reasonCode;
}

/*!
    \since 5.12

    Returns the user properties received from the broker when the subscription
    has been accepted.

    \note This function will only provide valid data when the client
    specifies QMqttClient::MQTT_5_0 as QMqttClient::ProtocolVersion.
*/
QMqttUserProperties QMqttSubscription::userProperties() const
{
    Q_D(const QMqttSubscription);
    return d->m_userProperties;
}

bool QMqttSubscription::isSharedSubscription() const
{
    Q_D(const QMqttSubscription);
    return d->m_shared;
}

QString QMqttSubscription::sharedSubscriptionName() const
{
    Q_D(const QMqttSubscription);
    return d->m_sharedSubscriptionName;
}

void QMqttSubscription::setState(QMqttSubscription::SubscriptionState state)
{
    Q_D(QMqttSubscription);
    if (d->m_state == state)
        return;

    d->m_state = state;
    emit stateChanged(d->m_state);
}

/*!
    Unsubscribes from \l topic.

    \note This might affect all shared pointer instances returned by
    \l QMqttClient::subscribe().
*/
void QMqttSubscription::unsubscribe()
{
    Q_D(QMqttSubscription);
    d->m_client->unsubscribe(d->m_topic);
}

void QMqttSubscription::setTopic(const QMqttTopicFilter &topic)
{
    Q_D(QMqttSubscription);
    d->m_topic = topic;
}

void QMqttSubscription::setClient(QMqttClient *client)
{
    Q_D(QMqttSubscription);
    d->m_client = client;
}

void QMqttSubscription::setQos(quint8 qos)
{
    Q_D(QMqttSubscription);
    d->m_qos = qos;
}

void QMqttSubscription::setSharedSubscription(bool s)
{
    Q_D(QMqttSubscription);
    d->m_shared = s;
}

void QMqttSubscription::setSharedSubscriptionName(const QString &name)
{
    Q_D(QMqttSubscription);
    d->m_sharedSubscriptionName = name;
}

QMqttSubscriptionPrivate::QMqttSubscriptionPrivate()
    : QObjectPrivate()
{

}

QT_END_NAMESPACE