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_p.h"
22#include "ldapmodelnode_p.h"
23#include "ldapsearch.h"
24
25#include <kdebug.h>
26
27using namespace KLDAP;
28
29LdapModel::LdapModelPrivate::LdapModelPrivate( LdapModel *parent )
30 : m_parent( parent ),
31 m_root( new LdapModelDNNode ),
32 m_search( new LdapSearch ),
33 m_searchResultObjects(),
34 m_baseDN(),
35 m_searchType( NotSearching ),
36 m_searchItem( 0 )
37{
38}
39
40LdapModel::LdapModelPrivate::LdapModelPrivate( LdapModel *parent, LdapConnection &connection )
41 : m_parent( parent ),
42 m_root( new LdapModelDNNode ),
43 m_search( new LdapSearch( connection ) ),
44 m_searchResultObjects(),
45 m_baseDN(),
46 m_searchType( NotSearching ),
47 m_searchItem( 0 )
48{
49}
50
51LdapModel::LdapModelPrivate::~LdapModelPrivate()
52{
53 delete m_root;
54
55 delete m_search;
56}
57
58void LdapModel::LdapModelPrivate::setConnection( LdapConnection &connection )
59{
60 m_search->setConnection( connection );
61}
62
63bool LdapModel::LdapModelPrivate::search( const LdapDN &searchBase,
64 LdapUrl::Scope scope,
65 const QString &filter,
66 const QStringList &attributes,
67 int pagesize )
68{
69 return m_search->search( searchBase, scope, filter, attributes, pagesize );
70}
71
72void LdapModel::LdapModelPrivate::setSearchType( SearchType t, LdapModelDNNode *item )
73{
74 //kDebug() << "item =" << item;
75 m_searchType = t;
76 m_searchItem = item;
77}
78
79void LdapModel::LdapModelPrivate::recreateRootItem()
80{
81 //kDebug();
82 delete m_root;
83 m_root = new LdapModelDNNode;
84 //kDebug() << "&m_root =" << &m_root;
85}
86
87void LdapModel::LdapModelPrivate::createConnections()
88{
89 connect( search(), SIGNAL(result(KLDAP::LdapSearch*)),
90 m_parent, SLOT(gotSearchResult(KLDAP::LdapSearch*)) );
91 connect( search(), SIGNAL(data(KLDAP::LdapSearch*,KLDAP::LdapObject)),
92 m_parent, SLOT(gotSearchData(KLDAP::LdapSearch*,KLDAP::LdapObject)) );
93}
94
95void LdapModel::LdapModelPrivate::populateRootToBaseDN()
96{
97 //kDebug();
98
99 if ( baseDN().isEmpty() ) {
100 // Query the server for the base DN
101 //kDebug() << "Searching for the baseDN";
102 setSearchType( LdapModelPrivate::NamingContexts, rootNode() );
103 search( LdapDN(), LdapUrl::Base, QString(), QStringList() << QLatin1String("namingContexts" ));
104 return;
105 }
106
107 // Start a search for the details of the baseDN object
108 //kDebug() << "Searching for attributes of the baseDN";
109 searchResults().clear();
110 setSearchType( LdapModelPrivate::BaseDN, rootNode() );
111 search( baseDN(), LdapUrl::Base, QString(), QStringList() << QLatin1String("dn") << QLatin1String("objectClass") );
112}
113
114void LdapModel::LdapModelPrivate::gotSearchResult( KLDAP::LdapSearch *search )
115{
116 Q_UNUSED( search );
117 kDebug();
118
119 switch ( searchType() ) {
120 case LdapModelPrivate::NamingContexts:
121 {
122 // Set the baseDN
123 QString baseDN;
124 if ( !searchResults().isEmpty() &&
125 searchResults().at( 0 ).hasAttribute( QLatin1String("namingContexts") ) ) {
126 baseDN = QLatin1String(searchResults().at( 0 ).value( QLatin1String("namingContexts") ));
127 //kDebug() << "Found baseDN =" << baseDN;
128 }
129 setBaseDN( LdapDN( baseDN ) );
130
131 // Flag that we are no longer searching for the baseDN
132 setSearchType( LdapModelPrivate::NotSearching );
133
134 // Populate the root item
135 populateRootToBaseDN();
136
137 break;
138 }
139 case LdapModelPrivate::BaseDN:
140 {
141 //kDebug() << "Found details of the baseDN object."
142 // << "Creating objects down to this level.";
143
144 // Get the baseDN LdapObject
145 LdapObject baseDNObj = searchResults().at( 0 );
146
147 // How many levels of items do we need to create?
148 int depth = baseDNObj.dn().depth();
149
150 // Create items that represent objects down to the baseDN
151 LdapModelDNNode *parent = rootNode();
152 LdapModelDNNode *item = 0;
153 for ( int i = 0; i < depth; ++i ) {
154 QString dn = baseDN().toString( i );
155 kDebug() << "Creating item for DN :" << dn;
156
157 //LdapObject obj( dn );
158 item = new LdapModelDNNode( parent, LdapDN( dn ) );
159 parent = item;
160 }
161
162 // Store the search result
163 if ( item ) {
164 item->setLdapObject( baseDNObj );
165 }
166
167 // Flag that we are no longer searching
168 setSearchType( LdapModelPrivate::NotSearching );
169 //emit( layoutChanged() );
170
171 // Let the world know we are ready for action
172 emit m_parent->ready();
173
174 break;
175 }
176 case LdapModelPrivate::ChildObjects:
177 {
178 //kDebug() << "Found" << searchResults().size() << "child objects";
179
180 if ( searchResults().size() != 0 ) {
181 // Create an index for the soon-to-be-a-parent item
182 LdapModelDNNode *parentNode = searchItem();
183 int r = parentNode->row();
184 QModelIndex parentIndex = m_parent->createIndex( r, 0, parentNode );
185
186 m_parent->beginInsertRows( parentIndex, 0, searchResults().size() );
187 for ( int i = 0; i < searchResults().size(); i++ ) {
188 LdapObject object = searchResults().at( i );
189 LdapModelDNNode *item = new LdapModelDNNode( parentNode, object.dn() );
190 item->setLdapObject( object );
191 }
192
193 m_parent->endInsertRows();
194 emit m_parent->layoutChanged();
195 }
196
197 // Flag that we are no longer searching
198 setSearchType( LdapModelPrivate::NotSearching );
199
200 break;
201 }
202 default:
203 break;
204 }
205}
206
207void LdapModel::LdapModelPrivate::gotSearchData( KLDAP::LdapSearch *search,
208 const KLDAP::LdapObject &obj )
209{
210 Q_UNUSED( search );
211 //kDebug();
212 //kDebug() << "Object:";
213 //kDebug() << obj.toString();
214 searchResults().append( obj );
215}
216