예제 #1
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()));
}
예제 #2
0
void IconFetcher::iconDownloaded()
{
    FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
    if (!reply) {
        return;
    }

    QByteArray response = reply->reply()->readAll();
    reply->deleteLater();

    if (!response.isEmpty()) {
        QImage image;
        image.loadFromData(response);
        QIcon icon = QIcon(QPixmap::fromImage(image));
        if (!icon.isNull()) {
            emit iconFetched(icon);
        }
    }

    emit finished();
}
예제 #3
0
void RSSManager::finished()
{
    FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
    if (!reply) {
        return;
    }

    QString replyUrl;
    for (int i = 0; i < m_replies.count(); i++) {
        QPair<FollowRedirectReply*, QUrl> pair = m_replies.at(i);
        if (pair.first == reply) {
            replyUrl = pair.second.toString();
            break;
        }
    }

    if (replyUrl.isEmpty()) {
        return;
    }

    QString currentTag;
    QString linkString;
    QString titleString;

    QXmlStreamReader xml;
    xml.addData(reply->readAll());

    reply->deleteLater();

    int tabIndex = -1;
    for (int i = 0; i < ui->tabWidget->count(); i++) {
        if (replyUrl == ui->tabWidget->tabToolTip(i)) {
            tabIndex = i;
            break;
        }
    }

    if (tabIndex == -1) {
        return;
    }

    TreeWidget* treeWidget = qobject_cast<TreeWidget*>(ui->tabWidget->widget(tabIndex));
    if (!treeWidget) {
        return;
    }
    treeWidget->clear();

    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (xml.name() == QLatin1String("item")) {
                linkString = xml.attributes().value("rss:about").toString();
            }
            currentTag = xml.qualifiedName().toString();
        }
        else if (xml.isEndElement()) {
            if (xml.qualifiedName() == QLatin1String("item")) {
                QTreeWidgetItem* item = new QTreeWidgetItem;
                item->setText(0, titleString);
                item->setIcon(0, QIcon(":/icons/other/feed.png"));
                item->setToolTip(0, linkString);
                treeWidget->addTopLevelItem(item);

                titleString.clear();
                linkString.clear();
            }
        }
        else if (xml.isCharacters() && !xml.isWhitespace()) {
            if (currentTag == QLatin1String("title")) {
                titleString = xml.text().toString();
            }
            else if (currentTag == QLatin1String("link")) {
                linkString += xml.text().toString();
            }
        }
    }

    if (treeWidget->topLevelItemCount() == 0) {
        QTreeWidgetItem* item = new QTreeWidgetItem;
        item->setText(0, tr("Error in fetching feed"));
        treeWidget->addTopLevelItem(item);
    }
}