expose the caching for wallpapers that don't use render() to use

svn path=/trunk/KDE/kdelibs/; revision=952060
This commit is contained in:
Aaron J. Seigo 2009-04-10 19:49:33 +00:00
parent f7bbfe7c86
commit 9f7edec998
3 changed files with 79 additions and 11 deletions

View File

@ -41,8 +41,9 @@ public:
{
};
QString cachePath(const QString &sourceImagePath, const QSize &size,
int resizeMethod, const QColor &color) const;
QString cachePath(const QString &key) const;
QString cacheKey(const QString &sourceImagePath, const QSize &size,
int resizeMethod, const QColor &color) const;
void renderCompleted(int token, const QImage &image,

View File

@ -22,6 +22,7 @@
#include <QColor>
#include <QFile>
#include <QFileInfo>
#include <QImage>
#include <kdebug.h>
@ -29,6 +30,8 @@
#include <kservicetypetrader.h>
#include <kstandarddirs.h>
#include <kio/job.h>
#include <version.h>
#include "plasma/private/dataengineconsumer_p.h"
@ -320,10 +323,10 @@ void Wallpaper::render(const QString &sourceImagePath, const QSize &size,
}
if (d->cacheRendering) {
QString cache = d->cachePath(sourceImagePath, size, resizeMethod, color);
if (QFile::exists(cache)) {
//kDebug() << "loading cached wallpaper from" << cache;
QImage img(cache);
QFileInfo info(sourceImagePath);
QString cache = d->cacheKey(sourceImagePath, size, resizeMethod, color);
QImage img;
if (findInCache(cache, img, info.lastModified().toTime_t())) {
emit renderCompleted(img);
return;
}
@ -333,13 +336,18 @@ void Wallpaper::render(const QString &sourceImagePath, const QSize &size,
//kDebug() << "rendering" << sourceImagePath << ", token is" << d->renderToken;
}
QString WallpaperPrivate::cachePath(const QString &sourceImagePath, const QSize &size,
int resizeMethod, const QColor &color) const
QString WallpaperPrivate::cacheKey(const QString &sourceImagePath, const QSize &size,
int resizeMethod, const QColor &color) const
{
const QString id = QString("plasma-wallpapers/%5_%3_%4_%1x%2.png")
const QString id = QString("%5_%3_%4_%1x%2")
.arg(size.width()).arg(size.height()).arg(color.name())
.arg(resizeMethod).arg(sourceImagePath);
return KGlobal::dirs()->locateLocal("cache", id);
return id;
}
QString WallpaperPrivate::cachePath(const QString &key) const
{
return KGlobal::dirs()->locateLocal("cache", "plasma-wallpapers/" + key + ".png");
}
void WallpaperPrivate::renderCompleted(int token, const QImage &image,
@ -352,13 +360,45 @@ void WallpaperPrivate::renderCompleted(int token, const QImage &image,
}
if (cacheRendering) {
image.save(cachePath(sourceImagePath, size, resizeMethod, color));
q->insertIntoCache(cacheKey(sourceImagePath, size, resizeMethod, color), image);
}
//kDebug() << "rendering complete!";
emit q->renderCompleted(image);
}
bool Wallpaper::findInCache(const QString &key, QImage &image, unsigned int lastModified)
{
if (d->cacheRendering) {
QString cache = d->cachePath(key);
if (QFile::exists(cache)) {
if (lastModified > 0) {
QFileInfo info(cache);
if (info.lastModified().toTime_t() < lastModified) {
return false;
}
}
image.load(cache);
return true;
}
}
return false;
}
void Wallpaper::insertIntoCache(const QString& key, const QImage &image)
{
//TODO: cache limits?
if (d->cacheRendering) {
if (image.isNull()) {
KIO::file_delete(d->cachePath(key));
} else {
image.save(d->cachePath(key));
}
}
}
} // Plasma namespace
#include "wallpaper.moc"

View File

@ -392,6 +392,33 @@ class PLASMA_EXPORT Wallpaper : public QObject
*/
void setUsingDiskCache(bool useCache);
/**
* Tries to load pixmap with the specified key from cache.
*
* @param key the name to use in the cache for this image
* @param image the image object to populate with the resulting data if found
* @param lastModified if non-zero, the time stamp is also checked on the file,
* and must be newer than the timestamp to be loaded
*
* @return true when pixmap was found and loaded from cache, false otherwise
* @since 4.3
**/
bool findInCache(const QString &key, QImage &image, unsigned int lastModified = 0);
/**
* Insert specified pixmap into the cache if usingDiskCache.
* If the cache already contains pixmap with the specified key then it is
* overwritten.
*
* @param key the name use in the cache for this image; if the image is specific
* to this wallpaper plugin, consider including the name() as part of
* the cache key to avoid collisions with other plugins.
* @param image the image to store in the cache
*
* @since 4.3
**/
void insertIntoCache(const QString& key, const QImage &image);
private:
Q_PRIVATE_SLOT(d, void renderCompleted(int token, const QImage &image,
const QString &sourceImagePath, const QSize &size,