libkdepim Library API Documentation

ldapurl.cpp

00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2004 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., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include <kdebug.h> 00022 #include <qstringlist.h> 00023 00024 #include "ldapurl.h" 00025 00026 using namespace KPIM; 00027 00028 LDAPUrl::LDAPUrl() 00029 { 00030 m_scope = Base; 00031 } 00032 00033 LDAPUrl::LDAPUrl(const KURL &_url) 00034 : KURL(_url), m_extensions() 00035 { 00036 m_dn = path(); 00037 if (m_dn.startsWith("/")) 00038 m_dn.remove(0,1); 00039 parseQuery(); 00040 } 00041 00042 void LDAPUrl::setDn( const QString &dn) 00043 { 00044 m_dn = dn; 00045 if ( m_dn.startsWith("/") ) 00046 m_dn.remove(0,1); 00047 setPath(m_dn); 00048 } 00049 00050 bool LDAPUrl::hasExtension( const QString &key ) const 00051 { 00052 return m_extensions.contains( key ); 00053 } 00054 00055 LDAPUrl::Extension LDAPUrl::extension( const QString &key ) const 00056 { 00057 QMap<QString, Extension>::const_iterator it; 00058 00059 it = m_extensions.find( key ); 00060 if ( it != m_extensions.constEnd() ) 00061 return (*it); 00062 else { 00063 Extension ext; 00064 ext.value = ""; 00065 ext.critical = false; 00066 return ext; 00067 } 00068 } 00069 00070 QString LDAPUrl::extension( const QString &key, bool &critical ) const 00071 { 00072 Extension ext; 00073 00074 ext = extension( key ); 00075 critical = ext.critical; 00076 return ext.value; 00077 } 00078 00079 void LDAPUrl::setExtension( const QString &key, const LDAPUrl::Extension &ext ) 00080 { 00081 m_extensions[ key ] = ext; 00082 updateQuery(); 00083 } 00084 00085 void LDAPUrl::setExtension( const QString &key, const QString &value, bool critical ) 00086 { 00087 Extension ext; 00088 ext.value = value; 00089 ext.critical = critical; 00090 setExtension( key, ext ); 00091 } 00092 00093 void LDAPUrl::removeExtension( const QString &key ) 00094 { 00095 m_extensions.remove( key ); 00096 updateQuery(); 00097 } 00098 00099 void LDAPUrl::updateQuery() 00100 { 00101 Extension ext; 00102 QMap<QString, Extension>::iterator it; 00103 QString q = "?"; 00104 00105 // set the attributes to query 00106 if ( m_attributes.count() > 0 ) q += m_attributes.join(","); 00107 00108 // set the scope 00109 q += "?"; 00110 switch( m_scope ) { 00111 case Sub: 00112 q += "sub"; 00113 break; 00114 case One: 00115 q += "one"; 00116 break; 00117 case Base: 00118 q += "base"; 00119 break; 00120 } 00121 00122 // set the filter 00123 q += "?"; 00124 if ( m_filter != "(objectClass=*)" && !m_filter.isEmpty() ) 00125 q += m_filter; 00126 00127 // set the extensions 00128 q += "?"; 00129 for ( it = m_extensions.begin(); it != m_extensions.end(); it++ ) { 00130 if ( it.data().critical ) q += "!"; 00131 q += it.key(); 00132 if ( !it.data().value.isEmpty() ) 00133 q += "=" + it.data().value; 00134 q += ","; 00135 } 00136 while ( q.endsWith("?") || q.endsWith(",") ) 00137 q.remove( q.length() - 1, 1 ); 00138 00139 setQuery(q); 00140 kdDebug(5700) << "LDAP URL updateQuery(): " << prettyURL() << endl; 00141 } 00142 00143 void LDAPUrl::parseQuery() 00144 { 00145 Extension ext; 00146 QStringList extensions; 00147 QString q = query(); 00148 // remove first ? 00149 if (q.startsWith("?")) 00150 q.remove(0,1); 00151 00152 // split into a list 00153 QStringList url_items = QStringList::split("?", q, true); 00154 00155 m_attributes.clear(); 00156 m_scope = Base; 00157 m_filter = "(objectClass=*)"; 00158 m_extensions.clear(); 00159 00160 int i = 0; 00161 for ( QStringList::Iterator it = url_items.begin(); it != url_items.end(); it++, i++ ) { 00162 switch (i) { 00163 case 0: 00164 m_attributes = QStringList::split(",", (*it), false); 00165 break; 00166 case 1: 00167 if ( (*it) == "sub" ) m_scope = Sub; else 00168 if ( (*it) == "one") m_scope = One; 00169 break; 00170 case 2: 00171 m_filter = decode_string( *it ); 00172 break; 00173 case 3: 00174 extensions = QStringList::split(",", (*it), false); 00175 break; 00176 } 00177 } 00178 00179 QString name,value; 00180 for ( QStringList::Iterator it = extensions.begin(); it != extensions.end(); it++ ) { 00181 ext.critical = false; 00182 name = decode_string( (*it).section('=',0,0) ).lower(); 00183 value = decode_string( (*it).section('=',1) ); 00184 if ( name.startsWith("!") ) { 00185 ext.critical = true; 00186 name.remove(0, 1); 00187 } 00188 kdDebug(5700) << "LDAPUrl extensions name= " << name << " value: " << value << endl; 00189 ext.value = value.replace( "%2", "," ); 00190 setExtension( name, ext ); 00191 } 00192 }
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:18:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003