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

KDE's Doxygen guidelines are available online.