API Documentation
Loading...
Searching...
No Matches
QTNetworkRequest.h
Go to the documentation of this file.
1#pragma once
2#include <NDEVR/NtpClient.h>
3#include <NDEVR/ItemDownloader.h>
4#include <NDEVR/String.h>
5#include <NDEVR/File.h>
6#include <NDEVR/Thread.h>
7#include <QNetworkRequest>
8#include <QNetworkAccessManager>
9#include <QNetworkReply>
10#include <QFile>
11namespace NDEVR
12{
13 static uint02 ntp_rx_port = 42912;
15 {
16 Q_OBJECT
17 public:
18 QtNetworkRequest(const QNetworkRequest& request, QNetworkAccessManager* manager)
20 , m_request(request)
21 , m_manager(manager)
22 {
23
24 }
26 {
27 m_data = data;
28 }
29 void setData(const File& data)
30 {
31 m_file = data;
32 }
33 virtual void close() override
34 {
35 if (m_reply)
36 m_reply->close();
37 }
38 virtual void abort() override
39 {
40 if (m_reply)
41 m_reply->abort();
42 }
43 void setReply(QNetworkReply* reply)
44 {
45 m_reply = reply;
46 //m_reply->setParent(this);
47 #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
48 connect(m_reply, &QNetworkReply::socketStartedConnecting, this
49 , [this] {emit socketStartedConnectingSignal(); });
50 connect(m_reply, &QNetworkReply::requestSent, this
51 , [this] {emit requestSentSignal(); });
52 connect(m_reply, &QNetworkReply::errorOccurred, this
53 , [this](QNetworkReply::NetworkError error)
54 {
56 });
57 #else
58 connect(m_reply, &QNetworkReply::error, this
59 , [this](QNetworkReply::NetworkError error) {emit errorOccurredSignal(cast<uint04>(error)); });
60 #endif
61 connect(m_reply, &QNetworkReply::metaDataChanged, this
62 , [this] {emit metaDataChangedSignal(); });
63 connect(m_reply, &QNetworkReply::finished, this
64 , [this] {emit finishedSignal(); });
65 connect(m_reply, &QNetworkReply::redirectAllowed, this
66 , [this] {emit redirectAllowedSignal(); });
67 connect(m_reply, &QNetworkReply::uploadProgress, this
68 , [this](qint64 bytesSent, qint64 bytesTotal) {emit uploadProgressSignal(bytesSent, bytesTotal); });
69 connect(m_reply, &QNetworkReply::downloadProgress, this
70 , [this](qint64 bytesSent, qint64 bytesTotal) {emit downloadProgressSignal(bytesSent, bytesTotal); });
71 if (m_reply->error() != QNetworkReply::NoError)
72 {
74 }
75 if (m_reply->isFinished())
76 {
77 emit finishedSignal();
78 }
79
80 }
81 virtual bool isRunning() override
82 {
83 if (m_reply)
84 return m_reply->isRunning();
85 else
86 return false;
87 }
88 virtual QByteArray data() override
89 {
90 if (m_reply)
91 return m_reply->readAll();
92 else
93 return QByteArray();
94 };
95 virtual bool start() override
96 {
97 if (m_data.size() > 0)
98 {
99 QByteArray qdata(rcast<char*>(m_data.begin()), m_data.size());
100 setReply(m_manager->put(m_request, qdata));
101 }
102 else if (m_file.size() > 0)
103 {
105 QFile qfile(m_file.getAs<QString>());
106 qfile.open(QIODeviceBase::ReadOnly);
107 setReply(m_manager->put(m_request, &qfile));
108 qfile.close();
109 }
110 else
111 {
113 }
114 return true;
115 }
116 QNetworkRequest m_request;
117 QNetworkReply* m_reply = nullptr;
118 QNetworkAccessManager* m_manager;
121 };
123 {
124 public:
125
126 QNetworkRequest ConvertToRequest(const WebTarget& target)
127 {
128 QUrl urlup(target.url.getAs<QString>());
129 if (target.password.size() > 0)
130 urlup.setPassword(target.password.getAs<QString>());
131 if(target.username.size() > 0)
132 urlup.setUserName(target.username.getAs<QString>());
133 if(!isNaN(target.port))
134 urlup.setPort(cast<int>(target.port));
135 return QNetworkRequest(urlup);
136 }
137 virtual NetworkRequest* downloadFromURL(const WebTarget& url) override
138 {
139 lib_assert(ThreadFunctions::IsPrimaryThread(), "Cannot download from non-main thread");
140 if (m_manager == nullptr)
141 m_manager = new QNetworkAccessManager();
143 }
144 virtual NetworkRequest* uploadToURL(const WebTarget& request, const Buffer<uint01>& data) override
145 {
146 if (m_manager == nullptr)
147 m_manager = new QNetworkAccessManager();
149 qtrequest->setData(data);
150 return qtrequest;
151 }
152 NetworkRequest* uploadToURL(const WebTarget& request, const File& data) override
153 {
154 if (m_manager == nullptr)
155 m_manager = new QNetworkAccessManager();
157 qtrequest->setData(data);
158 return qtrequest;
159 }
160 virtual TimeRequest* networkTimeFromURL(const String& url, uint02 port) override
161 {
162 NtpClient* client = new NtpClient(url, ntp_rx_port, port);
163 return client;
164 }
165 protected:
166 QNetworkAccessManager* m_manager = nullptr;
167 };
168}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
decltype(auto) begin()
Definition Buffer.hpp:504
Definition File.h:47
NDEVR_BASE_API void throwIfNotExist() const
Definition File.cpp:794
Definition ItemDownloader.h:96
Definition ItemDownloader.h:47
void errorOccurredSignal(int error)
void downloadProgressSignal(qint64 bytesReceived, qint64 bytesTotal)
void uploadProgressSignal(qint64 bytesSent, qint64 bytesTotal)
void socketStartedConnectingSignal()
Definition NtpClient.h:23
Definition QTNetworkRequest.h:123
NetworkRequest * uploadToURL(const WebTarget &request, const File &data) override
Definition QTNetworkRequest.h:152
virtual TimeRequest * networkTimeFromURL(const String &url, uint02 port) override
Definition QTNetworkRequest.h:160
QNetworkAccessManager * m_manager
Definition QTNetworkRequest.h:166
QNetworkRequest ConvertToRequest(const WebTarget &target)
Definition QTNetworkRequest.h:126
virtual NetworkRequest * downloadFromURL(const WebTarget &url) override
Definition QTNetworkRequest.h:137
virtual NetworkRequest * uploadToURL(const WebTarget &request, const Buffer< uint01 > &data) override
Definition QTNetworkRequest.h:144
Definition QTNetworkRequest.h:15
QtNetworkRequest(const QNetworkRequest &request, QNetworkAccessManager *manager)
Definition QTNetworkRequest.h:18
void setData(const File &data)
Definition QTNetworkRequest.h:29
QNetworkAccessManager * m_manager
Definition QTNetworkRequest.h:118
virtual void close() override
Definition QTNetworkRequest.h:33
void setReply(QNetworkReply *reply)
Definition QTNetworkRequest.h:43
virtual bool isRunning() override
Definition QTNetworkRequest.h:81
virtual QByteArray data() override
Definition QTNetworkRequest.h:88
QNetworkRequest m_request
Definition QTNetworkRequest.h:116
Buffer< uint01 > m_data
Definition QTNetworkRequest.h:119
virtual void abort() override
Definition QTNetworkRequest.h:38
QNetworkReply * m_reply
Definition QTNetworkRequest.h:117
File m_file
Definition QTNetworkRequest.h:120
void setData(const Buffer< uint01 > &data)
Definition QTNetworkRequest.h:25
virtual bool start() override
Definition QTNetworkRequest.h:95
Definition String.h:40
t_type getAs() const
Definition String.h:334
static bool IsPrimaryThread()
Definition Thread.cpp:102
Definition ItemDownloader.h:70
Definition ACIColor.h:37
constexpr t_to rcast(t_from value)
Definition BaseValues.hpp:427
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:109
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
Definition ItemDownloader.h:89
uint02 port
Definition ItemDownloader.h:93
String url
Definition ItemDownloader.h:90
String password
Definition ItemDownloader.h:92
String username
Definition ItemDownloader.h:91