summaryrefslogtreecommitdiff
path: root/src/enginio_client/enginioreply.cpp
blob: 6e0c8ac4d1e4c03bcbbf6ad03c551038970311cd (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtEnginio module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/qdebug.h>
#include <QtCore/qstring.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsondocument.h>
#include <QtNetwork/qnetworkreply.h>

#include <Enginio/enginioreplystate.h>
#include <Enginio/enginioreply.h>
#include <Enginio/private/enginioreply_p.h>
#include <Enginio/enginioclient.h>
#include <Enginio/private/enginioclient_p.h>
#include <Enginio/private/enginioobjectadaptor_p.h>

QT_BEGIN_NAMESPACE

/*!
  \class EnginioReply
  \since 5.3
  \brief The EnginioReply class contains the data from a request to the Enginio database.
  \inmodule enginio-qt
  \ingroup enginio-client

  The reply, when finished, contains information received from the server:
  \list
  \li Data - object, which is a result from an earlier request,
    see the \l {EnginioReply::data}{data} function
  \li Network status - in case of a network problem, additional information can
  be accessed through: errorType, errorString, networkError
  \li Backend status - a finished request is always associated with a backend status
  code, which is just an HTTP code, and it can be queried through backendStatus
  \endlist

  The finished signal is emitted when the query is done.

  \sa EnginioClient
*/

/*!
  \class EnginioReplyState
  \since 5.3
  \internal
*/

/*!
  \enum Enginio::ErrorType
  Describes the type of error that occured when making a request to the Enginio backend.
  \value NoError The reply returned without errors
  \value NetworkError The error was a networking problem
  \value BackendError The backend did not accept the query
*/

/*!
  \fn EnginioReply::finished(EnginioReply *reply)
  This signal is emitted when the EnginioReply \a reply is finished.
  After the network operation, use the \l{EnginioReply::isError()}{isError()} function to check for
  potential problems and then use the \l data property to access the returned data.
*/

/*!
  \fn EnginioReply::progress(qint64 bytesSent, qint64 bytesTotal)
  This signal is emitted for file operations, indicating the progress of up or downloads.
  The \a bytesSent is the current progress relative to the total \a bytesTotal.
*/

class EnginioReplyPrivate: public EnginioReplyStatePrivate {
    Q_DECLARE_PUBLIC(EnginioReply)
public:
    EnginioReplyPrivate(EnginioClientConnectionPrivate *p, QNetworkReply *reply)
        : EnginioReplyStatePrivate(p, reply)
    {}
    void emitFinished() Q_DECL_OVERRIDE;
};

/*!
  \internal
*/
EnginioReply::EnginioReply(EnginioClientConnectionPrivate *p, QNetworkReply *reply)
    : EnginioReplyState(p, reply, new EnginioReplyPrivate(p, reply))
{
    QObject::connect(this, &EnginioReply::dataChanged, this, &EnginioReplyState::dataChanged);
}

/*!
  \brief Destroys the EnginioReply.

  The reply needs to be deleted after the finished signal is emitted.
*/
EnginioReply::~EnginioReply()
{}

/*!
  \property EnginioReply::networkError
  This property holds the network error for the request.
*/

QNetworkReply::NetworkError EnginioReplyState::networkError() const
{
    Q_D(const EnginioReplyState);
    return d->errorCode();
}

/*!
  \property EnginioReply::errorString
  This property holds the error for the request as human readable string.
  Check \l{EnginioReply::isError()}{isError()} first to check if the reply is an error.
*/

QString EnginioReplyState::errorString() const
{
    Q_D(const EnginioReplyState);
    return d->errorString();
}

/*!
  \property EnginioReply::requestId
  This property holds the API request ID for the request.
  The request ID is useful for end-to-end tracking of requests and to identify
  the origin of notifications.
  \internal
*/

/*!
  \internal
*/
QString EnginioReplyState::requestId() const
{
    Q_D(const EnginioReplyState);
    return d->requestId();
}

/*!
  \property EnginioReply::data
  \brief The data returned from the backend
  This property holds the JSON data returned by the server after a successful request.
*/

QJsonObject EnginioReply::data() const
{
    Q_D(const EnginioReply);
    return d->data();
}

/*!
  \internal
*/
void EnginioReplyPrivate::emitFinished()
{
    Q_Q(EnginioReply);
    emit q->finished(q);
}

/*!
  \internal
*/
void EnginioReplyState::setNetworkReply(QNetworkReply *reply)
{
    Q_D(EnginioReplyState);
    d->setNetworkReply(reply);
}

void EnginioReplyStatePrivate::setNetworkReply(QNetworkReply *reply)
{
    Q_Q(EnginioReplyState);
    _client->unregisterReply(_nreply);

    if (gEnableEnginioDebugInfo)
        _client->_requestData.remove(_nreply);

    if (!_nreply->isFinished()) {
        _nreply->setParent(_nreply->manager());
        QObject::connect(_nreply, &QNetworkReply::finished, _nreply, &QNetworkReply::deleteLater);
        _nreply->abort();
    } else {
        _nreply->deleteLater();
    }
    _nreply = reply;
    _data = QByteArray();

    _client->registerReply(reply, q);
}

/*!
  \internal
*/
void EnginioReplyState::swapNetworkReply(EnginioReplyState *other)
{
    Q_D(EnginioReplyState);
    d->swapNetworkReply(other->d_func());
}

void EnginioReplyStatePrivate::swapNetworkReply(EnginioReplyStatePrivate *other)
{
    Q_Q(EnginioReplyState);
    Q_ASSERT(other->_client == _client);
    _client->unregisterReply(_nreply);
    _client->unregisterReply(other->_nreply);

    qSwap(_nreply, other->_nreply);
    _data = other->_data = QByteArray();

    _client->registerReply(_nreply, q);
    _client->registerReply(other->_nreply, other->q_func());
}

/*!
  \internal
*/
void EnginioReplyState::dumpDebugInfo() const
{
    Q_D(const EnginioReplyState);
    d->dumpDebugInfo();
}

/*!
  \internal
  Mark this EnginioReply as not finished, the finished signal
  will be delayed until delayFinishedSignal() is returning true.

  \note The feature can be used only with one EnginioClient
*/
void EnginioReplyState::setDelayFinishedSignal(bool delay)
{
    Q_D(EnginioReplyState);
    d->_delay = delay;
    d->_client->finishDelayedReplies();
}

/*!
  \internal
  Returns true if signal should be delayed
 */
bool EnginioReplyState::delayFinishedSignal()
{
    Q_D(EnginioReplyState);
    return d->_delay;
}

/*!
  \fn bool EnginioReply::isError() const
  Returns whether this reply was unsuccessful.
  Returns true if the reply did not succeed.
*/

bool EnginioReplyState::isError() const
{
    Q_D(const EnginioReplyState);
    return d->errorCode() != QNetworkReply::NoError;
}

/*!
  \fn bool EnginioReply::isFinished() const
  Returns whether this reply was finished or not.
  Returns true if the reply was finished.
*/

bool EnginioReplyState::isFinished() const
{
    Q_D(const EnginioReplyState);
    return d->isFinished();
}

/*!
  \property EnginioReply::backendStatus
  \return the backend return status for this reply.
  \sa Enginio::ErrorType
*/

int EnginioReplyState::backendStatus() const
{
    Q_D(const EnginioReplyState);
    return d->backendStatus();
}

/*!
  \property EnginioReply::errorType
  \return the type of the error
  \sa Enginio::ErrorType
*/

Enginio::ErrorType EnginioReplyState::errorType() const
{
    Q_D(const EnginioReplyState);
    return d->errorType();
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const EnginioReply *reply)
{
    if (!reply) {
        d << "EnginioReply(null)";
        return d;
    }
    d.nospace();
    d << "EnginioReply(" << hex << (const void *) reply << dec;

    if (!reply->isError()) {
        d << " success data=" << reply->data();
    } else {
        d << " errorCode=" << reply->networkError() << " ";
        d << " errorString=" << reply->errorString() << " ";
        d << " errorData=" << reply->data() << " ";
    }
    d << "backendStatus=" << reply->backendStatus();
    d << ")";
    return d.space();
}
#endif // QT_NO_DEBUG_STREAM

EnginioReplyState::EnginioReplyState(EnginioClientConnectionPrivate *parent, QNetworkReply *reply, EnginioReplyStatePrivate *priv)
    : QObject(*priv, parent->q_ptr)
{
    parent->registerReply(reply, this);
}

EnginioReplyState::~EnginioReplyState()
{
    Q_D(EnginioReplyState);
    Q_ASSERT(d->_nreply->parent() == this);
    if (Q_UNLIKELY(!d->isFinished())) {
        QObject::connect(d->_nreply, &QNetworkReply::finished, d->_nreply, &QNetworkReply::deleteLater);
        d->_client->unregisterReply(d->_nreply);
        d->_nreply->setParent(d->_nreply->manager());
        d->_nreply->abort();
    }
}

/*!
  \internal
*/
QJsonObject EnginioReplyState::data() const
{
    Q_D(const EnginioReplyState);
    return d->data();
}

QT_END_NAMESPACE