1 | // Copyright (C) 2024 Jarek Kobus |
2 | // Copyright (C) 2024 The Qt Company Ltd. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "networkquery.h" |
6 | |
7 | #include <QtNetwork/QNetworkAccessManager> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | namespace Tasking { |
12 | |
13 | void NetworkQuery::start() |
14 | { |
15 | if (m_reply) { |
16 | qWarning(msg: "The NetworkQuery is already running. Ignoring the call to start()." ); |
17 | return; |
18 | } |
19 | if (!m_manager) { |
20 | qWarning(msg: "Can't start the NetworkQuery without the QNetworkAccessManager. " |
21 | "Stopping with an error." ); |
22 | emit done(result: DoneResult::Error); |
23 | return; |
24 | } |
25 | switch (m_operation) { |
26 | case NetworkOperation::Get: |
27 | m_reply.reset(p: m_manager->get(request: m_request)); |
28 | break; |
29 | case NetworkOperation::Put: |
30 | m_reply.reset(p: m_manager->put(request: m_request, data: m_writeData)); |
31 | break; |
32 | case NetworkOperation::Post: |
33 | m_reply.reset(p: m_manager->post(request: m_request, data: m_writeData)); |
34 | break; |
35 | case NetworkOperation::Delete: |
36 | m_reply.reset(p: m_manager->deleteResource(request: m_request)); |
37 | break; |
38 | } |
39 | connect(sender: m_reply.get(), signal: &QNetworkReply::finished, context: this, slot: [this] { |
40 | disconnect(sender: m_reply.get(), signal: &QNetworkReply::finished, receiver: this, zero: nullptr); |
41 | emit done(result: toDoneResult(success: m_reply->error() == QNetworkReply::NoError)); |
42 | m_reply.release()->deleteLater(); |
43 | }); |
44 | if (m_reply->isRunning()) |
45 | emit started(); |
46 | } |
47 | |
48 | NetworkQuery::~NetworkQuery() |
49 | { |
50 | if (m_reply) { |
51 | disconnect(sender: m_reply.get(), signal: &QNetworkReply::finished, receiver: this, zero: nullptr); |
52 | m_reply->abort(); |
53 | } |
54 | } |
55 | |
56 | } // namespace Tasking |
57 | |
58 | QT_END_NAMESPACE |
59 | |