Commit e495fdb8 authored by Dan Leinir Turthra Jensen's avatar Dan Leinir Turthra Jensen 🌈
Browse files

Add a model displaying information about providers

parent 76656a26
......@@ -11,6 +11,7 @@ set (kaccountslib_SRCS
core.cpp
kaccountsdplugin.cpp
kaccountsuiplugin.cpp
providersmodel.cpp
servicesmodel.cpp
uipluginsmanager.cpp
......@@ -26,6 +27,7 @@ ecm_generate_headers(kaccountslib_HEADERS
Core
KAccountsUiPlugin
KAccountsDPlugin
ProvidersModel
ServicesModel
AccountServiceToggleJob
......
/*************************************************************************************
* Copyright (C) 2020 by Dan Leinir Turthra Jensen <admin@leinir.dk> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "providersmodel.h"
#include <Accounts/Provider>
#include <Accounts/Manager>
#include <KAccounts/Core>
class ProvidersModel::Private {
public:
Private() {}
Accounts::Manager* accountsManager{nullptr};
const Accounts::ProviderList& providerList() {
if (!accountsManager) {
accountsManager = KAccounts::accountsManager();
providers = accountsManager->providerList();
}
return providers;
}
private:
Accounts::ProviderList providers;
};
ProvidersModel::ProvidersModel(QObject* parent)
: QAbstractListModel(parent)
, d(new Private)
{
}
ProvidersModel::~ProvidersModel()
{
delete d;
}
QHash<int, QByteArray> ProvidersModel::roleNames() const
{
static const QHash<int, QByteArray> roleNames {
{NameRole, "name"},
{DisplayNameRole, "displayName"},
{DescriptionRole, "description"},
{IconNameRole, "iconName"},
{SupportsMultipleLoginsRole, "supportsMultipleLogins"},
{AccountsCountRole, "accountsCount"}
};
return roleNames;
}
int ProvidersModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
return 0;
}
return d->providerList().count();
}
QVariant ProvidersModel::data(const QModelIndex& index, int role) const
{
QVariant data;
if (checkIndex(index)) {
const Accounts::Provider& provider = d->providerList().value(index.row());
if (provider.isValid()) {
switch(role) {
case NameRole:
data.setValue(provider.name());
break;
case DisplayNameRole:
data.setValue(provider.displayName());
break;
case DescriptionRole:
data.setValue(provider.description());
break;
case IconNameRole:
data.setValue(provider.iconName());
break;
case SupportsMultipleLoginsRole:
data.setValue(!provider.isSingleAccount());
break;
case AccountsCountRole:
{
const Accounts::AccountIdList accounts = d->accountsManager->accountList();
int i{0};
for (const Accounts::AccountId& accountId : accounts) {
Accounts::Account* account = d->accountsManager->account(accountId);
if (account->providerName() == provider.name()) {
++i;
}
}
data.setValue<int>(i);
break;
}
default:
data.setValue(QString::fromLatin1("No such role: %1").arg(role));
break;
}
}
}
return data;
}
/*************************************************************************************
* Copyright (C) 2020 by Dan Leinir Turthra Jensen <admin@leinir.dk> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#ifndef PROVIDERS_MODEL_H
#define PROVIDERS_MODEL_H
#include "kaccounts_export.h"
#include <QAbstractListModel>
/**
* @brief A model which represents the available providers
*/
class KACCOUNTS_EXPORT ProvidersModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
NameRole = Qt::UserRole + 1, ///< The unique name identifier for this provider
DisplayNameRole, ///< The human-readable name for this provider
DescriptionRole, ///< A (usually single sentence) description of this provider
IconNameRole, ///< The name of the icon to be used for this provider (an XDG Icon Spec style name)
SupportsMultipleLoginsRole, ///< Whether or not this provider supports multiple simultaneous accounts
AccountsCountRole ///< The number of accounts which already exist for this provider
};
explicit ProvidersModel(QObject* parent = nullptr);
virtual ~ProvidersModel();
QHash< int, QByteArray > roleNames() const override;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
private:
class Private;
Private *d;
};
#endif//PROVIDERS_MODEL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment