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/09
18  * - added sgl namespace
19  * @version 2021/04/03
20  * - removed dependency on custom collections
21  * @version 2018/09/18
22  * - working version; had to fix various threading / Qt signal issues
23  * @version 2018/09/07
24  * - added doc comments for new documentation generation
25  * @version 2018/08/23
26  * - renamed to gdownloader.h to replace Java version
27  * @version 2018/08/03
28  * - initial version
29  */
30 
31 #ifndef _gdownloader_h
32 #define _gdownloader_h
33 
34 #include <map>
35 #include <string>
36 #include <QNetworkAccessManager>
37 #include <QNetworkReply>
38 
39 namespace sgl {
40 
45 class GDownloader : public QObject {
46  Q_OBJECT
47 
48 public:
52  GDownloader();
53 
57  virtual ~GDownloader();
58 
63  string downloadAsString(const string& url);
64 
69  void downloadToFile(const string& url, const string& file);
70 
75  string getErrorMessage() const;
76 
81  string getHeader(const string& name) const;
82 
88  int getHttpStatusCode() const;
89 
94  string getUserAgent() const;
95 
100  bool hasError() const;
101 
106  void httpGet(const string& url);
107 
112  void httpPost(const string& url);
113 
121  void setHeader(const string& name, const string& value);
122 
131  void setUserAgent(const string& userAgent);
132 
133 signals:
137  void downloaded();
138 
139 private slots:
140  void downloadInternal();
141  void fileDownloadError(QNetworkReply::NetworkError);
142  void saveDownloadedData(const string& member, const string& filename = "");
143  void sslErrors(QList<QSslError>);
144  void waitForDownload();
145 
146 private:
147  Q_DISABLE_COPY(GDownloader)
148 
149  static string qtNetworkErrorToString(QNetworkReply::NetworkError nerror);
150 
151  QNetworkAccessManager* _manager;
152  QNetworkReply* _reply;
153  std::map<string, string> _headers; // HTTP headers to send (name => value)
154  int _httpStatusCode;
155  bool _downloadComplete;
156  string _url;
157  string _filename;
158  string _filedata;
159  string _lastErrorMessage;
160 };
161 
162 } // namespace sgl
163 
164 #endif // _gdownloader_h
int getHttpStatusCode() const
Returns the most recent HTTP status code, which may be a successful code (e.g.
Definition: gdownloader.cpp:130
A GDownloader can download files and data over an internet connection.
Definition: gdownloader.h:45
virtual ~GDownloader()
Frees memory allocated internally by the downloader.
Definition: gdownloader.cpp:42
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:143
Definition: console.h:45
bool hasError() const
Returns true if the HTTP connection failed and had an error.
Definition: gdownloader.cpp:147
void downloadToFile(string url, string file)
Downloads the text contents of the given URL, saving it to the given output file. ...
Definition: gdownloader.cpp:64
void setUserAgent(string userAgent)
Definition: gdownloader.cpp:232
void httpPost(string url)
Performs an HTTP POST request to the given URL, submitting any headers and query parameters previousl...
string getHeader(string name) const
Returns the value of the given HTTP header for this URL request.
Definition: gdownloader.cpp:135
string getErrorMessage() const
Returns the last HTTP error message that occurred.
Definition: gdownloader.cpp:126
GDownloader()
Creates a new downloader.
Definition: gdownloader.cpp:35
string downloadAsString(string url)
Downloads the text contents of the given URL, returning them as a string.
Definition: gdownloader.cpp:48
void httpGet(string url)
Performs an HTTP GET request to the given URL.
void setHeader(string name, string value)
Definition: gdownloader.cpp:228
void downloaded()
This Qt signal fires when the data is done downloading.