annotate ui/downloader.cpp @ 10:fe39d93f1261

Start on Downloader component
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 13 Feb 2014 14:43:15 +0000
parents
children 7e2f14c7aba2
rev   line source
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 #include "downloader.h"
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
3 #ifndef MYVERSION
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 #define MYVERSION "1"
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 #endif
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
7 #ifndef DOWNLOAD_SERVER
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 #define DOWNLOAD_SERVER "https://www.intevation.de"
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 #endif
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11 #ifdef Q_OS_WIN
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
12 #include <windows.h>
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
13 #include <winhttp.h>
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14 #endif
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 #include <QFile>
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 #include <QDebug>
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19 Downloader::Downloader(QObject* parent, const QString& url)
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
20 {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
21 Downloader (parent, url, QFile(":Certificates/https").readAll());
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
22 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
23
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
24 Downloader::Downloader(QObject* parent, const QString& url,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25 const QByteArray& certificate) :
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
26 mUrl(url),
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
27 mCert(certificate),
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
28 QThread(parent)
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29 {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
31
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
32 void Downloader::run() {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33 #ifdef Q_OS_WIN
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 // We use WinAPI here instead of Qt because we want to avoid
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 // QtNetworks SSL stack which is based on OpenSSL and so
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 // we might be incompatible with GPL code. Also using the
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37 // native API means that the security of the SSL implementation
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 // is tied to the security of the system.
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39 BOOL bResults = FALSE;
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 HINTERNET hSession = NULL,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 hConnect = NULL,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 hRequest = NULL;
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 SYSTEMTIME lastModified;
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 DWORD sizeOfSystemtime = sizeof (SYSTEMTIME);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 // Get a syncronous session handle
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 hSession = WinHttpOpen(L"M13 "MYVERSION,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
48 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 WINHTTP_NO_PROXY_NAME,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50 WINHTTP_NO_PROXY_BYPASS, 0);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52 if (hSession) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53 // Initialize connection. No request is done here.
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54 hConnect = WinHttpConnect(hSession, L""DOWNLOAD_SERVER,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 INTERNET_DEFAULT_HTTPS_PORT, 0);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59 if (hConnect) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 // Make a head request
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61 hRequest = WinHttpOpenRequest(hConnect, L"HEAD",
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 L"/index.html",
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63 NULL, WINHTTP_NO_REFERER,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 WINHTTP_DEFAULT_ACCEPT_TYPES,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65 0);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 if (hRequest) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 bResults = WinHttpSendRequest(hRequest,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 WINHTTP_NO_ADDITIONAL_HEADERS,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 0, WINHTTP_NO_REQUEST_DATA, 0,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 0, 0);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75 if (bResults) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 bResults = WinHttpReceiveResponse(hRequest, NULL);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 if (bResults) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82 bResults = WinHttpQueryHeaders(hRequest,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
83 WINHTTP_QUERY_LAST_MODIFIED |
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84 WINHTTP_QUERY_FLAG_SYSTEMTIME,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
85 NULL,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
86 &lastModified,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
87 &sizeOfSystemtime,
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88 WINHTTP_NO_HEADER_INDEX );
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
89 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
90
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
91 qDebug() << "Last modified year: " << lastModified.wYear;
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
93
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
94 if (!bResults) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
95 // Report any errors.
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96 qDebug() << "Error" << GetLastError();
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
97 emit error(tr("Unknown Problem when connecting"), Unknown);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
98 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
99
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100 // Cleanup
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
101 if (hRequest) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
102 WinHttpCloseHandle(hRequest);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
104
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
105 if (hConnect) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
106 WinHttpCloseHandle(hConnect);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
107
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
108 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
109
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
110 if (hSession) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
111 WinHttpCloseHandle(hSession);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113 #endif
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
115 for (int i=0; i< 10; i++) {
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
116 qDebug("Going to sleep\n");
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
117 sleep(10);
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
119 }

http://wald.intevation.org/projects/trustbridge/