]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactFolders.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1046 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) initContactSources
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 *) lookupContext
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: lookupContext acquire: NO];
191   if (!obj)
192     {
193       if (!contactFolders)
194         [self initContactSources];
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   if (!contactFolders)
208     [self initContactSources];
209
210   return [contactFolders allKeys];
211 }
212
213 - (NSArray *) contactFolders
214 {
215   if (!contactFolders)
216     [self initContactSources];
217
218   return [contactFolders allValues];
219 }
220
221 // - (NSString *) roleOfUser: (NSString *) uid
222 // {
223 //   NSArray *roles, *traversalPath;
224 //   NSString *objectName, *role;
225
226 //   role = nil;
227 //   traversalPath = [context objectForKey: @"SoRequestTraversalPath"];
228 //   if ([traversalPath count] > 2)
229 //     {
230 //       objectName = [traversalPath objectAtIndex: 2];
231 //       roles = [[context activeUser]
232 //              rolesForObject: [self lookupName: objectName
233 //                                    inContext: context
234 //                                    acquire: NO]
235 //              inContext: context];
236 //       if ([roles containsObject: SOGoRole_Assistant]
237 //        || [roles containsObject: SOGoRole_Delegate])
238 //      role = SOGoRole_Assistant;
239 //     }
240
241 //   return role;
242 // }
243
244 - (BOOL) davIsCollection
245 {
246   return YES;
247 }
248
249 - (void) setBaseOCSPath: (NSString *) newOCSPath
250 {
251   if (OCSPath)
252     [OCSPath release];
253   OCSPath = newOCSPath;
254   if (OCSPath)
255     [OCSPath retain];
256 }
257
258 /* web interface */
259 - (NSString *) defaultSourceName
260 {
261   return @"personal";
262 }
263
264 @end