FlatpakBackend.cpp 51.4 KB
Newer Older
1
2
3
4
5
6
/*
 *   SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *   SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
 *
 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */
Jan Grulich's avatar
Jan Grulich committed
7
8

#include "FlatpakBackend.h"
9
#include "FlatpakFetchDataJob.h"
Jan Grulich's avatar
Jan Grulich committed
10
#include "FlatpakSourcesBackend.h"
11
#include "FlatpakJobTransaction.h"
Jan Grulich's avatar
Jan Grulich committed
12

13
#include <utils.h>
Jan Grulich's avatar
Jan Grulich committed
14
15
16
#include <resources/StandardBackendUpdater.h>
#include <resources/SourcesModel.h>
#include <Transaction/Transaction.h>
17
18
#include <appstream/OdrsReviewsBackend.h>
#include <appstream/AppStreamIntegration.h>
19
#include <appstream/AppStreamUtils.h>
Jan Grulich's avatar
Jan Grulich committed
20

21
#include <AppStreamQt/bundle.h>
Jan Grulich's avatar
Jan Grulich committed
22
#include <AppStreamQt/icon.h>
23
#include <AppStreamQt/metadata.h>
24

Jan Grulich's avatar
Jan Grulich committed
25
26
27
28
29
30
31
#include <KAboutData>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KConfigGroup>
#include <KSharedConfig>

#include <QAction>
32
#include <QtConcurrentRun>
Jan Grulich's avatar
Jan Grulich committed
33
34
35
#include <QDebug>
#include <QDir>
#include <QFile>
36
#include <QFileInfo>
37
#include <QFutureWatcher>
38
#include <QSettings>
Jan Grulich's avatar
Jan Grulich committed
39
40
#include <QThread>
#include <QTimer>
41
42
#include <QTextStream>
#include <QTemporaryFile>
43
#include <QNetworkAccessManager>
Jan Grulich's avatar
Jan Grulich committed
44

45
#include <glib.h>
46
#include <QRegularExpression>
47

48
49
#include <sys/stat.h>

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
50
DISCOVER_BACKEND_PLUGIN(FlatpakBackend)
Jan Grulich's avatar
Jan Grulich committed
51

52
53
54
55
56
57
58
59
60
61
62
63
QDebug operator<<(QDebug debug, const FlatpakResource::Id& id)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "FlatpakResource::Id(";
    debug.nospace() << "name:" << id.id << ',';
    debug.nospace() << "branch:" << id.branch << ',';
    debug.nospace() << "origin:" << id.origin << ',';
    debug.nospace() << "type:" << id.type;
    debug.nospace() << ')';
    return debug;
}

64
static FlatpakResource::Id idForInstalledRef(FlatpakInstallation *installation, FlatpakInstalledRef *ref, const QString &postfix)
65
66
{
    const FlatpakResource::ResourceType appType = flatpak_ref_get_kind(FLATPAK_REF(ref)) == FLATPAK_REF_KIND_APP ? FlatpakResource::DesktopApp : FlatpakResource::Runtime;
67
    const QString appId = QLatin1String(flatpak_ref_get_name(FLATPAK_REF(ref))) + postfix;
68
69
70
    const QString arch = QString::fromUtf8(flatpak_ref_get_arch(FLATPAK_REF(ref)));
    const QString branch = QString::fromUtf8(flatpak_ref_get_branch(FLATPAK_REF(ref)));

71
    return { installation, QString::fromUtf8(flatpak_installed_ref_get_origin(ref)), appType, appId, branch, arch };
72
73
}

Jan Grulich's avatar
Jan Grulich committed
74
75
76
FlatpakBackend::FlatpakBackend(QObject* parent)
    : AbstractResourcesBackend(parent)
    , m_updater(new StandardBackendUpdater(this))
77
    , m_reviews(AppStreamIntegration::global()->reviews())
78
    , m_refreshAppstreamMetadataJobs(0)
79
    , m_cancellable(g_cancellable_new())
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
80
    , m_threadPool(new QThreadPool(this))
Jan Grulich's avatar
Jan Grulich committed
81
82
83
{
    g_autoptr(GError) error = nullptr;

Jan Grulich's avatar
Jan Grulich committed
84
85
    connect(m_updater, &StandardBackendUpdater::updatesCountChanged, this, &FlatpakBackend::updatesCountChanged);

Jan Grulich's avatar
Jan Grulich committed
86
    // Load flatpak installation
87
    if (!setupFlatpakInstallations(&error)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
88
        qWarning() << "Failed to setup flatpak installations:" << error->message;
Jan Grulich's avatar
Jan Grulich committed
89
    } else {
90
        loadAppsFromAppstreamData();
91

92
        m_sources = new FlatpakSourcesBackend(m_installations, this);
93
        SourcesModel::global()->addSourcesBackend(m_sources);
Jan Grulich's avatar
Jan Grulich committed
94
95
    }

96
97
98
    connect(m_reviews.data(), &OdrsReviewsBackend::ratingsReady, this, [this] {
        m_reviews->emitRatingFetched(this, kTransform<QList<AbstractResource*>>(m_resources.values(), [] (AbstractResource* r) { return r; }));
    });
99
100
101
102
103
104
105

    /* Override the umask to 022 to make it possible to share files between
     * the plasma-discover process and flatpak system helper process.
     *
     * See https://github.com/flatpak/flatpak/pull/2856/
     */
    umask(022);
Jan Grulich's avatar
Jan Grulich committed
106
107
}

108
109
FlatpakBackend::~FlatpakBackend()
{
110
    g_cancellable_cancel(m_cancellable);
111
    for(auto inst : qAsConst(m_installations))
112
        g_object_unref(inst);
113
114
115
116
    if (!m_threadPool.waitForDone(200)) {
        qDebug() << "could not kill them all" << m_threadPool.activeThreadCount();
    }
    m_threadPool.clear();
117

118
    g_object_unref(m_cancellable);
119
120
}

121
122
bool FlatpakBackend::isValid() const
{
123
    return m_sources && !m_installations.isEmpty();
124
125
}

