diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-06-12 12:50:34 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-06-13 08:35:23 -0700 |
commit | 4c19d3da1d6c350f0c773a5260fe46dccdaddb43 (patch) | |
tree | cf1e35f725f64999e7b1afb918b5415d540ce0b4 /core | |
parent | be361c5cfb80344cf23c61a26c1c06f863bc28f2 (diff) | |
download | subsurface-4c19d3da1d6c350f0c773a5260fe46dccdaddb43.tar.gz |
Dive pictures: Make failure of loading images less noisy
For debug reasons, failure to load the original image was spilled
to the console, even if the local file was then found.
Only print a message, when also the local image failed loading.
This needed a bit of code reshuffling. To know when to print a
failed-loading message, the URL is now checked at the Thumbnailer
level, not the ImageDownloader level. The ImageDownloader is
passed the URL and the original filename (if different). The
image is loaded from the URL, but the signals send the original
filename, so that the thumbnail can be associated to the proper
image.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r-- | core/imagedownloader.cpp | 40 | ||||
-rw-r--r-- | core/imagedownloader.h | 2 |
2 files changed, 14 insertions, 28 deletions
diff --git a/core/imagedownloader.cpp b/core/imagedownloader.cpp index da5c86b99..e5dfc21d7 100644 --- a/core/imagedownloader.cpp +++ b/core/imagedownloader.cpp @@ -29,16 +29,8 @@ ImageDownloader::ImageDownloader() connect(&manager, &QNetworkAccessManager::finished, this, &ImageDownloader::saveImage); } -void ImageDownloader::load(QString filename) +void ImageDownloader::load(QUrl url, QString filename) { - QUrl url = QUrl::fromUserInput(filename); - - // If this is a file, we tried previously -> don't bother trying it again - if (url.scheme() == "file" || !url.isValid()) { - emit failed(filename); - return; - } - QNetworkRequest request(url); request.setAttribute(QNetworkRequest::User, filename); manager.get(request); @@ -73,21 +65,11 @@ void ImageDownloader::saveImage(QNetworkReply *reply) reply->deleteLater(); } -static void loadPicture(QString filename) +static void loadPicture(QUrl url, QString filename) { // This has to be done in UI main thread, because QNetworkManager refuses // to treat requests from other threads. - QMetaObject::invokeMethod(ImageDownloader::instance(), "load", Qt::AutoConnection, Q_ARG(QString, filename)); -} - -// Overwrite QImage::load() so that we can perform better error reporting. -static QImage loadImage(const QString &fileName, const char *format = nullptr) -{ - QImageReader reader(fileName, format); - QImage res = reader.read(); - if (res.isNull()) - qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString(); - return res; + QMetaObject::invokeMethod(ImageDownloader::instance(), "load", Qt::AutoConnection, Q_ARG(QUrl, url), Q_ARG(QString, filename)); } // Returns: thumbnail, still loading @@ -98,26 +80,30 @@ static std::pair<QImage,bool> getHashedImage(const QString &file_in, bool tryDow bool stillLoading = false; QUrl url = QUrl::fromUserInput(localFilePath(file)); if (url.isLocalFile()) - thumb = loadImage(url.toLocalFile()); + thumb.load(url.toLocalFile()); if (!thumb.isNull()) { // We loaded successfully. Now, make sure hash is up to date. hashPicture(file); } else if (tryDownload) { // This did not load anything. Let's try to get the image from other sources - QString filenameLocal = localFilePath(qPrintable(file)); - qDebug() << QStringLiteral("Translated filename: %1 -> %2").arg(file, filenameLocal); + QString filenameLocal = localFilePath(file); // Load locally from translated file name if it is different if (filenameLocal != file) - thumb = loadImage(filenameLocal); + thumb.load(filenameLocal); if (!thumb.isNull()) { // Make sure the hash still matches the image file hashPicture(filenameLocal); } else { // Interpret filename as URL - loadPicture(filenameLocal); - stillLoading = true; + QUrl url = QUrl::fromUserInput(filenameLocal); + if (!url.isLocalFile() && url.isValid()) { + loadPicture(url, file); + stillLoading = true; + } } } + if (thumb.isNull() && !stillLoading) + qInfo() << "Error loading image" << file << "[local:" << localFilePath(file) << "]"; return { thumb, stillLoading }; } diff --git a/core/imagedownloader.h b/core/imagedownloader.h index d91dbcaad..13ba80746 100644 --- a/core/imagedownloader.h +++ b/core/imagedownloader.h @@ -13,7 +13,7 @@ public: static ImageDownloader *instance(); ImageDownloader(); public slots: - void load(QString filename); + void load(QUrl url, QString filename); signals: void loaded(QString filename); void failed(QString filename); |