KLdap

ldapconfigurewidget.cpp
1/*
2 * SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "ldapconfigurewidget.h"
8#include <QLabel>
9#include <QListWidget>
10#include <QListWidgetItem>
11#include <QPushButton>
12#include <QToolButton>
13#include <QVBoxLayout>
14
15#include <KConfig>
16#include <KConfigGroup>
17#include <KLocalizedString>
18#include <KMessageBox>
19#include <QDialogButtonBox>
20#include <QHBoxLayout>
21
22#include "kldapcore/ldapserver.h"
23#include "ldapclientsearchconfig.h"
24#include "ldapclientsearchconfigwriteconfigjob.h"
25#include "ldapwidgetitem_p.h"
26#include "ldapwidgetitemreadconfigserverjob.h"
27
28#include "addhostdialog.h"
29
30using namespace KLDAPWidgets;
31
32LdapConfigureWidget::LdapConfigureWidget(QWidget *parent)
33 : QWidget(parent)
34 , mClientSearchConfig(new KLDAPWidgets::LdapClientSearchConfig)
35{
36 initGUI();
37
38 connect(mHostListView, &QListWidget::currentItemChanged, this, &LdapConfigureWidget::slotSelectionChanged);
39 connect(mHostListView, &QListWidget::itemDoubleClicked, this, &LdapConfigureWidget::slotEditHost);
40 connect(mHostListView, &QListWidget::itemClicked, this, &LdapConfigureWidget::slotItemClicked);
41
42 connect(mUpButton, &QToolButton::clicked, this, &LdapConfigureWidget::slotMoveUp);
43 connect(mDownButton, &QToolButton::clicked, this, &LdapConfigureWidget::slotMoveDown);
44}
45
46LdapConfigureWidget::~LdapConfigureWidget()
47{
48 delete mClientSearchConfig;
49}
50
51void LdapConfigureWidget::slotSelectionChanged(QListWidgetItem *item)
52{
53 bool state = (item != nullptr);
54 mEditButton->setEnabled(state);
55 mRemoveButton->setEnabled(state);
56 mDownButton->setEnabled(item && (mHostListView->row(item) != (mHostListView->count() - 1)));
57 mUpButton->setEnabled(item && (mHostListView->row(item) != 0));
58}
59
60void LdapConfigureWidget::slotItemClicked(QListWidgetItem *item)
61{
62 auto ldapItem = dynamic_cast<LdapWidgetItem *>(item);
63 if (!ldapItem) {
64 return;
65 }
66
67 if ((ldapItem->checkState() == Qt::Checked) != ldapItem->isActive()) {
68 Q_EMIT changed(true);
69 ldapItem->setIsActive(ldapItem->checkState() == Qt::Checked);
70 }
71}
72
73void LdapConfigureWidget::slotAddHost()
74{
76 KLDAPWidgets::AddHostDialog dlg(&server, this);
77
78 if (dlg.exec() && !server.host().trimmed().isEmpty()) { // krazy:exclude=crashy
79 auto item = new LdapWidgetItem(mHostListView);
80 item->setServer(server);
81
82 Q_EMIT changed(true);
83 }
84}
85
86void LdapConfigureWidget::slotEditHost()
87{
88 auto item = dynamic_cast<LdapWidgetItem *>(mHostListView->currentItem());
89 if (!item) {
90 return;
91 }
92
93 KLDAPCore::LdapServer server = item->server();
94 KLDAPWidgets::AddHostDialog dlg(&server, this);
95 dlg.setWindowTitle(i18nc("@title:window", "Edit Host"));
96
97 if (dlg.exec() && !server.host().isEmpty()) { // krazy:exclude=crashy
98 item->setServer(server);
99
100 Q_EMIT changed(true);
101 }
102}
103
104void LdapConfigureWidget::slotRemoveHost()
105{
106 QListWidgetItem *item = mHostListView->currentItem();
107 if (!item) {
108 return;
109 }
110 auto ldapItem = dynamic_cast<LdapWidgetItem *>(item);
112 i18n("Do you want to remove setting for host \"%1\"?", ldapItem->server().host()),
113 i18n("Remove Host"),
117 return;
118 }
119
120 delete mHostListView->takeItem(mHostListView->currentRow());
121
122 slotSelectionChanged(mHostListView->currentItem());
123
124 Q_EMIT changed(true);
125}
126
127static void swapItems(LdapWidgetItem *item, LdapWidgetItem *other)
128{
129 KLDAPCore::LdapServer server = item->server();
130 bool isActive = item->isActive();
131 item->setServer(other->server());
132 item->setIsActive(other->isActive());
133 item->setCheckState(other->isActive() ? Qt::Checked : Qt::Unchecked);
134 other->setServer(server);
135 other->setIsActive(isActive);
136 other->setCheckState(isActive ? Qt::Checked : Qt::Unchecked);
137}
138
139void LdapConfigureWidget::slotMoveUp()
140{
141 const QList<QListWidgetItem *> selectedItems = mHostListView->selectedItems();
142 if (selectedItems.isEmpty()) {
143 return;
144 }
145
146 LdapWidgetItem *item = static_cast<LdapWidgetItem *>(mHostListView->selectedItems().first());
147 if (!item) {
148 return;
149 }
150
151 auto above = static_cast<LdapWidgetItem *>(mHostListView->item(mHostListView->row(item) - 1));
152 if (!above) {
153 return;
154 }
155
156 swapItems(item, above);
157
158 mHostListView->setCurrentItem(above);
159 above->setSelected(true);
160
161 Q_EMIT changed(true);
162}
163
164void LdapConfigureWidget::slotMoveDown()
165{
166 const QList<QListWidgetItem *> selectedItems = mHostListView->selectedItems();
167 if (selectedItems.isEmpty()) {
168 return;
169 }
170
171 LdapWidgetItem *item = static_cast<LdapWidgetItem *>(mHostListView->selectedItems().first());
172 if (!item) {
173 return;
174 }
175
176 auto below = static_cast<LdapWidgetItem *>(mHostListView->item(mHostListView->row(item) + 1));
177 if (!below) {
178 return;
179 }
180
181 swapItems(item, below);
182
183 mHostListView->setCurrentItem(below);
184 below->setSelected(true);
185
186 Q_EMIT changed(true);
187}
188
189void LdapConfigureWidget::load()
190{
191 mHostListView->clear();
192 KConfig *config = KLDAPWidgets::LdapClientSearchConfig::config();
193 KConfigGroup group(config, QStringLiteral("LDAP"));
194
195 int count = group.readEntry("NumSelectedHosts", 0);
196 for (int i = 0; i < count; ++i) {
197 auto item = new LdapWidgetItem(mHostListView, true);
198 item->setCheckState(Qt::Checked);
199 auto job = new LdapWidgetItemReadConfigServerJob(this);
200 job->setCurrentIndex(i);
201 job->setActive(true);
202 job->setConfig(group);
203 job->setLdapWidgetItem(item);
204 job->start();
205 }
206
207 count = group.readEntry("NumHosts", 0);
208 for (int i = 0; i < count; ++i) {
209 auto item = new LdapWidgetItem(mHostListView);
210 auto job = new LdapWidgetItemReadConfigServerJob(this);
211 job->setCurrentIndex(i);
212 job->setActive(false);
213 job->setConfig(group);
214 job->setLdapWidgetItem(item);
215 job->start();
216 }
217
218 Q_EMIT changed(false);
219}
220
221void LdapConfigureWidget::save()
222{
223 // mClientSearchConfig->clearWalletPassword();
224 KConfig *config = KLDAPWidgets::LdapClientSearchConfig::config();
225 config->deleteGroup(QStringLiteral("LDAP"));
226
227 KConfigGroup group(config, QStringLiteral("LDAP"));
228
229 int selected = 0;
230 int unselected = 0;
231 for (int i = 0; i < mHostListView->count(); ++i) {
232 auto item = dynamic_cast<LdapWidgetItem *>(mHostListView->item(i));
233 if (!item) {
234 continue;
235 }
236
237 KLDAPCore::LdapServer server = item->server();
238 if (item->checkState() == Qt::Checked) {
239 auto job = new LdapClientSearchConfigWriteConfigJob;
240 job->setActive(true);
241 job->setConfig(group);
242 job->setServerIndex(selected);
243 job->setServer(server);
244 job->start();
245 selected++;
246 } else {
247 auto job = new LdapClientSearchConfigWriteConfigJob;
248 job->setActive(false);
249 job->setConfig(group);
250 job->setServerIndex(unselected);
251 job->setServer(server);
252 job->start();
253 unselected++;
254 }
255 }
256
257 group.writeEntry("NumSelectedHosts", selected);
258 group.writeEntry("NumHosts", unselected);
259 config->sync();
260
261 Q_EMIT changed(false);
262}
263
264void LdapConfigureWidget::initGUI()
265{
266 auto mainLayout = new QVBoxLayout(this);
267 mainLayout->setObjectName(QLatin1StringView("layout"));
268
269 // Contents of the QVGroupBox: label and hbox
270 auto label = new QLabel(i18n("Check all servers that should be used:"));
271 mainLayout->addWidget(label);
272
273 auto hBox = new QWidget(this);
274 mainLayout->addWidget(hBox);
275
276 auto hBoxHBoxLayout = new QHBoxLayout(hBox);
277 hBoxHBoxLayout->setContentsMargins(0, 0, 0, 0);
278 hBoxHBoxLayout->setSpacing(6);
279 // Contents of the hbox: listview and up/down buttons on the right (vbox)
280 mHostListView = new QListWidget(hBox);
281 hBoxHBoxLayout->addWidget(mHostListView);
282 mHostListView->setSortingEnabled(false);
283
284 auto upDownBox = new QWidget(hBox);
286 upDownBoxVBoxLayout->setContentsMargins(0, 0, 0, 0);
287 hBoxHBoxLayout->addWidget(upDownBox);
288 upDownBoxVBoxLayout->setSpacing(6);
289 mUpButton = new QToolButton(upDownBox);
290 upDownBoxVBoxLayout->addWidget(mUpButton);
291 mUpButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
292 mUpButton->setEnabled(false); // b/c no item is selected yet
293
294 mDownButton = new QToolButton(upDownBox);
295 upDownBoxVBoxLayout->addWidget(mDownButton);
296 mDownButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
297 mDownButton->setEnabled(false); // b/c no item is selected yet
298
299 auto spacer = new QWidget(upDownBox);
300 upDownBoxVBoxLayout->addWidget(spacer);
301 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
302
303 auto buttons = new QDialogButtonBox(this);
304 QPushButton *add = buttons->addButton(i18n("&Add Host..."), QDialogButtonBox::ActionRole);
305 connect(add, &QPushButton::clicked, this, &LdapConfigureWidget::slotAddHost);
306 mEditButton = buttons->addButton(i18n("&Edit Host..."), QDialogButtonBox::ActionRole);
307 connect(mEditButton, &QPushButton::clicked, this, &LdapConfigureWidget::slotEditHost);
308 mEditButton->setEnabled(false);
309 mRemoveButton = buttons->addButton(i18n("&Remove Host"), QDialogButtonBox::ActionRole);
310 connect(mRemoveButton, &QPushButton::clicked, this, &LdapConfigureWidget::slotRemoveHost);
311 mRemoveButton->setEnabled(false);
312
313 mainLayout->addWidget(buttons);
314}
315
316#include "moc_ldapconfigurewidget.cpp"
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
bool sync() override
A class that contains LDAP server connection settings.
Definition ldapserver.h:27
QString host() const
Returns the host of the LDAP connection.
The AddHostDialog class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KIOCORE_EXPORT void add(const QString &fileClass, const QString &directory)
KGuiItem remove()
KGuiItem cancel()
QString label(StandardShortcut id)
void clicked(bool checked)
void setIcon(const QIcon &icon)
QIcon fromTheme(const QString &name)
bool isEmpty() const const
void clear()
QListWidgetItem * currentItem() const const
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
QListWidgetItem * item(int row) const const
void itemClicked(QListWidgetItem *item)
void itemDoubleClicked(QListWidgetItem *item)
int row(const QListWidgetItem *item) const const
QList< QListWidgetItem * > selectedItems() const const
void setCurrentItem(QListWidgetItem *item)
void setSortingEnabled(bool enable)
QListWidgetItem * takeItem(int row)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
bool isEmpty() const const
QString trimmed() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.