summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-02-08 22:45:55 +0100
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2018-03-05 18:04:57 +0200
commit117f4a0d7d75bcbd2aa33b293f3d16f8a07ed9e1 (patch)
treeae068eae422fb27d2e88b8c0b52a0eb07d2b775b /core
parent54d56a74aaa8d09c801a1a73989a1d5d95a72795 (diff)
downloadsubsurface-117f4a0d7d75bcbd2aa33b293f3d16f8a07ed9e1.tar.gz
core/imagedownloader.cpp: remove recursion
If loading from hash failed in the saveImage() slot(!) it would recurse into loadFromUrl(), which would generate a new network reply. Very scary and a (small) wonder that it worked. Let's try to make this all more explicit. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r--core/imagedownloader.cpp33
-rw-r--r--core/imagedownloader.h6
2 files changed, 21 insertions, 18 deletions
diff --git a/core/imagedownloader.cpp b/core/imagedownloader.cpp
index 82ca53026..c3024764c 100644
--- a/core/imagedownloader.cpp
+++ b/core/imagedownloader.cpp
@@ -17,7 +17,6 @@ static QUrl cloudImageURL(const char *hash)
ImageDownloader::ImageDownloader(struct picture *pic)
{
picture = pic;
- loadFromHash = false;
}
ImageDownloader::~ImageDownloader()
@@ -25,18 +24,24 @@ ImageDownloader::~ImageDownloader()
picture_free(picture);
}
-void ImageDownloader::load(bool fromHash){
- QUrl url;
- loadFromHash = fromHash;
- if(fromHash)
- url = cloudImageURL(picture->hash);
- else
- url = QUrl::fromUserInput(QString(picture->filename));
+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;
if (url.isValid()) {
QEventLoop loop;
QNetworkAccessManager manager;
QNetworkRequest request(url);
- connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(saveImage(QNetworkReply *)));
+ connect(&manager, &QNetworkAccessManager::finished, this,
+ [this,&success] (QNetworkReply *reply) { saveImage(reply, success); });
QNetworkReply *reply = manager.get(request);
while (reply->isRunning()) {
loop.processEvents();
@@ -44,18 +49,18 @@ void ImageDownloader::load(bool fromHash){
}
delete reply;
}
+ return success;
}
-void ImageDownloader::saveImage(QNetworkReply *reply)
+void ImageDownloader::saveImage(QNetworkReply *reply, bool &success)
{
+ success = false;
QByteArray imageData = reply->readAll();
QImage image = QImage();
image.loadFromData(imageData);
- if (image.isNull()) {
- if (loadFromHash)
- load(false);
+ if (image.isNull())
return;
- }
+ success = true;
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(imageData);
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
diff --git a/core/imagedownloader.h b/core/imagedownloader.h
index 295b664f4..be196aaef 100644
--- a/core/imagedownloader.h
+++ b/core/imagedownloader.h
@@ -14,11 +14,9 @@ public:
void load(bool fromHash);
private:
+ bool loadFromUrl(const QUrl &); // return true on success
+ void saveImage(QNetworkReply *reply, bool &success);
struct picture *picture;
- bool loadFromHash;
-
-private slots:
- void saveImage(QNetworkReply *reply);
};
class SHashedImage : public QImage {