MainWindow::MainWindow()
{
...
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
file = 0;
downloadFile();
}
MainWindow::~MainWindow()
{
if(file) {
delete file;
file = 0;
}
}
void MainWindow::downloadFile()
{
if(file) {
delete file;
file = 0;
}
QUrl url
("http://localhost/sito/lastrelease.xml");
if (!file->open()) {
statusBar()->showMessage(tr("Impossible to save the file %1: %2.").arg(file->fileName()).arg(file->errorString()));
delete file;
file = 0;
return;
}
http->setHost(url.host(), url.port(80));
httpRequestAborted = false;
httpGetId = http->get(url.path(), file);
}
void MainWindow::httpRequestFinished(int requestId, bool error)
{
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if(file) {
file->close();
file->remove();
}
return;
}
if (requestId != httpGetId)
return;
file->close();
if (error) {
file->remove();
statusBar()->showMessage(tr("Download failed: %1.").arg(http->errorString()));
} else {
statusBar()->showMessage(tr("lastrelease downloaded"));
}
}
{
if (responseHeader.statusCode() != 200) {
statusBar()->showMessage(tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
httpRequestAborted = true;
http->abort();
return;
}
}
MainWindow::MainWindow()
{
...
http = new QHttp(this);
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
file = 0;
downloadFile();
}
MainWindow::~MainWindow()
{
if(file) {
delete file;
file = 0;
}
}
void MainWindow::downloadFile()
{
if(file) {
delete file;
file = 0;
}
QUrl url("http://localhost/sito/lastrelease.xml");
file = new QTemporaryFile;
if (!file->open()) {
statusBar()->showMessage(tr("Impossible to save the file %1: %2.").arg(file->fileName()).arg(file->errorString()));
delete file;
file = 0;
return;
}
http->setHost(url.host(), url.port(80));
httpRequestAborted = false;
httpGetId = http->get(url.path(), file);
}
void MainWindow::httpRequestFinished(int requestId, bool error)
{
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if(file) {
file->close();
file->remove();
}
return;
}
if (requestId != httpGetId)
return;
file->close();
if (error) {
file->remove();
statusBar()->showMessage(tr("Download failed: %1.").arg(http->errorString()));
} else {
statusBar()->showMessage(tr("lastrelease downloaded"));
}
}
void MainWindow::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
if (responseHeader.statusCode() != 200) {
statusBar()->showMessage(tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
httpRequestAborted = true;
http->abort();
return;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks