File: ldapmodel.cpp

package info (click to toggle)
kdepimlibs 4%3A4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 22,300 kB
  • ctags: 16,353
  • sloc: cpp: 118,725; ansic: 17,101; xml: 2,722; perl: 1,791; yacc: 954; sh: 168; ruby: 60; makefile: 44
file content (319 lines) | stat: -rw-r--r-- 8,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
  This file is part of libkldap.
  Copyright (c) 2006 Sean Harmer <sh@theharmers.co.uk>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General  Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "ldapmodel.h"
#include "ldapmodel_p.h"
#include "ldapmodelnode_p.h"
#include "ldapsearch.h"

#include <kdebug.h>
#include <klocale.h>

using namespace KLDAP;

LdapModel::LdapModel( QObject *parent )
  : QAbstractItemModel( parent ),
    m_d( new LdapModelPrivate( this ) )
{
  m_d->createConnections();
}

LdapModel::LdapModel( LdapConnection &connection, QObject *parent )
  : QAbstractItemModel( parent ),
    m_d( new LdapModelPrivate( this, connection ) )
{
  m_d->createConnections();

  // Populate items from the root object to that representing the baseDN
  m_d->populateRootToBaseDN();
}

LdapModel::~LdapModel()
{
  delete m_d;
}

void LdapModel::setConnection( LdapConnection &connection )
{
  m_d->setConnection( connection );

  // Refresh the model
  m_d->recreateRootItem();

  // Populate the root object by searching the baseDN
  m_d->populateRootToBaseDN();
}

QModelIndex LdapModel::parent( const QModelIndex &child ) const
{
  if ( !child.isValid() ) {
    return QModelIndex();
  }

  LdapModelNode *childItem = static_cast<LdapModelNode*>( child.internalPointer() );
  LdapModelDNNode *parentItem = childItem->parent();

  if ( parentItem == m_d->rootNode() ) {
    return QModelIndex();
  }

  return createIndex( parentItem->row(), 0, parentItem );
}

QModelIndex LdapModel::index( int row, int col, const QModelIndex &parent ) const
{
  // Retrieve a pointer to the parent item
  LdapModelDNNode *parentItem;
  if ( !parent.isValid() ) {
    parentItem = m_d->rootNode();
  } else {
    parentItem = static_cast<LdapModelDNNode*>( parent.internalPointer() );
  }

  LdapModelNode *childItem = parentItem->child( row );
  if ( childItem ) {
    return createIndex( row, col, childItem );
  }
  kDebug() << "Could not create valid index for row =" << row << ", col =" << col;
  return QModelIndex();
}

QVariant LdapModel::data( const QModelIndex &index, int role ) const
{
  if ( !index.isValid() ) {
    return QVariant();
  }

  if ( role == Qt::DisplayRole ) {
    // This is what gets displayed by the view delegates.
    LdapModelNode *node = static_cast<LdapModelNode*>( index.internalPointer() );
    if ( node->nodeType() == LdapModelNode::DN ) {
      LdapModelDNNode* dn = static_cast<LdapModelDNNode*>( node );
      if ( index.column() == 0 ) {
        return dn->dn().rdnString();
      } else {
        return QVariant();
      }
    } else {
      LdapModelAttrNode* attr = static_cast<LdapModelAttrNode*>( node );
      if ( index.column() == 0 ) {
        return QVariant( attr->attributeName() );
      } else {
        return QVariant( QString( attr->attributeData().constData() ) );
      }
    }
  } else if ( role == NodeTypeRole ) {
    LdapModelNode* node = static_cast<LdapModelNode*>( index.internalPointer() );
    return QVariant( int( node->nodeType() ) );
  }

  /** \todo Include support for nice decorative icons dependent upon
      the objectClass + other role data. */
  /** \todo Include support for other roles as needed */

  return QVariant();
}

bool LdapModel::setData( const QModelIndex &index,
                         const QVariant &value,
                         int role )
{
  Q_UNUSED( index );
  Q_UNUSED( value );
  Q_UNUSED( role );
  return false;
}

QVariant LdapModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
    if ( section == 0 ) {
      return QString( i18n( "Attribute" ) );
    } else {
      return QString( i18n( "Value" ) );
    }
  }

  return QVariant();
}

