KUnifiedPush

clientmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "clientmodel.h"
7
8#include <KService>
9
10ClientModel::ClientModel(org::kde::kunifiedpush::Management *iface, QObject *parent)
11 : QAbstractListModel(parent)
12 , m_iface(iface)
13{
14 m_clients = iface->registeredClients();
15 connect(iface, &org::kde::kunifiedpush::Management::registeredClientsChanged, this, &ClientModel::reload);
16}
17
18ClientModel::~ClientModel() = default;
19
20int ClientModel::rowCount(const QModelIndex &parent) const
21{
22 if (parent.isValid()) {
23 return 0;
24 }
25 return m_clients.size();
26}
27
28QVariant ClientModel::data(const QModelIndex &index, int role) const
29{
30 if (!checkIndex(index)) {
31 return {};
32 }
33
34 const auto &c = m_clients.at(index.row());
35 const auto service = KService::serviceByDesktopName(c.serviceName);
36 switch (role) {
37 case NameRole:
38 return service ? service->name() : c.serviceName;
39 case DescriptionRole:
40 if (!c.description.isEmpty()) {
41 return c.description;
42 }
43 if (service) {
44 return service->comment().isEmpty() ? service->genericName() : service->comment();
45 }
46 return {};
47 case IconNameRole:
48 return service ? service->icon() : QStringLiteral("application-x-executable");
49 case TokenRole:
50 return c.token;
51 }
52
53 return {};
54}
55
56QHash<int, QByteArray> ClientModel::roleNames() const
57{
59 n.insert(NameRole, "name");
60 n.insert(DescriptionRole, "description");
61 n.insert(IconNameRole, "iconName");
62 n.insert(TokenRole, "token");
63 return n;
64}
65
66void ClientModel::reload()
67{
69 m_clients = m_iface->registeredClients();
71}
72
73#include "moc_clientmodel.cpp"
static Ptr serviceByDesktopName(const QString &_name)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QHash< int, QByteArray > roleNames() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
const_reference at(qsizetype i) const const
qsizetype size() const const
int row() const const
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 10 2024 11:51:21 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.