• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KLDAP Library

ldapmodel_p.cpp

00001 /*
00002   This file is part of libkldap.
00003   Copyright (c) 2006 Sean Harmer <sh@theharmers.co.uk>
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General  Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Library General Public License for more details.
00014 
00015   You should have received a copy of the GNU Library General Public License
00016   along with this library; see the file COPYING.LIB.  If not, write to
00017   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018   Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "ldapmodel_p.h"
00022 #include "ldapmodelnode_p.h"
00023 #include "ldapsearch.h"
00024 
00025 #include <kdebug.h>
00026 
00027 using namespace KLDAP;
00028 
00029 LdapModel::LdapModelPrivate::LdapModelPrivate( LdapModel *parent )
00030   : m_parent( parent ),
00031     m_root( new LdapModelDNNode ),
00032     m_search( new LdapSearch ),
00033     m_searchResultObjects(),
00034     m_baseDN(),
00035     m_searchType( NotSearching ),
00036     m_searchItem( 0 )
00037 {
00038 }
00039 
00040 LdapModel::LdapModelPrivate::LdapModelPrivate( LdapModel *parent, LdapConnection &connection )
00041   : m_parent( parent ),
00042     m_root( new LdapModelDNNode ),
00043     m_search( new LdapSearch( connection ) ),
00044     m_searchResultObjects(),
00045     m_baseDN(),
00046     m_searchType( NotSearching ),
00047     m_searchItem( 0 )
00048 {
00049 }
00050 
00051 LdapModel::LdapModelPrivate::~LdapModelPrivate()
00052 {
00053   if ( m_root ) {
00054     delete m_root;
00055   }
00056 
00057   if ( m_search ) {
00058     delete m_search;
00059   }
00060 }
00061 
00062 void LdapModel::LdapModelPrivate::setConnection( LdapConnection &connection )
00063 {
00064   m_search->setConnection( connection );
00065 }
00066 
00067 bool LdapModel::LdapModelPrivate::search( const LdapDN &searchBase,
00068                                           LdapUrl::Scope scope,
00069                                           const QString &filter,
00070                                           const QStringList &attributes,
00071                                           int pagesize )
00072 {
00073   return m_search->search( searchBase, scope, filter, attributes, pagesize );
00074 }
00075 
00076 void LdapModel::LdapModelPrivate::setSearchType( SearchType t, LdapModelDNNode *item )
00077 {
00078   //kDebug(5322) << "LdapModel::LdapModelPrivate::setSearchType() : item =" << item;
00079   m_searchType = t;
00080   m_searchItem = item;
00081 }
00082 
00083 void LdapModel::LdapModelPrivate::recreateRootItem()
00084 {
00085   //kDebug(5322) << "LdapModel::LdapModelPrivate::recreateRootItem()";
00086   if ( m_root ) {
00087     delete m_root;
00088     m_root = 0;
00089   }
00090   m_root = new LdapModelDNNode;
00091   //kDebug(5322) << "&m_root =" << &m_root;
00092 }
00093 
00094 void LdapModel::LdapModelPrivate::createConnections()
00095 {
00096   connect( search(), SIGNAL( result( KLDAP::LdapSearch* ) ),
00097            m_parent, SLOT( gotSearchResult( KLDAP::LdapSearch* ) ) );
00098   connect( search(), SIGNAL( data( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) ),
00099            m_parent, SLOT( gotSearchData( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) ) );
00100 }
00101 
00102 void LdapModel::LdapModelPrivate::populateRootToBaseDN()
00103 {
00104   //kDebug(5322) << "LdapModel::LdapModelPrivate::populateRootToBaseDN()";
00105 
00106   if ( baseDN().isEmpty() ) {
00107     // Query the server for the base DN
00108     //kDebug(5322) << "Searching for the baseDN";
00109     setSearchType( LdapModelPrivate::NamingContexts, rootNode() );
00110     search( LdapDN(), LdapUrl::Base, QString(), QStringList() << "namingContexts" );
00111     return;
00112   }
00113 
00114   // Start a search for the details of the baseDN object
00115   //kDebug(5322) << "Searching for attributes of the baseDN";
00116   searchResults().clear();
00117   setSearchType( LdapModelPrivate::BaseDN, rootNode() );
00118   search( baseDN(), LdapUrl::Base, QString(), QStringList() << "dn" << "objectClass" );
00119 }
00120 
00121 void LdapModel::LdapModelPrivate::gotSearchResult( KLDAP::LdapSearch *search )
00122 {
00123   Q_UNUSED( search );
00124   kDebug(5322) << "LdapModel::LdapModelPrivate::gotSearchResult()";
00125 
00126   switch ( searchType() ) {
00127   case LdapModelPrivate::NamingContexts:
00128   {
00129     // Set the baseDN
00130     QString baseDN;
00131     if ( !searchResults().isEmpty() &&
00132          searchResults().at( 0 ).hasAttribute( "namingContexts" ) ) {
00133       baseDN = searchResults().at( 0 ).value( "namingContexts" );
00134       //kDebug(5322) << "Found baseDN =" << baseDN;
00135     }
00136     setBaseDN( LdapDN( baseDN ) );
00137 
00138     // Flag that we are no longer searching for the baseDN
00139     setSearchType( LdapModelPrivate::NotSearching );
00140 
00141     // Populate the root item
00142     populateRootToBaseDN();
00143 
00144     break;
00145   }
00146   case LdapModelPrivate::BaseDN:
00147   {
00148     //kDebug(5322) << "Found details of the baseDN object."
00149     //             << "Creating objects down to this level.";
00150 
00151     // Get the baseDN LdapObject
00152     LdapObject baseDNObj = searchResults().at( 0 );
00153 
00154     // How many levels of items do we need to create?
00155     int depth = baseDNObj.dn().depth();
00156 
00157     // Create items that represent objects down to the baseDN
00158     LdapModelDNNode *parent = rootNode();
00159     LdapModelDNNode *item = 0;
00160     for ( int i = 0; i < depth; i++ ) {
00161       QString dn = baseDN().toString( i );
00162       kDebug(5322) << "Creating item for DN :" << dn;
00163 
00164       //LdapObject obj( dn );
00165       item = new LdapModelDNNode( parent, LdapDN( dn ) );
00166       parent = item;
00167     }
00168 
00169     // Store the search result
00170     if ( item )
00171       item->setLdapObject( baseDNObj );
00172 
00173     // Flag that we are no longer searching
00174     setSearchType( LdapModelPrivate::NotSearching );
00175     //emit( layoutChanged() );
00176 
00177     // Let the world know we are ready for action
00178     emit m_parent->ready();
00179 
00180     break;
00181   }
00182   case LdapModelPrivate::ChildObjects:
00183   {
00184     //kDebug(5322) << "Found" << searchResults().size() << "child objects";
00185 
00186     if ( searchResults().size() != 0 )
00187     {
00188       // Create an index for the soon-to-be-a-parent item
00189       LdapModelDNNode *parentNode = searchItem();
00190       int r = parentNode->row();
00191       QModelIndex parentIndex = m_parent->createIndex( r, 0, parentNode );
00192 
00193       m_parent->beginInsertRows( parentIndex, 0, searchResults().size() );
00194       for ( int i = 0; i < searchResults().size(); i++ ) {
00195         LdapObject object = searchResults().at( i );
00196         LdapModelDNNode *item = new LdapModelDNNode( parentNode, object.dn() );
00197         item->setLdapObject( object );
00198       }
00199 
00200       m_parent->endInsertRows();
00201       emit m_parent->layoutChanged();
00202     }
00203 
00204     // Flag that we are no longer searching
00205     setSearchType( LdapModelPrivate::NotSearching );
00206 
00207     break;
00208   }
00209   default:
00210     break;
00211   }
00212 }
00213 
00214 void LdapModel::LdapModelPrivate::gotSearchData( KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj )
00215 {
00216   Q_UNUSED( search );
00217   //kDebug(5322) << "LdapModel::LdapModelPrivate::gotSearchData()";
00218   //kDebug(5322) << "Object:";
00219   //kDebug(5322) << obj.toString();
00220   searchResults().append( obj );
00221 }

KLDAP Library

Skip menu "KLDAP Library"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal