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/>.
23#include "ImageCache.h"
25ImageCache::ImageCache()
26 : QQuickImageProvider(QQmlImageProviderBase::Image,
27 QQmlImageProviderBase::ForceAsynchronousImageLoading)
31QString 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");
41QFileInfo 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);
55bool ImageCache::needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &imageSize, 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 finalSize = calculateSize(imageSize, requestedSize);
66 if (finalSize.isValid() && cacheSize != finalSize)
72QSize ImageCache::calculateSize(const QSize &imageSize, const QSize &requestedSize)
74 QSize finalSize(requestedSize);
76 if (finalSize.width() == 0) {
77 finalSize.setWidth(imageSize.width() * (((double)finalSize.height()) / imageSize.height()));
78 } else if (finalSize.height() == 0) {
79 finalSize.setHeight(imageSize.height() * (((double)finalSize.width()) / imageSize.width()));
85QImage ImageCache::loadAndCacheImage(QImageReader &reader, const QFileInfo &cachePath, const QSize &finalSize)
87 reader.setQuality(100);
88 reader.setScaledSize(finalSize);
89 auto format = reader.format(); // can't get this after reading
91 QImage loadedImage(reader.read());
92 if (loadedImage.isNull()) {
93 qWarning() << "ImageCache could not read image" << reader.fileName() << ":" << reader.errorString();
97 cachePath.dir().mkpath(QStringLiteral("."));
98 loadedImage.save(cachePath.filePath(), format, 100);
103QImage ImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
106 QImageReader imageReader(image.toLocalFile());
107 QSize imageSize(imageReader.size());
110 // Early exit here, with no sourceSize, scaled-up sourceSize, or bad source image
111 if ((requestedSize.width() <= 0 && requestedSize.height() <= 0) ||
112 imageSize.isEmpty() ||
113 requestedSize.height() >= imageSize.height() ||
114 requestedSize.width() >= imageSize.width()) {
115 // We're only interested in scaling down, not up.
116 result = imageReader.read();
117 *size = result.size();
121 auto cachePath = imagePath(image);
124 if (needsUpdate(image, cachePath, imageSize, requestedSize, finalSize)) {
125 if (finalSize.isEmpty()) {
126 finalSize = calculateSize(imageSize, requestedSize);
128 result = loadAndCacheImage(imageReader, cachePath, finalSize);
130 result = QImage(cachePath.filePath());
133 *size = result.size();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list