• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

KLDAP Library

  • kldap
ldapurl.cpp
1 /*
2  This file is part of libkldap.
3  Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "ldapurl.h"
22 
23 #include <kdebug.h>
24 
25 #include <QtCore/QStringList>
26 
27 using namespace KLDAP;
28 
29 class LdapUrl::LdapUrlPrivate
30 {
31  public:
32  LdapUrlPrivate()
33  : m_scope( Base )
34  {
35  }
36 
37  QMap<QString, Extension> m_extensions;
38  QStringList m_attributes;
39  Scope m_scope;
40  QString m_filter;
41 };
42 
43 LdapUrl::LdapUrl()
44  : d( new LdapUrlPrivate )
45 {
46 }
47 
48 LdapUrl::LdapUrl( const KUrl &_url )
49  : KUrl( _url ), d( new LdapUrlPrivate )
50 {
51  QString tmp = path();
52  if ( tmp.startsWith( '/' ) ) {
53  tmp = tmp.mid( 1 );
54  }
55  setPath( tmp );
56  parseQuery();
57 }
58 
59 LdapUrl::LdapUrl( const LdapUrl &that )
60  : KUrl( that ), d( new LdapUrlPrivate )
61 {
62  *d = *that.d;
63 }
64 
65 LdapUrl &LdapUrl::operator=( const LdapUrl &that )
66 {
67  if ( this == &that ) {
68  return *this;
69  }
70 
71  KUrl::operator=( that );
72  *d = *that.d;
73 
74  return *this;
75 }
76 
77 LdapUrl::~LdapUrl()
78 {
79  delete d;
80 }
81 
82 void LdapUrl::setDn( const LdapDN &dn )
83 {
84  QString tmp = dn.toString();
85  if ( tmp.startsWith( '/' ) ) {
86  tmp = tmp.mid( 1 );
87  }
88  setPath( tmp );
89 }
90 
91 LdapDN LdapUrl::dn() const
92 {
93  QString tmp = path();
94  if ( tmp.startsWith( '/' ) ) {
95  tmp = tmp.mid( 1 );
96  }
97  LdapDN tmpDN( tmp );
98  return tmpDN;
99 }
100 
101 QStringList LdapUrl::attributes() const
102 {
103  return d->m_attributes;
104 }
105 
106 void LdapUrl::setAttributes( const QStringList &attributes )
107 {
108  d->m_attributes=attributes;
109  updateQuery();
110 }
111 
112 LdapUrl::Scope LdapUrl::scope() const
113 {
114  return d->m_scope;
115 }
116 
117 void LdapUrl::setScope( Scope scope )
118 {
119  d->m_scope = scope;
120  updateQuery();
121 }
122 
123 QString LdapUrl::filter() const
124 {
125  return d->m_filter;
126 }
127 
128 void LdapUrl::setFilter( const QString &filter )
129 {
130  d->m_filter = filter;
131  updateQuery();
132 }
133 
134 bool LdapUrl::hasExtension( const QString &key ) const
135 {
136  return d->m_extensions.contains( key );
137 }
138 
139 LdapUrl::Extension LdapUrl::extension( const QString &key ) const
140 {
141  QMap<QString, Extension>::const_iterator it;
142 
143  it = d->m_extensions.constFind( key );
144  if ( it != d->m_extensions.constEnd() ) {
145  return ( *it );
146  } else {
147  Extension ext;
148  ext.value = "";
149  ext.critical = false;
150  return ext;
151  }
152 }
153 
154 QString LdapUrl::extension( const QString &key, bool &critical ) const
155 {
156  Extension ext;
157 
158  ext = extension( key );
159  critical = ext.critical;
160  return ext.value;
161 }
162 
163 void LdapUrl::setExtension( const QString &key, const LdapUrl::Extension &ext )
164 {
165  d->m_extensions[ key ] = ext;
166  updateQuery();
167 }
168 
169 void LdapUrl::setExtension( const QString &key, const QString &value, bool critical )
170 {
171  Extension ext;
172  ext.value = value;
173  ext.critical = critical;
174  setExtension( key, ext );
175 }
176 
177 void LdapUrl::setExtension( const QString &key, int value, bool critical )
178 {
179  Extension ext;
180  ext.value = QString::number( value );
181  ext.critical = critical;
182  setExtension( key, ext );
183 }
184 
185 void LdapUrl::removeExtension( const QString &key )
186 {
187  d->m_extensions.remove( key );
188  updateQuery();
189 }
190 
191 void LdapUrl::updateQuery()
192 {
193  Extension ext;
194  QMap<QString, Extension>::const_iterator it;
195  QString q( '?' );
196 
197  // set the attributes to query
198  if ( d->m_attributes.count() > 0 ) {
199  q += d->m_attributes.join( "," );
200  }
201 
202  // set the scope
203  q += '?';
204  switch ( d->m_scope ) {
205  case Sub:
206  q += "sub";
207  break;
208  case One:
209  q += "one";
210  break;
211  case Base:
212  q += "base";
213  break;
214  }
215 
216  // set the filter
217  q += '?';
218  if ( d->m_filter != "(objectClass=*)" && !d->m_filter.isEmpty() ) {
219  q += toPercentEncoding( d->m_filter );
220  }
221 
222  // set the extensions
223  q += '?';
224  for ( it = d->m_extensions.constBegin(); it != d->m_extensions.constEnd(); ++it ) {
225  if ( it.value().critical ) {
226  q += '!';
227  }
228  q += it.key();
229  if ( !it.value().value.isEmpty() ) {
230  q += '=' + toPercentEncoding( it.value().value );
231  }
232  q += ',';
233  }
234  while ( q.endsWith( '?' ) || q.endsWith( ',' ) ) {
235  q.remove( q.length() - 1, 1 );
236  }
237 
238  setQuery( q );
239  kDebug() << "LDAP URL updateQuery():" << prettyUrl();
240 }
241 
242 void LdapUrl::parseQuery()
243 {
244  Extension ext;
245  QStringList extensions;
246  QString q = query();
247  // remove first ?
248  if ( q.startsWith( '?' ) ) {
249  q.remove( 0, 1 );
250  }
251 
252  // split into a list
253  QStringList url_items = q.split( '?' );
254 
255  d->m_attributes.clear();
256  d->m_scope = Base;
257  d->m_filter = "(objectClass=*)";
258  d->m_extensions.clear();
259 
260  int i = 0;
261  QStringList::const_iterator end( url_items.constEnd() );
262  for ( QStringList::const_iterator it=url_items.constBegin();
263  it != end; ++it, i++ ) {
264  switch ( i ) {
265  case 0:
266  d->m_attributes = ( *it ).split( ',', QString::SkipEmptyParts );
267  break;
268  case 1:
269  if ( ( *it ) == QLatin1String( "sub" ) ) {
270  d->m_scope = Sub;
271  } else if ( ( *it ) == QLatin1String( "one" ) ) {
272  d->m_scope = One;
273  }
274  break;
275  case 2:
276  d->m_filter = fromPercentEncoding( ( *it ).toLatin1() );
277  break;
278  case 3:
279  extensions = ( *it ).split( ',', QString::SkipEmptyParts );
280  break;
281  }
282  }
283 
284  QString name, value;
285  QStringList::const_iterator end2( extensions.constEnd() );
286  for ( QStringList::const_iterator it=extensions.constBegin();
287  it != end2; ++it ) {
288  ext.critical = false;
289  name = fromPercentEncoding( ( *it ).section( '=', 0, 0 ).toLatin1() ).toLower();
290  value = fromPercentEncoding( ( *it ).section( '=', 1 ).toLatin1() );
291  if ( name.startsWith( '!' ) ) {
292  ext.critical = true;
293  name.remove( 0, 1 );
294  }
295  kDebug() << "LdapUrl extensions name=" << name << "value:" << value;
296  ext.value = value.replace( "%2", "," );
297  setExtension( name, ext );
298  }
299 }
KLDAP::LdapUrl::hasExtension
bool hasExtension(const QString &extension) const
Returns whether the specified extension exists in the LDAP url.
Definition: ldapurl.cpp:134
KLDAP::LdapUrl::operator=
LdapUrl & operator=(const LdapUrl &other)
Overwrites the values of the LDAP url with values from an other url.
Definition: ldapurl.cpp:65
KLDAP::LdapUrl::Sub
All levels below the url&#39;s level.
Definition: ldapurl.h:61
KLDAP::LdapUrl::filter
QString filter() const
Returns the filter part of the LDAP url.
Definition: ldapurl.cpp:123
KLDAP::LdapUrl::setScope
void setScope(Scope scope)
Sets the scope part of the LDAP url.
Definition: ldapurl.cpp:117
KLDAP::LdapUrl::attributes
QStringList attributes() const
Returns the attributes part of the LDAP url.
Definition: ldapurl.cpp:101
KLDAP::LdapUrl
A special url class for LDAP.
Definition: ldapurl.h:42
KLDAP::LdapUrl::setDn
void setDn(const LdapDN &dn)
Sets the dn part of the LDAP url.
Definition: ldapurl.cpp:82
KLDAP::LdapUrl::Extension
A class holding the extension name and state whether the extension is critical.
Definition: ldapurl.h:50
KLDAP::LdapUrl::setAttributes
void setAttributes(const QStringList &attributes)
Sets the attributes part of the LDAP url.
Definition: ldapurl.cpp:106
KLDAP::LdapUrl::~LdapUrl
virtual ~LdapUrl()
Destroys the LDAP url.
Definition: ldapurl.cpp:77
KLDAP::LdapUrl::Scope
Scope
Describes the scope of the LDAP url.
Definition: ldapurl.h:58
KLDAP::LdapUrl::setFilter
void setFilter(const QString &filter)
Sets the filter part of the LDAP url.
Definition: ldapurl.cpp:128
KLDAP::LdapUrl::scope
Scope scope() const
Returns the scope part of the LDAP url.
Definition: ldapurl.cpp:112
KLDAP::LdapUrl::LdapUrl
LdapUrl()
Constructs an empty LDAP url.
Definition: ldapurl.cpp:43
KLDAP::LdapUrl::One
The level of the url and the one below.
Definition: ldapurl.h:60
KLDAP::LdapUrl::removeExtension
void removeExtension(const QString &extension)
Removes the specified extension.
Definition: ldapurl.cpp:185
KLDAP::LdapUrl::updateQuery
void updateQuery()
Updates the query component from the attributes, scope, filter and extensions.
Definition: ldapurl.cpp:191
KLDAP::LdapUrl::setExtension
void setExtension(const QString &key, const Extension &extension)
Sets the specified extension key with the value and criticality in extension.
Definition: ldapurl.cpp:163
KLDAP::LdapUrl::parseQuery
void parseQuery()
Parses the query argument of the URL and makes it available via the attributes(), extension()...
Definition: ldapurl.cpp:242
KLDAP::LdapUrl::Base
Only the same level as the url.
Definition: ldapurl.h:59
KLDAP::LdapUrl::dn
LdapDN dn() const
Returns the dn part of the LDAP url.
Definition: ldapurl.cpp:91
KLDAP::LdapUrl::extension
Extension extension(const QString &extension) const
Returns the specified extension.
Definition: ldapurl.cpp:139
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:09 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KLDAP Library

Skip menu "KLDAP Library"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal