Exemplo n.º 1
0
void UltimateLyricsProvider::lyricsFetched()
{
    NetworkJob *reply = qobject_cast<NetworkJob*>(sender());
    if (!reply) {
        return;
    }

    int id = requests.take(reply);
    reply->deleteLater();
    Song song=songs.take(id);

    if (!reply->ok()) {
        //emit Finished(id);
        emit lyricsReady(id, QString());
        return;
    }

    const QTextCodec *codec = QTextCodec::codecForName(charset.toLatin1().constData());
    const QString originalContent = codec->toUnicode(reply->readAll()).replace("<br />", "<br/>");

    DBUG << name << "response" << originalContent;
    // Check for invalid indicators
    for (const QString &indicator: invalidIndicators) {
        if (originalContent.contains(indicator)) {
            //emit Finished(id);
            DBUG << name << "invalid";
            emit lyricsReady(id, QString());
            return;
        }
    }

    QString lyrics;

    // Apply extract rules
    for (const Rule& rule: extractRules) {
        QString content = originalContent;
        applyExtractRule(rule, content, song);
        #ifndef Q_OS_WIN
        content.replace(QLatin1String("\r"), QLatin1String(""));
        #endif
        content=content.trimmed();

        if (!content.isEmpty()) {
            lyrics = content;
            break;
        }
    }

    // Apply exclude rules
    for (const Rule& rule: excludeRules) {
        applyExcludeRule(rule, lyrics, song);
    }

    lyrics=lyrics.trimmed();
    lyrics.replace("<br/>\n", "<br/>");
    lyrics.replace("<br>\n", "<br/>");
    DBUG << name << (lyrics.isEmpty() ? "empty" : "succeeded");
    emit lyricsReady(id, lyrics);
}
int NetworkManager::startJob(int playerId, const String& pageGroupName, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, BlackBerry::Platform::NetworkStreamFactory* streamFactory, Frame* frame, int deferLoadingCount, int redirectCount, bool rereadCookies)
{
    // Make sure the ResourceHandle doesn't go out of scope while calling callbacks.
    RefPtr<ResourceHandle> guardJob(job);

    KURL url = request.url();

    // Only load the initial url once.
    bool isInitial = (url == m_initialURL);
    if (isInitial)
        m_initialURL = KURL();

    // Always reread cookies on a redirect
    if (redirectCount)
        rereadCookies = true;

    BlackBerry::Platform::NetworkRequest platformRequest;
    request.initializePlatformRequest(platformRequest, frame->loader() && frame->loader()->client() && static_cast<FrameLoaderClientBlackBerry*>(frame->loader()->client())->cookiesEnabled(), isInitial, rereadCookies);

    // GURL and KURL consider valid URLs differently, for example http:// is parsed as
    // http:/ by KURL and considered valid, while GURL considers it invalid.
    if (!platformRequest.url().is_valid())
        return BlackBerry::Platform::FilterStream::StatusErrorInvalidUrl;

    const String& documentUrl = frame->document()->url().string();
    if (!documentUrl.isEmpty())
        platformRequest.setReferrer(documentUrl);

    platformRequest.setSecurityOrigin(frame->document()->securityOrigin()->toRawString());

    // Attach any applicable auth credentials to the NetworkRequest.
    setAuthCredentials(platformRequest, guardJob->getInternal()->m_hostWebChallenge);
    setAuthCredentials(platformRequest, guardJob->getInternal()->m_proxyWebChallenge);

    if (!request.overrideContentType().isEmpty())
        platformRequest.setOverrideContentType(request.overrideContentType());

    NetworkJob* networkJob = new NetworkJob;
    networkJob->initialize(playerId, pageGroupName, url, platformRequest, guardJob, streamFactory, frame, deferLoadingCount, redirectCount);

    // Make sure we have only one NetworkJob for one ResourceHandle.
    ASSERT(!findJobForHandle(guardJob));

    m_jobs.append(networkJob);

    switch (networkJob->streamOpen()) {
    case BlackBerry::Platform::FilterStream::ResultOk:
        return BlackBerry::Platform::FilterStream::StatusSuccess;
    case BlackBerry::Platform::FilterStream::ResultNotReady:
        return BlackBerry::Platform::FilterStream::StatusErrorNotReady;
    case BlackBerry::Platform::FilterStream::ResultNotHandled:
    default:
        // This should never happen.
        break;
    }

    ASSERT_NOT_REACHED();
    return BlackBerry::Platform::FilterStream::StatusErrorConnectionFailed;
}
NetworkJob* NetworkManager::findJobForHandle(PassRefPtr<ResourceHandle> job)
{
    for (unsigned i = 0; i < m_jobs.size(); ++i) {
        NetworkJob* networkJob = m_jobs[i];
        // We have only one job for one handle (not including cancelled jobs which may hang
        // around briefly), so return the first non-cancelled job.
        if (!networkJob->isCancelled() && networkJob->handle() == job)
            return networkJob;
    }
    return 0;
}
Exemplo n.º 4
0
void UltimateLyricsProvider::wikiMediaLyricsFetched()
{
    NetworkJob *reply = qobject_cast<NetworkJob*>(sender());
    if (!reply) {
        return;
    }

    int id = requests.take(reply);
    reply->deleteLater();

    if (!reply->ok()) {
        emit lyricsReady(id, QString());
        return;
    }

    const QTextCodec *codec = QTextCodec::codecForName(charset.toLatin1().constData());
    QString contents = codec->toUnicode(reply->readAll()).replace("<br />", "<br/>");
    DBUG << name << "response" << contents;
    emit lyricsReady(id, extract(contents, QLatin1String("&lt;lyrics&gt;"), QLatin1String("&lt;/lyrics&gt;")));
}
Exemplo n.º 5
0
void ArtistView::handleSimilarReply()
{
    NetworkJob *reply = qobject_cast<NetworkJob*>(sender());
    if (!reply) {
        return;
    }
    if (reply==currentSimilarJob) {
        if (reply->ok()) {
            QByteArray data=reply->readAll();
            QStringList artists=parseSimilarResponse(data);
            if (!artists.isEmpty()) {
                buildSimilar(artists);
                setBio();
                QFile f(cacheFileName(reply->property(constNameKey).toString(), QString(), true, true));
                if (f.open(QIODevice::WriteOnly|QIODevice::Text)) {
                    QTextStream stream(&f);
                    foreach (const QString &artist, artists) {
                        stream << artist << endl;
                    }
                }
            }
Exemplo n.º 6
0
void WikipediaSettings::parseLangs()
{
    NetworkJob *reply = qobject_cast<NetworkJob*>(sender());
    if (!reply) {
        return;
    }
    reload->setEnabled(true);
    reply->deleteLater();
    if (reply!=job) {
        return;
    }
    job=0;
    QByteArray data=reply->readAll();
    parseLangs(data);
    QFile f(localeFile());
    QtIOCompressor compressor(&f);
    compressor.setStreamFormat(QtIOCompressor::GzipFormat);
    if (compressor.open(QIODevice::WriteOnly)) {
        compressor.write(data);
    }
}
Exemplo n.º 7
0
void UltimateLyricsProvider::wikiMediaSearchResponse()
{
    NetworkJob *reply = qobject_cast<NetworkJob*>(sender());
    if (!reply) {
        return;
    }

    int id = requests.take(reply);
    reply->deleteLater();

    if (!reply->ok()) {
        emit lyricsReady(id, QString());
        return;
    }

    QUrl url;
    QXmlStreamReader doc(reply->actualJob());
    while (!doc.atEnd()) {
        doc.readNext();
        if (doc.isStartElement() && QLatin1String("url")==doc.name()) {
            QString lyricsUrl=doc.readElementText();
            if (!lyricsUrl.contains(QLatin1String("action=edit"))) {
                url=QUrl::fromEncoded(lyricsUrl.toUtf8()).toString();
            }
            break;
        }
    }

    if (url.isValid()) {
        QString path=url.path();
        QByteArray u=url.scheme().toLatin1()+"://"+url.host().toLatin1()+"/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=";
        QByteArray titles=QUrl::toPercentEncoding(path.startsWith(QLatin1Char('/')) ? path.mid(1) : path).replace('+', "%2b");
        NetworkJob *reply = NetworkAccessManager::self()->get(QUrl::fromEncoded(u+titles));
        requests[reply] = id;
        connect(reply, SIGNAL(finished()), this, SLOT(wikiMediaLyricsFetched()));
    } else {
        emit lyricsReady(id, QString());
    }
}
BlackBerry::Platform::FilterStream* NetworkManager::streamForHandle(PassRefPtr<ResourceHandle> job)
{
    NetworkJob* networkJob = findJobForHandle(job);
    return networkJob ? networkJob->wrappedStream() : 0;
}
Exemplo n.º 9
0
bool NetworkManager::startJob(int playerId, const String& pageGroupName, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, BlackBerry::Platform::NetworkStreamFactory* streamFactory, Frame* frame, int deferLoadingCount, int redirectCount)
{
    // Make sure the ResourceHandle doesn't go out of scope while calling callbacks.
    RefPtr<ResourceHandle> guardJob(job);

    KURL url = request.url();

    // Only load the initial url once.
    bool isInitial = (url == m_initialURL);
    if (isInitial)
        m_initialURL = KURL();

    BlackBerry::Platform::NetworkRequest platformRequest;
    request.initializePlatformRequest(platformRequest, frame->loader() && frame->loader()->client() && static_cast<FrameLoaderClientBlackBerry*>(frame->loader()->client())->cookiesEnabled(), isInitial, redirectCount);

    const String& documentUrl = frame->document()->url().string();
    if (!documentUrl.isEmpty()) {
        platformRequest.setReferrer(documentUrl);
    }

    platformRequest.setSecurityOrigin(frame->document()->securityOrigin()->toRawString());

    // Attach any applicable auth credentials to the NetworkRequest.
    AuthenticationChallenge& challenge = guardJob->getInternal()->m_currentWebChallenge;
    if (!challenge.isNull()) {
        Credential credential = challenge.proposedCredential();
        const ProtectionSpace& protectionSpace = challenge.protectionSpace();

        String username = credential.user();
        String password = credential.password();

        BlackBerry::Platform::NetworkRequest::AuthType authType = BlackBerry::Platform::NetworkRequest::AuthTypeNone;
        switch (protectionSpace.serverType()) {
        case ProtectionSpaceServerHTTP:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeHTTP;
            break;
        case ProtectionSpaceServerHTTPS:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeHTTPS;
            break;
        case ProtectionSpaceServerFTP:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeFTP;
            break;
        case ProtectionSpaceServerFTPS:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeFTPS;
            break;
        case ProtectionSpaceProxyHTTP:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeProxyHTTP;
            break;
        case ProtectionSpaceProxyHTTPS:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeProxyHTTPS;
            break;
        case ProtectionSpaceProxyFTP:
            authType = BlackBerry::Platform::NetworkRequest::AuthTypeProxyFTP;
            break;
        default:
            ASSERT_NOT_REACHED();
            break;
        }

        BlackBerry::Platform::NetworkRequest::AuthScheme authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeNone;
        switch (protectionSpace.authenticationScheme()) {
        case ProtectionSpaceAuthenticationSchemeDefault:
            authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeDefault;
            break;
        case ProtectionSpaceAuthenticationSchemeHTTPBasic:
            authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeHTTPBasic;
            break;
        case ProtectionSpaceAuthenticationSchemeHTTPDigest:
            authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeHTTPDigest;
            break;
        case ProtectionSpaceAuthenticationSchemeNegotiate:
            authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeNegotiate;
            break;
        case ProtectionSpaceAuthenticationSchemeNTLM:
            authScheme = BlackBerry::Platform::NetworkRequest::AuthSchemeNTLM;
            break;
        default:
            ASSERT_NOT_REACHED();
            break;
        }

        if (authType != BlackBerry::Platform::NetworkRequest::AuthTypeNone && authScheme != BlackBerry::Platform::NetworkRequest::AuthSchemeNone)
            platformRequest.setCredentials(username.utf8().data(), password.utf8().data(), authType, authScheme);
    }

    if (!request.overrideContentType().isEmpty())
        platformRequest.setOverrideContentType(request.overrideContentType().latin1().data());

    NetworkJob* networkJob = new NetworkJob;
    if (!networkJob)
        return false;
    if (!networkJob->initialize(playerId, pageGroupName, url, platformRequest, guardJob, streamFactory, frame, deferLoadingCount, redirectCount)) {
        delete networkJob;
        return false;
    }

    // Make sure we have only one NetworkJob for one ResourceHandle.
    ASSERT(!findJobForHandle(guardJob));

    m_jobs.append(networkJob);

    int result = networkJob->streamOpen();
    if (result)
        return false;

    return true;
}