1 | /* |
2 | SPDX-FileCopyrightText: 2019 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 | #include "categoriesmodel.h" |
8 | |
9 | #include <KLocalizedString> |
10 | |
11 | CategoriesModel::CategoriesModel(KNSCore::EngineBase *parent) |
12 | : QAbstractListModel(parent) |
13 | , m_engine(parent) |
14 | { |
15 | connect(sender: m_engine, signal: &KNSCore::EngineBase::signalCategoriesMetadataLoded, context: this, slot: [this]() { |
16 | beginResetModel(); |
17 | endResetModel(); |
18 | }); |
19 | } |
20 | |
21 | CategoriesModel::~CategoriesModel() = default; |
22 | |
23 | QHash<int, QByteArray> CategoriesModel::roleNames() const |
24 | { |
25 | static const QHash<int, QByteArray> roles{{NameRole, "name" }, {IdRole, "id" }, {DisplayNameRole, "displayName" }}; |
26 | return roles; |
27 | } |
28 | |
29 | int CategoriesModel::rowCount(const QModelIndex &parent) const |
30 | { |
31 | if (parent.isValid()) { |
32 | return 0; |
33 | } |
34 | return m_engine->categoriesMetadata().count() + 1; |
35 | } |
36 | |
37 | QVariant CategoriesModel::data(const QModelIndex &index, int role) const |
38 | { |
39 | if (!index.isValid()) { |
40 | return QVariant(); |
41 | } |
42 | const QList<KNSCore::Provider::CategoryMetadata> categoriesMetadata = m_engine->categoriesMetadata(); |
43 | if (index.row() == 0) { |
44 | switch (role) { |
45 | case NameRole: |
46 | return QString(); |
47 | case IdRole: |
48 | return 0; |
49 | case DisplayNameRole: |
50 | return i18nc("The first entry in the category selection list (also the default value)" , "All Categories" ); |
51 | default: |
52 | return QStringLiteral("Unknown role" ); |
53 | } |
54 | } else if (index.row() <= categoriesMetadata.count()) { |
55 | const KNSCore::Provider::CategoryMetadata category = categoriesMetadata[index.row() - 1]; |
56 | switch (role) { |
57 | case NameRole: |
58 | return category.name; |
59 | case IdRole: |
60 | return category.id; |
61 | case DisplayNameRole: |
62 | return category.displayName; |
63 | default: |
64 | return QStringLiteral("Unknown role" ); |
65 | } |
66 | } |
67 | return QVariant(); |
68 | } |
69 | |
70 | QString CategoriesModel::idToDisplayName(const QString &id) const // TODO KF6 unused? |
71 | { |
72 | QString dispName = i18nc("The string passed back in the case the requested category is not known" , "Unknown Category" ); |
73 | const auto metaData = m_engine->categoriesMetadata(); |
74 | for (const KNSCore::Provider::CategoryMetadata &cat : metaData) { |
75 | if (cat.id == id) { |
76 | dispName = cat.displayName; |
77 | break; |
78 | } |
79 | } |
80 | return dispName; |
81 | } |
82 | |
83 | #include "moc_categoriesmodel.cpp" |
84 | |