1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Rewritten icon fetching in bookmarks importing, closes #24

Instead of using QWebPage and then loading the whole page in order to
get icon, it is now using own IconFetcher class.
It simply download page, looks for <link rel=shortcut and downloads icon
from here. If no <link rel=shortcut is present, the downloading standard
favicon.ico from base directory.
This commit is contained in:
nowrep 2011-10-28 23:17:38 +02:00
parent ed8969f7a9
commit 1b7c01bec2
13 changed files with 247 additions and 45 deletions

Binary file not shown.

View File

@ -186,6 +186,11 @@
background: transparent;
}
#sidebar-splitter::handle
{
background-color:transparent;
}
/*WebSearchBar*/
#websearchbar
{

View File

@ -225,6 +225,11 @@
background: transparent;
}
#sidebar-splitter::handle
{
background-color:transparent;
}
/*SourceViewer*/
#sourceviewer-textedit
{

View File

@ -182,6 +182,11 @@
background: transparent;
}
#sidebar-splitter::handle
{
background-color:transparent;
}
/*WebSearchBar*/
#websearchbar
{

View File

@ -191,6 +191,11 @@
background: transparent;
}
#sidebar-splitter::handle
{
background-color:transparent;
}
/*WebSearchBar*/
#websearchbar
{

View File

@ -164,7 +164,8 @@ SOURCES += main.cpp\
bookmarksimport/firefoximporter.cpp \
bookmarksimport/chromeimporter.cpp \
bookmarksimport/operaimporter.cpp \
bookmarksimport/bookmarksimportdialog.cpp
bookmarksimport/bookmarksimportdialog.cpp \
tools/iconfetcher.cpp
HEADERS += \
3rdparty/qtwin.h \
@ -275,7 +276,8 @@ HEADERS += \
bookmarksimport/firefoximporter.h \
bookmarksimport/chromeimporter.h \
bookmarksimport/operaimporter.h \
bookmarksimport/bookmarksimportdialog.h
bookmarksimport/bookmarksimportdialog.h \
tools/iconfetcher.h
FORMS += \
preferences/autofillmanager.ui \
@ -404,6 +406,8 @@ message($$DEFINES)

View File

@ -190,6 +190,7 @@ void QupZilla::setupUi()
m_mainLayout->setContentsMargins(0,0,0,0);
m_mainLayout->setSpacing(0);
m_mainSplitter = new QSplitter(this);
m_mainSplitter->setObjectName("sidebar-splitter");
m_mainSplitter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
m_tabWidget = new TabWidget(this);
m_superMenu = new QMenu(this);

View File

@ -252,7 +252,7 @@ bool BookmarksModel::createFolder(const QString &name)
if (query.next())
return false;
query.prepare("INSERT INTO folders (name) VALUES (?)");
query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'no')");
query.bindValue(0, name);
if (!query.exec())
return false;

View File

