1 /* SOGoContactFolders.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 /* exchange folder types: */
25 ContactItems IPF.Contact
26 AppointmentItems IPF.Appointment
27 NoteItems IPF.StickyNote
29 JournalItems IPF.Journal */
31 #import <Foundation/NSDictionary.h>
32 #import <Foundation/NSString.h>
34 #import <NGObjWeb/WOApplication.h>
35 #import <NGObjWeb/WOContext.h>
36 #import <NGObjWeb/WOContext+SoObjects.h>
37 #import <NGObjWeb/SoUser.h>
39 #import <GDLContentStore/GCSFolderManager.h>
40 #import <GDLContentStore/GCSChannelManager.h>
41 #import <GDLAccess/EOAdaptorChannel.h>
42 #import <GDLContentStore/NSURL+GCS.h>
44 #import <SoObjects/SOGo/SOGoPermissions.h>
48 #import "SOGoContactGCSFolder.h"
49 #import "SOGoContactLDAPFolder.h"
50 #import "SOGoContactFolders.h"
52 @implementation SOGoContactFolders
56 if ((self = [super init]))
68 [contactFolders release];
74 - (void) appendPersonalSourcesInContext: (WOContext *) context;
76 SOGoContactGCSFolder *ab;
77 GCSChannelManager *cm;
79 NSURL *folderLocation;
84 cm = [GCSChannelManager defaultChannelManager];
86 = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
87 fc = [cm acquireOpenChannelForURL: folderLocation];
91 = [NSString stringWithFormat: @"SELECT c_path4, c_foldername FROM %@"
92 @" WHERE c_path2 = '%@' AND c_folder_type = 'Contact'",
93 [folderLocation gcsTableName],
94 [self ownerInContext: nil]];
95 [fc evaluateExpressionX: sql];
96 attrs = [fc describeResults: NO];
97 row = [fc fetchAttributes: attrs withZone: NULL];
100 ab = [SOGoContactGCSFolder contactFolderWithName: [row objectForKey: @"c_path4"]
101 andDisplayName: [row objectForKey: @"c_foldername"]
103 [ab setOCSPath: [NSString stringWithFormat: @"%@/%@",
104 OCSPath, [row objectForKey: @"c_path4"]]];
105 [contactFolders setObject: ab forKey: [row objectForKey: @"c_path4"]];
106 row = [fc fetchAttributes: attrs withZone: NULL];
109 [cm releaseChannel: fc];
110 // sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'",
111 // uidColumnName, [self uid]];
115 - (WOResponse *) newFolderWithName: (NSString *) name
117 SOGoContactGCSFolder *newFolder;
118 WOResponse *response;
120 newFolder = [SOGoContactGCSFolder contactFolderWithName: name
122 inContainer: [self clientObject]];
123 if ([newFolder isKindOfClass: [NSException class]])
124 response = (WOResponse *) newFolder;
127 [newFolder setOCSPath: [NSString stringWithFormat: @"%@/%@",
129 if ([newFolder create])
131 response = [WOResponse new];
132 [response setStatus: 201];
133 [response autorelease];
136 response = [NSException exceptionWithHTTPStatus: 400
137 reason: @"The new folder could not be created"];
143 - (void) appendSystemSourcesInContext: (WOContext *) context;
146 NSEnumerator *ldapABs;
148 SOGoContactLDAPFolder *ab;
150 ud = [NSUserDefaults standardUserDefaults];
151 ldapABs = [[ud objectForKey: @"SOGoLDAPAddressBooks"] objectEnumerator];
152 udAB = [ldapABs nextObject];
155 ab = [SOGoContactLDAPFolder contactFolderWithName:
156 [udAB objectForKey: @"id"]
158 [udAB objectForKey: @"displayName"]
160 [ab LDAPSetHostname: [udAB objectForKey: @"hostname"]
161 setPort: [[udAB objectForKey: @"port"] intValue]
162 setBindDN: [udAB objectForKey: @"bindDN"]
163 setBindPW: [udAB objectForKey: @"bindPW"]
164 setContactIdentifier: [udAB objectForKey: @"idField"]
165 setUserIdentifier: [udAB objectForKey: @"userIdField"]
166 setRootDN: [udAB objectForKey: @"rootDN"]];
167 [contactFolders setObject: ab forKey: [udAB objectForKey: @"id"]];
168 udAB = [ldapABs nextObject];
172 - (void) initContactSourcesInContext: (WOContext *) context;
176 contactFolders = [NSMutableDictionary new];
177 [self appendPersonalSourcesInContext: context];
178 [self appendSystemSourcesInContext: context];
182 - (id) lookupName: (NSString *) name
183 inContext: (WOContext *) context
184 acquire: (BOOL) acquire
189 /* first check attributes directly bound to the application */
190 obj = [super lookupName: name inContext: context acquire: NO];
194 [self initContactSourcesInContext: context];
196 folder = [contactFolders objectForKey: name];
199 : [NSException exceptionWithHTTPStatus: 404]);
205 - (NSArray *) toManyRelationshipKeys
211 context = [[WOApplication application] context];
212 [self initContactSourcesInContext: context];
215 return [contactFolders allKeys];
218 - (NSArray *) contactFolders
224 context = [[WOApplication application] context];
225 [self initContactSourcesInContext: context];
228 return [contactFolders allValues];
231 - (NSString *) roleOfUser: (NSString *) uid
232 inContext: (WOContext *) context
234 NSArray *roles, *traversalPath;
235 NSString *objectName, *role;
238 traversalPath = [context objectForKey: @"SoRequestTraversalPath"];
239 if ([traversalPath count] > 2)
241 objectName = [traversalPath objectAtIndex: 2];
242 if ([objectName isEqualToString: @"personal"])
244 roles = [[context activeUser]
245 rolesForObject: [self lookupName: objectName
249 if ([roles containsObject: SOGoRole_Assistant]
250 || [roles containsObject: SOGoRole_Delegate])
251 role = SOGoRole_Assistant;
258 - (BOOL) davIsCollection
263 - (void) setBaseOCSPath: (NSString *) newOCSPath
267 OCSPath = newOCSPath;
273 - (NSString *) defaultSourceName