TDE core libraries
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

202 linhas
4.7KB

  1. /*
  2. This file is part of libtdeabc.
  3. Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <kdebug.h>
  18. #include <tqstringlist.h>
  19. #include <tqdir.h>
  20. #include "ldapurl.h"
  21. using namespace TDEABC;
  22. LDAPUrl::LDAPUrl()
  23. {
  24. m_scope = Base;
  25. }
  26. LDAPUrl::LDAPUrl(const KURL &_url)
  27. : KURL(_url), m_extensions()
  28. {
  29. m_dn = path();
  30. if ( !TQDir::isRelativePath(m_dn) )
  31. #ifdef Q_WS_WIN
  32. m_dn.remove(0,3); // e.g. "c:/"
  33. #else
  34. m_dn.remove(0,1);
  35. #endif
  36. parseQuery();
  37. }
  38. void LDAPUrl::setDn( const TQString &dn)
  39. {
  40. m_dn = dn;
  41. if ( !TQDir::isRelativePath(m_dn) )
  42. #ifdef Q_WS_WIN
  43. m_dn.remove(0,3); // e.g. "c:/"
  44. #else
  45. m_dn.remove(0,1);
  46. #endif
  47. setPath(m_dn);
  48. }
  49. bool LDAPUrl::hasExtension( const TQString &key ) const
  50. {
  51. return m_extensions.contains( key );
  52. }
  53. LDAPUrl::Extension LDAPUrl::extension( const TQString &key ) const
  54. {
  55. TQMap<TQString, Extension>::const_iterator it;
  56. it = m_extensions.find( key );
  57. if ( it != m_extensions.constEnd() )
  58. return (*it);
  59. else {
  60. Extension ext;
  61. ext.value = "";
  62. ext.critical = false;
  63. return ext;
  64. }
  65. }
  66. TQString LDAPUrl::extension( const TQString &key, bool &critical ) const
  67. {
  68. Extension ext;
  69. ext = extension( key );
  70. critical = ext.critical;
  71. return ext.value;
  72. }
  73. void LDAPUrl::setExtension( const TQString &key, const LDAPUrl::Extension &ext )
  74. {
  75. m_extensions[ key ] = ext;
  76. updateQuery();
  77. }
  78. void LDAPUrl::setExtension( const TQString &key, const TQString &value, bool critical )
  79. {
  80. Extension ext;
  81. ext.value = value;
  82. ext.critical = critical;
  83. setExtension( key, ext );
  84. }
  85. void LDAPUrl::removeExtension( const TQString &key )
  86. {
  87. m_extensions.remove( key );
  88. updateQuery();
  89. }
  90. void LDAPUrl::updateQuery()
  91. {
  92. Extension ext;
  93. TQMap<TQString, Extension>::iterator it;
  94. TQString q = "?";
  95. // set the attributes to query
  96. if ( m_attributes.count() > 0 ) q += m_attributes.join(",");
  97. // set the scope
  98. q += "?";
  99. switch( m_scope ) {
  100. case Sub:
  101. q += "sub";
  102. break;
  103. case One:
  104. q += "one";
  105. break;
  106. case Base:
  107. q += "base";
  108. break;
  109. }
  110. // set the filter
  111. q += "?";
  112. if ( m_filter != "(objectClass=*)" && !m_filter.isEmpty() )
  113. q += m_filter;
  114. // set the extensions
  115. q += "?";
  116. for ( it = m_extensions.begin(); it != m_extensions.end(); ++it ) {
  117. if ( it.data().critical ) q += "!";
  118. q += it.key();
  119. if ( !it.data().value.isEmpty() )
  120. q += "=" + it.data().value;
  121. q += ",";
  122. }
  123. while ( q.endsWith("?") || q.endsWith(",") )
  124. q.remove( q.length() - 1, 1 );
  125. setQuery(q);
  126. kdDebug(5700) << "LDAP URL updateQuery(): " << prettyURL() << endl;
  127. }
  128. void LDAPUrl::parseQuery()
  129. {
  130. Extension ext;
  131. TQStringList extensions;
  132. TQString q = query();
  133. // remove first ?
  134. if (q.startsWith("?"))
  135. q.remove(0,1);
  136. // split into a list
  137. TQStringList url_items = TQStringList::split("?", q, true);
  138. m_attributes.clear();
  139. m_scope = Base;
  140. m_filter = "(objectClass=*)";
  141. m_extensions.clear();
  142. int i = 0;
  143. for ( TQStringList::Iterator it = url_items.begin(); it != url_items.end(); ++it, i++ ) {
  144. switch (i) {
  145. case 0:
  146. m_attributes = TQStringList::split(",", (*it), false);
  147. break;
  148. case 1:
  149. if ( (*it) == "sub" ) m_scope = Sub; else
  150. if ( (*it) == "one") m_scope = One;
  151. break;
  152. case 2:
  153. m_filter = decode_string( *it );
  154. break;
  155. case 3:
  156. extensions = TQStringList::split(",", (*it), false);
  157. break;
  158. }
  159. }
  160. TQString name,value;
  161. for ( TQStringList::Iterator it = extensions.begin(); it != extensions.end(); ++it ) {
  162. ext.critical = false;
  163. name = decode_string( (*it).section('=',0,0) ).lower();
  164. value = decode_string( (*it).section('=',1) );
  165. if ( name.startsWith("!") ) {
  166. ext.critical = true;
  167. name.remove(0, 1);
  168. }
  169. kdDebug(5700) << "LDAPUrl extensions name= " << name << " value: " << value << endl;
  170. ext.value = value.replace( "%2", "," );
  171. setExtension( name, ext );
  172. }
  173. }