1 | /* |
2 | SPDX-FileCopyrightText: 2021 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "searchpresetmodel.h" |
8 | |
9 | #include "knewstuffquick_debug.h" |
10 | #include <KLocalizedString> |
11 | |
12 | SearchPresetModel::SearchPresetModel(KNSCore::EngineBase *engine) |
13 | : QAbstractListModel(engine) |
14 | , m_engine(engine) |
15 | { |
16 | connect(sender: m_engine, signal: &KNSCore::EngineBase::signalSearchPresetsLoaded, context: this, slot: [this]() { |
17 | beginResetModel(); |
18 | endResetModel(); |
19 | }); |
20 | } |
21 | |
22 | SearchPresetModel::~SearchPresetModel() = default; |
23 | |
24 | QHash<int, QByteArray> SearchPresetModel::roleNames() const |
25 | { |
26 | static const QHash<int, QByteArray> roles{{DisplayNameRole, "displayName" }, {IconRole, "iconName" }}; |
27 | return roles; |
28 | } |
29 | |
30 | QVariant SearchPresetModel::data(const QModelIndex &index, int role) const |
31 | { |
32 | if (index.isValid() && checkIndex(index)) { |
33 | const QList<KNSCore::Provider::SearchPreset> presets = m_engine->searchPresets(); |
34 | const KNSCore::Provider::SearchPreset preset = presets[index.row()]; |
35 | |
36 | if (role == DisplayNameRole) { |
37 | if (QString name = preset.displayName; !name.isEmpty()) { |
38 | return name; |
39 | } |
40 | |
41 | switch (preset.type) { |
42 | case KNSCore::Provider::SearchPresetTypes::GoBack: |
43 | return i18nc("Knewstuff5" , "Back" ); |
44 | case KNSCore::Provider::SearchPresetTypes::Popular: |
45 | return i18nc("Knewstuff5" , "Popular" ); |
46 | case KNSCore::Provider::SearchPresetTypes::Featured: |
47 | return i18nc("Knewstuff5" , "Featured" ); |
48 | case KNSCore::Provider::SearchPresetTypes::Start: |
49 | return i18nc("Knewstuff5" , "Restart" ); |
50 | case KNSCore::Provider::SearchPresetTypes::New: |
51 | return i18nc("Knewstuff5" , "New" ); |
52 | case KNSCore::Provider::SearchPresetTypes::Root: |
53 | return i18nc("Knewstuff5" , "Home" ); |
54 | case KNSCore::Provider::SearchPresetTypes::Shelf: |
55 | return i18nc("Knewstuff5" , "Shelf" ); |
56 | case KNSCore::Provider::SearchPresetTypes::FolderUp: |
57 | return i18nc("Knewstuff5" , "Up" ); |
58 | case KNSCore::Provider::SearchPresetTypes::Recommended: |
59 | return i18nc("Knewstuff5" , "Recommended" ); |
60 | case KNSCore::Provider::SearchPresetTypes::Subscription: |
61 | return i18nc("Knewstuff5" , "Subscriptions" ); |
62 | case KNSCore::Provider::SearchPresetTypes::AllEntries: |
63 | return i18nc("Knewstuff5" , "All Entries" ); |
64 | default: |
65 | return i18nc("Knewstuff5" , "Search Preset: %1" , preset.request.searchTerm); |
66 | } |
67 | } else if (role == IconRole) { |
68 | if (QString name = preset.iconName; !name.isEmpty()) { |
69 | return name; |
70 | } |
71 | |
72 | switch (preset.type) { |
73 | case KNSCore::Provider::SearchPresetTypes::GoBack: |
74 | return QStringLiteral("arrow-left" ); |
75 | case KNSCore::Provider::SearchPresetTypes::Popular: |
76 | case KNSCore::Provider::SearchPresetTypes::Featured: |
77 | case KNSCore::Provider::SearchPresetTypes::Recommended: |
78 | return QStringLiteral("rating" ); |
79 | case KNSCore::Provider::SearchPresetTypes::New: |
80 | return QStringLiteral("change-date-symbolic" ); |
81 | case KNSCore::Provider::SearchPresetTypes::Start: |
82 | return QStringLiteral("start-over" ); |
83 | case KNSCore::Provider::SearchPresetTypes::Root: |
84 | return QStringLiteral("go-home" ); |
85 | case KNSCore::Provider::SearchPresetTypes::Shelf: |
86 | case KNSCore::Provider::SearchPresetTypes::Subscription: |
87 | return QStringLiteral("bookmark" ); |
88 | case KNSCore::Provider::SearchPresetTypes::FolderUp: |
89 | return QStringLiteral("arrow-up" ); |
90 | default: |
91 | return QStringLiteral("search" ); |
92 | } |
93 | } |
94 | } |
95 | return QVariant(); |
96 | } |
97 | |
98 | int SearchPresetModel::rowCount(const QModelIndex &parent) const |
99 | { |
100 | if (parent.isValid()) { |
101 | return 0; |
102 | } |
103 | return m_engine->searchPresets().count(); |
104 | } |
105 | |
106 | void SearchPresetModel::loadSearch(const QModelIndex &index) |
107 | { |
108 | if (index.row() >= rowCount() || !index.isValid()) { |
109 | qCWarning(KNEWSTUFFQUICK) << "index SearchPresetModel::loadSearch invalid" << index; |
110 | return; |
111 | } |
112 | const KNSCore::Provider::SearchPreset preset = m_engine->searchPresets().at(i: index.row()); |
113 | m_engine->search(request: preset.request); |
114 | } |
115 | |
116 | #include "moc_searchpresetmodel.cpp" |
117 | |