forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmultimediadownload.cc
92 lines (69 loc) · 2.11 KB
/
webmultimediadownload.cc
1
2
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "webmultimediadownload.hh"
#include "filetype.hh"
namespace Dictionary {
#define MAX_REDIRECTS 10
WebMultimediaDownload::WebMultimediaDownload( QUrl const & url,
QNetworkAccessManager & _mgr ) :
mgr( _mgr ),
redirectCount( 0 )
{
connect( &mgr, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)), Qt::QueuedConnection );
reply = mgr.get( QNetworkRequest( url ) );
#ifndef QT_NO_OPENSSL
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ),
reply, SLOT( ignoreSslErrors() ) );
#endif
}
void WebMultimediaDownload::cancel()
{
reply = NULL;
finish();
}
void WebMultimediaDownload::replyFinished( QNetworkReply * r )
{
if ( !r || r != reply )
return; // Not our reply
if ( r->error() == QNetworkReply::NoError )
{
// Check for redirect reply
QVariant possibleRedirectUrl = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
QUrl redirectUrl = possibleRedirectUrl.toUrl();
if( !redirectUrl.isEmpty() )
{
disconnect( reply, 0, 0, 0 );
reply->deleteLater();
if( ++redirectCount > MAX_REDIRECTS )
{
setErrorString( "Too many redirects detected" );
finish();
return;
}
reply = mgr.get( QNetworkRequest( redirectUrl ) );
#ifndef QT_NO_OPENSSL
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ),
reply, SLOT( ignoreSslErrors() ) );
#endif
return;
}
// Handle reply data
QByteArray all = r->readAll();
Mutex::Lock _( dataMutex );
data.resize( all.size() );
memcpy( data.data(), all.data(), all.size() );
hasAnyData = true;
}
else
setErrorString( r->errorString() );
disconnect( r, 0, 0, 0 );
r->deleteLater();
reply = NULL;
finish();
}
bool WebMultimediaDownload::isAudioUrl( QUrl const & url )
{
// Note: we check for forvo sound links explicitly, as they don't have extensions
return ( url.scheme() == "http" || url.scheme() == "https" ) && (
Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" );
}
}