From b94135ead54d0cfd3ef68bf7cfa07127d4b7f015 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sun, 8 Feb 2015 11:13:12 +0100 Subject: [PATCH] WebView: Implement icon loading --- src/lib/history/history.cpp | 2 +- src/lib/navigation/locationbar.cpp | 2 -- src/lib/tabwidget/tabicon.cpp | 2 -- src/lib/tools/iconprovider.cpp | 28 ++++++++++++++++++ src/lib/tools/iconprovider.h | 20 +++++++++++++ src/lib/webkit/webview.cpp | 46 ++++++++++++------------------ src/lib/webkit/webview.h | 12 +++----- 7 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/lib/history/history.cpp b/src/lib/history/history.cpp index 000492ede..90806620c 100644 --- a/src/lib/history/history.cpp +++ b/src/lib/history/history.cpp @@ -53,7 +53,7 @@ void History::loadSettings() // AddHistoryEntry void History::addHistoryEntry(WebView* view) { - if (!m_isSaving || view->loadingError()) { + if (!m_isSaving) { return; } diff --git a/src/lib/navigation/locationbar.cpp b/src/lib/navigation/locationbar.cpp index d9c8e6987..040ca0ee5 100644 --- a/src/lib/navigation/locationbar.cpp +++ b/src/lib/navigation/locationbar.cpp @@ -126,9 +126,7 @@ void LocationBar::setWebView(TabbedWebView* view) connect(m_webView, SIGNAL(urlChanged(QUrl)), this, SLOT(showUrl(QUrl))); connect(m_webView, SIGNAL(rssChanged(bool)), this, SLOT(setRssIconVisible(bool))); connect(m_webView, SIGNAL(privacyChanged(bool)), this, SLOT(setPrivacyState(bool))); -#if QTWEBENGINE_DISABLED connect(m_webView, SIGNAL(iconChanged()), this, SLOT(updateSiteIcon())); -#endif } void LocationBar::setText(const QString &text) diff --git a/src/lib/tabwidget/tabicon.cpp b/src/lib/tabwidget/tabicon.cpp index 26c880c99..9345c8bfd 100644 --- a/src/lib/tabwidget/tabicon.cpp +++ b/src/lib/tabwidget/tabicon.cpp @@ -50,9 +50,7 @@ void TabIcon::setWebTab(WebTab* tab) connect(m_tab->webView(), SIGNAL(loadStarted()), this, SLOT(showLoadingAnimation())); connect(m_tab->webView(), SIGNAL(loadFinished(bool)), this, SLOT(hideLoadingAnimation())); -#if QTWEBENGINE_DISABLED connect(m_tab->webView(), SIGNAL(iconChanged()), this, SLOT(showIcon())); -#endif showIcon(); } diff --git a/src/lib/tools/iconprovider.cpp b/src/lib/tools/iconprovider.cpp index 37a7fd7cb..63eb3d0a0 100644 --- a/src/lib/tools/iconprovider.cpp +++ b/src/lib/tools/iconprovider.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "iconprovider.h" #include "mainapplication.h" +#include "followredirectreply.h" #include "sqldatabase.h" #include "autosaver.h" #include "webview.h" @@ -255,3 +256,30 @@ QIcon IconProvider::iconFromImage(const QImage &image) { return QIcon(QPixmap::fromImage(image)); } + +// IconLoader +IconLoader::IconLoader(const QUrl &url, QObject* parent) + : QObject(parent) +{ + m_reply = new FollowRedirectReply(url, mApp->networkManager()); + connect(m_reply, &FollowRedirectReply::finished, this, &IconLoader::finished); +} + +IconLoader::~IconLoader() +{ + delete m_reply; +} + +void IconLoader::finished() +{ + if (m_reply->error() != QNetworkReply::NoError) { + emit error(); + } + else { + const QByteArray data = m_reply->readAll(); + emit iconLoaded(QIcon(QPixmap::fromImage(QImage::fromData(data)))); + } + + delete m_reply; + m_reply = 0; +} diff --git a/src/lib/tools/iconprovider.h b/src/lib/tools/iconprovider.h index b2e53a58d..93900e829 100644 --- a/src/lib/tools/iconprovider.h +++ b/src/lib/tools/iconprovider.h @@ -30,6 +30,7 @@ class QIcon; class WebView; class AutoSaver; +class FollowRedirectReply; // Needs to be QWidget subclass, otherwise qproperty- setting won't work class QUPZILLA_EXPORT IconProvider : public QWidget @@ -83,4 +84,23 @@ private: AutoSaver* m_autoSaver; }; +class QUPZILLA_EXPORT IconLoader : public QObject +{ + Q_OBJECT + +public: + explicit IconLoader(const QUrl &url, QObject* parent = 0); + ~IconLoader(); + +signals: + void iconLoaded(const QIcon &icon); + void error(); + +private slots: + void finished(); + +private: + FollowRedirectReply* m_reply; +}; + #endif // ICONPROVIDER_H diff --git a/src/lib/webkit/webview.cpp b/src/lib/webkit/webview.cpp index 4c1eb2604..b81eb64f4 100644 --- a/src/lib/webkit/webview.cpp +++ b/src/lib/webkit/webview.cpp @@ -54,6 +54,7 @@ bool WebView::s_forceContextMenuOnMouseRelease = false; WebView::WebView(QWidget* parent) : QWebEngineView(parent) + , m_siteIconLoader(0) , m_isLoading(false) , m_progress(0) #if QTWEBENGINE_DISABLED @@ -69,9 +70,7 @@ WebView::WebView(QWidget* parent) connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int))); connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished())); connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(slotUrlChanged(QUrl))); -#if QTWEBENGINE_DISABLED - connect(this, SIGNAL(iconChanged()), this, SLOT(slotIconChanged())); -#endif + connect(this, SIGNAL(iconUrlChanged(QUrl)), this, SLOT(slotIconUrlChanged(QUrl))); m_zoomLevels = zoomLevels(); m_currentZoomLevel = m_zoomLevels.indexOf(100); @@ -218,11 +217,6 @@ void WebView::load(const LoadRequest &request) loadRequest(searchRequest); } -bool WebView::loadingError() const -{ - return page()->loadingError(); -} - bool WebView::isLoading() const { return m_isLoading; @@ -244,14 +238,6 @@ bool WebView::hasRss() const return m_hasRss; } -#if QTWEBENGINE_DISABLED -QWebElement WebView::activeElement() const -{ - QRect activeRect = inputMethodQuery(Qt::ImMicroFocus).toRect(); - return page()->mainFrame()->hitTestContent(activeRect.center()).element(); -} -#endif - int WebView::zoomLevel() const { return m_currentZoomLevel; @@ -436,9 +422,6 @@ void WebView::back() history->back(); emit urlChanged(url()); -#if QTWEBENGINE_DISABLED - emit iconChanged(); -#endif } } @@ -450,9 +433,6 @@ void WebView::forward() history->forward(); emit urlChanged(url()); -#if QTWEBENGINE_DISABLED - emit iconChanged(); -#endif } } @@ -514,14 +494,26 @@ void WebView::checkRss() #endif } -void WebView::slotIconChanged() +void WebView::slotIconUrlChanged(const QUrl &url) { - if (!loadingError()) { - m_siteIcon = icon(); - m_siteIconUrl = url(); + if (m_siteIconUrl == url) { + emit iconChanged(); + return; + } + + delete m_siteIconLoader; + m_siteIconLoader = new IconLoader(url, this); + + connect(m_siteIconLoader, &IconLoader::iconLoaded, [this, url](const QIcon &icon) { + if (icon.isNull()) + return; + + m_siteIcon = icon; + m_siteIconUrl = url; + emit iconChanged(); IconProvider::instance()->saveIcon(this); - } + }); } void WebView::slotUrlChanged(const QUrl &url) diff --git a/src/lib/webkit/webview.h b/src/lib/webkit/webview.h index bee702afb..25819e2dd 100644 --- a/src/lib/webkit/webview.h +++ b/src/lib/webkit/webview.h @@ -20,15 +20,13 @@ #include #include -#if QTWEBENGINE_DISABLED -#include -#endif #include "qzcommon.h" #include "loadrequest.h" class WebPage; class LoadRequest; +class IconLoader; class QUPZILLA_EXPORT WebView : public QWebEngineView { @@ -47,16 +45,12 @@ public: void setPage(QWebEnginePage* page); void load(const LoadRequest &request); - bool loadingError() const; bool isLoading() const; int loadingProgress() const; void fakeLoadingProgress(int progress); bool hasRss() const; -#if QTWEBENGINE_DISABLED - QWebElement activeElement() const; -#endif // Set zoom level (0 - 17) int zoomLevel() const; @@ -80,6 +74,7 @@ public: static void setForceContextMenuOnMouseRelease(bool force); signals: + void iconChanged(); void viewportResized(QSize); void showNotification(QWidget*); void privacyChanged(bool); @@ -121,7 +116,7 @@ protected slots: void slotLoadStarted(); void slotLoadProgress(int progress); void slotLoadFinished(); - void slotIconChanged(); + void slotIconUrlChanged(const QUrl &url); void slotUrlChanged(const QUrl &url); // Context menu slots @@ -205,6 +200,7 @@ private: QIcon m_siteIcon; QUrl m_siteIconUrl; + IconLoader* m_siteIconLoader; bool m_isLoading; int m_progress;