fixed image cache

This commit is contained in:
Patrick von Reth 2014-01-14 14:20:39 +01:00
parent 188adb974d
commit 271050cb22
3 changed files with 44 additions and 40 deletions

View File

@ -30,19 +30,22 @@ using namespace Snore;
IconData::IconData(const QString &url):
m_url(url),
m_hash(SnoreCorePrivate::computeHash(m_url.toLatin1())),
m_localUrl(QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash)),
m_isLocalFile(false),
m_isResource(m_url.startsWith(":/"))
{
if(!m_isResource && QFile(url).exists())
{
m_isLocalFile = true;
m_localUrl = url;
}
m_isRemoteFile = !m_isLocalFile && ! m_isResource;
}
IconData::IconData(const QImage &img):
m_img(img),
m_hash(SnoreCorePrivate::computeHash((char*)img.constBits())),
m_hash(SnoreCorePrivate::computeHash(dataFromImage(img))),
m_localUrl(QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash)),
m_isLocalFile(false),
m_isResource(false),
m_isRemoteFile(false)
@ -59,18 +62,9 @@ const QByteArray &Snore::IconData::imageData()
{
if(m_data.isEmpty())
{
if(!m_isLocalFile)
if(m_isRemoteFile)
{
if(!m_img.isNull())
{
QBuffer buffer( &m_data );
buffer.open( QBuffer::WriteOnly );
m_img.save( &buffer, "PNG" );
}
else if(m_isRemoteFile)
{
download();
}
download();
}
}
return m_data;
@ -95,33 +89,34 @@ const QImage &IconData::image()
QString IconData::localUrl()
{
if(m_isLocalFile)
if(!m_isLocalFile && !QFile(m_localUrl).exists())
{
return m_url;
}
else
{
QString out(QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash));
if(!QFile(out).exists())
{
image().save(out ,"PNG");
}
return out;
image().save(m_localUrl ,"PNG");
}
return m_localUrl;
}
void IconData::download()
{
if(m_data.isEmpty())
if(m_isRemoteFile && m_data.isEmpty())
{
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkRequest request(m_url);
request.setRawHeader("User-Agent", "SnoreNotify");
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
m_data = reply->readAll();
if(!QFile(m_localUrl).exists())
{
qDebug() << "Downloading:" << m_url;
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkRequest request(m_url);
request.setRawHeader("User-Agent", "SnoreNotify");
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
m_data = reply->readAll();
}
else
{
m_img = QImage(m_localUrl);
m_data = dataFromImage(m_img);
}
}
}

View File

@ -44,10 +44,20 @@ public:
QString localUrl();
void download();
inline QByteArray dataFromImage(const QImage &image)
{
QByteArray data;
QBuffer buffer( &data );
buffer.open( QBuffer::WriteOnly );
image.save( &buffer, "PNG" );
return data;
}
QImage m_img;
QByteArray m_data;
QString m_url;
QString m_url;
QString m_hash;
QString m_localUrl;
bool m_isLocalFile;
bool m_isResource;
bool m_isRemoteFile;

View File

@ -31,7 +31,12 @@ using namespace Snore;
QString const SnoreCorePrivate::snoreTMP(){
static const QString tmp = QString("%1/SnoreNotify/").arg(QDir::temp().path());
static QString tmp;
if(tmp.isNull())
{
tmp = QString("%1/libsnore/").arg(QDir::tempPath());
QDir(tmp).mkpath(".");
}
return tmp;
}
@ -49,12 +54,6 @@ SnoreCorePrivate::SnoreCorePrivate(QSystemTrayIcon *trayIcon):
m_trayIcon(trayIcon),
m_defaultApp("SnoreNotify",Icon(":/root/snore.png"))
{
QDir home ( snoreTMP() );
if ( !home.exists() ){
home.cdUp();
home.mkdir("SnoreNotify");
}
m_defaultApp.addAlert(Alert("Default"));
}