126
127
128
129
130
131
132
133
134
135
136
137
138
class FlatpakFetchRemoteResourceJob : public QNetworkAccessManager
{
Q_OBJECT
public:
    FlatpakFetchRemoteResourceJob(const QUrl &url, FlatpakBackend *backend)
        : QNetworkAccessManager(backend)
        , m_backend(backend)
        , m_url(url)
    {
    }

    void start()
    {
139
140
141
        QNetworkRequest req(m_url);
        req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
        auto replyGet = get(req);
142
        connect(replyGet, &QNetworkReply::finished, this, [this, replyGet] {
Laurent Montel's avatar
Laurent Montel committed
143
            QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> replyPtr(replyGet);
144
145
146
147
148
149
150
151
152
153
            const QUrl originalUrl = replyGet->request().url();
            if (replyGet->error() != QNetworkReply::NoError) {
                qWarning() << "couldn't download" << originalUrl << replyGet->errorString();
                Q_EMIT jobFinished(false, nullptr);
                return;
            }

            const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QLatin1Char('/') + originalUrl.fileName());
            auto replyPut = put(QNetworkRequest(fileUrl), replyGet->readAll());
            connect(replyPut, &QNetworkReply::finished, this, [this, originalUrl, fileUrl, replyPut]() {
Laurent Montel's avatar
Laurent Montel committed
154
                QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> replyPtr(replyPut);
155
                if (replyPut->error() != QNetworkReply::NoError) {
156
157
                    qWarning() << "couldn't save" << originalUrl << replyPut->errorString();
                    Q_EMIT jobFinished(false, nullptr);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
                    return;
                }
                if (!fileUrl.isLocalFile()) {
                    Q_EMIT jobFinished(false, nullptr);
                    return;
                }

                FlatpakResource *resource = nullptr;
                if (fileUrl.path().endsWith(QLatin1String(".flatpak"))) {
                    resource = m_backend->addAppFromFlatpakBundle(fileUrl);
                } else if (fileUrl.path().endsWith(QLatin1String(".flatpakref"))) {
                    resource = m_backend->addAppFromFlatpakRef(fileUrl);
                } else if (fileUrl.path().endsWith(QLatin1String(".flatpakrepo"))) {
                    resource = m_backend->addSourceFromFlatpakRepo(fileUrl);
                }

                if (resource) {
                    resource->setResourceFile(originalUrl);
                    Q_EMIT jobFinished(true, resource);
                } else {
                    qWarning() << "couldn't create resource from" << fileUrl.toLocalFile();
                    Q_EMIT jobFinished(false, nullptr);
180
                }
181
182
            }
            );
183
184
185
186
187
188
189
        });
    }

Q_SIGNALS:
    void jobFinished(bool success, FlatpakResource *resource);

private:
190
191
    FlatpakBackend  *const m_backend;
    const QUrl m_url;
192
193
};

Jan Grulich's avatar
Jan Grulich committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
FlatpakRemote * FlatpakBackend::getFlatpakRemoteByUrl(const QString &url, FlatpakInstallation *installation) const
{
    auto remotes = flatpak_installation_list_remotes(installation, m_cancellable, nullptr);
    if (!remotes) {
        return nullptr;
    }

    const QByteArray comparableUrl = url.toUtf8();
    for (uint i = 0; i < remotes->len; i++) {
        FlatpakRemote *remote = FLATPAK_REMOTE(g_ptr_array_index(remotes, i));

        if (comparableUrl == flatpak_remote_get_url(remote)) {
            return remote;
        }
    }
    return nullptr;
}

212
FlatpakInstalledRef * FlatpakBackend::getInstalledRefForApp(FlatpakResource *resource) const
213
214
215
{
    g_autoptr(GError) localError = nullptr;

216
    const auto type = resource->resourceType() == FlatpakResource::DesktopApp ? FLATPAK_REF_KIND_APP : FLATPAK_REF_KIND_RUNTIME;
217

218
    FlatpakInstalledRef *ref = flatpak_installation_get_installed_ref(resource->installation(),
219
                                                 type,
220
221
222
                                                 resource->flatpakName().toUtf8().constData(),
                                                 resource->arch().toUtf8().constData(),
                                                 resource->branch().toUtf8().constData(),
223
                                                 m_cancellable, &localError);
224
    return ref;
225
226
}

227
FlatpakResource * FlatpakBackend::getAppForInstalledRef(FlatpakInstallation *flatpakInstallation, FlatpakInstalledRef *ref) const
Jan Grulich's avatar
Jan Grulich committed
228
{
229
230
231
232
    auto r = m_resources.value(idForInstalledRef(flatpakInstallation, ref, {}));
    if (!r)
        r = m_resources.value(idForInstalledRef(flatpakInstallation, ref, QStringLiteral(".desktop")));

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
233
234
235
//     if (!r) {
//         qDebug() << "no" << flatpak_ref_get_name(FLATPAK_REF(ref));
//     }
236
    return r;
Jan Grulich's avatar
Jan Grulich committed
237
238
}

239
FlatpakResource * FlatpakBackend::getRuntimeForApp(FlatpakResource *resource) const
240
241
{
    FlatpakResource *runtime = nullptr;
242
243
    const QString runtimeName = resource->runtime();
    const auto runtimeInfo = runtimeName.splitRef(QLatin1Char('/'));
244
245
246
247
248

    if (runtimeInfo.count() != 3) {
        return runtime;
    }

249
    for(auto it = m_resources.constBegin(), itEnd = m_resources.constEnd(); it!=itEnd; ++it) {
250
        const auto& id = it.key();
251
        if (id.type == FlatpakResource::Runtime && id.id == runtimeInfo.at(0) && id.branch == runtimeInfo.at(2)) {
252
            runtime = *it;
253
254
255
256
257
            break;
        }
    }

    // TODO if runtime wasn't found, create a new one from available info
258
    if (!runtime) {
259
        qWarning() << "could not find runtime" << runtimeName << resource;
260
    }
261
262
263
264

    return runtime;
}

