Warning, file /utilities/telly-skout/src/networkfetcher.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // SPDX-FileCopyrightText: 2022 Plata Hill <plata.hill@kdemail.net> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "networkfetcher.h" 0005 0006 #include <QDebug> 0007 #include <QFile> 0008 #include <QFileInfo> 0009 #include <QStandardPaths> 0010 #include <QUrl> 0011 0012 NetworkFetcher::NetworkFetcher(QNetworkAccessManager *nam) 0013 : m_provider(nam) 0014 { 0015 } 0016 0017 QString NetworkFetcher::image(const QString &url, std::function<void()> callback, std::function<void(const Error &)> errorCallback) 0018 { 0019 QString path = imagePath(url); 0020 if (QFileInfo::exists(path)) { 0021 return path; 0022 } 0023 0024 downloadImage(url, callback, errorCallback); 0025 0026 return ""; 0027 } 0028 0029 QString NetworkFetcher::imagePath(const QString &url) 0030 { 0031 return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QUrl(url).fileName(); 0032 } 0033 0034 void NetworkFetcher::downloadImage(const QString &url, std::function<void()> callback, std::function<void(const Error &)> errorCallback) 0035 { 0036 m_provider.get( 0037 QUrl(url), 0038 [this, url, callback](QByteArray data) { 0039 QFile file(imagePath(url)); 0040 file.open(QIODevice::WriteOnly); 0041 file.write(data); 0042 file.close(); 0043 0044 if (callback) { 0045 callback(); 0046 } 0047 }, 0048 [url, errorCallback](const Error &error) { 0049 qWarning() << "Failed to download image" << url << ":" << error.m_message; 0050 0051 if (errorCallback) { 0052 errorCallback(error); 0053 } 0054 }); 0055 }