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 reloadUoidList();
21}
22
23void IdentityTreeModel::reloadUoidList()
24{
26 mIdentitiesUoid.clear();
27 for (const auto &identity : *mIdentityManager) {
28 mIdentitiesUoid << identity.uoid();
29 }
31}
32
33IdentityTreeModel::~IdentityTreeModel() = default;
34
35int IdentityTreeModel::columnCount(const QModelIndex &parent) const
36{
37 Q_UNUSED(parent)
38 constexpr int nbCol = static_cast<int>(IdentityRoles::LastColumn) + 1;
39 return nbCol;
40}
41
42QVariant IdentityTreeModel::data(const QModelIndex &index, int role) const
43{
44 if (!index.isValid()) {
45 return {};
46 }
47
48 const auto &identity = mIdentityManager->modifyIdentityForUoid(mIdentitiesUoid[index.row()]);
49 if (role == Qt::ToolTipRole) {
50 return identity.primaryEmailAddress();
51 }
52 if (role == Qt::FontRole) {
53 if (static_cast<IdentityRoles>(index.column()) == DisplayIdentityNameRole) {
54 if (identity.isDefault()) {
55 QFont f;
56 f.setBold(true);
57 return f;
58 }
59 }
60 }
61 if (role != Qt::DisplayRole) {
62 return {};
63 }
64 switch (static_cast<IdentityRoles>(index.column())) {
65 case FullEmailRole:
66 return identity.fullEmailAddr();
67 case EmailRole:
68 return identity.primaryEmailAddress();
69 case UoidRole:
70 return identity.uoid();
71 case IdentityNameRole:
72 return identity.identityName();
73 case DisplayIdentityNameRole:
74 return generateIdentityName(identity);
75 case DefaultRole:
76 return identity.isDefault();
77 case ActivitiesRole:
78 return identity.activities();
79 }
80
81 return {};
82}
83
84QString IdentityTreeModel::generateIdentityName(const Identity &identity) const
85{
86 QString str = identity.identityName();
87 if (mShowDefault && identity.isDefault()) {
88 str += QLatin1Char(' ') + i18nc("Default identity", " (default)");
89 }
90 return str;
91}
92
93KIdentityManagementCore::IdentityManager *IdentityTreeModel::identityManager() const
94{
95 return mIdentityManager;
96}
97
98int IdentityTreeModel::rowCount(const QModelIndex &parent) const
99{
100 Q_UNUSED(parent)
101 return mIdentitiesUoid.count();
102}
103
104void IdentityTreeModel::setShowDefault(bool show)
105{
106 mShowDefault = show;
107}
108
109uint IdentityTreeModel::identityUoid(int index) const
110{
111 return mIdentitiesUoid.at(index);
112}
113
114int IdentityTreeModel::uoidIndex(int uoid) const
115{
116 return mIdentitiesUoid.indexOf(uoid);
117}
118
119QVariant IdentityTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
120{
121 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
122 switch (static_cast<IdentityRoles>(section)) {
123 case DisplayIdentityNameRole:
124 return i18n("Identity Name");
125 case FullEmailRole:
126 case EmailRole:
127 return i18n("Email Address");
128 case UoidRole:
129 case DefaultRole:
130 case IdentityNameRole:
131 case ActivitiesRole:
132 return {};
133 }
134 }
135 return {};
136}
137
138Qt::ItemFlags IdentityTreeModel::flags(const QModelIndex &index) const
139{
140 if (!index.isValid())
141 return Qt::NoItemFlags;
142
143 if (static_cast<IdentityRoles>(index.column()) == DisplayIdentityNameRole) {
145 }
147}
148
149bool IdentityTreeModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
150{
151 if (!modelIndex.isValid()) {
152 qCWarning(KIDENTITYMANAGEMENT_LOG) << "ERROR: invalid index";
153 return false;
154 }
155 if (role != Qt::EditRole) {
156 return false;
157 }
158 const int idx = modelIndex.row();
159 auto &identity = mIdentityManager->modifyIdentityForUoid(mIdentitiesUoid[idx]);
160 switch (static_cast<IdentityRoles>(modelIndex.column())) {
161 case DisplayIdentityNameRole: {
162 const QModelIndex newIndex = index(modelIndex.row(), DisplayIdentityNameRole);
163 Q_EMIT dataChanged(newIndex, newIndex);
164 identity.setIdentityName(value.toString());
165 // TODO save ???
166 return true;
167 }
168 default:
169 break;
170 }
171 return false;
172}
173
174#include "moc_identitytreemodel.cpp"
Manages the list of identities.
Identity & modifyIdentityForUoid(uint uoid)
User identity information.
Definition identity.h:73
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
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)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:55:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.