8#include "ldap_core_debug.h"
11#include <KConfigGroup>
12#include <KLDAPCore/LdapClientSearchConfig>
13#include <KLDAPCore/LdapClientSearchConfigReadConfigJob>
14#include <KLDAPCore/LdapClientSearchConfigWriteConfigJob>
15using namespace KLDAPCore;
16LdapModel::LdapModel(
QObject *parent)
21LdapModel::~LdapModel() =
default;
25 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
26 KConfigGroup group(config, QStringLiteral(
"LDAP"));
28 const int countSelectedHost = group.readEntry(
"NumSelectedHosts", 0);
29 for (
int i = 0; i < countSelectedHost; ++i) {
30 auto job =
new KLDAPCore::LdapClientSearchConfigReadConfigJob(
this);
31 connect(job, &KLDAPCore::LdapClientSearchConfigReadConfigJob::configLoaded,
this, [
this, i](
const KLDAPCore::LdapServer &server) {
32 mLdapServerInfo.append({
true, i, server});
38 job->setConfig(group);
39 job->setServerIndex(i);
43 const int countUnselectedHost = group.readEntry(
"NumHosts", 0);
44 for (
int i = 0; i < countUnselectedHost; ++i) {
45 auto job =
new KLDAPCore::LdapClientSearchConfigReadConfigJob(
this);
46 connect(job, &KLDAPCore::LdapClientSearchConfigReadConfigJob::configLoaded,
this, [
this, i, countSelectedHost](
const KLDAPCore::LdapServer &server) {
47 mLdapServerInfo.append({
false, i + countSelectedHost, server});
52 job->setActive(
false);
53 job->setConfig(group);
54 job->setServerIndex(i);
61 mLdapServerInfo.clear();
67 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
70 KConfigGroup group(config, QStringLiteral(
"LDAP"));
74 for (
const auto &serverInfo : std::as_const(mLdapServerInfo)) {
75 if (serverInfo.enabled) {
76 auto job =
new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
78 job->setConfig(group);
79 job->setServerIndex(selected);
80 job->setServer(serverInfo.server);
84 auto job =
new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
85 job->setActive(
false);
86 job->setConfig(group);
87 job->setServerIndex(unselected);
88 job->setServer(serverInfo.server);
94 group.writeEntry(
"NumSelectedHosts", selected);
95 group.writeEntry(
"NumHosts", unselected);
99QList<LdapModel::ServerInfo> LdapModel::ldapServerInfo()
const
101 return mLdapServerInfo;
104void LdapModel::setLdapServerInfo(
const QList<ServerInfo> &newLdapServerInfo)
106 mLdapServerInfo = newLdapServerInfo;
109QVariant LdapModel::data(
const QModelIndex &index,
int role)
const
111 if (!
index.isValid()) {
114 const auto serverInfo = mLdapServerInfo[
index.row()];
116 return serverInfo.enabled ? Qt::CheckState::Checked : Qt::CheckState::Unchecked;
119 switch (
static_cast<LdapRoles
>(
index.column())) {
121 return serverInfo.server.host();
123 return serverInfo.index;
127 qDebug() <<
" serverInfo.server.activities()" << serverInfo.server.activities();
128 return serverInfo.server.activities();
129 case EnabledActivitiesRole:
130 return serverInfo.server.enablePlasmaActivities();
136bool LdapModel::setData(
const QModelIndex &modelIndex,
const QVariant &value,
int role)
139 qCWarning(LDAP_CORE_LOG) <<
"ERROR: invalid index";
143 const int idx = modelIndex.
row();
144 auto &serverInfo = mLdapServerInfo[idx];
145 switch (
static_cast<LdapRoles
>(modelIndex.
column())) {
147 const QModelIndex newIndex =
index(modelIndex.
row(), Name);
149 serverInfo.enabled = value.
toBool();
159 const int idx = modelIndex.
row();
160 auto &serverInfo = mLdapServerInfo[idx];
161 switch (
static_cast<LdapRoles
>(modelIndex.
column())) {
163 const QModelIndex newIndex =
index(modelIndex.
row(), Server);
165 serverInfo.server = value.
value<KLDAPCore::LdapServer>();
169 const QModelIndex newIndex =
index(modelIndex.
row(), Index);
171 serverInfo.index = value.
toInt();
181int LdapModel::rowCount(
const QModelIndex &parent)
const
184 return mLdapServerInfo.count();
187int LdapModel::columnCount(
const QModelIndex &parent)
const
190 constexpr int nbCol =
static_cast<int>(LdapRoles::LastColumn) + 1;
194Qt::ItemFlags LdapModel::flags(
const QModelIndex &index)
const
196 if (!
index.isValid())
199 if (
static_cast<LdapRoles
>(
index.column()) == Name) {
205void LdapModel::removeServer(
int index)
208 mLdapServerInfo.remove(
index);
212void LdapModel::insertServer(
const KLDAPCore::LdapServer &server)
214 beginInsertRows(QModelIndex(), mLdapServerInfo.count() - 1, mLdapServerInfo.count() - 1);
215 mLdapServerInfo.append({
true, 0, server});
219#include "moc_ldapmodel.cpp"
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) const const