Qt::ItemFlags LdapModel::flags( const QModelIndex &index ) const
{
  /** \TODO Read-only for now, make read-write upon request */
  if ( !index.isValid() ) {
    return Qt::ItemIsEnabled;
  }

  return Qt::ItemFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
}

int LdapModel::columnCount( const QModelIndex &parent ) const
{
  LdapModelDNNode *parentNode =
    parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
  return parentNode->columnCount();
}

int LdapModel::rowCount( const QModelIndex &parent ) const
{
  if ( parent.column() > 0 ) {
    return 0;
  }

  const LdapModelDNNode *parentNode =
    parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
  return parentNode->childCount();
}

bool LdapModel::hasChildren( const QModelIndex &parent ) const
{
  // We return true unless the item has been populated and we are able to do a definitive test
  const LdapModelNode *node = parent.isValid() ?
                              static_cast<const LdapModelNode*>( parent.internalPointer() ) :
                              m_d->rootNode();

  if ( node->nodeType() != LdapModelNode::DN ) {
    return false;
  }

  const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
  if ( !parent.isValid() || parentNode->isPopulated() ) {
    return parentNode->childCount() > 0;
  }
  return true;
}

bool LdapModel::canFetchMore( const QModelIndex &parent ) const
{
  const LdapModelDNNode *parentNode =
    parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();
  return !parentNode->isPopulated();
}

void LdapModel::fetchMore( const QModelIndex &parent )
{
  LdapModelDNNode *parentNode =
    parent.isValid() ? static_cast<LdapModelDNNode*>( parent.internalPointer() ) : m_d->rootNode();

  // Search for the immediate children of parentItem.
  m_d->searchResults().clear();
  m_d->setSearchType( LdapModelPrivate::ChildObjects, parentNode );
  m_d->search( parentNode->dn(),  // DN to search from
               LdapUrl::One,      // What to search
               QString() );       // Attributes to retrieve
  parentNode->setPopulated( true );
}

bool LdapModel::insertRows( int row, int count,
                            const QModelIndex &parent )
{
  Q_UNUSED( row );
  Q_UNUSED( count );
  Q_UNUSED( parent );
  return false;
}

bool LdapModel::removeRows( int row, int count,
                            const QModelIndex &parent )
{
  Q_UNUSED( row );
  Q_UNUSED( count );
  Q_UNUSED( parent );
  return false;
}

void LdapModel::sort( int column, Qt::SortOrder order )
{
  Q_UNUSED( column );
  Q_UNUSED( order );
}

Qt::DropActions LdapModel::supportedDropActions() const
{
  return Qt::MoveAction;
}

QMimeData *LdapModel::mimeData( const QModelIndexList &indexes ) const
{
  Q_UNUSED( indexes );
  return 0;
}

bool LdapModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
                              int row, int column, const QModelIndex &parent )
{
  /** \todo Implement drag and drop for LdapModel */
  Q_UNUSED( data );
  Q_UNUSED( action );
  Q_UNUSED( row );
  Q_UNUSED( column );
  Q_UNUSED( parent );
  return false;
}

bool LdapModel::hasChildrenOfType( const QModelIndex &parent, LdapDataType type ) const
{
  // Map from LdapDataType to our internal NodeType
  LdapModelNode::NodeType nodeType;
  switch ( type ) {
    case Attribute:
      nodeType = LdapModelNode::Attr;
      break;

    case DistinguishedName:
    default:
      nodeType = LdapModelNode::DN;
      break;
  }

  const LdapModelNode *node = parent.isValid() ?
                              static_cast<const LdapModelNode*>( parent.internalPointer() ) :
                              m_d->rootNode();

  const LdapModelDNNode* parentNode = static_cast<const LdapModelDNNode*>( node );
  if ( !parent.isValid() || parentNode->isPopulated() ) {
    // Check to see if the parent has any children of the specified type
    const QList<LdapModelNode*>& children = parentNode->children();
    foreach ( LdapModelNode *child, children ) {
      if ( child->nodeType() == nodeType ) {
        return true;
      }
    }

    // Either there are no children or only children of a different type
    return false;
  }

  // If the node is not populated or is the root node (invalid), then return
  // true to be on the safe side.
  return true;
}

void LdapModel::revert()
{

}

bool LdapModel::submit()
{
  return false;
}

#include "ldapmodel.moc"