2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
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
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
#include "FaviconCache.h"
|
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6
* Switch from QRegExp to QRegularExpression
While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.
* Use qIsEffectiveTLD instead of QUrl::topLevelDomain
The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.
* Use QStyle::State_Horizontal state when drawing progress bars
Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.
* Don't use QStringRef (and associated methods)
While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.
* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate
The latter was deprecated and replaced with the former in Qt 5.6.
* Don't use QApplication::globalStrut
This property has been deprecated in Qt 5.15 and removed in Qt 6.
* Use QImage::fromHICON instead of QtWin::fromHICON
WinExtras module (providind the latter helper) has been removed in Qt 6.
* Use QStringDecoder instead of QTextCodec
While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.
* Don't forward-declare QStringList
Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.
* Use explicit (since Qt 6) QFileInfo constructor
* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t
The latter was deprecated in Qt 5.8 and removed in Qt 6.
* Don't use QFuture<>'s operator==
It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.
* Use std::vector<> instead of QVector<>
The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.
* Don't use + for flags, cast to int explicitly
Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.
* Support Qt 6 in CMake and for MSI packaging
* Remove extra (empty) CMake variable use when constructing Qt target names
* Simplify logic in tr_qt_add_translation CMake helper
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-03 21:20:11 +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
|
|
|
}
|
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
void markSiteAsScraped(QString const& sitename)
|
2020-08-26 19:00:39 +00:00
|
|
|
{
|
|
|
|
auto skip_file = QFile(getScrapedFile());
|
|
|
|
if (skip_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
|
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
skip_file.write(sitename.toUtf8());
|
2020-08-26 19:00:39 +00:00
|
|
|
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
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto skip_file = QFile(getScrapedFile()); 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
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto const sitename = QString::fromUtf8(skip_file.readLine()).trimmed();
|
|
|
|
pixmaps_.try_emplace(sitename);
|
2020-08-26 19:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
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());
|
2022-02-12 17:30:27 +00:00
|
|
|
for (auto const& sitename : cache_dir.entryList(QDir::Files | QDir::Readable))
|
2020-08-26 19:00:39 +00:00
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
QPixmap const pixmap(cache_dir.absoluteFilePath(sitename));
|
2020-08-26 19:00:39 +00:00
|
|
|
if (!pixmap.isNull())
|
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
pixmaps_[sitename] = scale(pixmap);
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
QString FaviconCache::getDisplayName(QString const& sitename)
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto name = sitename;
|
2021-11-01 16:31:55 +00:00
|
|
|
if (!name.isEmpty())
|
|
|
|
{
|
|
|
|
name.front() = name.front().toTitleCase();
|
|
|
|
}
|
2019-11-12 01:37:05 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize FaviconCache::getIconSize()
|
2010-07-30 22:23:47 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
return { 16, 16 };
|
2010-07-30 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
QPixmap FaviconCache::find(QString const& sitename)
|
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
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
return pixmaps_[sitename];
|
2010-07-25 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
void FaviconCache::add(QString const& sitename, 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
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
// Try to download a favicon if we don't have one.
|
|
|
|
// Add a placeholder to prevent repeat downloads.
|
2022-02-14 05:44:38 +00:00
|
|
|
if (auto const already_had_it = !pixmaps_.try_emplace(sitename).second; already_had_it)
|
2010-07-25 17:16:03 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
return;
|
2020-08-26 19:00:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
markSiteAsScraped(sitename);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
auto const scrape = [this, sitename](auto const host)
|
2020-08-26 19:00:39 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +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)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
for (auto const& suffix : suffixes)
|
2020-09-17 05:38:13 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto const path = QStringLiteral("%1://%2/favicon.%3").arg(scheme).arg(host).arg(suffix);
|
|
|
|
auto request = QNetworkRequest(path);
|
|
|
|
request.setAttribute(QNetworkRequest::UserMax, sitename);
|
|
|
|
nam_->get(request);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2022-02-12 17:30:27 +00:00
|
|
|
}
|
|
|
|
};
|
2020-09-17 05:38:13 +00:00
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
// scrape tracker.domain.com
|
|
|
|
auto const host = QUrl(url_str).host();
|
|
|
|
scrape(host);
|
2020-09-17 05:38:13 +00:00
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
if (auto const idx = host.indexOf(sitename); idx != -1)
|
|
|
|
{
|
|
|
|
// scrape domain.com
|
|
|
|
auto const root = host.mid(idx);
|
|
|
|
if (root != host)
|
2020-09-17 05:38:13 +00:00
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
scrape(root);
|
|
|
|
}
|
2020-09-17 05:38:13 +00:00
|
|
|
|
2022-02-12 17:30:27 +00:00
|
|
|
// scrape www.domain.com
|
|
|
|
if (auto const www = QStringLiteral("www.") + root; www != host)
|
|
|
|
{
|
|
|
|
scrape(www);
|
2020-09-17 05:38:13 +00:00
|
|
|
}
|
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
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto const content = reply->readAll();
|
|
|
|
auto pixmap = QPixmap{};
|
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
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
auto sitename = reply->request().attribute(QNetworkRequest::UserMax).toString();
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// save it in memory...
|
2022-02-12 17:30:27 +00:00
|
|
|
pixmaps_[sitename] = scale(pixmap);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// save it on disk...
|
2022-07-27 14:03:13 +00:00
|
|
|
QDir const cache_dir(getCacheDir());
|
2020-06-05 19:02:11 +00:00
|
|
|
cache_dir.mkpath(cache_dir.absolutePath());
|
2022-02-12 17:30:27 +00:00
|
|
|
QFile file(cache_dir.absoluteFilePath(sitename));
|
2017-04-19 12:04:45 +00:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(content);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// notify listeners
|
2022-02-12 17:30:27 +00:00
|
|
|
emit pixmapReady(sitename);
|
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
|
|
|
}
|