annotate ui/downloader.cpp @ 46:d28e2624c1d5

Reset connection before the next request. This makes the downloader work for the first simple test.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Mar 2014 10:51:47 +0000
parents c6125d73faf4
children 09cd242d8443
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 DOWNLOAD_SERVER
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 #define DOWNLOAD_SERVER "https://www.intevation.de"
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 #include <QFile>
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
8 #include <QDir>
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 #include <QDebug>
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
10 #include <QStandardPaths>
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
11 #include <QTextStream>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
12 #include <QLocale>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
13 #include <QSaveFile>
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
14
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
15 #include <polarssl/net.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
16 #include <polarssl/ssl.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
17 #include <polarssl/entropy.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
18 #include <polarssl/ctr_drbg.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
19 #include <polarssl/error.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
20 #include <polarssl/certs.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
21
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
22 #define MAX_SW_SIZE 10485760
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
23 #define MAX_LIST_SIZE 1048576
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
24
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
26 Downloader::Downloader(QObject* parent, const QString& url,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
27 const QByteArray& certificate,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
28 const QDateTime& newestSW,
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
29 const QDateTime& newestList,
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
30 const QString& resourceSW,
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
31 const QString& resourceList):
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
32 QThread(parent),
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
33 mLastModSW(newestSW),
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
34 mLastModList(newestList),
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
35 mResourceSW(resourceSW),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
36 mResourceList(resourceList),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
37 mSSLConnection(url, certificate)
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 {
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
39 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
40
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
41
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
42 Downloader::~Downloader() {
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
45 QString Downloader::getDataDirectory()
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 {
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
47 QString candidate =
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
48 QStandardPaths::writableLocation(QStandardPaths::DataLocation);
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
49
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
50 if (candidate.isEmpty()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
51 qDebug() << "Could not find writeable locaction for me";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
52 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
53 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
54
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
55 QDir cDir(candidate);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
56
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
57 if (!cDir.exists()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
58 if (!cDir.mkpath(candidate)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
59 qDebug() << "Could not create path to: " << candidate;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
60 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
61 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
62 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
63 return cDir.absolutePath();
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
65
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
66
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
67 QDateTime Downloader::getLastModifiedHeader(const QString &resource) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
68 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
69 QByteArray response;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
70 QTextStream responseStream(&response);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
71 QLocale cLocale = QLocale::c();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
72 QString headRequest =
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
73 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
74
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
75 ret = mSSLConnection.write(headRequest.toUtf8());
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
76 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
77 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
78 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
79 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
80
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
81 response = mSSLConnection.read(1024);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
82
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
83 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
84 qDebug() << "No response";
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
85 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
86 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
87 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
88
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
89 while (1) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
90 QString line = responseStream.readLine();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
91 if (line.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
92 break;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
93 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
94 if (line.startsWith("Last-Modified:")) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
95 QDateTime candidate = cLocale.toDateTime(line, "'Last-Modified: 'ddd, dd MMM yyyy HH:mm:ss' GMT'");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
96 qDebug() << "Parsed line : " << line << " to " << candidate;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
97 if (candidate.isValid()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
98 return candidate;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
99 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
100 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
101 }
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
102 qDebug() << "Response: " << response;
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
103 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
104 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
105 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
106
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
107 bool Downloader::downloadFile(const QString &resource,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
108 const QString &fileName,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
109 size_t maxSize)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
110 {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
111 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
112 size_t bytesRead = 0;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
113 QString getRequest =
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
114 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
115
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
116 QSaveFile outputFile(fileName);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
117
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
118 ret = mSSLConnection.write(getRequest.toUtf8());
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
119
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
120 // Open / Create the file to write to.
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
121 if (!outputFile.open(QIODevice::WriteOnly)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
122 qDebug() << "Failed to open file";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
123 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
124 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
125
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
126 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
127 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
128 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
129 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
130
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
131 do {
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
132 /* Read the response in 8KiB chunks */
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
133 QByteArray response = mSSLConnection.read(8192);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
134 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
135 qDebug() << "Error reading response";
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
136 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
137 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
138 }
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
139 outputFile.write(response);
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
140 qDebug() << "Wrote: "<< response.size();
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
141 bytesRead += response.size();
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
142 if (response.size() < 8192) {
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
143 /* Nothing more to read for us */
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
144 break;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
145 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
146 } while (bytesRead < maxSize);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
147
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
148 return outputFile.commit();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
149 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
150
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
151 void Downloader::run() {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
152 int ret;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
153 QDateTime remoteModList;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
154 QDateTime remoteModSW;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
155
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
156 if (!mSSLConnection.initialized()) {
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
157 emit error(tr("Failed to initialize SSL Module."), SSLConnection::ErrUnknown);
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
158 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
159 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
160
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
161 ret = mSSLConnection.connect();
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
162
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
163 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
164 emit error(tr("Failed to connect."),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
165 mSSLConnection.getLastError());
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
166 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
167 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
168
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
169 emit progress(tr("Connected"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
170
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
171 remoteModSW = getLastModifiedHeader(mResourceSW);
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
172 remoteModList = getLastModifiedHeader(mResourceList);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
173
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
174 if (!remoteModSW.isValid() || !remoteModList.isValid()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
175 qDebug() << "Could not read headers";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
176 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
177 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
178
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
179 if (!mLastModSW.isValid() || remoteModSW > mLastModSW) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
180 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
181
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
182 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
183 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
184 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
185 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
186
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
187 QString fileName = dataDirectory.append("/SW-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
188 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
189 .append(".exe");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
190
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
191 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
192
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
193 if (!downloadFile(mResourceSW, fileName, MAX_SW_SIZE)) {
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
194 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
195 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
196
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
197 emit newSoftwareAvailable(fileName, remoteModSW);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
198 } else if (!mLastModList.isValid() || remoteModList > mLastModList) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
199 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
200
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
201 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
202 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
203 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
204 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
205
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
206 QString fileName = dataDirectory.append("/list-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
207 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
208 .append(".txt");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
209
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
210 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
211
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
212 if (!downloadFile(mResourceList, fileName, MAX_LIST_SIZE)) {
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
213 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
214 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
215
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
216 emit newListAvailable(fileName, remoteModList);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
217 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
218
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
219 emit progress(tr("Closing"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
220 }

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