68 buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole); |
68 buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole); |
69 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
69 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
70 |
70 |
71 progressDialog = new QProgressDialog(this); |
71 progressDialog = new QProgressDialog(this); |
72 |
72 |
73 http = new QHttp(this); |
|
74 |
|
75 connect(urlLineEdit, SIGNAL(textChanged(QString)), |
73 connect(urlLineEdit, SIGNAL(textChanged(QString)), |
76 this, SLOT(enableDownloadButton())); |
74 this, SLOT(enableDownloadButton())); |
77 connect(http, SIGNAL(requestFinished(int,bool)), |
75 |
78 this, SLOT(httpRequestFinished(int,bool))); |
76 connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
79 connect(http, SIGNAL(dataReadProgress(int,int)), |
77 this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*))); |
80 this, SLOT(updateDataReadProgress(int,int))); |
|
81 connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), |
|
82 this, SLOT(readResponseHeader(QHttpResponseHeader))); |
|
83 connect(http, SIGNAL(authenticationRequired(QString,quint16,QAuthenticator*)), |
|
84 this, SLOT(slotAuthenticationRequired(QString,quint16,QAuthenticator*))); |
|
85 #ifndef QT_NO_OPENSSL |
78 #ifndef QT_NO_OPENSSL |
86 connect(http, SIGNAL(sslErrors(QList<QSslError>)), |
79 connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), |
87 this, SLOT(sslErrors(QList<QSslError>))); |
80 this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); |
88 #endif |
81 #endif |
89 connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); |
82 connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); |
90 connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); |
83 connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); |
91 connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
84 connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
92 |
85 |
102 |
95 |
103 setWindowTitle(tr("HTTP")); |
96 setWindowTitle(tr("HTTP")); |
104 urlLineEdit->setFocus(); |
97 urlLineEdit->setFocus(); |
105 } |
98 } |
106 |
99 |
|
100 void HttpWindow::startRequest(QUrl url) |
|
101 { |
|
102 reply = qnam.get(QNetworkRequest(url)); |
|
103 connect(reply, SIGNAL(finished()), |
|
104 this, SLOT(httpFinished())); |
|
105 connect(reply, SIGNAL(readyRead()), |
|
106 this, SLOT(httpReadyRead())); |
|
107 connect(reply, SIGNAL(downloadProgress(qint64,qint64)), |
|
108 this, SLOT(updateDataReadProgress(qint64,qint64))); |
|
109 } |
|
110 |
107 void HttpWindow::downloadFile() |
111 void HttpWindow::downloadFile() |
108 { |
112 { |
109 QUrl url(urlLineEdit->text()); |
113 url = urlLineEdit->text(); |
|
114 |
110 QFileInfo fileInfo(url.path()); |
115 QFileInfo fileInfo(url.path()); |
111 QString fileName = fileInfo.fileName(); |
116 QString fileName = fileInfo.fileName(); |
112 if (fileName.isEmpty()) |
117 if (fileName.isEmpty()) |
113 fileName = "index.html"; |
118 fileName = "index.html"; |
114 |
119 |
130 delete file; |
135 delete file; |
131 file = 0; |
136 file = 0; |
132 return; |
137 return; |
133 } |
138 } |
134 |
139 |
135 QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; |
|
136 http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port()); |
|
137 |
|
138 if (!url.userName().isEmpty()) |
|
139 http->setUser(url.userName(), url.password()); |
|
140 |
|
141 httpRequestAborted = false; |
|
142 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/"); |
|
143 if (path.isEmpty()) |
|
144 path = "/"; |
|
145 httpGetId = http->get(path, file); |
|
146 |
140 |
147 progressDialog->setWindowTitle(tr("HTTP")); |
141 progressDialog->setWindowTitle(tr("HTTP")); |
148 progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); |
142 progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); |
149 downloadButton->setEnabled(false); |
143 downloadButton->setEnabled(false); |
|
144 |
|
145 // schedule the request |
|
146 httpRequestAborted = false; |
|
147 startRequest(url); |
150 } |
148 } |
151 |
149 |
152 void HttpWindow::cancelDownload() |
150 void HttpWindow::cancelDownload() |
153 { |
151 { |
154 statusLabel->setText(tr("Download canceled.")); |
152 statusLabel->setText(tr("Download canceled.")); |
155 httpRequestAborted = true; |
153 httpRequestAborted = true; |
156 http->abort(); |
154 reply->abort(); |
157 downloadButton->setEnabled(true); |
155 downloadButton->setEnabled(true); |
158 } |
156 } |
159 |
157 |
160 void HttpWindow::httpRequestFinished(int requestId, bool error) |
158 void HttpWindow::httpFinished() |
161 { |
159 { |
162 if (requestId != httpGetId) |
|
163 return; |
|
164 if (httpRequestAborted) { |
160 if (httpRequestAborted) { |
165 if (file) { |
161 if (file) { |
166 file->close(); |
162 file->close(); |
167 file->remove(); |
163 file->remove(); |
168 delete file; |
164 delete file; |
169 file = 0; |
165 file = 0; |
170 } |
166 } |
171 |
167 reply->deleteLater(); |
172 progressDialog->hide(); |
168 progressDialog->hide(); |
173 return; |
169 return; |
174 } |
170 } |
175 |
171 |
176 if (requestId != httpGetId) |
|
177 return; |
|
178 |
|
179 progressDialog->hide(); |
172 progressDialog->hide(); |
|
173 file->flush(); |
180 file->close(); |
174 file->close(); |
181 |
175 |
182 if (error) { |
176 |
|
177 QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); |
|
178 if (reply->error()) { |
183 file->remove(); |
179 file->remove(); |
184 QMessageBox::information(this, tr("HTTP"), |
180 QMessageBox::information(this, tr("HTTP"), |
185 tr("Download failed: %1.") |
181 tr("Download failed: %1.") |
186 .arg(http->errorString())); |
182 .arg(reply->errorString())); |
|
183 downloadButton->setEnabled(true); |
|
184 } else if (!redirectionTarget.isNull()) { |
|
185 QUrl newUrl = url.resolved(redirectionTarget.toUrl()); |
|
186 if (QMessageBox::question(this, tr("HTTP"), |
|
187 tr("Redirect to %1 ?").arg(newUrl.toString()), |
|
188 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { |
|
189 url = newUrl; |
|
190 reply->deleteLater(); |
|
191 file->open(QIODevice::WriteOnly); |
|
192 file->resize(0); |
|
193 startRequest(url); |
|
194 return; |
|
195 } |
187 } else { |
196 } else { |
188 QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); |
197 QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); |
189 statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName)); |
198 statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName)); |
190 } |
199 downloadButton->setEnabled(true); |
191 |
200 } |
192 downloadButton->setEnabled(true); |
201 |
|
202 reply->deleteLater(); |
|
203 reply = 0; |
193 delete file; |
204 delete file; |
194 file = 0; |
205 file = 0; |
195 } |
206 } |
196 |
207 |
197 void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) |
208 void HttpWindow::httpReadyRead() |
198 { |
209 { |
199 switch (responseHeader.statusCode()) { |
210 // this slot gets called everytime the QNetworkReply has new data. |
200 case 200: // Ok |
211 // We read all of its new data and write it into the file. |
201 case 301: // Moved Permanently |
212 // That way we use less RAM than when reading it at the finished() |
202 case 302: // Found |
213 // signal of the QNetworkReply |
203 case 303: // See Other |
214 if (file) |
204 case 307: // Temporary Redirect |
215 file->write(reply->readAll()); |
205 // these are not error conditions |
216 } |
206 break; |
217 |
207 |
218 void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes) |
208 default: |
|
209 QMessageBox::information(this, tr("HTTP"), |
|
210 tr("Download failed: %1.") |
|
211 .arg(responseHeader.reasonPhrase())); |
|
212 httpRequestAborted = true; |
|
213 progressDialog->hide(); |
|
214 http->abort(); |
|
215 } |
|
216 } |
|
217 |
|
218 void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes) |
|
219 { |
219 { |
220 if (httpRequestAborted) |
220 if (httpRequestAborted) |
221 return; |
221 return; |
222 |
222 |
223 progressDialog->setMaximum(totalBytes); |
223 progressDialog->setMaximum(totalBytes); |
227 void HttpWindow::enableDownloadButton() |
227 void HttpWindow::enableDownloadButton() |
228 { |
228 { |
229 downloadButton->setEnabled(!urlLineEdit->text().isEmpty()); |
229 downloadButton->setEnabled(!urlLineEdit->text().isEmpty()); |
230 } |
230 } |
231 |
231 |
232 void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator) |
232 void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator) |
233 { |
233 { |
234 QDialog dlg; |
234 QDialog dlg; |
235 Ui::Dialog ui; |
235 Ui::Dialog ui; |
236 ui.setupUi(&dlg); |
236 ui.setupUi(&dlg); |
237 dlg.adjustSize(); |
237 dlg.adjustSize(); |
238 ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName)); |
238 ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host())); |
239 |
239 |
|
240 // Did the URL have information? Fill the UI |
|
241 // This is only relevant if the URL-supplied credentials were wrong |
|
242 ui.userEdit->setText(url.userName()); |
|
243 ui.passwordEdit->setText(url.password()); |
|
244 |
240 if (dlg.exec() == QDialog::Accepted) { |
245 if (dlg.exec() == QDialog::Accepted) { |
241 authenticator->setUser(ui.userEdit->text()); |
246 authenticator->setUser(ui.userEdit->text()); |
242 authenticator->setPassword(ui.passwordEdit->text()); |
247 authenticator->setPassword(ui.passwordEdit->text()); |
243 } |
248 } |
244 } |
249 } |
245 |
250 |
246 #ifndef QT_NO_OPENSSL |
251 #ifndef QT_NO_OPENSSL |
247 void HttpWindow::sslErrors(const QList<QSslError> &errors) |
252 void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors) |
248 { |
253 { |
249 QString errorString; |
254 QString errorString; |
250 foreach (const QSslError &error, errors) { |
255 foreach (const QSslError &error, errors) { |
251 if (!errorString.isEmpty()) |
256 if (!errorString.isEmpty()) |
252 errorString += ", "; |
257 errorString += ", "; |
253 errorString += error.errorString(); |
258 errorString += error.errorString(); |
254 } |
259 } |
255 |
260 |
256 if (QMessageBox::warning(this, tr("HTTP Example"), |
261 if (QMessageBox::warning(this, tr("HTTP"), |
257 tr("One or more SSL errors has occurred: %1").arg(errorString), |
262 tr("One or more SSL errors has occurred: %1").arg(errorString), |
258 QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) { |
263 QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) { |
259 http->ignoreSslErrors(); |
264 reply->ignoreSslErrors(); |
260 } |
265 } |
261 } |
266 } |
262 #endif |
267 #endif |