File indexing completed on 2024-04-21 03:56:26
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 "entry.h" 0013 0014 #include <memory> 0015 0016 class ItemsModelPrivate; 0017 0018 /** 0019 * @short A model which shows the contents found in an Engine 0020 * 0021 * Use an instance of this model to show the content items represented by the configuration 0022 * file passed to an engine. The following sample assumes you are using the Engine component, 0023 * however it is also possible to pass a KNSCore::EngineBase instance created from C++ to this 0024 * property, if you have specific requirements not covered by the convenience component. 0025 * 0026 * Most data in the model is simple, but the DownloadLinks role will return a list of 0027 * DownloadLinkInfo entries, which you will need to manage in some way. 0028 * 0029 * You might also look at NewStuffList, NewStuffItem, and the other items, to see some more 0030 * detail on what can be done with the data. 0031 * 0032 * @see NewStuffList 0033 * @see NewStuffItem 0034 * @see NewStuffPage 0035 * @see NewStuffEntryDetails 0036 * @see NewStuffEntryComments 0037 * 0038 * \code 0039 import org.kde.newstuff 1.0 as NewStuff 0040 Item { 0041 NewStuff.ItemsModel { 0042 id: newStuffModel; 0043 engine: newStuffEngine 0044 } 0045 NewStuff.Engine { 0046 id: newStuffEngine 0047 configFile: "/some/filesystem/location/wallpaper.knsrc" 0048 onBusyMessageChanged: () => console.log("KNS Message: " + newStuffEngine.busyMessage); 0049 onErrorCode: (code, message, metadata) => 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 REQUIRED) 0061 public: 0062 explicit ItemsModel(QObject *parent = nullptr); 0063 ~ItemsModel() override; 0064 0065 enum Roles { 0066 NameRole = Qt::UserRole + 1, 0067 UniqueIdRole, 0068 CategoryRole, 0069 HomepageRole, 0070 AuthorRole, 0071 LicenseRole, 0072 ShortSummaryRole, 0073 SummaryRole, 0074 ChangelogRole, 0075 VersionRole, 0076 ReleaseDateRole, 0077 UpdateVersionRole, 0078 UpdateReleaseDateRole, 0079 PayloadRole, 0080 PreviewsSmallRole, ///@< this will return a list here, rather than be tied so tightly to the remote api 0081 PreviewsRole, ///@< this will return a list here, rather than be tied so tightly to the remote api 0082 InstalledFilesRole, 0083 UnInstalledFilesRole, 0084 RatingRole, 0085 NumberOfCommentsRole, 0086 DownloadCountRole, 0087 NumberFansRole, 0088 NumberKnowledgebaseEntriesRole, 0089 KnowledgebaseLinkRole, 0090 DownloadLinksRole, 0091 DonationLinkRole, 0092 ProviderIdRole, 0093 SourceRole, 0094 CommentsModelRole, 0095 EntryRole, 0096 }; 0097 Q_ENUM(Roles) 0098 0099 // The lists in OCS are one-indexed, and that isn't how one usually does things in C++. 0100 // Consequently, this enum removes what would seem like magic numbers from the code, and 0101 // makes their meaning more explicit. 0102 enum LinkId { // TODO KF6 reuse this enum in the transaction, we currently use magic numbers there 0103 AutoDetectLinkId = -1, 0104 FirstLinkId = 1, 0105 }; 0106 Q_ENUM(LinkId) 0107 0108 QHash<int, QByteArray> roleNames() const override; 0109 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0110 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0111 bool canFetchMore(const QModelIndex &parent) const override; 0112 void fetchMore(const QModelIndex &parent) override; 0113 0114 QObject *engine() const; 0115 void setEngine(QObject *newEngine); 0116 Q_SIGNAL void engineChanged(); 0117 0118 /** 0119 * Get the index of an entry based on that entry's unique ID 0120 * @param providerId The provider inside of which you wish to search for an entry 0121 * @param entryId The unique ID within the given provider of the entry you want to know the index of 0122 * @return The index of the entry. In case the entry is not found, -1 is returned 0123 * @see KNSCore::Entry::uniqueId() 0124 * @since 5.79 0125 */ 0126 Q_INVOKABLE int indexOfEntryId(const QString &providerId, const QString &entryId); 0127 Q_INVOKABLE int indexOfEntry(const KNSCore::Entry &e) 0128 { 0129 return indexOfEntryId(e.providerId(), e.uniqueId()); 0130 } 0131 0132 /** 0133 * @brief Fired when an entry's data changes 0134 * 0135 * @param index The index of the item which has changed 0136 */ 0137 Q_SIGNAL void entryChanged(const KNSCore::Entry &entry); 0138 0139 private: 0140 const std::unique_ptr<ItemsModelPrivate> d; 0141 }; 0142 0143 #endif // ITEMSMODEL_H