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
|
|
|
|
2011-10-28 17:52:42 +02:00
|
|
|
IconProvider::IconProvider(QObject* parent)
|
|
|
|
: QObject(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()));
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:31:48 +02:00
|
|
|
void IconProvider::saveIcon(WebView* view)
|
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-01-21 20:27:45 +01:00
|
|
|
if (item.image == QWebSettings::webGraphic(QWebSettings::DefaultFrameIconGraphic).toImage()) {
|
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-01-17 19:27:24 +01:00
|
|
|
return QWebSettings::webGraphic(QWebSettings::DefaultFrameIconGraphic).toImage();
|
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;
|
|
|
|
query.exec("SELECT icon FROM icons WHERE url LIKE '%" + url.host() + "%'");
|
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)
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
return mApp->style()->standardIcon(icon);
|
|
|
|
#else
|
|
|
|
switch (icon) {
|
|
|
|
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:
|
|
|
|
return QIcon(":/icons/faenza/forward.png");
|
|
|
|
|
2011-10-01 17:55:10 +02:00
|
|
|
case QStyle::SP_ArrowBack:
|
2011-09-30 21:44:18 +02:00
|
|
|
return QIcon(":/icons/faenza/back.png");
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QIcon();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap IconProvider::standardPixmap(QStyle::StandardPixmap icon)
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
return mApp->style()->standardPixmap(icon);
|
|
|
|
#else
|
|
|
|
switch (icon) {
|
|
|
|
case QStyle::SP_DialogCloseButton:
|
|
|
|
return QPixmap(":/icons/faenza/close.png");
|
|
|
|
|
|
|
|
case QStyle::SP_BrowserStop:
|
|
|
|
return QPixmap(":/icons/faenza/stop.png");
|
|
|
|
|
|
|
|
case QStyle::SP_BrowserReload:
|
|
|
|
return QPixmap(":/icons/faenza/reload.png");
|
|
|
|
|
|
|
|
case QStyle::SP_ArrowForward:
|
|
|
|
return QPixmap(":/icons/faenza/forward.png");
|
|
|
|
|
2011-10-01 17:55:10 +02:00
|
|
|
case QStyle::SP_ArrowBack:
|
2011-09-30 21:44:18 +02:00
|
|
|
return QPixmap(":/icons/faenza/back.png");
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QPixmap();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconProvider::fromTheme(const QString &icon)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (icon == "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
|
|
|
}
|
|
|
|
else if (icon == "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
|
|
|
}
|
|
|
|
else if (icon == "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
|
|
|
}
|
|
|
|
else if (icon == "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
|
|
|
}
|
|
|
|
else if (icon == "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
|
|
|
}
|
|
|
|
else if (icon == "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-01-17 19:27:24 +01:00
|
|
|
QIcon IconProvider::iconFromImage(const QImage &image)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
return QWebSettings::webGraphic(QWebSettings::DefaultFrameIconGraphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
IconProvider::~IconProvider()
|
|
|
|
{
|
|
|
|
}
|