File indexing completed on 2024-04-28 05:36:12

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003     SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "IdentitiesModel.h"
0009 #include <KLocalizedString>
0010 #include <KUser>
0011 #include <QDebug>
0012 
0013 IdentitiesModel::IdentitiesModel(QObject *parent)
0014     : QAbstractListModel(parent)
0015 {
0016 }
0017 
0018 void IdentitiesModel::setIdentities(const PolkitQt1::Identity::List &identities, bool withVoidItem)
0019 {
0020     beginResetModel();
0021     m_identities = identities;
0022     m_ids.clear();
0023     m_ids.reserve(m_identities.size() + 1);
0024 
0025     if (withVoidItem) {
0026         // Adds a Dummy user
0027         m_ids.append(Id{{}, i18n("Select User"), {}, {}});
0028     }
0029 
0030     // For each user
0031     for (const PolkitQt1::Identity &identity : identities) {
0032         // First check to see if the user is valid
0033 
0034         const QString identityString = identity.toString();
0035         qDebug() << "User: " << identityString;
0036         const KUser user(QString(identityString).remove("unix-user:"));
0037         if (!user.isValid()) {
0038             qWarning() << "User invalid: " << user.loginName();
0039             continue;
0040         }
0041 
0042         // Display user Full Name IF available
0043         QString display;
0044         if (!user.property(KUser::FullName).toString().isEmpty()) {
0045             display = i18nc("%1 is the full user name, %2 is the user login name", "%1 (%2)", user.property(KUser::FullName).toString(), user.loginName());
0046         } else {
0047             display = user.loginName();
0048         }
0049 
0050         QString icon;
0051         // load user icon face
0052         if (!user.faceIconPath().isEmpty()) {
0053             icon = user.faceIconPath();
0054         } else {
0055             icon = "user-identity";
0056         }
0057         m_ids.append(Id{icon, display, identityString, user.loginName()});
0058     }
0059     endResetModel();
0060 }
0061 
0062 QVariant IdentitiesModel::data(const QModelIndex &index, int role) const
0063 {
0064     if (!checkIndex(index, CheckIndexOption::IndexIsValid)) {
0065         return {};
0066     }
0067 
0068     const Id &id = m_ids[index.row()];
0069     switch (role) {
0070     case Qt::DecorationRole:
0071         return id.decoration;
0072     case Qt::DisplayRole:
0073         return id.display;
0074     case Qt::UserRole:
0075         return id.userRole;
0076     }
0077     return {};
0078 }
0079 
0080 int IdentitiesModel::rowCount(const QModelIndex &parent) const
0081 {
0082     return parent.isValid() ? 0 : m_ids.count();
0083 }
0084 
0085 Qt::ItemFlags IdentitiesModel::flags(const QModelIndex &index) const
0086 {
0087     if (!index.isValid())
0088         return {};
0089     auto ret = Qt::ItemNeverHasChildren | Qt::ItemNeverHasChildren;
0090     if (!m_ids[index.row()].userRole.isEmpty())
0091         ret |= Qt::ItemIsEnabled;
0092 
0093     return ret;
0094 }
0095 
0096 int IdentitiesModel::indexForUser(const QString &loginName) const
0097 {
0098     for (int i = 0, c = m_ids.count(); i < c; ++i) {
0099         if (m_ids[i].loginName == loginName) {
0100             return i;
0101         }
0102     }
0103     return -1;
0104 }
0105 
0106 QString IdentitiesModel::iconForIndex(int index) const
0107 {
0108     if (index < 0 || index >= m_ids.size()) {
0109         return {};
0110     }
0111     return m_ids[index].decoration;
0112 }
0113 
0114 QHash<int, QByteArray> IdentitiesModel::roleNames() const
0115 {
0116     return {{Qt::DecorationRole, "decoration"}, {Qt::DisplayRole, "display"}, {Qt::UserRole, "userRole"}};
0117 }
0118 
0119 #include "moc_IdentitiesModel.cpp"