2
* Copyright (C) 2016 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <QImageReader>
23
#include "ImageCache.h"
25
ImageCache::ImageCache()
26
: QQuickImageProvider(QQmlImageProviderBase::Image,
27
QQmlImageProviderBase::ForceAsynchronousImageLoading)
31
QString ImageCache::imageCacheRoot()
33
QString xdgCache(qgetenv("XDG_CACHE_HOME"));
34
if (xdgCache.isEmpty()) {
35
xdgCache = QDir::homePath() + QStringLiteral("/.cache");
38
return QDir::cleanPath(xdgCache) + QStringLiteral("/unity8/imagecache");
41
QFileInfo ImageCache::imagePath(const QUrl &image)
43
QUrlQuery query(image);
45
auto name = query.queryItemValue(QStringLiteral("name"));
47
name = QStringLiteral("/paths") + image.toLocalFile();
49
name = QStringLiteral("/names/") + name;
52
return QFileInfo(imageCacheRoot() + name);
55
bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &requestedSize, QSize &finalSize)
57
if (!cachePath.exists())
60
QFileInfo imageInfo(image.toLocalFile());
61
if (imageInfo.lastModified() > cachePath.lastModified())
64
QSize cacheSize(QImageReader(cachePath.filePath()).size());
65
QSize imageSize(QImageReader(imageInfo.filePath()).size());
66
finalSize = calculateSize(imageSize, requestedSize);
67
if (finalSize.isValid() && cacheSize != finalSize)
73
QSize ImageCache::calculateSize(const QSize &imageSize, const QSize &requestedSize)
75
QSize finalSize(requestedSize);
77
if (finalSize.width() == 0) {
78
finalSize.setWidth(imageSize.width() * (((double)finalSize.height()) / imageSize.height()));
79
} else if (finalSize.height() == 0) {
80
finalSize.setHeight(imageSize.height() * (((double)finalSize.width()) / imageSize.width()));
86
QImage ImageCache::loadAndCacheImage(const QUrl &image, const QFileInfo &cachePath, const QSize &finalSize)
88
QImageReader reader(image.toLocalFile());
89
reader.setQuality(100);
90
reader.setScaledSize(finalSize);
91
auto format = reader.format(); // can't get this after reading
93
QImage loadedImage(reader.read());
94
if (loadedImage.isNull()) {
95
qWarning() << "ImageCache could not read image" << image.path();
99
cachePath.dir().mkpath(QStringLiteral("."));
100
loadedImage.save(cachePath.filePath(), format, 100);
105
QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
108
QSize imageSize(QImageReader(image.toLocalFile()).size());
111
// Early exit here, with no sourceSize, scaled-up sourceSize, or bad source image
112
if ((requestedSize.width() <= 0 && requestedSize.height() <= 0) ||
113
imageSize.isEmpty() ||
114
requestedSize.height() >= imageSize.height() ||
115
requestedSize.width() >= imageSize.width()) {
116
// We're only interested in scaling down, not up.
117
result = QImage(image.toLocalFile());
118
*size = result.size();
122
auto cachePath = imagePath(image);
125
if (needsUpdate(image, cachePath, requestedSize, finalSize)) {
126
if (finalSize.isEmpty()) {
127
finalSize = calculateSize(imageSize, requestedSize);
129
result = loadAndCacheImage(image, cachePath, finalSize);
131
result = QImage(cachePath.filePath());
134
*size = result.size();