| 299 |
david |
1 |
/*============================================================================
|
|
|
2 |
* Copyright (C) 2013 Embedded Solutions
|
|
|
3 |
**============================================================================
|
|
|
4 |
**
|
|
|
5 |
** Project/Product :
|
|
|
6 |
** Filename : qmhttppath.c
|
|
|
7 |
** Author(s) : DDP
|
|
|
8 |
**
|
|
|
9 |
** Description : Class to load an HTML file from a URL
|
|
|
10 |
** This version will save it as a data blob internally
|
|
|
11 |
** Commented out code will write it to a file
|
|
|
12 |
**
|
|
|
13 |
** Information :
|
|
|
14 |
** Compiler : ANSI C++
|
|
|
15 |
** Target :
|
|
|
16 |
**
|
|
|
17 |
***==========================================================================*/
|
|
|
18 |
|
| 297 |
david |
19 |
#include <QtGui>
|
|
|
20 |
#include <QLineEdit>
|
|
|
21 |
#include <QLabel>
|
|
|
22 |
#include "qmhttppath.h"
|
| 326 |
david |
23 |
#include "qmconfig.h"
|
| 301 |
david |
24 |
#include "consts.h"
|
|
|
25 |
#include "structs.h"
|
|
|
26 |
#include "proto.h"
|
| 297 |
david |
27 |
|
|
|
28 |
#include <QtGui>
|
|
|
29 |
#include <QtNetwork>
|
|
|
30 |
|
|
|
31 |
#include "Qmhttppath.h"
|
|
|
32 |
#include "ui_authenticationdialog.h"
|
|
|
33 |
|
| 299 |
david |
34 |
/*----------------------------------------------------------------------------
|
|
|
35 |
** FUNCTION : Qmhttppath
|
|
|
36 |
**
|
|
|
37 |
** DESCRIPTION : Constructor
|
|
|
38 |
** Creates dialog to procces the web file loading
|
|
|
39 |
**
|
|
|
40 |
** INPUTS :
|
|
|
41 |
**
|
|
|
42 |
** RETURNS :
|
|
|
43 |
**
|
|
|
44 |
----------------------------------------------------------------------------*/
|
|
|
45 |
|
|
|
46 |
|
| 297 |
david |
47 |
Qmhttppath::Qmhttppath(QWidget *parent)
|
|
|
48 |
: QDialog(parent)
|
|
|
49 |
{
|
|
|
50 |
this->resize(500,40);
|
| 326 |
david |
51 |
urlLineEdit = new QLineEdit(appSettings->value("Recent/HtmlUpload",config.webUrl).toString());
|
| 297 |
david |
52 |
|
|
|
53 |
urlLabel = new QLabel(tr("&URL:"));
|
|
|
54 |
urlLabel->setBuddy(urlLineEdit);
|
| 300 |
david |
55 |
statusLabel = new QLabel(tr("Please enter the URL of a file you want to import."));
|
| 297 |
david |
56 |
|
| 300 |
david |
57 |
downloadButton = new QPushButton(tr("Import"));
|
| 297 |
david |
58 |
downloadButton->setDefault(true);
|
| 301 |
david |
59 |
quitButton = new QPushButton(tr("Cancel"));
|
| 297 |
david |
60 |
quitButton->setAutoDefault(false);
|
|
|
61 |
|
|
|
62 |
buttonBox = new QDialogButtonBox;
|
|
|
63 |
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
|
|
|
64 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|
|
65 |
|
|
|
66 |
progressDialog = new QProgressDialog(this);
|
|
|
67 |
|
|
|
68 |
connect(urlLineEdit, SIGNAL(textChanged(QString)),
|
|
|
69 |
this, SLOT(enableDownloadButton()));
|
|
|
70 |
|
|
|
71 |
connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
|
|
|
72 |
this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
|
|
|
73 |
#ifndef QT_NO_OPENSSL
|
|
|
74 |
connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
|
|
|
75 |
this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
|
|
|
76 |
#endif
|
|
|
77 |
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
|
|
|
78 |
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
|
|
|
79 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
|
|
80 |
|
|
|
81 |
QHBoxLayout *topLayout = new QHBoxLayout;
|
|
|
82 |
topLayout->addWidget(urlLabel);
|
|
|
83 |
topLayout->addWidget(urlLineEdit);
|
|
|
84 |
|
|
|
85 |
QVBoxLayout *mainLayout = new QVBoxLayout;
|
|
|
86 |
mainLayout->addLayout(topLayout);
|
|
|
87 |
mainLayout->addWidget(statusLabel);
|
|
|
88 |
mainLayout->addWidget(buttonBox);
|
|
|
89 |
setLayout(mainLayout);
|
|
|
90 |
|
|
|
91 |
setWindowTitle(tr("Download web entries via HTTP"));
|
|
|
92 |
urlLineEdit->setFocus();
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
void Qmhttppath::startRequest(QUrl url)
|
|
|
96 |
{
|
|
|
97 |
reply = qnam.get(QNetworkRequest(url));
|
|
|
98 |
connect(reply, SIGNAL(finished()),
|
|
|
99 |
this, SLOT(httpFinished()));
|
|
|
100 |
connect(reply, SIGNAL(readyRead()),
|
|
|
101 |
this, SLOT(httpReadyRead()));
|
|
|
102 |
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
|
|
|
103 |
this, SLOT(updateDataReadProgress(qint64,qint64)));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
void Qmhttppath::downloadFile()
|
|
|
107 |
{
|
|
|
108 |
url = urlLineEdit->text();
|
| 326 |
david |
109 |
|
|
|
110 |
/*
|
|
|
111 |
** Save Web address for future use
|
|
|
112 |
*/
|
| 301 |
david |
113 |
strncpy(config.webUrl, (const char *)(url.toString().toAscii()), sizeof(config.webUrl) );
|
|
|
114 |
config.write_config();
|
| 297 |
david |
115 |
|
| 326 |
david |
116 |
appSettings->setValue("Recent/HtmlUpload", url.toString());
|
|
|
117 |
|
|
|
118 |
|
| 297 |
david |
119 |
QFileInfo fileInfo(url.path());
|
|
|
120 |
QString fileName = fileInfo.fileName();
|
|
|
121 |
if (fileName.isEmpty())
|
|
|
122 |
fileName = "index.html";
|
|
|
123 |
|
|
|
124 |
// if (QFile::exists(fileName)) {
|
|
|
125 |
// if (QMessageBox::question(this, tr("HTTP"),
|
|
|
126 |
// tr("There already exists a file called %1 in "
|
|
|
127 |
// "the current directory. Overwrite?").arg(fileName),
|
|
|
128 |
// QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
|
|
|
129 |
// == QMessageBox::No)
|
|
|
130 |
// return;
|
|
|
131 |
// QFile::remove(fileName);
|
|
|
132 |
// }
|
|
|
133 |
|
|
|
134 |
// file = new QFile(fileName);
|
|
|
135 |
// if (!file->open(QIODevice::WriteOnly)) {
|
|
|
136 |
// QMessageBox::information(this, tr("HTTP"),
|
|
|
137 |
// tr("Unable to save the file %1: %2.")
|
|
|
138 |
// .arg(fileName).arg(file->errorString()));
|
|
|
139 |
// delete file;
|
|
|
140 |
// file = 0;
|
|
|
141 |
// return;
|
|
|
142 |
// }
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
progressDialog->setWindowTitle(tr("HTTP"));
|
|
|
146 |
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
|
|
|
147 |
downloadButton->setEnabled(false);
|
|
|
148 |
|
|
|
149 |
// schedule the request
|
|
|
150 |
httpRequestAborted = false;
|
|
|
151 |
startRequest(url);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
void Qmhttppath::cancelDownload()
|
|
|
155 |
{
|
|
|
156 |
statusLabel->setText(tr("Download canceled."));
|
|
|
157 |
httpRequestAborted = true;
|
|
|
158 |
reply->abort();
|
|
|
159 |
downloadButton->setEnabled(true);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
void Qmhttppath::httpFinished()
|
|
|
163 |
{
|
|
|
164 |
if (httpRequestAborted) {
|
|
|
165 |
// if (file) {
|
|
|
166 |
// file->close();
|
|
|
167 |
// file->remove();
|
|
|
168 |
// delete file;
|
|
|
169 |
// file = 0;
|
|
|
170 |
// }
|
|
|
171 |
reply->deleteLater();
|
|
|
172 |
progressDialog->hide();
|
|
|
173 |
return;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
progressDialog->hide();
|
|
|
177 |
// file->flush();
|
|
|
178 |
// file->close();
|
|
|
179 |
|
|
|
180 |
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
|
181 |
if (reply->error()) {
|
|
|
182 |
//file->remove();
|
|
|
183 |
QMessageBox::information(this, tr("HTTP"),
|
|
|
184 |
tr("Download failed: %1.")
|
|
|
185 |
.arg(reply->errorString()));
|
|
|
186 |
downloadButton->setEnabled(true);
|
|
|
187 |
} else if (!redirectionTarget.isNull()) {
|
|
|
188 |
QUrl newUrl = url.resolved(redirectionTarget.toUrl());
|
|
|
189 |
if (QMessageBox::question(this, tr("HTTP"),
|
|
|
190 |
tr("Redirect to %1 ?").arg(newUrl.toString()),
|
|
|
191 |
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
|
|
|
192 |
url = newUrl;
|
|
|
193 |
reply->deleteLater();
|
|
|
194 |
//file->open(QIODevice::WriteOnly);
|
|
|
195 |
//file->resize(0);
|
|
|
196 |
startRequest(url);
|
|
|
197 |
return;
|
|
|
198 |
}
|
|
|
199 |
} else {
|
|
|
200 |
QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
|
|
|
201 |
statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
|
|
|
202 |
downloadButton->setEnabled(true);
|
|
|
203 |
|
|
|
204 |
// Save all the data that we have
|
|
|
205 |
allData = reply->readAll();
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
reply->deleteLater();
|
|
|
209 |
reply = 0;
|
|
|
210 |
//delete file;
|
|
|
211 |
//file = 0;
|
|
|
212 |
accept();
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
void Qmhttppath::httpReadyRead()
|
|
|
216 |
{
|
|
|
217 |
// this slot gets called every time the QNetworkReply has new data.
|
|
|
218 |
// We read all of its new data and write it into the file.
|
|
|
219 |
// That way we use less RAM than when reading it at the finished()
|
|
|
220 |
// signal of the QNetworkReply
|
|
|
221 |
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
void Qmhttppath::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
|
|
|
225 |
{
|
|
|
226 |
if (httpRequestAborted)
|
|
|
227 |
return;
|
|
|
228 |
|
|
|
229 |
progressDialog->setMaximum(totalBytes);
|
|
|
230 |
progressDialog->setValue(bytesRead);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
void Qmhttppath::enableDownloadButton()
|
|
|
234 |
{
|
|
|
235 |
downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
void Qmhttppath::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator)
|
|
|
239 |
{
|
|
|
240 |
QDialog dlg;
|
|
|
241 |
Ui::Dialog ui;
|
|
|
242 |
ui.setupUi(&dlg);
|
|
|
243 |
dlg.adjustSize();
|
|
|
244 |
ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host()));
|
|
|
245 |
|
|
|
246 |
// Did the URL have information? Fill the UI
|
|
|
247 |
// This is only relevant if the URL-supplied credentials were wrong
|
|
|
248 |
ui.userEdit->setText(url.userName());
|
|
|
249 |
ui.passwordEdit->setText(url.password());
|
|
|
250 |
|
|
|
251 |
if (dlg.exec() == QDialog::Accepted) {
|
|
|
252 |
authenticator->setUser(ui.userEdit->text());
|
|
|
253 |
authenticator->setPassword(ui.passwordEdit->text());
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
#ifndef QT_NO_OPENSSL
|
|
|
258 |
void Qmhttppath::sslErrors(QNetworkReply*,const QList<QSslError> &errors)
|
|
|
259 |
{
|
|
|
260 |
QString errorString;
|
|
|
261 |
foreach (const QSslError &error, errors) {
|
|
|
262 |
if (!errorString.isEmpty())
|
|
|
263 |
errorString += ", ";
|
|
|
264 |
errorString += error.errorString();
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
if (QMessageBox::warning(this, tr("HTTP"),
|
|
|
268 |
tr("One or more SSL errors has occurred: %1").arg(errorString),
|
|
|
269 |
QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
|
|
|
270 |
reply->ignoreSslErrors();
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
#endif
|