1/*
2 SPDX-FileCopyrightText: 2021 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 "providersmodel.h"
8
9#include "enginebase.h"
10
11namespace KNSCore
12{
13class ProvidersModelPrivate
14{
15public:
16 EngineBase *engine = nullptr;
17 QStringList knownProviders;
18};
19
20ProvidersModel::ProvidersModel(QObject *parent)
21 : QAbstractListModel(parent)
22 , d(new ProvidersModelPrivate)
23{
24}
25
26ProvidersModel::~ProvidersModel() = default;
27
28QHash<int, QByteArray> KNSCore::ProvidersModel::roleNames() const
29{
30 static const QHash<int, QByteArray> roles{
31 {IdRole, "id"},
32 {NameRole, "name"},
33 {VersionRole, "version"},
34 {WebsiteRole, "website"},
35 {HostRole, "host"},
36 {ContactEmailRole, "contactEmail"},
37 {SupportsSslRole, "supportsSsl"},
38 {IconRole, "icon"},
39 {ObjectRole, "object"},
40 };
41 return roles;
42}
43
44int KNSCore::ProvidersModel::rowCount(const QModelIndex &parent) const
45{
46 if (parent.isValid()) {
47 return 0;
48 }
49 return d->knownProviders.count();
50}
51
52QVariant KNSCore::ProvidersModel::data(const QModelIndex &index, int role) const
53{
54 if (checkIndex(index) && d->engine) {
55 QSharedPointer<Provider> provider = d->engine->provider(providerId: d->knownProviders.value(i: index.row()));
56 if (provider) {
57 switch (role) {
58 case IdRole:
59 return provider->id();
60 case NameRole:
61 return provider->name();
62 case VersionRole:
63 return provider->version();
64 case WebsiteRole:
65 return provider->website();
66 case HostRole:
67 return provider->host();
68 case ContactEmailRole:
69 return provider->contactEmail();
70 case SupportsSslRole:
71 return provider->supportsSsl();
72 case IconRole:
73 return provider->icon();
74 case ObjectRole:
75 return QVariant::fromValue(value: provider.data());
76 }
77 }
78 }
79 return QVariant();
80}
81
82QObject *KNSCore::ProvidersModel::engine() const
83{
84 return d->engine;
85}
86
87void KNSCore::ProvidersModel::setEngine(QObject *engine)
88{
89 if (d->engine != engine) {
90 if (d->engine) {
91 d->engine->disconnect(receiver: this);
92 }
93 d->engine = qobject_cast<EngineBase *>(object: engine);
94 Q_EMIT engineChanged();
95 if (d->engine) {
96 connect(sender: d->engine, signal: &EngineBase::providersChanged, context: this, slot: [this]() {
97 beginResetModel();
98 d->knownProviders = d->engine->providerIDs();
99 endResetModel();
100 });
101 beginResetModel();
102 d->knownProviders = d->engine->providerIDs();
103 endResetModel();
104 }
105 }
106}
107
108}
109
110#include "moc_providersmodel.cpp"
111

source code of knewstuff/src/core/providersmodel.cpp