00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00198 if ( d->m_attributes.count() > 0 ) {
00199 q += d->m_attributes.join( "," );
00200 }
00201
00202
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
00217 q += '?';
00218 if ( d->m_filter != "(objectClass=*)" && !d->m_filter.isEmpty() ) {
00219 q += toPercentEncoding( d->m_filter );
00220 }
00221
00222
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
00248 if ( q.startsWith( '?' ) ) {
00249 q.remove( 0, 1 );
00250 }
00251
00252
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 for ( QStringList::const_iterator it=url_items.constBegin();
00262 it != url_items.constEnd(); ++it, i++ ) {
00263 switch ( i ) {
00264 case 0:
00265 d->m_attributes = (*it).split( ',', QString::SkipEmptyParts );
00266 break;
00267 case 1:
00268 if ( (*it) == "sub" ) {
00269 d->m_scope = Sub;
00270 } else if ( (*it) == "one" ) {
00271 d->m_scope = One;
00272 }
00273 break;
00274 case 2:
00275 d->m_filter = fromPercentEncoding( (*it).toLatin1() );
00276 break;
00277 case 3:
00278 extensions = (*it).split( ',', QString::SkipEmptyParts );
00279 break;
00280 }
00281 }
00282
00283 QString name, value;
00284 for ( QStringList::const_iterator it=extensions.constBegin();
00285 it != extensions.constEnd(); ++it ) {
00286 ext.critical = false;
00287 name = fromPercentEncoding( (*it).section( '=', 0, 0 ).toLatin1() ).toLower();
00288 value = fromPercentEncoding( (*it).section( '=', 1 ).toLatin1() );
00289 if ( name.startsWith( '!' ) ) {
00290 ext.critical = true;
00291 name.remove( 0, 1 );
00292 }
00293 kDebug() << "LdapUrl extensions name=" << name << "value:" << value;
00294 ext.value = value.replace( "%2", "," );
00295 setExtension( name, ext );
00296 }
00297 }