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

KDE's Doxygen guidelines are available online.