2011-04-27 09:05:54 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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"
|
|
|
|
#include "webview.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "mainapplication.h"
|
2011-12-08 19:12:43 +01:00
|
|
|
#include "databasewriter.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
|
|
|
{
|
|
|
|
m_timer = new QTimer(this);
|
2011-11-06 17:01:23 +01:00
|
|
|
m_timer->setInterval(10 * 1000);
|
2011-04-25 20:56:45 +02:00
|
|
|
m_timer->start();
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2011-04-25 20:56:45 +02:00
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(saveIconsToDatabase()));
|
|
|
|
}
|
|
|
|
|
2012-04-22 21:02:25 +02:00
|
|
|
IconProvider* IconProvider::instance()
|
2012-04-22 20:51:28 +02:00
|
|
|
{
|
|
|
|
if (!s_instance) {
|
|
|
|
s_instance = new IconProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:31:48 +02:00
|
|
|
void IconProvider::saveIcon(WebView* view)
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2012-06-26 11:49:39 +02:00
|
|
|
if (mApp->isPrivateSession()) {
|
2012-01-25 15:46:18 +01:00
|
|
|
// Don't save icons in private mode.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:56:45 +02:00
|
|
|
Icon item;
|
2012-01-17 19:27:24 +01:00
|
|
|
item.image = view->icon().pixmap(16, 16).toImage();
|
2011-04-25 20:56:45 +02:00
|
|
|
item.url = view->url();
|
|
|
|
|
2012-03-13 17:51:06 +01:00
|
|
|
if (item.image == 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
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Icon & ic, m_iconBuffer) {
|
2012-01-17 19:27:24 +01:00
|
|
|
if (ic.url == item.url && ic.image == item.image) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
m_iconBuffer.append(item);
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
QImage IconProvider::iconForUrl(const QUrl &url)
|
2011-04-25 20:56:45 +02:00
|
|
|
{
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Icon & ic, m_iconBuffer) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ic.url == url) {
|
2012-01-17 19:27:24 +01:00
|
|
|
return ic.image;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2011-07-30 17:57:14 +02:00
|
|
|
query.prepare("SELECT icon FROM icons WHERE url=?");
|
2011-04-26 10:03:17 +02:00
|
|
|
query.bindValue(0, url.toEncoded(QUrl::RemoveFragment));
|
2011-04-25 20:56:45 +02:00
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2012-01-17 19:27:24 +01:00
|
|
|
return QImage::fromData(query.value(0).toByteArray());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-03-13 17:51:06 +01:00
|
|
|
return IconProvider::emptyWebImage();
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
QImage IconProvider::iconForDomain(const QUrl &url)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Icon & ic, m_iconBuffer) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ic.url.host() == url.host()) {
|
2012-01-17 19:27:24 +01:00
|
|
|
return ic.image;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2012-08-23 15:37:43 +02:00
|
|
|
query.prepare("SELECT icon FROM icons WHERE url LIKE ?");
|
|
|
|
query.addBindValue(QString("%%1%").arg(url.host()));
|
|
|
|
query.exec();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2012-01-17 19:27:24 +01:00
|
|
|
return QImage::fromData(query.value(0).toByteArray());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
return QImage();
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2011-04-25 20:56:45 +02:00
|
|
|
void IconProvider::saveIconsToDatabase()
|
|
|
|
{
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Icon & ic, m_iconBuffer) {
|
2011-04-25 20:56:45 +02:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM icons WHERE url = ?");
|
2011-04-26 10:03:17 +02:00
|
|
|
query.bindValue(0, ic.url.toEncoded(QUrl::RemoveFragment));
|
2011-04-25 20:56:45 +02:00
|
|
|
query.exec();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (query.next()) {
|
2011-04-25 20:56:45 +02:00
|
|
|
query.prepare("UPDATE icons SET icon = ? WHERE url = ?");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-04-25 20:56:45 +02:00
|
|
|
query.prepare("INSERT INTO icons (icon, url) VALUES (?,?)");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
ic.image.save(&buffer, "PNG");
|
|
|
|
query.bindValue(0, buffer.data());
|
2011-04-26 10:03:17 +02:00
|
|
|
query.bindValue(1, ic.url.toEncoded(QUrl::RemoveFragment));
|
2011-12-08 19:12:43 +01:00
|
|
|
mApp->dbWriter()->executeQuery(query);
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_iconBuffer.clear();
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:31:48 +02:00
|
|
|
void IconProvider::clearIconDatabase()
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("DELETE FROM icons");
|
|
|
|
query.exec("VACUUM");
|
|
|
|
|
|
|
|
m_iconBuffer.clear();
|
|
|
|
}
|
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:
|
|
|
|
return QIcon::fromTheme("dialog-error", mApp->style()->standardIcon(QStyle::SP_MessageBoxCritical));
|
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxInformation:
|
|
|
|
return QIcon::fromTheme("dialog-information", mApp->style()->standardIcon(QStyle::SP_MessageBoxInformation));
|
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxQuestion:
|
|
|
|
return QIcon::fromTheme("dialog-question", mApp->style()->standardIcon(QStyle::SP_MessageBoxQuestion));
|
|
|
|
|
|
|
|
case QStyle::SP_MessageBoxWarning:
|
|
|
|
return QIcon::fromTheme("dialog-warning", mApp->style()->standardIcon(QStyle::SP_MessageBoxWarning));
|
|
|
|
|
2012-12-22 12:47:45 +01:00
|
|
|
#ifdef QZ_WS_X11
|
2012-03-28 16:42:50 +02:00
|
|
|
default:
|
|
|
|
return mApp->style()->standardIcon(icon);
|
2011-09-30 21:44:18 +02:00
|
|
|
#else
|
|
|
|
case QStyle::SP_DialogCloseButton:
|
|
|
|
return QIcon(":/icons/faenza/close.png");
|
|
|
|
|
|
|
|
case QStyle::SP_BrowserStop:
|
|
|
|
return QIcon(":/icons/faenza/stop.png");
|
|
|
|
|
|
|
|
case QStyle::SP_BrowserReload:
|
|
|
|
return QIcon(":/icons/faenza/reload.png");
|
|
|
|
|
|
|
|
case QStyle::SP_ArrowForward:
|
2012-08-09 04:00:09 +02:00
|
|
|
//RTL Support
|
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
|
|
|
return QIcon(":/icons/faenza/back.png");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QIcon(":/icons/faenza/forward.png");
|
|
|
|
}
|
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) {
|
|
|
|
return QIcon(":/icons/faenza/forward.png");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QIcon(":/icons/faenza/back.png");
|
|
|
|
}
|
2011-09-30 21:44:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return QIcon();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::fromTheme(const QString &icon)
|
|
|
|
{
|
2012-09-04 12:42:45 +02:00
|
|
|
if (icon == QLatin1String("go-home")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("go-home", QIcon(":/icons/faenza/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
|
|
|
}
|
2012-09-04 12:42:45 +02:00
|
|
|
else if (icon == QLatin1String("user-bookmarks")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("user-bookmarks", QIcon(":icons/faenza/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")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("list-remove", QIcon(":icons/faenza/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")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("go-next", QIcon(":icons/faenza/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")) {
|
2011-12-03 11:27:53 +01:00
|
|
|
return QIcon::fromTheme("go-previous", QIcon(":icons/faenza/go-previous.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()
|
|
|
|
{
|
|
|
|
return QPixmap::fromImage(m_emptyWebImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage IconProvider::emptyWebImage()
|
|
|
|
{
|
|
|
|
if (m_emptyWebImage.isNull()) {
|
|
|
|
m_emptyWebImage = fromTheme("text-plain").pixmap(16, 16).toImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_emptyWebImage;
|
|
|
|
}
|
|
|
|
|
2012-04-22 20:51:28 +02:00
|
|
|
QPixmap IconProvider::bookmarkIcon()
|
|
|
|
{
|
|
|
|
return m_bookmarkIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconProvider::setBookmarkIcon(const QPixmap &pixmap)
|
|
|
|
{
|
|
|
|
m_bookmarkIcon = pixmap;
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
QIcon IconProvider::iconFromImage(const QImage &image)
|
|
|
|
{
|
2012-03-13 17:51:06 +01:00
|
|
|
if (m_emptyWebImage.isNull()) {
|
|
|
|
m_emptyWebImage = fromTheme("text-plain").pixmap(16, 16).toImage();
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:27:24 +01:00
|
|
|
return QIcon(QPixmap::fromImage(image));
|
|
|
|
}
|
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
QIcon IconProvider::iconFromBase64(const QByteArray &data)
|
|
|
|
{
|
|
|
|
QIcon image;
|
|
|
|
QByteArray bArray = QByteArray::fromBase64(data);
|
|
|
|
QBuffer buffer(&bArray);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
QDataStream in(&buffer);
|
|
|
|
in >> image;
|
|
|
|
buffer.close();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!image.isNull()) {
|
2011-07-30 17:57:14 +02:00
|
|
|
return image;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-03-13 17:51:06 +01:00
|
|
|
return IconProvider::emptyWebIcon();
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray IconProvider::iconToBase64(const QIcon &icon)
|
|
|
|
{
|
|
|
|
QByteArray bArray;
|
|
|
|
QBuffer buffer(&bArray);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream out(&buffer);
|
|
|
|
out << icon;
|
|
|
|
buffer.close();
|
|
|
|
return bArray.toBase64();
|
|
|
|
}
|