• Skip to content
  • Skip to link menu
KDE 4.8 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

KLDAP Library

ldapmodel.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.h"
00022 #include "ldapmodel_p.h"
00023 #include "ldapmodelnode_p.h"
00024 #include "ldapsearch.h"
00025 
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <kglobal.h>
00029 
00030 static const KCatalogLoader loader( "libkldap" );
00031 
00032 using namespace KLDAP;
00033 
00034 LdapModel::LdapModel( QObject *parent )
00035   : QAbstractItemModel( parent ),
00036     m_d( new LdapModelPrivate( this ) )
00037 {
00038   m_d->createConnections();
00039 }
00040 
00041 LdapModel::LdapModel( LdapConnection &connection, QObject *parent )
00042   : QAbstractItemModel( parent ),
00043     m_d( new LdapModelPrivate( this, connection ) )
00044 {
00045   m_d->createConnections();
00046 
00047   // Populate items from the root object to that representing the baseDN
00048   m_d->populateRootToBaseDN();
00049 }
00050 
00051 LdapModel::~LdapModel()
00052 {
00053   delete m_d;
00054 }
00055 
00056 void LdapModel::setConnection( LdapConnection &connection )
00057 {
00058   m_d->setConnection( connection );
00059 
00060   // Refresh the model
00061   m_d->recreateRootItem();
00062 
00063   // Populate the root object by searching the baseDN
00064   m_d->populateRootToBaseDN();
00065 }
00066 
00067 QModelIndex LdapModel::parent( const QModelIndex &child ) const
00068 {
00069   if ( !child.isValid() ) {
00070     return QModelIndex();
00071   }
00072 
00073   LdapModelNode *childItem = static_cast<LdapModelNode*>( child.internalPointer() );
00074   LdapModelDNNode *parentItem = childItem->parent();
00075 
00076   if ( parentItem == m_d->rootNode() ) {
00077     return QModelIndex();
00078   }
00079 
00080   return createIndex( parentItem->row(), 0, parentItem );
00081 }
00082 
00083 QModelIndex LdapModel::index( int row, int col, const QModelIndex &parent ) const
00084 {
00085   // Retrieve a pointer to the parent item
00086   LdapModelDNNode *parentItem;
00087   if ( !parent.isValid() ) {
00088     parentItem = m_d->rootNode();
00089   } else {
00090     parentItem = static_cast<LdapModelDNNode*>( parent.internalPointer() );
00091   }
00092 
00093   LdapModelNode *childItem = parentItem->child( row );
00094   if ( childItem ) {
00095     return createIndex( row, col, childItem );
00096   }
00097   kDebug() << "Could not create valid index for row =" << row << ", col =" << col;
00098   return QModelIndex();
00099 }
00100 
00101 QVariant LdapModel::data( const QModelIndex &index, int role ) const
00102 {
00103   if ( !index.isValid() ) {
00104     return QVariant();
00105   }
00106 
00107   if ( role == Qt::DisplayRole ) {
00108     // This is what gets displayed by the view delegates.
00109     LdapModelNode *node = static_cast<LdapModelNode*>( index.internalPointer() );
00110     if ( node->nodeType() == LdapModelNode::DN ) {
00111       LdapModelDNNode* dn = static_cast<LdapModelDNNode*>( node );
00112       if ( index.column() == 0 ) {
00113         return dn->dn().rdnString();
00114       } else {
00115         return QVariant();
00116       }
00117     } else {
00118       LdapModelAttrNode* attr = static_cast<LdapModelAttrNode*>( node );
00119       if ( index.column() == 0 ) {
00120         return QVariant( attr->attributeName() );
00121       } else {
00122         return QVariant( QString( attr->attributeData().constData() ) );
00123       }
00124     }
00125   } else if ( role == NodeTypeRole ) {
00126     LdapModelNode* node = static_cast<LdapModelNode*>( index.internalPointer() );
00127     return QVariant( int( node->nodeType() ) );
00128   }
00129 
00134   return QVariant();
00135 }
00136 
00137 bool LdapModel::setData( const QModelIndex &index,
00138                          const QVariant &value,
00139                          int role )
00140 {
00141   Q_UNUSED( index );
00142   Q_UNUSED( value );
00143   Q_UNUSED( role );
00144   return false;
00145 }
00146 
00147 QVariant LdapModel::headerData( int section, Qt::Orientation orientation, int role ) const
00148 {
00149   if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
00150     if ( section == 0 ) {
00151       return i18n( "Attribute" );
00152     } else {
00153       return i18n( "Value" );
00154     }
00155   }
00156 
00157   return QVariant();
00158 }
00159 
00160 Qt::ItemFlags LdapModel::flags( const QModelIndex &index ) const
00161 {
00163   if ( !index.isValid() ) {
00164     return Qt::ItemIsEnabled;
00165   }
00166 
00167   return Qt::ItemFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
00168 }
00169 
00170 int LdapModel::columnCount( const QModelIndex &parent ) const
00171 {
00172   LdapModelDNNode *parentNode =
00173     parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
00174   return parentNode->columnCount();
00175 }
00176 
00177 int LdapModel::rowCount( const QModelIndex &parent ) const
00178 {
00179   if ( parent.column() > 0 ) {
00180     return 0;
00181   }
00182 
00183   const LdapModelDNNode *parentNode =
00184     parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
00185   return parentNode->childCount();
00186 }
00187 
00188 bool LdapModel::hasChildren( const QModelIndex &parent ) const
00189 {
00190   // We return true unless the item has been populated and we are able to do a definitive test
00191   const LdapModelNode *node = parent.isValid() ?
00192                               static_cast<const LdapModelNode*>( parent.internalPointer() ) :
00193                               m_d->rootNode();
00194 
00195   if ( node->nodeType() != LdapModelNode::DN ) {
00196     return false;
00197   }
00198 
00199   const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
00200   if ( !parent.isValid() || parentNode->isPopulated() ) {
00201     return parentNode->childCount() > 0;
00202   }
00203   return true;
00204 }
00205 
00206 bool LdapModel::canFetchMore( const QModelIndex &parent ) const
00207 {
00208   const LdapModelDNNode *parentNode =
00209     parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
00210   return !parentNode->isPopulated();
00211 }
00212 
00213 void LdapModel::fetchMore( const QModelIndex &parent )
00214 {
00215   LdapModelDNNode *parentNode =
00216     parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
00217 
00218   // Search for the immediate children of parentItem.
00219   m_d->searchResults().clear();
00220   m_d->setSearchType( LdapModelPrivate::ChildObjects, parentNode );
00221   m_d->search( parentNode->dn(),  // DN to search from
00222                LdapUrl::One,      // What to search
00223                QString() );       // Attributes to retrieve
00224   parentNode->setPopulated( true );
00225 }
00226 
00227 bool LdapModel::insertRows( int row, int count,
00228                             const QModelIndex &parent )
00229 {
00230   Q_UNUSED( row );
00231   Q_UNUSED( count );
00232   Q_UNUSED( parent );
00233   return false;
00234 }
00235 
00236 bool LdapModel::removeRows( int row, int count,
00237                             const QModelIndex &parent )
00238 {
00239   Q_UNUSED( row );
00240   Q_UNUSED( count );
00241   Q_UNUSED( parent );
00242   return false;
00243 }
00244 
00245 void LdapModel::sort( int column, Qt::SortOrder order )
00246 {
00247   Q_UNUSED( column );
00248   Q_UNUSED( order );
00249 }
00250 
00251 Qt::DropActions LdapModel::supportedDropActions() const
00252 {
00253   return Qt::MoveAction;
00254 }
00255 
00256 QMimeData *LdapModel::mimeData( const QModelIndexList &indexes ) const
00257 {
00258   Q_UNUSED( indexes );
00259   return 0;
00260 }
00261 
00262 bool LdapModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
00263                               int row, int column, const QModelIndex &parent )
00264 {
00266   Q_UNUSED( data );
00267   Q_UNUSED( action );
00268   Q_UNUSED( row );
00269   Q_UNUSED( column );
00270   Q_UNUSED( parent );
00271   return false;
00272 }
00273 
00274 bool LdapModel::hasChildrenOfType( const QModelIndex &parent, LdapDataType type ) const
00275 {
00276   // Map from LdapDataType to our internal NodeType
00277   LdapModelNode::NodeType nodeType;
00278   switch ( type ) {
00279     case Attribute:
00280       nodeType = LdapModelNode::Attr;
00281       break;
00282 
00283     case DistinguishedName:
00284     default:
00285       nodeType = LdapModelNode::DN;
00286       break;
00287   }
00288 
00289   const LdapModelNode *node = parent.isValid() ?
00290                               static_cast<const LdapModelNode*>( parent.internalPointer() ) :
00291                               m_d->rootNode();
00292 
00293   const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
00294   if ( !parent.isValid() || parentNode->isPopulated() ) {
00295     // Check to see if the parent has any children of the specified type
00296     const QList<LdapModelNode*>& children = parentNode->children();
00297     foreach ( LdapModelNode *child, children ) {
00298       if ( child->nodeType() == nodeType ) {
00299         return true;
00300       }
00301     }
00302 
00303     // Either there are no children or only children of a different type
00304     return false;
00305   }
00306 
00307   // If the node is not populated or is the root node (invalid), then return
00308   // true to be on the safe side.
00309   return true;
00310 }
00311 
00312 void LdapModel::revert()
00313 {
00314 
00315 }
00316 
00317 bool LdapModel::submit()
00318 {
00319   return false;
00320 }
00321 
00322 #include "ldapmodel.moc"

KLDAP Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.6.1
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