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

KDE's Doxygen guidelines are available online.