#include <QApplication>
#include <QDialog>
#include <QtGui>
#include <QtNetwork>
#include <iostream>
{
Q_OBJECT
public:
private slots:
void httpRequestFinished(int requestId, bool error);
void updateDataReadProgress(int bytesRead, int totalBytes);
void cancelDownload();
void downloadFile();
private:
int httpGetId;
bool httpRequestAborted;
void installFile();
};
{
buttonsLayout->addStretch();
buttonsLayout->addWidget(downloadButton);
buttonsLayout->addWidget(closeButton);
buttonsLayout->addStretch();
setLayout(buttonsLayout);
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(dataReadProgress(int, int)),
this, SLOT(updateDataReadProgress(int, int)));
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
}
void Dialog::downloadFile()
{
QUrl url
("http://www.nih.at/libzip/index.html");
fileName
= QDir::tempPath() + fileInfo.
fileName();
if (QFile::exists(fileName
)) { tr("HTTP"),
trUtf8("There already exists a file called %1 in the current directory. Overwrite?")
.arg(fileName),
return;
}
file = new QFile(fileName
);
tr("HTTP"),
trUtf8("Impossibile salvare il file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
return;
}
QHttp::ConnectionMode mode
= url.
scheme().
toLower() == "https" ?
QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
httpRequestAborted = false;
httpGetId = http->get(url.path(), file);
progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
downloadButton->setEnabled(false);
}
void Dialog::cancelDownload()
{
httpRequestAborted = true;
http->abort();
downloadButton->setEnabled(true);
}
void Dialog::httpRequestFinished(int requestId, bool error)
{
std::cout << "httpRequestFinished()" << std::endl;
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
progressDialog->hide();
return;
}
if (requestId != httpGetId)
return;
progressDialog->hide();
file->close();
if (error) {
file->remove();
tr("HTTP"),
trUtf8("Download failed: %1.")
.arg(http->errorString()));
} else
installFile();
delete file;
file = 0;
}
{
if (responseHeader.statusCode() != 200) {
tr("HTTP"),
trUtf8("Download failed: %1.").
arg(responseHeader.reasonPhrase()));
httpRequestAborted = true;
progressDialog->hide();
http->abort();
return;
}
}
void Dialog::updateDataReadProgress(int bytesRead, int totalBytes)
{
if (httpRequestAborted)
return;
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(bytesRead);
}
void Dialog::installFile()
{
tr("Ready"),
trUtf8("If you don't click me quickly\nI will make some unwanted thing."),
fileName = fi.fileName();
file
->copy
(QDir::tempPath() + "copied-index.html");
}
downloadButton->setEnabled(true);
}
int main(int argc, char *argv[])
{
Dialog *dialog = new Dialog;
dialog->show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QDialog>
#include <QtGui>
#include <QtNetwork>
#include <iostream>
class Dialog: public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private slots:
void httpRequestFinished(int requestId, bool error);
void updateDataReadProgress(int bytesRead, int totalBytes);
void readResponseHeader(const QHttpResponseHeader &responseHeader);
void cancelDownload();
void downloadFile();
private:
QString fileName;
QFile *file;
QPushButton *downloadButton;
QPushButton *closeButton;
QProgressDialog *progressDialog;
QHttp *http;
int httpGetId;
QDir dir;
bool httpRequestAborted;
void installFile();
};
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
downloadButton = new QPushButton("Download");
closeButton = new QPushButton("Close");
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(downloadButton);
buttonsLayout->addWidget(closeButton);
buttonsLayout->addStretch();
setLayout(buttonsLayout);
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
progressDialog = new QProgressDialog(this);
http = new QHttp(this);
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(dataReadProgress(int, int)),
this, SLOT(updateDataReadProgress(int, int)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
}
void Dialog::downloadFile()
{
QUrl url("http://www.nih.at/libzip/index.html");
QFileInfo fileInfo(url.path());
QString fileName;
fileName = QDir::tempPath() + fileInfo.fileName();
if (QFile::exists(fileName)) {
if (QMessageBox::question(this,
tr("HTTP"),
trUtf8("There already exists a file called %1 in the current directory. Overwrite?")
.arg(fileName),
QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel)
== QMessageBox::Cancel)
return;
QFile::remove(fileName);
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this,
tr("HTTP"),
trUtf8("Impossibile salvare il file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
return;
}
QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
httpRequestAborted = false;
httpGetId = http->get(url.path(), file);
progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
downloadButton->setEnabled(false);
}
void Dialog::cancelDownload()
{
httpRequestAborted = true;
http->abort();
downloadButton->setEnabled(true);
}
void Dialog::httpRequestFinished(int requestId, bool error)
{
std::cout << "httpRequestFinished()" << std::endl;
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
progressDialog->hide();
return;
}
if (requestId != httpGetId)
return;
progressDialog->hide();
file->close();
if (error) {
file->remove();
QMessageBox::information(this,
tr("HTTP"),
trUtf8("Download failed: %1.")
.arg(http->errorString()));
} else
installFile();
delete file;
file = 0;
}
void Dialog::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
if (responseHeader.statusCode() != 200) {
QMessageBox::information(this,
tr("HTTP"),
trUtf8("Download failed: %1.").
arg(responseHeader.reasonPhrase()));
httpRequestAborted = true;
progressDialog->hide();
http->abort();
return;
}
}
void Dialog::updateDataReadProgress(int bytesRead, int totalBytes)
{
if (httpRequestAborted)
return;
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(bytesRead);
}
void Dialog::installFile()
{
if (QMessageBox::question(this,
tr("Ready"),
trUtf8("If you don't click me quickly\nI will make some unwanted thing."),
QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel)
== QMessageBox::Ok) {
QString fileName;
QFileInfo fi(file->fileName());
fileName = fi.fileName();
file->copy(QDir::tempPath() + "copied-index.html");
}
downloadButton->setEnabled(true);
}
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
Dialog *dialog = new Dialog;
dialog->show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks