Exemplo n.º 1
0
// Grab the favicon.ico for each tracker to make the bug list prettier
void
MainWindow::fetchIcon(const QString &url,
                      const QString &savePath,
                      const QString &username,
                      const QString &password)
{
    QUrl u(url);
    QString fetch = "http://" + u.host() + "/favicon.ico";
    qDebug() << "fetchIcon: " << fetch;
    QUrl iconUrl(fetch);
    if (!username.isEmpty())
    {
        iconUrl.setUserName(username);
        iconUrl.setPassword(password);
    }
    QNetworkRequest req = QNetworkRequest(iconUrl);
    req.setAttribute(QNetworkRequest::User, QVariant(savePath));
    // We set this to 0 for a first request, and then to 1 if the url is redirected.
    req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User+1), QVariant(0));
    req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User+2), QVariant(username));
    req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User+3), QVariant(password));

    QNetworkReply *rep = pManager->get(req);
    connect(rep, SIGNAL(finished()),
            this, SLOT(iconDownloaded()));
}
Exemplo n.º 2
0
void GCF::IconCache::fetchIcon2(const QUrl& url)
{
    if(!url.isValid())
        return;

    d->mutex.lock();
    if(d->fetchingList.contains(url))
    {
        d->mutex.unlock();
        return;
    }
    d->fetchingList.append(url);
    d->mutex.unlock();

    GCF::AbstractFileDownloader* downloader = 0;
    downloader = GCF::AbstractFileDownloader::createDownloader( url.scheme() );
    if( !downloader )
        return; // cant help it.

    connect(downloader, SIGNAL(downloadCompleted()), this, SLOT(iconDownloaded()));
    connect(downloader, SIGNAL(downloadAborted(QString)), this, SLOT(iconDownloadAborted()));
    downloader->setAutoRemoveDownloadedFile(true);
    downloader->setUrl(url);

    GCF_UPDATER_LOG( QString("Fetching icon %1..").arg(url.toString()) );
    downloader->download();
}
Exemplo n.º 3
0
void
MainWindow::htmlIconDownloaded()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    QString url = reply->url().toString();
    QString savePath = reply->request().attribute(QNetworkRequest::User).toString();
    if (reply->error())
    {
        reply->close();
        qDebug() << "Couldn't get an icon or HTML for " << url << " so I'm giving up: " << reply->errorString();
        return;
    }

    QString html(reply->readAll());
    QRegExp reg("<link (rel=\"([^\"]+)\")?\\s*(type=\"([^\"]+)\")?\\s*(href=\"([^\"]+)\")?\\s*/?>");
    QString iconPath = "";
    // Look for the first match
    int pos = 0;
    while ((pos = reg.indexIn(html, pos)) != -1)
    {
        if (reg.cap(2).endsWith("icon"))
        {
            iconPath = reg.cap(6);
            break;
        }

        pos += reg.matchedLength();
    }

    if (iconPath.isEmpty())
    {
        qDebug() << "Couldn't find an icon in " << url;
        return;
    }

    if (!iconPath.startsWith("http"))
    {
        qDebug() << "Path was wrong, fixing";
        iconPath= "https://" + QUrl(url).host() + "/" + iconPath;
    }
    qDebug() << "Going to fetch " << iconPath;

    QNetworkRequest req = QNetworkRequest(QUrl(iconPath));
    req.setAttribute(QNetworkRequest::User, QVariant(savePath));
    QNetworkReply *rep = pManager->get(req);
    connect(rep, SIGNAL(finished()),
            this, SLOT(iconDownloaded()));
}
Exemplo n.º 4
0
void IconFetcher::pageDownloaded()
{
    FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
    if (!reply) {
        return;
    }

    QString html = reply->reply()->readAll();
    QUrl replyUrl = reply->reply()->url();
    reply->deleteLater();

    QRegExp rx("<link(.*)>", Qt::CaseInsensitive);
    rx.setMinimal(true);

    QString shortcutIconTag;
    int pos = 0;
    while ((pos = rx.indexIn(html, pos)) != -1) {
        QString linkTag = rx.cap(0);
        pos += rx.matchedLength();

        if (linkTag.contains("rel=\"shortcut icon\"", Qt::CaseInsensitive)) {
            shortcutIconTag = linkTag;
            break;
        }
    }

    FollowRedirectReply* newReply;
    if (shortcutIconTag.isEmpty()) {
//        QUrl faviconUrl = replyUrl.resolved(QUrl("favicon.ico"));
//
//        Rather getting favicon.ico from base directory than from subfolders
        QUrl faviconUrl = QUrl(replyUrl.toString(QUrl::RemovePath | QUrl::RemoveQuery) + "/favicon.ico");
        newReply = new FollowRedirectReply(faviconUrl, m_manager);
    }
    else {
        QRegExp rx("href=\"(.*)\"", Qt::CaseInsensitive);
        rx.setMinimal(true);
        rx.indexIn(shortcutIconTag);
        QUrl url = QUrl(rx.cap(1));

        QUrl iconUrl = QUrl(replyUrl).resolved(url);
        newReply = new FollowRedirectReply(iconUrl, m_manager);
    }

    connect(newReply, SIGNAL(finished()), this, SLOT(iconDownloaded()));
}
Exemplo n.º 5
0
plugPackageItem::plugPackageItem(ItemData *data)
        : parent(0)
{
    item_data = data;
    if (item_data->type==buddy&&!item_data->packageItem.properties.value("icon").isEmpty()) {
        plugDownloader *loader = new plugDownloader(plugPathes::getCachePath() + "icons/");
        downloaderItem item =
        {
            item_data->packageItem.properties.value("icon"),
            item_data->packageItem.properties.value("name")
            + "-"
            + item_data->packageItem.properties.value("version")
            + ".png" //FIXME для тестирования
        };
        loader->addItem(item);
		qDebug() << item.url << item.filename;
		connect(loader,SIGNAL(downloadFinished(QList<downloaderItem>)),SLOT(iconDownloaded(QList<downloaderItem>)));
		loader->startDownload();
    }
}
Exemplo n.º 6
0
// Callback from fetchIcon
void
MainWindow::iconDownloaded()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    QString savePath = reply->request().attribute(QNetworkRequest::User).toString();
    int wasRedirected = reply->request().attribute((QNetworkRequest::Attribute)(QNetworkRequest::User+1)).toInt();
    QString username = reply->request().attribute((QNetworkRequest::Attribute)(QNetworkRequest::User+2)).toString();
    QString password = reply->request().attribute((QNetworkRequest::Attribute)(QNetworkRequest::User+3)).toString();

    QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

    if (!redirect.toUrl().isEmpty() && !wasRedirected)
    {
        qDebug() << "Was redirected to " << redirect.toUrl();
        reply->deleteLater();
        QUrl redir = redirect.toUrl();
        if (!username.isEmpty())
        {
            redir.setUserName(username);
            redir.setPassword(password);
        }
        QNetworkRequest req = QNetworkRequest(redir);
        req.setAttribute(QNetworkRequest::User, QVariant(savePath));
        req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User+1), QVariant(1));
        QNetworkReply *rep = pManager->get(req);
        connect(rep, SIGNAL(finished()),
                this, SLOT(iconDownloaded()));
        return;
    }

    qDebug() << "Icon downloaded";
    if (reply->error())
    {
        reply->close();
        qDebug() << "Couldn't get icon";
        fetchHTMLIcon(reply->url().toString(), savePath);
        return;
    }

    QByteArray logoData = reply->readAll();
    // The favicon can be in various formats, so convert it to something
    // we know we can safely display
    QBuffer logoBuffer(&logoData);
    logoBuffer.open(QIODevice::ReadOnly);
    QImageReader reader(&logoBuffer);
    QSize iconSize(16, 16);
    if(reader.canRead())
    {
        while((reader.imageCount() > 1) && (reader.currentImageRect() != QRect(0, 0, 16, 16)))
        {
            if (!reader.jumpToNextImage())
                   break;
        }

        reader.setScaledSize(iconSize);
        const QImage icon = reader.read();
        if (icon.format() == QImage::Format_Invalid)
        {
            fetchHTMLIcon(reply->url().toString(), savePath);
        }
        else
        {
            icon.save(savePath, "PNG");
            QFileInfo info(savePath);
            int tabIndex = compareTabName(info.baseName());
            if (tabIndex != -1)
                ui->trackerTab->setTabIcon(tabIndex, QIcon(QPixmap::fromImage(icon)));
        }
    }
    else
    {
        qDebug() << "Invalid image";
        fetchHTMLIcon(reply->url().toString(), savePath);
    }

    logoBuffer.close();
    reply->close();
}