2011-04-27 09:05:54 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2016-02-26 10:43:34 +01:00
|
|
|
* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
|
2011-04-27 09:05:54 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "mainapplication.h"
|
2015-09-29 11:45:39 +02:00
|
|
|
#include "networkmanager.h"
|
2014-03-15 01:14:06 +01:00
|
|
|
#include "sqldatabase.h"
|
2014-03-07 18:03:42 +01:00
|
|
|
#include "autosaver.h"
|
|
|
|
#include "webview.h"
|
2014-06-13 21:22:44 +02:00
|
|
|
#include "qztools.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QBuffer>
|
2016-12-11 00:12:38 +01:00
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2014-03-07 18:24:13 +01:00
|
|
|
Q_GLOBAL_STATIC(IconProvider, qz_icon_provider)
|
2012-03-13 17:51:06 +01:00
|
|
|
|
2016-12-29 12:37:36 +01:00
|
|
|
static QByteArray encodeUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
return url.toEncoded(QUrl::RemoveFragment | QUrl::StripTrailingSlash);
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:24:13 +01:00
|
|
|
IconProvider::IconProvider()
|
|
|
|
: QWidget()
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
m_autoSaver = new AutoSaver(this);
|
|
|
|
connect(m_autoSaver, SIGNAL(save()), this, SLOT(saveIconsToDatabase()));
|
2012-04-22 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 18:31:48 +02:00
|
|
|
void IconProvider::saveIcon(WebView* view)
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
// Don't save icons in private mode.
|
2014-03-10 00:47:07 +01:00
|
|
|
if (mApp->isPrivate()) {
|
2012-01-25 15:46:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
const QIcon icon = view->icon(true);
|
|
|
|
if (icon.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-20 12:33:43 +01:00
|
|
|
const QStringList ignoredSchemes = {
|
|
|
|
QStringLiteral("qupzilla"),
|
|
|
|
QStringLiteral("ftp"),
|
|
|
|
QStringLiteral("file"),
|
|
|
|
QStringLiteral("view-source")
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &scheme : ignoredSchemes) {
|
|
|
|
if (view->url().scheme() == scheme) {
|
2015-10-23 12:57:04 +02:00
|
|
|
return;
|
2016-12-20 12:33:43 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 17:10:27 +02:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
BufferedIcon item;
|
|
|
|
item.first = view->url();
|
2016-12-30 17:13:15 +01:00
|
|
|
item.second = icon.pixmap(16).toImage();
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
if (m_iconBuffer.contains(item)) {
|
|
|
|
return;
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2011-04-25 20:56:45 +02:00
|
|
|
m_iconBuffer.append(item);
|
|
|
|
}
|
|
|
|
|
2016-01-27 12:44:19 +01:00
|
|
|
QIcon IconProvider::bookmarkIcon() const
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2016-01-27 12:44:19 +01:00
|
|
|
return QIcon::fromTheme(QSL("bookmarks"), m_bookmarkIcon);
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
2016-01-27 12:44:19 +01:00
|
|
|
void IconProvider::setBookmarkIcon(const QIcon &icon)
|
2011-04-28 18:31:48 +02:00
|
|
|
{
|
2016-01-27 12:44:19 +01:00
|
|
|
m_bookmarkIcon = icon;
|
2011-04-28 18:31:48 +02:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
2011-09-30 21:44:18 +02:00
|
|
|
QIcon IconProvider::standardIcon(QStyle::StandardPixmap icon)
|
|
|
|
{
|
2012-03-28 16:42:50 +02:00
|
|
|
switch (icon) {
|
|
|
|
case QStyle::SP_MessageBoxCritical:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("dialog-error"), QApplication::style()->standardIcon(icon));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxInformation:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("dialog-information"), QApplication::style()->standardIcon(icon));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxQuestion:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("dialog-question"), QApplication::style()->standardIcon(icon));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxWarning:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("dialog-warning"), QApplication::style()->standardIcon(icon));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
2011-09-30 21:44:18 +02:00
|
|
|
case QStyle::SP_DialogCloseButton:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("dialog-close"), QApplication::style()->standardIcon(icon));
|
2011-09-30 21:44:18 +02:00
|
|
|
|
|
|
|
case QStyle::SP_BrowserStop:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("process-stop"), QApplication::style()->standardIcon(icon));
|
2011-09-30 21:44:18 +02:00
|
|
|
|
|
|
|
case QStyle::SP_BrowserReload:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("view-refresh"), QApplication::style()->standardIcon(icon));
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2013-02-21 22:55:28 +01:00
|
|
|
case QStyle::SP_FileDialogToParent:
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("go-up"), QApplication::style()->standardIcon(icon));
|
|
|
|
|
|
|
|
case QStyle::SP_ArrowUp:
|
|
|
|
return QIcon::fromTheme(QSL("go-up"), QApplication::style()->standardIcon(icon));
|
|
|
|
|
|
|
|
case QStyle::SP_ArrowDown:
|
|
|
|
return QIcon::fromTheme(QSL("go-down"), QApplication::style()->standardIcon(icon));
|
2013-02-21 22:55:28 +01:00
|
|
|
|
2011-09-30 21:44:18 +02:00
|
|
|
case QStyle::SP_ArrowForward:
|
2012-08-09 04:00:09 +02:00
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("go-previous"), QApplication::style()->standardIcon(icon));
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("go-next"), QApplication::style()->standardIcon(icon));
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2011-10-01 17:55:10 +02:00
|
|
|
case QStyle::SP_ArrowBack:
|
2012-08-09 04:00:09 +02:00
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("go-next"), QApplication::style()->standardIcon(icon));
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("go-previous"), QApplication::style()->standardIcon(icon));
|
|
|
|
|
2013-02-21 22:55:28 +01:00
|
|
|
default:
|
2014-03-07 18:03:42 +01:00
|
|
|
return QApplication::style()->standardIcon(icon);
|
2011-09-30 21:44:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 16:08:33 +01:00
|
|
|
QIcon IconProvider::newTabIcon()
|
2011-09-30 21:44:18 +02:00
|
|
|
{
|
2016-12-29 15:07:34 +01:00
|
|
|
return QIcon::fromTheme(QSL("tab-new"), QIcon(QSL(":/icons/menu/tab-new.svg")));
|
2014-03-24 16:08:33 +01:00
|
|
|
}
|
2014-02-07 23:14:32 +01:00
|
|
|
|
2014-03-24 16:08:33 +01:00
|
|
|
QIcon IconProvider::newWindowIcon()
|
|
|
|
{
|
2016-12-29 15:07:34 +01:00
|
|
|
return QIcon::fromTheme(QSL("window-new"), QIcon(QSL(":/icons/menu/window-new.svg")));
|
2014-03-24 16:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::privateBrowsingIcon()
|
|
|
|
{
|
|
|
|
return QIcon(QSL(":/icons/menu/privatebrowsing.png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::settingsIcon()
|
|
|
|
{
|
2016-12-29 15:07:34 +01:00
|
|
|
return QIcon::fromTheme(QSL("configure"), QIcon(QSL(":/icons/menu/settings.svg")));
|
2011-09-30 21:44:18 +02:00
|
|
|
}
|
|
|
|
|
2012-03-13 17:51:06 +01:00
|
|
|
QIcon IconProvider::emptyWebIcon()
|
|
|
|
{
|
2014-03-24 16:08:33 +01:00
|
|
|
return QPixmap::fromImage(instance()->emptyWebImage());
|
2012-03-13 17:51:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage IconProvider::emptyWebImage()
|
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
if (instance()->m_emptyWebImage.isNull()) {
|
2016-12-30 17:20:01 +01:00
|
|
|
instance()->m_emptyWebImage = QIcon(QSL(":icons/other/webpage.svg")).pixmap(16).toImage();
|
2012-03-13 17:51:06 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
return instance()->m_emptyWebImage;
|
2012-03-13 17:51:06 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
QIcon IconProvider::iconForUrl(const QUrl &url, bool allowNull)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
2016-12-30 17:13:15 +01:00
|
|
|
return instance()->iconFromImage(imageForUrl(url, allowNull));
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
QImage IconProvider::imageForUrl(const QUrl &url, bool allowNull)
|
2012-04-22 20:51:28 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
if (url.path().isEmpty()) {
|
2016-12-30 17:13:15 +01:00
|
|
|
return allowNull ? QImage() : IconProvider::emptyWebImage();
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:37:36 +01:00
|
|
|
const QByteArray encodedUrl = encodeUrl(url);
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
foreach (const BufferedIcon &ic, instance()->m_iconBuffer) {
|
2016-12-29 12:37:36 +01:00
|
|
|
if (encodeUrl(ic.first) == encodedUrl) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return ic.second;
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2016-12-20 14:19:58 +01:00
|
|
|
query.prepare(QSL("SELECT icon FROM icons WHERE url GLOB ? LIMIT 1"));
|
2016-12-29 12:37:36 +01:00
|
|
|
query.addBindValue(QString("%1*").arg(QzTools::escapeSqlGlobString(QString::fromUtf8(encodedUrl))));
|
2016-12-20 11:22:31 +01:00
|
|
|
SqlDatabase::instance()->exec(query);
|
2014-03-07 18:03:42 +01:00
|
|
|
|
|
|
|
if (query.next()) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return QImage::fromData(query.value(0).toByteArray());
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
return allowNull ? QImage() : IconProvider::emptyWebImage();
|
2012-04-22 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
QIcon IconProvider::iconForDomain(const QUrl &url, bool allowNull)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
2016-12-30 17:13:15 +01:00
|
|
|
return instance()->iconFromImage(imageForDomain(url, allowNull));
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
QImage IconProvider::imageForDomain(const QUrl &url, bool allowNull)
|
2012-04-22 20:51:28 +02:00
|
|
|
{
|
2016-12-11 09:25:08 +01:00
|
|
|
if (url.host().isEmpty()) {
|
2016-12-30 17:13:15 +01:00
|
|
|
return allowNull ? QImage() : IconProvider::emptyWebImage();
|
2016-12-11 09:25:08 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
foreach (const BufferedIcon &ic, instance()->m_iconBuffer) {
|
|
|
|
if (ic.first.host() == url.host()) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return ic.second;
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2016-12-20 14:19:58 +01:00
|
|
|
query.prepare(QSL("SELECT icon FROM icons WHERE url GLOB ? LIMIT 1"));
|
2014-06-13 21:22:44 +02:00
|
|
|
|
2016-12-20 14:19:58 +01:00
|
|
|
query.addBindValue(QString("*%1*").arg(QzTools::escapeSqlGlobString(url.host())));
|
2014-03-07 18:03:42 +01:00
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.next()) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return QImage::fromData(query.value(0).toByteArray());
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
return allowNull ? QImage() : IconProvider::emptyWebImage();
|
2012-04-22 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
IconProvider* IconProvider::instance()
|
2012-01-17 19:27:24 +01:00
|
|
|
{
|
2014-03-07 18:24:13 +01:00
|
|
|
return qz_icon_provider();
|
2012-01-17 19:27:24 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
void IconProvider::saveIconsToDatabase()
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
foreach (const BufferedIcon &ic, m_iconBuffer) {
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM icons WHERE url = ?");
|
|
|
|
query.bindValue(0, ic.first.toEncoded(QUrl::RemoveFragment));
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.next()) {
|
|
|
|
query.prepare("UPDATE icons SET icon = ? WHERE url = ?");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
query.prepare("INSERT INTO icons (icon, url) VALUES (?,?)");
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
ic.second.save(&buffer, "PNG");
|
|
|
|
query.bindValue(0, buffer.data());
|
2016-12-29 12:37:36 +01:00
|
|
|
query.bindValue(1, encodeUrl(ic.first));
|
2014-03-07 18:03:42 +01:00
|
|
|
|
2014-03-15 01:14:06 +01:00
|
|
|
SqlDatabase::instance()->execAsync(query);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-03-07 18:03:42 +01:00
|
|
|
|
|
|
|
m_iconBuffer.clear();
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 00:08:09 +01:00
|
|
|
void IconProvider::clearOldIconsInDatabase()
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
2016-12-24 00:08:09 +01:00
|
|
|
// Delete icons for entries older than 6 months
|
|
|
|
const QDateTime date = QDateTime::currentDateTime().addMonths(-6);
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QSqlQuery query;
|
2016-12-24 00:08:09 +01:00
|
|
|
query.prepare(QSL("DELETE FROM icons WHERE url IN (SELECT url FROM history WHERE date < ?)"));
|
|
|
|
query.addBindValue(date.toMSecsSinceEpoch());
|
|
|
|
query.exec();
|
2014-03-07 18:03:42 +01:00
|
|
|
|
2016-12-24 00:08:09 +01:00
|
|
|
query.clear();
|
|
|
|
query.exec(QSL("VACUUM"));
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::iconFromImage(const QImage &image)
|
|
|
|
{
|
|
|
|
return QIcon(QPixmap::fromImage(image));
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|