KLdap

ldapmodel.cpp
1/*
2 * SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "ldapmodel.h"
8#include "ldap_core_debug.h"
9#include "ldapserver.h"
10#include <KConfig>
11#include <KConfigGroup>
12#include <KLDAPCore/LdapClientSearchConfig>
13#include <KLDAPCore/LdapClientSearchConfigReadConfigJob>
14#include <KLDAPCore/LdapClientSearchConfigWriteConfigJob>
15using namespace KLDAPCore;
16LdapModel::LdapModel(QObject *parent)
17 : QAbstractListModel{parent}
18{
19}
20
21LdapModel::~LdapModel() = default;
22
23void LdapModel::init()
24{
25 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
26 KConfigGroup group(config, QStringLiteral("LDAP"));
27
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});
33 // TODO improve it
36 });
37 job->setActive(true);
38 job->setConfig(group);
39 job->setServerIndex(i);
40 job->start();
41 }
42
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});
48 // TODO improve it
51 });
52 job->setActive(false);
53 job->setConfig(group);
54 job->setServerIndex(i);
55 job->start();
56 }
57}
58
59void LdapModel::load()
60{
61 mLdapServerInfo.clear();
62 init();
63}
64
65void LdapModel::save()
66{
67 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
68 config->deleteGroup(QStringLiteral("LDAP"));
69
70 KConfigGroup group(config, QStringLiteral("LDAP"));
71
72 int selected = 0;
73 int unselected = 0;
74 for (const auto &serverInfo : std::as_const(mLdapServerInfo)) {
75 if (serverInfo.enabled) {
76 auto job = new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
77 job->setActive(true);
78 job->setConfig(group);
79 job->setServerIndex(selected);
80 job->setServer(serverInfo.server);
81 job->start();
82 selected++;
83 } else {
84 auto job = new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
85 job->setActive(false);
86 job->setConfig(group);
87 job->setServerIndex(unselected);
88 job->setServer(serverInfo.server);
89 job->start();
90 unselected++;
91 }
92 }
93
94 group.writeEntry("NumSelectedHosts", selected);
95 group.writeEntry("NumHosts", unselected);
96 config->sync();
97}
98
99QList<LdapModel::ServerInfo> LdapModel::ldapServerInfo() const
100{
101 return mLdapServerInfo;
102}
103
104void LdapModel::setLdapServerInfo(const QList<ServerInfo> &newLdapServerInfo)
105{
106 mLdapServerInfo = newLdapServerInfo;
107}
108
109QVariant LdapModel::data(const QModelIndex &index, int role) const
110{
111 if (!index.isValid()) {
112 return {};
113 }
114 const auto serverInfo = mLdapServerInfo[index.row()];
115 if (role == Qt::CheckStateRole && static_cast<LdapRoles>(index.column()) == Name) {
116 return serverInfo.enabled ? Qt::CheckState::Checked : Qt::CheckState::Unchecked;
117 }
118 if (role == Qt::DisplayRole) {
119 switch (static_cast<LdapRoles>(index.column())) {
120 case Name:
121 return serverInfo.server.host();
122 case Index:
123 return serverInfo.index;
124 case Server:
125 return QVariant::fromValue(serverInfo.server);
126 case Activities:
127 qDebug() << " serverInfo.server.activities()" << serverInfo.server.activities();
128 return serverInfo.server.activities();
129 case EnabledActivitiesRole:
130 return serverInfo.server.enablePlasmaActivities();
131 }
132 }
133 return {};
134}
135
136bool LdapModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
137{
138 if (!modelIndex.isValid()) {
139 qCWarning(LDAP_CORE_LOG) << "ERROR: invalid index";
140 return false;
141 }
142 if (role == Qt::CheckStateRole) {
143 const int idx = modelIndex.row();
144 auto &serverInfo = mLdapServerInfo[idx];
145 switch (static_cast<LdapRoles>(modelIndex.column())) {
146 case Name: {
147 const QModelIndex newIndex = index(modelIndex.row(), Name);
148 Q_EMIT dataChanged(newIndex, newIndex);
149 serverInfo.enabled = value.toBool();
150 return true;
151 }
152 default:
153 break;
154 }
155 }
156 if (role != Qt::EditRole) {
157 return {};
158 }
159 const int idx = modelIndex.row();
160 auto &serverInfo = mLdapServerInfo[idx];
161 switch (static_cast<LdapRoles>(modelIndex.column())) {
162 case Server: {
163 const QModelIndex newIndex = index(modelIndex.row(), Server);
164 Q_EMIT dataChanged(newIndex, newIndex);
165 serverInfo.server = value.value<KLDAPCore::LdapServer>();
166 return true;
167 }
168 case Index: {
169 const QModelIndex newIndex = index(modelIndex.row(), Index);
170 // qDebug() << " serverInfo.server" << serverInfo.server << " value.toInt()" << value.toInt();
171 serverInfo.index = value.toInt();
172 Q_EMIT dataChanged(newIndex, newIndex);
173 return true;
174 }
175 default:
176 break;
177 }
178 return false;
179}
180
181int LdapModel::rowCount(const QModelIndex &parent) const
182{
183 Q_UNUSED(parent)
184 return mLdapServerInfo.count();
185}
186
187int LdapModel::columnCount(const QModelIndex &parent) const
188{
189 Q_UNUSED(parent)
190 constexpr int nbCol = static_cast<int>(LdapRoles::LastColumn) + 1;
191 return nbCol;
192}
193
194Qt::ItemFlags LdapModel::flags(const QModelIndex &index) const
195{
196 if (!index.isValid())
197 return Qt::NoItemFlags;
198
199 if (static_cast<LdapRoles>(index.column()) == Name) {
201 }
203}
204
205void LdapModel::removeServer(int index)
206{
208 mLdapServerInfo.remove(index);
210}
211
212void LdapModel::insertServer(const KLDAPCore::LdapServer &server)
213{
214 beginInsertRows(QModelIndex(), mLdapServerInfo.count() - 1, mLdapServerInfo.count() - 1);
215 mLdapServerInfo.append({true, 0, server});
217}
218
219#include "moc_ldapmodel.cpp"
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
bool sync() override
A class that contains LDAP server connection settings.
Definition ldapserver.h:27
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 override
void append(QList< T > &&value)
void clear()
qsizetype count() const const
void remove(qsizetype i, qsizetype n)
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
CheckStateRole
typedef ItemFlags
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.