mirror of
https://github.com/transmission/transmission
synced 2024-12-27 18:18:10 +00:00
be74cb6356
* 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>
284 lines
6.6 KiB
C++
284 lines
6.6 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 <array>
|
|
|
|
#include <QDir>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkRequest>
|
|
#include <QStandardPaths>
|
|
|
|
#include "FaviconCache.h"
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
Q_NETWORK_EXPORT bool qIsEffectiveTLD(QStringView domain);
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
|
|
QString getTopLevelDomain(QUrl const& url)
|
|
{
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
auto const host = url.host();
|
|
auto const dot = QChar(QLatin1Char('.'));
|
|
|
|
for (auto dot_pos = host.indexOf(dot); dot_pos != -1; dot_pos = host.indexOf(dot, dot_pos + 1))
|
|
{
|
|
if (qIsEffectiveTLD(QStringView(&host.data()[dot_pos + 1], host.size() - dot_pos - 1)))
|
|
{
|
|
return host.mid(dot_pos);
|
|
}
|
|
}
|
|
|
|
return {};
|
|
|
|
#else
|
|
|
|
return url.topLevelDomain();
|
|
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
FaviconCache::FaviconCache()
|
|
: nam_(new QNetworkAccessManager(this))
|
|
{
|
|
connect(nam_, &QNetworkAccessManager::finished, this, &FaviconCache::onRequestFinished);
|
|
}
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
namespace
|
|
{
|
|
|
|
QPixmap scale(QPixmap const& pixmap)
|
|
{
|
|
return pixmap.scaled(FaviconCache::getIconSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
}
|
|
|
|
QString getCacheDir()
|
|
{
|
|
auto const base = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
|
|
return QDir(base).absoluteFilePath(QStringLiteral("favicons"));
|
|
}
|
|
|
|
QString getScrapedFile()
|
|
{
|
|
auto const base = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
|
|
return QDir(base).absoluteFilePath(QStringLiteral("favicons-scraped.txt"));
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void FaviconCache::ensureCacheDirHasBeenScanned()
|
|
{
|
|
static bool has_been_scanned = false;
|
|
if (has_been_scanned)
|
|
{
|
|
return;
|
|
}
|
|
|
|
has_been_scanned = true;
|
|
|
|
// 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());
|
|
if (skip_file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
{
|
|
while (!skip_file.atEnd())
|
|
{
|
|
auto const url = QString::fromUtf8(skip_file.readLine()).trimmed();
|
|
auto const key = getKey(QUrl{ url });
|
|
keys_.insert({ url, key });
|
|
pixmaps_.try_emplace(key);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
QString FaviconCache::getDisplayName(Key const& key)
|
|
{
|
|
auto name = key;
|
|
if (!name.isEmpty())
|
|
{
|
|
name.front() = name.front().toTitleCase();
|
|
}
|
|
return name;
|
|
}
|
|
|
|
FaviconCache::Key FaviconCache::getKey(QUrl const& url)
|
|
{
|
|
auto host = url.host();
|
|
|
|
// remove tld
|
|
auto const suffix = getTopLevelDomain(url);
|
|
host.truncate(host.size() - suffix.size());
|
|
|
|
// remove subdomain
|
|
auto const pos = host.indexOf(QLatin1Char('.'));
|
|
return pos < 0 ? host : host.remove(0, pos + 1);
|
|
}
|
|
|
|
FaviconCache::Key FaviconCache::getKey(QString const& displayName)
|
|
{
|
|
return displayName.toLower();
|
|
}
|
|
|
|
QSize FaviconCache::getIconSize()
|
|
{
|
|
return QSize(16, 16);
|
|
}
|
|
|
|
QPixmap FaviconCache::find(Key const& key)
|
|
{
|
|
ensureCacheDirHasBeenScanned();
|
|
|
|
return pixmaps_[key];
|
|
}
|
|
|
|
FaviconCache::Key FaviconCache::add(QString const& url_str)
|
|
{
|
|
ensureCacheDirHasBeenScanned();
|
|
|
|
// find or add this url's key
|
|
auto k_it = keys_.find(url_str);
|
|
if (k_it != keys_.end())
|
|
{
|
|
return k_it->second;
|
|
}
|
|
|
|
auto const url = QUrl{ url_str };
|
|
auto const key = getKey(url);
|
|
keys_.insert({ url_str, key });
|
|
|
|
// 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);
|
|
|
|
auto const scrape = [this](auto const host)
|
|
{
|
|
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)
|
|
{
|
|
for (auto const& suffix : suffixes)
|
|
{
|
|
auto const path = QStringLiteral("%1://%2/favicon.%3").arg(scheme).arg(host).arg(suffix);
|
|
nam_->get(QNetworkRequest(path));
|
|
}
|
|
}
|
|
};
|
|
|
|
// 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));
|
|
}
|
|
}
|
|
}
|
|
|
|
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...
|
|
pixmaps_[key] = scale(pixmap);
|
|
|
|
// save it on disk...
|
|
QDir cache_dir(getCacheDir());
|
|
cache_dir.mkpath(cache_dir.absolutePath());
|
|
QFile file(cache_dir.absoluteFilePath(key));
|
|
file.open(QIODevice::WriteOnly);
|
|
file.write(content);
|
|
file.close();
|
|
|
|
// notify listeners
|
|
emit pixmapReady(key);
|
|
}
|
|
|
|
reply->deleteLater();
|
|
}
|