rendered paste body/////////////////////////////////////////////////////////////////////////////
/// CLASS PodcastFetcher
/// @note Allows us to download podcasts from any enclosure, even the weird ones.
////////////////////////////////////////////////////////////////////////////
PodcastFetcher::PodcastFetcher( QString url, const KURL &directory ):
m_directory ( directory )
{
m_url = QUrl( url );
setHost( m_url.host() );
redirected = false;
QObject::connect( this, SIGNAL( responseHeaderReceived ( const QHttpResponseHeader & ) ), this,
SLOT( responseReceived( const QHttpResponseHeader & ) ) );
QObject::connect(this, SIGNAL(done(bool)), this, SLOT(done(bool)));
fetch( );
}
void PodcastFetcher::fetch()
{
DEBUG_BLOCK
KURL filepath = m_directory;
debug() << "filename = " << m_url.fileName() << endl;
filepath.addPath( m_url.fileName() );
m_file = new QFile( filepath.path() );
if( m_file != 0 )
debug() << "QFile = " << m_file->name() << endl;
else
debug() << "BUG: m_file == NULL" << endl;
if ( m_file->exists() )
{
emit result( 0 );
debug() << m_file->name() << " exists, aborting download" << endl;
return;
}
debug() << " host = " << m_url.host() << endl;
setHost( m_url.host() );
debug() << "get( " << m_url.encodedPathAndQuery() << " )" << endl;
get( m_url.encodedPathAndQuery(), m_file );
if (error() )
debug() << errorString() << endl;
}
void PodcastFetcher::kill()
{
abort();
clearPendingRequests();
closeConnection();
if( m_file && m_file->exists() )
m_file->remove();
}
void PodcastFetcher::responseReceived( const QHttpResponseHeader & resp )
{
DEBUG_BLOCK
debug() << "RESPONCE, statuscode = " << resp.statusCode() << endl;
if( resp.statusCode() == 302 )
{
if (resp.hasKey( "location" ) )
{
m_url = QUrl( resp.value( "location" ) );
debug() << "redirected to " << m_url.toString( ) <<endl;
if ( m_file )
{
if ( m_file->remove() ) delete m_file;
else debug() << "BUG in PodcastFetcher::responceReceived(): could not remove m_file " << m_file->name() << endl;
}
redirected = true;
}
} else if (resp.statusCode() == 200 )
{
debug() << "filename = " << m_url.fileName() << endl;
}
}
void PodcastFetcher::done( bool done )
{
DEBUG_BLOCK
if (redirected)
{
redirected = false;
fetch();
}
else
{
debug() << "downloaded to " << m_file->name() << endl;
emit result( error() );
}
}