File indexing completed on 2024-05-05 12:20:49

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 ITEMSMODEL_H
0008 #define ITEMSMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 
0012 #include <memory>
0013 
0014 class ItemsModelPrivate;
0015 
0016 /**
0017  * @short A model which shows the contents found in an Engine
0018  *
0019  * Use an instance of this model to show the content items represented by the configuration
0020  * file passed to an engine. The following sample assumes you are using the Engine component,
0021  * however it is also possible to pass a KNSCore::Engine instance created from C++ to this
0022  * property, if you have specific requirements not covered by the convenience component.
0023  *
0024  * Most data in the model is simple, but the DownloadLinks role will return a list of
0025  * DownloadLinkInfo entries, which you will need to manage in some way.
0026  *
0027  * You might also look at NewStuffList, NewStuffItem, and the other items, to see some more
0028  * detail on what can be done with the data.
0029  *
0030  * @see NewStuffList
0031  * @see NewStuffItem
0032  * @see NewStuffPage
0033  * @see NewStuffEntryDetails
0034  * @see NewStuffEntryComments
0035  *
0036  * \code
0037     import org.kde.newstuff 1.0 as NewStuff
0038     Item {
0039         NewStuff.ItemsModel {
0040             id: newStuffModel;
0041             engine: newStuffEngine.engine;
0042         }
0043         NewStuff.Engine {
0044             id: newStuffEngine;
0045             configFile: "/some/filesystem/location/wallpaper.knsrc";
0046             onMessage: console.log("KNS Message: " + message);
0047             onIdleMessage: console.log("KNS Idle: " + message);
0048             onBusyMessage: console.log("KNS Busy: " + message);
0049             onErrorMessage: console.log("KNS Error: " + message);
0050         }
0051     }
0052     \endcode
0053  */
0054 class ItemsModel : public QAbstractListModel
0055 {
0056     Q_OBJECT
0057     /**
0058      * The NewStuffQuickEngine to show items from
0059      */
0060     Q_PROPERTY(QObject *engine READ engine WRITE setEngine NOTIFY engineChanged)
0061     /**
0062      * Whether or not the model is fetching information from a remote location
0063      * @since 5.65
0064      */
0065     Q_PROPERTY(bool isLoadingData READ isLoadingData NOTIFY isLoadingDataChanged)
0066 public:
0067     explicit ItemsModel(QObject *parent = nullptr);
0068     ~ItemsModel() override;
0069 
0070     enum Roles {
0071         NameRole = Qt::UserRole + 1,
0072         UniqueIdRole,
0073         CategoryRole,
0074         HomepageRole,
0075         AuthorRole,
0076         LicenseRole,
0077         ShortSummaryRole,
0078         SummaryRole,
0079         ChangelogRole,
0080         VersionRole,
0081         ReleaseDateRole,
0082         UpdateVersionRole,
0083         UpdateReleaseDateRole,
0084         PayloadRole,
0085         PreviewsSmallRole, ///@< this will return a list here, rather than be tied so tightly to the remote api
0086         PreviewsRole, ///@< this will return a list here, rather than be tied so tightly to the remote api
0087         InstalledFilesRole,
0088         UnInstalledFilesRole,
0089         RatingRole,
0090         NumberOfCommentsRole,
0091         DownloadCountRole,
0092         NumberFansRole,
0093         NumberKnowledgebaseEntriesRole,
0094         KnowledgebaseLinkRole,
0095         DownloadLinksRole,
0096         DonationLinkRole,
0097         ProviderIdRole,
0098         SourceRole,
0099         StatusRole,
0100         CommentsModelRole,
0101         EntryTypeRole
0102     };
0103     Q_ENUM(Roles)
0104     enum ItemStatus {
0105         InvalidStatus,
0106         DownloadableStatus,
0107         InstalledStatus,
0108         UpdateableStatus,
0109         DeletedStatus,
0110         InstallingStatus,
0111         UpdatingStatus,
0112     };
0113     Q_ENUM(ItemStatus)
0114 
0115     /**
0116      * Represents whether the current entry is an actual catalog entry,
0117      * or an entry that represents a set of entries.
0118      * @since 5.83
0119      */
0120     enum EntryType { CatalogEntry = 0, GroupEntry };
0121 
0122     Q_ENUM(EntryType)
0123     // The lists in OCS are one-indexed, and that isn't how one usually does things in C++.
0124     // Consequently, this enum removes what would seem like magic numbers from the code, and
0125     // makes their meaning more explicit.
0126     enum LinkId {
0127         AutoDetectLinkId = -1,
0128         FirstLinkId = 1,
0129     };
0130     Q_ENUM(LinkId)
0131 
0132     QHash<int, QByteArray> roleNames() const override;
0133     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0134     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0135     bool canFetchMore(const QModelIndex &parent) const override;
0136     void fetchMore(const QModelIndex &parent) override;
0137 
0138     QObject *engine() const;
0139     void setEngine(QObject *newEngine);
0140     Q_SIGNAL void engineChanged();
0141 
0142     /**
0143      * Get the index of an entry based on that entry's unique ID
0144      * @param providerId The provider inside of which you wish to search for an entry
0145      * @param entryId The unique ID within the given provider of the entry you want to know the index of
0146      * @return The index of the entry. In case the entry is not found, -1 is returned
0147      * @see KNSCore::EntryInternal::uniqueId()
0148      * @since 5.79
0149      */
0150     Q_INVOKABLE int indexOfEntryId(const QString &providerId, const QString &entryId);
0151 
0152     /**
0153      * Whether or not the model is fetching information from a remote location
0154      * @since 5.65
0155      */
0156     bool isLoadingData() const;
0157     /**
0158      * Fired when the isLoadingData value changes
0159      * @since 5.65
0160      */
0161     Q_SIGNAL void isLoadingDataChanged();
0162 
0163     /**
0164      * @brief This will install (or update, if already installed) the item at the given index
0165      *
0166      * There are no side effects of this function if it is called on an item which cannot be
0167      * installed or updated (that is, if the status is not one such that these are possible,
0168      * the function will simply return without performing any actions)
0169      *
0170      * @param index The index of the item to install or update
0171      * @param linkId The download item to install. If this is -1, it is assumed to be an update with an unknown payload, and a number of heuristics will be
0172      * applied by the engine
0173      * @see Engine::downloadLinkLoaded implementation for details
0174      * @see LinkId
0175      */
0176     Q_INVOKABLE void installItem(int index, int linkId);
0177     /**
0178      * @brief This will request an update of the given item
0179      *
0180      * There are no side effects of this function if it is called on an item which is not
0181      * in an updateable state (that is, nothing will happen if this is called on an item
0182      * which is not already installed, or on an installed item which does not have updates
0183      * available).
0184      *
0185      * @param index The index of the item you wish to update
0186      * @since 5.69
0187      */
0188     Q_INVOKABLE void updateItem(int index);
0189     /**
0190      * @brief Uninstall an already installed item
0191      *
0192      * There are no side effects of this function if it is called on an item which cannot be
0193      * uninstalled (that is, if the status is not one such that this is possible,
0194      * the function will simply return without performing any actions)
0195      *
0196      * @param index The index of the item to be uninstalled
0197      */
0198     Q_INVOKABLE void uninstallItem(int index);
0199 
0200     /**
0201      * @brief Run the adoption command on an already installed item
0202      *
0203      * @note This will simply fail quietly if the item is not installed
0204      *
0205      * @param index The intex of the item to be adopted
0206      */
0207     Q_INVOKABLE void adoptItem(int index);
0208 
0209     /**
0210      * @brief Fired when an entry's data changes
0211      *
0212      * @param index The index of the item which has changed
0213      */
0214     Q_SIGNAL void entryChanged(int index);
0215 
0216 private:
0217     const std::unique_ptr<ItemsModelPrivate> d;
0218 };
0219 Q_DECLARE_METATYPE(ItemsModel::ItemStatus)
0220 
0221 #endif // ITEMSMODEL_H