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 case EnabledActivitiesRole:
126 return serverInfo.server.enablePlasmaActivities();
127 }
128 return {};
129}
130
131bool LdapModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
132{
133 if (!modelIndex.isValid()) {
134 qCWarning(LDAP_CORE_LOG) << "ERROR: invalid index";
135 return false;
136 }
137 if (role == Qt::CheckStateRole) {
138 const int idx = modelIndex.row();
139 auto &serverInfo = mLdapServerInfo[idx];
140 switch (static_cast<LdapRoles>(modelIndex.column())) {
141 case Name: {
142 const QModelIndex newIndex = index(modelIndex.row(), Name);
143 Q_EMIT dataChanged(newIndex, newIndex);
144 serverInfo.enabled = value.toBool();
145 return true;
146 }
147 default:
148 break;
149 }
150 }
151 if (role != Qt::EditRole) {
152 return {};
153 }
154 const int idx = modelIndex.row();
155 auto &serverInfo = mLdapServerInfo[idx];
156 switch (static_cast<LdapRoles>(modelIndex.column())) {
157 case Server: {
158 const QModelIndex newIndex = index(modelIndex.row(), Server);
159 Q_EMIT dataChanged(newIndex, newIndex);
160 serverInfo.server = value.value<KLDAPCore::LdapServer>();
161 return true;
162 }
163 case Index: {
164 const QModelIndex newIndex = index(modelIndex.row(), Index);
165 // qDebug() << " serverInfo.server" << serverInfo.server << " value.toInt()" << value.toInt();
166 serverInfo.index = value.toInt();
167 Q_EMIT dataChanged(newIndex, newIndex);
168 return true;
169 }
170 default:
171 break;
172 }
173 return false;
174}
175
176int LdapModel::rowCount(const QModelIndex &parent) const
177{
178 Q_UNUSED(parent)
179 return mLdapServerInfo.count();
180}
181
182int LdapModel::columnCount(const QModelIndex &parent) const
183{
184 Q_UNUSED(parent)
185 constexpr int nbCol = static_cast<int>(LdapRoles::LastColumn) + 1;
186 return nbCol;
187}
188
189Qt::ItemFlags LdapModel::flags(const QModelIndex &index) const
190{
191 if (!index.isValid())
192 return Qt::NoItemFlags;
193
194 if (static_cast<LdapRoles>(index.column()) == Name) {
196 }
198}
199
200void LdapModel::removeServer(int index)
201{
203 mLdapServerInfo.remove(index);
205}
206
207void LdapModel::insertServer(const KLDAPCore::LdapServer &server)
208{
209 beginInsertRows(QModelIndex(), mLdapServerInfo.count() - 1, mLdapServerInfo.count() - 1);
210 mLdapServerInfo.append({true, 0, server});
212}
213
214#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
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 Mon Nov 4 2024 16:34:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.