its better to not cache ..

This commit is contained in:
Patrick von Reth 2011-07-22 14:56:51 +02:00
parent f90397a370
commit a7aff40dc2
2 changed files with 49 additions and 31 deletions

View File

@ -20,6 +20,7 @@
#include <QCryptographichash> #include <QCryptographichash>
#include <QBuffer> #include <QBuffer>
#include <QHash> #include <QHash>
#include <QFile>
#include <QDebug> #include <QDebug>
@ -35,14 +36,21 @@ public:
SnoreIconData(const QImage &img): SnoreIconData(const QImage &img):
_img(img), _img(img),
_isLocalFile(false) _isLocalFile(false)
{ } {}
SnoreIconData(const QString &url){
if(QFile(url).exists()){
_isLocalFile = true;
_localFileName = url;
}
}
~SnoreIconData() ~SnoreIconData()
{ } { }
QImage _img; QImage _img;
QByteArray _data; QByteArray _data;
QString _localFileName;
QString _hash; QString _hash;
bool _isLocalFile; bool _isLocalFile;
@ -60,10 +68,14 @@ SnoreIcon::SnoreIcon()
} }
SnoreIcon::SnoreIcon(const QImage &img) SnoreIcon::SnoreIcon(const QImage &img)
{ {
d = QSharedPointer<SnoreIconData>(new SnoreIconData(img)); d = QSharedPointer<SnoreIconData>(new SnoreIconData(img));
} }
SnoreIcon::SnoreIcon(const QString &url){
d = QSharedPointer<SnoreIconData>(new SnoreIconData(url));
}
SnoreIcon::SnoreIcon(const SnoreIcon &other): SnoreIcon::SnoreIcon(const SnoreIcon &other):
d(other.d) d(other.d)
{ } { }
@ -72,28 +84,25 @@ SnoreIcon::~SnoreIcon()
{ } { }
const QString &SnoreIcon::hash() const{
if(d->_hash.isEmpty()){
QCryptographicHash h(QCryptographicHash::Md5);
h.addData(imageData());
d->_hash = h.result().toHex();
}
return d->_hash;
}
const QImage &SnoreIcon::image() const{ const QImage &SnoreIcon::image() const{
if(d->_img.isNull() && d->_isLocalFile){
d->_img = QImage(d->_localFileName);
}
return d->_img; return d->_img;
} }
const QString &SnoreIcon::localUrl()const{ const QString &SnoreIcon::localUrl()const{
QString h = hash(); if(d->_localFileName.isEmpty()){
if(hasedImages.contains(h)) if(hasedImages.contains(hash())){
return hasedImages[h]; d->_localFileName = hasedImages[hash()];
QString fp = SnoreServer::snoreTMP(); }else{
fp = fp.append("/").append(h).append(".png"); d->_localFileName = SnoreServer::snoreTMP();
d->_img.save(fp,"PNG"); d->_localFileName = d->_localFileName .append("/").append(hash()).append(".png");
hasedImages[h] = fp; hasedImages[hash()] = d->_localFileName;
return hasedImages[h]; d->_img.save(d->_localFileName ,"PNG");
}
}
return d->_localFileName;
} }
const QByteArray &SnoreIcon::imageData() const{ const QByteArray &SnoreIcon::imageData() const{
@ -105,6 +114,15 @@ const QByteArray &SnoreIcon::imageData() const{
return d->_data; return d->_data;
} }
const QString &SnoreIcon::hash() const{
if(d->_hash.isEmpty()){
QCryptographicHash h(QCryptographicHash::Md5);
h.addData(imageData());
d->_hash = h.result().toHex();
}
return d->_hash;
}
const bool SnoreIcon::isLocalFile() const const bool SnoreIcon::isLocalFile() const
{ {
return d->_isLocalFile; return d->_isLocalFile;

View File

@ -26,25 +26,25 @@
class SNORE_EXPORT SnoreIcon class SNORE_EXPORT SnoreIcon
{ {
public: public:
SnoreIcon(); SnoreIcon();
SnoreIcon(const QImage &img); SnoreIcon(const QImage &img);
SnoreIcon(const QByteArray &img); SnoreIcon(const class QString &url);
SnoreIcon(const SnoreIcon &other); SnoreIcon(const SnoreIcon &other);
~SnoreIcon(); ~SnoreIcon();
const QImage &image() const; const QImage &image() const;
const QString &localUrl() const; const QString &localUrl() const;
const QByteArray &imageData() const; const QByteArray &imageData() const;
const bool isLocalFile() const; const QString &hash() const;
const bool isLocalFile() const;
private: private:
static QHash<QString,QString> hasedImages; static QHash<QString,QString> hasedImages;
private: private:
class SnoreIconData; class SnoreIconData;
QSharedPointer<SnoreIconData> d; QSharedPointer<SnoreIconData> d;
const QString &hash() const;