File indexing completed on 2024-06-16 07:48:10

0001 /*
0002  *  SPDX-FileCopyrightText: 2012 Alejandro Fiestas Olivares <afiestas@kde.org>
0003  *  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "servicesmodel.h"
0009 
0010 #include "core.h"
0011 
0012 #include <QDebug>
0013 
0014 #include <QIcon>
0015 
0016 #include <SignOn/Identity>
0017 
0018 #include <Accounts/Account>
0019 #include <Accounts/Manager>
0020 
0021 namespace KAccounts
0022 {
0023 
0024 class ServicesModel::Private : public QObject
0025 {
0026 public:
0027     Private(ServicesModel *model)
0028         : q(model)
0029     {
0030     }
0031     virtual ~Private()
0032     {
0033     }
0034 
0035     Accounts::ServiceList services;
0036     Accounts::Account *account{nullptr};
0037 
0038 private:
0039     ServicesModel *q;
0040 };
0041 
0042 ServicesModel::ServicesModel(QObject *parent)
0043     : QAbstractListModel(parent)
0044     , d(new ServicesModel::Private(this))
0045 {
0046 }
0047 
0048 ServicesModel::~ServicesModel()
0049 {
0050     delete d;
0051 }
0052 
0053 QHash<int, QByteArray> ServicesModel::roleNames() const
0054 {
0055     static QHash<int, QByteArray> roles{{NameRole, "name"},
0056                                         {DescriptionRole, "description"},
0057                                         {DisplayNameRole, "displayName"},
0058                                         {ServiceTypeRole, "servieType"},
0059                                         {ProviderNameRole, "providerName"},
0060                                         {IconNameRole, "iconName"},
0061                                         {TagsRole, "tags"},
0062                                         {EnabledRole, "enabled"}};
0063     return roles;
0064 }
0065 
0066 int ServicesModel::rowCount(const QModelIndex &parent) const
0067 {
0068     if (parent.isValid()) {
0069         return 0;
0070     }
0071 
0072     return d->services.count();
0073 }
0074 
0075 QVariant ServicesModel::data(const QModelIndex &index, int role) const
0076 {
0077     QVariant data;
0078     if (checkIndex(index)) {
0079         const Accounts::Service &service = d->services.value(index.row());
0080         if (service.isValid()) {
0081             switch (role) {
0082             case NameRole:
0083                 data.setValue(service.name());
0084                 break;
0085             case DescriptionRole:
0086                 // Not all services have descriptions and UIs should be designed with that in mind.
0087                 // Consequently, we can accept not having a fallback for this.
0088                 data.setValue(service.description());
0089                 break;
0090             case DisplayNameRole:
0091                 data.setValue(service.displayName());
0092                 break;
0093             case ServiceTypeRole:
0094                 data.setValue(service.serviceType());
0095                 break;
0096             case ProviderNameRole:
0097                 data.setValue(service.provider());
0098                 break;
0099             case IconNameRole:
0100                 data.setValue(service.iconName());
0101                 break;
0102             case TagsRole:
0103                 data.setValue(service.tags().values());
0104                 break;
0105             case EnabledRole:
0106                 data.setValue(d->account->enabledServices().contains(service));
0107                 break;
0108             }
0109         }
0110     }
0111     return data;
0112 }
0113 
0114 void ServicesModel::setAccount(QObject *account)
0115 {
0116     if (d->account != account) {
0117         beginResetModel();
0118         d->services.clear();
0119         if (d->account) {
0120             disconnect(d->account, nullptr, this, nullptr);
0121         }
0122         d->account = qobject_cast<Accounts::Account *>(account);
0123         if (d->account) {
0124             connect(d->account, &Accounts::Account::displayNameChanged, this, &ServicesModel::accountChanged);
0125             connect(d->account, &Accounts::Account::enabledChanged, this, [this](const QString &serviceName, bool /*enabled*/) {
0126                 int i{0};
0127                 for (const Accounts::Service &service : std::as_const(d->services)) {
0128                     if (service.name() == serviceName) {
0129                         break;
0130                     }
0131                     ++i;
0132                 }
0133                 Q_EMIT dataChanged(index(i), index(i));
0134             });
0135             connect(d->account, &QObject::destroyed, this, [this]() {
0136                 beginResetModel();
0137                 d->account = nullptr;
0138                 Q_EMIT accountChanged();
0139                 d->services.clear();
0140                 endResetModel();
0141             });
0142             d->services = d->account->services();
0143         }
0144         endResetModel();
0145         Q_EMIT accountChanged();
0146     }
0147 }
0148 
0149 QObject *ServicesModel::account() const
0150 {
0151     return d->account;
0152 }
0153 
0154 quint32 ServicesModel::accountId() const
0155 {
0156     if (d->account) {
0157         return d->account->id();
0158     }
0159     return -1;
0160 }
0161 
0162 QString ServicesModel::accountDisplayName() const
0163 {
0164     if (d->account) {
0165         return d->account->displayName();
0166     }
0167     return QString{};
0168 }
0169 
0170 QString ServicesModel::accountProviderName() const
0171 {
0172     if (d->account) {
0173         return d->account->providerName();
0174     }
0175     return QString{};
0176 }
0177 
0178 QString ServicesModel::accountIconName() const
0179 {
0180     if (d->account && d->account->provider().isValid() && !d->account->provider().iconName().isEmpty()) {
0181         return d->account->provider().iconName();
0182     }
0183     return QString::fromLatin1("user-identity");
0184 }
0185 
0186 };