KLdap

ldapconfigurewidgetng.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 "ldapconfigurewidgetng.h"
8
9#include <QCheckBox>
10#include <QHeaderView>
11#include <QLabel>
12#include <QPushButton>
13#include <QToolButton>
14#include <QTreeView>
15#include <QVBoxLayout>
16
17#include <KConfig>
18#include <KConfigGroup>
19#include <KLocalizedString>
20#include <KMessageBox>
21#include <QDialogButtonBox>
22#include <QHBoxLayout>
23
24#include <KLDAPCore/LdapClientSearchConfig>
25#include <KLDAPCore/LdapModel>
26#include <KLDAPCore/LdapServer>
27#include <KLDAPCore/LdapSortProxyModel>
28
29#include "addhostdialog.h"
30
31using namespace KLDAPWidgets;
32using namespace Qt::Literals::StringLiterals;
33
34LdapConfigureWidgetNg::LdapConfigureWidgetNg(QWidget *parent)
35 : QWidget(parent)
36 , mClientSearchConfig(new KLDAPCore::LdapClientSearchConfig)
37 , mLdapModel(new KLDAPCore::LdapModel(this))
38 , mLdapSortProxyModel(new KLDAPCore::LdapSortProxyModel(this))
39 , mLdapOnCurrentActivity(new QCheckBox(i18n("Show only ldap server on current activity"), this))
40{
41 mLdapSortProxyModel->setSourceModel(mLdapModel);
42 initGUI();
43 connect(mHostListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &LdapConfigureWidgetNg::updateButtons);
44 connect(mHostListView, &QTreeView::doubleClicked, this, &LdapConfigureWidgetNg::slotEditHost);
45 connect(mUpButton, &QToolButton::clicked, this, &LdapConfigureWidgetNg::slotMoveUp);
46 connect(mDownButton, &QToolButton::clicked, this, &LdapConfigureWidgetNg::slotMoveDown);
47}
48
49LdapConfigureWidgetNg::~LdapConfigureWidgetNg()
50{
51 delete mClientSearchConfig;
52}
53
54void LdapConfigureWidgetNg::save()
55{
56 mLdapModel->save();
57}
58
59void LdapConfigureWidgetNg::load()
60{
61 mLdapModel->load();
62}
63
64bool LdapConfigureWidgetNg::enablePlasmaActivities() const
65{
66 return mLdapSortProxyModel->enablePlasmaActivities();
67}
68
69void LdapConfigureWidgetNg::setEnablePlasmaActivities(bool newEnablePlasmaActivities)
70{
71 mLdapSortProxyModel->setEnablePlasmaActivities(newEnablePlasmaActivities);
72 mLdapOnCurrentActivity->setVisible(newEnablePlasmaActivities);
73}
74
75KLDAPCore::LdapActivitiesAbstract *LdapConfigureWidgetNg::ldapActivitiesAbstract() const
76{
77 return mLdapSortProxyModel->ldapActivitiesAbstract();
78}
79
80void LdapConfigureWidgetNg::setLdapActivitiesAbstract(KLDAPCore::LdapActivitiesAbstract *newldapActivitiesAbstract)
81{
82 mLdapSortProxyModel->setLdapActivitiesAbstract(newldapActivitiesAbstract);
83}
84
85void LdapConfigureWidgetNg::slotAddHost()
86{
88 KLDAPWidgets::AddHostDialog dlg(&server, this);
89
90 if (dlg.exec() && !server.host().trimmed().isEmpty()) {
91 mLdapModel->insertServer(server);
92 updateButtons();
93 mLdapSortProxyModel->invalidate();
94 Q_EMIT changed(true);
95 }
96}
97
98void LdapConfigureWidgetNg::updateButtons()
99{
100 const auto nbItems{mHostListView->selectionModel()->selectedRows().count()};
101 bool state = (nbItems >= 1);
102 mEditButton->setEnabled(state);
103 mRemoveButton->setEnabled(state);
104
105 if (!mHostListView->selectionModel()->hasSelection()) {
106 return;
107 }
108 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
109 const int initialRow = index.row();
110 mUpButton->setEnabled(initialRow != 0);
111 mDownButton->setEnabled(initialRow != (mHostListView->model()->rowCount() - 1));
112}
113
114void LdapConfigureWidgetNg::slotEditHost()
115{
116 if (!mHostListView->selectionModel()->hasSelection()) {
117 return;
118 }
119 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
120 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
121 KLDAPCore::LdapServer server = modelIndex.data().value<KLDAPCore::LdapServer>();
122 KLDAPWidgets::AddHostDialog dlg(&server, this);
123 dlg.setWindowTitle(i18nc("@title:window", "Edit Host"));
124
125 if (dlg.exec() && !server.host().isEmpty()) {
126 mHostListView->model()->setData(modelIndex, QVariant::fromValue(server));
127 mLdapSortProxyModel->invalidate();
128 Q_EMIT changed(true);
129 }
130}
131void LdapConfigureWidgetNg::slotRemoveHost()
132{
133 if (!mHostListView->selectionModel()->hasSelection()) {
134 return;
135 }
136 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
137 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
138 const KLDAPCore::LdapServer server = modelIndex.data().value<KLDAPCore::LdapServer>();
139 const int answer = KMessageBox::questionTwoActions(this,
140 i18n("Do you want to remove setting for host \"%1\"?", server.host()),
141 i18nc("@title:window", "Remove Host"),
144 if (answer == KMessageBox::SecondaryAction) {
145 return;
146 }
147 mLdapModel->removeServer(index.row());
148 updateButtons();
149 Q_EMIT changed(true);
150}
151
152void LdapConfigureWidgetNg::slotMoveUp()
153{
154 if (!mHostListView->selectionModel()->hasSelection()) {
155 return;
156 }
157 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
158 const int initialRow = index.row();
159
160 if (initialRow == 0) {
161 return;
162 }
163
164 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Index);
165 const QModelIndex previewIndex = mHostListView->model()->index(index.row() - 1, KLDAPCore::LdapModel::Index);
166
167 const int previousValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(previewIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
168 const int currentValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(modelIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
169
170 mHostListView->model()->setData(modelIndex, previousValue);
171 mHostListView->model()->setData(previewIndex, currentValue);
172 mHostListView->sortByColumn(KLDAPCore::LdapModel::Index, Qt::AscendingOrder);
173 updateButtons();
174 Q_EMIT changed(true);
175}
176
177void LdapConfigureWidgetNg::slotMoveDown()
178{
179 if (!mHostListView->selectionModel()->hasSelection()) {
180 return;
181 }
182 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
183 const int initialRow = index.row();
184 if (initialRow >= (mHostListView->model()->rowCount() - 1)) {
185 return;
186 }
187
188 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Index);
189 const QModelIndex nextIndex = mHostListView->model()->index(index.row() + 1, KLDAPCore::LdapModel::Index);
190
191 const int nextValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(nextIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
192 const int currentValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(modelIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
193
194 mHostListView->model()->setData(modelIndex, nextValue);
195 mHostListView->model()->setData(nextIndex, currentValue);
196 mHostListView->sortByColumn(KLDAPCore::LdapModel::Index, Qt::AscendingOrder);
197 updateButtons();
198 Q_EMIT changed(true);
199}
200
201void LdapConfigureWidgetNg::initGUI()
202{
203 auto mainLayout = new QVBoxLayout(this);
204 mainLayout->setObjectName("layout"_L1);
205
206 mainLayout->addWidget(mLdapOnCurrentActivity);
207
208 connect(mLdapOnCurrentActivity, &QCheckBox::checkStateChanged, this, [this](Qt::CheckState state) {
209 mLdapSortProxyModel->setEnablePlasmaActivities(state == Qt::Checked);
210 });
211 mLdapOnCurrentActivity->setVisible(false);
212
213 // Contents of the QVGroupBox: label and hbox
214 auto label = new QLabel(i18nc("@label:textbox", "Check all servers that should be used:"), this);
215 mainLayout->addWidget(label);
216
217 auto hBox = new QWidget(this);
218 mainLayout->addWidget(hBox);
219
220 auto hBoxHBoxLayout = new QHBoxLayout(hBox);
221 hBoxHBoxLayout->setContentsMargins({});
222 hBoxHBoxLayout->setSpacing(6);
223 // Contents of the hbox: listview and up/down buttons on the right (vbox)
224 mHostListView = new QTreeView(hBox);
225 mHostListView->setAlternatingRowColors(true);
226 // mHostListView->setSelectionMode(SingleSelection);
229 mHostListView->setRootIsDecorated(false);
230 mHostListView->setSortingEnabled(false);
231 mHostListView->header()->setSectionsMovable(false);
233 mHostListView->setModel(mLdapSortProxyModel);
234 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Activities, true);
235 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Index, true);
236 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Server, true);
237 mHostListView->setColumnHidden(KLDAPCore::LdapModel::EnabledActivitiesRole, true);
238 mHostListView->header()->hide();
239
240 hBoxHBoxLayout->addWidget(mHostListView);
241
242 auto upDownBox = new QWidget(hBox);
243 auto upDownBoxVBoxLayout = new QVBoxLayout(upDownBox);
244 upDownBoxVBoxLayout->setContentsMargins({});
245 hBoxHBoxLayout->addWidget(upDownBox);
246 upDownBoxVBoxLayout->setSpacing(6);
247 mUpButton = new QToolButton(upDownBox);
248 upDownBoxVBoxLayout->addWidget(mUpButton);
249 mUpButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
250 mUpButton->setEnabled(false); // b/c no item is selected yet
251
252 mDownButton = new QToolButton(upDownBox);
253 upDownBoxVBoxLayout->addWidget(mDownButton);
254 mDownButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
255 mDownButton->setEnabled(false); // b/c no item is selected yet
256
257 auto spacer = new QWidget(upDownBox);
258 upDownBoxVBoxLayout->addWidget(spacer);
259 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
260
261 auto buttons = new QDialogButtonBox(this);
262 QPushButton *add = buttons->addButton(i18nc("@action:button", "&Add Host…"), QDialogButtonBox::ActionRole);
263 connect(add, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotAddHost);
264 mEditButton = buttons->addButton(i18nc("@action:button", "&Edit Host…"), QDialogButtonBox::ActionRole);
265 connect(mEditButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotEditHost);
266 mEditButton->setEnabled(false);
267 mRemoveButton = buttons->addButton(i18nc("@action:button", "&Remove Host"), QDialogButtonBox::ActionRole);
268 connect(mRemoveButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotRemoveHost);
269 mRemoveButton->setEnabled(false);
270
271 mainLayout->addWidget(buttons);
272}
273
274#include "moc_ldapconfigurewidgetng.cpp"
The LdapActivitiesAbstract class.
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)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
void setAlternatingRowColors(bool enable)
void doubleClicked(const QModelIndex &index)
QAbstractItemModel * model() const const
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
QItemSelectionModel * selectionModel() const const
void setSectionResizeMode(ResizeMode mode)
void setSectionsMovable(bool movable)
QIcon fromTheme(const QString &name)
bool hasSelection() const const
QModelIndexList selectedRows(int column) const const
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
QVariant data(int role) const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
bool isEmpty() const const
QString trimmed() const const
CheckState
CustomContextMenu
AscendingOrder
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QHeaderView * header() const const
void setRootIsDecorated(bool show)
void setColumnHidden(int column, bool hide)
virtual void setModel(QAbstractItemModel *model) override
void sortByColumn(int column, Qt::SortOrder order)
void setSortingEnabled(bool enable)
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
T value() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void setEnabled(bool)
void hide()
virtual void setVisible(bool visible)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.