File indexing completed on 2025-01-05 04:58:19

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "addresseelineeditldap.h"
0008 #include "addresseelineeditmanager.h"
0009 #include <KLDAPCore/LdapServer>
0010 #include <KLDAPWidgets/LdapClient>
0011 #include <KLDAPWidgets/LdapClientSearch>
0012 #include <KLocalizedString>
0013 #include <QTimer>
0014 using namespace PimCommon;
0015 
0016 AddresseeLineEditLdap::AddresseeLineEditLdap(AddresseeLineEditManager *addressLineStatic, QObject *parent)
0017     : QObject(parent)
0018     , mAddressLineStatic(addressLineStatic)
0019 {
0020 }
0021 
0022 AddresseeLineEditLdap::~AddresseeLineEditLdap() = default;
0023 
0024 void AddresseeLineEditLdap::updateLDAPWeights()
0025 {
0026     /* Add completion sources for all ldap server, 0 to n. Added first so
0027      * that they map to the LdapClient::clientNumber() */
0028     mLdapSearch->updateCompletionWeights();
0029     int clientIndex = 0;
0030     const QList<KLDAPWidgets::LdapClient *> lstClients = mLdapSearch->clients();
0031     for (const KLDAPWidgets::LdapClient *client : lstClients) {
0032         const int sourceIndex = mAddressLineStatic->addCompletionSource(i18n("LDAP server: %1", client->server().host()), client->completionWeight());
0033         mLdapClientToCompletionSourceMap.insert(clientIndex, sourceIndex);
0034         ++clientIndex;
0035     }
0036 }
0037 
0038 QMap<int, int> AddresseeLineEditLdap::ldapClientToCompletionSourceMap() const
0039 {
0040     return mLdapClientToCompletionSourceMap;
0041 }
0042 
0043 int AddresseeLineEditLdap::ldapClientToCompletionSourceValue(int value) const
0044 {
0045     return mLdapClientToCompletionSourceMap[value];
0046 }
0047 
0048 bool AddresseeLineEditLdap::isLdapClientToCompletionSourceMapContains(int value) const
0049 {
0050     return mLdapClientToCompletionSourceMap.contains(value);
0051 }
0052 
0053 KLDAPWidgets::LdapClientSearch *AddresseeLineEditLdap::ldapSearch() const
0054 {
0055     return mLdapSearch;
0056 }
0057 
0058 void AddresseeLineEditLdap::init()
0059 {
0060     if (!mLdapTimer) {
0061         mLdapTimer = new QTimer(this);
0062         mLdapSearch = new KLDAPWidgets::LdapClientSearch(this);
0063 
0064         /* The reasoning behind this filter is:
0065          * If it's a person, or a distlist, show it, even if it doesn't have an email address.
0066          * If it's not a person, or a distlist, only show it if it has an email attribute.
0067          * This allows both resource accounts with an email address which are not a person and
0068          * person entries without an email address to show up, while still not showing things
0069          * like structural entries in the ldap tree.
0070          */
0071 
0072 #if 0
0073         mLdapSearch->setFilter(QStringLiteral("&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))"
0074                                               "(|(cn=%1*)(mail=%1*)(mail=*@%1*)(givenName=%1*)(sn=%1*))"));
0075 #endif
0076         // Fix bug 323272 "Exchange doesn't like any queries beginning with *."
0077         mLdapSearch->setFilter(
0078             QStringLiteral("&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))"
0079                            "(|(cn=%1*)(mail=%1*)(givenName=%1*)(sn=%1*))"));
0080     }
0081 }
0082 
0083 QTimer *AddresseeLineEditLdap::ldapTimer() const
0084 {
0085     return mLdapTimer;
0086 }
0087 
0088 QString AddresseeLineEditLdap::ldapText() const
0089 {
0090     return mLdapText;
0091 }
0092 
0093 void AddresseeLineEditLdap::setLdapText(const QString &ldapText)
0094 {
0095     mLdapText = ldapText;
0096 }
0097 
0098 AddresseeLineEdit *AddresseeLineEditLdap::addressLineEdit() const
0099 {
0100     return mAddressLineEdit;
0101 }
0102 
0103 void AddresseeLineEditLdap::setAddressLineEdit(AddresseeLineEdit *addressLineEdit)
0104 {
0105     mAddressLineEdit = addressLineEdit;
0106 }
0107 
0108 void AddresseeLineEditLdap::startLoadingLDAPEntries()
0109 {
0110     QString text(mLdapText);
0111 
0112     const int index = text.lastIndexOf(QLatin1Char(','));
0113     if (index >= 0) {
0114         text = text.mid(index + 1, 255).trimmed();
0115     }
0116 
0117     if (text.isEmpty()) {
0118         return;
0119     }
0120 
0121     mLdapSearch->startSearch(text);
0122 }
0123 
0124 void AddresseeLineEditLdap::restartLdap(const QString &searchString, AddresseeLineEdit *addressLine)
0125 {
0126     if (mLdapTimer) {
0127         if (mLdapText != searchString || mAddressLineEdit != addressLine) {
0128             stopLDAPLookup();
0129         }
0130 
0131         mLdapText = searchString;
0132         mAddressLineEdit = addressLine;
0133         mLdapTimer->setSingleShot(true);
0134         mLdapTimer->start(500);
0135     }
0136 }
0137 
0138 void AddresseeLineEditLdap::stopLDAPLookup()
0139 {
0140     if (mLdapSearch) {
0141         mLdapSearch->cancelSearch();
0142         setAddressLineEdit(nullptr);
0143     }
0144 }