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"
|
2011-12-08 19:12:43 +01:00
|
|
|
#include "databasewriter.h"
|
2014-03-07 18:03:42 +01:00
|
|
|
#include "autosaver.h"
|
|
|
|
#include "webview.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QBuffer>
|
|
|
|
|
2012-04-22 20:51:28 +02:00
|
|
|
IconProvider* IconProvider::s_instance = 0;
|
2012-03-13 17:51:06 +01:00
|
|
|
|
2012-04-22 20:51:28 +02:00
|
|
|
IconProvider::IconProvider(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
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.
|
2012-06-26 11:49:39 +02:00
|
|
|
if (mApp->isPrivateSession()) {
|
2012-01-25 15:46:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
BufferedIcon item;
|
|
|
|
item.first = view->url();
|
|
|
|
item.second = view->icon().pixmap(16, 16).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-07 18:03:42 +01:00
|
|
|
return QIcon::fromTheme("dialog-error", QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxInformation:
|
2014-03-07 18:03:42 +01:00
|
|
|
return QIcon::fromTheme("dialog-information", QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxQuestion:
|
2014-03-07 18:03:42 +01:00
|
|
|
return QIcon::fromTheme("dialog-question", QApplication::style()->standardIcon(QStyle::SP_MessageBoxQuestion));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxWarning:
|
2014-03-07 18:03:42 +01:00
|
|
|
return QIcon::fromTheme("dialog-warning", QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning));
|
2012-03-28 16:42:50 +02:00
|
|
|
|
2013-02-21 22:55:28 +01:00
|
|
|
#ifndef QZ_WS_X11
|
2011-09-30 21:44:18 +02:00
|
|
|
case QStyle::SP_DialogCloseButton:
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/close.png");
|
2011-09-30 21:44:18 +02:00
|
|
|
|
|
|
|
case QStyle::SP_BrowserStop:
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/stop.png");
|
2011-09-30 21:44:18 +02:00
|
|
|
|
|
|
|
case QStyle::SP_BrowserReload:
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/reload.png");
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2013-02-21 22:55:28 +01:00
|
|
|
case QStyle::SP_FileDialogToParent:
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/go-up.png");
|
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
|
|
|
//RTL Support
|
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/back.png");
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/forward.png");
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
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
|
|
|
//RTL Support
|
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/forward.png");
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon(":/icons/theme/back.png");
|
2012-08-09 04:00:09 +02:00
|
|
|
}
|
2011-09-30 21:44:18 +02:00
|
|
|
#endif
|
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-07 18:03:42 +01:00
|
|
|
QIcon IconProvider::iconFromTheme(const QString &icon)
|
2011-09-30 21:44:18 +02:00
|
|
|
{
|
2014-02-07 23:14:32 +01:00
|
|
|
// TODO: This should actually look in :icons/theme for fallback icon, not hardcode every icon
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (icon == QLatin1String("go-home")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("go-home", QIcon(":/icons/theme/home.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (icon == QLatin1String("text-plain")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("text-plain", QIcon(":icons/locationbar/unknownpage.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2013-06-02 22:47:02 +02:00
|
|
|
else if (icon == QLatin1String("bookmarks-organize")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("bookmarks-organize", QIcon(":icons/theme/user-bookmarks.png"));
|
2013-06-02 22:47:02 +02:00
|
|
|
}
|
|
|
|
else if (icon == QLatin1String("bookmark-new")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("bookmark-new", QIcon(":icons/theme/user-bookmarks.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (icon == QLatin1String("list-remove")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("list-remove", QIcon(":icons/theme/list-remove.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (icon == QLatin1String("go-next")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("go-next", QIcon(":icons/theme/go-next.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (icon == QLatin1String("go-previous")) {
|
2014-01-04 22:37:18 +01:00
|
|
|
return QIcon::fromTheme("go-previous", QIcon(":icons/theme/go-previous.png"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-02-07 23:14:32 +01:00
|
|
|
else if (icon == QLatin1String("view-restore")) {
|
|
|
|
return QIcon::fromTheme("view-restore", QIcon(":icons/theme/view-restore.png"));
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
else {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme(icon);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 21:44:18 +02:00
|
|
|
}
|
|
|
|
|
2012-03-13 17:51:06 +01:00
|
|
|
QIcon IconProvider::emptyWebIcon()
|
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
return QPixmap::fromImage(instance()->m_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()) {
|
|
|
|
instance()->m_emptyWebImage = iconFromTheme("text-plain").pixmap(16, 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
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QIcon IconProvider::iconForUrl(const QUrl &url)
|
2012-04-22 20:51:28 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
if (url.path().isEmpty()) {
|
|
|
|
return IconProvider::emptyWebIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const BufferedIcon &ic, instance()->m_iconBuffer) {
|
|
|
|
if (ic.first.toString().startsWith(url.toString())) {
|
|
|
|
return instance()->iconFromImage(ic.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT icon FROM icons WHERE url LIKE ? LIMIT 1");
|
|
|
|
query.addBindValue(QString("%1%").arg(QString::fromUtf8(url.toEncoded(QUrl::RemoveFragment))));
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.next()) {
|
|
|
|
return instance()->iconFromImage(QImage::fromData(query.value(0).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return IconProvider::emptyWebIcon();
|
2012-04-22 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
QIcon IconProvider::iconForDomain(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()) {
|
|
|
|
return instance()->iconFromImage(ic.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT icon FROM icons WHERE url LIKE ?");
|
|
|
|
query.addBindValue(QString("%%1%").arg(url.host()));
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.next()) {
|
|
|
|
return instance()->iconFromImage(QImage::fromData(query.value(0).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return QIcon();
|
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:03:42 +01:00
|
|
|
if (!s_instance) {
|
|
|
|
s_instance = new IconProvider;
|
2012-03-13 17:51:06 +01:00
|
|
|
}
|
2014-03-07 18:03:42 +01:00
|
|
|
return s_instance;
|
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));
|
|
|
|
|
|
|
|
DatabaseWriter::instance()->executeQuery(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)
|
|
|
|
{
|
|
|
|
if (m_emptyWebImage.isNull()) {
|
|
|
|
m_emptyWebImage = iconFromTheme("text-plain").pixmap(16, 16).toImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QIcon(QPixmap::fromImage(image));
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|