summaryrefslogtreecommitdiffstats
path: root/subsurface-core/imagedownloader.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-11-06 10:39:59 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-11-06 11:30:11 -0800
commit9edb4f3fa91b3907809375794d75a1b1a77c94a0 (patch)
treecd3b194081d0b928c5cb56aed0df7b9bc469fdd7 /subsurface-core/imagedownloader.cpp
parentbb566f7798f1bc7fe8e4d40813823dca0f6ea9a3 (diff)
downloadsubsurface-9edb4f3fa91b3907809375794d75a1b1a77c94a0.tar.gz
Move ImageDownloader out of the desktop widgets
This required a bit more untangling, but with this it seems we can build subsurface-mobile again (at least on the desktop). Interesting is the removal from inside the ImageDownloader of the call to DivePictureModel::instance()->updateDivePictures() - which actually could cause some interesting recursion issues. If it turns out we did indeed need this, it needs to be re-architected. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'subsurface-core/imagedownloader.cpp')
-rw-r--r--subsurface-core/imagedownloader.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/subsurface-core/imagedownloader.cpp b/subsurface-core/imagedownloader.cpp
new file mode 100644
index 000000000..8be1daccc
--- /dev/null
+++ b/subsurface-core/imagedownloader.cpp
@@ -0,0 +1,79 @@
+#include "dive.h"
+#include "metrics.h"
+#include "divelist.h"
+#include "qthelper.h"
+#include "imagedownloader.h"
+#include <unistd.h>
+
+#include <QtConcurrent>
+
+ImageDownloader::ImageDownloader(struct picture *pic)
+{
+ picture = pic;
+}
+
+void ImageDownloader::load(){
+ QUrl url = QUrl::fromUserInput(QString(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();
+ QImage image = QImage();
+ image.loadFromData(imageData);
+ if (image.isNull())
+ return;
+ 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());
+ }
+ reply->manager()->deleteLater();
+ reply->deleteLater();
+}
+
+void loadPicture(struct picture *picture)
+{
+ ImageDownloader download(picture);
+ download.load();
+}
+
+SHashedImage::SHashedImage(struct picture *picture) : QImage()
+{
+ QUrl url = QUrl::fromUserInput(QString(picture->filename));
+ if(url.isLocalFile())
+ load(url.toLocalFile());
+ if (isNull()) {
+ // Hash lookup.
+ load(fileFromHash(picture->hash));
+ if (!isNull()) {
+ QtConcurrent::run(updateHash, picture);
+ } else {
+ QtConcurrent::run(loadPicture, picture);
+ }
+ } else {
+ QByteArray hash = hashFile(url.toLocalFile());
+ free(picture->hash);
+ picture->hash = strdup(hash.toHex().data());
+ }
+}