KIdentityManagement

identitymodel.cpp
1// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
2// SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
3// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
4
5#include "identitymodel.h"
6
7#include <KLocalizedString>
8
9#include "identity.h"
10
11namespace KIdentityManagementCore
12{
13
14IdentityModel::IdentityModel(QObject *parent)
15 : QAbstractListModel(parent)
16 , m_identityManager(IdentityManager::self())
17{
18 connect(m_identityManager, &IdentityManager::needToReloadIdentitySettings, this, &IdentityModel::reloadUoidList);
19 connect(m_identityManager, &IdentityManager::identitiesWereChanged, this, &IdentityModel::reloadUoidList);
20 reloadUoidList();
21}
22
23void IdentityModel::reloadUoidList()
24{
25 beginResetModel();
26 m_identitiesUoid.clear();
27 for (const auto &identity : *m_identityManager) {
28 m_identitiesUoid << identity.uoid();
29 }
30 endResetModel();
31}
32
33IdentityModel::~IdentityModel() = default;
34
35QVariant IdentityModel::data(const QModelIndex &index, int role) const
36{
37 if (!index.isValid()) {
38 return {};
39 }
40 const auto &identity = m_identityManager->modifyIdentityForUoid(m_identitiesUoid[index.row()]);
41 switch (role) {
42 case Qt::DisplayRole:
43 case DisplayNameRole:
44 return QString(identity.identityName() + i18nc("Separator between identity name and email address", " - ") + identity.fullEmailAddr());
45 case EmailRole:
46 return identity.primaryEmailAddress();
47 case UoidRole:
48 return identity.uoid();
49 case IdentityNameRole:
50 return identity.identityName();
51 case DefaultRole:
52 return identity.isDefault();
53 case Qt::ToolTipRole:
54 return identity.primaryEmailAddress();
55 }
56
57 return {};
58}
59
60int IdentityModel::rowCount(const QModelIndex &parent) const
61{
62 if (parent.isValid()) {
63 return 0;
64 }
65
66 return m_identitiesUoid.count();
67}
68
69QString IdentityModel::email(uint uoid)
70{
71 return m_identityManager->identityForUoid(uoid).primaryEmailAddress();
72}
73
74QHash<int, QByteArray> IdentityModel::roleNames() const
75{
76 auto roles = QAbstractListModel::roleNames();
77 roles.insert({
78 {UoidRole, QByteArrayLiteral("uoid")},
79 {EmailRole, QByteArrayLiteral("email")},
80 {IdentityNameRole, QByteArrayLiteral("identityName")},
81 {DefaultRole, QByteArrayLiteral("isDefault")},
82 });
83 return roles;
84}
85
86}
87
88#include "moc_identitymodel.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
virtual QHash< int, QByteArray > roleNames() const const
bool isValid() const const
int row() const const
DisplayRole
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 Sat Apr 27 2024 22:14:39 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.