File indexing completed on 2024-11-17 04:55:38

0001 /*
0002  *   SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *   SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #pragma once
0009 
0010 #include "FlatpakResource.h"
0011 
0012 #include <QSharedPointer>
0013 #include <QThreadPool>
0014 #include <QVariantList>
0015 #include <resources/AbstractResourcesBackend.h>
0016 
0017 #include <AppStreamQt/component.h>
0018 
0019 #include "flatpak-helper.h"
0020 
0021 class FlatpakSourcesBackend;
0022 class FlatpakSource;
0023 class StandardBackendUpdater;
0024 class OdrsReviewsBackend;
0025 
0026 namespace AppStream
0027 {
0028 class Pool;
0029 }
0030 
0031 class FlatpakBackend : public AbstractResourcesBackend
0032 {
0033     Q_OBJECT
0034 public:
0035     explicit FlatpakBackend(QObject *parent = nullptr);
0036     ~FlatpakBackend();
0037 
0038     int updatesCount() const override;
0039     AbstractBackendUpdater *backendUpdater() const override;
0040     AbstractReviewsBackend *reviewsBackend() const override;
0041     ResultsStream *search(const AbstractResourcesBackend::Filters &search) override;
0042     ResultsStream *findResourceByPackageName(const QUrl &search);
0043     bool isValid() const override;
0044 
0045     Transaction *installApplication(AbstractResource *app) override;
0046     Transaction *installApplication(AbstractResource *app, const AddonList &addons) override;
0047     Transaction *removeApplication(AbstractResource *app) override;
0048     bool isFetching() const override
0049     {
0050         return m_isFetching > 0;
0051     }
0052     void checkForUpdates() override;
0053     QString displayName() const override;
0054     bool hasApplications() const override
0055     {
0056         return true;
0057     }
0058     bool extends(const QString &extends) const override;
0059 
0060     void addSourceFromFlatpakRepo(const QUrl &url, ResultsStream *stream);
0061     void addAppFromFlatpakBundle(const QUrl &url, ResultsStream *stream);
0062     void addAppFromFlatpakRef(const QUrl &url, ResultsStream *stream);
0063     FlatpakResource *getAppForInstalledRef(FlatpakInstallation *flatpakInstallation, FlatpakInstalledRef *ref, bool *freshResource = nullptr) const;
0064 
0065     FlatpakSourcesBackend *sources() const
0066     {
0067         return m_sources;
0068     }
0069 
0070     bool updateAppSize(FlatpakResource *resource);
0071     FlatpakInstalledRef *getInstalledRefForApp(const FlatpakResource *resource) const;
0072     void loadRemote(FlatpakInstallation *installation, FlatpakRemote *remote);
0073     void unloadRemote(FlatpakInstallation *installation, FlatpakRemote *remote);
0074 
0075     InlineMessage *explainDysfunction() const override;
0076     QVector<FlatpakInstallation *> installations() const
0077     {
0078         return m_installations;
0079     }
0080 
0081     bool isTracked(FlatpakResource *resource) const;
0082     QThreadPool *threadPool()
0083     {
0084         return &m_threadPool;
0085     }
0086 
0087     GCancellable *cancellable() const
0088     {
0089         return m_cancellable;
0090     }
0091 
0092 private Q_SLOTS:
0093     void onFetchMetadataFinished(FlatpakResource *resource, const QByteArray &metadata);
0094     void onFetchSizeFinished(FlatpakResource *resource, guint64 downloadSize, guint64 installedSize);
0095 
0096 Q_SIGNALS: // for tests
0097     void initialized();
0098 
0099 private:
0100     friend class FlatpakSource;
0101 
0102     void metadataRefreshed(FlatpakRemote *remote);
0103     bool flatpakResourceLessThan(const StreamResult &l, const StreamResult &r) const;
0104     bool flatpakResourceLessThan(AbstractResource *l, AbstractResource *r) const;
0105     FlatpakInstallation *preferredInstallation() const
0106     {
0107         return m_installations.constFirst();
0108     }
0109     QSharedPointer<FlatpakSource> integrateRemote(FlatpakInstallation *flatpakInstallation, FlatpakRemote *remote);
0110     FlatpakRemote *getFlatpakRemoteByUrl(const QString &url, FlatpakInstallation *installation) const;
0111     FlatpakResource *getRuntimeForApp(FlatpakResource *resource) const;
0112     FlatpakResource *resourceForComponent(const AppStream::Component &component, const QSharedPointer<FlatpakSource> &source) const;
0113     void checkRepositories(const QMap<QString, QStringList> &names);
0114 
0115     void loadAppsFromAppstreamData();
0116     bool loadAppsFromAppstreamData(FlatpakInstallation *flatpakInstallation);
0117     void loadLocalUpdates(FlatpakInstallation *flatpakInstallation);
0118     bool parseMetadataFromAppBundle(FlatpakResource *resource);
0119     bool setupFlatpakInstallations(GError **error);
0120     void updateAppInstalledMetadata(FlatpakInstalledRef *installedRef, FlatpakResource *resource);
0121     bool updateAppMetadata(FlatpakResource *resource);
0122     bool updateAppMetadata(FlatpakResource *resource, const QByteArray &data);
0123     bool updateAppMetadata(FlatpakResource *resource, const QString &path);
0124     bool updateAppSizeFromRemote(FlatpakResource *resource);
0125     void updateAppState(FlatpakResource *resource);
0126     QSharedPointer<FlatpakSource> findSource(FlatpakInstallation *installation, const QString &origin) const;
0127 
0128     QVector<StreamResult> resultsByAppstreamName(const QString &name) const;
0129     void acquireFetching(bool f);
0130     void checkForRemoteUpdates(FlatpakInstallation *flatpakInstallation, FlatpakRemote *remote);
0131     void createPool(QSharedPointer<FlatpakSource> source);
0132     FlatpakRemote *installSource(FlatpakResource *resource);
0133 
0134     ResultsStream *deferredResultStream(const QString &streamName, std::function<void(ResultsStream *)> callback);
0135 
0136     StandardBackendUpdater *m_updater;
0137     FlatpakSourcesBackend *m_sources = nullptr;
0138     QSharedPointer<OdrsReviewsBackend> m_reviews;
0139     uint m_isFetching = 0;
0140     QSet<FlatpakRemote *> m_refreshAppstreamMetadataJobs;
0141 
0142     GCancellable *m_cancellable;
0143     QVector<FlatpakInstallation *> m_installations;
0144     QThreadPool m_threadPool;
0145     QVector<QSharedPointer<FlatpakSource>> m_flatpakSources;
0146     QVector<QSharedPointer<FlatpakSource>> m_flatpakLoadingSources;
0147     QSharedPointer<FlatpakSource> m_localSource;
0148     QTimer *const m_checkForUpdatesTimer;
0149 };