Jan Grulich's avatar
Jan Grulich committed
265
266
267
268
269
270
FlatpakResource * FlatpakBackend::addAppFromFlatpakBundle(const QUrl &url)
{
    g_autoptr(GBytes) appstreamGz = nullptr;
    g_autoptr(GError) localError = nullptr;
    g_autoptr(GFile) file = nullptr;
    g_autoptr(FlatpakBundleRef) bundleRef = nullptr;
271
    AppStream::Component asComponent;
Jan Grulich's avatar
Jan Grulich committed
272

273
    file = g_file_new_for_path(url.toLocalFile().toUtf8().constData());
Jan Grulich's avatar
Jan Grulich committed
274
275
276
    bundleRef = flatpak_bundle_ref_new(file, &localError);

    if (!bundleRef) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
277
        qWarning() << "Failed to load bundle:" << localError->message;
Jan Grulich's avatar
Jan Grulich committed
278
279
280
        return nullptr;
    }

281
282

    gsize len = 0;
283
    g_autoptr(GBytes) metadata = flatpak_bundle_ref_get_metadata(bundleRef);
284
285
    const QByteArray metadataContent((char *)g_bytes_get_data(metadata, &len));

Jan Grulich's avatar
Jan Grulich committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    appstreamGz = flatpak_bundle_ref_get_appstream(bundleRef);
    if (appstreamGz) {
        g_autoptr(GZlibDecompressor) decompressor = nullptr;
        g_autoptr(GInputStream) streamGz = nullptr;
        g_autoptr(GInputStream) streamData = nullptr;
        g_autoptr(GBytes) appstream = nullptr;

        /* decompress data */
        decompressor = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
        streamGz = g_memory_input_stream_new_from_bytes (appstreamGz);
        if (!streamGz) {
            return nullptr;
        }

        streamData = g_converter_input_stream_new (streamGz, G_CONVERTER (decompressor));

        appstream = g_input_stream_read_bytes (streamData, 0x100000, m_cancellable, &localError);
        if (!appstream) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
304
            qWarning() << "Failed to extract appstream metadata from bundle:" << localError->message;
Jan Grulich's avatar
Jan Grulich committed
305
306
307
            return nullptr;
        }

308
309
310
        gsize len = 0;
        gconstpointer data = g_bytes_get_data(appstream, &len);

311
312
        AppStream::Metadata metadata;
        metadata.setFormatStyle(AppStream::Metadata::FormatStyleCollection);
313
        AppStream::Metadata::MetadataError error = metadata.parse(QString::fromUtf8((char*)data, len), AppStream::Metadata::FormatKindXml);
314
315
        if (error != AppStream::Metadata::MetadataErrorNoError) {
            qWarning() << "Failed to parse appstream metadata: " << error;
Jan Grulich's avatar
Jan Grulich committed
316
317
318
            return nullptr;
        }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
319
        const QList<AppStream::Component> components = metadata.components();
320
321
        if (components.size()) {
            asComponent = AppStream::Component(components.first());
Jan Grulich's avatar
Jan Grulich committed
322
323
324
325
326
327
        } else {
            qWarning() << "Failed to parse appstream metadata";
            return nullptr;
        }
    } else {
        qWarning() << "No appstream metadata in bundle";
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

        QTemporaryFile tempFile;
        tempFile.setAutoRemove(false);
        if (!tempFile.open()) {
            qWarning() << "Failed to get metadata file";
            return nullptr;
        }

        tempFile.write(metadataContent);
        tempFile.close();

        // Parse the temporary file
        QSettings setting(tempFile.fileName(), QSettings::NativeFormat);
        setting.beginGroup(QLatin1String("Application"));

        asComponent.setName(setting.value(QLatin1String("name")).toString());

        tempFile.remove();
Jan Grulich's avatar
Jan Grulich committed
346
347
    }

348
    FlatpakResource *resource = new FlatpakResource(asComponent, preferredInstallation(), this);
Jan Grulich's avatar
Jan Grulich committed
349
    if (!updateAppMetadata(resource, metadataContent)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
350
        delete resource;
Jan Grulich's avatar
Jan Grulich committed
351
352
353
354
        qWarning() << "Failed to update metadata from app bundle";
        return nullptr;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
355
    g_autoptr(GBytes) iconData = flatpak_bundle_ref_get_icon(bundleRef, 128);
Jan Grulich's avatar
Jan Grulich committed
356
357
358
359
360
361
362
    if (!iconData) {
        iconData = flatpak_bundle_ref_get_icon(bundleRef, 64);
    }

    if (iconData) {
        gsize len = 0;
        char * data = (char *)g_bytes_get_data(iconData, &len);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
363
364
365

        QPixmap pixmap;
        pixmap.loadFromData(QByteArray(data, len), "PNG");
Jan Grulich's avatar
Jan Grulich committed
366
367
368
        resource->setBundledIcon(pixmap);
    }

369
370
    const QString origin = QString::fromUtf8(flatpak_bundle_ref_get_origin(bundleRef));

371
    resource->setDownloadSize(0);
Jan Grulich's avatar
Jan Grulich committed
372
    resource->setInstalledSize(flatpak_bundle_ref_get_installed_size(bundleRef));
373
374
    resource->setPropertyState(FlatpakResource::DownloadSize, FlatpakResource::AlreadyKnown);
    resource->setPropertyState(FlatpakResource::InstalledSize, FlatpakResource::AlreadyKnown);
Jan Grulich's avatar
Jan Grulich committed
375
    resource->setFlatpakFileType(QStringLiteral("flatpak"));
376
    resource->setOrigin(origin.isEmpty() ? i18n("Local bundle") : origin);
Jan Grulich's avatar
Jan Grulich committed
377
378
379
380
381
382
383
384
385
386
387
388
    resource->setResourceFile(url);
    resource->setState(FlatpakResource::None);
    resource->setType(FlatpakResource::DesktopApp);

    addResource(resource);
    return resource;
}

