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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <kdebug.h> 00022 #include <tqstringlist.h> 00023 #include <tqdir.h> 00024 00025 #include "ldapurl.h" 00026 00027 using namespace KABC; 00028 00029 LDAPUrl::LDAPUrl() 00030 { 00031 m_scope = Base; 00032 } 00033 00034 LDAPUrl::LDAPUrl(const KURL &_url) 00035 : KURL(_url), m_extensions() 00036 { 00037 m_dn = path(); 00038 if ( !TQDir::isRelativePath(m_dn) ) 00039 #ifdef Q_WS_WIN 00040 m_dn.remove(0,3); // e.g. "c:/" 00041 #else 00042 m_dn.remove(0,1); 00043 #endif 00044 parseQuery(); 00045 } 00046 00047 void LDAPUrl::setDn( const TQString &dn) 00048 { 00049 m_dn = dn; 00050 if ( !TQDir::isRelativePath(m_dn) ) 00051 #ifdef Q_WS_WIN 00052 m_dn.remove(0,3); // e.g. "c:/" 00053 #else 00054 m_dn.remove(0,1); 00055 #endif 00056 setPath(m_dn); 00057 } 00058 00059 bool LDAPUrl::hasExtension( const TQString &key ) const 00060 { 00061 return m_extensions.contains( key ); 00062 } 00063 00064 LDAPUrl::Extension LDAPUrl::extension( const TQString &key ) const 00065 { 00066 TQMap<TQString, Extension>::const_iterator it; 00067 00068 it = m_extensions.find( key ); 00069 if ( it != m_extensions.constEnd() ) 00070 return (*it); 00071 else { 00072 Extension ext; 00073 ext.value = ""; 00074 ext.critical = false; 00075 return ext; 00076 } 00077 } 00078 00079 TQString LDAPUrl::extension( const TQString &key, bool &critical ) const 00080 { 00081 Extension ext; 00082 00083 ext = extension( key ); 00084 critical = ext.critical; 00085 return ext.value; 00086 } 00087 00088 void LDAPUrl::setExtension( const TQString &key, const LDAPUrl::Extension &ext ) 00089 { 00090 m_extensions[ key ] = ext; 00091 updateQuery(); 00092 } 00093 00094 void LDAPUrl::setExtension( const TQString &key, const TQString &value, bool critical ) 00095 { 00096 Extension ext; 00097 ext.value = value; 00098 ext.critical = critical; 00099 setExtension( key, ext ); 00100 } 00101 00102 void LDAPUrl::removeExtension( const TQString &key ) 00103 { 00104 m_extensions.remove( key ); 00105 updateQuery(); 00106 } 00107 00108 void LDAPUrl::updateQuery() 00109 { 00110 Extension ext; 00111 TQMap<TQString, Extension>::iterator it; 00112 TQString q = "?"; 00113 00114 // set the attributes to query 00115 if ( m_attributes.count() > 0 ) q += m_attributes.join(","); 00116 00117 // set the scope 00118 q += "?"; 00119 switch( m_scope ) { 00120 case Sub: 00121 q += "sub"; 00122 break; 00123 case One: 00124 q += "one"; 00125 break; 00126 case Base: 00127 q += "base"; 00128 break; 00129 } 00130 00131 // set the filter 00132 q += "?"; 00133 if ( m_filter != "(objectClass=*)" && !m_filter.isEmpty() ) 00134 q += m_filter; 00135 00136 // set the extensions 00137 q += "?"; 00138 for ( it = m_extensions.begin(); it != m_extensions.end(); ++it ) { 00139 if ( it.data().critical ) q += "!"; 00140 q += it.key(); 00141 if ( !it.data().value.isEmpty() ) 00142 q += "=" + it.data().value; 00143 q += ","; 00144 } 00145 while ( q.endsWith("?") || q.endsWith(",") ) 00146 q.remove( q.length() - 1, 1 ); 00147 00148 setQuery(q); 00149 kdDebug(5700) << "LDAP URL updateQuery(): " << prettyURL() << endl; 00150 } 00151 00152 void LDAPUrl::parseQuery() 00153 { 00154 Extension ext; 00155 TQStringList extensions; 00156 TQString q = query(); 00157 // remove first ? 00158 if (q.startsWith("?")) 00159 q.remove(0,1); 00160 00161 // split into a list 00162 TQStringList url_items = TQStringList::split("?", q, true); 00163 00164 m_attributes.clear(); 00165 m_scope = Base; 00166 m_filter = "(objectClass=*)"; 00167 m_extensions.clear(); 00168 00169 int i = 0; 00170 for ( TQStringList::Iterator it = url_items.begin(); it != url_items.end(); ++it, i++ ) { 00171 switch (i) { 00172 case 0: 00173 m_attributes = TQStringList::split(",", (*it), false); 00174 break; 00175 case 1: 00176 if ( (*it) == "sub" ) m_scope = Sub; else 00177 if ( (*it) == "one") m_scope = One; 00178 break; 00179 case 2: 00180 m_filter = decode_string( *it ); 00181 break; 00182 case 3: 00183 extensions = TQStringList::split(",", (*it), false); 00184 break; 00185 } 00186 } 00187 00188 TQString name,value; 00189 for ( TQStringList::Iterator it = extensions.begin(); it != extensions.end(); ++it ) { 00190 ext.critical = false; 00191 name = decode_string( (*it).section('=',0,0) ).lower(); 00192 value = decode_string( (*it).section('=',1) ); 00193 if ( name.startsWith("!") ) { 00194 ext.critical = true; 00195 name.remove(0, 1); 00196 } 00197 kdDebug(5700) << "LDAPUrl extensions name= " << name << " value: " << value << endl; 00198 ext.value = value.replace( "%2", "," ); 00199 setExtension( name, ext ); 00200 } 00201 }