2010-05-29 11:33:04 +00:00
|
|
|
#include "webmultimediadownload.hh"
|
|
|
|
#include "filetype.hh"
|
|
|
|
|
|
|
|
namespace Dictionary {
|
|
|
|
|
2017-03-13 14:40:19 +00:00
|
|
|
#define MAX_REDIRECTS 10
|
|
|
|
|
2010-05-29 11:33:04 +00:00
|
|
|
WebMultimediaDownload::WebMultimediaDownload( QUrl const & url, QNetworkAccessManager & _mgr ):
|
2017-03-13 14:40:19 +00:00
|
|
|
mgr( _mgr ),
|
|
|
|
redirectCount( 0 )
|
2010-05-29 11:33:04 +00:00
|
|
|
{
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( &mgr, &QNetworkAccessManager::finished, this, &WebMultimediaDownload::replyFinished, Qt::QueuedConnection );
|
2010-05-29 11:33:04 +00:00
|
|
|
|
|
|
|
reply = mgr.get( QNetworkRequest( url ) );
|
2017-03-13 14:40:19 +00:00
|
|
|
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2017-03-13 14:40:19 +00:00
|
|
|
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ), reply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
2010-05-29 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebMultimediaDownload::cancel()
|
|
|
|
{
|
2013-05-08 14:50:06 +00:00
|
|
|
reply = NULL;
|
2010-05-29 11:33:04 +00:00
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebMultimediaDownload::replyFinished( QNetworkReply * r )
|
|
|
|
{
|
2013-05-08 16:08:34 +00:00
|
|
|
if ( !r || r != reply ) {
|
2010-05-29 11:33:04 +00:00
|
|
|
return; // Not our reply
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2010-05-29 11:33:04 +00:00
|
|
|
|
2013-05-08 14:50:06 +00:00
|
|
|
if ( r->error() == QNetworkReply::NoError ) {
|
2017-03-13 14:40:19 +00:00
|
|
|
// Check for redirect reply
|
|
|
|
|
|
|
|
QVariant possibleRedirectUrl = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
|
|
|
|
QUrl redirectUrl = possibleRedirectUrl.toUrl();
|
|
|
|
if ( !redirectUrl.isEmpty() ) {
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( reply, 0, 0, 0 );
|
2017-03-13 14:40:19 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if ( ++redirectCount > MAX_REDIRECTS ) {
|
|
|
|
setErrorString( "Too many redirects detected" );
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = mgr.get( QNetworkRequest( redirectUrl ) );
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2017-03-13 14:40:19 +00:00
|
|
|
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ), reply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle reply data
|
|
|
|
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2010-05-29 11:33:04 +00:00
|
|
|
|
2018-03-12 17:22:38 +00:00
|
|
|
data.resize( r->bytesAvailable() );
|
2010-05-29 11:33:04 +00:00
|
|
|
|
2018-03-12 17:22:38 +00:00
|
|
|
r->read( data.data(), data.size() );
|
2010-05-29 11:33:04 +00:00
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
}
|
|
|
|
else {
|
2013-05-08 14:50:06 +00:00
|
|
|
setErrorString( r->errorString() );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2010-05-29 11:33:04 +00:00
|
|
|
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( r, 0, 0, 0 );
|
2013-05-08 16:08:34 +00:00
|
|
|
r->deleteLater();
|
2013-05-08 14:50:06 +00:00
|
|
|
reply = NULL;
|
2013-05-08 16:08:34 +00:00
|
|
|
|
|
|
|
finish();
|
2010-05-29 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dictionary
|