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/WOApplication.h>
29 #import <NGObjWeb/WOContext.h>
30 #import <NGObjWeb/WOContext+SoObjects.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGObjWeb/SoObject.h>
33 #import <NGObjWeb/SoUser.h>
34 #import <EOControl/EOSortOrdering.h>
35 #import <SaxObjC/XMLNamespaces.h>
37 #import <SoObjects/SOGo/LDAPSource.h>
38 #import <SoObjects/SOGo/NSString+Utilities.h>
39 #import "SOGoContactLDIFEntry.h"
40 #import "SOGoContactLDAPFolder.h"
41 #import <NGExtensions/NSString+misc.h>
42 #import <NGObjWeb/WORequest.h>
43 #import <NGObjWeb/SoSelectorInvocation.h>
47 @implementation SOGoContactLDAPFolder
49 #warning this should be unified within SOGoFolder
50 - (void) appendObject: (NSDictionary *) object
51 withBaseURL: (NSString *) baseURL
52 toREPORTResponse: (WOResponse *) r
54 SOGoContactLDIFEntry *component;
55 NSString *name, *etagLine, *contactString;
57 name = [object objectForKey: @"c_name"];
58 component = [self lookupName: name inContext: context acquire: NO];
60 [r appendContentString: @" <D:response>\r\n"];
61 [r appendContentString: @" <D:href>"];
62 [r appendContentString: baseURL];
63 if (![baseURL hasSuffix: @"/"])
64 [r appendContentString: @"/"];
65 [r appendContentString: name];
66 [r appendContentString: @"</D:href>\r\n"];
68 [r appendContentString: @" <D:propstat>\r\n"];
69 [r appendContentString: @" <D:prop>\r\n"];
70 etagLine = [NSString stringWithFormat: @" <D:getetag>%@</D:getetag>\r\n",
71 [component davEntityTag]];
72 [r appendContentString: etagLine];
73 [r appendContentString: @" </D:prop>\r\n"];
74 [r appendContentString: @" <D:status>HTTP/1.1 200 OK</D:status>\r\n"];
75 [r appendContentString: @" </D:propstat>\r\n"];
76 [r appendContentString: @" <C:addressbook-data>"];
77 contactString = [[component contentAsString] stringByEscapingXMLString];
78 [r appendContentString: contactString];
79 [r appendContentString: @"</C:addressbook-data>\r\n"];
80 [r appendContentString: @" </D:response>\r\n"];
83 + (id) folderWithName: (NSString *) aName
84 andDisplayName: (NSString *) aDisplayName
85 inContainer: (id) aContainer
89 folder = [[self alloc] initWithName: aName
90 andDisplayName: aDisplayName
91 inContainer: aContainer];
99 if ((self = [super init]))
109 - (id) initWithName: (NSString *) newName
110 andDisplayName: (NSString *) newDisplayName
111 inContainer: (id) newContainer
113 if ((self = [self initWithName: newName
114 inContainer: newContainer]))
116 ASSIGN (displayName, newDisplayName);
124 [displayName release];
126 [ldapSource release];
130 - (void) setLDAPSource: (LDAPSource *) newLDAPSource
132 ASSIGN (ldapSource, newLDAPSource);
135 - (NSString *) displayName
140 - (NSArray *) davNamespaces
142 return [NSArray arrayWithObject: @"urn:ietf:params:xml:ns:carddav"];
145 - (id) lookupName: (NSString *) objectName
146 inContext: (WOContext *) lookupContext
147 acquire: (BOOL) acquire
150 NSDictionary *ldifEntry;
152 //NSLog (@"looking up name '%@'...", objectName);
154 /* first check attributes directly bound to the application */
155 obj = [super lookupName: objectName inContext: lookupContext acquire: NO];
159 ldifEntry = [ldapSource lookupContactEntry: objectName];
161 ? [SOGoContactLDIFEntry contactEntryWithName: objectName
162 withLDIFEntry: ldifEntry
164 : [NSException exceptionWithHTTPStatus: 404]);
170 - (NSArray *) toOneRelationshipKeys
172 return [ldapSource allEntryIDs];
175 - (NSArray *) _flattenedRecords: (NSArray *) records
177 NSMutableArray *newRecords;
178 NSEnumerator *oldRecords;
179 NSDictionary *oldRecord;
180 NSMutableDictionary *newRecord;
183 newRecords = [[NSMutableArray alloc] initWithCapacity: [records count]];
184 [newRecords autorelease];
186 oldRecords = [records objectEnumerator];
187 oldRecord = [oldRecords nextObject];
190 newRecord = [NSMutableDictionary new];
191 [newRecord autorelease];
193 [newRecord setObject: [oldRecord objectForKey: @"c_uid"]
195 [newRecord setObject: [oldRecord objectForKey: @"c_name"]
198 data = [oldRecord objectForKey: @"displayName"];
200 data = [oldRecord objectForKey: @"c_cn"];
203 [newRecord setObject: data forKey: @"displayName"];
205 data = [oldRecord objectForKey: @"mail"];
208 [newRecord setObject: data forKey: @"mail"];
210 data = [oldRecord objectForKey: @"nsAIMid"];
212 data = [oldRecord objectForKey: @"nscpaimscreenname"];
215 [newRecord setObject: data forKey: @"screenName"];
217 data = [oldRecord objectForKey: @"o"];
220 [newRecord setObject: data forKey: @"org"];
222 data = [oldRecord objectForKey: @"telephoneNumber"];
224 data = [oldRecord objectForKey: @"cellphone"];
226 data = [oldRecord objectForKey: @"homePhone"];
229 [newRecord setObject: data forKey: @"phone"];
231 [newRecords addObject: newRecord];
232 oldRecord = [oldRecords nextObject];
238 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
239 sortBy: (NSString *) sortKey
240 ordering: (NSComparisonResult) sortOrdering
242 NSArray *records, *result;
243 EOSortOrdering *ordering;
247 if (filter && [filter length] > 0)
249 records = [self _flattenedRecords:
250 [ldapSource fetchContactsMatching: filter]];
252 = [EOSortOrdering sortOrderingWithKey: sortKey
253 selector: ((sortOrdering == NSOrderedDescending)
254 ? EOCompareCaseInsensitiveDescending
255 : EOCompareCaseInsensitiveAscending)];
257 = [records sortedArrayUsingKeyOrderArray:
258 [NSArray arrayWithObject: ordering]];
264 - (NSArray *) davResourceType
266 NSArray *rType, *groupDavCollection;
268 groupDavCollection = [NSArray arrayWithObjects: @"vcard-collection",
269 XMLNS_GROUPDAV, nil];
270 rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
275 - (NSString *) davContentType
277 return @"httpd/unix-directory";
280 - (BOOL) davIsCollection
285 - (NSString *) davDisplayName
296 - (NSComparisonResult) compare: (id) otherFolder
298 NSComparisonResult comparison;
300 if ([NSStringFromClass([otherFolder class])
301 isEqualToString: @"SOGoContactGCSFolder"])
302 comparison = NSOrderedDescending;
305 = [[self displayName]
306 localizedCaseInsensitiveCompare: [otherFolder displayName]];
312 - (NSString *) ownerInContext: (WOContext *) noContext
317 /* TODO: this might change one day when we support LDAP acls */
318 - (NSArray *) aclsForUser: (NSString *) uid