File indexing completed on 2024-05-19 15:57:54

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "providersmodel.h"
0007 
0008 #include "core.h"
0009 
0010 #include <Accounts/Manager>
0011 #include <Accounts/Provider>
0012 
0013 namespace KAccounts
0014 {
0015 
0016 class ProvidersModel::Private
0017 {
0018 public:
0019     Private()
0020     {
0021     }
0022     Accounts::Manager *accountsManager{nullptr};
0023 
0024     const Accounts::ProviderList &providerList()
0025     {
0026         if (!accountsManager) {
0027             accountsManager = KAccounts::accountsManager();
0028             providers = accountsManager->providerList();
0029         }
0030         return providers;
0031     }
0032 
0033 private:
0034     Accounts::ProviderList providers;
0035 };
0036 
0037 ProvidersModel::ProvidersModel(QObject *parent)
0038     : QAbstractListModel(parent)
0039     , d(new Private)
0040 {
0041 }
0042 
0043 ProvidersModel::~ProvidersModel()
0044 {
0045     delete d;
0046 }
0047 
0048 QHash<int, QByteArray> ProvidersModel::roleNames() const
0049 {
0050     static const QHash<int, QByteArray> roleNames{
0051         {NameRole, "name"},
0052         {DisplayNameRole, "displayName"},
0053         {DescriptionRole, "description"},
0054         {IconNameRole, "iconName"},
0055         {SupportsMultipleAccountsRole, "supportsMultipleAccounts"},
0056         {AccountsCountRole, "accountsCount"},
0057     };
0058     return roleNames;
0059 }
0060 
0061 int ProvidersModel::rowCount(const QModelIndex &parent) const
0062 {
0063     if (parent.isValid()) {
0064         return 0;
0065     }
0066     return d->providerList().count();
0067 }
0068 
0069 QVariant ProvidersModel::data(const QModelIndex &index, int role) const
0070 {
0071     QVariant data;
0072     if (checkIndex(index)) {
0073         const Accounts::Provider &provider = d->providerList().value(index.row());
0074         if (provider.isValid()) {
0075             switch (role) {
0076             case NameRole:
0077                 data.setValue(provider.name());
0078                 break;
0079             case DisplayNameRole:
0080                 data.setValue(provider.displayName());
0081                 break;
0082             case DescriptionRole:
0083                 data.setValue(provider.description());
0084                 break;
0085             case IconNameRole:
0086                 data.setValue(provider.iconName());
0087                 break;
0088             case SupportsMultipleAccountsRole:
0089                 data.setValue(!provider.isSingleAccount());
0090                 break;
0091             case AccountsCountRole: {
0092                 const Accounts::AccountIdList accounts = d->accountsManager->accountList();
0093                 int i{0};
0094                 for (const Accounts::AccountId &accountId : accounts) {
0095                     Accounts::Account *account = d->accountsManager->account(accountId);
0096                     if (account->providerName() == provider.name()) {
0097                         ++i;
0098                     }
0099                 }
0100                 data.setValue(i);
0101                 break;
0102             }
0103             default:
0104                 data.setValue(QStringLiteral("No such role: %1").arg(role));
0105                 break;
0106             }
0107         }
0108     }
0109     return data;
0110 }
0111 
0112 };