KIdentityManagement

identitytreemodel.cpp
1// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
2// SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
3// SPDX-FileCopyrightText: 2024-2025 Laurent Montel <montel@kde.org>
4// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5
6#include "identitytreemodel.h"
7#include "kidentitymanagementcore_debug.h"
8#include <KLocalizedString>
9#include <QFont>
10
11#include "identity.h"
12
13using namespace KIdentityManagementCore;
14IdentityTreeModel::IdentityTreeModel(IdentityManager *manager, QObject *parent)
15 : QAbstractListModel(parent)
16 , mIdentityManager(manager)
17{
18 connect(mIdentityManager, &IdentityManager::needToReloadIdentitySettings, this, &IdentityTreeModel::reloadUoidList);
19 connect(mIdentityManager, &IdentityManager::identitiesWereChanged, this, &IdentityTreeModel::reloadUoidList);
20 connect(mIdentityManager, &IdentityManager::deleted, this, &IdentityTreeModel::reloadUoidList);
21 connect(mIdentityManager, &IdentityManager::added, this, &IdentityTreeModel::reloadUoidList);
22 reloadUoidList();
23}
24
25void IdentityTreeModel::reloadUoidList()
26{
28 mIdentitiesUoid.clear();
29 for (const auto &identity : *mIdentityManager) {
30 mIdentitiesUoid << identity.uoid();
31 }
33}
34
35IdentityTreeModel::~IdentityTreeModel() = default;
36
37int IdentityTreeModel::columnCount(const QModelIndex &parent) const
38{
39 Q_UNUSED(parent)
40 constexpr int nbCol = static_cast<int>(IdentityRoles::LastColumn) + 1;
41 return nbCol;
42}
43
44QVariant IdentityTreeModel::data(const QModelIndex &index, int role) const
45{
46 if (!index.isValid()) {
47 return {};
48 }
49
50 const auto &identity = mIdentityManager->modifyIdentityForUoid(mIdentitiesUoid[index.row()]);
51 if (role == Qt::ToolTipRole) {
52 return identity.primaryEmailAddress();
53 }
54 if (role == Qt::FontRole) {
55 if (static_cast<IdentityRoles>(index.column()) == DisplayIdentityNameRole) {
56 if (identity.isDefault()) {
57 QFont f;
58 f.setBold(true);
59 return f;
60 }
61 }
62 }
63 if (role != Qt::DisplayRole) {
64 return {};
65 }
66 switch (static_cast<IdentityRoles>(index.column())) {
67 case FullEmailRole:
68 return identity.fullEmailAddr();
69 case EmailRole:
70 return identity.primaryEmailAddress();
71 case UoidRole:
72 return identity.uoid();
73 case IdentityNameRole:
74 return identity.identityName();
75 case DisplayIdentityNameRole:
76 return generateIdentityName(identity);
77 case DefaultRole:
78 return identity.isDefault();
79 case ActivitiesRole:
80 return identity.activities();
81 case EnabledActivitiesRole:
82 return identity.enabledActivities();
83 }
84
85 return {};
86}
87
88QString IdentityTreeModel::generateIdentityName(const Identity &identity) const
89{
90 QString str = identity.identityName();
91 if (mShowDefault && identity.isDefault()) {
92 str += QLatin1Char(' ') + i18nc("Default identity", " (default)");
93 }
94 return str;
95}
96
97KIdentityManagementCore::IdentityManager *IdentityTreeModel::identityManager() const
98{
99 return mIdentityManager;
100}
101
102int IdentityTreeModel::rowCount(const QModelIndex &parent) const
103{
104 if (parent.isValid()) // flat model
105 return 0;
106 return mIdentitiesUoid.count();
107}
108
109void IdentityTreeModel::setShowDefault(bool show)
110{
111 mShowDefault = show;
112}
113
114uint IdentityTreeModel::identityUoid(int index) const
115{
116 return mIdentitiesUoid.at(index);
117}
118
119int IdentityTreeModel::uoidIndex(int uoid) const
120{
121 return mIdentitiesUoid.indexOf(uoid);
122}
123
124QVariant IdentityTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
125{
126 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
127 switch (static_cast<IdentityRoles>(section)) {
128 case DisplayIdentityNameRole:
129 return i18n("Identity Name");
130 case FullEmailRole:
131 case EmailRole:
132 return i18n("Email Address");
133 case UoidRole:
134 case DefaultRole:
135 case IdentityNameRole:
136 case ActivitiesRole:
137 case EnabledActivitiesRole:
138 return {};
139 }
140 }
141 return {};
142}
143
144Qt::ItemFlags IdentityTreeModel::flags(const QModelIndex &index) const
145{
146 if (!index.isValid())
147 return Qt::NoItemFlags;
148
149 if (static_cast<IdentityRoles>(index.column()) == DisplayIdentityNameRole) {
151 }
153}
154
155bool IdentityTreeModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
156{
157 if (!modelIndex.isValid()) {
158 qCWarning(KIDENTITYMANAGEMENT_LOG) << "ERROR: invalid index";
159 return false;
160 }
161 if (role != Qt::EditRole) {
162 return false;
163 }
164 const int idx = modelIndex.row();
165 auto &identity = mIdentityManager->modifyIdentityForUoid(mIdentitiesUoid[idx]);
166 switch (static_cast<IdentityRoles>(modelIndex.column())) {
167 case IdentityNameRole: {
168 const QModelIndex newIndex = index(modelIndex.row(), IdentityNameRole);
169 Q_EMIT dataChanged(newIndex, newIndex);
170 identity.setIdentityName(value.toString());
171 mIdentityManager->saveIdentity(identity);
172 return true;
173 }
174 case DefaultRole: {
175 const QModelIndex newIndex = index(modelIndex.row(), UoidRole);
176 mIdentityManager->setAsDefault(newIndex.data().toInt());
177 Q_EMIT dataChanged(modelIndex, modelIndex);
178 return true;
179 }
180 default:
181 break;
182 }
183 return false;
184}
185
186void IdentityTreeModel::removeIdentities(const QStringList &identitiesName)
187{
188 for (const QString &name : identitiesName) {
189 mIdentityManager->removeIdentity(name);
190 }
191}
192
193#include "moc_identitytreemodel.cpp"
Manages the list of identities.
void added(const KIdentityManagementCore::Identity &ident)
Emitted on commit() for each new identity.
void deleted(uint uoid)
Emitted on commit() for each deleted identity.
User identity information.
Definition identity.h:76
void setIdentityName(const QString &name)
Identity/nickname for this collection.
Definition identity.cpp:540
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setBold(bool enable)
int column() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
qsizetype count() const const
ToolTipRole
typedef ItemFlags
Orientation
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:59:06 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.