2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGLdapDataSource.h"
24 #include "NGLdapEntry.h"
25 #include "NGLdapAttribute.h"
26 #include "NGLdapConnection.h"
27 #import <NGExtensions/NGFileFolderInfoDataSource.h>
28 #import <EOControl/EOControl.h>
31 @implementation NGLdapDataSource
33 - (id)initWithLdapConnection:(NGLdapConnection *)_con searchBase:(NSString *)_dn{
38 if ((self = [super init])) {
39 self->ldap = [_con retain];
40 self->searchBase = [_dn copy];
46 [self->searchBase release];
47 [self->fspec release];
52 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
53 /* should invalidate ds chain */
54 ASSIGN(self->fspec, _fspec);
56 - (EOFetchSpecification *)fetchSpecification {
62 - (NSDictionary *)_recordFromEntry:(NGLdapEntry *)_entry {
63 NSMutableDictionary *md;
71 md = [NSMutableDictionary dictionaryWithCapacity:[_entry count]];
73 if ((tmp = [_entry dn])) {
74 [md setObject:tmp forKey:@"NSFileIdentifier"];
75 [md setObject:tmp forKey:NSFilePath];
77 if ((tmp = [_entry rdn]))
78 [md setObject:tmp forKey:NSFileName];
80 keys = [[_entry attributeNames] objectEnumerator];
82 while ((key = [keys nextObject])) {
83 NGLdapAttribute *attribute;
87 attribute = [_entry attributeWithName:key];
88 count = [attribute count];
91 value = [EONull null];
93 value = [attribute stringValueAtIndex:0];
95 value = [attribute allStringValues];
97 [md setObject:value forKey:key];
100 return [[md copy] autorelease];
105 - (NSArray *)fetchObjects { // TODO: use the new fetch-enumerator
106 NSAutoreleasePool *pool;
108 EOQualifier *qualifier;
109 NSArray *sortOrderings;
112 NSMutableArray *results;
117 pool = [NSAutoreleasePool new];
126 NSString *entityName;
128 qualifier = [self->fspec qualifier];
129 sortOrderings = [self->fspec sortOrderings];
130 scope = [[self->fspec hints] objectForKey:@"NSFetchScope"];
131 attrs = [[self->fspec hints] objectForKey:@"NSFetchKeys"];
133 if ((entityName = [self->fspec entityName])) {
136 oq = [[EOKeyValueQualifier alloc]
137 initWithKey:@"objectclass"
138 operatorSelector:EOQualifierOperatorEqual
143 qa = [NSArray arrayWithObjects:oq, qualifier, nil];
144 qualifier = [[EOAndQualifier alloc] initWithQualifierArray:qa];
145 qualifier = [qualifier autorelease];
146 [oq release]; oq = nil;
149 qualifier = [oq autorelease];
155 static NSArray *so = nil;
158 o = [EOSortOrdering sortOrderingWithKey:@"NSFileIdentifier"
159 selector:EOCompareAscending];
160 so = [[NSArray alloc] initWithObjects:&o count:1];
166 scope = @"NSFetchScopeOneLevel";
168 baseDN = self->searchBase;
170 if ([scope isEqualToString:@"NSFetchScopeOneLevel"]) {
171 e = [self->ldap flatSearchAtBaseDN:baseDN
175 else if ([scope isEqualToString:@"NSFetchScopeSubTree"]) {
176 e = [self->ldap deepSearchAtBaseDN:baseDN
181 [NSException raise:@"NGLdapDataSourceException"
182 format:@"unsupported fetch-scope: '%@' !", scope];
192 /* transform results into records */
194 results = [NSMutableArray arrayWithCapacity:64];
195 while ((entry = [e nextObject])) {
196 NSDictionary *record;
198 if ((record = [self _recordFromEntry:entry]) == nil) {
199 NSLog(@"WARNING: couldn't transform entry %@ into record !", entry);
203 [results addObject:record];
205 array = [[results copy] autorelease];
207 /* apply sort-orderings in-memory */
209 array = [array sortedArrayUsingKeyOrderArray:sortOrderings];
211 array = [array retain];
214 return [array autorelease];
217 - (NSString *)searchBase {
218 return self->searchBase;
222 @end /* NGLdapDataSource */