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 servers 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 mLdapOnCurrentActivity->setVisible(newEnablePlasmaActivities);
72}
73
74KLDAPCore::LdapActivitiesAbstract *LdapConfigureWidgetNg::ldapActivitiesAbstract() const
75{
76 return mLdapSortProxyModel->ldapActivitiesAbstract();
77}
78
79void LdapConfigureWidgetNg::setLdapActivitiesAbstract(KLDAPCore::LdapActivitiesAbstract *newldapActivitiesAbstract)
80{
81 mLdapSortProxyModel->setLdapActivitiesAbstract(newldapActivitiesAbstract);
82}
83
84void LdapConfigureWidgetNg::slotAddHost()
85{
87 KLDAPWidgets::AddHostDialog dlg(&server, this);
88
89 if (dlg.exec() && !server.host().trimmed().isEmpty()) {
90 mLdapModel->insertServer(server);
91 updateButtons();
92 mLdapSortProxyModel->invalidate();
93 Q_EMIT changed(true);
94 }
95}
96
97void LdapConfigureWidgetNg::updateButtons()
98{
99 const auto nbItems{mHostListView->selectionModel()->selectedRows().count()};
100 bool state = (nbItems >= 1);
101 mEditButton->setEnabled(state);
102 mRemoveButton->setEnabled(state);
103
104 if (!mHostListView->selectionModel()->hasSelection()) {
105 return;
106 }
107 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
108 const int initialRow = index.row();
109 mUpButton->setEnabled(initialRow != 0);
110 mDownButton->setEnabled(initialRow != (mHostListView->model()->rowCount() - 1));
111}
112
113void LdapConfigureWidgetNg::slotEditHost()
114{
115 if (!mHostListView->selectionModel()->hasSelection()) {
116 return;
117 }
118 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
119 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
120 KLDAPCore::LdapServer server = modelIndex.data().value<KLDAPCore::LdapServer>();
121 KLDAPWidgets::AddHostDialog dlg(&server, this);
122 dlg.setWindowTitle(i18nc("@title:window", "Edit Host"));
123
124 if (dlg.exec() && !server.host().isEmpty()) {
125 mHostListView->model()->setData(modelIndex, QVariant::fromValue(server));
126 mLdapSortProxyModel->invalidate();
127 Q_EMIT changed(true);
128 }
129}
130void LdapConfigureWidgetNg::slotRemoveHost()
131{
132 if (!mHostListView->selectionModel()->hasSelection()) {
133 return;
134 }
135 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
136 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
137 const KLDAPCore::LdapServer server = modelIndex.data().value<KLDAPCore::LdapServer>();
138 const int answer = KMessageBox::questionTwoActions(this,
139 i18n("Do you want to remove setting for host \"%1\"?", server.host()),
140 i18nc("@title:window", "Remove Host"),
143 if (answer == KMessageBox::SecondaryAction) {
144 return;
145 }
146 mLdapModel->removeServer(index.row());
147 updateButtons();
148 Q_EMIT changed(true);
149}
150
151void LdapConfigureWidgetNg::slotMoveUp()
152{
153 if (!mHostListView->selectionModel()->hasSelection()) {
154 return;
155 }
156 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
157 const int initialRow = index.row();
158
159 if (initialRow == 0) {
160 return;
161 }
162
163 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Index);
164 const QModelIndex previewIndex = mHostListView->model()->index(index.row() - 1, KLDAPCore::LdapModel::Index);
165
166 const int previousValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(previewIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
167 const int currentValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(modelIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
168
169 mHostListView->model()->setData(modelIndex, previousValue);
170 mHostListView->model()->setData(previewIndex, currentValue);
171 mHostListView->sortByColumn(KLDAPCore::LdapModel::Index, Qt::AscendingOrder);
172 updateButtons();
173 Q_EMIT changed(true);
174}
175
176void LdapConfigureWidgetNg::slotMoveDown()
177{
178 if (!mHostListView->selectionModel()->hasSelection()) {
179 return;
180 }
181 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
182 const int initialRow = index.row();
183 if (initialRow >= (mHostListView->model()->rowCount() - 1)) {
184 return;
185 }
186
187 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Index);
188 const QModelIndex nextIndex = mHostListView->model()->index(index.row() + 1, KLDAPCore::LdapModel::Index);
189
190 const int nextValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(nextIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
191 const int currentValue = mLdapSortProxyModel->mapToSource(mLdapSortProxyModel->index(modelIndex.row(), KLDAPCore::LdapModel::Index)).data().toInt();
192
193 mHostListView->model()->setData(modelIndex, nextValue);
194 mHostListView->model()->setData(nextIndex, currentValue);
195 mHostListView->sortByColumn(KLDAPCore::LdapModel::Index, Qt::AscendingOrder);
196 updateButtons();
197 Q_EMIT changed(true);
198}
199
200void LdapConfigureWidgetNg::initGUI()
201{
202 auto mainLayout = new QVBoxLayout(this);
203 mainLayout->setObjectName("layout"_L1);
204
205 mainLayout->addWidget(mLdapOnCurrentActivity);
206
207 connect(mLdapOnCurrentActivity, &QCheckBox::checkStateChanged, this, [this](Qt::CheckState state) {
208 mLdapSortProxyModel->setEnablePlasmaActivities(state == Qt::Checked);
209 });
210 mLdapOnCurrentActivity->setVisible(false);
211
212 // Contents of the QVGroupBox: label and hbox
213 auto label = new QLabel(i18nc("@label:textbox", "Check all servers that should be used:"), this);
214 mainLayout->addWidget(label);
215
216 auto hBox = new QWidget(this);
217 mainLayout->addWidget(hBox);
218
219 auto hBoxHBoxLayout = new QHBoxLayout(hBox);
220 hBoxHBoxLayout->setContentsMargins({});
221 hBoxHBoxLayout->setSpacing(6);
222 // Contents of the hbox: listview and up/down buttons on the right (vbox)
223 mHostListView = new QTreeView(hBox);
224 mHostListView->setAlternatingRowColors(true);
225 // mHostListView->setSelectionMode(SingleSelection);
228 mHostListView->setRootIsDecorated(false);
229 mHostListView->setSortingEnabled(false);
230 mHostListView->header()->setSectionsMovable(false);
232 mHostListView->setModel(mLdapSortProxyModel);
233 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Activities, true);
234 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Index, true);
235 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Server, true);
236 mHostListView->setColumnHidden(KLDAPCore::LdapModel::EnabledActivitiesRole, true);
237 mHostListView->header()->hide();
238
239 hBoxHBoxLayout->addWidget(mHostListView);
240
241 auto upDownBox = new QWidget(hBox);
242 auto upDownBoxVBoxLayout = new QVBoxLayout(upDownBox);
243 upDownBoxVBoxLayout->setContentsMargins({});
244 hBoxHBoxLayout->addWidget(upDownBox);
245 upDownBoxVBoxLayout->setSpacing(6);
246 mUpButton = new QToolButton(upDownBox);
247 upDownBoxVBoxLayout->addWidget(mUpButton);
248 mUpButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
249 mUpButton->setEnabled(false); // b/c no item is selected yet
250
251 mDownButton = new QToolButton(upDownBox);
252 upDownBoxVBoxLayout->addWidget(mDownButton);
253 mDownButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
254 mDownButton->setEnabled(false); // b/c no item is selected yet
255
256 auto spacer = new QWidget(upDownBox);
257 upDownBoxVBoxLayout->addWidget(spacer);
258 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
259
260 auto buttons = new QDialogButtonBox(this);
261 QPushButton *add = buttons->addButton(i18nc("@action:button", "&Add Host…"), QDialogButtonBox::ActionRole);
262 connect(add, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotAddHost);
263 mEditButton = buttons->addButton(i18nc("@action:button", "&Edit Host…"), QDialogButtonBox::ActionRole);
264 connect(mEditButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotEditHost);
265 mEditButton->setEnabled(false);
266 mRemoveButton = buttons->addButton(i18nc("@action:button", "&Remove Host"), QDialogButtonBox::ActionRole);
267 connect(mRemoveButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotRemoveHost);
268 mRemoveButton->setEnabled(false);
269
270 mainLayout->addWidget(buttons);
271}
272
273#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)
KGuiItem add()
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 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.