Mercurial > trustbridge
comparison ui/downloader.h @ 15:95e1b6edf2fc
Implement more downloader functionality for Windows
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 19 Feb 2014 10:45:06 +0000 |
parents | fe39d93f1261 |
children | 62cd56cea09b |
comparison
equal
deleted
inserted
replaced
14:58d51a91d448 | 15:95e1b6edf2fc |
---|---|
7 */ | 7 */ |
8 | 8 |
9 #include <QThread> | 9 #include <QThread> |
10 #include <QString> | 10 #include <QString> |
11 #include <QByteArray> | 11 #include <QByteArray> |
12 #include <QDateTime> | |
13 | |
14 #ifdef Q_OS_WIN | |
15 #include <windows.h> | |
16 #include <winhttp.h> | |
17 #endif | |
18 | |
12 | 19 |
13 class Downloader: public QThread | 20 class Downloader: public QThread |
14 { | 21 { |
15 Q_OBJECT | 22 Q_OBJECT |
16 | 23 |
17 public: | 24 public: |
18 /** | 25 /** |
19 * @brief Construct a downloader to download data from url | 26 * @brief Construct a downloader with a specific certificate |
20 * | 27 * |
21 * Takes the builtin certificate for https verification. | 28 * The downloader will check the last-modified date of the |
29 * certificate list / sw on the server at the specified url | |
30 * and download those accordingly. If newestSW or newestList is | |
31 * are valid datetimes only files modified after the respective date | |
32 * are downloaded. | |
22 * | 33 * |
23 * @param[in] parent the parent object. | 34 * Downloaded files are placed in QStandardPaths::DataLocation |
24 * @param[in] url the Url to download data from. | |
25 */ | |
26 Downloader(QObject* parent, const QString& url); | |
27 | |
28 /** | |
29 * @brief Construct a downloader with a specific certificate | |
30 * | 35 * |
31 * @param[in] parent the parent object. | 36 * @param[in] parent the parent object. |
32 * @param[in] url the Url to download data from | 37 * @param[in] url the Url to download data from |
33 * @param[in] certificate to accept https connection from | 38 * @param[in] certificate optional certificate to validate https connection |
39 * @param[in] newestSW datetime after which software should be downloaded | |
40 * @param[in] newestList datetime after which the list should be downloaded | |
34 */ | 41 */ |
35 Downloader(QObject* parent, const QString& url, | 42 Downloader(QObject* parent, const QString& url, |
36 const QByteArray& certificate); | 43 const QByteArray& certificate = QByteArray(), |
44 const QDateTime& newestSW = QDateTime(), | |
45 const QDateTime& newestList = QDateTime()); | |
37 | 46 |
38 enum Status { | |
39 NewSoftwareAvailable, // A Software Update has been downloaded | |
40 NewListAvailable, // A certificate list has been downloaded | |
41 UpToDate, // Nothing changed | |
42 Error // An error happened | |
43 }; | |
44 | 47 |
45 enum ErrorCode { | 48 enum ErrorCode { |
46 NoConnection, | 49 NoConnection, |
47 InvalidCertificate, | 50 InvalidCertificate, |
48 ConnectionLost, | 51 ConnectionLost, |
49 Timeout, | 52 Timeout, |
50 Unknown | 53 ErrUnknown |
51 }; | 54 }; |
52 | 55 |
53 /** | 56 /** |
54 * @brief Construct a downloader with a specific certificate | 57 * @brief get the directory where the downloader saves data |
55 * | 58 * |
56 * @param[in] url the Url to download data from | 59 * If the directory does not exist this function ensures that it |
57 * @param[in] certificate to accept https connection from | 60 * is created. |
58 */ | 61 * |
59 QString getDownloadedFileName(); | 62 * @returns The directory in which downloaded files are placed. |
63 **/ | |
64 QString getDataDirectory(); | |
65 | |
60 | 66 |
61 protected: | 67 protected: |
62 void run(); | 68 void run(); |
63 | 69 |
64 private: | 70 private: |
65 QString mUrl; | 71 QString mUrl; |
66 QByteArray mCert; | 72 QByteArray mCert; |
67 | 73 |
74 QDateTime mLastModSW; | |
75 QDateTime mLastModList; | |
76 | |
77 #ifdef Q_OS_WIN | |
78 /** @brief Download a file from the Internet | |
79 * | |
80 * @param[in] HSession the session to work in. | |
81 * @param[in] HConnect the connection to use. | |
82 * @param[in] resource the resource to download. | |
83 * @param[in] filename where the file should be saved. | |
84 * @param[in] maxSize maximum amount of bytes to download | |
85 * | |
86 * @returns True if the download was successful. | |
87 */ | |
88 bool downloadFile(HINTERNET hSession, HINTERNET hConnect, | |
89 LPCWSTR resource, const QString &filename, DWORD maxSize); | |
90 | |
91 /** @brief get the last modified header of a resource. | |
92 * | |
93 * On error call getLastError to get extended error information. | |
94 * This function still does not do any networking but only initializes | |
95 * it. | |
96 * | |
97 * @param[in] HSession the session to work in. | |
98 * @param[in] HConnect the connection to use. | |
99 * @param[in] resource the resource to check the last-modified date on | |
100 * | |
101 * @returns the last modified date or a null datetime in case of errors | |
102 */ | |
103 QDateTime getLastModifiedHeader(HINTERNET hSession, | |
104 HINTERNET hConnect, LPCWSTR resource); | |
105 | |
106 /** @brief verify that the certificate of the request matches | |
107 * | |
108 * Validates the certificate against the member variable certificate | |
109 * | |
110 * @param[in] hRequest: The request from which to get the certificate | |
111 * | |
112 * @returns True if the certificate exactly matches the one in hRequest | |
113 */ | |
114 | |
115 bool verifyCertificate(HINTERNET hRequest); | |
116 #endif | |
117 | |
68 Q_SIGNALS: | 118 Q_SIGNALS: |
119 /** | |
120 * @brief software update is available | |
121 */ | |
122 void newSoftwareAvailable(const QString &fileName, | |
123 const QDateTime &lastMod); | |
124 | |
125 /** | |
126 * @brief new certificate list available | |
127 */ | |
128 void newListAvailable(const QString &fileName, const QDateTime &lastMod); | |
129 | |
69 /** | 130 /** |
70 * @brief Some progress has been made. | 131 * @brief Some progress has been made. |
71 * | 132 * |
72 * @param[out] message: A message to show. Can be empty. | 133 * @param[out] message: A message to show. Can be empty. |
73 * @param[out] current: Value of current progress. | 134 * @param[out] current: Value of current progress. |
78 /** | 139 /** |
79 * @brief An error happened | 140 * @brief An error happened |
80 * | 141 * |
81 * @param[out] message: A message to show. Can be empty. | 142 * @param[out] message: A message to show. Can be empty. |
82 * @param[out] errorCode: ErrorCode of this error. | 143 * @param[out] errorCode: ErrorCode of this error. |
83 * @param[out] total: Total value of possible progress. | |
84 */ | 144 */ |
85 void error(const QString &message, ErrorCode error); | 145 void error(const QString &message, ErrorCode error); |
86 }; | 146 }; |
87 #endif | 147 #endif |