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 | #ifndef TASKING_NETWORKQUERY_H |
6 | #define TASKING_NETWORKQUERY_H |
7 | |
8 | // |
9 | // W A R N I N G |
10 | // ------------- |
11 | // |
12 | // This file is not part of the Qt API. It exists purely as an |
13 | // implementation detail. This header file may change from version to |
14 | // version without notice, or even be removed. |
15 | // |
16 | // We mean it. |
17 | // |
18 | |
19 | #include "tasking_global.h" |
20 | |
21 | #include "tasktree.h" |
22 | |
23 | #include <QtNetwork/QNetworkReply> |
24 | #include <QtNetwork/QNetworkRequest> |
25 | |
26 | #include <memory> |
27 | |
28 | QT_BEGIN_NAMESPACE |
29 | class QNetworkAccessManager; |
30 | |
31 | namespace Tasking { |
32 | |
33 | // This class introduces the dependency to Qt::Network, otherwise Tasking namespace |
34 | // is independent on Qt::Network. |
35 | // Possibly, it could be placed inside Qt::Network library, as a wrapper around QNetworkReply. |
36 | |
37 | enum class NetworkOperation { Get, Put, Post, Delete }; |
38 | |
39 | class TASKING_EXPORT NetworkQuery final : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | public: |
44 | ~NetworkQuery(); |
45 | void setRequest(const QNetworkRequest &request) { m_request = request; } |
46 | void setOperation(NetworkOperation operation) { m_operation = operation; } |
47 | void setWriteData(const QByteArray &data) { m_writeData = data; } |
48 | void setNetworkAccessManager(QNetworkAccessManager *manager) { m_manager = manager; } |
49 | QNetworkReply *reply() const { return m_reply.get(); } |
50 | void start(); |
51 | |
52 | Q_SIGNALS: |
53 | void started(); |
54 | void done(DoneResult result); |
55 | |
56 | private: |
57 | QNetworkRequest m_request; |
58 | NetworkOperation m_operation = NetworkOperation::Get; |
59 | QByteArray m_writeData; // Used by Put and Post |
60 | QNetworkAccessManager *m_manager = nullptr; |
61 | std::unique_ptr<QNetworkReply> m_reply; |
62 | }; |
63 | |
64 | class TASKING_EXPORT NetworkQueryTaskAdapter : public TaskAdapter<NetworkQuery> |
65 | { |
66 | public: |
67 | NetworkQueryTaskAdapter() { connect(sender: task(), signal: &NetworkQuery::done, context: this, slot: &TaskInterface::done); } |
68 | void start() final { task()->start(); } |
69 | }; |
70 | |
71 | using NetworkQueryTask = CustomTask<NetworkQueryTaskAdapter>; |
72 | |
73 | } // namespace Tasking |
74 | |
75 | QT_END_NAMESPACE |
76 | |
77 | #endif // TASKING_NETWORKQUERY_H |
78 | |