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

KLDAP Library

  • sources
  • kde-4.14
  • kdepimlibs
  • kldap
ldapmodel.cpp
1 /*
2  This file is part of libkldap.
3  Copyright (c) 2006 Sean Harmer <sh@theharmers.co.uk>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public 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
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "ldapmodel.h"
22 #include "ldapmodel_p.h"
23 #include "ldapmodelnode_p.h"
24 #include "ldapsearch.h"
25 
26 #include <kdebug.h>
27 #include <klocalizedstring.h>
28 #include <kglobal.h>
29 
30 static const KCatalogLoader loader( QLatin1String("libkldap") );
31 
32 using namespace KLDAP;
33 
34 LdapModel::LdapModel( QObject *parent )
35  : QAbstractItemModel( parent ),
36  m_d( new LdapModelPrivate( this ) )
37 {
38  m_d->createConnections();
39 }
40 
41 LdapModel::LdapModel( LdapConnection &connection, QObject *parent )
42  : QAbstractItemModel( parent ),
43  m_d( new LdapModelPrivate( this, connection ) )
44 {
45  m_d->createConnections();
46 
47  // Populate items from the root object to that representing the baseDN
48  m_d->populateRootToBaseDN();
49 }
50 
51 LdapModel::~LdapModel()
52 {
53  delete m_d;
54 }
55 
56 void LdapModel::setConnection( LdapConnection &connection )
57 {
58  m_d->setConnection( connection );
59 
60  // Refresh the model
61  m_d->recreateRootItem();
62 
63  // Populate the root object by searching the baseDN
64  m_d->populateRootToBaseDN();
65 }
66 
67 QModelIndex LdapModel::parent( const QModelIndex &child ) const
68 {
69  if ( !child.isValid() ) {
70  return QModelIndex();
71  }
72 
73  LdapModelNode *childItem = static_cast<LdapModelNode*>( child.internalPointer() );
74  LdapModelDNNode *parentItem = childItem->parent();
75 
76  if ( parentItem == m_d->rootNode() ) {
77  return QModelIndex();
78  }
79 
80  return createIndex( parentItem->row(), 0, parentItem );
81 }
82 
83 QModelIndex LdapModel::index( int row, int col, const QModelIndex &parent ) const
84 {
85  // Retrieve a pointer to the parent item
86  LdapModelDNNode *parentItem;
87  if ( !parent.isValid() ) {
88  parentItem = m_d->rootNode();
89  } else {
90  parentItem = static_cast<LdapModelDNNode*>( parent.internalPointer() );
91  }
92 
93  LdapModelNode *childItem = parentItem->child( row );
94  if ( childItem ) {
95  return createIndex( row, col, childItem );
96  }
97  kDebug() << "Could not create valid index for row =" << row << ", col =" << col;
98  return QModelIndex();
99 }
100 
101 QVariant LdapModel::data( const QModelIndex &index, int role ) const
102 {
103  if ( !index.isValid() ) {
104  return QVariant();
105  }
106 
107  if ( role == Qt::DisplayRole ) {
108  // This is what gets displayed by the view delegates.
109  LdapModelNode *node = static_cast<LdapModelNode*>( index.internalPointer() );
110  if ( node->nodeType() == LdapModelNode::DN ) {
111  LdapModelDNNode* dn = static_cast<LdapModelDNNode*>( node );
112  if ( index.column() == 0 ) {
113  return dn->dn().rdnString();
114  } else {
115  return QVariant();
116  }
117  } else {
118  LdapModelAttrNode* attr = static_cast<LdapModelAttrNode*>( node );
119  if ( index.column() == 0 ) {
120  return QVariant( attr->attributeName() );
121  } else {
122  return QVariant( QLatin1String( attr->attributeData().constData() ) );
123  }
124  }
125  } else if ( role == NodeTypeRole ) {
126  LdapModelNode* node = static_cast<LdapModelNode*>( index.internalPointer() );
127  return QVariant( int( node->nodeType() ) );
128  }
129 
134  return QVariant();
135 }
136 
137 bool LdapModel::setData( const QModelIndex &index,
138  const QVariant &value,
139  int role )
140 {
141  Q_UNUSED( index );
142  Q_UNUSED( value );
143  Q_UNUSED( role );
144  return false;
145 }
146 
147 QVariant LdapModel::headerData( int section, Qt::Orientation orientation, int role ) const
148 {
149  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
150  if ( section == 0 ) {
151  return i18n( "Attribute" );
152  } else {
153  return i18n( "Value" );
154  }
155  }
156 
157  return QVariant();
158 }
159 
160 Qt::ItemFlags LdapModel::flags( const QModelIndex &index ) const
161 {
163  if ( !index.isValid() ) {
164  return Qt::ItemIsEnabled;
165  }
166 
167  return Qt::ItemFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
168 }
169 
170 int LdapModel::columnCount( const QModelIndex &parent ) const
171 {
172  LdapModelDNNode *parentNode =
173  parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
174  return parentNode->columnCount();
175 }
176 
177 int LdapModel::rowCount( const QModelIndex &parent ) const
178 {
179  if ( parent.column() > 0 ) {
180  return 0;
181  }
182 
183  const LdapModelDNNode *parentNode =
184  parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
185  return parentNode->childCount();
186 }
187 
188 bool LdapModel::hasChildren( const QModelIndex &parent ) const
189 {
190  // We return true unless the item has been populated and we are able to do a definitive test
191  const LdapModelNode *node = parent.isValid() ?
192  static_cast<const LdapModelNode*>( parent.internalPointer() ) :
193  m_d->rootNode();
194 
195  if ( node->nodeType() != LdapModelNode::DN ) {
196  return false;
197  }
198 
199  const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
200  if ( !parent.isValid() || parentNode->isPopulated() ) {
201  return parentNode->childCount() > 0;
202  }
203  return true;
204 }
205 
206 bool LdapModel::canFetchMore( const QModelIndex &parent ) const
207 {
208  const LdapModelDNNode *parentNode =
209  parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
210  return !parentNode->isPopulated();
211 }
212 
213 void LdapModel::fetchMore( const QModelIndex &parent )
214 {
215  LdapModelDNNode *parentNode =
216  parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
217 
218  // Search for the immediate children of parentItem.
219  m_d->searchResults().clear();
220  m_d->setSearchType( LdapModelPrivate::ChildObjects, parentNode );
221  m_d->search( parentNode->dn(), // DN to search from
222  LdapUrl::One, // What to search
223  QString() ); // Attributes to retrieve
224  parentNode->setPopulated( true );
225 }
226 
227 bool LdapModel::insertRows( int row, int count,
228  const QModelIndex &parent )
229 {
230  Q_UNUSED( row );
231  Q_UNUSED( count );
232  Q_UNUSED( parent );
233  return false;
234 }
235 
236 bool LdapModel::removeRows( int row, int count,
237  const QModelIndex &parent )
238 {
239  Q_UNUSED( row );
240  Q_UNUSED( count );
241  Q_UNUSED( parent );
242  return false;
243 }
244 
245 void LdapModel::sort( int column, Qt::SortOrder order )
246 {
247  Q_UNUSED( column );
248  Q_UNUSED( order );
249 }
250 
251 Qt::DropActions LdapModel::supportedDropActions() const
252 {
253  return Qt::MoveAction;
254 }
255 
256 QMimeData *LdapModel::mimeData( const QModelIndexList &indexes ) const
257 {
258  Q_UNUSED( indexes );
259  return 0;
260 }
261 
262 bool LdapModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
263  int row, int column, const QModelIndex &parent )
264 {
266  Q_UNUSED( data );
267  Q_UNUSED( action );
268  Q_UNUSED( row );
269  Q_UNUSED( column );
270  Q_UNUSED( parent );
271  return false;
272 }
273 
274 bool LdapModel::hasChildrenOfType( const QModelIndex &parent, LdapDataType type ) const
275 {
276  // Map from LdapDataType to our internal NodeType
277  LdapModelNode::NodeType nodeType;
278  switch ( type ) {
279  case Attribute:
280  nodeType = LdapModelNode::Attr;
281  break;
282 
283  case DistinguishedName:
284  default:
285  nodeType = LdapModelNode::DN;
286  break;
287  }
288 
289  const LdapModelNode *node = parent.isValid() ?
290  static_cast<const LdapModelNode*>( parent.internalPointer() ) :
291  m_d->rootNode();
292 
293  const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
294  if ( !parent.isValid() || parentNode->isPopulated() ) {
295  // Check to see if the parent has any children of the specified type
296  const QList<LdapModelNode*>& children = parentNode->children();
297  foreach ( LdapModelNode *child, children ) {
298  if ( child->nodeType() == nodeType ) {
299  return true;
300  }
301  }
302 
303  // Either there are no children or only children of a different type
304  return false;
305  }
306 
307  // If the node is not populated or is the root node (invalid), then return
308  // true to be on the safe side.
309  return true;
310 }
311 
312 void LdapModel::revert()
313 {
314 
315 }
316 
317 bool LdapModel::submit()
318 {
319  return false;
320 }
321 
322 #include "moc_ldapmodel.cpp"
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QList::clear
void clear()
QModelIndex
KLDAP::LdapModel::hasChildrenOfType
bool hasChildrenOfType(const QModelIndex &parent, LdapDataType type) const
Checks to see if the item referenced by parent has any children of the type type. ...
Definition: ldapmodel.cpp:274
KLDAP::LdapModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Reimplemented from QAbstractItemModel::columnCount().
Definition: ldapmodel.cpp:170
KLDAP::LdapModel::insertRows
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Reimplemented from QAbstractItemModel::insertRows().
Definition: ldapmodel.cpp:227
QObject::children
const QObjectList & children() const
KLDAP::LdapModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Reimplemented from QAbstractItemModel::headerData().
Definition: ldapmodel.cpp:147
KLDAP::LdapModelDNNode
Definition: ldapmodelnode_p.h:68
KLDAP::LdapModel::hasChildren
virtual bool hasChildren(const QModelIndex &parent) const
Reimplemented from QAbstractItemModel::hasChildren().
Definition: ldapmodel.cpp:188
KLDAP::LdapModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Reimplemented from QAbstractItemModel::data().
Definition: ldapmodel.cpp:101
KLDAP::LdapModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Reimplemented from QAbstractItemModel::rowCount().
Definition: ldapmodel.cpp:177
QMimeData
KLDAP::LdapModel::canFetchMore
virtual bool canFetchMore(const QModelIndex &parent) const
Reimplemented from QAbstractItemModel::canFetchMore().
Definition: ldapmodel.cpp:206
KLDAP::LdapModel::submit
virtual bool submit()
Reimplemented from QAbstractItemModel::revert().
Definition: ldapmodel.cpp:317
KLDAP::LdapModel::fetchMore
virtual void fetchMore(const QModelIndex &parent)
Reimplemented from QAbstractItemModel::fetchMore().
Definition: ldapmodel.cpp:213
QModelIndex::isValid
bool isValid() const
KLDAP::LdapModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented from QAbstractItemModel::flags().
Definition: ldapmodel.cpp:160
QObject
KLDAP::LdapModel::supportedDropActions
virtual Qt::DropActions supportedDropActions() const
Reimplemented from QAbstractItemModel::supportedDropActions().
Definition: ldapmodel.cpp:251
QByteArray::constData
const char * constData() const
KLDAP::LdapModel::revert
virtual void revert()
Reimplemented from QAbstractItemModel::revert().
Definition: ldapmodel.cpp:312
QModelIndex::internalPointer
void * internalPointer() const
QString
QList
KLDAP::LdapUrl::One
The level of the url and the one below.
Definition: ldapurl.h:60
KLDAP::LdapModel::LdapModelPrivate
Definition: ldapmodel_p.h:37
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
KLDAP::LdapConnection
This class represents a connection to an LDAP server.
Definition: ldapconnection.h:36
KLDAP::LdapModelNode
Definition: ldapmodelnode_p.h:40
KLDAP::LdapModel::index
virtual QModelIndex index(int row, int col, const QModelIndex &parent) const
Reimplemented from QAbstractItemModel::index().
Definition: ldapmodel.cpp:83
KLDAP::LdapModel::sort
virtual void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Reimplemented from QAbstractItemModel::removeRows().
Definition: ldapmodel.cpp:245
KLDAP::LdapModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Reimplemented from QAbstractItemModel::removeRows().
Definition: ldapmodel.cpp:236
QLatin1String
Qt::DropActions
typedef DropActions
KLDAP::LdapModelAttrNode
Definition: ldapmodelnode_p.h:102
KLDAP::LdapModel::mimeData
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Reimplemented from QAbstractItemModel::mimedata().
Definition: ldapmodel.cpp:256
KLDAP::LdapModel::dropMimeData
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented from QAbstractItemModel::dropMimedata().
Definition: ldapmodel.cpp:262
QModelIndex::column
int column() const
QAbstractItemModel
KLDAP::LdapModel::LdapModel
LdapModel(QObject *parent=0)
Constructs an LdapModel.
Definition: ldapmodel.cpp:34
QObject::parent
QObject * parent() const
KLDAP::LdapModel::setConnection
void setConnection(LdapConnection &connection)
Set the connection that the model should use.
Definition: ldapmodel.cpp:56
KLDAP::LdapModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Reimplemented from QAbstractItemModel::setData().
Definition: ldapmodel.cpp:137
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KLDAP Library

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

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