]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactFolders.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1057 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/NSException+HTTP.h>
35 #import <NGObjWeb/WOApplication.h>
36 #import <NGObjWeb/WOContext.h>
37 #import <NGObjWeb/WOContext+SoObjects.h>
38 #import <NGObjWeb/WOResponse.h>
39 #import <NGObjWeb/SoUser.h>
40
41 #import <GDLContentStore/GCSFolderManager.h>
42 #import <GDLContentStore/GCSChannelManager.h>
43 #import <GDLAccess/EOAdaptorChannel.h>
44 #import <GDLContentStore/NSURL+GCS.h>
45
46 #import <SoObjects/SOGo/LDAPUserManager.h>
47 #import <SoObjects/SOGo/SOGoPermissions.h>
48
49 #import "SOGoContactGCSFolder.h"
50 #import "SOGoContactLDAPFolder.h"
51 #import "SOGoContactFolders.h"
52
53 @implementation SOGoContactFolders
54
55 - (id) init
56 {
57   if ((self = [super init]))
58     {
59       contactFolders = nil;
60       OCSPath = nil;
61     }
62
63   return self;
64 }
65
66 - (void) dealloc
67 {
68   if (contactFolders)
69     [contactFolders release];
70   if (OCSPath)
71     [OCSPath release];
72   [super dealloc];
73 }
74
75 - (void) appendPersonalSources
76 {
77   SOGoContactGCSFolder *ab;
78   GCSChannelManager *cm;
79   EOAdaptorChannel *fc;
80   NSURL *folderLocation;
81   NSString *sql;
82   NSArray *attrs;
83   NSDictionary *row;
84
85   cm = [GCSChannelManager defaultChannelManager];
86   folderLocation
87     = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
88   fc = [cm acquireOpenChannelForURL: folderLocation];
89   if (fc)
90     {
91       sql = [NSString
92               stringWithFormat: (@"SELECT c_path4, c_foldername FROM %@"
93                                  @" WHERE c_path2 = '%@'"
94                                  @" AND c_folder_type = 'Contact'"),
95               [folderLocation gcsTableName], [self ownerInContext: context]];
96       [fc evaluateExpressionX: sql];
97       attrs = [fc describeResults: NO];
98       row = [fc fetchAttributes: attrs withZone: NULL];
99       while (row)
100         {
101           ab = [SOGoContactGCSFolder contactFolderWithName: [row objectForKey: @"c_path4"]
102                                      andDisplayName: [row objectForKey: @"c_foldername"]
103                                      inContainer: self];
104           [ab setOCSPath: [NSString stringWithFormat: @"%@/%@",
105                                     OCSPath, [row objectForKey: @"c_path4"]]];
106           [contactFolders setObject: ab forKey: [row objectForKey: @"c_path4"]];
107           row = [fc fetchAttributes: attrs withZone: NULL];
108         }
109
110       [cm releaseChannel: fc];
111 //       sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'", 
112 //                  uidColumnName, [self uid]];
113     }
114 }
115
116 - (void) appendSystemSources
117 {
118   LDAPUserManager *um;
119   NSEnumerator *sourceIDs;
120   NSString *currentSourceID, *displayName;
121   SOGoContactLDAPFolder *currentFolder;
122
123   um = [LDAPUserManager sharedUserManager];
124   sourceIDs = [[um addressBookSourceIDs] objectEnumerator]; 
125   currentSourceID = [sourceIDs nextObject];
126   while (currentSourceID)
127     {
128       displayName = [um displayNameForSourceWithID: currentSourceID];
129       currentFolder = [SOGoContactLDAPFolder contactFolderWithName: currentSourceID
130                                              andDisplayName: displayName
131                                              inContainer: self];
132       [currentFolder setLDAPSource: [um sourceWithID: currentSourceID]];
133       [contactFolders setObject: currentFolder forKey: currentSourceID];
134       currentSourceID = [sourceIDs nextObject];
135     }
136 }
137
138 - (WOResponse *) newFolderWithName: (NSString *) name
139 {
140   SOGoContactGCSFolder *newFolder;
141   WOResponse *response;
142
143   newFolder = [SOGoContactGCSFolder contactFolderWithName: name
144                                     andDisplayName: name
145                                     inContainer: self];
146   if ([newFolder isKindOfClass: [NSException class]])
147     response = (WOResponse *) newFolder;
148   else
149     {
150       [newFolder setOCSPath: [NSString stringWithFormat: @"%@/%@",
151                                        OCSPath, name]];
152       if ([newFolder create])
153         {
154           response = [WOResponse new];
155           [response setStatus: 201];
156           [response autorelease];
157         }
158       else
159         response = [NSException exceptionWithHTTPStatus: 400
160                                 reason: @"The new folder could not be created"];
161     }
162
163   return response;
164 }
165
166 - (void) initContactSources
167 {
168   if (!contactFolders)
169     {
170       contactFolders = [NSMutableDictionary new];
171       [self appendPersonalSources];
172       [self appendSystemSources];
173     }
174 }
175
176 - (id) lookupName: (NSString *) name
177         inContext: (WOContext *) lookupContext
178           acquire: (BOOL) acquire
179 {
180   id obj;
181
182   /* first check attributes directly bound to the application */
183   obj = [super lookupName: name inContext: lookupContext acquire: NO];
184   if (!obj)
185     {
186       if (!contactFolders)
187         [self initContactSources];
188
189       obj = [contactFolders objectForKey: name];
190       if (!obj)
191         obj = [NSException exceptionWithHTTPStatus: 404];
192     }
193
194   return obj;
195 }
196
197 - (NSArray *) toManyRelationshipKeys
198 {
199   if (!contactFolders)
200     [self initContactSources];
201
202   return [contactFolders allKeys];
203 }
204
205 - (NSArray *) contactFolders
206 {
207   if (!contactFolders)
208     [self initContactSources];
209
210   return [contactFolders allValues];
211 }
212
213 /* acls */
214 - (NSArray *) aclsForUser: (NSString *) uid
215 {
216   return nil;
217 }
218
219 // - (NSString *) roleOfUser: (NSString *) uid
220 // {
221 //   NSArray *roles, *traversalPath;
222 //   NSString *objectName, *role;
223
224 //   role = nil;
225 //   traversalPath = [context objectForKey: @"SoRequestTraversalPath"];
226 //   if ([traversalPath count] > 2)
227 //     {
228 //       objectName = [traversalPath objectAtIndex: 2];
229 //       roles = [[context activeUser]
230 //              rolesForObject: [self lookupName: objectName
231 //                                    inContext: context
232 //                                    acquire: NO]
233 //              inContext: context];
234 //       if ([roles containsObject: SOGoRole_Assistant]
235 //        || [roles containsObject: SOGoRole_Delegate])
236 //      role = SOGoRole_Assistant;
237 //     }
238
239 //   return role;
240 // }
241
242 - (BOOL) davIsCollection
243 {
244   return YES;
245 }
246
247 - (void) setBaseOCSPath: (NSString *) newOCSPath
248 {
249   if (OCSPath)
250     [OCSPath release];
251   OCSPath = newOCSPath;
252   if (OCSPath)
253     [OCSPath retain];
254 }
255
256 /* web interface */
257 - (NSString *) defaultSourceName
258 {
259   return @"personal";
260 }
261
262 @end