]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactLDAPFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1057 d1b88da0-ebda-0310...
[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
35 #import <SoObjects/SOGo/LDAPSource.h>
36 #import "SOGoContactLDIFEntry.h"
37 #import "SOGoContactLDAPFolder.h"
38
39 #define folderListingFields [NSArray arrayWithObjects: @"c_name", @"cn", \
40                                      @"displayName",                     \
41                                      @"streetAddress",                   \
42                                      @"o", \
43                                      @"sn", @"givenname", @"l",          \
44                                      @"mail", @"telephonenumber",        \
45                                      @"mailNickname",                    \
46                                      @"sAMAccountName",                  \
47                                      @"uid",                  \
48                                      nil]
49
50 @class WOContext;
51
52 @implementation SOGoContactLDAPFolder
53
54 + (id <SOGoContactFolder>) contactFolderWithName: (NSString *) aName
55                                   andDisplayName: (NSString *) aDisplayName
56                                      inContainer: (SOGoObject *) aContainer
57 {
58   SOGoContactLDAPFolder *folder;
59
60   folder = [[self alloc] initWithName: aName
61                          andDisplayName: aDisplayName
62                          inContainer: aContainer];
63   [folder autorelease];
64
65   return folder;
66 }
67
68 - (id) init
69 {
70   if ((self = [super init]))
71     {
72       name = nil;
73       displayName = nil;
74       container = nil;
75       entries = nil;
76       ldapSource = nil;
77     }
78
79   return self;
80 }
81
82 - (id <SOGoContactFolder>) initWithName: (NSString *) newName
83                          andDisplayName: (NSString *) newDisplayName
84                             inContainer: (SOGoObject *) newContainer
85 {
86   self = [self init];
87
88   ASSIGN (name, newName);
89   ASSIGN (displayName, newDisplayName);
90   ASSIGN (container, newContainer);
91
92   return self;
93 }
94
95 - (void) dealloc
96 {
97   [name release];
98   [displayName release];
99   [container release];
100   [entries release];
101   [ldapSource release];
102   [super dealloc];
103 }
104
105 - (void) setLDAPSource: (LDAPSource *) newLDAPSource
106 {
107   ASSIGN (ldapSource, newLDAPSource);
108 }
109
110 - (NSString *) displayName
111 {
112   return displayName;
113 }
114
115 - (NSString *) nameInContainer
116 {
117   return name;
118 }
119
120 - (id) lookupName: (NSString *) objectName
121         inContext: (WOContext *) lookupContext
122           acquire: (BOOL) acquire
123 {
124   id obj;
125   NSDictionary *ldifEntry;
126
127 //   NSLog (@"looking up name '%@'...", name);
128
129   /* first check attributes directly bound to the application */
130   obj = [super lookupName: objectName inContext: lookupContext acquire: NO];
131   if (!obj)
132     {
133       ldifEntry = [ldapSource lookupContactEntry: objectName];
134       obj = ((ldifEntry)
135              ? [SOGoContactLDIFEntry contactEntryWithName: name
136                                      withLDIFEntry: ldifEntry
137                                      inContainer: self]
138              : [NSException exceptionWithHTTPStatus: 404]);
139     }
140
141   return obj;
142 }
143
144 - (NSArray *) toOneRelationshipKeys
145 {
146   return [ldapSource allEntryIDs];
147 }
148
149 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
150                                 sortBy: (NSString *) sortKey
151                               ordering: (NSComparisonResult) sortOrdering
152 {
153   NSArray *records, *result;
154   EOSortOrdering *ordering;
155
156   result = nil;
157
158   if (filter && [filter length] > 0)
159     {
160       records = [ldapSource fetchContactsMatching: filter];
161       ordering
162         = [EOSortOrdering sortOrderingWithKey: sortKey
163                           selector: ((sortOrdering == NSOrderedDescending)
164                                      ? EOCompareCaseInsensitiveDescending
165                                      : EOCompareCaseInsensitiveAscending)];
166       result
167         = [records sortedArrayUsingKeyOrderArray:
168                      [NSArray arrayWithObject: ordering]];
169     }
170
171   //[self debugWithFormat:@"fetched %i records.", [records count]];
172   return result;
173 }
174
175 - (NSString *) groupDavResourceType
176 {
177   return @"vcard-collection";
178 }
179
180 /* acls */
181 /* TODO: this might change one day when we support LDAP acls */
182 - (NSArray *) aclsForUser: (NSString *) uid
183 {
184   return nil;
185 }
186
187 @end