2011-12-04 16:54:57 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-12-04 16:54:57 +01: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-12-02 23:25:27 +01:00
|
|
|
#include "speeddial.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "pagethumbnailer.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-12-02 23:25:27 +01:00
|
|
|
|
|
|
|
SpeedDial::SpeedDial(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_loaded(false)
|
|
|
|
, m_regenerateScript(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::loadSettings()
|
|
|
|
{
|
|
|
|
m_loaded = true;
|
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-12-02 23:25:27 +01:00
|
|
|
settings.beginGroup("SpeedDial");
|
2011-12-04 20:35:49 +01:00
|
|
|
m_allPages = settings.value("pages", "").toString();
|
2012-01-09 16:23:03 +01:00
|
|
|
m_bgImg = settings.value("background", "").toString();
|
|
|
|
m_bgImgSize = settings.value("backsize", "auto").toString();
|
2011-12-02 23:25:27 +01:00
|
|
|
settings.endGroup();
|
|
|
|
|
2011-12-04 20:35:49 +01:00
|
|
|
if (m_allPages.isEmpty()) {
|
|
|
|
m_allPages = "url:\"http://www.google.com\"|title:\"Google\";"
|
2012-01-02 13:56:52 +01:00
|
|
|
"url:\"http://www.qupzilla.com\"|title:\"QupZilla\";"
|
2012-01-15 18:22:59 +01:00
|
|
|
"url:\"http://blog.qupzilla.com\"|title:\"QupZilla Blog\";"
|
2011-12-04 20:35:49 +01:00
|
|
|
"url:\"https://github.com/nowrep/QupZilla\"|title:\"QupZilla GitHub\";"
|
|
|
|
"url:\"http://facebook.com\"|title:\"Facebook\";";
|
|
|
|
}
|
|
|
|
|
2011-12-09 21:56:01 +01:00
|
|
|
m_thumbnailsDir = mApp->getActiveProfilPath() + "thumbnails/";
|
2011-12-02 23:25:27 +01:00
|
|
|
|
|
|
|
// If needed, create thumbnails directory
|
|
|
|
if (!QDir(m_thumbnailsDir).exists()) {
|
|
|
|
QDir(mApp->getActiveProfilPath()).mkdir("thumbnails");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::saveSettings()
|
|
|
|
{
|
2012-01-08 10:56:50 +01:00
|
|
|
if (m_allPages.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-12-02 23:25:27 +01:00
|
|
|
settings.beginGroup("SpeedDial");
|
|
|
|
settings.setValue("pages", m_allPages);
|
2012-01-09 16:23:03 +01:00
|
|
|
settings.setValue("background", m_bgImg);
|
|
|
|
settings.setValue("backsize", m_bgImgSize);
|
2011-12-02 23:25:27 +01:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::addWebFrame(QWebFrame* frame)
|
|
|
|
{
|
|
|
|
if (!m_webFrames.contains(frame)) {
|
|
|
|
m_webFrames.append(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-24 13:31:32 +01:00
|
|
|
void SpeedDial::addPage(const QUrl &url, const QString &title)
|
|
|
|
{
|
|
|
|
if (url.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString sitePair = QString("url:\"%1\"|title:\"%2\";").arg(url.toString(), title);
|
|
|
|
|
|
|
|
m_allPages.append(sitePair);
|
|
|
|
|
|
|
|
for (int i = 0; i < m_webFrames.count(); i++) {
|
|
|
|
QWebFrame* frame = m_webFrames.at(i).data();
|
|
|
|
if (!frame) {
|
|
|
|
m_webFrames.removeAt(i);
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->page()->triggerAction(QWebPage::Reload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 16:23:03 +01:00
|
|
|
QString SpeedDial::backgroundImage()
|
|
|
|
{
|
|
|
|
if (!m_loaded) {
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_bgImg;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SpeedDial::backgroundImageSize()
|
|
|
|
{
|
|
|
|
if (!m_loaded) {
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_bgImgSize;
|
|
|
|
}
|
|
|
|
|
2011-12-02 23:25:27 +01:00
|
|
|
QString SpeedDial::initialScript()
|
|
|
|
{
|
|
|
|
if (!m_loaded) {
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_regenerateScript) {
|
|
|
|
return m_initialScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_regenerateScript = false;
|
|
|
|
m_initialScript.clear();
|
|
|
|
|
|
|
|
QStringList entries = m_allPages.split("\";");
|
|
|
|
|
|
|
|
foreach(QString entry, entries) {
|
|
|
|
if (entry.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList tmp = entry.split("\"|");
|
|
|
|
if (tmp.count() != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString url = tmp.at(0).mid(5);
|
|
|
|
QString title = tmp.at(1).mid(7);
|
|
|
|
|
2011-12-04 16:54:57 +01:00
|
|
|
QString imgSource = m_thumbnailsDir + QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md4).toHex() + ".png";
|
2011-12-02 23:25:27 +01:00
|
|
|
|
|
|
|
if (!QFile(imgSource).exists()) {
|
|
|
|
loadThumbnail(url);
|
2011-12-24 12:21:23 +01:00
|
|
|
imgSource = "qrc:html/loading.gif";
|
2011-12-02 23:25:27 +01:00
|
|
|
|
|
|
|
if (url.isEmpty()) {
|
|
|
|
imgSource = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-12-03 15:51:08 +01:00
|
|
|
imgSource = QUrl::fromLocalFile(imgSource).toString();
|
2011-12-02 23:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_initialScript.append(QString("addBox('%1', '%2', '%3');\n").arg(url, title, imgSource));
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_initialScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::changed(const QString &allPages)
|
|
|
|
{
|
2011-12-04 20:35:49 +01:00
|
|
|
if (allPages.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-02 23:25:27 +01:00
|
|
|
m_allPages = allPages;
|
|
|
|
m_regenerateScript = true;
|
|
|
|
}
|
|
|
|
|
2011-12-23 13:47:38 +01:00
|
|
|
void SpeedDial::loadThumbnail(const QString &url, bool loadTitle)
|
2011-12-02 23:25:27 +01:00
|
|
|
{
|
|
|
|
if (url.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageThumbnailer* thumbnailer = new PageThumbnailer(this);
|
|
|
|
thumbnailer->setUrl(QUrl(url));
|
2011-12-23 13:47:38 +01:00
|
|
|
thumbnailer->setLoadTitle(loadTitle);
|
2011-12-02 23:25:27 +01:00
|
|
|
connect(thumbnailer, SIGNAL(thumbnailCreated(QPixmap)), this, SLOT(thumbnailCreated(QPixmap)));
|
|
|
|
|
|
|
|
thumbnailer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::removeImageForUrl(const QString &url)
|
|
|
|
{
|
2011-12-04 16:54:57 +01:00
|
|
|
QString fileName = m_thumbnailsDir + QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md4).toHex() + ".png";
|
2011-12-02 23:25:27 +01:00
|
|
|
|
|
|
|
if (QFile(fileName).exists()) {
|
|
|
|
QFile(fileName).remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedDial::thumbnailCreated(const QPixmap &image)
|
|
|
|
{
|
|
|
|
PageThumbnailer* thumbnailer = qobject_cast<PageThumbnailer*>(sender());
|
|
|
|
if (!thumbnailer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-23 13:47:38 +01:00
|
|
|
bool loadTitle = thumbnailer->loadTitle();
|
|
|
|
QString title = thumbnailer->title();
|
2011-12-02 23:25:27 +01:00
|
|
|
QString url = thumbnailer->url().toString();
|
2011-12-04 16:54:57 +01:00
|
|
|
QString fileName = m_thumbnailsDir + QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md4).toHex() + ".png";
|
2011-12-02 23:25:27 +01:00
|
|
|
|
2011-12-11 12:34:51 +01:00
|
|
|
if (image.isNull()) {
|
|
|
|
fileName = "qrc:/html/broken-page.png";
|
2011-12-23 13:47:38 +01:00
|
|
|
title = tr("Unable to load");
|
|
|
|
loadTitle = true;
|
2011-12-11 12:34:51 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!image.save(fileName)) {
|
|
|
|
qWarning() << "SpeedDial::thumbnailCreated Cannot save thumbnail to " << fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName = QUrl::fromLocalFile(fileName).toString();
|
2011-12-02 23:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_regenerateScript = true;
|
|
|
|
|
2011-12-09 21:56:01 +01:00
|
|
|
for (int i = 0; i < m_webFrames.count(); i++) {
|
|
|
|
QWebFrame* frame = m_webFrames.at(i).data();
|
2011-12-02 23:25:27 +01:00
|
|
|
if (!frame) {
|
2011-12-09 21:56:01 +01:00
|
|
|
m_webFrames.removeAt(i);
|
|
|
|
i--;
|
2011-12-02 23:25:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-03 15:33:23 +01:00
|
|
|
frame->evaluateJavaScript(QString("setImageToUrl('%1', '%2');").arg(url, fileName));
|
2011-12-23 13:47:38 +01:00
|
|
|
if (loadTitle) {
|
|
|
|
frame->evaluateJavaScript(QString("setTitleToUrl('%1', '%2');").arg(url, title));
|
|
|
|
}
|
2011-12-02 23:25:27 +01:00
|
|
|
}
|
2011-12-04 19:15:44 +01:00
|
|
|
|
|
|
|
thumbnailer->deleteLater();
|
2011-12-02 23:25:27 +01:00
|
|
|
}
|