ldapurl.cpp
00001 /* 00002 This file is part of libkldap. 00003 Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "ldapurl.h" 00022 00023 #include <kdebug.h> 00024 00025 #include <QtCore/QStringList> 00026 00027 using namespace KLDAP; 00028 00029 class LdapUrl::LdapUrlPrivate 00030 { 00031 public: 00032 LdapUrlPrivate() 00033 : m_scope( Base ) 00034 { 00035 } 00036 00037 QMap<QString, Extension> m_extensions; 00038 QStringList m_attributes; 00039 Scope m_scope; 00040 QString m_filter; 00041 }; 00042 00043 LdapUrl::LdapUrl() 00044 : d( new LdapUrlPrivate ) 00045 { 00046 } 00047 00048 LdapUrl::LdapUrl( const KUrl &_url ) 00049 : KUrl( _url ), d( new LdapUrlPrivate ) 00050 { 00051 QString tmp = path(); 00052 if ( tmp.startsWith( '/' ) ) { 00053 tmp = tmp.mid( 1 ); 00054 } 00055 setPath( tmp ); 00056 parseQuery(); 00057 } 00058 00059 LdapUrl::LdapUrl( const LdapUrl &that ) 00060 : KUrl( that ), d( new LdapUrlPrivate ) 00061 { 00062 *d = *that.d; 00063 } 00064 00065 LdapUrl &LdapUrl::operator=( const LdapUrl &that ) 00066 { 00067 if ( this == &that ) { 00068 return *this; 00069 } 00070 00071 KUrl::operator=( that ); 00072 *d = *that.d; 00073 00074 return *this; 00075 } 00076 00077 LdapUrl::~LdapUrl() 00078 { 00079 delete d; 00080 } 00081 00082 void LdapUrl::setDn( const LdapDN &dn ) 00083 { 00084 QString tmp = dn.toString(); 00085 if ( tmp.startsWith( '/' ) ) { 00086 tmp = tmp.mid( 1 ); 00087 } 00088 setPath( tmp ); 00089 } 00090 00091 LdapDN LdapUrl::dn() const 00092 { 00093 QString tmp = path(); 00094 if ( tmp.startsWith( '/' ) ) { 00095 tmp = tmp.mid( 1 ); 00096 } 00097 LdapDN tmpDN( tmp ); 00098 return tmpDN; 00099 } 00100 00101 QStringList LdapUrl::attributes() const 00102 { 00103 return d->m_attributes; 00104 } 00105 00106 void LdapUrl::setAttributes( const QStringList &attributes ) 00107 { 00108 d->m_attributes=attributes; 00109 updateQuery(); 00110 } 00111 00112 LdapUrl::Scope LdapUrl::scope() const 00113 { 00114 return d->m_scope; 00115 } 00116 00117 void LdapUrl::setScope( Scope scope ) 00118 { 00119 d->m_scope = scope; 00120 updateQuery(); 00121 } 00122 00123 QString LdapUrl::filter() const 00124 { 00125 return d->m_filter; 00126 } 00127 00128 void LdapUrl::setFilter( const QString &filter ) 00129 { 00130 d->m_filter = filter; 00131 updateQuery(); 00132 } 00133 00134 bool LdapUrl::hasExtension( const QString &key ) const 00135 { 00136 return d->m_extensions.contains( key ); 00137 } 00138 00139 LdapUrl::Extension LdapUrl::extension( const QString &key ) const 00140 { 00141 QMap<QString, Extension>::const_iterator it; 00142 00143 it = d->m_extensions.constFind( key ); 00144 if ( it != d->m_extensions.constEnd() ) { 00145 return (*it); 00146 } else { 00147 Extension ext; 00148 ext.value = ""; 00149 ext.critical = false; 00150 return ext; 00151 } 00152 } 00153 00154 QString LdapUrl::extension( const QString &key, bool &critical ) const 00155 { 00156 Extension ext; 00157 00158 ext = extension( key ); 00159 critical = ext.critical; 00160 return ext.value; 00161 } 00162 00163 void LdapUrl::setExtension( const QString &key, const LdapUrl::Extension &ext ) 00164 { 00165 d->m_extensions[ key ] = ext; 00166 updateQuery(); 00167 } 00168 00169 void LdapUrl::setExtension( const QString &key, const QString &value, bool critical ) 00170 { 00171 Extension ext; 00172 ext.value = value; 00173 ext.critical = critical; 00174 setExtension( key, ext ); 00175 } 00176 00177 void LdapUrl::setExtension( const QString &key, int value, bool critical ) 00178 { 00179 Extension ext; 00180 ext.value = QString::number( value ); 00181 ext.critical = critical; 00182 setExtension( key, ext ); 00183 } 00184 00185 void LdapUrl::removeExtension( const QString &key ) 00186 { 00187 d->m_extensions.remove( key ); 00188 updateQuery(); 00189 } 00190 00191 void LdapUrl::updateQuery() 00192 { 00193 Extension ext; 00194 QMap<QString, Extension>::const_iterator it; 00195 QString q( '?' ); 00196 00197 // set the attributes to query 00198 if ( d->m_attributes.count() > 0 ) { 00199 q += d->m_attributes.join( "," ); 00200 } 00201 00202 // set the scope 00203 q += '?'; 00204 switch( d->m_scope ) { 00205 case Sub: 00206 q += "sub"; 00207 break; 00208 case One: 00209 q += "one"; 00210 break; 00211 case Base: 00212 q += "base"; 00213 break; 00214 } 00215 00216 // set the filter 00217 q += '?'; 00218 if ( d->m_filter != "(objectClass=*)" && !d->m_filter.isEmpty() ) { 00219 q += toPercentEncoding( d->m_filter ); 00220 } 00221 00222 // set the extensions 00223 q += '?'; 00224 for ( it = d->m_extensions.constBegin(); it != d->m_extensions.constEnd(); ++it ) { 00225 if ( it.value().critical ) { 00226 q += '!'; 00227 } 00228 q += it.key(); 00229 if ( !it.value().value.isEmpty() ) { 00230 q += '=' + toPercentEncoding( it.value().value ); 00231 } 00232 q += ','; 00233 } 00234 while ( q.endsWith( '?' ) || q.endsWith( ',' ) ) { 00235 q.remove( q.length() - 1, 1 ); 00236 } 00237 00238 setQuery( q ); 00239 kDebug() << "LDAP URL updateQuery():" << prettyUrl(); 00240 } 00241 00242 void LdapUrl::parseQuery() 00243 { 00244 Extension ext; 00245 QStringList extensions; 00246 QString q = query(); 00247 // remove first ? 00248 if ( q.startsWith( '?' ) ) { 00249 q.remove( 0, 1 ); 00250 } 00251 00252 // split into a list 00253 QStringList url_items = q.split( '?' ); 00254 00255 d->m_attributes.clear(); 00256 d->m_scope = Base; 00257 d->m_filter = "(objectClass=*)"; 00258 d->m_extensions.clear(); 00259 00260 int i = 0; 00261 QStringList::const_iterator end( url_items.constEnd() ); 00262 for ( QStringList::const_iterator it=url_items.constBegin(); 00263 it != end; ++it, i++ ) { 00264 switch ( i ) { 00265 case 0: 00266 d->m_attributes = (*it).split( ',', QString::SkipEmptyParts ); 00267 break; 00268 case 1: 00269 if ( (*it) == QLatin1String( "sub" ) ) { 00270 d->m_scope = Sub; 00271 } else if ( (*it) == QLatin1String( "one" ) ) { 00272 d->m_scope = One; 00273 } 00274 break; 00275 case 2: 00276 d->m_filter = fromPercentEncoding( (*it).toLatin1() ); 00277 break; 00278 case 3: 00279 extensions = (*it).split( ',', QString::SkipEmptyParts ); 00280 break; 00281 } 00282 } 00283 00284 QString name, value; 00285 QStringList::const_iterator end2( extensions.constEnd() ); 00286 for ( QStringList::const_iterator it=extensions.constBegin(); 00287 it != end2; ++it ) { 00288 ext.critical = false; 00289 name = fromPercentEncoding( (*it).section( '=', 0, 0 ).toLatin1() ).toLower(); 00290 value = fromPercentEncoding( (*it).section( '=', 1 ).toLatin1() ); 00291 if ( name.startsWith( '!' ) ) { 00292 ext.critical = true; 00293 name.remove( 0, 1 ); 00294 } 00295 kDebug() << "LdapUrl extensions name=" << name << "value:" << value; 00296 ext.value = value.replace( "%2", "," ); 00297 setExtension( name, ext ); 00298 } 00299 }