2010-07-25 17:16:03 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-07-25 17:16:03 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2010-07-25 17:16:03 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2013-08-17 16:22:56 +00:00
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#else
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#endif
|
2010-07-25 17:16:03 +00:00
|
|
|
|
|
|
|
#include "favicon.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: Favicons ()
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
myNAM = new QNetworkAccessManager ();
|
|
|
|
connect (myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: ~Favicons ()
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
delete myNAM;
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-25 17:42:43 +00:00
|
|
|
QString
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: getCacheDir ()
|
2010-07-25 17:42:43 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString base =
|
2013-08-17 16:22:56 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2013-09-14 22:45:04 +00:00
|
|
|
QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
|
2013-08-17 16:22:56 +00:00
|
|
|
#else
|
2013-09-14 22:45:04 +00:00
|
|
|
QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
|
2013-08-17 16:22:56 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return QDir(base).absoluteFilePath ("favicons");
|
2010-07-25 17:42:43 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 17:16:03 +00:00
|
|
|
void
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: ensureCacheDirHasBeenScanned ()
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
static bool hasBeenScanned = false;
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!hasBeenScanned)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
hasBeenScanned = true;
|
|
|
|
|
|
|
|
QDir cacheDir (getCacheDir ());
|
|
|
|
cacheDir.mkpath (cacheDir.absolutePath ());
|
|
|
|
|
|
|
|
QStringList files = cacheDir.entryList (QDir::Files|QDir::Readable);
|
|
|
|
foreach (QString file, files)
|
|
|
|
{
|
|
|
|
QPixmap pixmap;
|
|
|
|
pixmap.load (cacheDir.absoluteFilePath (file));
|
|
|
|
if (!pixmap.isNull ())
|
|
|
|
myPixmaps.insert (file, pixmap);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: getHost (const QUrl& url)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QString host = url.host ();
|
|
|
|
const int first_dot = host.indexOf ('.');
|
|
|
|
const int last_dot = host.lastIndexOf ('.');
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if ((first_dot != -1) && (last_dot != -1) && (first_dot != last_dot))
|
|
|
|
host.remove (0, first_dot + 1);
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return host;
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: find (const QUrl& url)
|
2010-07-30 22:23:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
return findFromHost (getHost (url));
|
2010-07-30 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const QSize rightSize (16, 16);
|
2010-07-30 22:23:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
QPixmap
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: findFromHost (const QString& host)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
ensureCacheDirHasBeenScanned ();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
const QPixmap pixmap = myPixmaps[ host ];
|
|
|
|
return pixmap.size()==rightSize ? pixmap : pixmap.scaled(rightSize);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: add (const QUrl& url)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
ensureCacheDirHasBeenScanned ();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString host = getHost (url);
|
2010-07-25 20:38:44 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!myPixmaps.contains (host))
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// add a placholder s.t. we only ping the server once per session
|
|
|
|
QPixmap tmp (rightSize);
|
|
|
|
tmp.fill (Qt::transparent);
|
|
|
|
myPixmaps.insert (host, tmp);
|
|
|
|
|
|
|
|
// try to download the favicon
|
|
|
|
const QString path = "http://" + host + "/favicon.";
|
|
|
|
QStringList suffixes;
|
|
|
|
suffixes << "ico" << "png" << "gif" << "jpg";
|
|
|
|
foreach (QString suffix, suffixes)
|
|
|
|
myNAM->get (QNetworkRequest (path + suffix));
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-14 22:45:04 +00:00
|
|
|
Favicons :: onRequestFinished (QNetworkReply * reply)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString host = reply->url().host();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
QPixmap pixmap;
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
const QByteArray content = reply->readAll ();
|
|
|
|
if (!reply->error ())
|
|
|
|
pixmap.loadFromData (content);
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!pixmap.isNull ())
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// save it in memory...
|
|
|
|
myPixmaps.insert (host, pixmap);
|
|
|
|
|
|
|
|
// save it on disk...
|
|
|
|
QDir cacheDir (getCacheDir ());
|
|
|
|
cacheDir.mkpath (cacheDir.absolutePath ());
|
|
|
|
QFile file (cacheDir.absoluteFilePath (host));
|
|
|
|
file.open (QIODevice::WriteOnly);
|
|
|
|
file.write (content);
|
|
|
|
file.close ();
|
|
|
|
|
|
|
|
// notify listeners
|
|
|
|
emit pixmapReady (host);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
}
|