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 <QHeaderView>
10#include <QLabel>
11#include <QPushButton>
12#include <QToolButton>
13#include <QTreeView>
14#include <QVBoxLayout>
15
16#include <KConfig>
17#include <KConfigGroup>
18#include <KLocalizedString>
19#include <KMessageBox>
20#include <QDialogButtonBox>
21#include <QHBoxLayout>
22
23#include <KLDAPCore/LdapClientSearchConfig>
24#include <KLDAPCore/LdapModel>
25#include <KLDAPCore/LdapServer>
26#include <KLDAPCore/LdapSortProxyModel>
27
28#include "addhostdialog.h"
29
30using namespace KLDAPWidgets;
31using namespace Qt::Literals::StringLiterals;
32
33LdapConfigureWidgetNg::LdapConfigureWidgetNg(QWidget *parent)
34 : QWidget(parent)
35 , mClientSearchConfig(new KLDAPCore::LdapClientSearchConfig)
36 , mLdapModel(new KLDAPCore::LdapModel(this))
37 , mLdapSortProxyModel(new KLDAPCore::LdapSortProxyModel(this))
38{
39 mLdapSortProxyModel->setSourceModel(mLdapModel);
40 initGUI();
41 connect(mHostListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &LdapConfigureWidgetNg::slotSelectionChanged);
42 connect(mHostListView, &QTreeView::doubleClicked, this, &LdapConfigureWidgetNg::slotEditHost);
43 connect(mUpButton, &QToolButton::clicked, this, &LdapConfigureWidgetNg::slotMoveUp);
44 connect(mDownButton, &QToolButton::clicked, this, &LdapConfigureWidgetNg::slotMoveDown);
45}
46
47LdapConfigureWidgetNg::~LdapConfigureWidgetNg()
48{
49 delete mClientSearchConfig;
50}
51
52void LdapConfigureWidgetNg::save()
53{
54 mLdapModel->save();
55}
56
57void LdapConfigureWidgetNg::slotAddHost()
58{
60 KLDAPWidgets::AddHostDialog dlg(&server, this);
61
62 if (dlg.exec() && !server.host().trimmed().isEmpty()) { // krazy:exclude=crashy
63 mLdapModel->insertServer(server);
64 Q_EMIT changed(true);
65 }
66}
67
68void LdapConfigureWidgetNg::slotSelectionChanged()
69{
70 const auto nbItems{mHostListView->selectionModel()->selectedRows().count()};
71 bool state = (nbItems >= 1);
72 mEditButton->setEnabled(state);
73 mRemoveButton->setEnabled(state);
74
75 if (!mHostListView->selectionModel()->hasSelection()) {
76 return;
77 }
78 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
79 const int initialRow = index.row();
80 mUpButton->setEnabled(initialRow != 0);
81 mDownButton->setEnabled(initialRow != (mHostListView->model()->rowCount() - 1));
82}
83
84void LdapConfigureWidgetNg::slotEditHost()
85{
86 if (!mHostListView->selectionModel()->hasSelection()) {
87 return;
88 }
89 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
90 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
92 KLDAPWidgets::AddHostDialog dlg(&server, this);
93 dlg.setWindowTitle(i18nc("@title:window", "Edit Host"));
94
95 if (dlg.exec() && !server.host().isEmpty()) { // krazy:exclude=crashy
96 mHostListView->model()->setData(modelIndex, QVariant::fromValue(server));
97 Q_EMIT changed(true);
98 }
99}
100void LdapConfigureWidgetNg::slotRemoveHost()
101{
102 if (!mHostListView->selectionModel()->hasSelection()) {
103 return;
104 }
105 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
106 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Server);
107 const KLDAPCore::LdapServer server = modelIndex.data().value<KLDAPCore::LdapServer>();
108 const int answer = KMessageBox::questionTwoActions(this,
109 i18n("Do you want to remove setting for host \"%1\"?", server.host()),
110 i18n("Remove Host"),
113 if (answer == KMessageBox::SecondaryAction) {
114 return;
115 }
116 mLdapModel->removeServer(index.row());
117 Q_EMIT changed(true);
118}
119
120void LdapConfigureWidgetNg::slotMoveUp()
121{
122 if (!mHostListView->selectionModel()->hasSelection()) {
123 return;
124 }
125 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
126 const int initialRow = index.row();
127
128 if (initialRow == 0) {
129 return;
130 }
131
132 const QModelIndex modelIndex = mHostListView->model()->index(index.row(), KLDAPCore::LdapModel::Index);
133
134 const QModelIndex previewIndex = mHostListView->model()->index(index.row() - 1, KLDAPCore::LdapModel::Index);
135 mHostListView->model()->setData(modelIndex, initialRow - 1);
136 mHostListView->model()->setData(previewIndex, initialRow);
137 mLdapSortProxyModel->invalidate();
138#if 0
139 mHostListView->setCurrentItem(above);
140 above->setSelected(true);
141#endif
142 Q_EMIT changed(true);
143}
144
145void LdapConfigureWidgetNg::slotMoveDown()
146{
147 if (!mHostListView->selectionModel()->hasSelection()) {
148 return;
149 }
150 const QModelIndex index = mHostListView->selectionModel()->selectedRows().constFirst();
151 const int initialRow = index.row();
152 if (initialRow != (mHostListView->model()->rowCount() - 1)) {
153 return;
154 }
155 // TODO mLdapModel->setData(index, initialRow + 1, KLDAPCore::LdapModel::Index);
156 // TODO next item
157 // const QModelIndex nextIndex = mHostListView->selectionModel()->selectedRows().constFirst();
158 // TODO mLdapModel->setData(nextIndex, initialRow, KLDAPCore::LdapModel::Index);
159
160 mLdapSortProxyModel->invalidate();
161#if 0
162
163 mHostListView->setCurrentItem(below);
164 below->setSelected(true);
165
166#endif
167 Q_EMIT changed(true);
168}
169
170void LdapConfigureWidgetNg::initGUI()
171{
172 auto mainLayout = new QVBoxLayout(this);
173 mainLayout->setObjectName("layout"_L1);
174
175 // Contents of the QVGroupBox: label and hbox
176 auto label = new QLabel(i18nc("@label:textbox", "Check all servers that should be used:"), this);
177 mainLayout->addWidget(label);
178
179 auto hBox = new QWidget(this);
180 mainLayout->addWidget(hBox);
181
182 auto hBoxHBoxLayout = new QHBoxLayout(hBox);
183 hBoxHBoxLayout->setContentsMargins({});
184 hBoxHBoxLayout->setSpacing(6);
185 // Contents of the hbox: listview and up/down buttons on the right (vbox)
186 mHostListView = new QTreeView(hBox);
187 mHostListView->setAlternatingRowColors(true);
188 // mHostListView->setSelectionMode(SingleSelection);
191 mHostListView->setRootIsDecorated(false);
192 mHostListView->setSortingEnabled(false);
193 mHostListView->header()->setSectionsMovable(false);
195 mHostListView->setModel(mLdapSortProxyModel);
196 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Activities, true);
197 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Index, true);
198 mHostListView->setColumnHidden(KLDAPCore::LdapModel::Server, true);
199 mHostListView->header()->hide();
200
201 hBoxHBoxLayout->addWidget(mHostListView);
202
203 auto upDownBox = new QWidget(hBox);
204 auto upDownBoxVBoxLayout = new QVBoxLayout(upDownBox);
205 upDownBoxVBoxLayout->setContentsMargins({});
206 hBoxHBoxLayout->addWidget(upDownBox);
207 upDownBoxVBoxLayout->setSpacing(6);
208 mUpButton = new QToolButton(upDownBox);
209 upDownBoxVBoxLayout->addWidget(mUpButton);
210 mUpButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
211 mUpButton->setEnabled(false); // b/c no item is selected yet
212
213 mDownButton = new QToolButton(upDownBox);
214 upDownBoxVBoxLayout->addWidget(mDownButton);
215 mDownButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
216 mDownButton->setEnabled(false); // b/c no item is selected yet
217
218 auto spacer = new QWidget(upDownBox);
219 upDownBoxVBoxLayout->addWidget(spacer);
220 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
221
222 auto buttons = new QDialogButtonBox(this);
223 QPushButton *add = buttons->addButton(i18nc("@action:button", "&Add Host…"), QDialogButtonBox::ActionRole);
224 connect(add, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotAddHost);
225 mEditButton = buttons->addButton(i18nc("@action:button", "&Edit Host…"), QDialogButtonBox::ActionRole);
226 connect(mEditButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotEditHost);
227 mEditButton->setEnabled(false);
228 mRemoveButton = buttons->addButton(i18nc("@action:button", "&Remove Host"), QDialogButtonBox::ActionRole);
229 connect(mRemoveButton, &QPushButton::clicked, this, &LdapConfigureWidgetNg::slotRemoveHost);
230 mRemoveButton->setEnabled(false);
231
232 mainLayout->addWidget(buttons);
233}
234
235#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
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()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:55:03 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.