Commit 75ad92ea authored by Rinigus Saar's avatar Rinigus Saar

use webengine image provider directly to fetch icons

parent 76919e1d
......@@ -66,9 +66,9 @@ void BrowserManager::lastVisited(const QString &url)
m_dbmanager.lastVisited(url);
}
void BrowserManager::updateIcon(const QString &url, const QString &iconSource, const QImage &image)
void BrowserManager::updateIcon(const QString &url, const QString &iconSource)
{
m_dbmanager.updateIcon(url, iconSource, image);
m_dbmanager.updateIcon(url, iconSource);
}
void BrowserManager::setHomepage(const QString &homepage)
......
......@@ -74,7 +74,7 @@ public slots:
void removeFromHistory(const QString &url);
void lastVisited(const QString &url);
void updateIcon(const QString &url, const QString &iconSource, const QImage &image);
void updateIcon(const QString &url, const QString &iconSource);
void setHomepage(const QString &homepage);
void setSearchBaseUrl(const QString &searchBaseUrl);
......
......@@ -57,25 +57,6 @@ WebEngineView {
visible: false
}
Image {
id: favicon
source: webEngineView.icon
visible: false
onSourceChanged: grab()
onStatusChanged: grab()
function grab() {
if (webEngineView.privateMode || status !== Image.Ready || !source)
return;
var url = webEngineView.url;
var icon = webEngineView.icon;
favicon.grabToImage(function (result){
BrowserManager.updateIcon(url, icon, result.image);
});
}
}
Timer {
id: snaphotTimer
interval: 1000
......@@ -218,6 +199,11 @@ WebEngineView {
print("Settings: " + settings);
}
onIconChanged: {
if (icon && !privateMode)
BrowserManager.updateIcon(url, icon)
}
onNewViewRequested: {
if (request.userInitiated) {
tabsModel.newTab(request.requestedUrl.toString())
......
......@@ -25,7 +25,6 @@
#include <QDateTime>
#include <QDebug>
#include <QImage>
#include <QStandardPaths>
#include <QSqlDatabase>
#include <QSqlError>
......@@ -231,9 +230,9 @@ void DBManager::lastVisited(const QString &url)
lastVisitedRecord(QStringLiteral("history"), url);
}
void DBManager::updateIcon(const QString &url, const QString &iconSource, const QImage &image)
void DBManager::updateIcon(const QString &url, const QString &iconSource)
{
QString updatedSource = IconImageProvider::storeImage(iconSource, image);
QString updatedSource = IconImageProvider::storeImage(iconSource);
updateIconRecord(QStringLiteral("bookmarks"), url, updatedSource);
updateIconRecord(QStringLiteral("history"), url, updatedSource);
}
......@@ -48,7 +48,7 @@ public:
void addToHistory(const QVariantMap &pagedata);
void removeFromHistory(const QString &url);
void updateIcon(const QString &url, const QString &iconSource, const QImage &image);
void updateIcon(const QString &url, const QString &iconSource);
void lastVisited(const QString &url);
private:
......
......@@ -4,21 +4,29 @@
#include <QBuffer>
#include <QDebug>
#include <QImage>
#include <QPixmap>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>
#include <QQmlApplicationEngine>
IconImageProvider::IconImageProvider() :
// As there is only one instance of the IconImageProvider
// and icons are added into the database using static methods,
// engine has to be accessed via static property
QQmlApplicationEngine *IconImageProvider::s_engine;
IconImageProvider::IconImageProvider(QQmlApplicationEngine *engine) :
QQuickImageProvider(QQmlImageProviderBase::Image)
{
s_engine = engine;
}
QString IconImageProvider::providerId()
{
return "angelfish";
return "angelfish-favicon";
}
QString IconImageProvider::storeImage(const QString &iconSource, const QImage &image)
QString IconImageProvider::storeImage(const QString &iconSource)
{
QLatin1String prefix_favicon = QLatin1String("image://favicon/");
if (!iconSource.startsWith(prefix_favicon)) {
......@@ -43,18 +51,39 @@ QString IconImageProvider::storeImage(const QString &iconSource, const QImage &i
if (query_check.next()) {
// there is corresponding record in the database already
// no need to store it again
qDebug() << "Icon stored already" << url;
return url;
}
query_check.finish();
// Store new icon
QQuickImageProvider *provider = dynamic_cast<QQuickImageProvider *>(s_engine->imageProvider("favicon"));
if (provider == nullptr) {
qWarning() << Q_FUNC_INFO << "Failed to load image provider" << url;
return iconSource; // as something is wrong
}
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
if (!image.save(&buffer, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save image" << url;
return iconSource; // as something is wrong
QSize sz_requested;
QSize sz_obtained;
QString providerIconName = iconSource.mid(prefix_favicon.size());
if (provider->imageType() == QQmlImageProviderBase::Image) {
QImage image = provider->requestImage(providerIconName, &sz_obtained, sz_requested);
if (!image.save(&buffer, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save image" << url;
return iconSource; // as something is wrong
}
} else if (provider->imageType() == QQmlImageProviderBase::Pixmap) {
QPixmap image = provider->requestPixmap(providerIconName, &sz_obtained, sz_requested);
if (!image.save(&buffer, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save pixmap" << url;
return iconSource; // as something is wrong
}
} else {
qWarning() << Q_FUNC_INFO << "Unsupported image provider" << provider->imageType();
return iconSource; // as something is wrong
}
QSqlQuery query_write;
......
......@@ -2,19 +2,23 @@
#define ICONIMAGEPROVIDER_H
#include <QQuickImageProvider>
#include <QQmlApplicationEngine>
class IconImageProvider : public QQuickImageProvider
{
public:
IconImageProvider();
IconImageProvider(QQmlApplicationEngine *engine);
virtual QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
// store image into the database if it is missing. Return new
// image:// uri that should be used to fetch the icon
static QString storeImage(const QString &iconSource, const QImage &image);
static QString storeImage(const QString &iconSource);
static QString providerId();
private:
static QQmlApplicationEngine *s_engine;
};
#endif // ICONIMAGEPROVIDER_H
......@@ -64,7 +64,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QQmlApplicationEngine engine;
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
engine.addImageProvider(IconImageProvider::providerId(), new IconImageProvider);
engine.addImageProvider(IconImageProvider::providerId(), new IconImageProvider(&engine));
// initial url command line parameter
QString initialUrl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment