Changed: Use sender() instead of member
This commit is contained in:
parent
a46bb29254
commit
3f25776129
2 changed files with 43 additions and 23 deletions
|
@ -26,7 +26,7 @@
|
||||||
#define new DEBUG_NEW
|
#define new DEBUG_NEW
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CDownloader::CDownloader(QObject *parent, IOperationProgressListener *listener):QObject(parent), m_listener(listener), m_manager(NULL), m_reply(NULL), m_timer(NULL),
|
CDownloader::CDownloader(QObject *parent, IOperationProgressListener *listener):QObject(parent), m_listener(listener), m_manager(NULL), m_timer(NULL),
|
||||||
m_offset(0), m_size(0), m_supportsAcceptRanges(false), m_supportsContentRange(false),
|
m_offset(0), m_size(0), m_supportsAcceptRanges(false), m_supportsContentRange(false),
|
||||||
m_downloadAfterHead(false), m_file(NULL)
|
m_downloadAfterHead(false), m_file(NULL)
|
||||||
{
|
{
|
||||||
|
@ -169,10 +169,10 @@ void CDownloader::getFileHead()
|
||||||
request.setRawHeader("Range", QString("bytes=%1-").arg(m_offset).toLatin1());
|
request.setRawHeader("Range", QString("bytes=%1-").arg(m_offset).toLatin1());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_reply = m_manager->head(request);
|
QNetworkReply *reply = m_manager->head(request);
|
||||||
|
|
||||||
connect(m_reply, SIGNAL(finished()), SLOT(onHeadFinished()));
|
connect(reply, SIGNAL(finished()), SLOT(onHeadFinished()));
|
||||||
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)));
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)));
|
||||||
|
|
||||||
startTimer();
|
startTimer();
|
||||||
}
|
}
|
||||||
|
@ -212,12 +212,12 @@ void CDownloader::downloadFile()
|
||||||
request.setRawHeader("Range", QString("bytes=%1-%2").arg(m_offset).arg(m_size-1).toLatin1());
|
request.setRawHeader("Range", QString("bytes=%1-%2").arg(m_offset).arg(m_size-1).toLatin1());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_reply = m_manager->get(request);
|
QNetworkReply *reply = m_manager->get(request);
|
||||||
|
|
||||||
connect(m_reply, SIGNAL(finished()), SLOT(onDownloadFinished()));
|
connect(reply, SIGNAL(finished()), SLOT(onDownloadFinished()));
|
||||||
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)));
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)));
|
||||||
connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(onDownloadProgress(qint64, qint64)));
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(onDownloadProgress(qint64, qint64)));
|
||||||
connect(m_reply, SIGNAL(readyRead()), SLOT(onDownloadRead()));
|
connect(reply, SIGNAL(readyRead()), SLOT(onDownloadRead()));
|
||||||
|
|
||||||
if (m_listener) m_listener->operationStart();
|
if (m_listener) m_listener->operationStart();
|
||||||
|
|
||||||
|
@ -254,18 +254,27 @@ void CDownloader::onHeadFinished()
|
||||||
{
|
{
|
||||||
stopTimer();
|
stopTimer();
|
||||||
|
|
||||||
int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
QString redirection = m_reply->header(QNetworkRequest::LocationHeader).toString();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
QString url = reply->url().toString();
|
||||||
|
|
||||||
m_size = m_reply->header(QNetworkRequest::ContentLengthHeader).toInt();
|
QString redirection = reply->header(QNetworkRequest::LocationHeader).toString();
|
||||||
m_lastModified = m_reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toUTC();
|
|
||||||
|
|
||||||
QString acceptRanges = QString::fromLatin1(m_reply->rawHeader("Accept-Ranges"));
|
m_size = reply->header(QNetworkRequest::ContentLengthHeader).toInt();
|
||||||
QString contentRange = QString::fromLatin1(m_reply->rawHeader("Content-Range"));
|
m_lastModified = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toUTC();
|
||||||
|
|
||||||
m_reply->deleteLater();
|
QString acceptRanges = QString::fromLatin1(reply->rawHeader("Accept-Ranges"));
|
||||||
m_reply = NULL;
|
QString contentRange = QString::fromLatin1(reply->rawHeader("Content-Range"));
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
nlinfo("HTTP status code %d on HEAD for %s", status, Q2C(url));
|
||||||
|
|
||||||
|
if (!redirection.isEmpty())
|
||||||
|
{
|
||||||
|
nlinfo("Redirected to %s", Q2C(redirection));
|
||||||
|
}
|
||||||
|
|
||||||
// redirection
|
// redirection
|
||||||
if (status == 302)
|
if (status == 302)
|
||||||
|
@ -374,10 +383,14 @@ void CDownloader::onHeadFinished()
|
||||||
|
|
||||||
void CDownloader::onDownloadFinished()
|
void CDownloader::onDownloadFinished()
|
||||||
{
|
{
|
||||||
int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
m_reply->deleteLater();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
m_reply = NULL;
|
QString url = reply->url().toString();
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
nlwarning("Download finished with HTTP status code %d when downloading %s", status, Q2C(url));
|
||||||
|
|
||||||
closeFile();
|
closeFile();
|
||||||
|
|
||||||
|
@ -408,6 +421,10 @@ void CDownloader::onDownloadFinished()
|
||||||
|
|
||||||
void CDownloader::onError(QNetworkReply::NetworkError error)
|
void CDownloader::onError(QNetworkReply::NetworkError error)
|
||||||
{
|
{
|
||||||
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
|
nlwarning("Network error %s (%d) when downloading %s", Q2C(reply->errorString()), error, Q2C(m_url));
|
||||||
|
|
||||||
if (!m_listener) return;
|
if (!m_listener) return;
|
||||||
|
|
||||||
if (error == QNetworkReply::OperationCanceledError)
|
if (error == QNetworkReply::OperationCanceledError)
|
||||||
|
@ -422,13 +439,17 @@ void CDownloader::onDownloadProgress(qint64 current, qint64 total)
|
||||||
|
|
||||||
if (!m_listener) return;
|
if (!m_listener) return;
|
||||||
|
|
||||||
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
m_listener->operationProgress(m_offset + current, m_url);
|
m_listener->operationProgress(m_offset + current, m_url);
|
||||||
|
|
||||||
// abort download
|
// abort download
|
||||||
if (m_listener->operationShouldStop() && m_reply) m_reply->abort();
|
if (m_listener->operationShouldStop() && reply) reply->abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CDownloader::onDownloadRead()
|
void CDownloader::onDownloadRead()
|
||||||
{
|
{
|
||||||
if (m_file) m_file->write(m_reply->readAll());
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
|
if (m_file && reply) m_file->write(reply->readAll());
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,6 @@ protected:
|
||||||
bool checkDownloadedFile();
|
bool checkDownloadedFile();
|
||||||
|
|
||||||
QNetworkAccessManager *m_manager;
|
QNetworkAccessManager *m_manager;
|
||||||
QNetworkReply *m_reply;
|
|
||||||
QTimer *m_timer;
|
QTimer *m_timer;
|
||||||
|
|
||||||
QString m_url;
|
QString m_url;
|
||||||
|
|
Loading…
Reference in a new issue