Changeset View
Changeset View
Standalone View
Standalone View
libdiscover/backends/FlatpakBackend/FlatpakResource.cpp
Show All 29 Lines | |||||
30 | 30 | | |||
31 | #include <KFormat> | 31 | #include <KFormat> | ||
32 | #include <KLocalizedString> | 32 | #include <KLocalizedString> | ||
33 | 33 | | |||
34 | #include <QDebug> | 34 | #include <QDebug> | ||
35 | #include <QDesktopServices> | 35 | #include <QDesktopServices> | ||
36 | #include <QIcon> | 36 | #include <QIcon> | ||
37 | #include <QFileInfo> | 37 | #include <QFileInfo> | ||
38 | #include <QNetworkAccessManager> | ||||
39 | #include <QNetworkReply> | ||||
40 | #include <QNetworkRequest> | ||||
38 | #include <QStringList> | 41 | #include <QStringList> | ||
39 | #include <QTimer> | 42 | #include <QTimer> | ||
40 | 43 | | |||
41 | FlatpakResource::FlatpakResource(AppStream::Component *component, FlatpakBackend *parent) | 44 | FlatpakResource::FlatpakResource(AppStream::Component *component, FlatpakBackend *parent) | ||
42 | : AbstractResource(parent) | 45 | : AbstractResource(parent) | ||
43 | , m_appdata(component) | 46 | , m_appdata(component) | ||
44 | , m_downloadSize(0) | 47 | , m_downloadSize(0) | ||
45 | , m_installedSize(0) | 48 | , m_installedSize(0) | ||
46 | , m_scope(FlatpakResource::System) | 49 | , m_scope(FlatpakResource::System) | ||
47 | , m_state(AbstractResource::None) | 50 | , m_state(AbstractResource::None) | ||
48 | , m_type(FlatpakResource::DesktopApp) | 51 | , m_type(FlatpakResource::DesktopApp) | ||
49 | { | 52 | { | ||
53 | // Start fetching remote icons during initialization | ||||
54 | const auto icons = m_appdata->icons(); | ||||
55 | if (!icons.isEmpty()) { | ||||
56 | foreach (const AppStream::Icon &icon, icons) { | ||||
57 | if (icon.kind() == AppStream::Icon::KindRemote) { | ||||
58 | const QString fileName = QStringLiteral("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) | ||||
59 | .arg(icon.url().fileName()); | ||||
60 | if (!QFileInfo::exists(fileName)) { | ||||
61 | QNetworkAccessManager *manager = new QNetworkAccessManager(this); | ||||
62 | connect(manager, &QNetworkAccessManager::finished, [this, icon, fileName, manager] (QNetworkReply *reply) { | ||||
63 | if (reply->error() == QNetworkReply::NoError) { | ||||
64 | QByteArray iconData = reply->readAll(); | ||||
65 | QFile file(fileName); | ||||
66 | if (file.open(QIODevice::WriteOnly)) { | ||||
67 | file.write(iconData); | ||||
68 | } | ||||
69 | file.close(); | ||||
70 | Q_EMIT iconChanged(); | ||||
71 | } | ||||
72 | manager->deleteLater(); | ||||
73 | }); | ||||
74 | manager->get(QNetworkRequest(icon.url())); | ||||
75 | } | ||||
76 | } | ||||
77 | } | ||||
78 | } | ||||
50 | } | 79 | } | ||
51 | 80 | | |||
52 | AppStream::Component *FlatpakResource::appstreamComponent() const | 81 | AppStream::Component *FlatpakResource::appstreamComponent() const | ||
53 | { | 82 | { | ||
54 | return m_appdata; | 83 | return m_appdata; | ||
55 | } | 84 | } | ||
56 | 85 | | |||
57 | QList<PackageState> FlatpakResource::addonsInformation() | 86 | QList<PackageState> FlatpakResource::addonsInformation() | ||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Line(s) | 180 | case AppStream::Icon::KindCached: | |||
155 | } else { | 184 | } else { | ||
156 | ret = QIcon::fromTheme(QStringLiteral("package-x-generic")); | 185 | ret = QIcon::fromTheme(QStringLiteral("package-x-generic")); | ||
157 | } | 186 | } | ||
158 | break; | 187 | break; | ||
159 | case AppStream::Icon::KindStock: | 188 | case AppStream::Icon::KindStock: | ||
160 | stock += icon.name(); | 189 | stock += icon.name(); | ||
161 | break; | 190 | break; | ||
162 | case AppStream::Icon::KindRemote: | 191 | case AppStream::Icon::KindRemote: | ||
163 | // TODO fetch remote icon | 192 | const QString fileName = QStringLiteral("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) | ||
193 | .arg(icon.url().fileName()); | ||||
194 | if (QFileInfo::exists(fileName)) { | ||||
195 | ret.addFile(fileName); | ||||
196 | } else { | ||||
164 | ret = QIcon::fromTheme(QStringLiteral("package-x-generic")); | 197 | ret = QIcon::fromTheme(QStringLiteral("package-x-generic")); | ||
165 | break; | 198 | break; | ||
166 | } | 199 | } | ||
200 | } | ||||
167 | 201 | | |||
168 | if (ret.isNull() && !stock.isEmpty()) { | 202 | if (ret.isNull() && !stock.isEmpty()) { | ||
169 | ret = QIcon::fromTheme(stock.first(), QIcon::fromTheme(QStringLiteral("package-x-generic"))); | 203 | ret = QIcon::fromTheme(stock.first(), QIcon::fromTheme(QStringLiteral("package-x-generic"))); | ||
170 | } | 204 | } | ||
171 | } | 205 | } | ||
172 | return ret; | 206 | return ret; | ||
173 | } | 207 | } | ||
174 | 208 | | |||
▲ Show 20 Lines • Show All 337 Lines • Show Last 20 Lines |