summaryrefslogtreecommitdiffstats
path: root/kabc/ldapurl.cpp
blob: 6bca31b3970017c5b44e9453cbbd99ba9046c6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
    This file is part of libkabc.
    Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General  Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <kdebug.h>
#include <tqstringlist.h>
#include <tqdir.h>

#include "ldapurl.h"

using namespace KABC;

LDAPUrl::LDAPUrl()
{
  m_scope = Base;
}

LDAPUrl::LDAPUrl(const KURL &_url)
  : KURL(_url), m_extensions()
{
  m_dn = path();
  if ( !TQDir::isRelativePath(m_dn) )
#ifdef Q_WS_WIN
    m_dn.remove(0,3); // e.g. "c:/"
#else
    m_dn.remove(0,1);
#endif
  parseQuery();
}

void LDAPUrl::setDn( const TQString &dn)
{
  m_dn = dn;
  if ( !TQDir::isRelativePath(m_dn) )
#ifdef Q_WS_WIN
    m_dn.remove(0,3); // e.g. "c:/"
#else
    m_dn.remove(0,1);
#endif
  setPath(m_dn);
}

bool LDAPUrl::hasExtension( const TQString &key ) const
{
  return m_extensions.tqcontains( key );
}

LDAPUrl::Extension LDAPUrl::extension( const TQString &key ) const
{
  TQMap<TQString, Extension>::const_iterator it;

  it = m_extensions.tqfind( key );
  if ( it != m_extensions.constEnd() )
    return (*it);
  else {
    Extension ext;
    ext.value = "";
    ext.critical = false;
    return ext;
  }
}

TQString LDAPUrl::extension( const TQString &key, bool &critical ) const
{
  Extension ext;

  ext = extension( key );
  critical = ext.critical;
  return ext.value;
}

void LDAPUrl::setExtension( const TQString &key, const LDAPUrl::Extension &ext )
{
  m_extensions[ key ] = ext;
  updateQuery();
}

void LDAPUrl::setExtension( const TQString &key, const TQString &value, bool critical )
{
  Extension ext;
  ext.value = value;
  ext.critical = critical;
  setExtension( key, ext );
}

void LDAPUrl::removeExtension( const TQString &key )
{
  m_extensions.remove( key );
  updateQuery();
}

void LDAPUrl::updateQuery()
{
  Extension ext;
  TQMap<TQString, Extension>::iterator it;
  TQString q = "?";

  // set the attributes to query
  if ( m_attributes.count() > 0 ) q += m_attributes.join(",");

  // set the scope
  q += "?";
  switch( m_scope ) {
    case Sub:
      q += "sub";
      break;
    case One:
      q += "one";
      break;
    case Base:
      q += "base";
      break;
  }

  // set the filter
  q += "?";
  if ( m_filter != "(objectClass=*)" && !m_filter.isEmpty() )
    q += m_filter;

  // set the extensions
  q += "?";
  for ( it = m_extensions.begin(); it != m_extensions.end(); ++it ) {
    if ( it.data().critical ) q += "!";
    q += it.key();
    if ( !it.data().value.isEmpty() ) 
      q += "=" + it.data().value;
    q += ",";
  }
  while  ( q.endsWith("?") || q.endsWith(",") )
    q.remove( q.length() - 1, 1 );

  setQuery(q);
  kdDebug(5700) << "LDAP URL updateQuery(): " << prettyURL() << endl;
}

void LDAPUrl::parseQuery()
{
  Extension ext;
  TQStringList extensions;
  TQString q = query();
  // remove first ?
  if (q.startsWith("?"))
    q.remove(0,1);

  // split into a list
  TQStringList url_items = TQStringList::split("?", q, true);

  m_attributes.clear();
  m_scope = Base;
  m_filter = "(objectClass=*)";
  m_extensions.clear();

  int i = 0;
  for ( TQStringList::Iterator it = url_items.begin(); it != url_items.end(); ++it, i++ ) {
    switch (i) {
      case 0:
        m_attributes = TQStringList::split(",", (*it), false);
        break;
      case 1:
        if ( (*it) == "sub" ) m_scope = Sub; else
        if ( (*it) == "one") m_scope = One;
        break;
      case 2:
        m_filter = decode_string( *it );
        break;
      case 3:
        extensions = TQStringList::split(",", (*it), false);
        break;
    }
  }

  TQString name,value;
  for ( TQStringList::Iterator it = extensions.begin(); it != extensions.end(); ++it ) {
    ext.critical = false;
    name = decode_string( (*it).section('=',0,0) ).lower();
    value = decode_string( (*it).section('=',1) );
    if ( name.startsWith("!") ) {
      ext.critical = true;
      name.remove(0, 1);
    }
    kdDebug(5700) << "LDAPUrl extensions name= " << name << " value: " << value << endl;
    ext.value = value.replace( "%2", "," );
    setExtension( name, ext );
  }
}