File indexing completed on 2024-05-19 05:17:27

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
0003 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0004 
0005 #include "identitymodel.h"
0006 
0007 #include <KLocalizedString>
0008 
0009 #include "identity.h"
0010 
0011 namespace KIdentityManagementCore
0012 {
0013 
0014 IdentityModel::IdentityModel(QObject *parent)
0015     : QAbstractListModel(parent)
0016     , m_identityManager(IdentityManager::self())
0017 {
0018     connect(m_identityManager, &IdentityManager::needToReloadIdentitySettings, this, &IdentityModel::reloadUoidList);
0019     connect(m_identityManager, &IdentityManager::identitiesWereChanged, this, &IdentityModel::reloadUoidList);
0020     reloadUoidList();
0021 }
0022 
0023 void IdentityModel::reloadUoidList()
0024 {
0025     beginResetModel();
0026     m_identitiesUoid.clear();
0027     for (const auto &identity : *m_identityManager) {
0028         m_identitiesUoid << identity.uoid();
0029     }
0030     endResetModel();
0031 }
0032 
0033 IdentityModel::~IdentityModel()
0034 {
0035 }
0036 
0037 QVariant IdentityModel::data(const QModelIndex &index, int role) const
0038 {
0039     if (!index.isValid()) {
0040         return {};
0041     }
0042     const auto &identity = m_identityManager->modifyIdentityForUoid(m_identitiesUoid[index.row()]);
0043     switch (role) {
0044     case Qt::DisplayRole:
0045     case DisplayNameRole:
0046         return QString(identity.identityName() + i18nc("Separator between identity name and email address", " - ") + identity.fullEmailAddr());
0047     case EmailRole:
0048         return identity.primaryEmailAddress();
0049     case UoidRole:
0050         return identity.uoid();
0051     case IdentityNameRole:
0052         return identity.identityName();
0053     }
0054 
0055     return {};
0056 }
0057 
0058 int IdentityModel::rowCount(const QModelIndex &parent) const
0059 {
0060     if (parent.isValid()) {
0061         return 0;
0062     }
0063 
0064     return m_identitiesUoid.count();
0065 }
0066 
0067 QString IdentityModel::email(uint uoid)
0068 {
0069     return m_identityManager->identityForUoid(uoid).primaryEmailAddress();
0070 }
0071 
0072 QHash<int, QByteArray> IdentityModel::roleNames() const
0073 {
0074     auto roles = QAbstractListModel::roleNames();
0075     roles.insert({
0076         {UoidRole, QByteArrayLiteral("uoid")},
0077         {EmailRole, QByteArrayLiteral("email")},
0078         {IdentityNameRole, QByteArrayLiteral("identityName")},
0079     });
0080     return roles;
0081 }
0082 
0083 }
0084 
0085 #include "moc_identitymodel.cpp"