2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-11-06 18:39:59 +00:00
|
|
|
#include "dive.h"
|
|
|
|
#include "metrics.h"
|
|
|
|
#include "divelist.h"
|
|
|
|
#include "qthelper.h"
|
|
|
|
#include "imagedownloader.h"
|
2018-03-10 13:15:50 +00:00
|
|
|
#include "qt-models/divepicturemodel.h"
|
|
|
|
#include "metadata.h"
|
2015-11-06 18:39:59 +00:00
|
|
|
#include <unistd.h>
|
2016-04-30 22:00:29 +00:00
|
|
|
#include <QString>
|
2018-03-30 05:57:30 +00:00
|
|
|
#include <QImageReader>
|
2018-03-10 13:15:50 +00:00
|
|
|
#include <QDataStream>
|
2018-05-09 21:08:44 +00:00
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QPainter>
|
2015-11-06 18:39:59 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2018-02-18 15:22:34 +00:00
|
|
|
static QUrl cloudImageURL(const char *filename)
|
2016-01-09 15:29:49 +00:00
|
|
|
{
|
2018-02-18 15:22:34 +00:00
|
|
|
QString hash = hashString(filename);
|
2016-01-09 15:29:49 +00:00
|
|
|
return QUrl::fromUserInput(QString("https://cloud.subsurface-divelog.org/images/").append(hash));
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
ImageDownloader::ImageDownloader(const QString &filename_in) : filename(filename_in)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2016-03-15 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:45:55 +00:00
|
|
|
void ImageDownloader::load(bool fromHash)
|
|
|
|
{
|
2018-03-07 15:37:31 +00:00
|
|
|
if (fromHash && loadFromUrl(cloudImageURL(qPrintable(filename))))
|
2018-02-08 21:45:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If loading from hash failed, try to load from filename
|
2018-03-07 15:37:31 +00:00
|
|
|
loadFromUrl(QUrl::fromUserInput(filename));
|
2018-02-08 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageDownloader::loadFromUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
bool success = false;
|
2015-11-06 18:39:59 +00:00
|
|
|
if (url.isValid()) {
|
|
|
|
QEventLoop loop;
|
2018-02-08 21:19:03 +00:00
|
|
|
QNetworkAccessManager manager;
|
2015-11-06 18:39:59 +00:00
|
|
|
QNetworkRequest request(url);
|
2018-02-08 21:45:55 +00:00
|
|
|
connect(&manager, &QNetworkAccessManager::finished, this,
|
|
|
|
[this,&success] (QNetworkReply *reply) { saveImage(reply, success); });
|
2018-02-09 22:07:42 +00:00
|
|
|
connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
|
2018-03-25 14:46:16 +00:00
|
|
|
qDebug() << "Downloading image from" << url;
|
2015-11-06 18:39:59 +00:00
|
|
|
QNetworkReply *reply = manager.get(request);
|
2018-02-09 22:07:42 +00:00
|
|
|
loop.exec();
|
2018-02-08 21:09:01 +00:00
|
|
|
delete reply;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-02-08 21:45:55 +00:00
|
|
|
return success;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:45:55 +00:00
|
|
|
void ImageDownloader::saveImage(QNetworkReply *reply, bool &success)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-02-08 21:45:55 +00:00
|
|
|
success = false;
|
2015-11-06 18:39:59 +00:00
|
|
|
QByteArray imageData = reply->readAll();
|
2018-02-08 22:16:20 +00:00
|
|
|
QImage image;
|
2015-11-06 18:39:59 +00:00
|
|
|
image.loadFromData(imageData);
|
2018-02-08 21:45:55 +00:00
|
|
|
if (image.isNull())
|
2015-11-06 18:39:59 +00:00
|
|
|
return;
|
2018-02-08 21:45:55 +00:00
|
|
|
success = true;
|
2015-11-06 18:39:59 +00:00
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(imageData);
|
|
|
|
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(path);
|
|
|
|
QFile imageFile(path.append("/").append(hash.result().toHex()));
|
|
|
|
if (imageFile.open(QIODevice::WriteOnly)) {
|
2018-03-25 14:46:16 +00:00
|
|
|
qDebug() << "Write image to" << imageFile.fileName();
|
2015-11-06 18:39:59 +00:00
|
|
|
QDataStream stream(&imageFile);
|
|
|
|
stream.writeRawData(imageData.data(), imageData.length());
|
|
|
|
imageFile.waitForBytesWritten(-1);
|
|
|
|
imageFile.close();
|
2018-03-07 15:37:31 +00:00
|
|
|
learnHash(filename, imageFile.fileName(), hash.result());
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2016-03-15 21:45:17 +00:00
|
|
|
// This should be called to make the picture actually show.
|
2016-04-05 05:02:03 +00:00
|
|
|
// Problem is DivePictureModel is not in core.
|
2016-03-15 21:45:17 +00:00
|
|
|
// Nevertheless, the image shows when the dive is selected the next time.
|
|
|
|
// DivePictureModel::instance()->updateDivePictures();
|
|
|
|
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
static void loadPicture(QString filename, bool fromHash)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-02-06 18:58:07 +00:00
|
|
|
static QSet<QString> queuedPictures;
|
|
|
|
static QMutex pictureQueueMutex;
|
|
|
|
|
2016-04-30 22:00:29 +00:00
|
|
|
QMutexLocker locker(&pictureQueueMutex);
|
2018-03-07 15:37:31 +00:00
|
|
|
if (queuedPictures.contains(filename))
|
2016-04-30 22:00:29 +00:00
|
|
|
return;
|
2018-03-07 15:37:31 +00:00
|
|
|
queuedPictures.insert(filename);
|
2018-02-06 19:04:09 +00:00
|
|
|
locker.unlock();
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
ImageDownloader download(filename);
|
2016-01-09 15:29:49 +00:00
|
|
|
download.load(fromHash);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 05:57:30 +00:00
|
|
|
// Overwrite QImage::load() so that we can perform better error reporting.
|
2018-03-04 15:40:06 +00:00
|
|
|
static QImage loadImage(const QString &fileName, const char *format = nullptr)
|
2018-03-30 05:57:30 +00:00
|
|
|
{
|
|
|
|
QImageReader reader(fileName, format);
|
2018-03-04 15:40:06 +00:00
|
|
|
QImage res = reader.read();
|
|
|
|
if (res.isNull())
|
2018-03-30 05:57:30 +00:00
|
|
|
qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString();
|
2018-03-04 15:40:06 +00:00
|
|
|
return res;
|
2018-03-30 05:57:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
QImage getHashedImage(const QString &file)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-03-04 15:40:06 +00:00
|
|
|
QImage res;
|
2018-03-07 15:37:31 +00:00
|
|
|
QUrl url = QUrl::fromUserInput(localFilePath(file));
|
|
|
|
if (url.isLocalFile())
|
2018-03-04 15:40:06 +00:00
|
|
|
res = loadImage(url.toLocalFile());
|
|
|
|
if (res.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// This did not load anything. Let's try to get the image from other sources
|
|
|
|
// Let's try to load it locally via its hash
|
2018-03-07 15:37:31 +00:00
|
|
|
QString filenameLocal = localFilePath(qPrintable(file));
|
|
|
|
qDebug() << QStringLiteral("Translated filename: %1 -> %2").arg(file, filenameLocal);
|
|
|
|
if (filenameLocal.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// That didn't produce a local filename.
|
|
|
|
// Try the cloud server
|
2018-02-18 15:22:34 +00:00
|
|
|
// TODO: This is dead code at the moment.
|
2018-03-10 13:15:50 +00:00
|
|
|
loadPicture(file, true);
|
2015-11-06 18:39:59 +00:00
|
|
|
} else {
|
2016-01-09 15:29:49 +00:00
|
|
|
// Load locally from translated file name
|
2018-03-07 15:37:31 +00:00
|
|
|
res = loadImage(filenameLocal);
|
2018-03-04 15:40:06 +00:00
|
|
|
if (!res.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// Make sure the hash still matches the image file
|
2018-03-10 13:15:50 +00:00
|
|
|
hashPicture(filenameLocal);
|
2016-01-09 15:29:49 +00:00
|
|
|
} else {
|
|
|
|
// Interpret filename as URL
|
2018-03-10 13:15:50 +00:00
|
|
|
loadPicture(filenameLocal, false);
|
2016-01-09 15:29:49 +00:00
|
|
|
}
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-01-09 15:29:49 +00:00
|
|
|
// We loaded successfully. Now, make sure hash is up to date.
|
2018-03-10 13:15:50 +00:00
|
|
|
hashPicture(file);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-03-04 15:40:06 +00:00
|
|
|
return res;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-03-10 13:15:50 +00:00
|
|
|
|
2018-05-09 21:08:44 +00:00
|
|
|
static QImage renderIcon(const char *id, int size)
|
|
|
|
{
|
|
|
|
QImage res(size, size, QImage::Format_ARGB32);
|
|
|
|
QSvgRenderer svg{QString(id)};
|
|
|
|
QPainter painter(&res);
|
|
|
|
svg.render(&painter);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnailer::Thumbnailer() : failImage(renderIcon(":filter-close", maxThumbnailSize())), // TODO: Don't misuse filter close icon
|
|
|
|
dummyImage(renderIcon(":camera-icon", maxThumbnailSize()))
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
// Currently, we only process one image at a time. Stefan Fuchs reported problems when
|
|
|
|
// calculating multiple thumbnails at once and this hopefully helps.
|
|
|
|
pool.setMaxThreadCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnailer *Thumbnailer::instance()
|
|
|
|
{
|
|
|
|
static Thumbnailer self;
|
|
|
|
return &self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QImage getThumbnailFromCache(const QString &picture_filename)
|
|
|
|
{
|
|
|
|
// First, check if we know a hash for this filename
|
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
if (filename.isEmpty())
|
|
|
|
return QImage();
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return QImage();
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
|
|
|
// Each thumbnail file is composed of a media-type and an image file.
|
|
|
|
// Currently, the type is ignored. This will be used to mark videos.
|
|
|
|
quint32 type;
|
|
|
|
QImage res;
|
|
|
|
stream >> type;
|
|
|
|
stream >> res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addThumbnailToCache(const QImage &thumbnail, const QString &picture_filename)
|
|
|
|
{
|
|
|
|
if (thumbnail.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
|
|
|
|
// If we got a thumbnail, we are guaranteed to have its hash and therefore
|
|
|
|
// thumbnailFileName() should return a filename.
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
qWarning() << "Internal error: can't get filename of recently created thumbnail";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSaveFile file(filename);
|
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
return;
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
|
|
|
// For format of the file, see comments in getThumnailForCache
|
|
|
|
quint32 type = MEDIATYPE_PICTURE;
|
|
|
|
stream << type;
|
|
|
|
stream << thumbnail;
|
|
|
|
file.commit();
|
|
|
|
}
|
|
|
|
|
2018-03-11 09:19:08 +00:00
|
|
|
void Thumbnailer::processItem(QString filename)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
QImage thumbnail = getThumbnailFromCache(filename);
|
|
|
|
|
|
|
|
if (thumbnail.isNull()) {
|
|
|
|
thumbnail = getHashedImage(filename);
|
|
|
|
if (thumbnail.isNull()) {
|
2018-03-11 09:19:08 +00:00
|
|
|
thumbnail = failImage;
|
2018-03-10 13:15:50 +00:00
|
|
|
} else {
|
2018-03-11 09:19:08 +00:00
|
|
|
int size = maxThumbnailSize();
|
2018-03-10 13:15:50 +00:00
|
|
|
thumbnail = thumbnail.scaled(size, size, Qt::KeepAspectRatio);
|
|
|
|
addThumbnailToCache(thumbnail, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
emit thumbnailChanged(filename, thumbnail);
|
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
2018-03-11 09:19:08 +00:00
|
|
|
QImage Thumbnailer::fetchThumbnail(PictureEntry &entry)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
|
|
|
|
// We are not currently fetching this thumbnail - add it to the list.
|
|
|
|
const QString &filename = entry.filename;
|
|
|
|
if (!workingOn.contains(filename)) {
|
|
|
|
workingOn.insert(filename,
|
2018-03-11 09:19:08 +00:00
|
|
|
QtConcurrent::run(&pool, [this, filename]() { processItem(filename); }));
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
2018-03-11 09:19:08 +00:00
|
|
|
return dummyImage;
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thumbnailer::clearWorkQueue()
|
|
|
|
{
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
for (auto it = workingOn.begin(); it != workingOn.end(); ++it)
|
|
|
|
it->cancel();
|
|
|
|
workingOn.clear();
|
|
|
|
}
|
2018-03-11 09:19:08 +00:00
|
|
|
|
|
|
|
static const int maxZoom = 3; // Maximum zoom: thrice of standard size
|
|
|
|
|
|
|
|
int Thumbnailer::defaultThumbnailSize()
|
|
|
|
{
|
|
|
|
return defaultIconMetrics().sz_pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Thumbnailer::maxThumbnailSize()
|
|
|
|
{
|
|
|
|
return defaultThumbnailSize() * maxZoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Thumbnailer::thumbnailSize(double zoomLevel)
|
|
|
|
{
|
|
|
|
// Calculate size of thumbnails. The standard size is defaultIconMetrics().sz_pic.
|
|
|
|
// We use exponential scaling so that the central point is the standard
|
|
|
|
// size and the minimum and maximum extreme points are a third respectively
|
|
|
|
// three times the standard size.
|
|
|
|
// Naturally, these three zoom levels are then represented by
|
|
|
|
// -1.0 (minimum), 0 (standard) and 1.0 (maximum). The actual size is
|
|
|
|
// calculated as standard_size*3.0^zoomLevel.
|
|
|
|
return static_cast<int>(round(defaultThumbnailSize() * pow(maxZoom, zoomLevel)));
|
|
|
|
}
|