KNewStuff

providersmodel.cpp
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{
15 ProvidersModel *const q;
16
17public:
18 explicit ProvidersModelPrivate(ProvidersModel *qq)
19 : q(qq)
20 {
21 }
22
23 EngineBase *getEngine() const;
24 void setEngine(EngineBase *engine);
25
26 EngineBase *engine = nullptr;
27 QStringList knownProviders;
28};
29
30EngineBase *ProvidersModelPrivate::getEngine() const
31{
32 return engine;
33}
34
35void ProvidersModelPrivate::setEngine(EngineBase *engine)
36{
37 q->setEngine(engine);
38}
39
41 : QAbstractListModel(parent)
42 , d(new ProvidersModelPrivate(this))
43{
44}
45
46ProvidersModel::~ProvidersModel() = default;
47
48QHash<int, QByteArray> KNSCore::ProvidersModel::roleNames() const
49{
50 static const QHash<int, QByteArray> roles{
51 {IdRole, "id"},
52 {NameRole, "name"},
53 {VersionRole, "version"},
54 {WebsiteRole, "website"},
55 {HostRole, "host"},
56 {ContactEmailRole, "contactEmail"},
57 {SupportsSslRole, "supportsSsl"},
58 {IconRole, "icon"},
59 {ObjectRole, "object"},
60 };
61 return roles;
62}
63
64int KNSCore::ProvidersModel::rowCount(const QModelIndex &parent) const
65{
66 if (parent.isValid()) {
67 return 0;
68 }
69 return d->knownProviders.count();
70}
71
72QVariant KNSCore::ProvidersModel::data(const QModelIndex &index, int role) const
73{
74 if (checkIndex(index) && d->engine) {
75 QSharedPointer<Provider> provider = d->engine->provider(d->knownProviders.value(index.row()));
76 if (provider) {
77 switch (role) {
78 case IdRole:
79 return provider->id();
80 case NameRole:
81 return provider->name();
82 case VersionRole:
83 return provider->version();
84 case WebsiteRole:
85 return provider->website();
86 case HostRole:
87 return provider->host();
88 case ContactEmailRole:
89 return provider->contactEmail();
90 case SupportsSslRole:
91 return provider->supportsSsl();
92 case IconRole:
93 return provider->icon();
94 case ObjectRole:
95 return QVariant::fromValue(provider.data());
96 }
97 }
98 }
99 return QVariant();
100}
101
102QObject *KNSCore::ProvidersModel::engine() const
103{
104 return d->engine;
105}
106
107void KNSCore::ProvidersModel::setEngine(QObject *engine)
108{
109 if (d->engine != engine) {
110 if (d->engine) {
111 d->engine->disconnect(this);
112 }
113 d->engine = qobject_cast<EngineBase *>(engine);
114 Q_EMIT engineChanged();
115 if (d->engine) {
116 connect(d->engine, &EngineBase::providersChanged, this, [this]() {
117 beginResetModel();
118 d->knownProviders = d->engine->providerIDs();
119 endResetModel();
120 });
121 beginResetModel();
122 d->knownProviders = d->engine->providerIDs();
123 endResetModel();
124 }
125 }
126}
127
128}
129
130#include "moc_providersmodel.cpp"
void providersChanged()
Fired whenever the list of providers changes.
ProvidersModel(QObject *parent=nullptr)
The Engine for which this model displays Providers.
bool isValid() const const
int row() const const
T * data() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:13:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.