]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactFolders.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1033 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Contacts / SOGoContactFolders.m
1 /* SOGoContactFolders.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 /* exchange folder types:                   */
24 /* MailItems                IPF.Note
25    ContactItems             IPF.Contact
26    AppointmentItems         IPF.Appointment
27    NoteItems                IPF.StickyNote
28    TaskItems                IPF.Task
29    JournalItems             IPF.Journal     */
30
31 #import <Foundation/NSDictionary.h>
32 #import <Foundation/NSString.h>
33
34 #import <NGObjWeb/WOApplication.h>
35 #import <NGObjWeb/WOContext.h>
36 #import <NGObjWeb/WOContext+SoObjects.h>
37 #import <NGObjWeb/SoUser.h>
38
39 #import <GDLContentStore/GCSFolderManager.h>
40 #import <GDLContentStore/GCSChannelManager.h>
41 #import <GDLAccess/EOAdaptorChannel.h>
42 #import <GDLContentStore/NSURL+GCS.h>
43
44 #import <SoObjects/SOGo/SOGoPermissions.h>
45
46 #import "common.h"
47
48 #import "SOGoContactGCSFolder.h"
49 #import "SOGoContactLDAPFolder.h"
50 #import "SOGoContactFolders.h"
51
52 @implementation SOGoContactFolders
53
54 - (id) init
55 {
56   if ((self = [super init]))
57     {
58       contactFolders = nil;
59       OCSPath = nil;
60     }
61
62   return self;
63 }
64
65 - (void) dealloc
66 {
67   if (contactFolders)
68     [contactFolders release];
69   if (OCSPath)
70     [OCSPath release];
71   [super dealloc];
72 }
73
74 - (void) appendPersonalSourcesInContext: (WOContext *) context;
75 {
76   SOGoContactGCSFolder *ab;
77   GCSChannelManager *cm;
78   EOAdaptorChannel *fc;
79   NSURL *folderLocation;
80   NSString *sql;
81   NSArray *attrs;
82   NSDictionary *row;
83
84   cm = [GCSChannelManager defaultChannelManager];
85   folderLocation
86     = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
87   fc = [cm acquireOpenChannelForURL: folderLocation];
88   if (fc)
89     {
90       sql
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];
98       while (row)
99         {
100           ab = [SOGoContactGCSFolder contactFolderWithName: [row objectForKey: @"c_path4"]
101                                      andDisplayName: [row objectForKey: @"c_foldername"]
102                                      inContainer: self];
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];
107         }
108
109       [cm releaseChannel: fc];
110 //       sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'", 
111 //                  uidColumnName, [self uid]];
112     }
113 }
114
115 - (WOResponse *) newFolderWithName: (NSString *) name
116 {
117   SOGoContactGCSFolder *newFolder;
118   WOResponse *response;
119
120   newFolder = [SOGoContactGCSFolder contactFolderWithName: name
121                                     andDisplayName: name
122                                     inContainer: [self clientObject]];
123   if ([newFolder isKindOfClass: [NSException class]])
124     response = (WOResponse *) newFolder;
125   else
126     {
127       [newFolder setOCSPath: [NSString stringWithFormat: @"%@/%@",
128                                        OCSPath, name]];
129       if ([newFolder create])
130         {
131           response = [WOResponse new];
132           [response setStatus: 201];
133           [response autorelease];
134         }
135       else
136         response = [NSException exceptionWithHTTPStatus: 400
137                                 reason: @"The new folder could not be created"];
138     }
139
140   return response;
141 }
142
143 - (void) appendSystemSourcesInContext: (WOContext *) context;
144 {
145   NSUserDefaults *ud;
146   NSEnumerator *ldapABs;
147   NSDictionary *udAB;
148   SOGoContactLDAPFolder *ab;
149
150   ud = [NSUserDefaults standardUserDefaults];
151   ldapABs = [[ud objectForKey: @"SOGoLDAPAddressBooks"] objectEnumerator];
152   udAB = [ldapABs nextObject];
153   while (udAB)
154     {
155       ab = [SOGoContactLDAPFolder contactFolderWithName:
156                                     [udAB objectForKey: @"id"]
157                                   andDisplayName:
158                                     [udAB objectForKey: @"displayName"]
159                                   inContainer: self];
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];
169     }
170 }
171
172 - (void) initContactSourcesInContext: (WOContext *) context;
173 {
174   if (!contactFolders)
175     {
176       contactFolders = [NSMutableDictionary new];
177       [self appendPersonalSourcesInContext: context];
178       [self appendSystemSourcesInContext: context];
179     }
180 }
181
182 - (id) lookupName: (NSString *) name
183         inContext: (WOContext *) context
184           acquire: (BOOL) acquire
185 {
186   id obj;
187   id folder;
188
189   /* first check attributes directly bound to the application */
190   obj = [super lookupName: name inContext: context acquire: NO];
191   if (!obj)
192     {
193       if (!contactFolders)
194         [self initContactSourcesInContext: context];
195
196       folder = [contactFolders objectForKey: name];
197       obj = ((folder)
198              ? folder
199              : [NSException exceptionWithHTTPStatus: 404]);
200     }
201
202   return obj;
203 }
204
205 - (NSArray *) toManyRelationshipKeys
206 {
207   WOContext *context;
208
209   if (!contactFolders)
210     {
211       context = [[WOApplication application] context];
212       [self initContactSourcesInContext: context];
213     }
214
215   return [contactFolders allKeys];
216 }
217
218 - (NSArray *) contactFolders
219 {
220   WOContext *context;
221
222   if (!contactFolders)
223     {
224       context = [[WOApplication application] context];
225       [self initContactSourcesInContext: context];
226     }
227
228   return [contactFolders allValues];
229 }
230
231 - (NSString *) roleOfUser: (NSString *) uid
232                 inContext: (WOContext *) context
233 {
234   NSArray *roles, *traversalPath;
235   NSString *objectName, *role;
236
237   role = nil;
238   traversalPath = [context objectForKey: @"SoRequestTraversalPath"];
239   if ([traversalPath count] > 2)
240     {
241       objectName = [traversalPath objectAtIndex: 2];
242       if ([objectName isEqualToString: @"personal"])
243         {
244           roles = [[context activeUser]
245                     rolesForObject: [self lookupName: objectName
246                                           inContext: context
247                                           acquire: NO]
248                     inContext: context];
249           if ([roles containsObject: SOGoRole_Assistant]
250               || [roles containsObject: SOGoRole_Delegate])
251             role = SOGoRole_Assistant;
252         }
253     }
254
255   return role;
256 }
257
258 - (BOOL) davIsCollection
259 {
260   return YES;
261 }
262
263 - (void) setBaseOCSPath: (NSString *) newOCSPath
264 {
265   if (OCSPath)
266     [OCSPath release];
267   OCSPath = newOCSPath;
268   if (OCSPath)
269     [OCSPath retain];
270 }
271
272 /* web interface */
273 - (NSString *) defaultSourceName
274 {
275   return @"personal";
276 }
277
278 @end