File indexing completed on 2024-05-05 17:33:20

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QCollatorSortKey>
0010 #include <QColor>
0011 #include <QDate>
0012 #include <QJsonArray>
0013 #include <QJsonObject>
0014 #include <QObject>
0015 #include <QScopedPointer>
0016 #include <QSet>
0017 #include <QStringList>
0018 #include <QUrl>
0019 #include <QVector>
0020 
0021 #include "PackageState.h"
0022 #include "discovercommon_export.h"
0023 
0024 class Category;
0025 class Rating;
0026 class AbstractResourcesBackend;
0027 
0028 struct Screenshot {
0029     Screenshot(const QUrl &screenshot)
0030         : thumbnail(screenshot)
0031         , screenshot(screenshot)
0032     {
0033     }
0034 
0035     Screenshot(const QUrl &thumbnail, const QUrl &screenshot, bool isAnimated)
0036         : thumbnail(thumbnail)
0037         , screenshot(screenshot)
0038         , isAnimated(isAnimated)
0039     {
0040     }
0041 
0042     QUrl thumbnail;
0043     QUrl screenshot;
0044     bool isAnimated = false;
0045 };
0046 
0047 using Screenshots = QVector<Screenshot>;
0048 
0049 /**
0050  * \class AbstractResource  AbstractResource.h "AbstractResource.h"
0051  *
0052  * \brief This is the base class of all resources.
0053  *
0054  * Each backend must reimplement its own resource class which needs to derive from this one.
0055  */
0056 class DISCOVERCOMMON_EXPORT AbstractResource : public QObject
0057 {
0058     Q_OBJECT
0059     Q_PROPERTY(QString name READ name CONSTANT)
0060     Q_PROPERTY(QString packageName READ packageName CONSTANT)
0061     Q_PROPERTY(QString comment READ comment CONSTANT)
0062     Q_PROPERTY(QVariant icon READ icon NOTIFY iconChanged)
0063     Q_PROPERTY(bool canExecute READ canExecute CONSTANT)
0064     Q_PROPERTY(State state READ state NOTIFY stateChanged)
0065     Q_PROPERTY(QString status READ status NOTIFY stateChanged)
0066     Q_PROPERTY(QStringList category READ categories CONSTANT)
0067     Q_PROPERTY(QUrl homepage READ homepage CONSTANT)
0068     Q_PROPERTY(QUrl helpURL READ helpURL CONSTANT)
0069     Q_PROPERTY(QUrl bugURL READ bugURL CONSTANT)
0070     Q_PROPERTY(QUrl donationURL READ donationURL CONSTANT)
0071     Q_PROPERTY(QUrl contributeURL READ contributeURL CONSTANT)
0072     Q_PROPERTY(bool canUpgrade READ canUpgrade NOTIFY stateChanged)
0073     Q_PROPERTY(bool isInstalled READ isInstalled NOTIFY stateChanged)
0074     Q_PROPERTY(QJsonArray licenses READ licenses NOTIFY licensesChanged)
0075     Q_PROPERTY(QString longDescription READ longDescription NOTIFY longDescriptionChanged)
0076     Q_PROPERTY(QString origin READ origin CONSTANT)
0077     Q_PROPERTY(QString displayOrigin READ displayOrigin CONSTANT)
0078     Q_PROPERTY(quint64 size READ size NOTIFY sizeChanged)
0079     Q_PROPERTY(QString sizeDescription READ sizeDescription NOTIFY sizeChanged)
0080     Q_PROPERTY(QString installedVersion READ installedVersion NOTIFY versionsChanged)
0081     Q_PROPERTY(QString availableVersion READ availableVersion NOTIFY versionsChanged)
0082     Q_PROPERTY(QString section READ section CONSTANT)
0083     Q_PROPERTY(QStringList mimetypes READ mimetypes CONSTANT)
0084     Q_PROPERTY(QObject *backend READ backendObject CONSTANT)
0085     Q_PROPERTY(QVariant rating READ ratingVariant NOTIFY ratingFetched)
0086     Q_PROPERTY(QString appstreamId READ appstreamId CONSTANT)
0087     Q_PROPERTY(QUrl url READ url CONSTANT)
0088     Q_PROPERTY(QString executeLabel READ executeLabel CONSTANT)
0089     Q_PROPERTY(QString sourceIcon READ sourceIcon CONSTANT)
0090     Q_PROPERTY(QString author READ author CONSTANT)
0091     Q_PROPERTY(QDate releaseDate READ releaseDate NOTIFY versionsChanged)
0092     Q_PROPERTY(QString upgradeText READ upgradeText NOTIFY versionsChanged)
0093     Q_PROPERTY(bool isRemovable READ isRemovable CONSTANT)
0094     Q_PROPERTY(QString versionString READ versionString NOTIFY versionsChanged)
0095     Q_PROPERTY(QString contentRatingText READ contentRatingText CONSTANT)
0096     Q_PROPERTY(QString contentRatingDescription READ contentRatingDescription CONSTANT)
0097     Q_PROPERTY(ContentIntensity contentRatingIntensity READ contentRatingIntensity CONSTANT)
0098     Q_PROPERTY(uint contentRatingMinimumAge READ contentRatingMinimumAge CONSTANT)
0099 public:
0100     /**
0101      * This describes the state of the resource
0102      */
0103     enum State {
0104         /**
0105          * When the resource is somehow broken
0106          */
0107         Broken,
0108         /**
0109          * This means that the resource is neither installed nor broken
0110          */
0111         None,
0112         /**
0113          * The resource is installed and up-to-date
0114          */
0115         Installed,
0116         /**
0117          * The resource is installed and an update is available
0118          */
0119         Upgradeable,
0120     };
0121     Q_ENUM(State)
0122 
0123     enum ContentIntensity {
0124         Mild,
0125         Intense,
0126     };
0127     Q_ENUM(ContentIntensity)
0128 
0129     /**
0130      * Constructs the AbstractResource with its corresponding backend
0131      */
0132     explicit AbstractResource(AbstractResourcesBackend *parent);
0133     ~AbstractResource() override;
0134 
0135     /// used as internal identification of a resource
0136     virtual QString packageName() const = 0;
0137 
0138     /// resource name to be displayed
0139     virtual QString name() const = 0;
0140 
0141     /// short description of the resource
0142     virtual QString comment() = 0;
0143 
0144     /// xdg-compatible icon name to represent the resource, url or QIcon
0145     virtual QVariant icon() const = 0;
0146 
0147     ///@returns whether invokeApplication makes something
0148     /// false if not overridden
0149     virtual bool canExecute() const = 0;
0150 
0151     /// executes the resource, if applies.
0152     Q_SCRIPTABLE virtual void invokeApplication() const = 0;
0153 
0154     virtual State state() = 0;
0155 
0156     virtual QStringList categories() = 0;
0157     ///@returns a URL that points to the app's website
0158     virtual QUrl homepage();
0159     ///@returns a URL that points to the app's online documentation
0160     virtual QUrl helpURL();
0161     ///@returns a URL that points to the place where you can file a bug
0162     virtual QUrl bugURL();
0163     ///@returns a URL that points to the place where you can donate money to the app developer
0164     virtual QUrl donationURL();
0165     ///@returns a URL that points to the place where you can contribute to develop the app
0166     virtual QUrl contributeURL();
0167 
0168     enum Type {
0169         Application,
0170         Addon,
0171         Technical,
0172     };
0173     Q_ENUM(Type)
0174     virtual Type type() const = 0;
0175 
0176     virtual quint64 size() = 0;
0177     virtual QString sizeDescription();
0178 
0179     ///@returns a list of pairs with the name of the license and a URL pointing at it
0180     virtual QJsonArray licenses() = 0;
0181 
0182     virtual QString installedVersion() const = 0;
0183     virtual QString availableVersion() const = 0;
0184     virtual QString longDescription() = 0;
0185 
0186     virtual QString origin() const = 0;
0187     virtual QString displayOrigin() const;
0188     virtual QString section() = 0;
0189     virtual QString author() const = 0;
0190 
0191     ///@returns what kind of mime types the resource can consume
0192     virtual QStringList mimetypes() const;
0193 
0194     virtual QList<PackageState> addonsInformation() = 0;
0195 
0196     virtual QStringList extends() const;
0197 
0198     virtual QString appstreamId() const;
0199 
0200     void addMetadata(const QString &key, const QJsonValue &value);
0201     QJsonValue getMetadata(const QString &key);
0202 
0203     bool canUpgrade();
0204     bool isInstalled();
0205 
0206     ///@returns a user-readable explanation of the resource status
0207     /// by default, it will specify what state() is returning
0208     virtual QString status();
0209 
0210     AbstractResourcesBackend *backend() const;
0211     QObject *backendObject() const;
0212 
0213     /**
0214      * @returns a name sort key for faster sorting
0215      */
0216     QCollatorSortKey nameSortKey();
0217 
0218     /**
0219      * Convenience method to fetch the resource's rating
0220      *
0221      * @returns the rating for the resource or null if not available
0222      */
0223     Rating *rating() const;
0224     QVariant ratingVariant() const;
0225 
0226     bool categoryMatches(Category *cat);
0227 
0228     QSet<Category *> categoryObjects(const QVector<Category *> &cats) const;
0229 
0230     /**
0231      * @returns a url that uniquely identifies the application
0232      */
0233     virtual QUrl url() const;
0234 
0235     virtual QString executeLabel() const;
0236     virtual QString sourceIcon() const = 0;
0237     /**
0238      * @returns the date of the resource's most recent release
0239      */
0240     virtual QDate releaseDate() const = 0;
0241 
0242     virtual QSet<QString> alternativeAppstreamIds() const
0243     {
0244         return {};
0245     }
0246 
0247     virtual QString upgradeText() const;
0248 
0249     /**
0250      * @returns whether the package can ever be removed
0251      */
0252     virtual bool isRemovable() const
0253     {
0254         return true;
0255     }
0256 
0257     virtual QString versionString();
0258 
0259     virtual QString contentRatingText() const;
0260     virtual ContentIntensity contentRatingIntensity() const;
0261     virtual QString contentRatingDescription() const;
0262     virtual uint contentRatingMinimumAge() const;
0263 
0264 public Q_SLOTS:
0265     virtual void fetchScreenshots();
0266     virtual void fetchChangelog() = 0;
0267     virtual void fetchUpdateDetails()
0268     {
0269         fetchChangelog();
0270     }
0271 
0272 Q_SIGNALS:
0273     void iconChanged();
0274     void sizeChanged();
0275     void stateChanged();
0276     void licensesChanged();
0277     void ratingFetched();
0278     void longDescriptionChanged();
0279     void versionsChanged();
0280 
0281     /// response to the fetchScreenshots method
0282     void screenshotsFetched(const Screenshots &screenshots);
0283     void changelogFetched(const QString &changelog);
0284 
0285 private:
0286     void reportNewState();
0287 
0288     //     TODO: make it std::optional or make QCollatorSortKey()
0289     QScopedPointer<QCollatorSortKey> m_collatorKey;
0290     QJsonObject m_metadata;
0291 };
0292 
0293 Q_DECLARE_METATYPE(QVector<AbstractResource *>)