NDEVR
API Documentation
ItemDownloader.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: ItemDownloader
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/SoftwareService.h>
35#include <NDEVR/BaseValues.h>
36#include <NDEVR/TimeSpan.h>
37#include <NDEVR/Dictionary.h>
38#include <NDEVR/String.h>
39#include <QObject>
40#include <QByteArray>
41class QNetworkRequest;
42class QNetworkReply;
43class QNetworkAccessManager;
44namespace NDEVR
45{
46 class ProgressInfo;
47 class ReplyTimeout;
48 class String;
52 class NDEVR_WIDGETS_API NetworkRequest : public QObject
53 {
54 Q_OBJECT
55 public:
57 virtual void close() = 0;
59 virtual void abort() = 0;
64 virtual bool isRunning() = 0;
69 virtual bool start() = 0;
74 virtual QByteArray data() = 0;
75 signals:
81 //void encryptedSignal();
82 //void sslErrorsSignal(const QList<QSslError>& errors);
83 //void preSharedKeyAuthenticationRequiredSignal(QSslPreSharedKeyAuthenticator* authenticator);
84 //void redirectedSignal(const QUrl& url);
86 void uploadProgressSignal(qint64 bytesSent, qint64 bytesTotal);
87 void downloadProgressSignal(qint64 bytesReceived, qint64 bytesTotal);
88 };
89
92 class NDEVR_WIDGETS_API TimeRequest : public QObject
93 {
94 Q_OBJECT
95 public:
100 virtual Time serverTime() const = 0;
105 virtual Time txTime() const = 0;
110 virtual Time rxTime() const = 0;
116 {
117 return rxTime() - txTime();
118 }
119
124 {
125 return ((serverTime() - rxTime()) - (serverTime() - txTime())) / 2.0;
126 }
127 signals:
129 void errorOccurredSignal(int error);
130 };
131
135 {
139 uint02 port = Constant<uint02>::Invalid;
140 };
141
144 class NDEVR_WIDGETS_API NetworkAccessManager : public SoftwareService
145 {
146 public:
152 virtual NetworkRequest* downloadFromURL(const WebTarget& url) = 0;
159 virtual NetworkRequest* uploadToURL(const WebTarget& request, const Buffer<uint01>& data) = 0;
166 virtual NetworkRequest* uploadToURL(const WebTarget& request, const File& data) = 0;
173 virtual TimeRequest* networkTimeFromURL(const StringView& url, uint02 port = 123) = 0;
174 };
175
178 class NDEVR_WIDGETS_API ItemDownloader : public QObject, public SoftwareServiceManager
179 {
180 Q_OBJECT
181 protected:
187 explicit ItemDownloader(const StringView& url, QObject* parent = nullptr);
193 explicit ItemDownloader(QUrl url, QObject* parent = nullptr);
194 virtual ~ItemDownloader();
195 public:
206 [[nodiscard]] const QByteArray& downloadedData() const;
211 [[nodiscard]] uint08 currentDownloadSize() const { return m_current_download_size; }
216 [[nodiscard]] uint08 totalDownloadSize() const { return m_total_download_size; }
221 [[nodiscard]] fltp08 downloadPercent() const;
236 static bool HasNetworkManager();
244 static bool DownloadToFile(const StringView& url, const File& location, ProgressInfo& log);
249 void start(TimeSpan timeout = TimeSpan(10.0));
250 signals:
254 protected:
260 private slots:
262 void fileDownloadedSlot();
264 void downloadedErrorSlot(StringView error);
266 void updateDownloadProgressSlot(qint64 ist, qint64 max);
267 private:
268 static NetworkAccessManager* s_web_ctrl;
269 static Dictionary<String, ItemDownloader*> s_current_downloads;
270 NetworkRequest* m_request;
271 ReplyTimeout* m_reply_timeout;
272 //QNetworkAccessManager* m_web_ctrl;
273 QByteArray m_data;
274 uint08 m_last_percent = 0U;
275 uint08 m_current_download_size;
276 uint08 m_total_download_size;
277 bool m_failed;
278 };
279
280}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
ItemDownloader(const StringView &url, QObject *parent=nullptr)
Constructs an ItemDownloader for the given URL string.
const QByteArray & downloadedData() const
Returns the downloaded data.
ItemDownloader(QUrl url, QObject *parent=nullptr)
Constructs an ItemDownloader for the given QUrl.
void start(TimeSpan timeout=TimeSpan(10.0))
Starts the download with an optional timeout.
uint08 currentDownloadSize() const
Returns the number of bytes downloaded so far.
static bool DownloadToFile(const StringView &url, const File &location, ProgressInfo &log)
Downloads a file from a URL and saves it to a local path.
static NetworkAccessManager * NetworkManager()
Returns the global NetworkAccessManager.
static bool HasNetworkManager()
Returns whether a NetworkAccessManager has been set.
void setNetworkRequest(const StringView &url)
Configures the internal network request for the given URL.
void downloadFailedSignal(StringView)
Emitted when a download fails, with an error message.
static ItemDownloader * GetItemDownloader(const StringView &url)
Returns an existing or new ItemDownloader for the given URL.
fltp08 downloadPercent() const
Returns the download progress as a percentage.
void percentChangedSignal()
Emitted when the download progress percentage changes.
static void SetAccessManager(NetworkAccessManager *manager)
Sets the global NetworkAccessManager used for all downloads.
void downloadedSignal()
Emitted when the download completes successfully.
uint08 totalDownloadSize() const
Returns the total expected download size.
Provides an interface for accessing resources on the internet.
virtual NetworkRequest * downloadFromURL(const WebTarget &url)=0
Creates a network request to download data from the given URL.
virtual NetworkRequest * uploadToURL(const WebTarget &request, const Buffer< uint01 > &data)=0
Creates a network request to upload raw data to the given URL.
virtual TimeRequest * networkTimeFromURL(const StringView &url, uint02 port=123)=0
Creates a time request to query network time from the given URL.
virtual NetworkRequest * uploadToURL(const WebTarget &request, const File &data)=0
Creates a network request to upload a file to the given URL.
A request for data or information from a network.
virtual void close()=0
Closes the network request and releases resources.
void requestSentSignal()
Emitted when the request has been sent.
void metaDataChangedSignal()
Emitted when response metadata changes.
void errorOccurredSignal(StringView error)
Emitted when an error occurs, with a description.
virtual bool isRunning()=0
Returns whether the request is currently in progress.
void uploadProgressSignal(qint64 bytesSent, qint64 bytesTotal)
Emitted to report upload progress.
void finishedSignal()
Emitted when the request has finished.
virtual QByteArray data()=0
Returns the data received from the network request.
virtual bool start()=0
Starts the network request.
void socketStartedConnectingSignal()
Emitted when the socket begins connecting.
virtual void abort()=0
Aborts the network request immediately.
void downloadProgressSignal(qint64 bytesReceived, qint64 bytesTotal)
Emitted to report download progress.
void redirectAllowedSignal()
Emitted when a redirect is allowed.
Used with InfoPipe to signal that the system will be using progress.
Handles timing out certain operations such as downloads.
Software Service Managers take a Software service to modify the behavior of the software.
Base interface for services that extend or modify software behavior through modules.
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
A request to get the time from the network.
virtual Time txTime() const =0
Returns the time the request was transmitted.
virtual Time serverTime() const =0
Returns the time reported by the server.
void finishedSignal()
Emitted when the time request has finished.
TimeSpan localClockOffset() const
Calculates the offset between the local clock and the server clock.
void errorOccurredSignal(int error)
Emitted when an error occurs, with the error code.
TimeSpan roundTripDelay() const
Calculates the round-trip delay of the time request.
virtual Time rxTime() const =0
Returns the time the response was received.
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
The primary namespace for the NDEVR SDK.
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
double fltp08
Defines an alias representing an 8 byte floating-point number.
A url target and optional username and password for a network request.
String password
Optional password for authentication.
String username
Optional username for authentication.
String url
The target URL.
uint02 port
Optional port number, or Invalid for default.