summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqttsubscription.cpp
blob: 80e8aed3a75d40465a61b8c56e96ef35f6fefa04 (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
/******************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtMqtt module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "qmqttsubscription.h"
#include "qmqttsubscription_p.h"
#include <QtMqtt/QMqttClient>

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.
*/

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;
}

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

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

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);
    setState(Unsubscribed);
}

void QMqttSubscription::setTopic(const QString &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;
}

QMqttSubscriptionPrivate::QMqttSubscriptionPrivate()
    : QObjectPrivate()
{

}

QT_END_NAMESPACE