FlatpakResource * FlatpakBackend::addAppFromFlatpakRef(const QUrl &url)
{
    QSettings settings(url.toLocalFile(), QSettings::NativeFormat);
    const QString refurl = settings.value(QStringLiteral("Flatpak Ref/Url")).toString();
389
390
391
392
393
394
395
396
397
398
399
    const QString name = settings.value(QStringLiteral("Flatpak Ref/Name")).toString();

    auto item = m_sources->sourceByUrl(refurl);
    if (item) {
        const auto resources = resourcesByAppstreamName(name);
        for (auto resource : resources) {
            if (resource->origin() == item->data(AbstractSourcesBackend::IdRole)) {
                return static_cast<FlatpakResource*>(resource);
            }
        }
    }
Jan Grulich's avatar
Jan Grulich committed
400

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
401
    g_autoptr(GError) error = nullptr;
Jan Grulich's avatar
Jan Grulich committed
402
403
404
405
406
407
408
409
410
411
412
    g_autoptr(FlatpakRemoteRef) remoteRef = nullptr;
    {
        QFile f(url.toLocalFile());
        if (!f.open(QFile::ReadOnly | QFile::Text)) {
            return nullptr;
        }

        QByteArray contents = f.readAll();

        g_autoptr(GBytes) bytes = g_bytes_new (contents.data(), contents.size());

413
        remoteRef = flatpak_installation_install_ref_file (preferredInstallation(), bytes, m_cancellable, &error);
Jan Grulich's avatar
Jan Grulich committed
414
        if (!remoteRef) {
415
            qWarning() << "Failed to create install ref file:" << error->message;
416
            const auto resources = resourcesByAppstreamName(name);
417
418
419
            if (!resources.isEmpty()) {
                return qobject_cast<FlatpakResource*>(resources.constFirst());
            }
Jan Grulich's avatar
Jan Grulich committed
420
421
422
423
424
425
426
427
            return nullptr;
        }
    }

    const auto remoteName = flatpak_remote_ref_get_remote_name(remoteRef);

    auto ref = FLATPAK_REF(remoteRef);

428
429
430
431
432
    AppStream::Component asComponent;
    asComponent.addUrl(AppStream::Component::UrlKindHomepage, settings.value(QStringLiteral("Flatpak Ref/Homepage")).toString());
    asComponent.setDescription(settings.value(QStringLiteral("Flatpak Ref/Description")).toString());
    asComponent.setName(settings.value(QStringLiteral("Flatpak Ref/Title")).toString());
    asComponent.setSummary(settings.value(QStringLiteral("Flatpak Ref/Comment")).toString());
433
    asComponent.setId(name);
434

Jan Grulich's avatar
Jan Grulich committed
435
436
    const QString iconUrl = settings.value(QStringLiteral("Flatpak Ref/Icon")).toString();
    if (!iconUrl.isEmpty()) {
437
438
439
440
        AppStream::Icon icon;
        icon.setKind(AppStream::Icon::KindRemote);
        icon.setUrl(QUrl(iconUrl));
        asComponent.addIcon(icon);
Jan Grulich's avatar
Jan Grulich committed
441
442
    }

443
    auto resource = new FlatpakResource(asComponent, preferredInstallation(), this);
Jan Grulich's avatar
Jan Grulich committed
444
445
446
    resource->setFlatpakFileType(QStringLiteral("flatpakref"));
    resource->setOrigin(QString::fromUtf8(remoteName));
    resource->updateFromRef(ref);
447
448
449
450

    QUrl runtimeUrl = QUrl(settings.value(QStringLiteral("Flatpak Ref/RuntimeRepo")).toString());
    if (!runtimeUrl.isEmpty()) {
        // We need to fetch metadata to find information about required runtime
451
        auto fw = new QFutureWatcher<QByteArray>(this);
452
        connect(fw, &QFutureWatcher<QByteArray>::finished, this, [this, resource, fw, runtimeUrl]() {
453
            const auto metadata = fw->result();
454
            // Even when we failed to fetch information about runtime we still want to show the application
455
            if (metadata.isEmpty()) {
456
                Q_EMIT onFetchMetadataFinished(resource, metadata);
457
            } else {
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
                updateAppMetadata(resource, metadata);

                auto runtime = getRuntimeForApp(resource);
                if (!runtime || (runtime && !runtime->isInstalled())) {
                    FlatpakFetchRemoteResourceJob *fetchRemoteResource = new FlatpakFetchRemoteResourceJob(runtimeUrl, this);
                    connect(fetchRemoteResource, &FlatpakFetchRemoteResourceJob::jobFinished, this, [this, resource] (bool success, FlatpakResource *repoResource) {
                        if (success) {
                            installApplication(repoResource);
                        }
                        addResource(resource);
                    });
                    fetchRemoteResource->start();
                    return;
                } else {
                    addResource(resource);
                }
474
            }
475
            fw->deleteLater();
476
        });
477
        fw->setFuture(QtConcurrent::run(&m_threadPool, &FlatpakRunnables::fetchMetadata, resource, m_cancellable));
478
479
480
481
    } else {
        addResource(resource);
    }

Jan Grulich's avatar
Jan Grulich committed
482
483
484
    return resource;
}

