2011-04-27 09:05:54 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 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>
|
|
|
|
|
2014-03-07 18:24:13 +01:00
|
|
|
Q_GLOBAL_STATIC(IconProvider, qz_icon_provider)
|
2012-03-13 17:51:06 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:57:04 +02:00
|
|
|
static const char *ignoredSchemes[] = { "qupzilla", "ftp", "file", "view-source" };
|
|
|
|
for (unsigned i = 0; i < sizeof(ignoredSchemes) / sizeof(ignoredSchemes[0]); ++i)
|
|
|
|
if (view->url().scheme() == ignoredSchemes[i])
|
|
|
|
return;
|
2015-10-02 17:10:27 +02:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
BufferedIcon item;
|
|
|
|
item.first = view->url();
|
2015-10-24 12:43:15 +02:00
|
|
|
item.second = view->icon().pixmap(32).toImage();
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
if (item.second == IconProvider::emptyWebImage()) {
|
2011-04-25 20:56:45 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
m_autoSaver->changeOcurred();
|
2011-04-25 20:56:45 +02:00
|
|
|
m_iconBuffer.append(item);
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QPixmap IconProvider::bookmarkIcon() const
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
return m_bookmarkIcon;
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
void IconProvider::setBookmarkIcon(const QPixmap &pixmap)
|
2011-04-28 18:31:48 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
m_bookmarkIcon = pixmap;
|
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
|
|
|
{
|
2014-03-24 16:08:33 +01:00
|
|
|
return QIcon::fromTheme(QSL("tab-new"), QIcon(QSL(":/icons/menu/tab-new.png")));
|
|
|
|
}
|
2014-02-07 23:14:32 +01:00
|
|
|
|
2014-03-24 16:08:33 +01:00
|
|
|
QIcon IconProvider::newWindowIcon()
|
|
|
|
{
|
|
|
|
return QIcon::fromTheme(QSL("window-new"), QIcon(QSL(":/icons/menu/window-new.png")));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::privateBrowsingIcon()
|
|
|
|
{
|
|
|
|
return QIcon(QSL(":/icons/menu/privatebrowsing.png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::settingsIcon()
|
|
|
|
{
|
|
|
|
return QIcon(QSL(":/icons/menu/settings.png"));
|
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()) {
|
2014-03-24 16:08:33 +01:00
|
|
|
instance()->m_emptyWebImage = QPixmap(":icons/other/empty-page.png").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
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QIcon IconProvider::iconForUrl(const QUrl &url)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
|
|
|
return instance()->iconFromImage(imageForUrl(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage IconProvider::imageForUrl(const QUrl &url)
|
2012-04-22 20:51:28 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
if (url.path().isEmpty()) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return IconProvider::emptyWebImage();
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const BufferedIcon &ic, instance()->m_iconBuffer) {
|
|
|
|
if (ic.first.toString().startsWith(url.toString())) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return ic.second;
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2014-06-13 21:22:44 +02:00
|
|
|
query.prepare(QSL("SELECT icon FROM icons WHERE url LIKE ? ESCAPE ? LIMIT 1"));
|
|
|
|
|
|
|
|
query.addBindValue(QString("%1%").arg(QzTools::escapeSqlString(QString::fromUtf8(url.toEncoded(QUrl::RemoveFragment)))));
|
|
|
|
query.addBindValue(QL1S("!"));
|
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
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
return IconProvider::emptyWebImage();
|
2012-04-22 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QIcon IconProvider::iconForDomain(const QUrl &url)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
|
|
|
return instance()->iconFromImage(imageForDomain(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage IconProvider::imageForDomain(const QUrl &url)
|
2012-04-22 20:51:28 +02: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;
|
2014-06-13 21:22:44 +02:00
|
|
|
query.prepare(QSL("SELECT icon FROM icons WHERE url LIKE ? ESCAPE ? LIMIT 1"));
|
|
|
|
|
|
|
|
query.addBindValue(QString("%%1%").arg(QzTools::escapeSqlString(url.host())));
|
|
|
|
query.addBindValue(QL1S("!"));
|
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
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
return QImage();
|
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());
|
|
|
|
query.bindValue(1, ic.first.toEncoded(QUrl::RemoveFragment));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
void IconProvider::clearIconsDatabase()
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("DELETE FROM icons");
|
|
|
|
query.exec("VACUUM");
|
|
|
|
|
|
|
|
m_iconBuffer.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::iconFromImage(const QImage &image)
|
|
|
|
{
|
|
|
|
return QIcon(QPixmap::fromImage(image));
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|