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"
|
|
|
|
#include <unistd.h>
|
2016-04-30 22:00:29 +00:00
|
|
|
#include <QString>
|
2015-11-06 18:39:59 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2018-02-06 18:58:07 +00:00
|
|
|
static QUrl cloudImageURL(const char *hash)
|
2016-01-09 15:29:49 +00:00
|
|
|
{
|
|
|
|
return QUrl::fromUserInput(QString("https://cloud.subsurface-divelog.org/images/").append(hash));
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:39:59 +00:00
|
|
|
ImageDownloader::ImageDownloader(struct picture *pic)
|
|
|
|
{
|
|
|
|
picture = pic;
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:31:59 +00:00
|
|
|
ImageDownloader::~ImageDownloader()
|
|
|
|
{
|
|
|
|
picture_free(picture);
|
|
|
|
}
|
|
|
|
|
2018-02-08 21:45:55 +00:00
|
|
|
void ImageDownloader::load(bool fromHash)
|
|
|
|
{
|
|
|
|
if (fromHash && loadFromUrl(cloudImageURL(picture->hash)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If loading from hash failed, try to load from filename
|
|
|
|
loadFromUrl(QUrl::fromUserInput(QString(picture->filename)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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)) {
|
|
|
|
QDataStream stream(&imageFile);
|
|
|
|
stream.writeRawData(imageData.data(), imageData.length());
|
|
|
|
imageFile.waitForBytesWritten(-1);
|
|
|
|
imageFile.close();
|
|
|
|
add_hash(imageFile.fileName(), hash.result());
|
|
|
|
learnHash(picture, hash.result());
|
|
|
|
}
|
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-02-06 18:58:07 +00:00
|
|
|
static void loadPicture(struct picture *picture, 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-29 14:18:06 +00:00
|
|
|
if (!picture)
|
|
|
|
return;
|
2016-04-30 22:00:29 +00:00
|
|
|
QMutexLocker locker(&pictureQueueMutex);
|
2018-02-08 21:05:29 +00:00
|
|
|
if (queuedPictures.contains(QString(picture->filename))) {
|
|
|
|
picture_free(picture);
|
2016-04-30 22:00:29 +00:00
|
|
|
return;
|
2018-02-08 21:05:29 +00:00
|
|
|
}
|
2016-04-30 22:00:29 +00:00
|
|
|
queuedPictures.insert(QString(picture->filename));
|
2018-02-06 19:04:09 +00:00
|
|
|
locker.unlock();
|
|
|
|
|
2015-11-06 18:39:59 +00:00
|
|
|
ImageDownloader download(picture);
|
2016-01-09 15:29:49 +00:00
|
|
|
download.load(fromHash);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SHashedImage::SHashedImage(struct picture *picture) : QImage()
|
|
|
|
{
|
2016-03-15 21:45:17 +00:00
|
|
|
QUrl url = QUrl::fromUserInput(localFilePath(QString(picture->filename)));
|
2015-11-06 18:39:59 +00:00
|
|
|
if(url.isLocalFile())
|
|
|
|
load(url.toLocalFile());
|
|
|
|
if (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
|
|
|
|
QString filename = fileFromHash(picture->hash);
|
2016-12-15 13:31:20 +00:00
|
|
|
if (filename.isNull())
|
|
|
|
filename = QString(picture->filename);
|
2016-01-09 15:29:49 +00:00
|
|
|
if (filename.isNull()) {
|
|
|
|
// That didn't produce a local filename.
|
|
|
|
// Try the cloud server
|
2016-03-15 20:31:59 +00:00
|
|
|
QtConcurrent::run(loadPicture, clone_picture(picture), true);
|
2015-11-06 18:39:59 +00:00
|
|
|
} else {
|
2016-01-09 15:29:49 +00:00
|
|
|
// Load locally from translated file name
|
|
|
|
load(filename);
|
|
|
|
if (!isNull()) {
|
|
|
|
// Make sure the hash still matches the image file
|
2016-03-15 20:31:59 +00:00
|
|
|
QtConcurrent::run(updateHash, clone_picture(picture));
|
2016-01-09 15:29:49 +00:00
|
|
|
} else {
|
|
|
|
// Interpret filename as URL
|
2016-03-15 20:31:59 +00:00
|
|
|
QtConcurrent::run(loadPicture, clone_picture(picture), 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.
|
2016-03-15 20:31:59 +00:00
|
|
|
QtConcurrent::run(hashPicture, clone_picture(picture));
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 15:29:49 +00:00
|
|
|
|