485
486
FlatpakResource * FlatpakBackend::addSourceFromFlatpakRepo(const QUrl &url)
{
487
    Q_ASSERT(url.isLocalFile());
488
489
490
491
492
493
494
495
496
497
    QSettings settings(url.toLocalFile(), QSettings::NativeFormat);

    const QString gpgKey = settings.value(QStringLiteral("Flatpak Repo/GPGKey")).toString();
    const QString title = settings.value(QStringLiteral("Flatpak Repo/Title")).toString();
    const QString repoUrl = settings.value(QStringLiteral("Flatpak Repo/Url")).toString();

    if (gpgKey.isEmpty() || title.isEmpty() || repoUrl.isEmpty()) {
        return nullptr;
    }

498
    if (gpgKey.startsWith(QLatin1String("http://")) || gpgKey.startsWith(QLatin1String("https://"))) {
499
500
501
        return nullptr;
    }

502
503
504
505
506
507
508
    AppStream::Component asComponent;
    asComponent.addUrl(AppStream::Component::UrlKindHomepage, settings.value(QStringLiteral("Flatpak Repo/Homepage")).toString());
    asComponent.setSummary(settings.value(QStringLiteral("Flatpak Repo/Comment")).toString());
    asComponent.setDescription(settings.value(QStringLiteral("Flatpak Repo/Description")).toString());
    asComponent.setName(title);
    asComponent.setId(settings.value(QStringLiteral("Flatpak Ref/Name")).toString());

509
    const QString iconUrl = settings.value(QStringLiteral("Flatpak Repo/Icon")).toString();
510
    if (!iconUrl.isEmpty()) {
511
512
513
514
        AppStream::Icon icon;
        icon.setKind(AppStream::Icon::KindRemote);
        icon.setUrl(QUrl(iconUrl));
        asComponent.addIcon(icon);
515
516
    }

517
    auto resource = new FlatpakResource(asComponent, preferredInstallation(), this);
518
519
520
521
522
523
524
    // Use metadata only for stuff which are not common for all resources
    resource->addMetadata(QStringLiteral("gpg-key"), gpgKey);
    resource->addMetadata(QStringLiteral("repo-url"), repoUrl);
    resource->setBranch(settings.value(QStringLiteral("Flatpak Repo/DefaultBranch")).toString());
    resource->setFlatpakName(url.fileName().remove(QStringLiteral(".flatpakrepo")));
    resource->setType(FlatpakResource::Source);

525
    auto repo = flatpak_installation_get_remote_by_name(preferredInstallation(), resource->flatpakName().toUtf8().constData(), m_cancellable, nullptr);
526
527
528
529
530
531
532
533
534
    if (!repo) {
        resource->setState(AbstractResource::State::None);
    } else {
        resource->setState(AbstractResource::State::Installed);
    }

    return resource;
}

535
536
537
538
void FlatpakBackend::addResource(FlatpakResource *resource)
{
    // Update app with all possible information we have
    if (!parseMetadataFromAppBundle(resource)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
539
        qWarning() << "Failed to parse metadata from app bundle for" << resource->name();
540
541
    }

542
    updateAppState(resource);
543
544

    m_resources.insert(resource->uniqueId(), resource);
545
546
547
548
    if (!resource->extends().isEmpty()) {
        m_extends.append(resource->extends());
        m_extends.removeDuplicates();
    }
549
550
551
552
553

    connect(resource, &FlatpakResource::sizeChanged, this, [this, resource] {
        if (!isFetching())
            Q_EMIT resourcesChanged(resource, {"size", "sizeDescription"});
    });
554
555
}

556
class FlatpakSource
Jan Grulich's avatar
Jan Grulich committed
557
{
558
559
public:
    FlatpakSource(FlatpakRemote* remote) : m_remote(remote) {}
Jan Grulich's avatar
Jan Grulich committed
560

561
562
563
    bool isEnabled() const
    {
        return !flatpak_remote_get_disabled(m_remote);
Jan Grulich's avatar
Jan Grulich committed
564
565
    }

566
567
568
569
    QString appstreamDir() const
    {
        g_autoptr(GFile) appstreamDir = flatpak_remote_get_appstream_dir(m_remote, nullptr);
        if (!appstreamDir) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
570
            qWarning() << "No appstream dir for" << flatpak_remote_get_name(m_remote);
571
572
            return {};
        }
573
574
        g_autofree char *path_str = g_file_get_path(appstreamDir);
        return QString::fromUtf8(path_str);
575
576
577
578
579
580
581
582
583
584
585
    }

    QString name() const
    {
        return QString::fromUtf8(flatpak_remote_get_name(m_remote));
    }

private:
    FlatpakRemote* m_remote;
};

586
587
588
589
void FlatpakBackend::loadAppsFromAppstreamData()
{
    for (auto installation : qAsConst(m_installations)) {
        // Load applications from appstream metadata
590
591
592
        if (g_cancellable_is_cancelled(m_cancellable))
            break;

593
594
595
596
597
598
        if (!loadAppsFromAppstreamData(installation)) {
            qWarning() << "Failed to load packages from appstream data from installation" << installation;
        }
    }
}

599
600
601
602
bool FlatpakBackend::loadAppsFromAppstreamData(FlatpakInstallation *flatpakInstallation)
{
    Q_ASSERT(flatpakInstallation);

603
    g_autoptr(GPtrArray) remotes = flatpak_installation_list_remotes(flatpakInstallation, m_cancellable, nullptr);
Jan Grulich's avatar
Jan Grulich committed
604
605
606
607
    if (!remotes) {
        return false;
    }

608
609
    m_refreshAppstreamMetadataJobs += remotes->len;

Jan Grulich's avatar
Jan Grulich committed
610
611
    for (uint i = 0; i < remotes->len; i++) {
        FlatpakRemote *remote = FLATPAK_REMOTE(g_ptr_array_index(remotes, i));
612
        g_autoptr(GFile) fileTimestamp = flatpak_remote_get_appstream_timestamp(remote, flatpak_get_default_arch());
613

614
615
        g_autofree char *path_str = g_file_get_path(fileTimestamp);
        QFileInfo fileInfo = QFileInfo(QString::fromUtf8(path_str));
616
617
618
619
620
621
        // Refresh appstream metadata in case they have never been refreshed or the cache is older than 6 hours
        if (!fileInfo.exists() || fileInfo.lastModified().toUTC().secsTo(QDateTime::currentDateTimeUtc()) > 21600) {
            refreshAppstreamMetadata(flatpakInstallation, remote);
        } else {
            integrateRemote(flatpakInstallation, remote);
        }
622
623
624
    }
    return true;
}
Jan Grulich's avatar
Jan Grulich committed
625

626
627
628
629
630
631
632
633
634
void FlatpakBackend::metadataRefreshed()
{
    m_refreshAppstreamMetadataJobs--;
    if (m_refreshAppstreamMetadataJobs == 0) {
        loadInstalledApps();
        checkForUpdates();
    }
}

