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

libkdepim

  • sources
  • kde-4.14
  • 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 <KDebug>
34 #include <kio/job.h>
35 
36 #include <QtCore/QPointer>
37 
38 using namespace KLDAP;
39 
40 class LdapClient::Private
41 {
42 public:
43  Private( LdapClient *qq )
44  : q( qq ),
45  mJob( 0 ),
46  mActive( false ),
47  mSession( 0 )
48  {
49  }
50 
51  ~Private()
52  {
53  q->cancelQuery();
54 #ifdef KDEPIM_INPROCESS_LDAP
55  mSession->disconnectAndDelete();
56  mSession = 0;
57 #endif
58  }
59 
60  void startParseLDIF();
61  void parseLDIF( const QByteArray &data );
62  void endParseLDIF();
63  void finishCurrentObject();
64 
65  void slotData( KIO::Job*, const QByteArray &data );
66  void slotData( const QByteArray &data );
67  void slotInfoMessage( KJob*, const QString &info, const QString& );
68  void slotDone();
69 
70  LdapClient *q;
71 
72  KLDAP::LdapServer mServer;
73  QString mScope;
74  QStringList mAttrs;
75 
76  QPointer<KJob> mJob;
77  bool mActive;
78 
79  KLDAP::LdapObject mCurrentObject;
80  KLDAP::Ldif mLdif;
81  int mClientNumber;
82  int mCompletionWeight;
83 
84  KLDAP::LdapSession *mSession;
85 };
86 
87 LdapClient::LdapClient( int clientNumber, QObject *parent )
88  : QObject( parent ), d( new Private( this ) )
89 {
90  d->mClientNumber = clientNumber;
91  d->mCompletionWeight = 50 - d->mClientNumber;
92 }
93 
94 LdapClient::~LdapClient()
95 {
96  delete d;
97 }
98 
99 bool LdapClient::isActive() const
100 {
101  return d->mActive;
102 }
103 
104 void LdapClient::setServer( const KLDAP::LdapServer &server )
105 {
106  d->mServer = server;
107 #ifdef KDEPIM_INPROCESS_LDAP
108  if ( !d->mSession )
109  d->mSession = new LdapSession( this );
110  d->mSession->connectToServer( server );
111 #endif
112 }
113 
114 const KLDAP::LdapServer LdapClient::server() const
115 {
116  return d->mServer;
117 }
118 
119 void LdapClient::setAttributes( const QStringList &attrs )
120 {
121  d->mAttrs = attrs;
122  d->mAttrs << QLatin1String("objectClass"); // via objectClass we detect distribution lists
123 }
124 
125 QStringList LdapClient::attributes() const
126 {
127  return d->mAttrs;
128 }
129 
130 void LdapClient::setScope( const QString scope )
131 {
132  d->mScope = scope;
133 }
134 
135 void LdapClient::startQuery( const QString &filter )
136 {
137  cancelQuery();
138  KLDAP::LdapUrl url;
139 
140  url = d->mServer.url();
141 
142  url.setAttributes( d->mAttrs );
143  url.setScope( d->mScope == QLatin1String("one") ? KLDAP::LdapUrl::One : KLDAP::LdapUrl::Sub );
144  const QString userFilter = url.filter();
145  QString finalFilter = filter;
146  // combine the filter set by the user in the config dialog (url.filter()) and the filter from this query
147  if (!userFilter.isEmpty()) {
148  finalFilter = QLatin1String("&(") + finalFilter + QLatin1String(")(") + userFilter + QLatin1Char(')');
149  }
150  url.setFilter( QLatin1Char('(') + finalFilter + 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  const 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  const 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  const 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 "moc_ldapclient.cpp"
KLDAP::LdapClient::attributes
QStringList attributes() const
Returns the LDAP attributes that should be returned in the query result.
Definition: ldapclient.cpp:125
KLDAP::LdapClient::server
const KLDAP::LdapServer server() const
Returns the ldap server information that are used by this client.
Definition: ldapclient.cpp:114
QString::append
QString & append(QChar ch)
QByteArray::toLower
QByteArray toLower() const
KLDAP::LdapClient::completionWeight
int completionWeight() const
Returns the completion weight of this client.
Definition: ldapclient.cpp:312
QByteArray
KLDAP::LdapClient::~LdapClient
virtual ~LdapClient()
Destroys the ldap client.
Definition: ldapclient.cpp:94
ldapsession.h
QList::at
const T & at(int i) const
QPointer< KJob >
KLDAP::LdapSession
Definition: ldapsession.h:37
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:99
QString::clear
void clear()
ldapclient.h
KLDAP::LdapClient::startQuery
void startQuery(const QString &filter)
Starts the query with the given filter.
Definition: ldapclient.cpp:135
QList::count
int count(const T &value) const
QObject
QString::isEmpty
bool isEmpty() const
KLDAP::LdapClient::setAttributes
void setAttributes(const QStringList &attributes)
Sets the LDAP attributes that should be returned in the query result.
Definition: ldapclient.cpp:119
QList::first
T & first()
QString
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.
QStringList
QLatin1Char
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QLatin1String
KLDAP::LdapClient::LdapClient
LdapClient(int clientNumber, QObject *parent=0)
Creates a new ldap client.
Definition: ldapclient.cpp:87
KLDAP::LdapClient::setServer
void setServer(const KLDAP::LdapServer &server)
Sets the LDAP server information that shall be used by this client.
Definition: ldapclient.cpp:104
QStringList::split
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
KLDAP::LdapClient::setCompletionWeight
void setCompletionWeight(int weight)
Sets the completion weight of this client.
Definition: ldapclient.cpp:317
QByteArray::size
int size() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KLDAP::LdapClient
An object that represents a configured LDAP server.
Definition: ldapclient.h:46
KLDAP::LdapClient::setScope
void setScope(const QString scope)
Sets the scope of the LDAP query.
Definition: ldapclient.cpp:130
KJob
QByteArray::find
int find(char c, int from) const
KLDAP::LdapClient::cancelQuery
void cancelQuery()
Cancels a running query.
Definition: ldapclient.cpp:173
ldapqueryjob.h
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 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
  • pimprint

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