diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-02-08 22:45:55 +0100 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2018-03-05 18:04:57 +0200 |
commit | 117f4a0d7d75bcbd2aa33b293f3d16f8a07ed9e1 (patch) | |
tree | ae068eae422fb27d2e88b8c0b52a0eb07d2b775b /core/imagedownloader.cpp | |
parent | 54d56a74aaa8d09c801a1a73989a1d5d95a72795 (diff) | |
download | subsurface-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/imagedownloader.cpp')
-rw-r--r-- | core/imagedownloader.cpp | 33 |
1 files changed, 19 insertions, 14 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(); |