Ejemplo n.º 1
0
 void _k_contentTypeCheckFailed(KJob* job)
 {
     KIO::TransferJob* tJob = qobject_cast<KIO::TransferJob*>(job);
     // On error simply call downloadResource which will probably fail as well.
     if (tJob && tJob->error()) {
         (void)downloadResource(tJob->url(), QString(), window, tJob->metaData());
     }
 }
Ejemplo n.º 2
0
void FavIconRequestJobPrivate::slotData(Job *job, const QByteArray &data)
{
    KIO::TransferJob *tjob = static_cast<KIO::TransferJob *>(job);
    unsigned int oldSize = m_iconData.size();
    // Size limit. Stop downloading if the file is huge.
    // Testcase (as of june 2008, at least): http://planet-soc.com/favicon.ico, 136K and strange format.
    // Another case: sites which redirect from "/favicon.ico" to "/" and return the main page.
    if (oldSize > 0x10000) { // 65K
        qCDebug(FAVICONS_LOG) << "Favicon too big, aborting download of" << tjob->url();
        const QUrl iconUrl = tjob->url();
        KIO::FavIconsCache::instance()->addFailedDownload(iconUrl);
        tjob->kill(KJob::EmitResult);
    } else {
        m_iconData.resize(oldSize + data.size());
        memcpy(m_iconData.data() + oldSize, data.data(), data.size());
    }
}
Ejemplo n.º 3
0
void FavIconRequestJob::slotResult(KJob *job)
{
    KIO::TransferJob *tjob = static_cast<KIO::TransferJob *>(job);
    const QUrl iconUrl = tjob->url();
    KIO::FavIconsCache *cache = KIO::FavIconsCache::instance();
    if (!job->error()) {
        QBuffer buffer(&d->m_iconData);
        buffer.open(QIODevice::ReadOnly);
        QImageReader ir(&buffer);
        QSize desired(16, 16);
        if (ir.canRead()) {
            while (ir.imageCount() > 1
                    && ir.currentImageRect() != QRect(0, 0, desired.width(), desired.height())) {
                if (!ir.jumpToNextImage()) {
                    break;
                }
            }
            ir.setScaledSize(desired);
            const QImage img = ir.read();
            if (!img.isNull()) {
                cache->ensureCacheExists();
                const QString localPath = cache->cachePathForIconUrl(iconUrl);
                qCDebug(FAVICONS_LOG) << "Saving image to" << localPath;
                QSaveFile saveFile(localPath);
                if (saveFile.open(QIODevice::WriteOnly) &&
                        img.save(&saveFile, "PNG") && saveFile.commit()) {
                    d->m_iconFile = localPath;
                } else {
                    setError(KIO::ERR_COULD_NOT_WRITE);
                    setErrorText(i18n("Error saving image to %1", localPath));
                }
            } else {
                qCDebug(FAVICONS_LOG) << "QImageReader read() returned a null image";
            }
        } else {
            qCDebug(FAVICONS_LOG) << "QImageReader canRead returned false";
        }
    } else if (job->error() == KJob::KilledJobError) { // we killed it in slotData
        setError(KIO::ERR_SLAVE_DEFINED);
        setErrorText(i18n("Icon file too big, download aborted"));
    } else {
        setError(job->error());
        setErrorText(job->errorString()); // not errorText(), because "this" is a KJob, with no errorString building logic
    }
    d->m_iconData.clear(); // release memory
    if (d->m_iconFile.isEmpty()) {
        qCDebug(FAVICONS_LOG) << "adding" << iconUrl << "to failed downloads due to error:" << errorString();
        cache->addFailedDownload(iconUrl);
    } else {
        cache->removeFailedDownload(iconUrl);
    }
    KCompositeJob::removeSubjob(job);
    emitResult();
}
Ejemplo n.º 4
0
 void _k_receivedContentType(KIO::Job* job, const QString& mimetype)
 {
     KIO::TransferJob* tJob = qobject_cast<KIO::TransferJob*>(job);
     if (tJob && !tJob->error()) {
         tJob->putOnHold();
         KIO::Scheduler::publishSlaveOnHold();
         // Get suggested file name...
         mimeType = mimetype;
         const QString suggestedFileName (tJob->queryMetaData(QL1S("content-disposition-filename")));
         // kDebug(800) << "suggested filename:" << suggestedFileName << ", mimetype:" << mimetype;
         (void) downloadResource(tJob->url(), suggestedFileName, window, tJob->metaData());
     }
 }
Ejemplo n.º 5
0
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() ) );
}