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 init();
20}
21
22LdapModel::~LdapModel() = default;
23
24void LdapModel::init()
25{
26 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
27 KConfigGroup group(config, QStringLiteral("LDAP"));
28
29 const int countSelectedHost = group.readEntry("NumSelectedHosts", 0);
30 for (int i = 0; i < countSelectedHost; ++i) {
31 auto job = new KLDAPCore::LdapClientSearchConfigReadConfigJob(this);
32 connect(job, &KLDAPCore::LdapClientSearchConfigReadConfigJob::configLoaded, this, [this, i](const KLDAPCore::LdapServer &server) {
33 mLdapServerInfo.append({true, i, server});
34 // TODO improve it
37 });
38 job->setActive(true);
39 job->setConfig(group);
40 job->setServerIndex(i);
41 job->start();
42 }
43
44 const int countUnselectedHost = group.readEntry("NumHosts", 0);
45 for (int i = 0; i < countUnselectedHost; ++i) {
46 auto job = new KLDAPCore::LdapClientSearchConfigReadConfigJob(this);
47 connect(job, &KLDAPCore::LdapClientSearchConfigReadConfigJob::configLoaded, this, [this, i, countSelectedHost](const KLDAPCore::LdapServer &server) {
48 mLdapServerInfo.append({false, i + countSelectedHost, server});
49 // TODO improve it
52 });
53 job->setActive(false);
54 job->setConfig(group);
55 job->setServerIndex(i);
56 job->start();
57 }
58}
59
60void LdapModel::save()
61{
62 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
63 config->deleteGroup(QStringLiteral("LDAP"));
64
65 KConfigGroup group(config, QStringLiteral("LDAP"));
66
67 int selected = 0;
68 int unselected = 0;
69 for (const auto &serverInfo : std::as_const(mLdapServerInfo)) {
70 if (serverInfo.enabled) {
71 auto job = new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
72 job->setActive(true);
73 job->setConfig(group);
74 job->setServerIndex(selected);
75 job->setServer(serverInfo.server);
76 job->start();
77 selected++;
78 } else {
79 auto job = new KLDAPCore::LdapClientSearchConfigWriteConfigJob;
80 job->setActive(false);
81 job->setConfig(group);
82 job->setServerIndex(unselected);
83 job->setServer(serverInfo.server);
84 job->start();
85 unselected++;
86 }
87 }
88
89 group.writeEntry("NumSelectedHosts", selected);
90 group.writeEntry("NumHosts", unselected);
91 config->sync();
92}
93
94QList<LdapModel::ServerInfo> LdapModel::ldapServerInfo() const
95{
96 return mLdapServerInfo;
97}
98
99void LdapModel::setLdapServerInfo(const QList<ServerInfo> &newLdapServerInfo)
100{
101 mLdapServerInfo = newLdapServerInfo;
102}
103
104QVariant LdapModel::data(const QModelIndex &index, int role) const
105{
106 if (!index.isValid()) {
107 return {};
108 }
109 const auto serverInfo = mLdapServerInfo[index.row()];
110 if (role == Qt::CheckStateRole && static_cast<LdapRoles>(index.column()) == Name) {
111 return serverInfo.enabled ? Qt::CheckState::Checked : Qt::CheckState::Unchecked;
112 }
113 if (role != Qt::DisplayRole) {
114 return {};
115 }
116 switch (static_cast<LdapRoles>(index.column())) {
117 case Name:
118 return serverInfo.server.host();
119 case Index:
120 return serverInfo.index;
121 case Server:
122 return QVariant::fromValue(serverInfo.server);
123 case Activities:
124 return serverInfo.server.activities();
125 }
126 return {};
127}
128
129bool LdapModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
130{
131 if (!modelIndex.isValid()) {
132 qCWarning(LDAP_CORE_LOG) << "ERROR: invalid index";
133 return false;
134 }
135 if (role == Qt::CheckStateRole) {
136 const int idx = modelIndex.row();
137 auto &serverInfo = mLdapServerInfo[idx];
138 switch (static_cast<LdapRoles>(modelIndex.column())) {
139 case Name: {
140 const QModelIndex newIndex = index(modelIndex.row(), Name);
141 Q_EMIT dataChanged(newIndex, newIndex);
142 serverInfo.enabled = value.toBool();
143 return true;
144 }
145 default:
146 break;
147 }
148 }
149 if (role != Qt::EditRole) {
150 return {};
151 }
152 const int idx = modelIndex.row();
153 auto &serverInfo = mLdapServerInfo[idx];
154 switch (static_cast<LdapRoles>(modelIndex.column())) {
155 case Server: {
156 const QModelIndex newIndex = index(modelIndex.row(), Server);
157 Q_EMIT dataChanged(newIndex, newIndex);
158 serverInfo.server = value.value<KLDAPCore::LdapServer>();
159 return true;
160 }
161 case Index: {
162 const QModelIndex newIndex = index(modelIndex.row(), Index);
163 // qDebug() << " serverInfo.server" << serverInfo.server << " value.toInt()" << value.toInt();
164 serverInfo.index = value.toInt();
165 Q_EMIT dataChanged(newIndex, newIndex);
166 return true;
167 }
168 default:
169 break;
170 }
171 return false;
172}
173
174int LdapModel::rowCount(const QModelIndex &parent) const
175{
176 Q_UNUSED(parent)
177 return mLdapServerInfo.count();
178}
179
180int LdapModel::columnCount(const QModelIndex &parent) const
181{
182 Q_UNUSED(parent)
183 constexpr int nbCol = static_cast<int>(LdapRoles::LastColumn) + 1;
184 return nbCol;
185}
186
187QVariant LdapModel::headerData(int section, Qt::Orientation orientation, int role) const
188{
189 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
190 switch (static_cast<LdapRoles>(section)) {
191 case Name:
192 case Index:
193 case Server:
194 case Activities:
195 return {};
196 }
197 }
198 return {};
199}
200
201Qt::ItemFlags LdapModel::flags(const QModelIndex &index) const
202{
203 if (!index.isValid())
204 return Qt::NoItemFlags;
205
206 if (static_cast<LdapRoles>(index.column()) == Name) {
208 }
210}
211
212void LdapModel::removeServer(int index)
213{
215 mLdapServerInfo.remove(index);
217}
218
219void LdapModel::insertServer(const KLDAPCore::LdapServer &server)
220{
221 beginInsertRows(QModelIndex(), mLdapServerInfo.count() - 1, mLdapServerInfo.count() - 1);
222 mLdapServerInfo.append({true, 0, server});
224}
225
226#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 init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
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)
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
Orientation
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 Fri Oct 11 2024 12:14:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.