File indexing completed on 2025-01-26 04:52:14

0001 /*
0002   This file is part of libkldap.
0003 
0004   SPDX-FileCopyrightText: 2002-2010 Tobias Koenig <tokoe@kde.org>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "addhostdialog.h"
0010 
0011 #include "kldapcore/ldapserver.h"
0012 #include "kldapwidgets/ldapconfigwidget.h"
0013 #include <KAcceleratorManager>
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 #include <KWindowConfig>
0018 #include <QDialogButtonBox>
0019 #include <QHBoxLayout>
0020 #include <QPushButton>
0021 #include <QVBoxLayout>
0022 #include <QWindow>
0023 
0024 using namespace KLDAPWidgets;
0025 namespace
0026 {
0027 static const char myAddHostDialogPrivateGroupName[] = "AddHostDialog";
0028 }
0029 class KLDAPWidgets::AddHostDialogPrivate
0030 {
0031 public:
0032     explicit AddHostDialogPrivate(AddHostDialog *qq)
0033         : q(qq)
0034     {
0035     }
0036 
0037     ~AddHostDialogPrivate()
0038     {
0039     }
0040 
0041     KLDAPWidgets::LdapConfigWidget *mCfg = nullptr;
0042     KLDAPCore::LdapServer *mServer = nullptr;
0043     QPushButton *mOkButton = nullptr;
0044     AddHostDialog *const q;
0045 };
0046 
0047 void AddHostDialog::readConfig()
0048 {
0049     create(); // ensure a window is created
0050     windowHandle()->resize(QSize(600, 400));
0051     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAddHostDialogPrivateGroupName));
0052     KWindowConfig::restoreWindowSize(windowHandle(), group);
0053     resize(windowHandle()->size()); // workaround for QTBUG-40584
0054 }
0055 
0056 void AddHostDialog::writeConfig()
0057 {
0058     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAddHostDialogPrivateGroupName));
0059     KWindowConfig::saveWindowSize(windowHandle(), group);
0060     group.sync();
0061 }
0062 
0063 AddHostDialog::AddHostDialog(KLDAPCore::LdapServer *server, QWidget *parent)
0064     : QDialog(parent)
0065     , d(new KLDAPWidgets::AddHostDialogPrivate(this))
0066 {
0067     setWindowTitle(i18nc("@title:window", "Add Host"));
0068     auto mainLayout = new QVBoxLayout(this);
0069     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0070     d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0071     d->mOkButton->setDefault(true);
0072     d->mOkButton->setShortcut({Qt::CTRL, Qt::Key_Return});
0073     connect(buttonBox, &QDialogButtonBox::rejected, this, &AddHostDialog::reject);
0074 
0075     setModal(true);
0076 
0077     d->mServer = server;
0078 
0079     auto page = new QWidget(this);
0080     mainLayout->addWidget(page);
0081     mainLayout->addWidget(buttonBox);
0082     auto layout = new QHBoxLayout(page);
0083     layout->setContentsMargins(0, 0, 0, 0);
0084 
0085     d->mCfg = new KLDAPWidgets::LdapConfigWidget(
0086         KLDAPWidgets::LdapConfigWidget::W_USER | KLDAPWidgets::LdapConfigWidget::W_PASS | KLDAPWidgets::LdapConfigWidget::W_BINDDN
0087             | KLDAPWidgets::LdapConfigWidget::W_REALM | KLDAPWidgets::LdapConfigWidget::W_HOST | KLDAPWidgets::LdapConfigWidget::W_PORT
0088             | KLDAPWidgets::LdapConfigWidget::W_VER | KLDAPWidgets::LdapConfigWidget::W_TIMELIMIT | KLDAPWidgets::LdapConfigWidget::W_SIZELIMIT
0089             | KLDAPWidgets::LdapConfigWidget::W_PAGESIZE | KLDAPWidgets::LdapConfigWidget::W_DN | KLDAPWidgets::LdapConfigWidget::W_FILTER
0090             | KLDAPWidgets::LdapConfigWidget::W_SECBOX | KLDAPWidgets::LdapConfigWidget::W_AUTHBOX,
0091         page);
0092 
0093     layout->addWidget(d->mCfg);
0094     d->mCfg->setHost(d->mServer->host());
0095     d->mCfg->setPort(d->mServer->port());
0096     d->mCfg->setDn(d->mServer->baseDn());
0097     d->mCfg->setUser(d->mServer->user());
0098     d->mCfg->setBindDn(d->mServer->bindDn());
0099     d->mCfg->setPassword(d->mServer->password());
0100     d->mCfg->setTimeLimit(d->mServer->timeLimit());
0101     d->mCfg->setSizeLimit(d->mServer->sizeLimit());
0102     d->mCfg->setPageSize(d->mServer->pageSize());
0103     d->mCfg->setVersion(d->mServer->version());
0104     d->mCfg->setFilter(d->mServer->filter());
0105     switch (d->mServer->security()) {
0106     case KLDAPCore::LdapServer::TLS:
0107         d->mCfg->setSecurity(KLDAPWidgets::LdapConfigWidget::TLS);
0108         break;
0109     case KLDAPCore::LdapServer::SSL:
0110         d->mCfg->setSecurity(KLDAPWidgets::LdapConfigWidget::SSL);
0111         break;
0112     default:
0113         d->mCfg->setSecurity(KLDAPWidgets::LdapConfigWidget::None);
0114     }
0115 
0116     switch (d->mServer->auth()) {
0117     case KLDAPCore::LdapServer::Simple:
0118         d->mCfg->setAuth(KLDAPWidgets::LdapConfigWidget::Simple);
0119         break;
0120     case KLDAPCore::LdapServer::SASL:
0121         d->mCfg->setAuth(KLDAPWidgets::LdapConfigWidget::SASL);
0122         break;
0123     default:
0124         d->mCfg->setAuth(KLDAPWidgets::LdapConfigWidget::Anonymous);
0125     }
0126     d->mCfg->setMech(d->mServer->mech());
0127 
0128     KAcceleratorManager::manage(this);
0129     connect(d->mCfg, &KLDAPWidgets::LdapConfigWidget::hostNameChanged, this, &AddHostDialog::slotHostEditChanged);
0130     connect(d->mOkButton, &QPushButton::clicked, this, &AddHostDialog::slotOk);
0131     d->mOkButton->setEnabled(!d->mServer->host().isEmpty());
0132     readConfig();
0133 }
0134 
0135 AddHostDialog::~AddHostDialog()
0136 {
0137     writeConfig();
0138 }
0139 
0140 void AddHostDialog::slotHostEditChanged(const QString &text)
0141 {
0142     d->mOkButton->setEnabled(!text.isEmpty());
0143 }
0144 
0145 void AddHostDialog::slotOk()
0146 {
0147     d->mServer->setHost(d->mCfg->host());
0148     d->mServer->setPort(d->mCfg->port());
0149     d->mServer->setBaseDn(d->mCfg->dn());
0150     d->mServer->setUser(d->mCfg->user());
0151     d->mServer->setBindDn(d->mCfg->bindDn());
0152     d->mServer->setPassword(d->mCfg->password());
0153     d->mServer->setTimeLimit(d->mCfg->timeLimit());
0154     d->mServer->setSizeLimit(d->mCfg->sizeLimit());
0155     d->mServer->setPageSize(d->mCfg->pageSize());
0156     d->mServer->setVersion(d->mCfg->version());
0157     d->mServer->setFilter(d->mCfg->filter());
0158     switch (d->mCfg->security()) {
0159     case KLDAPWidgets::LdapConfigWidget::TLS:
0160         d->mServer->setSecurity(KLDAPCore::LdapServer::TLS);
0161         break;
0162     case KLDAPWidgets::LdapConfigWidget::SSL:
0163         d->mServer->setSecurity(KLDAPCore::LdapServer::SSL);
0164         break;
0165     default:
0166         d->mServer->setSecurity(KLDAPCore::LdapServer::None);
0167     }
0168     switch (d->mCfg->auth()) {
0169     case KLDAPWidgets::LdapConfigWidget::Simple:
0170         d->mServer->setAuth(KLDAPCore::LdapServer::Simple);
0171         break;
0172     case KLDAPWidgets::LdapConfigWidget::SASL:
0173         d->mServer->setAuth(KLDAPCore::LdapServer::SASL);
0174         break;
0175     default:
0176         d->mServer->setAuth(KLDAPCore::LdapServer::Anonymous);
0177     }
0178     d->mServer->setMech(d->mCfg->mech());
0179     QDialog::accept();
0180 }
0181 
0182 #include "moc_addhostdialog.cpp"