All pastes #70587 Raw Edit

Someone

public text v1 · immutable
#70587 ·published 2006-06-24 14:24 UTC
rendered paste body
/////////////////////////////////////////////////////////////////////////////
///    CLASS PodcastFetcher
///    @note Allows us to download podcasts from any enclosure, even the weird ones, with decent filenames..
////////////////////////////////////////////////////////////////////////////

PodcastFetcher::PodcastFetcher( QString url, const KURL &directory ):
        m_directory ( directory ),
        m_url( QUrl( url ))
{
    
    m_http = new QHttp( m_url.host() );
    m_redirected = false;
    QObject::connect(m_http, SIGNAL( responseHeaderReceived ( const QHttpResponseHeader & ) ), this,
                      SLOT( slotResponseReceived( const QHttpResponseHeader & ) ) );
    QObject::connect(m_http, SIGNAL(done( bool )), this, SLOT(slotDone( 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() << "get( http://"<< m_url.host() << m_url.encodedPathAndQuery() << " )" << endl;
    m_http->get( m_url.encodedPathAndQuery(), m_file );
    if ( m_http->error() )
        debug() <<  m_http->errorString() << endl;
}

void PodcastFetcher::kill()
{
    m_http->abort();
    m_http->clearPendingRequests();
    m_http->closeConnection();
    if( m_file && m_file->exists() )
        m_file->remove();
}

void PodcastFetcher::slotResponseReceived( const QHttpResponseHeader & resp )
{
    DEBUG_BLOCK
    debug() << "RESPONCE, statuscode = " << resp.statusCode() << endl;
    if( resp.statusCode() == 302 )
    {
        if (resp.hasKey( "location" ) )
        {
            QString oldHost = m_url.host();
            m_url = QUrl( resp.value( "location" ) );
            if( m_url.host() != oldHost )
                m_http->setHost( m_url.host() );

            debug() << "m_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;
            }
            m_redirected = true;
        }
    } else if (resp.statusCode() == 200 )
    {
        debug() << "filename = " << m_url.fileName() << endl;
    }
}

void PodcastFetcher::slotDone( bool error )
{
    DEBUG_BLOCK
    if ( m_redirected )
    {
        m_redirected = false;
        fetch();
    }
    else if ( !error )
    {
        debug() << "downloaded to " << m_file->name() << endl;
    }
    else debug() << "ERROR: " << " errorstring = " << m_http->errorString() << endl;
    emit result( m_http->error() );
}