FlatpakBackend.cpp 51.6 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(FlatpakInstallation *flatpakInstallation, FlatpakResource *resource) const
213 214 215 216 217 218 219 220
{
    FlatpakInstalledRef *ref = nullptr;
    g_autoptr(GError) localError = nullptr;

    if (!flatpakInstallation) {
        return ref;
    }

221
    const auto type = resource->resourceType() == FlatpakResource::DesktopApp ? FLATPAK_REF_KIND_APP : FLATPAK_REF_KIND_RUNTIME;
222 223 224

    return flatpak_installation_get_installed_ref(flatpakInstallation,
                                                 type,
225 226 227
                                                 resource->flatpakName().toUtf8().constData(),
                                                 resource->arch().toUtf8().constData(),
                                                 resource->branch().toUtf8().constData(),
228
                                                 m_cancellable, &localError);
229 230
}

231
FlatpakResource * FlatpakBackend::getAppForInstalledRef(FlatpakInstallation *flatpakInstallation, FlatpakInstalledRef *ref) const
Jan Grulich's avatar
Jan Grulich committed
232
{
233 234 235 236
    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
237 238 239
//     if (!r) {
//         qDebug() << "no" << flatpak_ref_get_name(FLATPAK_REF(ref));
//     }
240
    return r;
Jan Grulich's avatar
Jan Grulich committed
241 242
}

243
FlatpakResource * FlatpakBackend::getRuntimeForApp(FlatpakResource *resource) const
244 245
{
    FlatpakResource *runtime = nullptr;
246 247
    const QString runtimeName = resource->runtime();
    const auto runtimeInfo = runtimeName.splitRef(QLatin1Char('/'));
248 249 250 251 252

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

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

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

    return runtime;
}

Jan Grulich's avatar
Jan Grulich committed
269 270 271 272 273 274
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;
275
    AppStream::Component asComponent;
Jan Grulich's avatar
Jan Grulich committed
276

277
    file = g_file_new_for_path(url.toLocalFile().toUtf8().constData());
Jan Grulich's avatar
Jan Grulich committed
278 279 280
    bundleRef = flatpak_bundle_ref_new(file, &localError);

    if (!bundleRef) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
281
        qWarning() << "Failed to load bundle:" << localError->message;
Jan Grulich's avatar
Jan Grulich committed
282 283 284
        return nullptr;
    }

285
    g_autoptr(GBytes) metadata = flatpak_bundle_ref_get_metadata(bundleRef);
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 346 347

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

        gsize len = 0;
        QByteArray metadataContent = QByteArray((char *)g_bytes_get_data(metadata, &len));
        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
348 349
    }

350
    FlatpakResource *resource = new FlatpakResource(asComponent, preferredInstallation(), this);
Jan Grulich's avatar
Jan Grulich committed
351

352
    gsize len = 0;
Jan Grulich's avatar
Jan Grulich committed
353 354
    QByteArray metadataContent = QByteArray((char *)g_bytes_get_data(metadata, &len));
    if (!updateAppMetadata(resource, metadataContent)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
355
        delete resource;
Jan Grulich's avatar
Jan Grulich committed
356 357 358 359
        qWarning() << "Failed to update metadata from app bundle";
        return nullptr;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
360
    g_autoptr(GBytes) iconData = flatpak_bundle_ref_get_icon(bundleRef, 128);
Jan Grulich's avatar
Jan Grulich committed
361 362 363 364 365 366 367
    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
368 369 370

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

374 375
    const QString origin = QString::fromUtf8(flatpak_bundle_ref_get_origin(bundleRef));

376
    resource->setDownloadSize(0);
Jan Grulich's avatar
Jan Grulich committed
377
    resource->setInstalledSize(flatpak_bundle_ref_get_installed_size(bundleRef));
378 379
    resource->setPropertyState(FlatpakResource::DownloadSize, FlatpakResource::AlreadyKnown);
    resource->setPropertyState(FlatpakResource::InstalledSize, FlatpakResource::AlreadyKnown);
Jan Grulich's avatar
Jan Grulich committed
380
    resource->setFlatpakFileType(QStringLiteral("flatpak"));
381
    resource->setOrigin(origin.isEmpty() ? i18n("Local bundle") : origin);
Jan Grulich's avatar
Jan Grulich committed
382 383 384 385 386 387 388 389 390 391 392 393
    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();
394 395 396 397 398 399 400 401 402 403 404
    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
405

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
406
    g_autoptr(GError) error = nullptr;
Jan Grulich's avatar
Jan Grulich committed
407 408 409 410 411 412 413 414 415 416 417
    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());

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

    const auto remoteName = flatpak_remote_ref_get_remote_name(remoteRef);

    auto ref = FLATPAK_REF(remoteRef);

433 434 435 436 437
    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());
438
    asComponent.setId(name);
439

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

448
    auto resource = new FlatpakResource(asComponent, preferredInstallation(), this);