@ -4,6 +4,8 @@
#include "chromeimporter.h"
#include "operaimporter.h"
#include "mainapplication.h"
#include "iconfetcher.h"
#include "networkmanager.h"
BookmarksImportDialog::BookmarksImportDialog(QWidget* parent)
: QDialog(parent)
@ -80,21 +82,23 @@ void BookmarksImportDialog::startFetchingIcons()
ui->treeWidget->addTopLevelItem(item);
i++;
QWebPage* page = new QWebPage();
QWebFrame* frame = page->mainFrame();
frame->load(b.url);
connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished()));
connect(frame, SIGNAL(iconChanged()), this, SLOT(iconChanged()));
QPair<QWebFrame*, QUrl> pair;
pair.first = frame;
IconFetcher* fetcher = new IconFetcher(this);
fetcher->setNetworkAccessManager(mApp->networkManager());
connect(fetcher, SIGNAL(finished()), this, SLOT(loadFinished()));
connect(fetcher, SIGNAL(iconFetched(QIcon)), this, SLOT(iconFetched(QIcon)));
fetcher->fetchIcon(b.url);
QPair<IconFetcher*, QUrl> pair;
pair.first = fetcher;
pair.second = b.url;
m_webViews.append(pair);
m_fetchers.append(pair);
}
}
void BookmarksImportDialog::stopDownloading()
{
ui->nextButton->setEnabled(true);
ui->stopButton->hide();
ui->progressBar->setValue(ui->progressBar->maximum());
}
@ -102,20 +106,22 @@ void BookmarksImportDialog::loadFinished()
{
ui->progressBar->setValue(ui->progressBar->value() + 1);
if (ui->progressBar->value() == ui->progressBar->maximum())
if (ui->progressBar->value() == ui->progressBar->maximum()) {
ui->stopButton->hide();
ui->nextButton->setEnabled(true);
}
}
void BookmarksImportDialog::iconChanged()
void BookmarksImportDialog::iconFetched(const QIcon &icon)
{
QWebFrame* view = qobject_cast<QWebFrame*>(sender());
if (!view)
IconFetcher* fetcher = qobject_cast<IconFetcher*>(sender());
if (!fetcher)
return;
QUrl url;
for (int i = 0; i < m_webViews.count(); i++) {
QPair<QWebFrame*, QUrl> pair = m_webViews.at(i);
if (pair.first == view) {
for (int i = 0; i < m_fetchers.count(); i++) {
QPair<IconFetcher*, QUrl> pair = m_fetchers.at(i);
if (pair.first == fetcher) {
url = pair.second;
break;
}
@ -128,19 +134,19 @@ void BookmarksImportDialog::iconChanged()
if (items.count() == 0)
return;
QTreeWidgetItem* item = items.at(0);
item->setIcon(0, view->icon());
foreach (QTreeWidgetItem* item, items) {
item->setIcon(0, icon);
foreach (BookmarksModel::Bookmark b, m_exportedBookmarks) {
if (b.url == url) {
m_exportedBookmarks.removeOne(b);
b.icon = view->icon();
b.icon = icon;
m_exportedBookmarks.append(b);
break;
}
}
}
}
bool BookmarksImportDialog::exportedOK()
{
@ -280,10 +286,10 @@ void BookmarksImportDialog::setupBrowser(Browser browser)
BookmarksImportDialog::~BookmarksImportDialog()
{
if (m_webViews.count() > 0) {
for (int i = 0; i < m_webViews.count(); i++) {tr("");
QWebFrame* frame= m_webViews.at(i).first;
delete frame->page();
if (m_fetchers.count() > 0) {
for (int i = 0; i < m_fetchers.count(); i++) {tr("");
IconFetcher* fetcher = m_fetchers.at(i).first;
delete fetcher;
}
}

View File

@ -15,6 +15,7 @@ namespace Ui {
class BookmarksImportDialog;
}
class IconFetcher;
class BookmarksImportDialog : public QDialog
{
Q_OBJECT
@ -27,9 +28,8 @@ private slots:
void nextPage();
void setFile();
void stopDownloading();
void iconChanged();
void iconFetched(const QIcon &icon);
void loadFinished();
private:
@ -54,7 +54,7 @@ private:
QList<BookmarksModel::Bookmark> m_exportedBookmarks;
QList<QPair<QWebFrame*, QUrl> > m_webViews;
QList<QPair<IconFetcher*, QUrl> > m_fetchers;
};
#endif // BOOKMARKSIMPORTDIALOG_H

116
src/tools/iconfetcher.cpp Normal file
View File

@ -0,0 +1,116 @@
#include "iconfetcher.h"
#include <QDebug>
FollowRedirectReply::FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager)
: QObject()
, m_manager(manager)
, m_redirectCount(0)
{
m_reply = m_manager->get(QNetworkRequest(url));
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void FollowRedirectReply::replyFinished()
{
int replyStatus = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if ( (replyStatus != 301 && replyStatus != 302) || m_redirectCount == 5) {
emit finished();
return;
}
m_redirectCount++;
QUrl redirectUrl = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
m_reply->close();
m_reply->deleteLater();
m_reply = m_manager->get(QNetworkRequest(redirectUrl));
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
FollowRedirectReply::~FollowRedirectReply()
{
m_reply->close();
m_reply->deleteLater();
}
IconFetcher::IconFetcher(QObject* parent)
: QObject(parent)
, m_manager(0)
{
}
void IconFetcher::fetchIcon(const QUrl &url)
{
if (!m_manager)
return;
FollowRedirectReply* reply = new FollowRedirectReply(url, m_manager);
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
}
void IconFetcher::pageDownloaded()
{
FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
if (!reply)
return;
QString html = reply->reply()->readAll();
QUrl replyUrl = reply->reply()->url();
delete reply;
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()));
}
void IconFetcher::iconDownloaded()
{
FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
if (!reply)
return;
QByteArray response = reply->reply()->readAll();
delete reply;
if (!response.isEmpty()) {
QImage image;
image.loadFromData(response);
QIcon icon = QIcon(QPixmap::fromImage(image));
if (!icon.isNull())
emit iconFetched(icon);
}
emit finished();
}

55
src/tools/iconfetcher.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef ICONFETCHER_H
#define ICONFETCHER_H
#include <QObject>
#include <QIcon>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class FollowRedirectReply : public QObject
{
Q_OBJECT
public:
explicit FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager);
~FollowRedirectReply();
QNetworkReply* reply() { return m_reply; }
signals:
void finished();
private slots:
void replyFinished();
private:
QNetworkAccessManager* m_manager;
QNetworkReply* m_reply;
int m_redirectCount;
};
class IconFetcher : public QObject
{
Q_OBJECT
public:
explicit IconFetcher(QObject* parent = 0);
void setNetworkAccessManager(QNetworkAccessManager* manager) { m_manager = manager; }
void fetchIcon(const QUrl &url);
signals:
void iconFetched(QIcon);
void finished();
public slots:
private slots:
void pageDownloaded();
void iconDownloaded();
private:
QNetworkAccessManager* m_manager;
};
#endif // ICONFETCHER_H

View File

@ -481,7 +481,7 @@ p, li { white-space: pre-wrap; }
<location filename="../src/bookmarks/bookmarksmanager.ui" line="83"/>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="114"/>
<source>Rename Folder</source>
<translation type="unfinished"></translation>
<translation>Premenovať priečinok</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarksmanager.ui" line="90"/>
@ -506,7 +506,7 @@ p, li { white-space: pre-wrap; }
<message>
<location filename="../src/bookmarks/bookmarksmanager.ui" line="76"/>
<source>Add Subfolder</source>
<translation type="unfinished"></translation>
<translation>Pridať podpriečinok</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="84"/>
@ -552,17 +552,17 @@ p, li { white-space: pre-wrap; }
<message>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="93"/>
<source>Add new subfolder</source>
<translation type="unfinished"></translation>
<translation>Pridať nový podpriečinok</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="93"/>
<source>Choose name for new subfolder in bookmarks toolbar: </source>
<translation type="unfinished"></translation>
<translation>Zvoľte meno pre nový podpriečinok v panely záložiek: </translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="114"/>
<source>Choose name for folder: </source>
<translation type="unfinished"></translation>
<translation>Zvoľte meno pre priečinok: </translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarksmanager.cpp" line="188"/>
@ -655,7 +655,7 @@ p, li { white-space: pre-wrap; }
<message>
<location filename="../src/sidebar/bookmarkssidebar.cpp" line="101"/>
<source>Copy address</source>
<translation type="unfinished"></translation>
<translation>Kopírovať adresu</translation>
</message>
<message>
<source>Move bookmark to &amp;folder</source>
@ -716,17 +716,17 @@ p, li { white-space: pre-wrap; }
<message>
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="80"/>
<source>Move right</source>
<translation type="unfinished"></translation>
<translation>Posunúť doprava</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="81"/>
<source>Move left</source>
<translation type="unfinished"></translation>
<translation>Posunúť doľava</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="83"/>
<source>Remove bookmark</source>
<translation type="unfinished"></translation>
<translation>Odstrániť záložku</translation>
</message>
<message>
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="411"/>
@ -742,7 +742,7 @@ p, li { white-space: pre-wrap; }
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="449"/>
<location filename="../src/bookmarks/bookmarkstoolbar.cpp" line="466"/>
<source>Empty</source>
<translation type="unfinished">Prázdne</translation>
<translation>Prázdny</translation>
</message>
<message>
<source>Sites You visited the most</source>
@ -1647,7 +1647,7 @@ p, li { white-space: pre-wrap; }
<message>
<location filename="../src/sidebar/historysidebar.cpp" line="84"/>
<source>Copy address</source>
<translation type="unfinished"></translation>
<translation>Kopírovať adresu</translation>
</message>
<message>
<location filename="../src/sidebar/historysidebar.cpp" line="101"/>