1 | /* |
2 | SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #ifndef ITEMSMODEL_H |
8 | #define ITEMSMODEL_H |
9 | |
10 | #include <QAbstractListModel> |
11 | |
12 | #include "entry.h" |
13 | #include "quickengine.h" |
14 | |
15 | #include <memory> |
16 | |
17 | class ItemsModelPrivate; |
18 | |
19 | /** |
20 | * @short A model which shows the contents found in an Engine |
21 | * |
22 | * Use an instance of this model to show the content items represented by the configuration |
23 | * file passed to an engine. The following sample assumes you are using the Engine component, |
24 | * however it is also possible to pass a KNSCore::EngineBase instance created from C++ to this |
25 | * property, if you have specific requirements not covered by the convenience component. |
26 | * |
27 | * Most data in the model is simple, but the DownloadLinks role will return a list of |
28 | * DownloadLinkInfo entries, which you will need to manage in some way. |
29 | * |
30 | * You might also look at NewStuffList, NewStuffItem, and the other items, to see some more |
31 | * detail on what can be done with the data. |
32 | * |
33 | * @see NewStuffList |
34 | * @see NewStuffItem |
35 | * @see NewStuffPage |
36 | * @see NewStuffEntryDetails |
37 | * @see NewStuffEntryComments |
38 | * |
39 | * \code |
40 | import org.kde.newstuff as NewStuff |
41 | Item { |
42 | NewStuff.ItemsModel { |
43 | id: newStuffModel |
44 | engine: newStuffEngine |
45 | } |
46 | NewStuff.Engine { |
47 | id: newStuffEngine |
48 | configFile: "/some/filesystem/location/wallpaper.knsrc" |
49 | onBusyMessageChanged: () => console.log("KNS Message: " + newStuffEngine.busyMessage) |
50 | onErrorCode: (code, message, metadata) => console.log("KNS Error: " + message) |
51 | } |
52 | } |
53 | \endcode |
54 | */ |
55 | class ItemsModel : public QAbstractListModel |
56 | { |
57 | Q_OBJECT |
58 | /** |
59 | * The NewStuffQuickEngine to show items from |
60 | */ |
61 | Q_PROPERTY(Engine *engine READ engine WRITE setEngine NOTIFY engineChanged REQUIRED) |
62 | public: |
63 | explicit ItemsModel(QObject *parent = nullptr); |
64 | ~ItemsModel() override; |
65 | |
66 | enum Roles { |
67 | NameRole = Qt::UserRole + 1, |
68 | UniqueIdRole, |
69 | CategoryRole, |
70 | , |
71 | AuthorRole, |
72 | LicenseRole, |
73 | ShortSummaryRole, |
74 | SummaryRole, |
75 | ChangelogRole, |
76 | VersionRole, |
77 | ReleaseDateRole, |
78 | UpdateVersionRole, |
79 | UpdateReleaseDateRole, |
80 | PayloadRole, |
81 | PreviewsSmallRole, ///@< this will return a list here, rather than be tied so tightly to the remote api |
82 | PreviewsRole, ///@< this will return a list here, rather than be tied so tightly to the remote api |
83 | InstalledFilesRole, |
84 | UnInstalledFilesRole, |
85 | RatingRole, |
86 | , |
87 | DownloadCountRole, |
88 | NumberFansRole, |
89 | NumberKnowledgebaseEntriesRole, |
90 | KnowledgebaseLinkRole, |
91 | DownloadLinksRole, |
92 | DonationLinkRole, |
93 | ProviderIdRole, |
94 | SourceRole, |
95 | , |
96 | EntryRole, |
97 | }; |
98 | Q_ENUM(Roles) |
99 | |
100 | // The lists in OCS are one-indexed, and that isn't how one usually does things in C++. |
101 | // Consequently, this enum removes what would seem like magic numbers from the code, and |
102 | // makes their meaning more explicit. |
103 | enum LinkId { // TODO KF6 reuse this enum in the transaction, we currently use magic numbers there |
104 | AutoDetectLinkId = -1, |
105 | FirstLinkId = 1, |
106 | }; |
107 | Q_ENUM(LinkId) |
108 | |
109 | QHash<int, QByteArray> roleNames() const override; |
110 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
111 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
112 | bool canFetchMore(const QModelIndex &parent) const override; |
113 | void fetchMore(const QModelIndex &parent) override; |
114 | |
115 | Engine *engine() const; |
116 | void setEngine(Engine *newEngine); |
117 | Q_SIGNAL void engineChanged(); |
118 | |
119 | /** |
120 | * Get the index of an entry based on that entry's unique ID |
121 | * @param providerId The provider inside of which you wish to search for an entry |
122 | * @param entryId The unique ID within the given provider of the entry you want to know the index of |
123 | * @return The index of the entry. In case the entry is not found, -1 is returned |
124 | * @see KNSCore::Entry::uniqueId() |
125 | * @since 5.79 |
126 | */ |
127 | Q_INVOKABLE int indexOfEntryId(const QString &providerId, const QString &entryId); |
128 | Q_INVOKABLE int indexOfEntry(const KNSCore::Entry &e) |
129 | { |
130 | return indexOfEntryId(providerId: e.providerId(), entryId: e.uniqueId()); |
131 | } |
132 | |
133 | /** |
134 | * @brief Fired when an entry's data changes |
135 | * |
136 | * @param entry The entry which has changed |
137 | */ |
138 | Q_SIGNAL void entryChanged(const KNSCore::Entry &entry); |
139 | |
140 | private: |
141 | const std::unique_ptr<ItemsModelPrivate> d; |
142 | }; |
143 | |
144 | #endif // ITEMSMODEL_H |
145 | |