| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 
 |  
#include <QApplication>
#include <QString>
#include <QtNetwork>
#include <QtGui>
#include <QDebug>
#include "FenMaj.h"
#include "ui_FenMaj.h"
 
FenMaj::FenMaj(QWidget *parent) : QWidget(parent)
{
    setupUi(this);
}
 
void FenMaj::on_boutonMaj_clicked()
{
	const QUrl URL= QUrl ("http://machin.com/truc.txt");
    const QNetworkRequest requette(URL);
    QNetworkAccessManager *m = new QNetworkAccessManager;
	QNetworkReply *r = m->get(requette);
	bytesTotalLus = r->readBufferSize();
	qDebug() << "bytesTotalLus : " << bytesTotalLus;
    connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(messageErreur(QNetworkReply::NetworkError)));
    connect(r, SIGNAL(finished()), this, SLOT(enregistrer()));
    connect(r, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(progressionDownload(qint64, qint64) ));
	boutonMaj->setEnabled(false);
}
 
void FenMaj::progressionDownload(qint64 bytesReceived, qint64 bytesTotal)
{
	//QNetworkReply *r = qobject_cast<QNetworkReply*>(sender());
	progress->setRange(0, bytesTotalLus);
	//qDebug() << "recu : " << bytesReceived << " ; total : " << bytesTotal;
	progress->setValue(bytesReceived);
 
	if(bytesTotal != -1)
	{
		progress->setRange(0, bytesTotal);
	}
}
 
void FenMaj::messageErreur(QNetworkReply::NetworkError code)
{
	QCoreApplication::processEvents();
    Q_UNUSED(code);
    QNetworkReply *r = qobject_cast<QNetworkReply*>(sender());
    QMessageBox *dialog = new QMessageBox(QMessageBox::Critical, "Erreur", "Erreur lors du chargement. Vérifiez votre connexion internet ou réessayez plus tard <br /><br /> Code de l'erreur : <br /><em>" + r->errorString() + "</em>");
    dialog->show();
    r->close();
    close();
}
 
void FenMaj::enregistrer()
{
    QNetworkReply *r = static_cast<QNetworkReply*>(sender());
    QFile f("fortunes.txt");
    f.open(QIODevice::WriteOnly);
    QString UTF8Text = QString::fromUtf8(r->readAll());
    f.write(UTF8Text.toLatin1());
    f.close();
    r->deleteLater();
	QMessageBox::information(this, "Fin de téléchargement", "Téléchargement terminé !");
	close();
} | 
Partager