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

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