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 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 Q_UNUSED(parent)
105 return mIdentitiesUoid.count();
106}
107
108void IdentityTreeModel::setShowDefault(bool show)
109{
110 mShowDefault = show;
111}
112
113uint IdentityTreeModel::identityUoid(int index) const
114{
115 return mIdentitiesUoid.at(index);
116}
117
118int IdentityTreeModel::uoidIndex(int uoid) const
119{
120 return mIdentitiesUoid.indexOf(uoid);
121}
122
123QVariant IdentityTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
124{
125 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
126 switch (static_cast<IdentityRoles>(section)) {
127 case DisplayIdentityNameRole:
128 return i18n("Identity Name");
129 case FullEmailRole:
130 case EmailRole:
131 return i18n("Email Address");
132 case UoidRole:
133 case DefaultRole:
134 case IdentityNameRole:
135 case ActivitiesRole:
136 case EnabledActivitiesRole:
137 return {};
138 }
139 }
140 return {};
141}
142
143Qt::ItemFlags IdentityTreeModel::flags(const QModelIndex &index) const
144{
145 if (!index.isValid())
146 return Qt::NoItemFlags;
147
148 if (static_cast<IdentityRoles>(index.column()) == DisplayIdentityNameRole) {
150 }
152}
153
154bool IdentityTreeModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
155{
156 if (!modelIndex.isValid()) {
157 qCWarning(KIDENTITYMANAGEMENT_LOG) << "ERROR: invalid index";
158 return false;
159 }
160 if (role != Qt::EditRole) {
161 return false;
162 }
163 const int idx = modelIndex.row();
164 auto &identity = mIdentityManager->modifyIdentityForUoid(mIdentitiesUoid[idx]);
165 switch (static_cast<IdentityRoles>(modelIndex.column())) {
166 case IdentityNameRole: {
167 const QModelIndex newIndex = index(modelIndex.row(), IdentityNameRole);
168 Q_EMIT dataChanged(newIndex, newIndex);
169 identity.setIdentityName(value.toString());
170 mIdentityManager->saveIdentity(identity);
171 return true;
172 }
173 case DefaultRole: {
174 const QModelIndex newIndex = index(modelIndex.row(), UoidRole);
175 mIdentityManager->setAsDefault(newIndex.data().toInt());
176 Q_EMIT dataChanged(modelIndex, modelIndex);
177 return true;
178 }
179 default:
180 break;
181 }
182 return false;
183}
184
185void IdentityTreeModel::removeIdentities(const QStringList &identitiesName)
186{
187 for (const QString &name : identitiesName) {
188 mIdentityManager->removeIdentity(name);
189 }
190}
191
192#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:74
void setIdentityName(const QString &name)
Identity/nickname for this collection.
Definition identity.cpp:526
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 override
void setBold(bool enable)
const_reference at(qsizetype i) const const
void clear()
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
int column() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() 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-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.