635
636
void FlatpakBackend::integrateRemote(FlatpakInstallation *flatpakInstallation, FlatpakRemote *remote)
{
637
    Q_ASSERT(m_refreshAppstreamMetadataJobs != 0);
638

639
640
    FlatpakSource source(remote);
    if (!source.isEnabled() || flatpak_remote_get_noenumerate(remote)) {
641
        metadataRefreshed();
642
643
        return;
    }
Jan Grulich's avatar
Jan Grulich committed
644

645
    const QString appstreamDirPath = source.appstreamDir();
646
    const QString appstreamIconsPath = source.appstreamDir() + QLatin1String("/icons/");
647
648
    const QString appDirFileName = appstreamDirPath + QLatin1String("/appstream.xml.gz");
    if (!QFile::exists(appDirFileName)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
649
        qWarning() << "No" << appDirFileName << "appstream metadata found for" << source.name();
650
        metadataRefreshed();
651
        return;
Jan Grulich's avatar
Jan Grulich committed
652
653
    }

654
655
656
657
    auto fw = new QFutureWatcher<QList<AppStream::Component>>(this);
    const auto sourceName = source.name();
    connect(fw, &QFutureWatcher<QList<AppStream::Component>>::finished, this, [this, fw, flatpakInstallation, appstreamIconsPath, sourceName]() {
        const auto components = fw->result();
658
659
        QVector<FlatpakResource*> resources;
        for (const AppStream::Component& appstreamComponent : components) {
660
661
662
            FlatpakResource *resource = new FlatpakResource(appstreamComponent, flatpakInstallation, this);
            resource->setIconPath(appstreamIconsPath);
            resource->setOrigin(sourceName);
663
664
665
666
667
668
669
            if (resource->resourceType() == FlatpakResource::Runtime) {
                resources.prepend(resource);
            } else {
                resources.append(resource);
            }
        }
        for (auto resource : qAsConst(resources)) {
670
671
            addResource(resource);
        }
672

673
        metadataRefreshed();
674
        acquireFetching(false);
675
676
        fw->deleteLater();
    });
677
678
679
680
681
682
683
684
685
686
687
688
    acquireFetching(true);
    fw->setFuture(QtConcurrent::run(&m_threadPool, [appDirFileName]() -> QList<AppStream::Component> {
        AppStream::Metadata metadata;
        metadata.setFormatStyle(AppStream::Metadata::FormatStyleCollection);
        AppStream::Metadata::MetadataError error = metadata.parseFile(appDirFileName, AppStream::Metadata::FormatKindXml);
        if (error != AppStream::Metadata::MetadataErrorNoError) {
            qWarning() << "Failed to parse appstream metadata: " << error;
            return {};
        }

        return metadata.components();
    }));
689
690
691
692
693
694
695
696
697
698
}

void FlatpakBackend::loadInstalledApps()
{
    for (auto installation : qAsConst(m_installations)) {
        // Load installed applications and update existing resources with info from installed application
        if (!loadInstalledApps(installation)) {
            qWarning() << "Failed to load installed packages from installation" << installation;
        }
    }
Jan Grulich's avatar
Jan Grulich committed
699
700
}

701
bool FlatpakBackend::loadInstalledApps(FlatpakInstallation *flatpakInstallation)
Jan Grulich's avatar
Jan Grulich committed
702
{
703
    Q_ASSERT(flatpakInstallation);
Jan Grulich's avatar
Jan Grulich committed
704

705
706
707
708
709
710
711
    g_autoptr(GError) localError = nullptr;
    g_autoptr(GPtrArray) refs = flatpak_installation_list_installed_refs(flatpakInstallation, m_cancellable, &localError);
    if (!refs) {
        qWarning() << "Failed to get list of installed refs for listing updates:" << localError->message;
        return false;
    }

712
713
    const QString pathExports = FlatpakResource::installationPath(flatpakInstallation) + QLatin1String("/exports/");
    const QString pathApps = pathExports + QLatin1String("share/applications/");
Jan Grulich's avatar
Jan Grulich committed
714

715
    QVector<FlatpakResource*> resources;
716
717
    for (uint i = 0; i < refs->len; i++) {
        FlatpakInstalledRef *ref = FLATPAK_INSTALLED_REF(g_ptr_array_index(refs, i));
Jan Grulich's avatar
Jan Grulich committed
718

719
720
721
722
        const auto name = QLatin1String(flatpak_ref_get_name(FLATPAK_REF(ref)));
        if (name.endsWith(QLatin1String(".Debug")) || name.endsWith(QLatin1String(".Locale")) || name.endsWith(QLatin1String(".BaseApp")) || name.endsWith(QLatin1String(".Docs")))
            continue;

723
724
725
726
727
        const auto res = getAppForInstalledRef(flatpakInstallation, ref);
        if (res) {
            res->setState(AbstractResource::Installed);
            continue;
        }
728

729
        AppStream::Component cid;
730
        AppStream::Metadata metadata;
731
        const QString fnDesktop = pathApps + name + QLatin1String(".desktop");
732
        AppStream::Metadata::MetadataError error = metadata.parseFile(fnDesktop, AppStream::Metadata::FormatKindDesktopEntry);
733
        if (error != AppStream::Metadata::MetadataErrorNoError) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
734
735
            if (QFile::exists(fnDesktop))
                qDebug() << "Failed to parse appstream metadata:" << error << fnDesktop;
736
737

            cid.setId(QString::fromLatin1(flatpak_ref_get_name(FLATPAK_REF(ref))));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
738
#if FLATPAK_CHECK_VERSION(1,1,2)
739
            cid.setName(QString::fromUtf8(flatpak_installed_ref_get_appdata_name(ref)));
740
#endif
741
742
        } else
            cid = metadata.component();
743

744
        FlatpakResource *resource = new FlatpakResource(cid, flatpakInstallation, this);
745
746
747
748
749
750

        resource->setIconPath(pathExports);
        resource->setState(AbstractResource::Installed);
        resource->setOrigin(QString::fromUtf8(flatpak_installed_ref_get_origin(ref)));
        resource->updateFromRef(FLATPAK_REF(ref));

751
752
753
754
755
        if (resource->resourceType() == FlatpakResource::Runtime) {
            resources.prepend(resource);
        } else {
            resources.append(resource);
        }
Jan Grulich's avatar
Jan Grulich committed
756
    }