Jan Grulich's avatar
Jan Grulich committed
449 450 451
    resource->setFlatpakFileType(QStringLiteral("flatpakref"));
    resource->setOrigin(QString::fromUtf8(remoteName));
    resource->updateFromRef(ref);
452 453 454

    QUrl runtimeUrl = QUrl(settings.value(QStringLiteral("Flatpak Ref/RuntimeRepo")).toString());
    if (!runtimeUrl.isEmpty()) {
455
        auto installation = preferredInstallation();
456
        // We need to fetch metadata to find information about required runtime
457 458 459
        auto fw = new QFutureWatcher<QByteArray>(this);
        connect(fw, &QFutureWatcher<QByteArray>::finished, this, [this, installation, resource, fw, runtimeUrl]() {
            const auto metadata = fw->result();
460
            // Even when we failed to fetch information about runtime we still want to show the application
461 462
            if (metadata.isEmpty()) {
                onFetchMetadataFinished(installation, resource, metadata);
463
            } else {
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                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);
                }
480
            }
481
            fw->deleteLater();
482
        });
483
        fw->setFuture(QtConcurrent::run(&m_threadPool, &FlatpakRunnables::fetchMetadata, installation, resource));
484 485 486 487
    } else {
        addResource(resource);
    }

Jan Grulich's avatar
Jan Grulich committed
488 489 490
    return resource;
}

491 492
FlatpakResource * FlatpakBackend::addSourceFromFlatpakRepo(const QUrl &url)
{
493
    Q_ASSERT(url.isLocalFile());
494 495 496 497 498 499 500 501 502 503
    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;
    }

504
    if (gpgKey.startsWith(QLatin1String("http://")) || gpgKey.startsWith(QLatin1String("https://"))) {
505 506 507
        return nullptr;
    }

508 509 510 511 512 513 514
    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());

515
    const QString iconUrl = settings.value(QStringLiteral("Flatpak Repo/Icon")).toString();
516
    if (!iconUrl.isEmpty()) {
517 518 519 520
        AppStream::Icon icon;
        icon.setKind(AppStream::Icon::KindRemote);
        icon.setUrl(QUrl(iconUrl));
        asComponent.addIcon(icon);
521 522
    }

523
    auto resource = new FlatpakResource(asComponent, preferredInstallation(), this);
524 525 526 527 528 529 530
    // 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);

531
    auto repo = flatpak_installation_get_remote_by_name(preferredInstallation(), resource->flatpakName().toUtf8().constData(), m_cancellable, nullptr);
532 533 534 535 536 537 538 539 540
    if (!repo) {
        resource->setState(AbstractResource::State::None);
    } else {
        resource->setState(AbstractResource::State::Installed);
    }

    return resource;
}

541 542 543 544
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
545
        qWarning() << "Failed to parse metadata from app bundle for" << resource->name();
546 547
    }

548
    updateAppState(resource);
549

550
    // This will update also metadata (required runtime)
551
    updateAppSize(resource);
552 553

    m_resources.insert(resource->uniqueId(), resource);
554 555 556 557
    if (!resource->extends().isEmpty()) {
        m_extends.append(resource->extends());
        m_extends.removeDuplicates();
    }
558 559
}

560
class FlatpakSource
Jan Grulich's avatar
Jan Grulich committed
561
{
562 563
public:
    FlatpakSource(FlatpakRemote* remote) : m_remote(remote) {}
Jan Grulich's avatar
Jan Grulich committed
564

565 566 567
    bool isEnabled() const
    {
        return !flatpak_remote_get_disabled(m_remote);
Jan Grulich's avatar
Jan Grulich committed
568 569
    }

570 571 572 573
    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
574
            qWarning() << "No appstream dir for" << flatpak_remote_get_name(m_remote);
575 576 577 578 579 580 581 582 583 584 585 586 587 588
            return {};
        }
        return QString::fromUtf8(g_file_get_path(appstreamDir));
    }

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

private:
    FlatpakRemote* m_remote;
};

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

596 597 598 599 600 601
        if (!loadAppsFromAppstreamData(installation)) {
            qWarning() << "Failed to load packages from appstream data from installation" << installation;
        }
    }
}

602 603 604 605
bool FlatpakBackend::loadAppsFromAppstreamData(FlatpakInstallation *flatpakInstallation)
{
    Q_ASSERT(flatpakInstallation);

606
    GPtrArray *remotes = flatpak_installation_list_remotes(flatpakInstallation, m_cancellable, nullptr);
Jan Grulich's avatar
Jan Grulich committed
607 608 609 610
    if (!remotes) {
        return false;
    }

611 612
    m_refreshAppstreamMetadataJobs += remotes->len;

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

617 618 619 620 621 622 623
        QFileInfo fileInfo = QFileInfo(QString::fromUtf8(g_file_get_path(fileTimestamp)));
        // 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);
        }
624 625 626
    }
    return true;
}
Jan Grulich's avatar
Jan Grulich committed
627

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

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

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

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

656 657 658 659
    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();
