KLdap

ldapclientsearchconfigwriteconfigjob.cpp
1/*
2 * SPDX-FileCopyrightText: 2020-2025 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "ldapclientsearchconfigwriteconfigjob.h"
8#include "ldap_core_debug.h"
9
10#include "kldapcore/ldapdn.h"
11#include <qt6keychain/keychain.h>
12using namespace QKeychain;
13
14using namespace KLDAPCore;
15LdapClientSearchConfigWriteConfigJob::LdapClientSearchConfigWriteConfigJob(QObject *parent)
16 : QObject(parent)
17{
18}
19
20LdapClientSearchConfigWriteConfigJob::~LdapClientSearchConfigWriteConfigJob() = default;
21
22bool LdapClientSearchConfigWriteConfigJob::canStart() const
23{
24 return mServerIndex != -1 && mConfig.isValid();
25}
26
27void LdapClientSearchConfigWriteConfigJob::writeLdapClientConfigFinished()
28{
29 Q_EMIT configSaved();
31}
32
33void LdapClientSearchConfigWriteConfigJob::start()
34{
35 if (!canStart()) {
36 // Failed !
37 writeLdapClientConfigFinished();
38 return;
39 }
40 writeConfig();
41}
42
43bool LdapClientSearchConfigWriteConfigJob::active() const
44{
45 return mActive;
46}
47
48void LdapClientSearchConfigWriteConfigJob::setActive(bool newActive)
49{
50 mActive = newActive;
51}
52
53int LdapClientSearchConfigWriteConfigJob::serverIndex() const
54{
55 return mServerIndex;
56}
57
58void LdapClientSearchConfigWriteConfigJob::setServerIndex(int newServerIndex)
59{
60 mServerIndex = newServerIndex;
61}
62
63KConfigGroup LdapClientSearchConfigWriteConfigJob::config() const
64{
65 return mConfig;
66}
67
68void LdapClientSearchConfigWriteConfigJob::setConfig(const KConfigGroup &newConfig)
69{
70 mConfig = newConfig;
71}
72
73void LdapClientSearchConfigWriteConfigJob::writeConfig()
74{
75 QString prefix;
76 if (mActive) {
77 prefix = QStringLiteral("Selected");
78 }
79
80 mConfig.writeEntry(prefix + QStringLiteral("Host%1").arg(mServerIndex), mServer.host());
81 mConfig.writeEntry(prefix + QStringLiteral("Port%1").arg(mServerIndex), mServer.port());
82 mConfig.writeEntry(prefix + QStringLiteral("Base%1").arg(mServerIndex), mServer.baseDn().toString());
83 mConfig.writeEntry(prefix + QStringLiteral("User%1").arg(mServerIndex), mServer.user());
84 mConfig.writeEntry(prefix + QStringLiteral("Bind%1").arg(mServerIndex), mServer.bindDn());
85
86 const QString passwordEntry = prefix + QStringLiteral("PwdBind%1").arg(mServerIndex);
87 const QString password = mServer.password();
88 if (!password.isEmpty()) {
89 auto writeJob = new WritePasswordJob(QStringLiteral("ldapclient"), this);
90 connect(writeJob, &Job::finished, this, [](QKeychain::Job *baseJob) {
91 if (baseJob->error()) {
92 qCWarning(LDAP_CORE_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
93 }
94 });
95 writeJob->setKey(passwordEntry);
96 writeJob->setTextData(password);
97 writeJob->start();
98 }
99
100 mConfig.writeEntry(prefix + QStringLiteral("TimeLimit%1").arg(mServerIndex), mServer.timeLimit());
101 mConfig.writeEntry(prefix + QStringLiteral("SizeLimit%1").arg(mServerIndex), mServer.sizeLimit());
102 mConfig.writeEntry(prefix + QStringLiteral("PageSize%1").arg(mServerIndex), mServer.pageSize());
103 mConfig.writeEntry(prefix + QStringLiteral("Version%1").arg(mServerIndex), mServer.version());
104 mConfig.writeEntry(prefix + QStringLiteral("Activities%1").arg(mServerIndex), mServer.activities());
105 mConfig.writeEntry(prefix + QStringLiteral("EnabledActivities%1").arg(mServerIndex), mServer.enablePlasmaActivities());
106
107 QString tmp;
108 switch (mServer.security()) {
109 case KLDAPCore::LdapServer::TLS:
110 tmp = QStringLiteral("TLS");
111 break;
112 case KLDAPCore::LdapServer::SSL:
113 tmp = QStringLiteral("SSL");
114 break;
115 default:
116 tmp = QStringLiteral("None");
117 }
118 mConfig.writeEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), tmp);
119 switch (mServer.auth()) {
120 case KLDAPCore::LdapServer::Simple:
121 tmp = QStringLiteral("Simple");
122 break;
123 case KLDAPCore::LdapServer::SASL:
124 tmp = QStringLiteral("SASL");
125 break;
126 default:
127 tmp = QStringLiteral("Anonymous");
128 }
129 mConfig.writeEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), tmp);
130 mConfig.writeEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), mServer.mech());
131 mConfig.writeEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), mServer.filter().trimmed());
132 if (mServer.completionWeight() > -1) {
133 mConfig.writeEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), mServer.completionWeight());
134 }
135}
136
137KLDAPCore::LdapServer LdapClientSearchConfigWriteConfigJob::server() const
138{
139 return mServer;
140}
141
142void LdapClientSearchConfigWriteConfigJob::setServer(const KLDAPCore::LdapServer &server)
143{
144 mServer = server;
145}
146
147#include "moc_ldapclientsearchconfigwriteconfigjob.cpp"
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:20 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.