• 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
ldapsession.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3  Author: Volker Krause <volker.krause@kdab.com>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "ldapsession.h"
22 
23 #include <kldap/ldapoperation.h>
24 #include <kldap/ldif.h>
25 #include <kldap/ldapcontrol.h>
26 #include <kldap/ldapdefs.h>
27 
28 #include <KDebug>
29 #include "ldapqueryjob.h"
30 
31 using namespace KLDAP;
32 
33 LdapSession::LdapSession(QObject* parent) :
34  QThread(parent),
35  m_state( Disconnected ),
36  m_currentJob( 0 )
37 {
38  kDebug();
39 }
40 
41 // runs in other thread
42 void LdapSession::connectToServer(const KLDAP::LdapServer& server)
43 {
44  kDebug();
45  if ( m_state != Disconnected )
46  return;
47  m_server = server;
48  start();
49 }
50 
51 // runs in this thread
52 void LdapSession::connectToServerInternal()
53 {
54  kDebug();
55  m_conn.setServer( m_server );
56  if ( m_conn.connect() != 0 ) {
57  kWarning() << "failed to connect: " << m_conn.connectionError();
58  return;
59  }
60  m_state = Connected;
61  authenticate();
62 }
63 
64 // runs in other threads
65 void LdapSession::disconnectAndDelete()
66 {
67  kDebug();
68  QMetaObject::invokeMethod( this, "quit", Qt::QueuedConnection );
69  QMetaObject::invokeMethod( this, "deleteLater", Qt::QueuedConnection );
70 }
71 
72 // runs in this thread
73 void LdapSession::disconnectFromServerInternal()
74 {
75  kDebug();
76  m_conn.close();
77  m_state = Disconnected;
78 }
79 
80 void LdapSession::authenticate()
81 {
82  kDebug();
83  LdapOperation op( m_conn );
84  while ( true ) {
85  int retval = op.bind_s();
86  if ( retval == 0 ) {
87  kDebug() << "connected!";
88  m_state = Authenticated;
89  return;
90  }
91  if ( retval == KLDAP_INVALID_CREDENTIALS ||
92  retval == KLDAP_INSUFFICIENT_ACCESS ||
93  retval == KLDAP_INAPPROPRIATE_AUTH ||
94  retval == KLDAP_UNWILLING_TO_PERFORM ) {
95 
96  if ( m_server.auth() != LdapServer::SASL ) {
97  m_server.setBindDn( m_server.user() );
98  }
99  m_conn.setServer( m_server );
100  } else {
101 // LDAPErr( retval );
102  disconnectFromServerInternal();
103  kDebug() << "error" << retval;
104  return;
105  }
106  }
107 }
108 
109 // called from other thread
110 LdapQueryJob* LdapSession::get(const KLDAP::LdapUrl& url)
111 {
112  kDebug() << url;
113  LdapQueryJob* job = new LdapQueryJob( url, this );
114  job->moveToThread( this ); // make sure the job is in the thread so that the result connections are queued
115  connect( job, SIGNAL(result(KJob*)), SLOT(jobDone(KJob*)) );
116  QMutexLocker locker( &m_mutex );
117  m_jobQueue.enqueue( job );
118  QMetaObject::invokeMethod( this, "executeNext", Qt::QueuedConnection );
119  return job;
120 }
121 
122 void LdapSession::run()
123 {
124  connectToServerInternal();
125  QMetaObject::invokeMethod( this, "executeNext", Qt::QueuedConnection );
126  exec();
127  disconnectFromServerInternal();
128 }
129 
130 void LdapSession::executeNext()
131 {
132  if ( m_state != Authenticated || m_currentJob )
133  return;
134  QMutexLocker locker( &m_mutex );
135  if ( m_jobQueue.isEmpty() )
136  return;
137  m_currentJob = m_jobQueue.dequeue();
138  locker.unlock();
139  QMetaObject::invokeMethod( m_currentJob, "triggerStart", Qt::QueuedConnection );
140 }
141 
142 LdapServer LdapSession::server() const
143 {
144  return m_server;
145 }
146 
147 LdapConnection& LdapSession::connection()
148 {
149  return m_conn;
150 }
151 
152 void LdapSession::jobDone(KJob* job)
153 {
154  if ( m_currentJob == job )
155  m_currentJob = 0;
156  QMutexLocker locker( &m_mutex );
157  m_jobQueue.removeAll( static_cast<LdapQueryJob*>( job ) );
158  locker.unlock();
159  QMetaObject::invokeMethod( this, "executeNext", Qt::QueuedConnection );
160 }
161 
162 
163 #include "ldapsession.moc"
KLDAP::LdapSession::disconnectAndDelete
void disconnectAndDelete()
call this instead of the dtor
Definition: ldapsession.cpp:65
KLDAP::LdapSession::connectToServerInternal
void connectToServerInternal()
Definition: ldapsession.cpp:52
ldapsession.h
KLDAP::LdapSession::server
LdapServer server() const
Definition: ldapsession.cpp:142
QObject
KLDAP::LdapSession::connection
LdapConnection & connection()
Definition: ldapsession.cpp:147
KLDAP::LdapSession::disconnectFromServerInternal
void disconnectFromServerInternal()
Definition: ldapsession.cpp:73
KLDAP::LdapSession::run
void run()
Definition: ldapsession.cpp:122
KLDAP::LdapSession::get
LdapQueryJob * get(const LdapUrl &url)
Definition: ldapsession.cpp:110
KLDAP::LdapSession::connectToServer
void connectToServer(const LdapServer &server)
call this instead of start()
Definition: ldapsession.cpp:42
KLDAP::LdapQueryJob
Definition: ldapqueryjob.h:31
QThread
KJob
ldapqueryjob.h
KLDAP::LdapSession::LdapSession
LdapSession(QObject *parent=0)
Definition: ldapsession.cpp:33
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