660 661
        QVector<FlatpakResource*> resources;
        for (const AppStream::Component& appstreamComponent : components) {
662 663 664
            FlatpakResource *resource = new FlatpakResource(appstreamComponent, flatpakInstallation, this);
            resource->setIconPath(appstreamIconsPath);
            resource->setOrigin(sourceName);
665 666 667 668 669 670 671
            if (resource->resourceType() == FlatpakResource::Runtime) {
                resources.prepend(resource);
            } else {
                resources.append(resource);
            }
        }
        for (auto resource : qAsConst(resources)) {
672 673
            addResource(resource);
        }
674

675
        metadataRefreshed();
676
        acquireFetching(false);
677 678
        fw->deleteLater();
    });
679 680 681 682 683 684 685 686 687 688 689 690
    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();
    }));
691 692 693 694 695 696 697 698 699 700
}

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
701 702
}

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

707 708 709 710 711 712 713
    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;
    }

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

717
    QVector<FlatpakResource*> resources;
718 719
    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
720

721 722 723 724
        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;

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

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

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

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

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

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

    return true;
}

Jan Grulich's avatar
Jan Grulich committed
765 766 767 768 769 770 771
void FlatpakBackend::loadLocalUpdates(FlatpakInstallation *flatpakInstallation)
{
    g_autoptr(GError) localError = nullptr;
    g_autoptr(GPtrArray) refs = nullptr;

    refs = flatpak_installation_list_installed_refs(flatpakInstallation, m_cancellable, &localError);
    if (!refs) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
772
        qWarning() << "Failed to get list of installed refs for listing updates:" << localError->message;
Jan Grulich's avatar
Jan Grulich committed
773 774 775 776 777
        return;
    }

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

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
779
        const gchar *latestCommit = flatpak_installed_ref_get_latest_commit(ref);
Jan Grulich's avatar
Jan Grulich committed
780 781

        if (!latestCommit) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
782
            qWarning() << "Couldn't get latest commit for" << flatpak_ref_get_name(FLATPAK_REF(ref));
783
            continue;
Jan Grulich's avatar
Jan Grulich committed
784 785
        }

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

        FlatpakResource *resource = getAppForInstalledRef(flatpakInstallation, ref);
        if (resource) {
            resource->setState(AbstractResource::Upgradeable);
794
            updateAppSize(resource);
Jan Grulich's avatar
Jan Grulich committed
795 796 797 798
        }
    }
}

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

823
void FlatpakBackend::onFetchUpdatesFinished(FlatpakInstallation *flatpakInstallation, GPtrArray *fetchedUpdates)
824
{
825
    if (!fetchedUpdates) {
826 827 828 829
        qWarning() << "could not get updates for" << flatpakInstallation;
        return;
    }

830 831
    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
832 833 834
        FlatpakResource *resource = getAppForInstalledRef(flatpakInstallation, ref);
        if (resource) {
            resource->setState(AbstractResource::Upgradeable);
835
            updateAppSize(resource);
836
        } else
837
            qWarning() << "could not find updated resource" << flatpak_ref_get_name(FLATPAK_REF(ref)) << m_resources.size();
Jan Grulich's avatar
Jan Grulich committed
838 839 840
    }
}

841 842 843 844
bool FlatpakBackend::parseMetadataFromAppBundle(FlatpakResource *resource)
{
    g_autoptr(FlatpakRef) ref = nullptr;
    g_autoptr(GError) localError = nullptr;
845
    AppStream::Bundle bundle = resource->appstreamComponent().bundle(AppStream::Bundle::KindFlatpak);
846 847

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

    return true;
}

861 862 863 864 865 866
class FlatpakRefreshAppstreamMetadataJob : public QThread
{
    Q_OBJECT
public:
    FlatpakRefreshAppstreamMetadataJob(FlatpakInstallation *installation, FlatpakRemote *remote)
        : QThread()
867
        , m_cancellable(g_cancellable_new())
868 869 870
        , m_installation(installation)
        , m_remote(remote)
    {
871
        connect(this, &FlatpakRefreshAppstreamMetadataJob::finished, this, &QObject::deleteLater);
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
    }

    ~FlatpakRefreshAppstreamMetadataJob()
    {
        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
894
#endif
895 896 897
            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
898 899
        } else  {
            Q_EMIT jobRefreshAppstreamMetadataFinished(m_installation, m_remote);
900 901 902 903
        }
    }

Q_SIGNALS:
904
    void jobRefreshAppstreamMetadataFailed(const QString &errorMessage);
905 906 907 908 909 910 911 912 913 914 915
    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);
916
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFailed, this, &FlatpakBackend::metadataRefreshed);
917
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFailed, this, [this] (const QString &errorMessage) { Q_EMIT passiveMessage(errorMessage); });
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
918
    connect(job, &FlatpakRefreshAppstreamMetadataJob::jobRefreshAppstreamMetadataFinished, this, &FlatpakBackend::integrateRemote);
919 920 921
    connect(job, &FlatpakRefreshAppstreamMetadataJob::finished, this, [this] { acquireFetching(false); });

    acquireFetching(true);
922 923 924
    job->start();
}