SGL
gdownloader.h
1 /*
2  * File: gdownloader.h
3  * -------------------
4  * A GDownloader can download data from URLs and save them to files or return
5  * the data as a string.
6  *
7  * Note that because the downloader uses a pure-C++ implementation, your project
8  * must include the 'network' component of Qt to function properly.
9  * If you get errors when trying to connect to HTTPS URLs, you may also need to
10  * install various SSL packages on your system, such as openssl, libssl-dev,
11  * libssl1.0, and so on. This varies by operating system.
12  *
13  * Based somewhat on this source:
14  * https://wiki.qt.io/Download_Data_from_URL
15  *
16  * @author Marty Stepp
17  * @version 2021/04/03
18  * - removed dependency on custom collections
19  * @version 2018/09/18
20  * - working version; had to fix various threading / Qt signal issues
21  * @version 2018/09/07
22  * - added doc comments for new documentation generation
23  * @version 2018/08/23
24  * - renamed to gdownloader.h to replace Java version
25  * @version 2018/08/03
26  * - initial version
27  */
28 
29 
30 #ifndef _gdownloader_h
31 #define _gdownloader_h
32 
33 #include <map>
34 #include <string>
35 #include <QNetworkAccessManager>
36 #include <QNetworkReply>
37 
42 class GDownloader : public QObject {
43  Q_OBJECT
44 
45 public:
49  GDownloader();
50 
54  virtual ~GDownloader();
55 
60  string downloadAsString(const string& url);
61 
66  void downloadToFile(const string& url, const string& file);
67 
72  string getErrorMessage() const;
73 
78  string getHeader(const string& name) const;
79 
85  int getHttpStatusCode() const;
86 
91  string getUserAgent() const;
92 
97  bool hasError() const;
98 
103  void httpGet(const string& url);
104 
109  void httpPost(const string& url);
110 
118  void setHeader(const string& name, const string& value);
119 
128  void setUserAgent(const string& userAgent);
129 
130 signals:
134  void downloaded();
135 
136 private slots:
137  void downloadInternal();
138  void fileDownloadError(QNetworkReply::NetworkError);
139  void saveDownloadedData(const string& member, const string& filename = "");
140  void sslErrors(QList<QSslError>);
141  void waitForDownload();
142 
143 private:
144  Q_DISABLE_COPY(GDownloader)
145 
146  static string qtNetworkErrorToString(QNetworkReply::NetworkError nerror);
147 
148  QNetworkAccessManager* _manager;
149  QNetworkReply* _reply;
150  std::map<string, string> _headers; // HTTP headers to send (name => value)
151  int _httpStatusCode;
152  bool _downloadComplete;
153  string _url;
154  string _filename;
155  string _filedata;
156  string _lastErrorMessage;
157 };
158 
159 #endif // _gdownloader_h
virtual ~GDownloader()
Frees memory allocated internally by the downloader.
Definition: gdownloader.cpp:38
A GDownloader can download files and data over an internet connection.
Definition: gdownloader.h:42
string downloadAsString(string url)
Downloads the text contents of the given URL, returning them as a string.
Definition: gdownloader.cpp:44
string getUserAgent() const
Returns the value of the HTTP "User-Agent" header for this URL request, or an empty string if the use...
Definition: gdownloader.cpp:139
void setHeader(string name, string value)
Definition: gdownloader.cpp:224
void httpGet(string url)
Performs an HTTP GET request to the given URL.
string getHeader(string name) const
Returns the value of the given HTTP header for this URL request.
Definition: gdownloader.cpp:131
string getErrorMessage() const
Returns the last HTTP error message that occurred.
Definition: gdownloader.cpp:122
void downloaded()
This Qt signal fires when the data is done downloading.
GDownloader()
Creates a new downloader.
Definition: gdownloader.cpp:31
int getHttpStatusCode() const
Returns the most recent HTTP status code, which may be a successful code (e.g.
Definition: gdownloader.cpp:126
bool hasError() const
Returns true if the HTTP connection failed and had an error.
Definition: gdownloader.cpp:143
void httpPost(string url)
Performs an HTTP POST request to the given URL, submitting any headers and query parameters previousl...
void downloadToFile(string url, string file)
Downloads the text contents of the given URL, saving it to the given output file. ...
Definition: gdownloader.cpp:60
void setUserAgent(string userAgent)
Definition: gdownloader.cpp:228