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{
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(d->knownProviders.value(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(provider.data());
76 }
77 }
78 }
79 return QVariant();
80}
81
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(this);
92 }
93 d->engine = qobject_cast<EngineBase *>(engine);
94 Q_EMIT engineChanged();
95 if (d->engine) {
96 connect(d->engine, &EngineBase::providersChanged, this, [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"
void providersChanged()
Fired whenever the list of providers changes.
QObject * engine
The Engine for which this model displays Providers.
@ ObjectRole
The actual Provider object. Do not hold this locally and expect it to disappear at a moment's notice.
bool isValid() const const
int row() const const
T * data() const const
QVariant fromValue(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:48:13 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.