7#include "gravatarcache.h"
10#include "gravatar_debug.h"
18#include <QStandardPaths>
23using namespace Gravatar;
27class Gravatar::GravatarCachePrivate
31 inline void insertMissingHash(std::vector<T> &vec,
const T &hash)
33 auto it = std::lower_bound(vec.begin(), vec.end(), hash);
34 if (it != vec.end() && *it == hash) {
41 inline void saveVector(
const std::vector<T> &vec,
const QString &fileName)
45 qCWarning(GRAVATAR_LOG) <<
"Can't write missing hashes cache file:" << f.fileName() << f.errorString();
49 f.resize(vec.size() *
sizeof(T));
50 f.write(
reinterpret_cast<const char *
>(vec.data()), vec.size() *
sizeof(T));
55 inline void loadVector(std::vector<T> &vec,
const QString &fileName)
61 QFile f(mGravatarPath + fileName);
65 if (f.size() %
sizeof(T) != 0) {
66 qCWarning(GRAVATAR_LOG) <<
"Missing hash cache is corrupt:" << f.fileName();
69 vec.resize(f.size() /
sizeof(T));
70 f.read(
reinterpret_cast<char *
>(vec.data()), f.size());
75 std::vector<Hash128> mMd5Misses;
76 std::vector<Hash256> mSha256Misses;
79GravatarCache::GravatarCache()
80 : d(new Gravatar::GravatarCachePrivate)
82 d->mCachePixmap.setMaxCost(20);
88GravatarCache::~GravatarCache() =
default;
92 return s_gravatarCache;
95void GravatarCache::saveGravatarPixmap(
const Hash &hash,
const QPixmap &pixmap)
97 if (!hash.isValid() || pixmap.
isNull()) {
101 const QString path = d->mGravatarPath + hash.hexString() +
".png"_L1;
102 qCDebug(GRAVATAR_LOG) <<
" path " <<
path;
103 if (pixmap.
save(path)) {
104 qCDebug(GRAVATAR_LOG) <<
" saved in cache " <<
path;
105 d->mCachePixmap.insert(hash,
new QPixmap(pixmap));
109void GravatarCache::saveMissingGravatar(
const Hash &hash)
111 switch (hash.type()) {
115 d->insertMissingHash(d->mMd5Misses, hash.md5());
116 d->saveVector(d->mMd5Misses, QStringLiteral(
"missing.md5"));
119 d->insertMissingHash(d->mSha256Misses, hash.sha256());
120 d->saveVector(d->mSha256Misses, QStringLiteral(
"missing.sha256"));
125QPixmap GravatarCache::loadGravatarPixmap(
const Hash &hash,
bool &gravatarStored)
127 gravatarStored =
false;
129 if (!hash.isValid()) {
134 if (d->mCachePixmap.contains(hash)) {
135 qCDebug(GRAVATAR_LOG) <<
" contains in cache " << hash.hexString();
136 gravatarStored =
true;
137 return *(d->mCachePixmap.object(hash));
141 const QString path = d->mGravatarPath + hash.hexString() +
".png"_L1;
144 if (pix.
load(path)) {
145 qCDebug(GRAVATAR_LOG) <<
" add to cache " << hash.hexString() <<
path;
146 d->mCachePixmap.insert(hash,
new QPixmap(pix));
147 gravatarStored =
true;
153 switch (hash.type()) {
157 d->loadVector(d->mMd5Misses, QStringLiteral(
"missing.md5"));
158 gravatarStored = std::binary_search(d->mMd5Misses.begin(), d->mMd5Misses.end(), hash.md5());
161 d->loadVector(d->mSha256Misses, QStringLiteral(
"missing.sha256"));
162 gravatarStored = std::binary_search(d->mSha256Misses.begin(), d->mSha256Misses.end(), hash.sha256());
169int GravatarCache::maximumSize()
const
171 return d->mCachePixmap.maxCost();
174void GravatarCache::setMaximumSize(
int maximumSize)
176 if (d->mCachePixmap.maxCost() != maximumSize) {
177 d->mCachePixmap.setMaxCost(maximumSize);
181void GravatarCache::clear()
183 d->mCachePixmap.clear();
186void GravatarCache::clearAllCache()
192 const QFileInfoList
list =
dir.entryInfoList();
199 d->mMd5Misses.clear();
200 d->mSha256Misses.clear();
Cache for both positive and negative avatar lookups.
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
bool mkpath(const QString &dirPath) const const
bool exists() const const
bool isNull() const const
bool load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
bool save(QIODevice *device, const char *format, int quality) const const
QString writableLocation(StandardLocation type)
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)