• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • ldap
ldapclient.cpp
Go to the documentation of this file.
1 /* kldapclient.cpp - LDAP access
2  * Copyright (C) 2002 Klarälvdalens Datakonsult AB
3  *
4  * Author: Steffen Hansen <hansen@kde.org>
5  *
6  * Ported to KABC by Daniel Molkentin <molkentin@kde.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "ldapclient.h"
25 #include "ldapsession.h"
26 #include "ldapqueryjob.h"
27 
28 #include <kldap/ldapobject.h>
29 #include <kldap/ldapserver.h>
30 #include <kldap/ldapurl.h>
31 #include <kldap/ldif.h>
32 
33 #include <KConfig>
34 #include <KConfigGroup>
35 #include <KDebug>
36 #include <KDirWatch>
37 #include <KProtocolInfo>
38 #include <KStandardDirs>
39 #include <kio/job.h>
40 
41 #include <QtCore/QPointer>
42 #include <QtCore/QTimer>
43 
44 using namespace KLDAP;
45 
46 class LdapClient::Private
47 {
48  public:
49  Private( LdapClient *qq )
50  : q( qq ),
51  mJob( 0 ),
52  mActive( false ),
53  mSession( 0 )
54  {
55  }
56 
57  ~Private()
58  {
59  q->cancelQuery();
60 #ifdef KDEPIM_INPROCESS_LDAP
61  mSession->disconnectAndDelete();
62  mSession = 0;
63 #endif
64  }
65 
66  void startParseLDIF();
67  void parseLDIF( const QByteArray &data );
68  void endParseLDIF();
69  void finishCurrentObject();
70 
71  void slotData( KIO::Job*, const QByteArray &data );
72  void slotData( const QByteArray &data );
73  void slotInfoMessage( KJob*, const QString &info, const QString& );
74  void slotDone();
75 
76  LdapClient *q;
77 
78  KLDAP::LdapServer mServer;
79  QString mScope;
80  QStringList mAttrs;
81 
82  QPointer<KJob> mJob;
83  bool mActive;
84 
85  KLDAP::LdapObject mCurrentObject;
86  KLDAP::Ldif mLdif;
87  int mClientNumber;
88  int mCompletionWeight;
89 
90  KLDAP::LdapSession *mSession;
91 };
92 
93 LdapClient::LdapClient( int clientNumber, QObject *parent )
94  : QObject( parent ), d( new Private( this ) )
95 {
96  d->mClientNumber = clientNumber;
97  d->mCompletionWeight = 50 - d->mClientNumber;
98 }
99 
100 LdapClient::~LdapClient()
101 {
102  delete d;
103 }
104 
105 bool LdapClient::isActive() const
106 {
107  return d->mActive;
108 }
109 
110 void LdapClient::setServer( const KLDAP::LdapServer &server )
111 {
112  d->mServer = server;
113 #ifdef KDEPIM_INPROCESS_LDAP
114  if ( !d->mSession )
115  d->mSession = new LdapSession( this );
116  d->mSession->connectToServer( server );
117 #endif
118 }
119 
120 const KLDAP::LdapServer LdapClient::server() const
121 {
122  return d->mServer;
123 }
124 
125 void LdapClient::setAttributes( const QStringList &attrs )
126 {
127  d->mAttrs = attrs;
128  d->mAttrs << QLatin1String("objectClass"); // via objectClass we detect distribution lists
129 }
130 
131 QStringList LdapClient::attributes() const
132 {
133  return d->mAttrs;
134 }
135 
136 void LdapClient::setScope( const QString scope )
137 {
138  d->mScope = scope;
139 }
140 
141 void LdapClient::startQuery( const QString &filter )
142 {
143  cancelQuery();
144  KLDAP::LdapUrl url;
145 
146  url = d->mServer.url();
147 
148  url.setAttributes( d->mAttrs );
149  url.setScope( d->mScope == QLatin1String("one") ? KLDAP::LdapUrl::One : KLDAP::LdapUrl::Sub );
150  url.setFilter( QLatin1Char('(') + filter + QLatin1Char(')') );
151 
152  kDebug(5300) <<"LdapClient: Doing query:" << url.prettyUrl();
153 
154  d->startParseLDIF();
155  d->mActive = true;
156 #ifndef KDEPIM_INPROCESS_LDAP
157  d->mJob = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );
158  connect( d->mJob, SIGNAL(data(KIO::Job*,QByteArray)),
159  this, SLOT(slotData(KIO::Job*,QByteArray)) );
160 #else
161  if ( !d->mSession )
162  return;
163  d->mJob = d->mSession->get( url );
164  connect( d->mJob, SIGNAL(data(QByteArray)),
165  this, SLOT(slotData(QByteArray)) );
166 #endif
167  connect( d->mJob, SIGNAL(infoMessage(KJob*,QString,QString)),
168  this, SLOT(slotInfoMessage(KJob*,QString,QString)) );
169  connect( d->mJob, SIGNAL(result(KJob*)),
170  this, SLOT(slotDone()) );
171 }
172 
173 void LdapClient::cancelQuery()
174 {
175  if ( d->mJob ) {
176  d->mJob->kill();
177  d->mJob = 0;
178  }
179 
180  d->mActive = false;
181 }
182 
183 void LdapClient::Private::slotData( KIO::Job*, const QByteArray &data )
184 {
185  parseLDIF( data );
186 }
187 
188 void LdapClient::Private::slotData( const QByteArray &data )
189 {
190  parseLDIF( data );
191 }
192 
193 void LdapClient::Private::slotInfoMessage( KJob*, const QString&, const QString& )
194 {
195  //qDebug("Job said \"%s\"", info.toLatin1());
196 }
197 
198 void LdapClient::Private::slotDone()
199 {
200  endParseLDIF();
201  mActive = false;
202  if ( !mJob )
203  return;
204  int err = mJob->error();
205  if ( err && err != KIO::ERR_USER_CANCELED ) {
206  emit q->error( mJob->errorString() );
207  }
208 #ifdef KDEPIM_INPROCESS_LDAP
209  QMetaObject::invokeMethod( mJob, "deleteLater", Qt::QueuedConnection ); // it's in a different thread
210 #endif
211  emit q->done();
212 }
213 
214 void LdapClient::Private::startParseLDIF()
215 {
216  mCurrentObject.clear();
217  mLdif.startParsing();
218 }
219 
220 void LdapClient::Private::endParseLDIF()
221 {
222 }
223 
224 void LdapClient::Private::finishCurrentObject()
225 {
226  mCurrentObject.setDn( mLdif.dn() );
227  KLDAP::LdapAttrValue objectclasses;
228  KLDAP::LdapAttrMap::ConstIterator end = mCurrentObject.attributes().constEnd();
229  for ( KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().constBegin();
230  it != end; ++it ) {
231 
232  if ( it.key().toLower() == QLatin1String("objectclass") ) {
233  objectclasses = it.value();
234  break;
235  }
236  }
237 
238  bool groupofnames = false;
239  KLDAP::LdapAttrValue::ConstIterator endValue(objectclasses.constEnd());
240  for ( KLDAP::LdapAttrValue::ConstIterator it = objectclasses.constBegin();
241  it != endValue; ++it ) {
242 
243  QByteArray sClass = (*it).toLower();
244  if ( sClass == "groupofnames" || sClass == "kolabgroupofnames" ) {
245  groupofnames = true;
246  }
247  }
248 
249  if ( groupofnames ) {
250  KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().find( QLatin1String("mail") );
251  if ( it == mCurrentObject.attributes().end() ) {
252  // No explicit mail address found so far?
253  // Fine, then we use the address stored in the DN.
254  QString sMail;
255  QStringList lMail = mCurrentObject.dn().toString().split( QLatin1String(",dc="), QString::SkipEmptyParts );
256  const int n = lMail.count();
257  if ( n ) {
258  if ( lMail.first().toLower().startsWith( QLatin1String( "cn=" ) ) ) {
259  sMail = lMail.first().simplified().mid( 3 );
260  if ( 1 < n ) {
261  sMail.append( QLatin1Char('@') );
262  }
263  for ( int i = 1; i < n; ++i ) {
264  sMail.append( lMail.at(i) );
265  if ( i < n - 1 ) {
266  sMail.append( QLatin1Char('.') );
267  }
268  }
269  mCurrentObject.addValue( QLatin1String("mail"), sMail.toUtf8() );
270  }
271  }
272  }
273  }
274  emit q->result( *q, mCurrentObject );
275  mCurrentObject.clear();
276 }
277 
278 void LdapClient::Private::parseLDIF( const QByteArray &data )
279 {
280  //kDebug(5300) <<"LdapClient::parseLDIF(" << QCString(data.data(), data.size()+1) <<" )";
281  if ( data.size() ) {
282  mLdif.setLdif( data );
283  } else {
284  mLdif.endLdif();
285  }
286  KLDAP::Ldif::ParseValue ret;
287  QString name;
288  do {
289  ret = mLdif.nextItem();
290  switch ( ret ) {
291  case KLDAP::Ldif::Item:
292  {
293  name = mLdif.attr();
294  QByteArray value = mLdif.value();
295  mCurrentObject.addValue( name, value );
296  }
297  break;
298  case KLDAP::Ldif::EndEntry:
299  finishCurrentObject();
300  break;
301  default:
302  break;
303  }
304  } while ( ret != KLDAP::Ldif::MoreData );
305 }
306 
307 int LdapClient::clientNumber() const
308 {
309  return d->mClientNumber;
310 }
311 
312 int LdapClient::completionWeight() const
313 {
314  return d->mCompletionWeight;
315 }
316 
317 void LdapClient::setCompletionWeight( int weight )
318 {
319  d->mCompletionWeight = weight;
320 }
321 
322 
323 #include "ldapclient.moc"
KLDAP::LdapClient::attributes
QStringList attributes() const
Returns the LDAP attributes that should be returned in the query result.
Definition: ldapclient.cpp:131
KLDAP::LdapClient::server
const KLDAP::LdapServer server() const
Returns the ldap server information that are used by this client.
Definition: ldapclient.cpp:120
KLDAP::LdapClient::completionWeight
int completionWeight() const
Returns the completion weight of this client.
Definition: ldapclient.cpp:312
KLDAP::LdapClient::~LdapClient
virtual ~LdapClient()
Destroys the ldap client.
Definition: ldapclient.cpp:100
ldapsession.h
KLDAP::LdapSession
Definition: ldapsession.h:37
QObject
KLDAP::LdapClient::clientNumber
int clientNumber() const
Returns the number of this client.
Definition: ldapclient.cpp:307
KLDAP::LdapClient::isActive
bool isActive() const
Returns whether this client is currently running a search query.
Definition: ldapclient.cpp:105
ldapclient.h
KLDAP::LdapClient::startQuery
void startQuery(const QString &filter)
Starts the query with the given filter.
Definition: ldapclient.cpp:141
KLDAP::LdapClient::setAttributes
void setAttributes(const QStringList &attributes)
Sets the LDAP attributes that should be returned in the query result.
Definition: ldapclient.cpp:125
KLDAP::LdapClient::result
void result(const KLDAP::LdapClient &client, const KLDAP::LdapObject &)
This signal is emitted once for each object that is returned from the query.
KLDAP::LdapClient::LdapClient
LdapClient(int clientNumber, QObject *parent=0)
Creates a new ldap client.
Definition: ldapclient.cpp:93
KLDAP::LdapClient::setServer
void setServer(const KLDAP::LdapServer &server)
Sets the LDAP server information that shall be used by this client.
Definition: ldapclient.cpp:110
KLDAP::LdapClient::setCompletionWeight
void setCompletionWeight(int weight)
Sets the completion weight of this client.
Definition: ldapclient.cpp:317
KLDAP::LdapClient
An object that represents a configured LDAP server.
Definition: ldapclient.h:48
KLDAP::LdapClient::setScope
void setScope(const QString scope)
Sets the scope of the LDAP query.
Definition: ldapclient.cpp:136
KJob
KLDAP::LdapClient::cancelQuery
void cancelQuery()
Cancels a running query.
Definition: ldapclient.cpp:173
ldapqueryjob.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal