diff options
-rw-r--r-- | core/imagedownloader.cpp | 33 | ||||
-rw-r--r-- | core/imagedownloader.h | 6 |
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 { |