KLdap

ldapurl.cpp
1/*
2 This file is part of libkldap.
3 SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "ldapurl.h"
9
10#include "ldap_core_debug.h"
11
12using namespace KLDAPCore;
13
14class Q_DECL_HIDDEN LdapUrl::LdapUrlPrivate
15{
16public:
17 LdapUrlPrivate()
18 : m_scope(Base)
19 {
20 }
21
22 QMap<QString, Extension> m_extensions;
23 QStringList m_attributes;
24 Scope m_scope;
25 QString m_filter;
26};
27
29 : d(new LdapUrlPrivate)
30{
31}
32
34 : QUrl(_url)
35 , d(new LdapUrlPrivate)
36{
37 parseQuery();
38}
39
41 : QUrl(that)
42 , d(new LdapUrlPrivate)
43{
44 *d = *that.d;
45}
46
48{
49 if (this == &that) {
50 return *this;
51 }
52
53 QUrl::operator=(that);
54 *d = *that.d;
55
56 return *this;
57}
58
59LdapUrl::~LdapUrl() = default;
60
61void LdapUrl::setDn(const LdapDN &dn)
62{
63 const QString tmp = dn.toString();
64 if (tmp.startsWith(QLatin1Char('/'))) {
65 setPath(tmp);
66 } else {
67 setPath(QLatin1Char('/') + tmp);
68 }
69}
70
71LdapDN LdapUrl::dn() const
72{
73 QString tmp = path();
74 if (tmp.startsWith(QLatin1Char('/'))) {
75 tmp = tmp.mid(1);
76 }
77 const LdapDN tmpDN(tmp);
78 return tmpDN;
79}
80
82{
83 return d->m_attributes;
84}
85
86void LdapUrl::setAttributes(const QStringList &attributes)
87{
88 d->m_attributes = attributes;
90}
91
93{
94 return d->m_scope;
95}
96
98{
99 d->m_scope = scope;
100 updateQuery();
101}
102
104{
105 return d->m_filter;
106}
107
108void LdapUrl::setFilter(const QString &filter)
109{
110 d->m_filter = filter;
111 updateQuery();
112}
113
114bool LdapUrl::hasExtension(const QString &key) const
115{
116 return d->m_extensions.contains(key);
117}
118
120{
122
123 it = d->m_extensions.constFind(key);
124 if (it != d->m_extensions.constEnd()) {
125 return *it;
126 } else {
127 Extension ext;
128 ext.value = QLatin1StringView("");
129 ext.critical = false;
130 return ext;
131 }
132}
133
134QString LdapUrl::extension(const QString &key, bool &critical) const
135{
136 const Extension ext = extension(key);
137 critical = ext.critical;
138 return ext.value;
139}
140
141void LdapUrl::setExtension(const QString &key, const LdapUrl::Extension &ext)
142{
143 d->m_extensions[key] = ext;
144 updateQuery();
145}
146
147void LdapUrl::setExtension(const QString &key, const QString &value, bool critical)
148{
149 Extension ext;
150 ext.value = value;
151 ext.critical = critical;
152 setExtension(key, ext);
153}
154
155void LdapUrl::setExtension(const QString &key, int value, bool critical)
156{
157 Extension ext;
158 ext.value = QString::number(value);
159 ext.critical = critical;
160 setExtension(key, ext);
161}
162
164{
165 d->m_extensions.remove(key);
166 updateQuery();
167}
168
170{
172 QString q(QLatin1Char('?'));
173
174 // set the attributes to query
175 if (!d->m_attributes.isEmpty()) {
176 q += d->m_attributes.join(QLatin1Char(','));
177 }
178
179 // set the scope
180 q += QLatin1Char('?');
181 switch (d->m_scope) {
182 case Sub:
183 q += QStringLiteral("sub");
184 break;
185 case One:
186 q += QStringLiteral("one");
187 break;
188 case Base:
189 q += QStringLiteral("base");
190 break;
191 }
192
193 // set the filter
194 q += QLatin1Char('?');
195 if (d->m_filter != QLatin1StringView("(objectClass=*)") && !d->m_filter.isEmpty()) {
196 q += QLatin1StringView(toPercentEncoding(d->m_filter));
197 }
198
199 // set the extensions
200 q += QLatin1Char('?');
201 for (it = d->m_extensions.constBegin(); it != d->m_extensions.constEnd(); ++it) {
202 if (it.value().critical) {
203 q += QLatin1Char('!');
204 }
205 q += it.key();
206 if (!it.value().value.isEmpty()) {
207 q += QLatin1Char('=') + QLatin1StringView(toPercentEncoding(it.value().value));
208 }
209 q += QLatin1Char(',');
210 }
211 while (q.endsWith(QLatin1Char('?')) || q.endsWith(QLatin1Char(','))) {
212 q.remove(q.length() - 1, 1);
213 }
214
215 setQuery(q);
216 qCDebug(LDAP_LOG) << "LDAP URL updateQuery():" << toDisplayString();
217}
218
220{
221 Extension ext;
222 QStringList extensions;
224 // remove first ?
225 if (q.startsWith(QLatin1Char('?'))) {
226 q.remove(0, 1);
227 }
228
229 // split into a list
230 const QStringList url_items = q.split(QLatin1Char('?'));
231
232 d->m_attributes.clear();
233 d->m_scope = Base;
234 d->m_filter = QStringLiteral("(objectClass=*)");
235 d->m_extensions.clear();
236
237 int i = 0;
238 QStringList::const_iterator end(url_items.constEnd());
239 for (QStringList::const_iterator it = url_items.constBegin(); it != end; ++it, i++) {
240 switch (i) {
241 case 0:
242 d->m_attributes = (*it).split(QLatin1Char(','), Qt::SkipEmptyParts);
243 break;
244 case 1:
245 if ((*it) == QLatin1StringView("sub")) {
246 d->m_scope = Sub;
247 } else if ((*it) == QLatin1StringView("one")) {
248 d->m_scope = One;
249 }
250 break;
251 case 2:
252 d->m_filter = fromPercentEncoding((*it).toLatin1());
253 break;
254 case 3:
255 extensions = (*it).split(QLatin1Char(','), Qt::SkipEmptyParts);
256 break;
257 }
258 }
259
260 QString name;
261 QString value;
262 QStringList::const_iterator end2(extensions.constEnd());
263 for (QStringList::const_iterator it = extensions.constBegin(); it != end2; ++it) {
264 ext.critical = false;
265 name = fromPercentEncoding((*it).section(QLatin1Char('='), 0, 0).toLatin1()).toLower();
266 value = fromPercentEncoding((*it).section(QLatin1Char('='), 1).toLatin1());
267 if (name.startsWith(QLatin1Char('!'))) {
268 ext.critical = true;
269 name.remove(0, 1);
270 }
271 qCDebug(LDAP_LOG) << "LdapUrl extensions name=" << name << "value:" << value;
272 ext.value = value.replace(QLatin1StringView("%2"), QLatin1StringView(","));
273 setExtension(name, ext);
274 }
275}
A special url class for LDAP.
Definition ldapurl.h:30
void setFilter(const QString &filter)
Sets the filter part of the LDAP url.
Definition ldapurl.cpp:108
void setExtension(const QString &key, const Extension &extension)
Sets the specified extension key with the value and criticality in extension.
void setScope(Scope scope)
Sets the scope part of the LDAP url.
Definition ldapurl.cpp:97
LdapUrl & operator=(const LdapUrl &other)
Overwrites the values of the LDAP url with values from an other url.
Definition ldapurl.cpp:47
Scope scope() const
Returns the scope part of the LDAP url.
Definition ldapurl.cpp:92
struct { QString value; bool critical; } Extension
A class holding the extension name and state whether the extension is critical.
Definition ldapurl.h:36
~LdapUrl()
Destroys the LDAP url.
LdapDN dn() const
Returns the dn part of the LDAP url.
Definition ldapurl.cpp:71
void setAttributes(const QStringList &attributes)
Sets the attributes part of the LDAP url.
Definition ldapurl.cpp:86
enum { Base, One, Sub } Scope
Describes the scope of the LDAP url.
Definition ldapurl.h:44
QStringList attributes() const
Returns the attributes part of the LDAP url.
Definition ldapurl.cpp:81
QString filter() const
Returns the filter part of the LDAP url.
Definition ldapurl.cpp:103
void setDn(const LdapDN &dn)
Sets the dn part of the LDAP url.
Definition ldapurl.cpp:61
LdapUrl()
Constructs an empty LDAP url.
Definition ldapurl.cpp:28
void updateQuery()
Updates the query component from the attributes, scope, filter and extensions.
Definition ldapurl.cpp:169
bool hasExtension(const QString &extension) const
Returns whether the specified extension exists in the LDAP url.
Definition ldapurl.cpp:114
Extension extension(const QString &extension) const
Returns the specified extension.
Definition ldapurl.cpp:119
void removeExtension(const QString &extension)
Removes the specified extension.
Definition ldapurl.cpp:163
void parseQuery()
Parses the query argument of the URL and makes it available via the attributes(), extension(),...
Definition ldapurl.cpp:219
const_iterator constBegin() const const
const_iterator constEnd() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
SkipEmptyParts
FullyEncoded
QString fromPercentEncoding(const QByteArray &input)
QUrl & operator=(QUrl &&other)
QString path(ComponentFormattingOptions options) const const
QString query(ComponentFormattingOptions options) const const
void setPath(const QString &path, ParsingMode mode)
void setQuery(const QString &query, ParsingMode mode)
QString toDisplayString(FormattingOptions options) const const
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.