Pimcommon

addresseelineeditldap.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "addresseelineeditldap.h"
8#include "addresseelineeditmanager.h"
9#include <KLDAPCore/LdapServer>
10#include <KLDAPWidgets/LdapClient>
11#include <KLDAPWidgets/LdapClientSearch>
12#include <KLocalizedString>
13#include <QTimer>
14using namespace PimCommon;
15
16AddresseeLineEditLdap::AddresseeLineEditLdap(AddresseeLineEditManager *addressLineStatic, QObject *parent)
17 : QObject(parent)
18 , mAddressLineStatic(addressLineStatic)
19{
20}
21
22AddresseeLineEditLdap::~AddresseeLineEditLdap() = default;
23
24void AddresseeLineEditLdap::updateLDAPWeights()
25{
26 /* Add completion sources for all ldap server, 0 to n. Added first so
27 * that they map to the LdapClient::clientNumber() */
28 mLdapSearch->updateCompletionWeights();
29 int clientIndex = 0;
30 const QList<KLDAPWidgets::LdapClient *> lstClients = mLdapSearch->clients();
31 for (const KLDAPWidgets::LdapClient *client : lstClients) {
32 const int sourceIndex = mAddressLineStatic->addCompletionSource(i18n("LDAP server: %1", client->server().host()), client->completionWeight());
33 mLdapClientToCompletionSourceMap.insert(clientIndex, sourceIndex);
34 ++clientIndex;
35 }
36}
37
38QMap<int, int> AddresseeLineEditLdap::ldapClientToCompletionSourceMap() const
39{
40 return mLdapClientToCompletionSourceMap;
41}
42
43int AddresseeLineEditLdap::ldapClientToCompletionSourceValue(int value) const
44{
45 return mLdapClientToCompletionSourceMap[value];
46}
47
48bool AddresseeLineEditLdap::isLdapClientToCompletionSourceMapContains(int value) const
49{
50 return mLdapClientToCompletionSourceMap.contains(value);
51}
52
53KLDAPWidgets::LdapClientSearch *AddresseeLineEditLdap::ldapSearch() const
54{
55 return mLdapSearch;
56}
57
58void AddresseeLineEditLdap::init()
59{
60 if (!mLdapTimer) {
61 mLdapTimer = new QTimer(this);
62 mLdapSearch = new KLDAPWidgets::LdapClientSearch(this);
63
64 /* The reasoning behind this filter is:
65 * If it's a person, or a distlist, show it, even if it doesn't have an email address.
66 * If it's not a person, or a distlist, only show it if it has an email attribute.
67 * This allows both resource accounts with an email address which are not a person and
68 * person entries without an email address to show up, while still not showing things
69 * like structural entries in the ldap tree.
70 */
71
72#if 0
73 mLdapSearch->setFilter(QStringLiteral("&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))"
74 "(|(cn=%1*)(mail=%1*)(mail=*@%1*)(givenName=%1*)(sn=%1*))"));
75#endif
76 // Fix bug 323272 "Exchange doesn't like any queries beginning with *."
77 mLdapSearch->setFilter(
78 QStringLiteral("&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))"
79 "(|(cn=%1*)(mail=%1*)(givenName=%1*)(sn=%1*))"));
80 }
81}
82
83QTimer *AddresseeLineEditLdap::ldapTimer() const
84{
85 return mLdapTimer;
86}
87
88QString AddresseeLineEditLdap::ldapText() const
89{
90 return mLdapText;
91}
92
93void AddresseeLineEditLdap::setLdapText(const QString &ldapText)
94{
95 mLdapText = ldapText;
96}
97
98AddresseeLineEdit *AddresseeLineEditLdap::addressLineEdit() const
99{
100 return mAddressLineEdit;
101}
102
103void AddresseeLineEditLdap::setAddressLineEdit(AddresseeLineEdit *addressLineEdit)
104{
105 mAddressLineEdit = addressLineEdit;
106}
107
108void AddresseeLineEditLdap::startLoadingLDAPEntries()
109{
110 QString text(mLdapText);
111
112 const int index = text.lastIndexOf(QLatin1Char(','));
113 if (index >= 0) {
114 text = text.mid(index + 1, 255).trimmed();
115 }
116
117 if (text.isEmpty()) {
118 return;
119 }
120
121 mLdapSearch->startSearch(text);
122}
123
124void AddresseeLineEditLdap::restartLdap(const QString &searchString, AddresseeLineEdit *addressLine)
125{
126 if (mLdapTimer) {
127 if (mLdapText != searchString || mAddressLineEdit != addressLine) {
128 stopLDAPLookup();
129 }
130
131 mLdapText = searchString;
132 mAddressLineEdit = addressLine;
133 mLdapTimer->setSingleShot(true);
134 mLdapTimer->start(500);
135 }
136}
137
138void AddresseeLineEditLdap::stopLDAPLookup()
139{
140 if (mLdapSearch) {
141 mLdapSearch->cancelSearch();
142 setAddressLineEdit(nullptr);
143 }
144}
void startSearch(const QString &query)
void setFilter(const QString &)
QList< LdapClient * > clients() const
QString i18n(const char *text, const TYPE &arg...)
folderdialogacltab.h
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
void setSingleShot(bool singleShot)
void start()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.