File indexing completed on 2024-04-21 15:02:25

0001 /*
0002     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef ENGINE_H
0008 #define ENGINE_H
0009 
0010 #include <QObject>
0011 #include <QQmlListProperty>
0012 
0013 #include "entrywrapper.h"
0014 #include "errorcode.h"
0015 #include "knewstuffquick_export.h"
0016 
0017 class EnginePrivate;
0018 
0019 /**
0020  * @short Encapsulates a KNSCore::Engine for use in Qt Quick
0021  *
0022  * This class takes care of initialisation of a KNSCore::Engine when assigned a config file.
0023  * The actual KNSCore:Engine can be read through the Engine::engine property.
0024  *
0025  * @see ItemsModel
0026  */
0027 class Engine : public QObject
0028 {
0029     Q_OBJECT
0030 #if KNEWSTUFFQUICK_BUILD_DEPRECATED_SINCE(5, 81)
0031     KNEWSTUFFQUICK_DEPRECATED_VERSION(5, 81, "Use NewStuff.Settings.allowedByKiosk instead")
0032     Q_PROPERTY(bool allowedByKiosk READ allowedByKiosk CONSTANT)
0033 #endif
0034     Q_PROPERTY(QString configFile READ configFile WRITE setConfigFile NOTIFY configFileChanged)
0035     Q_PROPERTY(QObject *engine READ engine NOTIFY engineChanged)
0036     /**
0037      * Whether or not the engine is performing its initial loading operations
0038      * @since 5.65
0039      */
0040     Q_PROPERTY(bool isLoading READ isLoading NOTIFY isLoadingChanged)
0041     Q_PROPERTY(bool hasAdoptionCommand READ hasAdoptionCommand NOTIFY engineInitialized)
0042     Q_PROPERTY(QString name READ name NOTIFY engineInitialized)
0043     Q_PROPERTY(QObject *categories READ categories NOTIFY categoriesChanged)
0044     Q_PROPERTY(QStringList categoriesFilter READ categoriesFilter WRITE setCategoriesFilter RESET resetCategoriesFilter NOTIFY categoriesFilterChanged)
0045     Q_PROPERTY(int filter READ filter WRITE setFilter NOTIFY filterChanged)
0046     Q_PROPERTY(int sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
0047     Q_PROPERTY(QString searchTerm READ searchTerm WRITE setSearchTerm RESET resetSearchTerm NOTIFY searchTermChanged)
0048     Q_PROPERTY(QObject *searchPresetModel READ searchPresetModel NOTIFY searchPresetModelChanged)
0049 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 82)
0050     Q_PROPERTY(QQmlListProperty<KNSCore::EntryWrapper> changedEntries READ changedEntries NOTIFY changedEntriesChanged)
0051     Q_PROPERTY(int changedEntriesCount READ changedEntriesCount NOTIFY changedEntriesChanged)
0052 #endif
0053     Q_PROPERTY(bool isValid READ isValid NOTIFY engineInitialized)
0054 public:
0055     explicit Engine(QObject *parent = nullptr);
0056     ~Engine() override;
0057 
0058 #if KNEWSTUFFQUICK_BUILD_DEPRECATED_SINCE(5, 81)
0059     KNEWSTUFFQUICK_DEPRECATED_VERSION(5, 81, "Use KNewStuffQuick::Settings::allowedByKiosk() instead")
0060     bool allowedByKiosk() const;
0061 #endif
0062 
0063     enum EntryEvent {
0064         UnknownEvent = KNSCore::EntryInternal::UnknownEvent,
0065         StatusChangedEvent = KNSCore::EntryInternal::StatusChangedEvent,
0066         AdoptedEvent = KNSCore::EntryInternal::AdoptedEvent,
0067         DetailsLoadedEvent = KNSCore::EntryInternal::DetailsLoadedEvent,
0068     };
0069     Q_ENUM(EntryEvent)
0070 
0071     /**
0072      * Registering the error codes from KNSCore to allow them to be used easily in QtQuick
0073      * @see KNSCore::ErrorCode
0074      * @since 5.84
0075      */
0076     enum ErrorCode {
0077         UnknownError = KNSCore::ErrorCode::UnknownError,
0078         NetworkError = KNSCore::ErrorCode::NetworkError,
0079         OcsError = KNSCore::ErrorCode::OcsError,
0080         ConfigFileError = KNSCore::ErrorCode::ConfigFileError,
0081         ProviderError = KNSCore::ErrorCode::ProviderError,
0082         InstallationError = KNSCore::ErrorCode::InstallationError,
0083         ImageError = KNSCore::ErrorCode::ImageError,
0084         AdoptionError = KNSCore::ErrorCode::AdoptionError,
0085         TryAgainLaterError = KNSCore::ErrorCode::TryAgainLaterError,
0086     };
0087     Q_ENUM(ErrorCode)
0088 
0089     QString configFile() const;
0090     void setConfigFile(const QString &newFile);
0091     Q_SIGNAL void configFileChanged();
0092 
0093     QObject *engine() const;
0094     Q_SIGNAL void engineChanged();
0095 
0096     /**
0097      * Whether or not the engine is performing its initial loading operations
0098      * @since 5.65
0099      */
0100     bool isLoading() const;
0101     /**
0102      * Fired when the isLoading value changes
0103      * @since 5.65
0104      */
0105     Q_SIGNAL void isLoadingChanged();
0106 
0107     bool hasAdoptionCommand() const;
0108     QString name() const;
0109     Q_SIGNAL void engineInitialized();
0110 
0111     QObject *categories() const;
0112     Q_SIGNAL void categoriesChanged();
0113 
0114     QStringList categoriesFilter() const;
0115     void setCategoriesFilter(const QStringList &newCategoriesFilter);
0116     Q_INVOKABLE void resetCategoriesFilter();
0117     Q_SIGNAL void categoriesFilterChanged();
0118 
0119     int filter() const;
0120     void setFilter(int newFilter);
0121     Q_SIGNAL void filterChanged();
0122 
0123     int sortOrder() const;
0124     void setSortOrder(int newSortOrder);
0125     Q_SIGNAL void sortOrderChanged();
0126 
0127     QString searchTerm() const;
0128     void setSearchTerm(const QString &newSearchTerm);
0129     Q_INVOKABLE void resetSearchTerm();
0130     Q_SIGNAL void searchTermChanged();
0131 
0132     QObject *searchPresetModel() const;
0133     Q_SIGNAL void searchPresetModelChanged();
0134 
0135 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 82)
0136     QQmlListProperty<KNSCore::EntryWrapper> changedEntries();
0137     Q_INVOKABLE void resetChangedEntries();
0138     Q_SIGNAL void changedEntriesChanged();
0139     int changedEntriesCount() const;
0140 #endif
0141 
0142     bool isValid();
0143 Q_SIGNALS:
0144     void message(const QString &message);
0145     void idleMessage(const QString &message);
0146     void busyMessage(const QString &message);
0147     void errorMessage(const QString &message);
0148 
0149     /**
0150      * This is fired for events related directly to a single EntryInternal instance
0151      * The intermediate states Updating and Installing are not forwarded. In case you
0152      * need those you have to listen to the signals of the KNSCore::Engine instance of the engine property.
0153      *
0154      * As an example, if you need to know when the status of an entry changes, you might write:
0155      \code
0156         function onEntryEvent(entry, event) {
0157             if (event == NewStuff.Engine.StatusChangedEvent) {
0158                 myModel.ghnsEntryChanged(entry);
0159             }
0160         }
0161      \endcode
0162      *
0163      * nb: The above example is also how one would port a handler for the old changedEntries signal
0164      *
0165      * @see EntryInternal::EntryEvent for details on which specific event is being notified
0166      * @since 5.81
0167      */
0168     void entryEvent(KNSCore::EntryWrapper *entry, EntryEvent event);
0169 
0170     /**
0171      * Fires in the case of any critical or serious errors, such as network or API problems.
0172      * This forwards the signal from KNSCore::Engine::signalErrorCode, but with QML friendly
0173      * enumerations.
0174      * @param errorCode Represents the specific type of error which has occurred
0175      * @param message A human-readable message which can be shown to the end user
0176      * @param metadata Any additional data which might be hepful to further work out the details of the error (see KNSCore::EntryInternal::ErrorCode for the
0177      * metadata details)
0178      * @see KNSCore::Engine::signalErrorCode
0179      * @since 5.84
0180      */
0181     void errorCode(const Engine::ErrorCode &errorCode, const QString &message, const QVariant &metadata);
0182 
0183 private:
0184     const std::unique_ptr<EnginePrivate> d;
0185 };
0186 
0187 #endif // ENGINE_H