File indexing completed on 2025-01-05 05:14:39
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "gravatarcache.h" 0008 0009 #include <QCryptographicHash> 0010 #include <QFile> 0011 #include <QNetworkAccessManager> 0012 #include <QNetworkReply> 0013 #include <QNetworkRequest> 0014 #include <QStandardPaths> 0015 0016 GravatarCache::GravatarCache(QObject *parent) 0017 : QObject{parent} 0018 { 0019 QStandardPaths::writableLocation(QStandardPaths::CacheLocation); 0020 } 0021 0022 QString GravatarCache::avatarPath(const QString &email) 0023 { 0024 auto emailHash = QCryptographicHash::hash(email.toUtf8(), QCryptographicHash::Sha3_256).toHex().toLower(); 0025 0026 if (mAvatarsCache.contains(emailHash)) 0027 return mAvatarsCache[emailHash]; 0028 0029 auto avatarUrl = "https://www.gravatar.com/avatar/" + emailHash; 0030 0031 QNetworkRequest request{QUrl{avatarUrl}}; 0032 0033 QNetworkReply *reply = mNet.get(request); 0034 connect(reply, &QNetworkReply::finished, [&reply]() { 0035 QFile avatarFile; 0036 if (!avatarFile.open(QIODevice::WriteOnly)) 0037 return; 0038 avatarFile.write(reply->readAll()); 0039 avatarFile.close(); 0040 }); 0041 return {}; 0042 }