2010-07-25 17:16:03 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2012-2015 Mnemosyne LLC
|
2010-07-25 17:16:03 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-07-25 17:16:03 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-09-17 05:38:13 +00:00
|
|
|
#include <array>
|
|
|
|
|
2010-07-25 17:16:03 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2017-02-11 10:44:34 +00:00
|
|
|
#include <QStandardPaths>
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FaviconCache.h"
|
2010-07-25 17:16:03 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FaviconCache::FaviconCache()
|
|
|
|
: nam_(new QNetworkAccessManager(this))
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(nam_, &QNetworkAccessManager::finished, this, &FaviconCache::onRequestFinished);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
namespace
|
2010-07-25 17:42:43 +00:00
|
|
|
{
|
2013-08-17 16:22:56 +00:00
|
|
|
|
2020-11-09 03:31:02 +00:00
|
|
|
QPixmap scale(QPixmap const& pixmap)
|
2020-08-26 19:00:39 +00:00
|
|
|
{
|
|
|
|
return pixmap.scaled(FaviconCache::getIconSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2010-07-25 17:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
QString getCacheDir()
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
2020-08-26 19:00:39 +00:00
|
|
|
auto const base = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
|
|
|
|
return QDir(base).absoluteFilePath(QStringLiteral("favicons"));
|
|
|
|
}
|
2019-11-12 01:37:05 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
QString getScrapedFile()
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
2020-08-26 19:00:39 +00:00
|
|
|
auto const base = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
|
|
|
|
return QDir(base).absoluteFilePath(QStringLiteral("favicons-scraped.txt"));
|
2019-11-12 01:37:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
void markUrlAsScraped(QString const& url_str)
|
|
|
|
{
|
|
|
|
auto skip_file = QFile(getScrapedFile());
|
|
|
|
if (skip_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
|
|
|
|
{
|
|
|
|
skip_file.write(url_str.toUtf8());
|
|
|
|
skip_file.write("\n");
|
|
|
|
}
|
2019-11-12 01:37:05 +00:00
|
|
|
}
|
2020-08-26 19:00:39 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
} // namespace
|
2020-08-26 19:00:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FaviconCache::ensureCacheDirHasBeenScanned()
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
static bool has_been_scanned = false;
|
2020-08-26 19:00:39 +00:00
|
|
|
if (has_been_scanned)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2020-08-26 19:00:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
has_been_scanned = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
// remember which hosts we've asked for a favicon so that we
|
|
|
|
// don't re-ask them every time we start a new session
|
|
|
|
auto skip_file = QFile(getScrapedFile());
|
2020-09-02 14:25:45 +00:00
|
|
|
if (skip_file.open(QIODevice::ReadOnly | QIODevice::Text))
|
2020-08-26 19:00:39 +00:00
|
|
|
{
|
|
|
|
while (!skip_file.atEnd())
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-08-26 19:00:39 +00:00
|
|
|
auto const url = QString::fromUtf8(skip_file.readLine()).trimmed();
|
|
|
|
auto const key = getKey(QUrl{ url });
|
|
|
|
keys_.insert({ url, key });
|
|
|
|
pixmaps_.try_emplace(key);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
// load the cached favicons
|
|
|
|
auto cache_dir = QDir(getCacheDir());
|
|
|
|
cache_dir.mkpath(cache_dir.absolutePath());
|
|
|
|
QStringList const files = cache_dir.entryList(QDir::Files | QDir::Readable);
|
|
|
|
for (auto const& file : files)
|
|
|
|
{
|
|
|
|
QPixmap pixmap(cache_dir.absoluteFilePath(file));
|
|
|
|
if (!pixmap.isNull())
|
|
|
|
{
|
|
|
|
pixmaps_[file] = scale(pixmap);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-06-24 18:28:54 +00:00
|
|
|
QString FaviconCache::getDisplayName(Key const& key)
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
|
|
|
auto name = key;
|
|
|
|
name[0] = name.at(0).toTitleCase();
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-06-24 18:28:54 +00:00
|
|
|
FaviconCache::Key FaviconCache::getKey(QUrl const& url)
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
|
|
|
auto host = url.host();
|
|
|
|
|
|
|
|
// remove tld
|
|
|
|
auto const suffix = url.topLevelDomain();
|
|
|
|
host.truncate(host.size() - suffix.size());
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
// remove subdomain
|
|
|
|
auto const pos = host.indexOf(QLatin1Char('.'));
|
|
|
|
return pos < 0 ? host : host.remove(0, pos + 1);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:28:54 +00:00
|
|
|
FaviconCache::Key FaviconCache::getKey(QString const& displayName)
|
2015-01-20 23:28:38 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
return displayName.toLower();
|
2015-01-20 23:28:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
QSize FaviconCache::getIconSize()
|
2010-07-30 22:23:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
return QSize(16, 16);
|
2010-07-30 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:28:54 +00:00
|
|
|
QPixmap FaviconCache::find(Key const& key)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ensureCacheDirHasBeenScanned();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return pixmaps_[key];
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
FaviconCache::Key FaviconCache::add(QString const& url_str)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ensureCacheDirHasBeenScanned();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
// find or add this url's key
|
|
|
|
auto k_it = keys_.find(url_str);
|
|
|
|
if (k_it != keys_.end())
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2020-08-26 19:00:39 +00:00
|
|
|
return k_it->second;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const url = QUrl{ url_str };
|
2020-08-26 19:00:39 +00:00
|
|
|
auto const key = getKey(url);
|
|
|
|
keys_.insert({ url_str, key });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-08-26 19:00:39 +00:00
|
|
|
// Try to download a favicon if we don't have one.
|
|
|
|
// Add a placeholder to prevent repeat downloads.
|
|
|
|
if (pixmaps_.try_emplace(key).second)
|
|
|
|
{
|
|
|
|
markUrlAsScraped(url_str);
|
|
|
|
|
2020-09-17 05:38:13 +00:00
|
|
|
auto const scrape = [this](auto const host)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
auto const schemes = std::array<QString, 2>{
|
|
|
|
QStringLiteral("http"),
|
|
|
|
QStringLiteral("https"),
|
|
|
|
};
|
|
|
|
auto const suffixes = std::array<QString, 5>{
|
|
|
|
QStringLiteral("gif"), //
|
|
|
|
QStringLiteral("ico"), //
|
|
|
|
QStringLiteral("jpg"), //
|
|
|
|
QStringLiteral("png"), //
|
|
|
|
QStringLiteral("svg"), //
|
|
|
|
};
|
|
|
|
for (auto const& scheme : schemes)
|
2020-09-17 05:38:13 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
for (auto const& suffix : suffixes)
|
2020-09-17 05:38:13 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const path = QStringLiteral("%1://%2/favicon.%3").arg(scheme).arg(host).arg(suffix);
|
|
|
|
nam_->get(QNetworkRequest(path));
|
2020-09-17 05:38:13 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
};
|
2020-09-17 05:38:13 +00:00
|
|
|
|
|
|
|
// tracker.domain.com
|
|
|
|
auto host = url.host();
|
|
|
|
scrape(host);
|
|
|
|
|
|
|
|
auto const delim = QStringLiteral(".");
|
|
|
|
auto const has_subdomain = host.count(delim) > 1;
|
|
|
|
if (has_subdomain)
|
|
|
|
{
|
|
|
|
auto const original_subdomain = host.left(host.indexOf(delim));
|
|
|
|
host.remove(0, original_subdomain.size() + 1);
|
|
|
|
// domain.com
|
|
|
|
scrape(host);
|
|
|
|
|
|
|
|
auto const www = QStringLiteral("www");
|
|
|
|
if (original_subdomain != www)
|
|
|
|
{
|
|
|
|
// www.domain.com
|
|
|
|
scrape(QStringLiteral("%1.%2").arg(www).arg(host));
|
|
|
|
}
|
|
|
|
}
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
2019-11-12 01:37:05 +00:00
|
|
|
|
|
|
|
return key;
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FaviconCache::onRequestFinished(QNetworkReply* reply)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
auto const key = getKey(reply->url());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QPixmap pixmap;
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QByteArray const content = reply->readAll();
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (reply->error() == QNetworkReply::NoError)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
pixmap.loadFromData(content);
|
|
|
|
}
|
2010-07-25 17:16:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!pixmap.isNull())
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// save it in memory...
|
2020-05-27 21:53:12 +00:00
|
|
|
pixmaps_[key] = scale(pixmap);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// save it on disk...
|
2020-06-05 19:02:11 +00:00
|
|
|
QDir cache_dir(getCacheDir());
|
|
|
|
cache_dir.mkpath(cache_dir.absolutePath());
|
|
|
|
QFile file(cache_dir.absoluteFilePath(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(content);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// notify listeners
|
2019-11-12 01:37:05 +00:00
|
|
|
emit pixmapReady(key);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
2020-08-26 19:00:39 +00:00
|
|
|
|
|
|
|
reply->deleteLater();
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|