]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactLDAPFolder.m
2704be3321abfd684d775400a5d9cf23ce2fe24f
[scalable-opengroupware.org] / SoObjects / Contacts / SOGoContactLDAPFolder.m
1 /* SOGoContactLDAPFolder.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
26
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>
35
36 #import <SoObjects/SOGo/LDAPSource.h>
37 #import "SOGoContactLDIFEntry.h"
38 #import "SOGoContactLDAPFolder.h"
39
40 @class WOContext;
41
42 @implementation SOGoContactLDAPFolder
43
44 + (id <SOGoContactFolder>) contactFolderWithName: (NSString *) aName
45                                   andDisplayName: (NSString *) aDisplayName
46                                      inContainer: (SOGoObject *) aContainer
47 {
48   SOGoContactLDAPFolder *folder;
49
50   folder = [[self alloc] initWithName: aName
51                          andDisplayName: aDisplayName
52                          inContainer: aContainer];
53   [folder autorelease];
54
55   return folder;
56 }
57
58 - (id) init
59 {
60   if ((self = [super init]))
61     {
62       displayName = nil;
63       entries = nil;
64       ldapSource = nil;
65       ignoreSoObjectHunger = NO;
66     }
67
68   return self;
69 }
70
71 - (id <SOGoContactFolder>) initWithName: (NSString *) newName
72                          andDisplayName: (NSString *) newDisplayName
73                             inContainer: (SOGoObject *) newContainer
74 {
75   if ((self = [self initWithName: newName
76                     inContainer: newContainer]))
77     {
78       ASSIGN (displayName, newDisplayName);
79     }
80
81   return self;
82 }
83
84 - (void) dealloc
85 {
86   [displayName release];
87   [entries release];
88   [ldapSource release];
89   [super dealloc];
90 }
91
92 - (void) setLDAPSource: (LDAPSource *) newLDAPSource
93 {
94   ASSIGN (ldapSource, newLDAPSource);
95 }
96
97 - (NSString *) displayName
98 {
99   return displayName;
100 }
101
102 - (id) lookupName: (NSString *) objectName
103         inContext: (WOContext *) lookupContext
104           acquire: (BOOL) acquire
105 {
106   id obj;
107   NSDictionary *ldifEntry;
108
109 //   NSLog (@"looking up name '%@'...", name);
110
111   /* first check attributes directly bound to the application */
112   ignoreSoObjectHunger = YES;
113   obj = [super lookupName: objectName inContext: lookupContext acquire: NO];
114   ignoreSoObjectHunger = NO;
115   if (!obj)
116     {
117       ldifEntry = [ldapSource lookupContactEntry: objectName];
118       obj = ((ldifEntry)
119              ? [SOGoContactLDIFEntry contactEntryWithName: objectName
120                                      withLDIFEntry: ldifEntry
121                                      inContainer: self]
122              : [NSException exceptionWithHTTPStatus: 404]);
123     }
124
125   return obj;
126 }
127
128 - (NSArray *) toOneRelationshipKeys
129 {
130   NSArray *keys;
131
132   if (ignoreSoObjectHunger)
133     keys = nil;
134   else
135     keys = [ldapSource allEntryIDs];
136
137   return keys;
138 }
139
140 - (NSArray *) _flattenedRecords: (NSArray *) records
141 {
142   NSMutableArray *newRecords;
143   NSEnumerator *oldRecords;
144   NSDictionary *oldRecord;
145   NSMutableDictionary *newRecord;
146   NSString *data;
147   
148   newRecords = [[NSMutableArray alloc] initWithCapacity: [records count]];
149   [newRecords autorelease];
150
151   oldRecords = [records objectEnumerator];
152   oldRecord = [oldRecords nextObject];
153   while (oldRecord)
154     {
155       newRecord = [NSMutableDictionary new];
156       [newRecord autorelease];
157
158       [newRecord setObject: [oldRecord objectForKey: @"c_uid"]
159                  forKey: @"c_uid"];
160       [newRecord setObject: [oldRecord objectForKey: @"c_name"]
161                  forKey: @"c_name"];
162
163       data = [oldRecord objectForKey: @"displayName"];
164       if (!data)
165         data = @"";
166       [newRecord setObject: data
167                  forKey: @"displayName"];
168
169       data = [oldRecord objectForKey: @"mail"];
170       if (!data)
171         data = @"";
172       [newRecord setObject: data forKey: @"mail"];
173
174       data = [oldRecord objectForKey: @"nsAIMid"];
175       if (![data length])
176         data = [oldRecord objectForKey: @"nscpaimscreenname"];
177       if (![data length])
178         data = @"";
179       [newRecord setObject: data forKey: @"screenName"];
180
181       data = [oldRecord objectForKey: @"o"];
182       if (!data)
183         data = @"";
184       [newRecord setObject: data forKey: @"org"];
185
186       data = [oldRecord objectForKey: @"telephoneNumber"];
187       if (![data length])
188         data = [oldRecord objectForKey: @"cellphone"];
189       if (![data length])
190         data = [oldRecord objectForKey: @"homePhone"];
191       if (![data length])
192         data = @"";
193       [newRecord setObject: data forKey: @"phone"];
194
195       [newRecords addObject: newRecord];
196       oldRecord = [oldRecords nextObject];
197     }
198
199   return newRecords;
200 }
201
202 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
203                                 sortBy: (NSString *) sortKey
204                               ordering: (NSComparisonResult) sortOrdering
205 {
206   NSArray *records, *result;
207   EOSortOrdering *ordering;
208
209   result = nil;
210
211   if (filter && [filter length] > 0)
212     {
213       records = [self _flattenedRecords:
214                         [ldapSource fetchContactsMatching: filter]];
215       ordering
216         = [EOSortOrdering sortOrderingWithKey: sortKey
217                           selector: ((sortOrdering == NSOrderedDescending)
218                                      ? EOCompareCaseInsensitiveDescending
219                                      : EOCompareCaseInsensitiveAscending)];
220       result
221         = [records sortedArrayUsingKeyOrderArray:
222                      [NSArray arrayWithObject: ordering]];
223     }
224
225   return result;
226 }
227
228 - (NSArray *) davResourceType
229 {
230   NSArray *rType, *groupDavCollection;
231
232   groupDavCollection = [NSArray arrayWithObjects: @"vcard-collection",
233                                 XMLNS_GROUPDAV, nil];
234   rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
235
236   return rType;
237 }
238
239 - (NSString *) davContentType
240 {
241   return @"httpd/unix-directory";
242 }
243
244 - (BOOL) davIsCollection
245 {
246   return YES;
247 }
248
249 - (NSString *) davDisplayName
250 {
251   return displayName;
252 }
253
254 - (BOOL) isFolderish
255 {
256   return YES;
257 }
258
259 /* acls */
260 /* TODO: this might change one day when we support LDAP acls */
261 - (NSArray *) aclsForUser: (NSString *) uid
262 {
263   return nil;
264 }
265
266 @end