7#include "gravatarresolvurljob.h"
10#include "gravatar_debug.h"
11#include "misc/gravatarcache.h"
13#include <PimCommon/NetworkManager>
15#include <QCryptographicHash>
16#include <QNetworkReply>
19using namespace Gravatar;
21class Gravatar::GravatarResolvUrlJobPrivate
24 GravatarResolvUrlJobPrivate() =
default;
36 int mBackends = Gravatar;
38 bool mHasGravatar =
false;
39 bool mUseDefaultPixmap =
false;
42GravatarResolvUrlJob::GravatarResolvUrlJob(
QObject *parent)
44 , d(new Gravatar::GravatarResolvUrlJobPrivate)
48GravatarResolvUrlJob::~GravatarResolvUrlJob() =
default;
50bool GravatarResolvUrlJob::canStart()
const
52 if (PimCommon::NetworkManager::self()->isOnline()) {
54 return !d->mEmail.trimmed().isEmpty() && (d->mEmail.contains(
QLatin1Char(
'@')));
60QUrl GravatarResolvUrlJob::generateGravatarUrl(
bool useLibravatar)
62 return createUrl(useLibravatar);
65bool GravatarResolvUrlJob::hasGravatar()
const
67 return d->mHasGravatar;
70void GravatarResolvUrlJob::startNetworkManager(
const QUrl &url)
72 if (!d->mNetworkAccessManager) {
75 d->mNetworkAccessManager->setStrictTransportSecurityEnabled(
true);
76 d->mNetworkAccessManager->enableStrictTransportSecurityStore(
true);
83 d->mNetworkAccessManager->get(req);
86void GravatarResolvUrlJob::start()
88 if (d->mBackends == GravatarResolvUrlJobPrivate::None) {
89 d->mBackends = GravatarResolvUrlJobPrivate::Gravatar;
92 d->mHasGravatar =
false;
96 qCDebug(GRAVATAR_LOG) <<
"Gravatar can not start";
101void GravatarResolvUrlJob::processNextBackend()
103 if (d->mHasGravatar || d->mBackends == GravatarResolvUrlJobPrivate::None) {
110 if (d->mBackends & GravatarResolvUrlJobPrivate::Libravatar) {
111 d->mBackends &= ~GravatarResolvUrlJobPrivate::Libravatar;
112 url = createUrl(
true);
113 }
else if (d->mBackends & GravatarResolvUrlJobPrivate::Gravatar) {
114 d->mBackends &= ~GravatarResolvUrlJobPrivate::Gravatar;
115 url = createUrl(
false);
120 if (!cacheLookup(d->mCalculatedHash)) {
121 startNetworkManager(url);
123 processNextBackend();
127void GravatarResolvUrlJob::slotFinishLoadPixmap(
QNetworkReply *reply)
131 d->mPixmap.loadFromData(data);
132 d->mHasGravatar =
true;
134 if (!d->mUseDefaultPixmap) {
135 GravatarCache::self()->saveGravatarPixmap(d->mCalculatedHash, d->mPixmap);
139 GravatarCache::self()->saveMissingGravatar(d->mCalculatedHash);
146 processNextBackend();
149QString GravatarResolvUrlJob::email()
const
154void GravatarResolvUrlJob::setEmail(
const QString &email)
159Hash GravatarResolvUrlJob::calculateHash()
161 const auto email = d->mEmail.toLower().toUtf8();
165bool GravatarResolvUrlJob::fallbackGravatar()
const
167 return d->mBackends & GravatarResolvUrlJobPrivate::Gravatar;
170void GravatarResolvUrlJob::setFallbackGravatar(
bool fallbackGravatar)
172 if (fallbackGravatar) {
173 d->mBackends |= GravatarResolvUrlJobPrivate::Gravatar;
175 d->mBackends &= ~GravatarResolvUrlJobPrivate::Gravatar;
179bool GravatarResolvUrlJob::useLibravatar()
const
181 return d->mBackends & GravatarResolvUrlJobPrivate::Libravatar;
184void GravatarResolvUrlJob::setUseLibravatar(
bool useLibravatar)
187 d->mBackends |= GravatarResolvUrlJobPrivate::Libravatar;
189 d->mBackends &= ~GravatarResolvUrlJobPrivate::Libravatar;
193bool GravatarResolvUrlJob::useDefaultPixmap()
const
195 return d->mUseDefaultPixmap;
198void GravatarResolvUrlJob::setUseDefaultPixmap(
bool useDefaultPixmap)
200 d->mUseDefaultPixmap = useDefaultPixmap;
203int GravatarResolvUrlJob::size()
const
208QPixmap GravatarResolvUrlJob::pixmap()
const
213void GravatarResolvUrlJob::setSize(
int size)
217 }
else if (size > 2048) {
223Hash GravatarResolvUrlJob::calculatedHash()
const
225 return d->mCalculatedHash;
228QUrl GravatarResolvUrlJob::createUrl(
bool useLibravatar)
231 d->mCalculatedHash = Hash();
236 if (!d->mUseDefaultPixmap) {
238 query.addQueryItem(QStringLiteral(
"d"), QStringLiteral(
"404"));
240 if (d->mSize != 80) {
245 url.
setHost(QStringLiteral(
"seccdn.libravatar.org"));
247 url.
setHost(QStringLiteral(
"secure.gravatar.com"));
249 d->mCalculatedHash = calculateHash();
250 url.
setPath(
"/avatar/"_L1 + d->mCalculatedHash.hexString());
255bool GravatarResolvUrlJob::cacheLookup(
const Hash &hash)
257 bool haveStoredPixmap =
false;
258 const QPixmap pix = GravatarCache::self()->loadGravatarPixmap(hash, haveStoredPixmap);
259 if (haveStoredPixmap && !pix.
isNull()) {
261 d->mHasGravatar =
true;
266 return haveStoredPixmap;
269#include "moc_gravatarresolvurljob.cpp"
std::optional< QSqlQuery > query(const QString &queryStatement)
QByteArray hash(QByteArrayView data, Algorithm method)
QString errorString() const const
void finished(QNetworkReply *reply)
NetworkError error() const const
QNetworkRequest request() const const
HttpPipeliningAllowedAttribute
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isNull() const const
QString number(double n, char format, int precision)
void setHost(const QString &host, ParsingMode mode)
void setPath(const QString &path, ParsingMode mode)
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)