1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 08:13:27 +00:00
transmission/qt/FaviconCache.cc
Charles Kerr c62cb35fd4
qt client speedups
* faster updating of trackers combobox.
* generate trackerDisplayNames just once per torrent
* refactor: cache torrent delegate's warning emblem
* refactor: change mainwin refresh debounce to 200ms
* refactor: do not store trackers, hosts in QVariant
* refactor: don't use `virtual` when it's not needed
* refactor: faster counting torrents-matching-filter
* refactor: faster tracker handling in filterbar
* refactor: improve json parser's prealloc heuristic
* refactor: make Torrent::hasError() faster
* refactor: remove redundant speed stats collection
* refactor: remove unnecessary tor->isQueued() calls
* refactor: use unordered containers where possible
* scale favicons only once, when adding to the cache
2019-11-11 19:37:05 -06:00

178 lines
3.7 KiB
C++

/*
* This file Copyright (C) 2012-2015 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <QDir>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QStandardPaths>
#include "FaviconCache.h"
/***
****
***/
FaviconCache::FaviconCache()
{
myNAM = new QNetworkAccessManager();
connect(myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
}
FaviconCache::~FaviconCache()
{
delete myNAM;
}
/***
****
***/
QString FaviconCache::getCacheDir()
{
QString const base = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
return QDir(base).absoluteFilePath(QLatin1String("favicons"));
}
namespace
{
QPixmap scale(QPixmap pixmap)
{
return pixmap.scaled(FaviconCache::getIconSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
void FaviconCache::ensureCacheDirHasBeenScanned()
{
static bool hasBeenScanned = false;
if (!hasBeenScanned)
{
hasBeenScanned = true;
QDir cacheDir(getCacheDir());
cacheDir.mkpath(cacheDir.absolutePath());
QStringList files = cacheDir.entryList(QDir::Files | QDir::Readable);
for (QString const& file : files)
{
QPixmap pixmap;
pixmap.load(cacheDir.absoluteFilePath(file));
if (!pixmap.isNull())
{
myPixmaps[file] = scale(pixmap);
}
}
}
}
/***
****
***/
QString FaviconCache::getDisplayName(QUrl const& url)
{
return getDisplayName(getKey(url));
}
QString FaviconCache::getDisplayName(QString const& key)
{
auto name = key;
name[0] = name.at(0).toTitleCase();
return name;
}
QString FaviconCache::getKey(QUrl const& url)
{
auto host = url.host();
// remove tld
auto const suffix = url.topLevelDomain();
host.truncate(host.size() - suffix.size());
// remove subdomain
auto const pos = host.indexOf(QLatin1Char('.'));
return pos < 0 ? host : host.remove(0, pos + 1);
}
QString FaviconCache::getKey(QString const& displayName)
{
return displayName.toLower();
}
QSize FaviconCache::getIconSize()
{
return QSize(16, 16);
}
QPixmap FaviconCache::find(QString const& key)
{
ensureCacheDirHasBeenScanned();
return myPixmaps[key];
}
QString FaviconCache::add(QUrl const& url)
{
ensureCacheDirHasBeenScanned();
QString const key = getKey(url);
if (myPixmaps.count(key) == 0)
{
// add a placholder s.t. we only ping the server once per session
myPixmaps[key] = QPixmap();
// try to download the favicon
QString const path = QLatin1String("http://") + url.host() + QLatin1String("/favicon.");
QStringList suffixes;
suffixes << QLatin1String("ico") << QLatin1String("png") << QLatin1String("gif") << QLatin1String("jpg");
for (QString const& suffix : suffixes)
{
myNAM->get(QNetworkRequest(path + suffix));
}
}
return key;
}
void FaviconCache::onRequestFinished(QNetworkReply* reply)
{
auto const key = getKey(reply->url());
QPixmap pixmap;
QByteArray const content = reply->readAll();
if (reply->error() == QNetworkReply::NoError)
{
pixmap.loadFromData(content);
}
if (!pixmap.isNull())
{
// save it in memory...
myPixmaps[key] = scale(pixmap);
// save it on disk...
QDir cacheDir(getCacheDir());
cacheDir.mkpath(cacheDir.absolutePath());
QFile file(cacheDir.absoluteFilePath(key));
file.open(QIODevice::WriteOnly);
file.write(content);
file.close();
// notify listeners
emit pixmapReady(key);
}
}