You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.4 KiB
103 lines
2.4 KiB
14 years ago
|
/*
|
||
|
* UBHttpFileDownloader.cpp
|
||
|
*
|
||
|
* Created on: 28 avr. 2009
|
||
|
* Author: Luc
|
||
|
*/
|
||
|
|
||
|
#include "UBHttpFileDownloader.h"
|
||
|
|
||
|
#include "network/UBNetworkAccessManager.h"
|
||
|
|
||
|
|
||
|
UBHttpFileDownloader::UBHttpFileDownloader(QObject *parent)
|
||
|
: QObject(parent)
|
||
|
{
|
||
|
// NOOP
|
||
|
}
|
||
|
|
||
|
|
||
|
UBHttpFileDownloader::~UBHttpFileDownloader()
|
||
|
{
|
||
|
// NOOP
|
||
|
}
|
||
|
|
||
|
|
||
|
void UBHttpFileDownloader::download(const QList<QUrl>& urls, const QList<QFile*>& files)
|
||
|
{
|
||
|
mUrlsToDownload = urls;
|
||
|
mFilesToSave = files;
|
||
|
|
||
|
mSuccess = true;
|
||
|
|
||
|
downloadNext();
|
||
|
}
|
||
|
|
||
|
|
||
|
void UBHttpFileDownloader::downloadNext()
|
||
|
{
|
||
|
if (mUrlsToDownload.length() > 0 && mFilesToSave.length() > 0)
|
||
|
{
|
||
|
QUrl url = mUrlsToDownload.takeFirst();
|
||
|
mCurrentFile = mFilesToSave.takeFirst();
|
||
|
|
||
|
if (!mCurrentFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
|
||
|
{
|
||
|
qCritical() << "cannot open " << mCurrentFile << " for writing ...";
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
UBNetworkAccessManager * nam = UBNetworkAccessManager::defaultAccessManager();
|
||
|
QNetworkRequest request(url);
|
||
|
mReply = nam->get(request); // UB 4.2 TODO who owns and delete the reply ?
|
||
|
|
||
|
connect(mReply, SIGNAL(finished ()), this, SLOT(finished ()));
|
||
|
connect(mReply, SIGNAL(error ( QNetworkReply::NetworkError )),
|
||
|
this, SLOT(error ( QNetworkReply::NetworkError)));
|
||
|
connect(mReply, SIGNAL(downloadProgress ( qint64 , qint64 )),
|
||
|
this, SLOT(downloadProgress(qint64 , qint64)));
|
||
|
|
||
|
connect(mReply, SIGNAL(readyRead ()), this, SLOT( readyRead()));
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
emit finished(mSuccess);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void UBHttpFileDownloader::downloadProgress (qint64 bytesReceived, qint64 bytesTotal)
|
||
|
{
|
||
|
Q_UNUSED(bytesReceived);
|
||
|
Q_UNUSED(bytesTotal);
|
||
|
//qDebug() << bytesReceived << "/" << bytesTotal;
|
||
|
}
|
||
|
|
||
|
|
||
|
void UBHttpFileDownloader::error ( QNetworkReply::NetworkError code )
|
||
|
{
|
||
|
qDebug() << "UBHttpFileDownloader::error : code=" << code;
|
||
|
mSuccess = mSuccess && (code == QNetworkReply::NoError);
|
||
|
|
||
|
}
|
||
|
|
||
|
void UBHttpFileDownloader::finished ()
|
||
|
{
|
||
|
mCurrentFile->flush();
|
||
|
mCurrentFile->close();
|
||
|
|
||
|
if (mReply)
|
||
|
delete mReply;
|
||
|
|
||
|
downloadNext();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void UBHttpFileDownloader::readyRead()
|
||
|
{
|
||
|
mCurrentFile->write(mReply->readAll());
|
||
|
}
|