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

source code of knewstuff/src/qtquick/quickitemsmodel.h