KLDAP Library
ldapurl.cpp
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/QDir>
00026 #include <QtCore/QStringList>
00027
00028 using namespace KLDAP;
00029
00030 class LdapUrl::LdapUrlPrivate
00031 {
00032 public:
00033 LdapUrlPrivate()
00034 : m_scope( Base )
00035 {
00036 }
00037
00038 QMap<QString, Extension> m_extensions;
00039 QStringList m_attributes;
00040 Scope m_scope;
00041 QString m_filter;
00042 };
00043
00044 LdapUrl::LdapUrl()
00045 : d( new LdapUrlPrivate )
00046 {
00047 }
00048
00049 LdapUrl::LdapUrl( const KUrl &_url )
00050 : KUrl( _url ), d( new LdapUrlPrivate )
00051 {
00052 QString tmp = path();
00053 if ( !QDir::isRelativePath( tmp ) ) {
00054 tmp.remove( 0, QDir::rootPath().length() );
00055 }
00056 setPath( tmp );
00057 parseQuery();
00058 }
00059
00060 LdapUrl::LdapUrl( const LdapUrl &that )
00061 : KUrl( that ), d( new LdapUrlPrivate )
00062 {
00063 *d = *that.d;
00064 }
00065
00066 LdapUrl &LdapUrl::operator=( const LdapUrl &that )
00067 {
00068 if ( this == &that ) {
00069 return *this;
00070 }
00071
00072 KUrl::operator=( that );
00073 *d = *that.d;
00074
00075 return *this;
00076 }
00077
00078 LdapUrl::~LdapUrl()
00079 {
00080 delete d;
00081 }
00082
00083 void LdapUrl::setDn( const LdapDN &dn )
00084 {
00085 QString tmp = dn.toString();
00086 if ( !QDir::isRelativePath( tmp ) ) {
00087 tmp.remove( 0, QDir::rootPath().length() );
00088 }
00089 setPath( tmp );
00090 }
00091
00092 LdapDN LdapUrl::dn() const
00093 {
00094 QString tmp = path();
00095 if ( !QDir::isRelativePath( tmp ) ) {
00096 tmp.remove( 0, QDir::rootPath().length() );
00097 }
00098 LdapDN tmpDN( tmp );
00099 return tmpDN;
00100 }
00101
00102 QStringList LdapUrl::attributes() const
00103 {
00104 return d->m_attributes;
00105 }
00106
00107 void LdapUrl::setAttributes( const QStringList &attributes )
00108 {
00109 d->m_attributes=attributes;
00110 updateQuery();
00111 }
00112
00113 LdapUrl::Scope LdapUrl::scope() const
00114 {
00115 return d->m_scope;
00116 }
00117
00118 void LdapUrl::setScope( Scope scope )
00119 {
00120 d->m_scope = scope;
00121 updateQuery();
00122 }
00123
00124 QString LdapUrl::filter() const
00125 {
00126 return d->m_filter;
00127 }
00128
00129 void LdapUrl::setFilter( const QString &filter )
00130 {
00131 d->m_filter = filter;
00132 updateQuery();
00133 }
00134
00135 bool LdapUrl::hasExtension( const QString &key ) const
00136 {
00137 return d->m_extensions.contains( key );
00138 }
00139
00140 LdapUrl::Extension LdapUrl::extension( const QString &key ) const
00141 {
00142 QMap<QString, Extension>::const_iterator it;
00143
00144 it = d->m_extensions.find( key );
00145 if ( it != d->m_extensions.constEnd() ) {
00146 return (*it);
00147 } else {
00148 Extension ext;
00149 ext.value = "";
00150 ext.critical = false;
00151 return ext;
00152 }
00153 }
00154
00155 QString LdapUrl::extension( const QString &key, bool &critical ) const
00156 {
00157 Extension ext;
00158
00159 ext = extension( key );
00160 critical = ext.critical;
00161 return ext.value;
00162 }
00163
00164 void LdapUrl::setExtension( const QString &key, const LdapUrl::Extension &ext )
00165 {
00166 d->m_extensions[ key ] = ext;
00167 updateQuery();
00168 }
00169
00170 void LdapUrl::setExtension( const QString &key, const QString &value, bool critical )
00171 {
00172 Extension ext;
00173 ext.value = value;
00174 ext.critical = critical;
00175 setExtension( key, ext );
00176 }
00177
00178 void LdapUrl::setExtension( const QString &key, int value, bool critical )
00179 {
00180 Extension ext;
00181 ext.value = QString::number( value );
00182 ext.critical = critical;
00183 setExtension( key, ext );
00184 }
00185
00186 void LdapUrl::removeExtension( const QString &key )
00187 {
00188 d->m_extensions.remove( key );
00189 updateQuery();
00190 }
00191
00192 void LdapUrl::updateQuery()
00193 {
00194 Extension ext;
00195 QMap<QString, Extension>::const_iterator it;
00196 QString q = "?";
00197
00198
00199 if ( d->m_attributes.count() > 0 ) {
00200 q += d->m_attributes.join(",");
00201 }
00202
00203
00204 q += '?';
00205 switch( d->m_scope ) {
00206 case Sub:
00207 q += "sub";
00208 break;
00209 case One:
00210 q += "one";
00211 break;
00212 case Base:
00213 q += "base";
00214 break;
00215 }
00216
00217
00218 q += '?';
00219 if ( d->m_filter != "(objectClass=*)" && !d->m_filter.isEmpty() ) {
00220 q += toPercentEncoding( d->m_filter );
00221 }
00222
00223
00224 q += '?';
00225 for ( it = d->m_extensions.constBegin(); it != d->m_extensions.constEnd(); ++it ) {
00226 if ( it.value().critical ) {
00227 q += '!';
00228 }
00229 q += it.key();
00230 if ( !it.value().value.isEmpty() ) {
00231 q += '=' + toPercentEncoding(it.value().value);
00232 }
00233 q += ',';
00234 }
00235 while ( q.endsWith( '?' ) || q.endsWith( ',' ) ) {
00236 q.remove( q.length() - 1, 1 );
00237 }
00238
00239 setQuery( q );
00240 kDebug(5322) << "LDAP URL updateQuery():" << prettyUrl();
00241 }
00242
00243 void LdapUrl::parseQuery()
00244 {
00245 Extension ext;
00246 QStringList extensions;
00247 QString q = query();
00248
00249 if ( q.startsWith( '?' ) ) {
00250 q.remove( 0, 1 );
00251 }
00252
00253
00254 QStringList url_items = q.split( '?' );
00255
00256 d->m_attributes.clear();
00257 d->m_scope = Base;
00258 d->m_filter = "(objectClass=*)";
00259 d->m_extensions.clear();
00260
00261 int i = 0;
00262 for ( QStringList::Iterator it = url_items.begin(); it != url_items.end(); ++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::Iterator it = extensions.begin(); it != extensions.end(); ++it ) {
00285 ext.critical = false;
00286 name = fromPercentEncoding( (*it).section( '=', 0, 0 ).toLatin1() ).toLower();
00287 value = fromPercentEncoding( (*it).section( '=', 1 ).toLatin1() );
00288 if ( name.startsWith( '!' ) ) {
00289 ext.critical = true;
00290 name.remove( 0, 1 );
00291 }
00292 kDebug(5322) << "LdapUrl extensions name=" << name << "value:" << value;
00293 ext.value = value.replace( "%2", "," );
00294 setExtension( name, ext );
00295 }
00296 }