757
758
    for (auto resource : qAsConst(resources))
        addResource(resource);
Jan Grulich's avatar
Jan Grulich committed
759
760
761
762

    return true;
}

Jan Grulich's avatar
Jan Grulich committed
763
764
765
void FlatpakBackend::loadLocalUpdates(FlatpakInstallation *flatpakInstallation)
{
    g_autoptr(GError) localError = nullptr;
766
    g_autoptr(GPtrArray) refs = flatpak_installation_list_installed_refs(flatpakInstallation, m_cancellable, &localError);
Jan Grulich's avatar
Jan Grulich committed
767
    if (!refs) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
768
        qWarning() << "Failed to get list of installed refs for listing updates:" << localError->message;
Jan Grulich's avatar
Jan Grulich committed
769
770
771
772
773
        return;
    }

    for (uint i = 0; i < refs->len; i++) {
        FlatpakInstalledRef *ref = FLATPAK_INSTALLED_REF(g_ptr_array_index(refs, i));
774

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
775
        const gchar *latestCommit = flatpak_installed_ref_get_latest_commit(ref);
Jan Grulich's avatar
Jan Grulich committed
776
777

        if (!latestCommit) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
778
            qWarning() << "Couldn't get latest commit for" << flatpak_ref_get_name(FLATPAK_REF(ref));
779
            continue;
Jan Grulich's avatar
Jan Grulich committed
780
781
        }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
782
        const gchar *commit = flatpak_ref_get_commit(FLATPAK_REF(ref));
Jan Grulich's avatar
Jan Grulich committed
783
784
785
786
787
788
789
        if (g_strcmp0(commit, latestCommit) == 0) {
            continue;
        }

        FlatpakResource *resource = getAppForInstalledRef(flatpakInstallation, ref);
        if (resource) {
            resource->setState(AbstractResource::Upgradeable);
790
            updateAppSize(resource);
Jan Grulich's avatar
Jan Grulich committed
791
792
793
794
        }
    }
}

795
void FlatpakBackend::loadRemoteUpdates(FlatpakInstallation* installation)
Jan Grulich's avatar
Jan Grulich committed
796
{
797
    auto fw = new QFutureWatcher<GPtrArray *>(this);
798
    connect(fw, &QFutureWatcher<GPtrArray *>::finished, this, [this, installation, fw](){
799
        g_autoptr(GPtrArray) refs = fw->result();
800
801
        onFetchUpdatesFinished(installation, refs);
        fw->deleteLater();
802
        acquireFetching(false);
803
    });
804
    acquireFetching(true);
805
    fw->setFuture(QtConcurrent::run(&m_threadPool, [installation, this]() -> GPtrArray * {
806
        g_autoptr(GError) localError = nullptr;
807
808
809
810
        if (g_cancellable_is_cancelled(m_cancellable)) {
            qWarning() << "don't issue commands after cancelling";
            return {};
        }
811
        GPtrArray *refs = flatpak_installation_list_installed_refs_for_update(installation, m_cancellable, &localError);
812
813
814
815
816
        if (!refs) {
            qWarning() << "Failed to get list of installed refs for listing updates: " << localError->message;
        }
        return refs;
    }));
817
}
Jan Grulich's avatar
Jan Grulich committed
818

819
void FlatpakBackend::onFetchUpdatesFinished(FlatpakInstallation *flatpakInstallation, GPtrArray *fetchedUpdates)
820
{
821
    if (!fetchedUpdates) {
822
823
824
825
        qWarning() << "could not get updates for" << flatpakInstallation;
        return;
    }

826
827
    for (uint i = 0; i < fetchedUpdates->len; i++) {
        FlatpakInstalledRef *ref = FLATPAK_INSTALLED_REF(g_ptr_array_index(fetchedUpdates, i));
Jan Grulich's avatar
Jan Grulich committed
828
829
830
        FlatpakResource *resource = getAppForInstalledRef(flatpakInstallation, ref);
        if (resource) {
            resource->setState(AbstractResource::Upgradeable);
831
            updateAppSize(resource);
832
        } else
833
            qWarning() << "could not find updated resource" << flatpak_ref_get_name(FLATPAK_REF(ref)) << m_resources.size();
Jan Grulich's avatar
Jan Grulich committed
834
835
836
    }
}

837
838
839
840
bool FlatpakBackend::parseMetadataFromAppBundle(FlatpakResource *resource)
{
    g_autoptr(FlatpakRef) ref = nullptr;
    g_autoptr(GError) localError = nullptr;
841
    AppStream::Bundle bundle = resource->appstreamComponent().bundle(AppStream::Bundle::KindFlatpak);
842
843

    // Get arch/branch/commit/name from FlatpakRef
844
    if (!bundle.isEmpty()) {
845
        ref = flatpak_ref_parse(bundle.id().toUtf8().constData(), &localError);
846
        if (!ref) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
847
            qWarning() << "Failed to parse" << bundle.id() << localError->message;
848
849
            return false;
        } else {
850
            resource->updateFromRef(ref);
851
852
853
854
855
856
        }
    }

    return true;
}

857
858
859
860
861
862
class FlatpakRefreshAppstreamMetadataJob : public QThread
{
    Q_OBJECT
public:
    FlatpakRefreshAppstreamMetadataJob(FlatpakInstallation *installation, FlatpakRemote *remote)
        : QThread()
863
        , m_cancellable(g_cancellable_new())
864
865
866
        , m_installation(installation)
        , m_remote(remote)
    {
867
        g_object_ref(m_remote);
868
        connect(this, &FlatpakRefreshAppstreamMetadataJob::finished, this, &QObject::deleteLater);
869
870
871
872
    }

    ~FlatpakRefreshAppstreamMetadataJob()
    {
873
        g_object_unref(m_remote);
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
        g_object_unref(m_cancellable);
    }

    void cancel()
    {
        g_cancellable_cancel(m_cancellable);
    }

