void ShareProvider::onTransferJobResultReceived(KJob* job)
{
    if (d->m_data.size() == 0) {
        Q_EMIT finishedError(this, i18n("Service was not available"));
        return;
    }

    KIO::TransferJob *tfJob = qobject_cast<KIO::TransferJob*>(job);
    if (tfJob) {
        QString mimeType = tfJob->mimetype();
        AbstractSharer *sharer = d->getSharer();

        if (sharer) {
            sharer->parseResponse(d->m_data);
            if (tfJob->isErrorPage() || sharer->hasError()) {
                QString errorMessage = sharer->errorMessage();
                if (!errorMessage.isEmpty()) {
                    Q_EMIT finishedError(this, errorMessage);
                } else {
                    Q_EMIT finishedError(this, tfJob->errorString());
                }
            } else {
                Q_EMIT finishedSuccess(this, sharer->imageUrl().url());
            }
        }
    }
}
void HttpContainer::fetchFinished(KJob *job)
{
    if (!job->error()) {
        // We now set the data on the source with the retrieved data and some
        // additional stats. Note that we don't include the source name, as that
        // is implied as this object *is* the DataContainer. setData is called
        // with just key/value pairs.
        setData("Contents", m_data);
        setData("Size", job->processedAmount(KJob::Bytes));

        // Since we only create TransferJobs, it's safe to just static_cast here.
        // In many real-world situations, this isn't the safest thing to do and a
        // qobject_cast with a test on the result is often safer and cleaner.
        KIO::TransferJob *tjob = static_cast<KIO::TransferJob *>(job);
        setData("Error Page", tjob->isErrorPage());
        setData("Mimetype", tjob->mimetype());

        // Let DataContainer know we have data that needs storing
        setNeedsToBeStored(true);

        // Notify DataContainer that now is a good time to check to see that
        // data has been updated. This will cause visualizations to be updated.
        checkForUpdate();

        // Clean up behind ourselves so there isn't unecessary memory usage
        m_data.clear();
    }
}
void KRun::slotJobData( KIO::Job *job, const QByteArray &data )
{
    // Are we pumping data for an external program?
    if ( m_fhandle != -1 )
    {
	// This should *REALLY* be implemented in another thread or process
	if ( data.isEmpty() )
	{
	    emit error();
	    delete this;
	}
	else
	    for ( uint d = 0; d < data.count(); )
	    {
		int n = data.count() - d;
		if ( n > 1024 )
		    n = 1024;
		n = ::write( m_fhandle, data.data() + d, n );
		if ( n >= 0 )
		    d += n;
		else
		{
		    if ( errno == EAGAIN )
			usleep(5000);
		    else
		    {
			delete this;
			break;
		    }
		}
	    }
	return;
    }
    assert( job->inherits( "KIO::TransferJob" ) );
    assert( job == m_job );

    KIO::TransferJob *transferJob = static_cast<KIO::TransferJob *>( job );

    QString referrer;
    {
	if ( transferJob->outgoingMetaData().contains( "referrer" ) )
	    referrer = transferJob->outgoingMetaData()["referrer"];
    }
    QString mtype = transferJob->mimetype();

    // if no MIME type is available attempt to guess it
    if ( mtype.isEmpty() )
    {
	const KIO::MimeHandler* guess = KIO::MimeHandler::Find( m_strURL );
	if ( !guess )
	    guess = KIO::MimeHandler::Find( data );

	if ( guess )
	    mtype = guess->Preferred();
	else
	    mtype = QString::fromLatin1( "text/html" );

    }

    // If app wants us to pump it data don't detach from job
    const KIO::MimeHandler* mime = KIO::MimeHandler::Find( mtype );
    if ( mime && mime->isExtApp() && mime->isExtApp()->wantsData() )
    {
	m_fhandle = exec( ( static_cast<const KIO::MimeExtApp *>( mime ) )->getExtApp(), m_strURL.url(), referrer, true );
	if ( m_fhandle >= 0 )
	{
	    // Since we already got some data call itself once
	    slotJobData( job, data );
	    return;
	}
    }

    transferJob->detach( data );

    job->disconnect( this, 0 );

    KURL url = transferJob->url();
    m_strURL = url;

    if ( mime && mime->isExtApp() )
    {
	job->kill();
	exec( mime->isExtApp()->getExtApp(), m_strURL.url(), referrer );
	emit error();
    }
    else if ( mime && mime->isUnknown() )
    {
	job->kill();
	emit error();
    }
    else
	foundMimeType( mtype );

    QTimer::singleShot( 0, this, SLOT( slotSuicide() ) );
}