1 /* SOGoContactLDAPFolder.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGObjWeb/SoObject.h>
29 #import <NGObjWeb/WOApplication.h>
30 #import <NGObjWeb/WOContext.h>
31 #import <NGObjWeb/WOContext+SoObjects.h>
32 #import <NGObjWeb/SoUser.h>
33 #import <EOControl/EOSortOrdering.h>
34 #import <SaxObjC/XMLNamespaces.h>
36 #import <SoObjects/SOGo/LDAPSource.h>
37 #import "SOGoContactLDIFEntry.h"
38 #import "SOGoContactLDAPFolder.h"
42 @implementation SOGoContactLDAPFolder
44 + (id <SOGoContactFolder>) contactFolderWithName: (NSString *) aName
45 andDisplayName: (NSString *) aDisplayName
46 inContainer: (SOGoObject *) aContainer
48 SOGoContactLDAPFolder *folder;
50 folder = [[self alloc] initWithName: aName
51 andDisplayName: aDisplayName
52 inContainer: aContainer];
60 if ((self = [super init]))
65 ignoreSoObjectHunger = NO;
71 - (id <SOGoContactFolder>) initWithName: (NSString *) newName
72 andDisplayName: (NSString *) newDisplayName
73 inContainer: (SOGoObject *) newContainer
75 if ((self = [self initWithName: newName
76 inContainer: newContainer]))
78 ASSIGN (displayName, newDisplayName);
86 [displayName release];
92 - (void) setLDAPSource: (LDAPSource *) newLDAPSource
94 ASSIGN (ldapSource, newLDAPSource);
97 - (NSString *) displayName
102 - (id) lookupName: (NSString *) objectName
103 inContext: (WOContext *) lookupContext
104 acquire: (BOOL) acquire
107 NSDictionary *ldifEntry;
109 // NSLog (@"looking up name '%@'...", name);
111 /* first check attributes directly bound to the application */
112 ignoreSoObjectHunger = YES;
113 obj = [super lookupName: objectName inContext: lookupContext acquire: NO];
114 ignoreSoObjectHunger = NO;
117 ldifEntry = [ldapSource lookupContactEntry: objectName];
119 ? [SOGoContactLDIFEntry contactEntryWithName: objectName
120 withLDIFEntry: ldifEntry
122 : [NSException exceptionWithHTTPStatus: 404]);
128 - (NSArray *) toOneRelationshipKeys
132 if (ignoreSoObjectHunger)
135 keys = [ldapSource allEntryIDs];
140 - (NSArray *) _flattenedRecords: (NSArray *) records
142 NSMutableArray *newRecords;
143 NSEnumerator *oldRecords;
144 NSDictionary *oldRecord;
145 NSMutableDictionary *newRecord;
148 newRecords = [[NSMutableArray alloc] initWithCapacity: [records count]];
149 [newRecords autorelease];
151 oldRecords = [records objectEnumerator];
152 oldRecord = [oldRecords nextObject];
155 newRecord = [NSMutableDictionary new];
156 [newRecord autorelease];
158 [newRecord setObject: [oldRecord objectForKey: @"c_uid"]
160 [newRecord setObject: [oldRecord objectForKey: @"c_name"]
163 data = [oldRecord objectForKey: @"displayName"];
166 [newRecord setObject: data
167 forKey: @"displayName"];
169 data = [oldRecord objectForKey: @"mail"];
172 [newRecord setObject: data forKey: @"mail"];
174 data = [oldRecord objectForKey: @"nsAIMid"];
176 data = [oldRecord objectForKey: @"nscpaimscreenname"];
179 [newRecord setObject: data forKey: @"screenName"];
181 data = [oldRecord objectForKey: @"o"];
184 [newRecord setObject: data forKey: @"org"];
186 data = [oldRecord objectForKey: @"telephoneNumber"];
188 data = [oldRecord objectForKey: @"cellphone"];
190 data = [oldRecord objectForKey: @"homePhone"];
193 [newRecord setObject: data forKey: @"phone"];
195 [newRecords addObject: newRecord];
196 oldRecord = [oldRecords nextObject];
202 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
203 sortBy: (NSString *) sortKey
204 ordering: (NSComparisonResult) sortOrdering
206 NSArray *records, *result;
207 EOSortOrdering *ordering;
211 if (filter && [filter length] > 0)
213 records = [self _flattenedRecords:
214 [ldapSource fetchContactsMatching: filter]];
216 = [EOSortOrdering sortOrderingWithKey: sortKey
217 selector: ((sortOrdering == NSOrderedDescending)
218 ? EOCompareCaseInsensitiveDescending
219 : EOCompareCaseInsensitiveAscending)];
221 = [records sortedArrayUsingKeyOrderArray:
222 [NSArray arrayWithObject: ordering]];
228 - (NSArray *) davResourceType
230 NSArray *rType, *groupDavCollection;
232 groupDavCollection = [NSArray arrayWithObjects: @"vcard-collection",
233 XMLNS_GROUPDAV, nil];
234 rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
239 - (NSString *) davContentType
241 return @"httpd/unix-directory";
244 - (BOOL) davIsCollection
249 - (NSString *) davDisplayName
260 /* TODO: this might change one day when we support LDAP acls */
261 - (NSArray *) aclsForUser: (NSString *) uid