diff options
author | Robert C. Helling <helling@atdotde.de> | 2015-03-02 16:18:16 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-03-16 12:42:36 -0700 |
commit | 6b3b50cc2f8083ebbc013c1d270c1ec1d1252a23 (patch) | |
tree | 634abaf36f1908b24f6128c1c02c31b3eba6360e /qt-ui/divepicturewidget.cpp | |
parent | 869ddc915daf4616ee2e010ab06fa7802b7e6d5f (diff) | |
download | subsurface-6b3b50cc2f8083ebbc013c1d270c1ec1d1252a23.tar.gz |
Load remote images
When loading an image by filename and by hash fails, try to interpret
the filename as URL and download the image.
Signed-off-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/divepicturewidget.cpp')
-rw-r--r-- | qt-ui/divepicturewidget.cpp | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/qt-ui/divepicturewidget.cpp b/qt-ui/divepicturewidget.cpp index 1baceeb7e..6de317778 100644 --- a/qt-ui/divepicturewidget.cpp +++ b/qt-ui/divepicturewidget.cpp @@ -2,21 +2,34 @@ #include "metrics.h" #include "dive.h" #include "divelist.h" +#include <unistd.h> #include <QtConcurrentMap> #include <QtConcurrentRun> #include <QFuture> #include <QDir> #include <QCryptographicHash> +#include <QNetworkAccessManager> +#include <QNetworkReply> #include <mainwindow.h> #include <qthelper.h> +#include <QStandardPaths> + +void loadPicuture(struct picture *picture) +{ + ImageDownloader download(picture); + download.load(); +} SHashedImage::SHashedImage(struct picture *picture) : QImage(picture->filename) { if (isNull()) { // Hash lookup. load(fileFromHash(picture->hash)); - if (!isNull()) + if (!isNull()) { QtConcurrent::run(updateHash, picture); + } else { + QtConcurrent::run(loadPicuture, picture); + } } else { QByteArray hash = hashFile(QString(picture->filename)); free(picture->hash); @@ -24,6 +37,45 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage(picture->filename) } } +ImageDownloader::ImageDownloader(struct picture *pic) +{ + picture = pic; +} + +void ImageDownloader::load(){ + QUrl url(picture->filename); + if (url.isValid()) { + QEventLoop loop; + QNetworkRequest request(url); + connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(saveImage(QNetworkReply *))); + QNetworkReply *reply = manager.get(request); + while (reply->isRunning()) { + loop.processEvents(); + sleep(1); + } + } + +} + +void ImageDownloader::saveImage(QNetworkReply *reply) +{ + QByteArray imageData = reply->readAll(); + QCryptographicHash hash(QCryptographicHash::Sha1); + hash.addData(imageData); + QFile imageFile(QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first().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()); + DivePictureModel::instance()->updateDivePictures(); + } + reply->manager()->deleteLater(); + reply->deleteLater(); +} + DivePictureModel *DivePictureModel::instance() { static DivePictureModel *self = new DivePictureModel(); @@ -47,8 +99,10 @@ SPixmap scaleImages(picturepointer picture) ret.second = cache.value(picture->filename); } else { int dim = defaultIconMetrics().sz_pic; - QImage p = SHashedImage(picture).scaled(dim, dim, Qt::KeepAspectRatio); - cache.insert(picture->filename, p); + QImage p = SHashedImage(picture); + if(!p.isNull()) + p = p.scaled(dim, dim, Qt::KeepAspectRatio); + cache.insert(picture->filename, p); ret.second = p; } return ret; |