diff options
author | Robert C. Helling <helling@atdotde.de> | 2016-01-09 16:29:49 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2016-01-10 14:12:36 -0800 |
commit | 82c87204e48654a9b2661c68fe66b9f16f81c4ba (patch) | |
tree | 49a379cc0684b2d2194ffc549f96d5a476ab0235 /subsurface-core/imagedownloader.cpp | |
parent | 3ae6326847d84cddf633ff3b9ea5651a76e6d1ca (diff) | |
download | subsurface-82c87204e48654a9b2661c68fe66b9f16f81c4ba.tar.gz |
If all else fails try loading images from cloud server
Of course, as of this writing, there are no images on the server.
In addition, this patch adds comments to explain the by now convoluted
image retrieval logic (local file, filename as URL, by hash, cloud server).
Signed-off-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'subsurface-core/imagedownloader.cpp')
-rw-r--r-- | subsurface-core/imagedownloader.cpp | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/subsurface-core/imagedownloader.cpp b/subsurface-core/imagedownloader.cpp index 8be1daccc..9451f8a1b 100644 --- a/subsurface-core/imagedownloader.cpp +++ b/subsurface-core/imagedownloader.cpp @@ -7,13 +7,22 @@ #include <QtConcurrent> +QUrl cloudImageURL(const char *hash) +{ + return QUrl::fromUserInput(QString("https://cloud.subsurface-divelog.org/images/").append(hash)); +} + ImageDownloader::ImageDownloader(struct picture *pic) { picture = pic; } -void ImageDownloader::load(){ - QUrl url = QUrl::fromUserInput(QString(picture->filename)); +void ImageDownloader::load(bool fromHash){ + QUrl url; + if(fromHash) + url = cloudImageURL(picture->hash); + else + url = QUrl::fromUserInput(QString(picture->filename)); if (url.isValid()) { QEventLoop loop; QNetworkRequest request(url); @@ -52,10 +61,10 @@ void ImageDownloader::saveImage(QNetworkReply *reply) reply->deleteLater(); } -void loadPicture(struct picture *picture) +void loadPicture(struct picture *picture, bool fromHash) { ImageDownloader download(picture); - download.load(); + download.load(fromHash); } SHashedImage::SHashedImage(struct picture *picture) : QImage() @@ -64,16 +73,27 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage() if(url.isLocalFile()) load(url.toLocalFile()); if (isNull()) { - // Hash lookup. - load(fileFromHash(picture->hash)); - if (!isNull()) { - QtConcurrent::run(updateHash, picture); + // 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); + if (filename.isNull()) { + // That didn't produce a local filename. + // Try the cloud server + QtConcurrent::run(loadPicture, picture, true); } else { - QtConcurrent::run(loadPicture, picture); + // Load locally from translated file name + load(filename); + if (!isNull()) { + // Make sure the hash still matches the image file + QtConcurrent::run(updateHash, picture); + } else { + // Interpret filename as URL + QtConcurrent::run(loadPicture, picture, false); + } } } else { - QByteArray hash = hashFile(url.toLocalFile()); - free(picture->hash); - picture->hash = strdup(hash.toHex().data()); + // We loaded successfully. Now, make sure hash is up to date. + QtConcurrent::run(hashPicture, picture); } } + |