#include <QApplication>
#include <QPushButton>
#include <QGridLayout>
#include <QProgressBar>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QHBoxLayout>
#include <QHttp>
#include <QFile>
#include <QUrl>
#include <QMessageBox>
#include <QFileDialog>
class FAQHttp : public QWidget
{
Q_OBJECT
QPushButton *m_Bouton_Dowload;
QProgressBar *m_ProgressBar;
QLineEdit *m_Edit;
QLabel *m_Label;
QHttp *m_http;
QFile *m_file;
int m_httpId;
public :
FAQHttp()
{
this->m_Bouton_Dowload = new QPushButton("Télécharger",this);
this->m_ProgressBar = new QProgressBar(this);
this->m_Edit = new QLineEdit(this);
this->m_Label = new QLabel("URL:", this);
m_Label->setBuddy(m_Edit);
QHBoxLayout *firstLayout = new QHBoxLayout;
firstLayout->addWidget(m_Label);
firstLayout->addWidget(m_Edit);
QHBoxLayout *Layout = new QHBoxLayout;
Layout->addLayout(firstLayout);
Layout->addWidget(m_Bouton_Dowload);
QVBoxLayout *secondeLayout = new QVBoxLayout();
secondeLayout->addLayout(Layout);
secondeLayout->addWidget(m_ProgressBar);
setLayout(secondeLayout);
connect(this->m_Bouton_Dowload, SIGNAL(clicked(bool)), this, SLOT(click_Download(bool)));
resize(400, 150);
}
private slots:
void DownloadFile(QString source , QString fichier)
{
QUrl adresse(source);
this->m_http = new QHttp(adresse.host(),80,this);
m_file = new QFile(fichier);
if(!m_file->open(QIODevice::WriteOnly))
{
QMessageBox::information(this, "HTTP Problemes"," Probleme lors du telechargement");
delete this->m_http;
delete this->m_file;
return;
}
m_httpId = m_http->get(adresse.toString(), m_file);
connect(m_http,SIGNAL(requestFinished(int, bool)),this,SLOT(finish_dowload(int ,bool)));
connect(m_http,SIGNAL(dataSendProgress(int,int)),this,SLOT(progress_dowload(int, int)));
this->m_ProgressBar->reset();
this->m_ProgressBar->setMinimum(0);
}
void finish_dowload(int httpId, bool error)
{
if (this->m_httpId != httpId)
{
return;
}
else
{
if(error)
QMessageBox::information(this, "HTTP Problemes"," Probleme lors du telechargement");
else
QMessageBox::information(this, "HTTP"," Le telechargement a réussi");
this->m_file->close();
delete this->m_http;
delete this->m_file;
}
}
void click_Download(bool valid)
{
QString saveFile = QFileDialog::getSaveFileName(this,"Download Files","C:\\", "All Files (*.*)");
if(!saveFile.isEmpty())
DownloadFile(this->m_Edit->text(),saveFile);
}
void progress_dowload(int done , int total)
{
this->m_ProgressBar->setMaximum(total);
this->m_ProgressBar->setValue(done);
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FAQHttp w;
w.show();
return a.exec();
}