Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit eb508869 authored by Laurent Montel's avatar Laurent Montel 😁

Use cache to avoid to load from disk or check server

parent 0d79914e
......@@ -16,12 +16,17 @@
*/
#include "gravatarcache.h"
#include <KGlobal>
#include <KStandardDirs>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
using namespace PimCommon;
GravatarCache::GravatarCache(QObject *parent)
: QObject(parent),
mMaximumSize(0)
K_GLOBAL_STATIC( GravatarCache, s_gravatarCache )
GravatarCache::GravatarCache()
: mMaximumSize(0)
{
}
......@@ -31,16 +36,53 @@ GravatarCache::~GravatarCache()
}
void GravatarCache::saveGravatarPixmap(const QString &hashStr, const QPixmap &pixmap)
GravatarCache *GravatarCache::self()
{
return s_gravatarCache;
}
void GravatarCache::saveGravatarPixmap(const QString &hashStr, const QPixmap &pixmap)
{
if (!hashStr.isEmpty() && !pixmap.isNull()) {
if (!mCachePixmap.contains(hashStr)) {
const QString path = KGlobal::dirs()->locateLocal("data", QLatin1String("gravatar/") + hashStr + QLatin1String(".png"));
if (!pixmap.save(path)) {
qDebug() <<" saved in cache "<< hashStr;
mCachePixmap.insert(hashStr, pixmap);
}
}
}
}
bool GravatarCache::loadGravatarPixmap(const QString &hashStr, bool &gravatarStored)
QPixmap GravatarCache::loadGravatarPixmap(const QString &hashStr, bool &gravatarStored)
{
//TODO
return false;
gravatarStored = false;
qDebug()<<" hashStr"<<hashStr;
if (!hashStr.isEmpty()) {
if (mCachePixmap.contains(hashStr)) {
qDebug()<<" contains in cache "<< hashStr;
return mCachePixmap.value(hashStr);
} else {
const QString path = KGlobal::dirs()->locateLocal("data", QLatin1String("gravatar/") + hashStr + QLatin1String(".png"));
QFileInfo fi(path);
if (fi.exists()) {
QPixmap pix;
if (pix.load(path)) {
qDebug() << " add to cache "<<hashStr;
mCachePixmap.insert(hashStr, pix);
gravatarStored = true;
return pix;
}
} else {
return QPixmap();
}
}
}
return QPixmap();
}
int GravatarCache::maximumSize() const
{
return mMaximumSize;
......
......@@ -18,25 +18,27 @@
#ifndef GRAVATARCACHE_H
#define GRAVATARCACHE_H
#include <QObject>
#include <QPixmap>
#include <QHash>
namespace PimCommon {
class GravatarCache : public QObject
class GravatarCache
{
Q_OBJECT
public:
explicit GravatarCache(QObject *parent = 0);
static GravatarCache *self();
GravatarCache();
~GravatarCache();
void saveGravatarPixmap(const QString &hashStr, const QPixmap &pixmap);
bool loadGravatarPixmap(const QString &hashStr, bool &gravatarStored);
QPixmap loadGravatarPixmap(const QString &hashStr, bool &gravatarStored);
int maximumSize() const;
void setMaximumSize(int maximumSize);
private:
QHash<QString, QPixmap> mCachePixmap;
int mMaximumSize;
};
}
......
......@@ -19,6 +19,7 @@
#include "gravatarresolvurljob.h"
#include "gravatarcache.h"
#include <QCryptographicHash>
#include <QStringList>
#include <QDebug>
......@@ -67,17 +68,25 @@ void GravatarResolvUrlJob::start()
mCalculatedHash.clear();
const KUrl url = createUrl();
Q_EMIT resolvUrl(url);
if ( Solid::Networking::status() == Solid::Networking::Unconnected ) {
qDebug() <<" network is not connected";
bool haveStoredPixmap = false;
const QPixmap pix = GravatarCache::self()->loadGravatarPixmap(mCalculatedHash, haveStoredPixmap);
if (haveStoredPixmap && !pix.isNull()) {
mPixmap = pix;
Q_EMIT finished(this);
deleteLater();
return;
} else {
if ( Solid::Networking::status() == Solid::Networking::Unconnected ) {
qDebug() <<" network is not connected";
deleteLater();
return;
}
if (!mNetworkAccessManager) {
mNetworkAccessManager = new QNetworkAccessManager(this);
connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinishLoadPixmap(QNetworkReply*)));
}
QNetworkReply *reply = mNetworkAccessManager->get(QNetworkRequest(url));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}
if (!mNetworkAccessManager) {
mNetworkAccessManager = new QNetworkAccessManager(this);
connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinishLoadPixmap(QNetworkReply*)));
}
QNetworkReply *reply = mNetworkAccessManager->get(QNetworkRequest(url));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
} else {
qDebug() << "Gravatar can not start";
deleteLater();
......@@ -89,6 +98,7 @@ void GravatarResolvUrlJob::slotFinishLoadPixmap(QNetworkReply *reply)
if (reply->error() == QNetworkReply::NoError) {
mPixmap.loadFromData(reply->readAll());
mHasGravatar = true;
GravatarCache::self()->saveGravatarPixmap(mCalculatedHash, mPixmap);
}
reply->deleteLater();
Q_EMIT finished(this);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment