2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGLdapDataSource.h"
23 #include "NGLdapEntry.h"
24 #include "NGLdapAttribute.h"
25 #include "NGLdapConnection.h"
26 #import <NGExtensions/NGFileFolderInfoDataSource.h>
27 #import <EOControl/EOControl.h>
30 @implementation NGLdapDataSource
32 - (id)initWithLdapConnection:(NGLdapConnection *)_con searchBase:(NSString *)_dn{
37 if ((self = [super init])) {
38 self->ldap = [_con retain];
39 self->searchBase = [_dn copy];
45 [self->searchBase release];
46 [self->fspec release];
51 - (void)setFetchSpecification:(EOFetchSpecification *)_fspec {
52 /* should invalidate ds chain */
53 ASSIGN(self->fspec, _fspec);
55 - (EOFetchSpecification *)fetchSpecification {
61 - (NSDictionary *)_recordFromEntry:(NGLdapEntry *)_entry {
62 NSMutableDictionary *md;
70 md = [NSMutableDictionary dictionaryWithCapacity:[_entry count]];
72 if ((tmp = [_entry dn])) {
73 [md setObject:tmp forKey:@"NSFileIdentifier"];
74 [md setObject:tmp forKey:NSFilePath];
76 if ((tmp = [_entry rdn]))
77 [md setObject:tmp forKey:NSFileName];
79 keys = [[_entry attributeNames] objectEnumerator];
81 while ((key = [keys nextObject])) {
82 NGLdapAttribute *attribute;
86 attribute = [_entry attributeWithName:key];
87 count = [attribute count];
90 value = [EONull null];
92 value = [attribute stringValueAtIndex:0];
94 value = [attribute allStringValues];
96 [md setObject:value forKey:key];
99 return [[md copy] autorelease];
104 - (NSArray *)fetchObjects { // TODO: use the new fetch-enumerator
105 NSAutoreleasePool *pool;
107 EOQualifier *qualifier;
108 NSArray *sortOrderings;
111 NSMutableArray *results;
116 pool = [NSAutoreleasePool new];
125 NSString *entityName;
127 qualifier = [self->fspec qualifier];
128 sortOrderings = [self->fspec sortOrderings];
129 scope = [[self->fspec hints] objectForKey:@"NSFetchScope"];
130 attrs = [[self->fspec hints] objectForKey:@"NSFetchKeys"];
132 if ((entityName = [self->fspec entityName])) {
135 oq = [[EOKeyValueQualifier alloc]
136 initWithKey:@"objectclass"
137 operatorSelector:EOQualifierOperatorEqual
142 qa = [NSArray arrayWithObjects:oq, qualifier, nil];
143 qualifier = [[EOAndQualifier alloc] initWithQualifierArray:qa];
144 qualifier = [qualifier autorelease];
145 [oq release]; oq = nil;
148 qualifier = [oq autorelease];
154 static NSArray *so = nil;
157 o = [EOSortOrdering sortOrderingWithKey:@"NSFileIdentifier"
158 selector:EOCompareAscending];
159 so = [[NSArray alloc] initWithObjects:&o count:1];
165 scope = @"NSFetchScopeOneLevel";
167 baseDN = self->searchBase;
169 if ([scope isEqualToString:@"NSFetchScopeOneLevel"]) {
170 e = [self->ldap flatSearchAtBaseDN:baseDN
174 else if ([scope isEqualToString:@"NSFetchScopeSubTree"]) {
175 e = [self->ldap deepSearchAtBaseDN:baseDN
180 [NSException raise:@"NGLdapDataSourceException"
181 format:@"unsupported fetch-scope: '%@' !", scope];
191 /* transform results into records */
193 results = [NSMutableArray arrayWithCapacity:64];
194 while ((entry = [e nextObject])) {
195 NSDictionary *record;
197 if ((record = [self _recordFromEntry:entry]) == nil) {
198 NSLog(@"WARNING: couldn't transform entry %@ into record !", entry);
202 [results addObject:record];
204 array = [[results copy] autorelease];
206 /* apply sort-orderings in-memory */
208 array = [array sortedArrayUsingKeyOrderArray:sortOrderings];
210 array = [array retain];
213 return [array autorelease];
216 - (NSString *)searchBase {
217 return self->searchBase;
221 @end /* NGLdapDataSource */