    void run() override
    {
        g_autoptr(GError) localError = nullptr;

#if FLATPAK_CHECK_VERSION(0,9,4)
        // With Flatpak 0.9.4 we can use flatpak_installation_update_appstream_full_sync() providing progress reporting which we don't use at this moment, but still
        // better to use newer function in case the previous one gets deprecated
        if (!flatpak_installation_update_appstream_full_sync(m_installation, flatpak_remote_get_name(m_remote), nullptr, nullptr, nullptr, nullptr, m_cancellable, &localError)) {
#else
        if (!flatpak_installation_update_appstream_sync(m_installation, flatpak_remote_get_name(m_remote), nullptr, nullptr, m_cancellable, &localError)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
892
#endif
893
894
895
            const QString error = localError ? QString::fromUtf8(localError->message) : QStringLiteral("<no error>");
            qWarning() << "Failed to refresh appstream metadata for " << flatpak_remote_get_name(m_remote) << ": " << error;
            Q_EMIT jobRefreshAppstreamMetadataFailed(error);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
896
897
        } else  {
            Q_EMIT jobRefreshAppstreamMetadataFinished(m_installation, m_remote);
898
899
900
901
        }
    }

Q_SIGNALS:
902
    void jobRefreshAppstreamMetadataFailed(const QString &errorMessage);
903
904
905
906
907
908
909
910
911
912
913
    void jobRefreshAppstreamMetadataFinished(FlatpakInstallation *installation, FlatpakRemote *remote);

private:
    GCancellable *m_cancellable;
    FlatpakInstallation *m_installation;
    FlatpakRemote *m_remote;
};

void FlatpakBackend::refreshAppstreamMetadata(FlatpakInstallation *installation, FlatpakRemote *remote)
{
    FlatpakRefreshAppstreamMetadataJob *job = new FlatpakRefreshAppstreamMetadataJob(installation, remote);
914
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFailed, this, &FlatpakBackend::metadataRefreshed);
915
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFailed, this, [this] (const QString &errorMessage) { Q_EMIT passiveMessage(errorMessage); });
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
916
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFinished, this, &FlatpakBackend::integrateRemote);
917
918
919
    connect(job, &FlatpakRefreshAppstreamMetadataJob::finished, this, [this] { acquireFetching(false); });

    acquireFetching(true);
920
921
922
    job->start();
}

923
bool FlatpakBackend::setupFlatpakInstallations(GError **error)
Jan Grulich's avatar
Jan Grulich committed
924
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
925
926
927
928
929
930
931
932
    if (qEnvironmentVariableIsSet("FLATPAK_TEST_MODE")) {
        const QString path = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QLatin1String("/discover-flatpak-test");
        qDebug() << "running flatpak backend on test mode" << path;
        g_autoptr(GFile) file = g_file_new_for_path(QFile::encodeName(path).constData());
        m_installations << flatpak_installation_new_for_path(file, true, m_cancellable, error);
        return true;
    }

933
    g_autoptr(GPtrArray) installations = flatpak_get_system_installations(m_cancellable, error);
934
935
936
937
    if (*error) {
        qWarning() << "Failed to call flatpak_get_system_installations:" << (*error)->message;
    }
    for (uint i = 0; installations && i < installations->len; i++) {
938
939
940
        auto installation = FLATPAK_INSTALLATION(g_ptr_array_index(installations, i));
        g_object_ref(installation);
        m_installations << installation;
Jan Grulich's avatar
Jan Grulich committed
941
942
    }

943
944
945
    auto user = flatpak_installation_new_user(m_cancellable, error);
    if (user) {
        m_installations << user;
Jan Grulich's avatar
Jan Grulich committed
946
947
    }

948
    return !m_installations.isEmpty();
Jan Grulich's avatar
Jan Grulich committed
949
950
}

951
void FlatpakBackend::updateAppInstalledMetadata(FlatpakInstalledRef *installedRef, FlatpakResource *resource)
Jan Grulich's avatar
Jan Grulich committed
952
{
953
    // Update the rest
954
    resource->updateFromRef(FLATPAK_REF(installedRef));
955
    resource->setInstalledSize(flatpak_installed_ref_get_installed_size(installedRef));
956
    resource->setOrigin(QString::fromUtf8(flatpak_installed_ref_get_origin(installedRef)));
957
958
    if (resource->state() < AbstractResource::Installed)
        resource->setState(AbstractResource::Installed);
959
960
}

961
bool FlatpakBackend::updateAppMetadata(FlatpakResource *resource)
962
{
963
    if (resource->resourceType() != FlatpakResource::DesktopApp) {
964
        return true;
Jan Grulich's avatar
Jan Grulich committed
965
966
    }

967
    const QString path = resource->installPath() + QStringLiteral("/metadata");
Jan Grulich's avatar
Jan Grulich committed
968

969
    if (QFile::exists(path)) {
970
        return updateAppMetadata(resource, path);
971
    } else {
972
        auto fw = new QFutureWatcher<QByteArray>(this);
973
        connect(fw, &QFutureWatcher<QByteArray>::finished, this, [this, resource, fw]() {
974
975
            const auto metadata = fw->result();
            if (!metadata.isEmpty())
976
                onFetchMetadataFinished(resource, metadata);
977
978
            fw->deleteLater();
        });
979
        fw->setFuture(QtConcurrent::run(&m_threadPool, &FlatpakRunnables::fetchMetadata, resource, m_cancellable));
980

981
        // Return false to indicate we cannot continue (right now used only in updateAppSize())
982
983
        return false;
    }
984
985
}

986
void FlatpakBackend::onFetchMetadataFinished(FlatpakResource *resource, const QByteArray &metadata)
987
988
{
    updateAppMetadata(resource, metadata);
989

990
991
    // Right now we attempt to update metadata for calculating the size so call updateSizeFromRemote()
    // as it's what we want. In future if there are other reason to update metadata we will need to somehow
992
    // distinguish between these calls
993
    updateAppSizeFromRemote(resource);
Jan Grulich's avatar
Jan Grulich committed
994
995
}

996
997
998
999
1000
bool FlatpakBackend::updateAppMetadata(FlatpakResource *resource, const QString &path)
{
    // Parse the temporary file
    QSettings setting(path, QSettings::NativeFormat);
    setting.beginGroup(QLatin1String("Application"));